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