comparison libmpdemux/demux_nemesi.c @ 24564:e4a38a7cdb81

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