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