xref: /haiku/src/apps/mediaplayer/Controller.h (revision b028e77473189065f2baefc6f5e10d451cf591e2)
1 /*
2  * Controller.h - Media Player for the Haiku Operating System
3  *
4  * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de>
5  * Copyright (C) 2007 Stephan Aßmus <superstippi@gmx.de>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  *
20  */
21 #ifndef __CONTROLLER_H
22 #define __CONTROLLER_H
23 
24 #include <Entry.h>
25 #include <MediaDefs.h>
26 #include <MediaFormats.h>
27 #include <MediaNode.h>
28 #include <List.h>
29 #include <Locker.h>
30 #include <String.h>
31 
32 class AudioSupplier;
33 class BBitmap;
34 class BMediaFile;
35 class BMediaTrack;
36 class SoundOutput;
37 class VideoSupplier;
38 class VideoView;
39 
40 class Controller {
41 public:
42 
43 	class Listener {
44 	public:
45 							Listener();
46 		virtual				~Listener();
47 
48 		virtual	void		FileFinished();
49 		virtual	void		FileChanged();
50 
51 		virtual	void		VideoTrackChanged(int32 index);
52 		virtual	void		AudioTrackChanged(int32 index);
53 
54 		virtual	void		VideoStatsChanged();
55 		virtual	void		AudioStatsChanged();
56 
57 		virtual	void		PlaybackStateChanged(uint32 state);
58 		virtual	void		PositionChanged(float position);
59 		virtual	void		VolumeChanged(float volume);
60 	};
61 
62 							Controller();
63 	virtual 				~Controller();
64 
65 	bool					Lock();
66 	status_t				LockWithTimeout(bigtime_t timeout);
67 	void					Unlock();
68 
69 	status_t				SetTo(const entry_ref &ref);
70 	void					GetSize(int *width, int *height);
71 
72 	int						AudioTrackCount();
73 	int						VideoTrackCount();
74 
75 	status_t				SelectAudioTrack(int n);
76 	status_t				SelectVideoTrack(int n);
77 
78 	void					Stop();
79 	void					Play();
80 	void					Pause();
81 	void					TogglePlaying();
82 
83 	bool					IsPaused() const;
84 	bool					IsStopped() const;
85 	uint32					PlaybackState() const;
86 
87 	bigtime_t				Duration();
88 	bigtime_t				Position();
89 
90 	void					SetVolume(float value);
91 	float					Volume() const;
92 	void					SetPosition(float value);
93 
94 	bool					HasFile();
95 	status_t				GetFileFormatInfo(media_file_format* fileFormat);
96 	status_t				GetCopyright(BString* copyright);
97 	status_t				GetLocation(BString* location);
98 	status_t				GetName(BString* name);
99 	status_t				GetEncodedVideoFormat(media_format* format);
100 	status_t				GetVideoCodecInfo(media_codec_info* info);
101 	status_t				GetEncodedAudioFormat(media_format* format);
102 	status_t				GetAudioCodecInfo(media_codec_info* info);
103 
104 	// video view
105 	void					SetVideoView(VideoView *view);
106 
107 	bool					IsOverlayActive();
108 
109 	bool					LockBitmap();
110 	void					UnlockBitmap();
111 	BBitmap *				Bitmap();
112 
113 	// notification support
114 	bool					AddListener(Listener* listener);
115 	void					RemoveListener(Listener* listener);
116 
117 private:
118 	void					_AudioDecodeThread();
119 	void					_AudioPlayThread();
120 
121 	void					_VideoDecodeThread();
122 	void					_VideoPlayThread();
123 
124 	void					_StartThreads();
125 	void					_StopThreads();
126 
127 	void					_EndOfStreamReached(bool isVideo = false);
128 	void					_UpdatePosition(bigtime_t position,
129 								bool isVideoPosition = false,
130 								bool force = false);
131 
132 	static int32			_VideoDecodeThreadEntry(void *self);
133 	static int32			_VideoPlayThreadEntry(void *self);
134 	static int32			_AudioDecodeThreadEntry(void *self);
135 	static int32			_AudioPlayThreadEntry(void *self);
136 
137 private:
138 	void					_NotifyFileChanged();
139 	void					_NotifyFileFinished();
140 	void					_NotifyVideoTrackChanged(int32 index);
141 	void					_NotifyAudioTrackChanged(int32 index);
142 
143 	void					_NotifyVideoStatsChanged();
144 	void					_NotifyAudioStatsChanged();
145 
146 	void					_NotifyPlaybackStateChanged();
147 	void					_NotifyPositionChanged(float position);
148 	void					_NotifyVolumeChanged(float volume);
149 
150 	friend class InfoWin;
151 
152 	enum {
153 		MAX_AUDIO_BUFFERS = 8,
154 		MAX_VIDEO_BUFFERS = 3,
155 	};
156 
157 	struct buffer_info {
158 		char *	 		buffer;
159 		BBitmap *		bitmap;
160 		size_t			sizeUsed;
161 		size_t			sizeMax;
162 		bigtime_t		startTime;
163 		bool			formatChanged;
164 		bool			endOfStream;
165 		media_format	mediaFormat;
166 	};
167 
168 	VideoView *				fVideoView;
169 	volatile bool			fPaused;
170 	volatile bool			fStopped;
171 	float					fVolume;
172 
173 	entry_ref				fRef;
174 	BMediaFile *			fMediaFile;
175 	mutable BLocker			fDataLock;
176 
177 	VideoSupplier*			fVideoSupplier;
178 	AudioSupplier*			fAudioSupplier;
179 
180 	BLocker					fVideoSupplierLock;
181 	BLocker					fAudioSupplierLock;
182 
183 	BList *					fAudioTrackList;
184 	BList *					fVideoTrackList;
185 	media_format			fAudioFormat;
186 	media_format			fVideoFormat;
187 
188 	sem_id					fAudioDecodeSem;
189 	sem_id					fVideoDecodeSem;
190 	sem_id					fAudioPlaySem;
191 	sem_id					fVideoPlaySem;
192 	sem_id					fAudioWaitSem;
193 	sem_id					fVideoWaitSem;
194 	thread_id				fAudioDecodeThread;
195 	thread_id				fVideoDecodeThread;
196 	thread_id				fAudioPlayThread;
197 	thread_id				fVideoPlayThread;
198 
199 	SoundOutput	*			fSoundOutput;
200 	volatile bool			fSeekAudio;
201 	volatile bool			fSeekVideo;
202 	volatile bigtime_t		fSeekPosition;
203 	bigtime_t				fPosition;
204 	bigtime_t				fDuration;
205 
206 	int32					fAudioBufferCount;
207 	int32					fAudioBufferReadIndex;
208 	int32					fAudioBufferWriteIndex;
209 	int32					fVideoBufferCount;
210 	int32					fVideoBufferReadIndex;
211 	int32					fVideoBufferWriteIndex;
212 
213 	buffer_info				fAudioBuffer[MAX_AUDIO_BUFFERS];
214 	buffer_info				fVideoBuffer[MAX_VIDEO_BUFFERS];
215 
216 	BLocker					fTimeSourceLock;
217 	bigtime_t				fTimeSourceSysTime;
218 	bigtime_t				fTimeSourcePerfTime;
219 	bool 					fAutoplay;
220 	volatile bool			fPauseAtEndOfStream;
221 	volatile bool			fSeekToStartAfterPause;
222 
223 	BBitmap *				fCurrentBitmap;
224 	BLocker					fBitmapLock;
225 
226 	BList					fListeners;
227 };
228 
229 
230 #endif
231 
232