xref: /haiku/src/add-ons/media/media-add-ons/dvb/DVBMediaNode.h (revision e3857211d305a595c2d0b58768f25623d5967675)
1 /*
2  * Copyright (c) 2004-2007 Marcus Overhagen <marcus@overhagen.de>
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify,
8  * merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef __DVB_MEDIA_NODE_H
25 #define __DVB_MEDIA_NODE_H
26 
27 
28 #include <OS.h>
29 #include <media/BufferProducer.h>
30 #include <media/Controllable.h>
31 #include <media/MediaDefs.h>
32 #include <media/MediaEventLooper.h>
33 #include <media/MediaNode.h>
34 #include <support/Locker.h>
35 
36 #include "TransportStreamDemux.h"
37 #include "MediaStreamDecoder.h"
38 #include "DVBCard.h"
39 #include "StringList.h"
40 
41 class BDiscreteParameter;
42 class BParameterGroup;
43 class PacketQueue;
44 class DVBCard;
45 
46 
47 class DVBMediaNode : public virtual BBufferProducer,
48 	public virtual BControllable, public virtual BMediaEventLooper {
49 public:
50 						DVBMediaNode(BMediaAddOn *addon,
51 								const char *name, int32 internal_id, DVBCard *card);
52 virtual					~DVBMediaNode();
53 
54 virtual	status_t		InitCheck() const { return fInitStatus; }
55 
56 /* BMediaNode */
57 public:
58 virtual	BMediaAddOn	*AddOn(int32 * internal_id) const;
59 virtual	status_t 	HandleMessage(int32 message, const void *data,
60 							size_t size);
61 protected:
62 virtual	void 		Preroll();
63 virtual void		SetTimeSource(BTimeSource * time_source);
64 virtual void		SetRunMode(run_mode mode);
65 
66 /* BMediaEventLooper */
67 protected:
68 virtual	void 		NodeRegistered();
69 virtual void		Stop(bigtime_t performance_time, bool immediate);
70 
71 virtual void		HandleEvent(const media_timed_event *event,
72 							bigtime_t lateness, bool realTimeEvent = false);
73 
74 /* BBufferProducer */
75 protected:
76 virtual	status_t	FormatSuggestionRequested(media_type type, int32 quality,
77 							media_format * format);
78 virtual	status_t 	FormatProposal(const media_source &source,
79 							media_format *format);
80 virtual	status_t	FormatChangeRequested(const media_source &source,
81 							const media_destination &destination,
82 							media_format *io_format, int32 *_deprecated_);
83 virtual	status_t 	GetNextOutput(int32 * cookie, media_output * out_output);
84 virtual	status_t	DisposeOutputCookie(int32 cookie);
85 virtual	status_t	SetBufferGroup(const media_source &for_source,
86 							BBufferGroup * group);
87 virtual	status_t 	VideoClippingChanged(const media_source &for_source,
88 							int16 num_shorts, int16 *clip_data,
89 							const media_video_display_info &display,
90 							int32 * _deprecated_);
91 virtual	status_t	GetLatency(bigtime_t * out_latency);
92 virtual	status_t	PrepareToConnect(const media_source &what,
93 							const media_destination &where,
94 							media_format *format,
95 							media_source *out_source, char *out_name);
96 virtual	void		Connect(status_t error, const media_source &source,
97 							const media_destination &destination,
98 							const media_format & format, char *io_name);
99 virtual	void 		Disconnect(const media_source & what,
100 							const media_destination & where);
101 virtual	void 		LateNoticeReceived(const media_source & what,
102 							bigtime_t how_much, bigtime_t performance_time);
103 virtual	void 		EnableOutput(const media_source & what, bool enabled,
104 							int32 * _deprecated_);
105 virtual	status_t	SetPlayRate(int32 numer,int32 denom);
106 virtual	void 		AdditionalBufferRequested(const media_source & source,
107 							media_buffer_id prev_buffer, bigtime_t prev_time,
108 							const media_seek_tag * prev_tag);
109 virtual	void		LatencyChanged(const media_source & source,
110 							const media_destination & destination,
111 							bigtime_t new_latency, uint32 flags);
112 
113 /* BControllable */
114 protected:
115 virtual status_t	GetParameterValue(int32 id, bigtime_t *last_change,
116 							void *value, size_t *size);
117 virtual void		SetParameterValue(int32 id, bigtime_t when,
118 							const void *value, size_t size);
119 
120 /* state */
121 private:
122 		void				HandleStart(bigtime_t performance_time);
123 		void				HandleStop();
124 		void				HandleTimeWarp(bigtime_t performance_time);
125 		void				HandleSeek(bigtime_t performance_time);
126 
127 		void				InitDefaultFormats();
128 		void				SpecializeFormatRawVideo(media_raw_video_format *format);
129 		void				SpecializeFormatRawAudio(media_multi_audio_format *format);
130 		void				SpecializeFormatEncVideo(media_encoded_video_format *format);
131 		void				SpecializeFormatEncAudio(media_encoded_audio_format *format);
132 		void				SpecializeFormatTS(media_multistream_format *format);
133 
134 		bool				VerifyFormatRawVideo(const media_raw_video_format &format);
135 		bool				VerifyFormatRawAudio(const media_multi_audio_format &format);
136 
137 		status_t			GetStreamFormat(PacketQueue *queue, media_format *format);
138 
139 		status_t			SetOutput(const media_source &source, const media_destination &destination, const media_format &format);
140 		status_t			ResetOutput(const media_source &source);
141 		const char *		SourceDefaultName(const media_source &source);
142 
143 		BParameterWeb *		CreateParameterWeb();
144 		void				SetAboutInfo(BParameterGroup *about);
145 
146 
147 		void				RefreshParameterWeb();
148 
149 		void				LoadSettings();
150 
151 		void				AddStateItems(BDiscreteParameter *param);
152 		void				AddRegionItems(BDiscreteParameter *param);
153 		void				AddChannelItems(BDiscreteParameter *param);
154 		void				AddAudioItems(BDiscreteParameter *param);
155 
156 		void				RefreshStateList();
157 		void				RefreshRegionList();
158 		void				RefreshChannelList();
159 		void				RefreshAudioList();
160 
161 		status_t			StartCapture();
162 		status_t			StopCapture();
163 		status_t			StartCaptureThreads();
164 		status_t			StopCaptureThreads();
165 		status_t			Tune();
166 
167 		status_t			ExtractTuningParams(const char *description, int audio_pid_index,
168 												dvb_tuning_parameters_t *param,
169 												int *video_pid, int *audio_pid, int *pcr_pid);
170 
171 		static int32		_card_reader_thread_(void *arg);
172 		static int32		_mpeg_demux_thread_(void *arg);
173 		static int32		_enc_audio_thread_(void *arg);
174 		static int32		_enc_video_thread_(void *arg);
175 		static int32		_mpeg_ts_thread_(void *arg);
176 		static int32		_raw_audio_thread_(void *arg);
177 		static int32		_raw_video_thread_(void *arg);
178 
179 		void				card_reader_thread();
180 		void				mpeg_demux_thread();
181 		void				enc_audio_thread();
182 		void				enc_video_thread();
183 		void				mpeg_ts_thread();
184 		void				raw_audio_thread();
185 		void				raw_video_thread();
186 
187 		status_t			GetNextVideoChunk(const void **chunkData, size_t *chunkLen, media_header *mh);
188 		status_t			GetNextAudioChunk(const void **chunkData, size_t *chunkLen, media_header *mh);
189 
190 		static status_t		_GetNextVideoChunk(const void **chunkData, size_t *chunkLen, media_header *mh, void *cookie);
191 		static status_t		_GetNextAudioChunk(const void **chunkData, size_t *chunkLen, media_header *mh, void *cookie);
192 
193 		status_t			fInitStatus;
194 
195 		int32				fInternalID;
196 		BMediaAddOn			*fAddOn;
197 
198 		bool				fStopDisabled;
199 
200 		bool				fOutputEnabledRawVideo;
201 		bool				fOutputEnabledRawAudio;
202 		bool				fOutputEnabledEncVideo;
203 		bool				fOutputEnabledEncAudio;
204 		bool				fOutputEnabledTS;
205 
206 		media_output		fOutputRawVideo;
207 		media_output		fOutputRawAudio;
208 		media_output		fOutputEncVideo;
209 		media_output		fOutputEncAudio;
210 		media_output		fOutputTS;
211 
212 		media_format		fDefaultFormatRawVideo;
213 		media_format		fDefaultFormatRawAudio;
214 		media_format		fDefaultFormatEncVideo;
215 		media_format		fDefaultFormatEncAudio;
216 		media_format		fDefaultFormatTS;
217 
218 		media_format		fRequiredFormatRawVideo;
219 		media_format		fRequiredFormatRawAudio;
220 		media_format		fRequiredFormatEncVideo;
221 		media_format		fRequiredFormatEncAudio;
222 		media_format		fRequiredFormatTS;
223 
224 		PacketQueue *		fCardDataQueue; // holds MPEG TS data read from the card
225 		PacketQueue *		fRawVideoQueue; // holds encoded PES frames for video decoding
226 		PacketQueue *		fRawAudioQueue; // holds encoded PES frames for audio decoding
227 		PacketQueue *		fEncVideoQueue; // holds encoded PES frames for enc video out
228 		PacketQueue *		fEncAudioQueue; // holds encoded PES frames for enc audio out
229 		PacketQueue *		fMpegTsQueue; 	// holds encoded PES frames for MPEG TS out
230 
231 		DVBCard *			fCard;
232 
233 		bool				fCaptureThreadsActive;
234 
235 		thread_id			fThreadIdCardReader;
236 		thread_id			fThreadIdMpegDemux;
237 		thread_id			fThreadIdRawAudio;
238 		thread_id			fThreadIdRawVideo;
239 		thread_id			fThreadIdEncAudio;
240 		thread_id			fThreadIdEncVideo;
241 		thread_id			fThreadIdMpegTS;
242 
243 		volatile bool		fTerminateThreads;
244 
245 		TransportStreamDemux * fDemux;
246 
247 		BBufferGroup *		fBufferGroupRawVideo;
248 		BBufferGroup *		fBufferGroupRawAudio;
249 
250 		BParameterWeb *		fWeb;
251 
252 	dvb_tuning_parameters_t fTuningParam;
253 	dvb_type_t				fInterfaceType;
254 
255 		int					fAudioPid;
256 		int					fVideoPid;
257 		int					fPcrPid;
258 
259 		bool				fTuningSuccess;
260 		bool				fCaptureActive;
261 
262 		sem_id				fVideoDelaySem;
263 		sem_id				fAudioDelaySem;
264 
265 		int					fSelectedState;
266 		int					fSelectedRegion;
267 		int					fSelectedChannel;
268 		int					fSelectedAudio;
269 
270 		StringList *		fStateList;
271 		StringList *		fRegionList;
272 		StringList *		fChannelList;
273 		StringList *		fAudioList;
274 
275 		MediaStreamDecoder *fVideoDecoder;
276 		MediaStreamDecoder *fAudioDecoder;
277 
278 		Packet *			fCurrentVideoPacket;
279 		Packet *			fCurrentAudioPacket;
280 
281 		BLocker lock;
282 
283 		// used only for debugging:
284 		int					fVideoFile;
285 		int					fAudioFile;
286 		int					fRawAudioFile;
287 		int					fMpegTsFile;
288 };
289 
290 #endif	// __DVB_MEDIA_NODE_H
291