xref: /haiku/src/apps/mediaplayer/media_node_framework/PlaybackManager.h (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
1 /*
2  * Copyright (c) 2000-2008, Ingo Weinhold <ingo_weinhold@gmx.de>,
3  * Copyright (c) 2000-2008, Stephan Aßmus <superstippi@gmx.de>,
4  * All Rights Reserved. Distributed under the terms of the MIT license.
5  */
6 
7 /*!	This class controls our playback
8 	Note: Add/RemoveListener() are the only methods that lock the object
9 	themselves. All other methods need it to be locked before.
10 */
11 #ifndef PLAYBACK_MANAGER_H
12 #define PLAYBACK_MANAGER_H
13 
14 
15 #include <List.h>
16 #include <Looper.h>
17 #include <Rect.h>
18 
19 
20 class PlaybackListener;
21 
22 
23 enum {
24 	MODE_PLAYING_FORWARD			= 1,
25 	MODE_PLAYING_BACKWARD			= 2,
26 	MODE_PLAYING_PAUSED_FORWARD		= -1,
27 	MODE_PLAYING_PAUSED_BACKWARD	= -2,
28 };
29 
30 enum {
31 	LOOPING_ALL						= 0,
32 	LOOPING_RANGE					= 1,
33 	LOOPING_SELECTION				= 2,
34 	LOOPING_VISIBLE					= 3,
35 };
36 
37 enum {
38 	MSG_PLAYBACK_FORCE_UPDATE		= 'pbfu',
39 	MSG_PLAYBACK_SET_RANGE			= 'pbsr',
40 	MSG_PLAYBACK_SET_VISIBLE		= 'pbsv',
41 	MSG_PLAYBACK_SET_LOOP_MODE		= 'pbsl',
42 };
43 
44 
45 class PlaybackManager : public BLooper {
46 private:
47 			struct PlayingState;
48 			struct SpeedInfo;
49 
50 public:
51 								PlaybackManager();
52 	virtual						~PlaybackManager();
53 
54 			void				Init(float frameRate,
55 									 int32 loopingMode = LOOPING_ALL,
56 									 bool loopingEnabled = true,
57 									 float speed = 1.0,
58 									 int32 playMode = MODE_PLAYING_PAUSED_FORWARD,
59 									 int32 currentFrame = 0);
60 			void				Cleanup();
61 
62 	// BHandler interface
63 	virtual	void				MessageReceived(BMessage* message);
64 
65 			void				StartPlaying(bool atBeginning = false);
66 			void				StopPlaying();
67 			void				TogglePlaying(bool atBeginning = false);
68 			void				PausePlaying();
69 			bool				IsPlaying() const;
70 
71 			int32				PlayMode() const;
72 			int32				LoopMode() const;
73 			bool				IsLoopingEnabled() const;
74 			int32				CurrentFrame() const;
75 			float				Speed() const;
76 
77 	virtual	void				SetFramesPerSecond(float framesPerSecond);
78 			float				FramesPerSecond() const;
79 
80 	virtual	BRect				VideoBounds() const = 0;
81 
82 	virtual	void				FrameDropped() const;
83 
84 			void				DurationChanged();
85 	virtual	int64				Duration() = 0;
86 
87 	virtual	void				SetVolume(float percent) = 0;
88 
89 			// playing state manipulation
90 			void				SetCurrentFrame(int64 frame);
91 	virtual	void				SetPlayMode(int32 mode,
92 									bool continuePlaying = true);
93 			void				SetLoopMode(int32 mode,
94 									bool continuePlaying = true);
95 			void				SetLoopingEnabled(bool enabled,
96 									bool continuePlaying = true);
97 			void				SetSpeed(float speed);
98 
99 			// playing state change info
100 			int64				NextFrame() const;
101 			int64				NextPlaylistFrame() const;
102 
103 			int64				FirstPlaybackRangeFrame();
104 			int64				LastPlaybackRangeFrame();
105 
106 			// playing state access
107 			int64				StartFrameAtFrame(int64 frame);
108 			int64				StartFrameAtTime(bigtime_t time);
109 			int64				EndFrameAtFrame(int64 frame);
110 			int64				EndFrameAtTime(bigtime_t time);
111 			int64				FrameCountAtFrame(int64 frame);
112 			int64				FrameCountAtTime(bigtime_t time);
113 			int32				PlayModeAtFrame(int64 frame);
114 			int32				PlayModeAtTime(bigtime_t time);
115 			int32				LoopModeAtFrame(int64 frame);
116 			int32				LoopModeAtTime(bigtime_t time);
117 			// ...
118 
119 			// playing frame/time/interval info
120 			int64				PlaylistFrameAtFrame(int64 frame,
121 									int32& playingDirection,
122 									bool& newState) const;
123 			int64				PlaylistFrameAtFrame(int64 frame,
124 									int32& playingDirection) const;
125 			int64				PlaylistFrameAtFrame(int64 frame) const;
126 			int64				NextChangeFrame(int64 startFrame,
127 												int64 endFrame) const;
128 			bigtime_t			NextChangeTime(bigtime_t startTime,
129 											   bigtime_t endTime) const;
130 			void				GetPlaylistFrameInterval(
131 									int64 startFrame, int64& endFrame,
132 									int64& xStartFrame, int64& xEndFrame,
133 									int32& playingDirection) const;
134 
135 	// PlaybackManagerInterface
136 	virtual	void				GetPlaylistTimeInterval(
137 									bigtime_t startTime, bigtime_t& endTime,
138 									bigtime_t& xStartTime, bigtime_t& xEndTime,
139 									float& playingSpeed) const;
140 
141 			// conversion: video frame <-> (performance) time
142 			int64				FrameForTime(bigtime_t time) const;
143 			bigtime_t			TimeForFrame(int64 frame) const;
144 			// conversion: (performance) time <-> real time
145 	virtual	bigtime_t			RealTimeForTime(bigtime_t time) const = 0;
146 	virtual	bigtime_t			TimeForRealTime(bigtime_t time) const = 0;
147 			// conversion: Playist frame <-> Playlist time
148 			int64				PlaylistFrameForTime(bigtime_t time) const;
149 			bigtime_t			PlaylistTimeForFrame(int64 frame) const;
150 
151 			// to be called by audio/video producers
152 	virtual	void				SetCurrentAudioTime(bigtime_t time);
153 			void				SetCurrentVideoFrame(int64 frame);
154 			void				SetCurrentVideoTime(bigtime_t time);
155 			void				SetPerformanceFrame(int64 frame);
156 			void				SetPerformanceTime(bigtime_t time);
157 
158 			// listener support
159 			void				AddListener(PlaybackListener* listener);
160 			void				RemoveListener(PlaybackListener* listener);
161 
162 	virtual	void				NotifyPlayModeChanged(int32 mode) const;
163 	virtual	void				NotifyLoopModeChanged(int32 mode) const;
164 	virtual	void				NotifyLoopingEnabledChanged(
165 									bool enabled) const;
166 	virtual	void				NotifyVideoBoundsChanged(BRect bounds) const;
167 	virtual	void				NotifyFPSChanged(float fps) const;
168 	virtual	void				NotifyCurrentFrameChanged(int32 frame) const;
169 	virtual	void				NotifySpeedChanged(float speed) const;
170 	virtual	void				NotifyFrameDropped() const;
171 	virtual	void				NotifyStopFrameReached() const;
172 	virtual	void				NotifySeekHandled() const;
173 
174 			// debugging
175 			void				PrintState(PlayingState* state);
176 			void				PrintStateAtFrame(int64 frame);
177 
178  private:
179 			// state management
180 			void				_PushState(PlayingState* state,
181 										   bool adjustCurrentFrame);
182 			void				_UpdateStates();
183 			int32				_IndexForFrame(int64 frame) const;
184 			int32				_IndexForTime(bigtime_t time) const;
185 			PlayingState*		_LastState() const;
186 			PlayingState*		_StateAt(int32 index) const;
187 			PlayingState*		_StateAtTime(bigtime_t time) const;
188 			PlayingState*		_StateAtFrame(int64 frame) const;
189 
190 	static	int32				_PlayingDirectionFor(int32 playingMode);
191 	static	int32				_PlayingDirectionFor(PlayingState* state);
192 	static	void				_GetPlayingBoundsFor(PlayingState* state,
193 													 int64& startFrame,
194 													 int64& endFrame,
195 													 int64& frameCount);
196 	static	int64				_PlayingStartFrameFor(PlayingState* state);
197 	static	int64				_PlayingEndFrameFor(PlayingState* state);
198 	static	int64				_RangeFrameForFrame(PlayingState* state,
199 													int64 frame);
200 	static	int64				_FrameForRangeFrame(PlayingState* state,
201 													int64 index);
202 	static	int64				_NextFrameInRange(PlayingState* state,
203 												  int64 frame);
204 
205 			// speed management
206 			void				_PushSpeedInfo(SpeedInfo* info);
207 			SpeedInfo*			_LastSpeedInfo() const;
208 			SpeedInfo*			_SpeedInfoAt(int32 index) const;
209 			int32				_SpeedInfoIndexForFrame(int64 frame) const;
210 			int32				_SpeedInfoIndexForTime(bigtime_t time) const;
211 			SpeedInfo*			_SpeedInfoForFrame(int64 frame) const;
212 			SpeedInfo*			_SpeedInfoForTime(bigtime_t time) const;
213 			void				_UpdateSpeedInfos();
214 
215 			bigtime_t			_TimeForLastFrame() const;
216 			bigtime_t			_TimeForNextFrame() const;
217 
218 			void				_CheckStopPlaying();
219 
220  private:
221 			BList				fStates;
222 			BList				fSpeeds;
223 	volatile bigtime_t			fCurrentAudioTime;
224 	volatile bigtime_t			fCurrentVideoTime;
225 	volatile bigtime_t			fPerformanceTime;
226 	volatile float				fFrameRate;			// video frame rate
227 	volatile bigtime_t			fStopPlayingFrame;	// frame at which playing
228 													// shall be stopped,
229 													// disabled: -1
230 
231 			BList				fListeners;
232  protected:
233  			bool				fNoAudio;
234 };
235 
236 
237 #endif	// PLAYBACK_MANAGER_H
238