xref: /haiku/src/add-ons/media/media-add-ons/dvb/DVBMediaNode.h (revision 2f470aec1c92ce6917b8a903e343795dc77af41f)
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 
25 #ifndef __DVB_MEDIA_NODE_H
26 #define __DVB_MEDIA_NODE_H
27 
28 #include <kernel/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 PacketQueue;
43 class DVBCard;
44 
45 class DVBMediaNode :
46 	public virtual BBufferProducer,
47 	public virtual BControllable,
48 	public virtual BMediaEventLooper
49 {
50 public:
51 						DVBMediaNode(BMediaAddOn *addon,
52 								const char *name, int32 internal_id, DVBCard *card);
53 virtual					~DVBMediaNode();
54 
55 virtual	status_t		InitCheck() const { return fInitStatus; }
56 
57 /* BMediaNode */
58 public:
59 virtual	BMediaAddOn	*AddOn(int32 * internal_id) const;
60 virtual	status_t 	HandleMessage(int32 message, const void *data,
61 							size_t size);
62 protected:
63 virtual	void 		Preroll();
64 virtual void		SetTimeSource(BTimeSource * time_source);
65 virtual void		SetRunMode(run_mode mode);
66 
67 /* BMediaEventLooper */
68 protected:
69 virtual	void 		NodeRegistered();
70 virtual void		Stop(bigtime_t performance_time, bool immediate);
71 
72 virtual void		HandleEvent(const media_timed_event *event,
73 							bigtime_t lateness, bool realTimeEvent = false);
74 
75 /* BBufferProducer */
76 protected:
77 virtual	status_t	FormatSuggestionRequested(media_type type, int32 quality,
78 							media_format * format);
79 virtual	status_t 	FormatProposal(const media_source &source,
80 							media_format *format);
81 virtual	status_t	FormatChangeRequested(const media_source &source,
82 							const media_destination &destination,
83 							media_format *io_format, int32 *_deprecated_);
84 virtual	status_t 	GetNextOutput(int32 * cookie, media_output * out_output);
85 virtual	status_t	DisposeOutputCookie(int32 cookie);
86 virtual	status_t	SetBufferGroup(const media_source &for_source,
87 							BBufferGroup * group);
88 virtual	status_t 	VideoClippingChanged(const media_source &for_source,
89 							int16 num_shorts, int16 *clip_data,
90 							const media_video_display_info &display,
91 							int32 * _deprecated_);
92 virtual	status_t	GetLatency(bigtime_t * out_latency);
93 virtual	status_t	PrepareToConnect(const media_source &what,
94 							const media_destination &where,
95 							media_format *format,
96 							media_source *out_source, char *out_name);
97 virtual	void		Connect(status_t error, const media_source &source,
98 							const media_destination &destination,
99 							const media_format & format, char *io_name);
100 virtual	void 		Disconnect(const media_source & what,
101 							const media_destination & where);
102 virtual	void 		LateNoticeReceived(const media_source & what,
103 							bigtime_t how_much, bigtime_t performance_time);
104 virtual	void 		EnableOutput(const media_source & what, bool enabled,
105 							int32 * _deprecated_);
106 virtual	status_t	SetPlayRate(int32 numer,int32 denom);
107 virtual	void 		AdditionalBufferRequested(const media_source & source,
108 							media_buffer_id prev_buffer, bigtime_t prev_time,
109 							const media_seek_tag * prev_tag);
110 virtual	void		LatencyChanged(const media_source & source,
111 							const media_destination & destination,
112 							bigtime_t new_latency, uint32 flags);
113 
114 /* BControllable */
115 protected:
116 virtual status_t	GetParameterValue(int32 id, bigtime_t *last_change,
117 							void *value, size_t *size);
118 virtual void		SetParameterValue(int32 id, bigtime_t when,
119 							const void *value, size_t size);
120 
121 /* state */
122 private:
123 		void				HandleStart(bigtime_t performance_time);
124 		void				HandleStop();
125 		void				HandleTimeWarp(bigtime_t performance_time);
126 		void				HandleSeek(bigtime_t performance_time);
127 
128 		void				InitDefaultFormats();
129 		void				SpecializeFormatRawVideo(media_raw_video_format *format);
130 		void				SpecializeFormatRawAudio(media_multi_audio_format *format);
131 		void				SpecializeFormatEncVideo(media_encoded_video_format *format);
132 		void				SpecializeFormatEncAudio(media_encoded_audio_format *format);
133 		void				SpecializeFormatTS(media_multistream_format *format);
134 
135 		bool				VerifyFormatRawVideo(const media_raw_video_format &format);
136 		bool				VerifyFormatRawAudio(const media_multi_audio_format &format);
137 
138 		status_t			GetStreamFormat(PacketQueue *queue, media_format *format);
139 
140 		status_t			SetOutput(const media_source &source, const media_destination &destination, const media_format &format);
141 		status_t			ResetOutput(const media_source &source);
142 		const char *		SourceDefaultName(const media_source &source);
143 
144 		BParameterWeb *		CreateParameterWeb();
145 		void				SetAboutInfo(BParameterGroup *about);
146 
147 
148 		void				RefreshParameterWeb();
149 
150 		void				LoadSettings();
151 
152 		void				AddStateItems(BDiscreteParameter *param);
153 		void				AddRegionItems(BDiscreteParameter *param);
154 		void				AddChannelItems(BDiscreteParameter *param);
155 		void				AddAudioItems(BDiscreteParameter *param);
156 
157 		void				RefreshStateList();
158 		void				RefreshRegionList();
159 		void				RefreshChannelList();
160 		void				RefreshAudioList();
161 
162 		status_t			StartCapture();
163 		status_t			StopCapture();
164 		status_t			StartCaptureThreads();
165 		status_t			StopCaptureThreads();
166 		status_t			Tune();
167 
168 		status_t			ExtractTuningParams(const char *description, int audio_pid_index,
169 												dvb_tuning_parameters_t *param,
170 												int *video_pid, int *audio_pid, int *pcr_pid);
171 
172 		static int32		_card_reader_thread_(void *arg);
173 		static int32		_mpeg_demux_thread_(void *arg);
174 		static int32		_enc_audio_thread_(void *arg);
175 		static int32		_enc_video_thread_(void *arg);
176 		static int32		_mpeg_ts_thread_(void *arg);
177 		static int32		_raw_audio_thread_(void *arg);
178 		static int32		_raw_video_thread_(void *arg);
179 
180 		void				card_reader_thread();
181 		void				mpeg_demux_thread();
182 		void				enc_audio_thread();
183 		void				enc_video_thread();
184 		void				mpeg_ts_thread();
185 		void				raw_audio_thread();
186 		void				raw_video_thread();
187 
188 		status_t			GetNextVideoChunk(const void **chunkData, size_t *chunkLen, media_header *mh);
189 		status_t			GetNextAudioChunk(const void **chunkData, size_t *chunkLen, media_header *mh);
190 
191 		static status_t		_GetNextVideoChunk(const void **chunkData, size_t *chunkLen, media_header *mh, void *cookie);
192 		static status_t		_GetNextAudioChunk(const void **chunkData, size_t *chunkLen, media_header *mh, void *cookie);
193 
194 		status_t			fInitStatus;
195 
196 		int32				fInternalID;
197 		BMediaAddOn			*fAddOn;
198 
199 		bool				fStopDisabled;
200 
201 		bool				fOutputEnabledRawVideo;
202 		bool				fOutputEnabledRawAudio;
203 		bool				fOutputEnabledEncVideo;
204 		bool				fOutputEnabledEncAudio;
205 		bool				fOutputEnabledTS;
206 
207 		media_output		fOutputRawVideo;
208 		media_output		fOutputRawAudio;
209 		media_output		fOutputEncVideo;
210 		media_output		fOutputEncAudio;
211 		media_output		fOutputTS;
212 
213 		media_format		fDefaultFormatRawVideo;
214 		media_format		fDefaultFormatRawAudio;
215 		media_format		fDefaultFormatEncVideo;
216 		media_format		fDefaultFormatEncAudio;
217 		media_format		fDefaultFormatTS;
218 
219 		media_format		fRequiredFormatRawVideo;
220 		media_format		fRequiredFormatRawAudio;
221 		media_format		fRequiredFormatEncVideo;
222 		media_format		fRequiredFormatEncAudio;
223 		media_format		fRequiredFormatTS;
224 
225 		PacketQueue *		fCardDataQueue; // holds MPEG TS data read from the card
226 		PacketQueue *		fRawVideoQueue; // holds encoded PES frames for video decoding
227 		PacketQueue *		fRawAudioQueue; // holds encoded PES frames for audio decoding
228 		PacketQueue *		fEncVideoQueue; // holds encoded PES frames for enc video out
229 		PacketQueue *		fEncAudioQueue; // holds encoded PES frames for enc audio out
230 		PacketQueue *		fMpegTsQueue; 	// holds encoded PES frames for MPEG TS out
231 
232 		DVBCard *			fCard;
233 
234 		bool				fCaptureThreadsActive;
235 
236 		thread_id			fThreadIdCardReader;
237 		thread_id			fThreadIdMpegDemux;
238 		thread_id			fThreadIdRawAudio;
239 		thread_id			fThreadIdRawVideo;
240 		thread_id			fThreadIdEncAudio;
241 		thread_id			fThreadIdEncVideo;
242 		thread_id			fThreadIdMpegTS;
243 
244 		volatile bool		fTerminateThreads;
245 
246 		TransportStreamDemux * fDemux;
247 
248 		BBufferGroup *		fBufferGroupRawVideo;
249 		BBufferGroup *		fBufferGroupRawAudio;
250 
251 		BParameterWeb *		fWeb;
252 
253 	dvb_tuning_parameters_t fTuningParam;
254 	dvb_type_t				fInterfaceType;
255 
256 		int					fAudioPid;
257 		int					fVideoPid;
258 		int					fPcrPid;
259 
260 		bool				fTuningSuccess;
261 		bool				fCaptureActive;
262 
263 		sem_id				fVideoDelaySem;
264 		sem_id				fAudioDelaySem;
265 
266 		int					fSelectedState;
267 		int					fSelectedRegion;
268 		int					fSelectedChannel;
269 		int					fSelectedAudio;
270 
271 		StringList *		fStateList;
272 		StringList *		fRegionList;
273 		StringList *		fChannelList;
274 		StringList *		fAudioList;
275 
276 		MediaStreamDecoder *fVideoDecoder;
277 		MediaStreamDecoder *fAudioDecoder;
278 
279 		Packet *			fCurrentVideoPacket;
280 		Packet *			fCurrentAudioPacket;
281 
282 		BLocker lock;
283 
284 		// used only for debugging:
285 		int					fVideoFile;
286 		int					fAudioFile;
287 		int					fRawAudioFile;
288 };
289 
290 #endif
291