Mercurial > mplayer.hg
annotate libmpdemux/demux_nemesi.c @ 25010:9a4d15a28d12
fetch metadata for audio (from amol)
author | lu_zero |
---|---|
date | Tue, 13 Nov 2007 23:00:12 +0000 |
parents | 8af79723e63e |
children | 39b7d358106a |
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; | |
25010 | 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 |
25010 | 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 */ | |
25010 | 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; | |
159 | |
160 int media_format = atoi(info->fmts); | |
161 rtp_pt * ptinfo = rtp_get_pt_info(sess, media_format); | |
162 char const * format_name = ptinfo ? ptinfo->name : NULL; | |
163 | |
164 if (sess->parsers[media_format] == NULL) { | |
165 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
166 "libNemesi unsupported media format: %s\n", | |
167 format_name ? format_name : info->fmts); | |
168 continue; | |
169 } | |
170 else { | |
171 mp_msg(MSGT_DEMUX, MSGL_INFO, | |
172 "libNemesi supported media: %s\n", | |
173 format_name); | |
174 } | |
175 | |
176 if (ptinfo->type == AU) { | |
177 if (ndsd->session[NEMESI_SESSION_AUDIO] == NULL) { | |
178 sh_audio_t* sh_audio = new_sh_audio(demuxer,0); | |
25009 | 179 WAVEFORMATEX* wf = calloc(1,sizeof(WAVEFORMATEX)); |
24564 | 180 demux_stream_t* d_audio = demuxer->audio; |
181 | |
182 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as AUDIO stream...\n"); | |
183 | |
184 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_AUDIO, | |
25009 | 185 sess, NULL, NULL); |
24564 | 186 |
187 sh_audio->wf = wf; | |
188 d_audio->sh = sh_audio; | |
189 sh_audio->ds = d_audio; | |
190 wf->nSamplesPerSec = 0; | |
191 | |
192 //List of known audio formats | |
193 if (!strcmp(format_name, "MPA")) | |
194 wf->wFormatTag = | |
195 sh_audio->format = 0x55; | |
196 else if (!strcmp(format_name, "vorbis")) | |
197 wf->wFormatTag = | |
198 sh_audio->format = mmioFOURCC('v','r','b','s'); | |
199 else | |
200 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
201 "Unknown MPlayer format code for MIME" | |
202 " type \"audio/%s\"\n", format_name); | |
203 } else { | |
204 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
205 "There is already an audio session registered," | |
206 " ignoring...\n"); | |
207 } | |
208 } else if (ptinfo->type == VI) { | |
25009 | 209 if (ndsd->session[NEMESI_SESSION_AUDIO] == NULL) { |
24564 | 210 sh_video_t* sh_video; |
211 BITMAPINFOHEADER* bih; | |
212 demux_stream_t* d_video; | |
213 int fps = 0; | |
25009 | 214 rtp_buff buff; |
215 memset(&buff, 0, sizeof(rtp_buff)); | |
24564 | 216 |
217 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as VIDEO stream...\n"); | |
218 | |
219 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_VIDEO, | |
220 sess, &buff, &fps); | |
221 | |
222 if (buff.len) { | |
223 bih = calloc(1,sizeof(BITMAPINFOHEADER)+buff.len); | |
224 bih->biSize = sizeof(BITMAPINFOHEADER)+buff.len; | |
225 memcpy(bih+1, buff.data, buff.len); | |
226 } else { | |
227 bih = calloc(1,sizeof(BITMAPINFOHEADER)); | |
228 bih->biSize = sizeof(BITMAPINFOHEADER); | |
229 } | |
230 | |
231 sh_video = new_sh_video(demuxer,0); | |
232 sh_video->bih = bih; | |
233 d_video = demuxer->video; | |
234 d_video->sh = sh_video; | |
235 sh_video->ds = d_video; | |
236 | |
24856 | 237 if (fps) { |
24564 | 238 sh_video->fps = fps; |
24856 | 239 sh_video->frametime = 1.0/fps; |
240 } | |
24564 | 241 |
242 //List of known video formats | |
243 if (!strcmp(format_name, "MPV")) { | |
244 bih->biCompression = | |
245 sh_video->format = mmioFOURCC('M','P','E','G'); | |
246 } else if (!strcmp(format_name, "H264")) { | |
247 bih->biCompression = | |
248 sh_video->format = mmioFOURCC('H','2','6','4'); | |
24625 | 249 } else if (!strcmp(format_name, "H263-1998")) { |
250 bih->biCompression = | |
251 sh_video->format = mmioFOURCC('H','2','6','3'); | |
24999 | 252 } else if (!strcmp(format_name, "MP4V-ES")) { |
253 bih->biCompression = | |
254 sh_video->format = mmioFOURCC('M','P','4','V'); | |
24564 | 255 } else { |
256 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
257 "Unknown MPlayer format code for MIME" | |
258 " type \"video/%s\"\n", format_name); | |
259 } | |
260 } else { | |
261 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
262 "There is already a video session registered," | |
263 " ignoring...\n"); | |
264 } | |
265 } else { | |
266 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported media type\n"); | |
267 } | |
268 } | |
269 | |
270 demuxer->stream->eof = 0; | |
271 | |
272 return demuxer; | |
273 } | |
274 | |
275 static int get_data_for_session(Nemesi_DemuxerStreamData * ndsd, | |
24997 | 276 Nemesi_SessionType stype, rtp_ssrc * ssrc, |
277 rtp_frame * fr) | |
24564 | 278 { |
24997 | 279 if (ndsd->first_pkt[stype].len != 0) { |
280 fr->data = ndsd->first_pkt[stype].data; | |
281 fr->time_sec = ndsd->first_pkt[stype].time_sec; | |
282 fr->len = ndsd->first_pkt[stype].len; | |
283 ndsd->first_pkt[stype].len = 0; | |
284 return RTP_FILL_OK; | |
285 } else { | |
286 rtp_buff buff; | |
287 return rtp_fill_buffer(ssrc, fr, &buff); | |
288 } | |
289 } | |
24564 | 290 |
24997 | 291 static void stream_add_packet(Nemesi_DemuxerStreamData * ndsd, |
292 Nemesi_SessionType stype, | |
293 demux_stream_t* ds, rtp_frame * fr) | |
294 { | |
295 demux_packet_t* dp = new_demux_packet(fr->len); | |
296 memcpy(dp->buffer, fr->data, fr->len); | |
24564 | 297 |
24997 | 298 fr->time_sec += ndsd->seek; |
299 ndsd->time[stype] = dp->pts = fr->time_sec; | |
300 | |
301 ds_add_packet(ds, dp); | |
24564 | 302 } |
303 | |
304 int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds) | |
305 { | |
306 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
307 Nemesi_SessionType stype; | |
24997 | 308 rtp_ssrc * ssrc; |
24564 | 309 rtp_frame fr; |
310 | |
24997 | 311 if ( (!ndsd->rtsp->rtsp_queue) || (demuxer->stream->eof) ) { |
24564 | 312 mp_msg(MSGT_DEMUX, MSGL_INFO, "End of Stream...\n"); |
313 demuxer->stream->eof = 1; | |
314 return 0; | |
315 } | |
316 | |
24997 | 317 stype = DS_TO_STYPE(demuxer, ds); |
318 if ( (ssrc = wait_for_packets(ndsd, stype)) == NULL ) { | |
319 mp_msg(MSGT_DEMUX, MSGL_INFO, "Bye...\n"); | |
320 demuxer->stream->eof = 1; | |
24564 | 321 return 0; |
322 } | |
323 | |
24997 | 324 if(!get_data_for_session(ndsd, stype, ssrc, &fr)) |
325 stream_add_packet(ndsd, stype, ds, &fr); | |
326 else { | |
327 stype = INVERT_STYPE(stype); | |
328 ds = STYPE_TO_DS(demuxer, stype); | |
329 ssrc = wait_for_packets(ndsd, stype); | |
330 | |
331 if(!get_data_for_session(ndsd, stype, ssrc, &fr)) | |
332 stream_add_packet(ndsd, stype, ds, &fr); | |
24564 | 333 } |
334 | |
335 return 1; | |
336 } | |
337 | |
338 | |
339 void demux_close_rtp(demuxer_t* demuxer) | |
340 { | |
341 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
342 rtsp_ctrl * ctl = ndsd->rtsp; | |
343 RTSP_Error err; | |
344 | |
345 mp_msg(MSGT_DEMUX, MSGL_INFO, "Closing libNemesi RTSP Stream...\n"); | |
346 | |
347 if (ndsd == NULL) | |
348 return; | |
349 | |
350 free(ndsd); | |
351 | |
352 if (rtsp_close(ctl)) { | |
353 err = rtsp_wait(ctl); | |
354 if (err.got_error) | |
355 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
356 "Error Closing Stream: %s\n", | |
357 err.message.reply_str); | |
358 } | |
359 | |
360 rtsp_uninit(ctl); | |
361 } | |
362 | |
363 static void demux_seek_rtp(demuxer_t *demuxer, float rel_seek_secs, | |
364 float audio_delay, int flags) | |
365 { | |
366 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
367 rtsp_ctrl * ctl = ndsd->rtsp; | |
368 sdp_attr * r_attr = NULL; | |
369 sdp_range r = {0, 0}; | |
370 double time = ndsd->time[NEMESI_SESSION_VIDEO] ? | |
371 ndsd->time[NEMESI_SESSION_VIDEO] : | |
372 ndsd->time[NEMESI_SESSION_AUDIO]; | |
373 | |
374 if (!ctl->rtsp_queue) | |
375 return; | |
376 | |
377 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range"); | |
378 if (r_attr) | |
379 r = sdp_parse_range(r_attr->value); | |
380 | |
381 //flags & 1 -> absolute seek | |
382 //flags & 2 -> percent seek | |
383 if (flags == 0) { | |
384 time += rel_seek_secs; | |
385 if (time < r.begin) | |
386 time = r.begin; | |
387 else if (time > r.end) | |
388 time = r.end; | |
389 ndsd->seek = time; | |
390 | |
391 mp_msg(MSGT_DEMUX,MSGL_WARN,"libNemesi SEEK %f on %f - %f)\n", | |
392 time, r.begin, r.end); | |
393 | |
394 if (!rtsp_seek(ctl, time, 0)) { | |
395 RTSP_Error err = rtsp_wait(ctl); | |
396 if (err.got_error) { | |
397 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
398 "Error Performing Seek: %s\n", | |
399 err.message.reply_str); | |
400 demuxer->stream->eof = 1; | |
401 } | |
402 else | |
403 mp_msg(MSGT_DEMUX, MSGL_INFO, "Seek, performed\n"); | |
404 } | |
405 else { | |
406 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unable to pause stream to perform seek\n"); | |
407 demuxer->stream->eof = 1; | |
408 } | |
409 } | |
410 else | |
411 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported seek type\n"); | |
412 } | |
413 | |
414 static int demux_rtp_control(struct demuxer_st *demuxer, int cmd, void *arg) | |
415 { | |
416 Nemesi_DemuxerStreamData * ndsd = demuxer->priv; | |
417 rtsp_ctrl * ctl = ndsd->rtsp; | |
418 sdp_attr * r_attr = NULL; | |
419 sdp_range r = {0, 0}; | |
420 double time = ndsd->time[NEMESI_SESSION_VIDEO] ? | |
421 ndsd->time[NEMESI_SESSION_VIDEO] : | |
422 ndsd->time[NEMESI_SESSION_AUDIO]; | |
423 | |
424 if (!ctl->rtsp_queue) | |
425 return DEMUXER_CTRL_DONTKNOW; | |
426 | |
427 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range"); | |
428 if (r_attr) | |
429 r = sdp_parse_range(r_attr->value); | |
430 | |
431 switch (cmd) { | |
432 case DEMUXER_CTRL_GET_TIME_LENGTH: | |
433 if (r.end == 0) | |
434 return DEMUXER_CTRL_DONTKNOW; | |
435 | |
436 *((double *)arg) = ((double)r.end) - ((double)r.begin); | |
437 return DEMUXER_CTRL_OK; | |
438 | |
439 case DEMUXER_CTRL_GET_PERCENT_POS: | |
440 if (r.end == 0) | |
441 return DEMUXER_CTRL_DONTKNOW; | |
442 | |
443 *((int *)arg) = (int)( time * 100 / (r.end - r.begin) ); | |
444 return DEMUXER_CTRL_OK; | |
445 default: | |
446 return DEMUXER_CTRL_DONTKNOW; | |
447 } | |
448 } | |
449 | |
450 demuxer_desc_t demuxer_desc_rtp = { | |
451 "libNemesi demuxer", | |
452 "rtp", | |
453 "", | |
454 "Alessandro Molina", | |
455 "requires libNemesi", | |
456 DEMUXER_TYPE_RTP, | |
457 0, // no autodetect | |
458 NULL, | |
459 demux_rtp_fill_buffer, | |
460 demux_open_rtp, | |
461 demux_close_rtp, | |
462 demux_seek_rtp, | |
463 demux_rtp_control | |
464 }; |