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