Mercurial > mplayer.hg
annotate libmpdemux/demux_nemesi.c @ 25006:cc8cb1bbd38e
Check for second stream presence, fixes single stream playback (from amol)
author | lu_zero |
---|---|
date | Tue, 13 Nov 2007 14:29:02 +0000 |
parents | a4c783db6195 |
children | 8af79723e63e |
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 | |
24997 | 41 |
42 #define STYPE_TO_DS(demuxer, stype) \ | |
43 ((stype) == NEMESI_SESSION_VIDEO ? (demuxer)->video : (demuxer)->audio) | |
44 | |
45 #define DS_TO_STYPE(demuxer, ds) \ | |
46 ((ds) == (demuxer)->video ? NEMESI_SESSION_VIDEO : NEMESI_SESSION_AUDIO) | |
47 | |
48 #define INVERT_STYPE(stype) ((stype + 1) % 2) | |
49 | |
50 | |
51 static rtp_ssrc *wait_for_packets(Nemesi_DemuxerStreamData * ndsd, Nemesi_SessionType stype) | |
52 { | |
53 rtp_ssrc *ssrc = NULL; | |
54 | |
55 /* Wait for prebuffering (prebuffering must be enabled in nemesi) */ | |
56 int terminated = rtp_fill_buffers(rtsp_get_rtp_th(ndsd->rtsp)); | |
57 | |
58 /* Wait for the ssrc to be registered, if prebuffering is on in nemesi | |
59 this will just get immediatly the correct ssrc */ | |
60 if (!terminated) { | |
61 while ( !(ssrc = rtp_session_get_ssrc(ndsd->session[stype], ndsd->rtsp)) ) | |
62 sched_yield(); | |
63 } | |
64 | |
65 return ssrc; | |
66 } | |
67 | |
24564 | 68 static void link_session_and_fetch_conf(Nemesi_DemuxerStreamData * ndsd, |
69 Nemesi_SessionType stype, | |
70 rtp_session * sess, | |
71 rtp_buff * buff, unsigned int * fps) | |
72 { | |
73 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
|
74 rtp_ssrc *ssrc = NULL; |
24564 | 75 rtp_frame * fr = &ndsd->first_pkt[stype]; |
76 rtp_buff trash_buff; | |
25001 | 77 int must_prefetch = ((fps != NULL) || (buff != NULL)) ? 1 : 0; |
24564 | 78 |
79 ndsd->session[stype] = sess; | |
80 | |
24997 | 81 ssrc = wait_for_packets(ndsd, stype); |
24564 | 82 |
25000 | 83 if ( ((ssrc) && (must_prefetch)) ) { |
24997 | 84 if (buff == NULL) |
85 buff = &trash_buff; | |
24856 | 86 |
87 rtp_fill_buffer(ssrc, fr, buff); //Prefetch the first packet | |
24564 | 88 |
24998 | 89 /* Packet prefecthing must be done anyway or we won't be |
90 able to get the metadata, but fps calculation happens | |
91 only if the user didn't specify the FPS */ | |
25000 | 92 if ( ((!force_fps) && (fps != NULL)) ) { |
24998 | 93 while ( *fps <= 0 ) { |
94 //Wait more pkts to calculate FPS and try again | |
95 sched_yield(); | |
96 *fps = rtp_get_fps(ssrc); | |
97 } | |
98 } | |
24564 | 99 } |
100 } | |
101 | |
102 demuxer_t* demux_open_rtp(demuxer_t* demuxer) | |
103 { | |
104 nms_rtsp_hints hints; | |
105 char * url = demuxer->stream->streaming_ctrl->url->url; | |
106 rtsp_ctrl * ctl; | |
107 RTSP_Error reply; | |
108 rtsp_medium * media; | |
109 Nemesi_DemuxerStreamData * ndsd = calloc(1, sizeof(Nemesi_DemuxerStreamData)); | |
110 | |
111 memset(&hints,0,sizeof(hints)); | |
112 // if (rtsp_port) hints.first_rtp_port = rtsp_port; | |
113 if (rtsp_transport_tcp) { | |
114 hints.pref_rtsp_proto = TCP; | |
115 hints.pref_rtp_proto = TCP; | |
116 } | |
117 if (rtsp_transport_sctp) { | |
118 hints.pref_rtsp_proto = SCTP; | |
119 hints.pref_rtp_proto = SCTP; | |
120 } | |
121 | |
122 mp_msg(MSGT_DEMUX, MSGL_INFO, "Initializing libNemesi\n"); | |
123 if ((ctl = rtsp_init(&hints)) == NULL) { | |
124 free(ndsd); | |
125 return STREAM_ERROR; | |
126 } | |
127 | |
128 ndsd->rtsp = ctl; | |
129 demuxer->priv = ndsd; | |
130 //nms_verbosity_set(1); | |
131 | |
132 mp_msg(MSGT_DEMUX, MSGL_INFO, "Opening: %s\n", url); | |
133 if (rtsp_open(ctl, url)) { | |
134 mp_msg(MSGT_DEMUX, MSGL_ERR, "rtsp_open failed.\n"); | |
135 return demuxer; | |
136 } | |
137 | |
138 reply = rtsp_wait(ctl); | |
139 if (reply.got_error) { | |
140 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
141 "OPEN Error from the server: %s\n", | |
142 reply.message.reply_str); | |
143 return demuxer; | |
144 } | |
145 | |
146 rtsp_play(ctl, 0, 0); | |
147 reply = rtsp_wait(ctl); | |
148 if (reply.got_error) { | |
149 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
150 "PLAY Error from the server: %s\n", | |
151 reply.message.reply_str); | |
152 return demuxer; | |
153 } | |
154 | |
155 media = ctl->rtsp_queue->media_queue; | |
156 for (; media; media=media->next) { | |
157 sdp_medium_info * info = media->medium_info; | |
158 rtp_session * sess = media->rtp_sess; | |
25001 | 159 rtp_buff buff; |
24564 | 160 |
161 int media_format = atoi(info->fmts); | |
162 rtp_pt * ptinfo = rtp_get_pt_info(sess, media_format); | |
163 char const * format_name = ptinfo ? ptinfo->name : NULL; | |
164 | |
25001 | 165 memset(&buff, 0, sizeof(rtp_buff)); |
166 | |
24564 | 167 if (sess->parsers[media_format] == NULL) { |
168 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
169 "libNemesi unsupported media format: %s\n", | |
170 format_name ? format_name : info->fmts); | |
171 continue; | |
172 } | |
173 else { | |
174 mp_msg(MSGT_DEMUX, MSGL_INFO, | |
175 "libNemesi supported media: %s\n", | |
176 format_name); | |
177 } | |
178 | |
179 if (ptinfo->type == AU) { | |
180 if (ndsd->session[NEMESI_SESSION_AUDIO] == NULL) { | |
181 sh_audio_t* sh_audio = new_sh_audio(demuxer,0); | |
25001 | 182 WAVEFORMATEX* wf; |
24564 | 183 demux_stream_t* d_audio = demuxer->audio; |
184 | |
185 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as AUDIO stream...\n"); | |
186 | |
187 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_AUDIO, | |
25001 | 188 sess, &buff, NULL); |
189 | |
190 if (buff.len) { | |
191 wf = calloc(1,sizeof(WAVEFORMATEX)+buff.len); | |
192 wf->cbSize = buff.len; | |
193 memcpy(wf+1, buff.data, buff.len); | |
194 } else { | |
195 wf = calloc(1,sizeof(WAVEFORMATEX)); | |
196 } | |
24564 | 197 |
198 sh_audio->wf = wf; | |
199 d_audio->sh = sh_audio; | |
200 sh_audio->ds = d_audio; | |
201 wf->nSamplesPerSec = 0; | |
202 | |
203 //List of known audio formats | |
204 if (!strcmp(format_name, "MPA")) | |
205 wf->wFormatTag = | |
206 sh_audio->format = 0x55; | |
207 else if (!strcmp(format_name, "vorbis")) | |
208 wf->wFormatTag = | |
209 sh_audio->format = mmioFOURCC('v','r','b','s'); | |
210 else | |
211 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
212 "Unknown MPlayer format code for MIME" | |
213 " type \"audio/%s\"\n", format_name); | |
214 } else { | |
215 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
216 "There is already an audio session registered," | |
217 " ignoring...\n"); | |
218 } | |
219 } else if (ptinfo->type == VI) { | |
25002 | 220 if (ndsd->session[NEMESI_SESSION_VIDEO] == NULL) { |
24564 | 221 sh_video_t* sh_video; |
222 BITMAPINFOHEADER* bih; | |
223 demux_stream_t* d_video; | |
224 int fps = 0; | |
225 | |
226 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as VIDEO stream...\n"); | |
227 | |
228 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_VIDEO, | |
229 sess, &buff, &fps); | |
230 | |
231 if (buff.len) { | |
232 bih = calloc(1,sizeof(BITMAPINFOHEADER)+buff.len); | |
233 bih->biSize = sizeof(BITMAPINFOHEADER)+buff.len; | |
234 memcpy(bih+1, buff.data, buff.len); | |
235 } else { | |
236 bih = calloc(1,sizeof(BITMAPINFOHEADER)); | |
237 bih->biSize = sizeof(BITMAPINFOHEADER); | |
238 } | |
239 | |
240 sh_video = new_sh_video(demuxer,0); | |
241 sh_video->bih = bih; | |
242 d_video = demuxer->video; | |
243 d_video->sh = sh_video; | |
244 sh_video->ds = d_video; | |
245 | |
24856 | 246 if (fps) { |
24564 | 247 sh_video->fps = fps; |
24856 | 248 sh_video->frametime = 1.0/fps; |
249 } | |
24564 | 250 |
251 //List of known video formats | |
252 if (!strcmp(format_name, "MPV")) { | |
253 bih->biCompression = | |
254 sh_video->format = mmioFOURCC('M','P','E','G'); | |
255 } else if (!strcmp(format_name, "H264")) { | |
256 bih->biCompression = | |
257 sh_video->format = mmioFOURCC('H','2','6','4'); | |
24625 | 258 } else if (!strcmp(format_name, "H263-1998")) { |
259 bih->biCompression = | |
260 sh_video->format = mmioFOURCC('H','2','6','3'); | |
24999 | 261 } else if (!strcmp(format_name, "MP4V-ES")) { |
262 bih->biCompression = | |
263 sh_video->format = mmioFOURCC('M','P','4','V'); | |
24564 | 264 } else { |
265 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
266 "Unknown MPlayer format code for MIME" | |
267 " type \"video/%s\"\n", format_name); | |
268 } | |
269 } else { | |
270 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
271 "There is already a video session registered," | |
272 " ignoring...\n"); | |
273 } | |
274 } else { | |
275 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported media type\n"); | |
276 } | |
277 } | |
278 | |
279 demuxer->stream->eof = 0; | |
280 | |
281 return demuxer; | |
282 } | |
283 | |
284 static int get_data_for_session(Nemesi_DemuxerStreamData * ndsd, | |
24997 | 285 Nemesi_SessionType stype, rtp_ssrc * ssrc, |
286 rtp_frame * fr) | |
24564 | 287 { |
24997 | 288 if (ndsd->first_pkt[stype].len != 0) { |
289 fr->data = ndsd->first_pkt[stype].data; | |
290 fr->time_sec = ndsd->first_pkt[stype].time_sec; | |
291 fr->len = ndsd->first_pkt[stype].len; | |
292 ndsd->first_pkt[stype].len = 0; | |
293 return RTP_FILL_OK; | |
294 } else { | |
295 rtp_buff buff; | |
296 return rtp_fill_buffer(ssrc, fr, &buff); | |
297 } | |
298 } | |
24564 | 299 |
24997 | 300 static void stream_add_packet(Nemesi_DemuxerStreamData * ndsd, |
301 Nemesi_SessionType stype, | |
302 demux_stream_t* ds, rtp_frame * fr) | |
303 { | |
304 demux_packet_t* dp = new_demux_packet(fr->len); | |
305 memcpy(dp->buffer, fr->data, fr->len); | |
24564 | 306 |
24997 | 307 fr->time_sec += ndsd->seek; |
308 ndsd->time[stype] = dp->pts = fr->time_sec; | |
309 | |
310 ds_add_packet(ds, dp); | |
24564 | 311 } |
312 | |
313 int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds) | |
314 { | |
315 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
316 Nemesi_SessionType stype; | |
24997 | 317 rtp_ssrc * ssrc; |
24564 | 318 rtp_frame fr; |
319 | |
24997 | 320 if ( (!ndsd->rtsp->rtsp_queue) || (demuxer->stream->eof) ) { |
24564 | 321 mp_msg(MSGT_DEMUX, MSGL_INFO, "End of Stream...\n"); |
322 demuxer->stream->eof = 1; | |
323 return 0; | |
324 } | |
325 | |
24997 | 326 stype = DS_TO_STYPE(demuxer, ds); |
327 if ( (ssrc = wait_for_packets(ndsd, stype)) == NULL ) { | |
328 mp_msg(MSGT_DEMUX, MSGL_INFO, "Bye...\n"); | |
329 demuxer->stream->eof = 1; | |
24564 | 330 return 0; |
331 } | |
332 | |
24997 | 333 if(!get_data_for_session(ndsd, stype, ssrc, &fr)) |
334 stream_add_packet(ndsd, stype, ds, &fr); | |
335 else { | |
336 stype = INVERT_STYPE(stype); | |
25006
cc8cb1bbd38e
Check for second stream presence, fixes single stream playback (from amol)
lu_zero
parents:
25002
diff
changeset
|
337 |
cc8cb1bbd38e
Check for second stream presence, fixes single stream playback (from amol)
lu_zero
parents:
25002
diff
changeset
|
338 //Must check if we actually have a stream of the other type |
cc8cb1bbd38e
Check for second stream presence, fixes single stream playback (from amol)
lu_zero
parents:
25002
diff
changeset
|
339 if (!ndsd->session[stype]) |
cc8cb1bbd38e
Check for second stream presence, fixes single stream playback (from amol)
lu_zero
parents:
25002
diff
changeset
|
340 return 1; |
cc8cb1bbd38e
Check for second stream presence, fixes single stream playback (from amol)
lu_zero
parents:
25002
diff
changeset
|
341 |
24997 | 342 ds = STYPE_TO_DS(demuxer, stype); |
343 ssrc = wait_for_packets(ndsd, stype); | |
344 | |
345 if(!get_data_for_session(ndsd, stype, ssrc, &fr)) | |
346 stream_add_packet(ndsd, stype, ds, &fr); | |
24564 | 347 } |
348 | |
349 return 1; | |
350 } | |
351 | |
352 | |
353 void demux_close_rtp(demuxer_t* demuxer) | |
354 { | |
355 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
356 rtsp_ctrl * ctl = ndsd->rtsp; | |
357 RTSP_Error err; | |
358 | |
359 mp_msg(MSGT_DEMUX, MSGL_INFO, "Closing libNemesi RTSP Stream...\n"); | |
360 | |
361 if (ndsd == NULL) | |
362 return; | |
363 | |
364 free(ndsd); | |
365 | |
366 if (rtsp_close(ctl)) { | |
367 err = rtsp_wait(ctl); | |
368 if (err.got_error) | |
369 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
370 "Error Closing Stream: %s\n", | |
371 err.message.reply_str); | |
372 } | |
373 | |
374 rtsp_uninit(ctl); | |
375 } | |
376 | |
377 static void demux_seek_rtp(demuxer_t *demuxer, float rel_seek_secs, | |
378 float audio_delay, int flags) | |
379 { | |
380 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
381 rtsp_ctrl * ctl = ndsd->rtsp; | |
382 sdp_attr * r_attr = NULL; | |
383 sdp_range r = {0, 0}; | |
384 double time = ndsd->time[NEMESI_SESSION_VIDEO] ? | |
385 ndsd->time[NEMESI_SESSION_VIDEO] : | |
386 ndsd->time[NEMESI_SESSION_AUDIO]; | |
387 | |
388 if (!ctl->rtsp_queue) | |
389 return; | |
390 | |
391 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range"); | |
392 if (r_attr) | |
393 r = sdp_parse_range(r_attr->value); | |
394 | |
395 //flags & 1 -> absolute seek | |
396 //flags & 2 -> percent seek | |
397 if (flags == 0) { | |
398 time += rel_seek_secs; | |
399 if (time < r.begin) | |
400 time = r.begin; | |
401 else if (time > r.end) | |
402 time = r.end; | |
403 ndsd->seek = time; | |
404 | |
405 mp_msg(MSGT_DEMUX,MSGL_WARN,"libNemesi SEEK %f on %f - %f)\n", | |
406 time, r.begin, r.end); | |
407 | |
408 if (!rtsp_seek(ctl, time, 0)) { | |
409 RTSP_Error err = rtsp_wait(ctl); | |
410 if (err.got_error) { | |
411 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
412 "Error Performing Seek: %s\n", | |
413 err.message.reply_str); | |
414 demuxer->stream->eof = 1; | |
415 } | |
416 else | |
417 mp_msg(MSGT_DEMUX, MSGL_INFO, "Seek, performed\n"); | |
418 } | |
419 else { | |
420 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unable to pause stream to perform seek\n"); | |
421 demuxer->stream->eof = 1; | |
422 } | |
423 } | |
424 else | |
425 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported seek type\n"); | |
426 } | |
427 | |
428 static int demux_rtp_control(struct demuxer_st *demuxer, int cmd, void *arg) | |
429 { | |
430 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
431 rtsp_ctrl * ctl = ndsd->rtsp; | |
432 sdp_attr * r_attr = NULL; | |
433 sdp_range r = {0, 0}; | |
434 double time = ndsd->time[NEMESI_SESSION_VIDEO] ? | |
435 ndsd->time[NEMESI_SESSION_VIDEO] : | |
436 ndsd->time[NEMESI_SESSION_AUDIO]; | |
437 | |
438 if (!ctl->rtsp_queue) | |
439 return DEMUXER_CTRL_DONTKNOW; | |
440 | |
441 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range"); | |
442 if (r_attr) | |
443 r = sdp_parse_range(r_attr->value); | |
444 | |
445 switch (cmd) { | |
446 case DEMUXER_CTRL_GET_TIME_LENGTH: | |
447 if (r.end == 0) | |
448 return DEMUXER_CTRL_DONTKNOW; | |
449 | |
450 *((double *)arg) = ((double)r.end) - ((double)r.begin); | |
451 return DEMUXER_CTRL_OK; | |
452 | |
453 case DEMUXER_CTRL_GET_PERCENT_POS: | |
454 if (r.end == 0) | |
455 return DEMUXER_CTRL_DONTKNOW; | |
456 | |
457 *((int *)arg) = (int)( time * 100 / (r.end - r.begin) ); | |
458 return DEMUXER_CTRL_OK; | |
459 default: | |
460 return DEMUXER_CTRL_DONTKNOW; | |
461 } | |
462 } | |
463 | |
464 demuxer_desc_t demuxer_desc_rtp = { | |
465 "libNemesi demuxer", | |
466 "rtp", | |
467 "", | |
468 "Alessandro Molina", | |
469 "requires libNemesi", | |
470 DEMUXER_TYPE_RTP, | |
471 0, // no autodetect | |
472 NULL, | |
473 demux_rtp_fill_buffer, | |
474 demux_open_rtp, | |
475 demux_close_rtp, | |
476 demux_seek_rtp, | |
477 demux_rtp_control | |
478 }; |