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