Mercurial > mplayer.hg
annotate libmpdemux/demux_nemesi.c @ 24990:fd919e77c834
Support mute when passthrough to digital output.
author | ulion |
---|---|
date | Sun, 11 Nov 2007 02:54:57 +0000 |
parents | 4365a0d7a4b8 |
children | e429129fa02b |
rev | line source |
---|---|
24564 | 1 /* |
2 * Copyright (C) 2007 Alessandro Molina <amol.wrk@gmail.com> | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with MPlayer; if not, write to the Free Software Foundation, | |
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 */ | |
18 | |
19 #include "demux_nemesi.h" | |
20 #include "stheader.h" | |
21 #define HAVE_STRUCT_SOCKADDR_STORAGE | |
22 #include "nemesi/rtsp.h" | |
23 #include "nemesi/rtp.h" | |
24856 | 24 #include <sched.h> |
24564 | 25 |
26 int rtsp_transport_tcp = 0; | |
27 int rtsp_transport_sctp = 0; | |
28 // extern int rtsp_port; | |
29 | |
30 typedef enum { NEMESI_SESSION_VIDEO, | |
31 NEMESI_SESSION_AUDIO } Nemesi_SessionType; | |
32 | |
33 typedef struct { | |
34 rtsp_ctrl * rtsp; | |
35 rtp_session * session[2]; | |
36 rtp_frame first_pkt[2]; | |
37 double time[2]; | |
38 double seek; | |
39 } Nemesi_DemuxerStreamData; | |
40 | |
41 static void link_session_and_fetch_conf(Nemesi_DemuxerStreamData * ndsd, | |
42 Nemesi_SessionType stype, | |
43 rtp_session * sess, | |
44 rtp_buff * buff, unsigned int * fps) | |
45 { | |
46 extern float force_fps; | |
24855
2c790baff42c
Update to use newer libnemesi, should fix desync, fps guessing may fail now
lu_zero
parents:
24625
diff
changeset
|
47 rtp_ssrc *ssrc = NULL; |
24564 | 48 rtsp_ctrl * ctl = ndsd->rtsp; |
49 rtp_frame * fr = &ndsd->first_pkt[stype]; | |
50 rtp_buff trash_buff; | |
51 | |
52 ndsd->session[stype] = sess; | |
53 | |
54 if (buff == NULL) | |
55 buff = &trash_buff; | |
56 | |
57 if ( (buff != NULL) || (fps != NULL) ) { | |
24856 | 58 while ( !(ssrc = rtp_session_get_ssrc(sess, ctl)) ) //Wait for the ssrc to be registered |
59 sched_yield(); | |
60 | |
61 rtp_fill_buffer(ssrc, fr, buff); //Prefetch the first packet | |
62 | |
63 while ( !(rtp_get_pkt(ssrc, NULL)) ) //Wait for the second packet to calculate FPS | |
64 sched_yield(); | |
24564 | 65 |
66 if ( (force_fps == 0.0) && (fps != NULL) ) { | |
67 rtp_fill_buffers(rtsp_get_rtp_th(ctl)); | |
68 *fps = rtp_get_fps(ssrc); | |
69 } | |
70 } | |
71 } | |
72 | |
73 demuxer_t* demux_open_rtp(demuxer_t* demuxer) | |
74 { | |
75 nms_rtsp_hints hints; | |
76 char * url = demuxer->stream->streaming_ctrl->url->url; | |
77 rtsp_ctrl * ctl; | |
78 RTSP_Error reply; | |
79 rtsp_medium * media; | |
80 Nemesi_DemuxerStreamData * ndsd = calloc(1, sizeof(Nemesi_DemuxerStreamData)); | |
81 | |
82 memset(&hints,0,sizeof(hints)); | |
83 // if (rtsp_port) hints.first_rtp_port = rtsp_port; | |
84 if (rtsp_transport_tcp) { | |
85 hints.pref_rtsp_proto = TCP; | |
86 hints.pref_rtp_proto = TCP; | |
87 } | |
88 if (rtsp_transport_sctp) { | |
89 hints.pref_rtsp_proto = SCTP; | |
90 hints.pref_rtp_proto = SCTP; | |
91 } | |
92 | |
93 mp_msg(MSGT_DEMUX, MSGL_INFO, "Initializing libNemesi\n"); | |
94 if ((ctl = rtsp_init(&hints)) == NULL) { | |
95 free(ndsd); | |
96 return STREAM_ERROR; | |
97 } | |
98 | |
99 ndsd->rtsp = ctl; | |
100 demuxer->priv = ndsd; | |
101 //nms_verbosity_set(1); | |
102 | |
103 mp_msg(MSGT_DEMUX, MSGL_INFO, "Opening: %s\n", url); | |
104 if (rtsp_open(ctl, url)) { | |
105 mp_msg(MSGT_DEMUX, MSGL_ERR, "rtsp_open failed.\n"); | |
106 return demuxer; | |
107 } | |
108 | |
109 reply = rtsp_wait(ctl); | |
110 if (reply.got_error) { | |
111 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
112 "OPEN Error from the server: %s\n", | |
113 reply.message.reply_str); | |
114 return demuxer; | |
115 } | |
116 | |
117 rtsp_play(ctl, 0, 0); | |
118 reply = rtsp_wait(ctl); | |
119 if (reply.got_error) { | |
120 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
121 "PLAY Error from the server: %s\n", | |
122 reply.message.reply_str); | |
123 return demuxer; | |
124 } | |
125 | |
126 media = ctl->rtsp_queue->media_queue; | |
127 for (; media; media=media->next) { | |
128 sdp_medium_info * info = media->medium_info; | |
129 rtp_session * sess = media->rtp_sess; | |
130 | |
131 int media_format = atoi(info->fmts); | |
132 rtp_pt * ptinfo = rtp_get_pt_info(sess, media_format); | |
133 char const * format_name = ptinfo ? ptinfo->name : NULL; | |
134 | |
135 if (sess->parsers[media_format] == NULL) { | |
136 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
137 "libNemesi unsupported media format: %s\n", | |
138 format_name ? format_name : info->fmts); | |
139 continue; | |
140 } | |
141 else { | |
142 mp_msg(MSGT_DEMUX, MSGL_INFO, | |
143 "libNemesi supported media: %s\n", | |
144 format_name); | |
145 } | |
146 | |
147 if (ptinfo->type == AU) { | |
148 if (ndsd->session[NEMESI_SESSION_AUDIO] == NULL) { | |
149 sh_audio_t* sh_audio = new_sh_audio(demuxer,0); | |
150 WAVEFORMATEX* wf = calloc(1,sizeof(WAVEFORMATEX)); | |
151 demux_stream_t* d_audio = demuxer->audio; | |
152 | |
153 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as AUDIO stream...\n"); | |
154 | |
155 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_AUDIO, | |
156 sess, NULL, NULL); | |
157 | |
158 sh_audio->wf = wf; | |
159 d_audio->sh = sh_audio; | |
160 sh_audio->ds = d_audio; | |
161 wf->nSamplesPerSec = 0; | |
162 | |
163 //List of known audio formats | |
164 if (!strcmp(format_name, "MPA")) | |
165 wf->wFormatTag = | |
166 sh_audio->format = 0x55; | |
167 else if (!strcmp(format_name, "vorbis")) | |
168 wf->wFormatTag = | |
169 sh_audio->format = mmioFOURCC('v','r','b','s'); | |
170 else | |
171 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
172 "Unknown MPlayer format code for MIME" | |
173 " type \"audio/%s\"\n", format_name); | |
174 } else { | |
175 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
176 "There is already an audio session registered," | |
177 " ignoring...\n"); | |
178 } | |
179 } else if (ptinfo->type == VI) { | |
180 if (ndsd->session[NEMESI_SESSION_AUDIO] == NULL) { | |
181 sh_video_t* sh_video; | |
182 BITMAPINFOHEADER* bih; | |
183 demux_stream_t* d_video; | |
184 int fps = 0; | |
185 rtp_buff buff; | |
24855
2c790baff42c
Update to use newer libnemesi, should fix desync, fps guessing may fail now
lu_zero
parents:
24625
diff
changeset
|
186 memset(&buff, 0, sizeof(rtp_buff)); |
24564 | 187 |
188 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as VIDEO stream...\n"); | |
189 | |
190 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_VIDEO, | |
191 sess, &buff, &fps); | |
192 | |
193 if (buff.len) { | |
194 bih = calloc(1,sizeof(BITMAPINFOHEADER)+buff.len); | |
195 bih->biSize = sizeof(BITMAPINFOHEADER)+buff.len; | |
196 memcpy(bih+1, buff.data, buff.len); | |
197 } else { | |
198 bih = calloc(1,sizeof(BITMAPINFOHEADER)); | |
199 bih->biSize = sizeof(BITMAPINFOHEADER); | |
200 } | |
201 | |
202 sh_video = new_sh_video(demuxer,0); | |
203 sh_video->bih = bih; | |
204 d_video = demuxer->video; | |
205 d_video->sh = sh_video; | |
206 sh_video->ds = d_video; | |
207 | |
24856 | 208 if (fps) { |
24564 | 209 sh_video->fps = fps; |
24856 | 210 sh_video->frametime = 1.0/fps; |
211 } | |
24564 | 212 |
213 //List of known video formats | |
214 if (!strcmp(format_name, "MPV")) { | |
215 bih->biCompression = | |
216 sh_video->format = mmioFOURCC('M','P','E','G'); | |
217 } else if (!strcmp(format_name, "H264")) { | |
218 bih->biCompression = | |
219 sh_video->format = mmioFOURCC('H','2','6','4'); | |
24625 | 220 } else if (!strcmp(format_name, "H263-1998")) { |
221 bih->biCompression = | |
222 sh_video->format = mmioFOURCC('H','2','6','3'); | |
24564 | 223 } else { |
224 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
225 "Unknown MPlayer format code for MIME" | |
226 " type \"video/%s\"\n", format_name); | |
227 } | |
228 } else { | |
229 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
230 "There is already a video session registered," | |
231 " ignoring...\n"); | |
232 } | |
233 } else { | |
234 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported media type\n"); | |
235 } | |
236 } | |
237 | |
238 demuxer->stream->eof = 0; | |
239 | |
240 return demuxer; | |
241 } | |
242 | |
243 static int get_data_for_session(Nemesi_DemuxerStreamData * ndsd, | |
244 Nemesi_SessionType stype, rtp_frame * fr) | |
245 { | |
246 rtsp_ctrl * ctl = ndsd->rtsp; | |
247 rtp_ssrc *ssrc = NULL; | |
248 | |
249 for (ssrc = rtp_active_ssrc_queue(rtsp_get_rtp_queue(ctl)); | |
250 ssrc; | |
251 ssrc = rtp_next_active_ssrc(ssrc)) { | |
252 if (ssrc->rtp_sess == ndsd->session[stype]) { | |
253 if (ndsd->first_pkt[stype].len != 0) { | |
254 fr->data = ndsd->first_pkt[stype].data; | |
255 fr->time_sec = ndsd->first_pkt[stype].time_sec; | |
256 fr->len = ndsd->first_pkt[stype].len; | |
257 ndsd->first_pkt[stype].len = 0; | |
258 return RTP_FILL_OK; | |
259 } else { | |
260 rtp_buff buff; | |
261 return rtp_fill_buffer(ssrc, fr, &buff); | |
262 } | |
263 } | |
264 } | |
265 | |
266 return RTP_SSRC_NOTVALID; | |
267 } | |
268 | |
269 int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds) | |
270 { | |
271 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
272 Nemesi_SessionType stype; | |
273 rtsp_ctrl * ctl = ndsd->rtsp; | |
274 rtp_thread * rtp_th = rtsp_get_rtp_th(ctl); | |
275 rtp_frame fr; | |
276 | |
277 demux_packet_t* dp; | |
278 | |
279 if ( (!ctl->rtsp_queue) || (demuxer->stream->eof) || (rtp_fill_buffers(rtp_th)) ) { | |
280 mp_msg(MSGT_DEMUX, MSGL_INFO, "End of Stream...\n"); | |
281 demuxer->stream->eof = 1; | |
282 return 0; | |
283 } | |
284 | |
285 if (ds == demuxer->video) | |
286 stype = NEMESI_SESSION_VIDEO; | |
287 else if (ds == demuxer->audio) | |
288 stype = NEMESI_SESSION_AUDIO; | |
289 else | |
290 return 0; | |
291 | |
292 if(!get_data_for_session(ndsd, stype, &fr)) { | |
293 dp = new_demux_packet(fr.len); | |
294 memcpy(dp->buffer, fr.data, fr.len); | |
295 fr.time_sec += ndsd->seek; | |
296 ndsd->time[stype] = dp->pts = fr.time_sec; | |
297 ds_add_packet(ds, dp); | |
298 } | |
299 else { | |
300 stype = (stype + 1) % 2; | |
301 if (stype == NEMESI_SESSION_VIDEO) | |
302 ds = demuxer->video; | |
303 else | |
304 ds = demuxer->audio; | |
305 | |
306 if(!get_data_for_session(ndsd, stype, &fr)) { | |
307 dp = new_demux_packet(fr.len); | |
308 memcpy(dp->buffer, fr.data, fr.len); | |
309 fr.time_sec += ndsd->seek; | |
310 ndsd->time[stype] = dp->pts = fr.time_sec; | |
311 ds_add_packet(ds, dp); | |
312 } | |
313 } | |
314 | |
315 return 1; | |
316 } | |
317 | |
318 | |
319 void demux_close_rtp(demuxer_t* demuxer) | |
320 { | |
321 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
322 rtsp_ctrl * ctl = ndsd->rtsp; | |
323 RTSP_Error err; | |
324 | |
325 mp_msg(MSGT_DEMUX, MSGL_INFO, "Closing libNemesi RTSP Stream...\n"); | |
326 | |
327 if (ndsd == NULL) | |
328 return; | |
329 | |
330 free(ndsd); | |
331 | |
332 if (rtsp_close(ctl)) { | |
333 err = rtsp_wait(ctl); | |
334 if (err.got_error) | |
335 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
336 "Error Closing Stream: %s\n", | |
337 err.message.reply_str); | |
338 } | |
339 | |
340 rtsp_uninit(ctl); | |
341 } | |
342 | |
343 static void demux_seek_rtp(demuxer_t *demuxer, float rel_seek_secs, | |
344 float audio_delay, int flags) | |
345 { | |
346 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
347 rtsp_ctrl * ctl = ndsd->rtsp; | |
348 sdp_attr * r_attr = NULL; | |
349 sdp_range r = {0, 0}; | |
350 double time = ndsd->time[NEMESI_SESSION_VIDEO] ? | |
351 ndsd->time[NEMESI_SESSION_VIDEO] : | |
352 ndsd->time[NEMESI_SESSION_AUDIO]; | |
353 | |
354 if (!ctl->rtsp_queue) | |
355 return; | |
356 | |
357 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range"); | |
358 if (r_attr) | |
359 r = sdp_parse_range(r_attr->value); | |
360 | |
361 //flags & 1 -> absolute seek | |
362 //flags & 2 -> percent seek | |
363 if (flags == 0) { | |
364 time += rel_seek_secs; | |
365 if (time < r.begin) | |
366 time = r.begin; | |
367 else if (time > r.end) | |
368 time = r.end; | |
369 ndsd->seek = time; | |
370 | |
371 mp_msg(MSGT_DEMUX,MSGL_WARN,"libNemesi SEEK %f on %f - %f)\n", | |
372 time, r.begin, r.end); | |
373 | |
374 if (!rtsp_seek(ctl, time, 0)) { | |
375 RTSP_Error err = rtsp_wait(ctl); | |
376 if (err.got_error) { | |
377 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
378 "Error Performing Seek: %s\n", | |
379 err.message.reply_str); | |
380 demuxer->stream->eof = 1; | |
381 } | |
382 else | |
383 mp_msg(MSGT_DEMUX, MSGL_INFO, "Seek, performed\n"); | |
384 } | |
385 else { | |
386 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unable to pause stream to perform seek\n"); | |
387 demuxer->stream->eof = 1; | |
388 } | |
389 } | |
390 else | |
391 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported seek type\n"); | |
392 } | |
393 | |
394 static int demux_rtp_control(struct demuxer_st *demuxer, int cmd, void *arg) | |
395 { | |
396 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
397 rtsp_ctrl * ctl = ndsd->rtsp; | |
398 sdp_attr * r_attr = NULL; | |
399 sdp_range r = {0, 0}; | |
400 double time = ndsd->time[NEMESI_SESSION_VIDEO] ? | |
401 ndsd->time[NEMESI_SESSION_VIDEO] : | |
402 ndsd->time[NEMESI_SESSION_AUDIO]; | |
403 | |
404 if (!ctl->rtsp_queue) | |
405 return DEMUXER_CTRL_DONTKNOW; | |
406 | |
407 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range"); | |
408 if (r_attr) | |
409 r = sdp_parse_range(r_attr->value); | |
410 | |
411 switch (cmd) { | |
412 case DEMUXER_CTRL_GET_TIME_LENGTH: | |
413 if (r.end == 0) | |
414 return DEMUXER_CTRL_DONTKNOW; | |
415 | |
416 *((double *)arg) = ((double)r.end) - ((double)r.begin); | |
417 return DEMUXER_CTRL_OK; | |
418 | |
419 case DEMUXER_CTRL_GET_PERCENT_POS: | |
420 if (r.end == 0) | |
421 return DEMUXER_CTRL_DONTKNOW; | |
422 | |
423 *((int *)arg) = (int)( time * 100 / (r.end - r.begin) ); | |
424 return DEMUXER_CTRL_OK; | |
425 default: | |
426 return DEMUXER_CTRL_DONTKNOW; | |
427 } | |
428 } | |
429 | |
430 demuxer_desc_t demuxer_desc_rtp = { | |
431 "libNemesi demuxer", | |
432 "rtp", | |
433 "", | |
434 "Alessandro Molina", | |
435 "requires libNemesi", | |
436 DEMUXER_TYPE_RTP, | |
437 0, // no autodetect | |
438 NULL, | |
439 demux_rtp_fill_buffer, | |
440 demux_open_rtp, | |
441 demux_close_rtp, | |
442 demux_seek_rtp, | |
443 demux_rtp_control | |
444 }; |