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