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