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;
|
|
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');
|
24625
|
218 } else if (!strcmp(format_name, "H263-1998")) {
|
|
219 bih->biCompression =
|
|
220 sh_video->format = mmioFOURCC('H','2','6','3');
|
24564
|
221 } else {
|
|
222 mp_msg(MSGT_DEMUX, MSGL_WARN,
|
|
223 "Unknown MPlayer format code for MIME"
|
|
224 " type \"video/%s\"\n", format_name);
|
|
225 }
|
|
226 } else {
|
|
227 mp_msg(MSGT_DEMUX, MSGL_ERR,
|
|
228 "There is already a video session registered,"
|
|
229 " ignoring...\n");
|
|
230 }
|
|
231 } else {
|
|
232 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported media type\n");
|
|
233 }
|
|
234 }
|
|
235
|
|
236 demuxer->stream->eof = 0;
|
|
237
|
|
238 return demuxer;
|
|
239 }
|
|
240
|
|
241 static int get_data_for_session(Nemesi_DemuxerStreamData * ndsd,
|
|
242 Nemesi_SessionType stype, rtp_frame * fr)
|
|
243 {
|
|
244 rtsp_ctrl * ctl = ndsd->rtsp;
|
|
245 rtp_ssrc *ssrc = NULL;
|
|
246
|
|
247 for (ssrc = rtp_active_ssrc_queue(rtsp_get_rtp_queue(ctl));
|
|
248 ssrc;
|
|
249 ssrc = rtp_next_active_ssrc(ssrc)) {
|
|
250 if (ssrc->rtp_sess == ndsd->session[stype]) {
|
|
251 if (ndsd->first_pkt[stype].len != 0) {
|
|
252 fr->data = ndsd->first_pkt[stype].data;
|
|
253 fr->time_sec = ndsd->first_pkt[stype].time_sec;
|
|
254 fr->len = ndsd->first_pkt[stype].len;
|
|
255 ndsd->first_pkt[stype].len = 0;
|
|
256 return RTP_FILL_OK;
|
|
257 } else {
|
|
258 rtp_buff buff;
|
|
259 return rtp_fill_buffer(ssrc, fr, &buff);
|
|
260 }
|
|
261 }
|
|
262 }
|
|
263
|
|
264 return RTP_SSRC_NOTVALID;
|
|
265 }
|
|
266
|
|
267 int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds)
|
|
268 {
|
|
269 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
|
|
270 Nemesi_SessionType stype;
|
|
271 rtsp_ctrl * ctl = ndsd->rtsp;
|
|
272 rtp_thread * rtp_th = rtsp_get_rtp_th(ctl);
|
|
273 rtp_frame fr;
|
|
274
|
|
275 demux_packet_t* dp;
|
|
276
|
|
277 if ( (!ctl->rtsp_queue) || (demuxer->stream->eof) || (rtp_fill_buffers(rtp_th)) ) {
|
|
278 mp_msg(MSGT_DEMUX, MSGL_INFO, "End of Stream...\n");
|
|
279 demuxer->stream->eof = 1;
|
|
280 return 0;
|
|
281 }
|
|
282
|
|
283 if (ds == demuxer->video)
|
|
284 stype = NEMESI_SESSION_VIDEO;
|
|
285 else if (ds == demuxer->audio)
|
|
286 stype = NEMESI_SESSION_AUDIO;
|
|
287 else
|
|
288 return 0;
|
|
289
|
|
290 if(!get_data_for_session(ndsd, stype, &fr)) {
|
|
291 dp = new_demux_packet(fr.len);
|
|
292 memcpy(dp->buffer, fr.data, fr.len);
|
|
293 fr.time_sec += ndsd->seek;
|
|
294 ndsd->time[stype] = dp->pts = fr.time_sec;
|
|
295 ds_add_packet(ds, dp);
|
|
296 }
|
|
297 else {
|
|
298 stype = (stype + 1) % 2;
|
|
299 if (stype == NEMESI_SESSION_VIDEO)
|
|
300 ds = demuxer->video;
|
|
301 else
|
|
302 ds = demuxer->audio;
|
|
303
|
|
304 if(!get_data_for_session(ndsd, stype, &fr)) {
|
|
305 dp = new_demux_packet(fr.len);
|
|
306 memcpy(dp->buffer, fr.data, fr.len);
|
|
307 fr.time_sec += ndsd->seek;
|
|
308 ndsd->time[stype] = dp->pts = fr.time_sec;
|
|
309 ds_add_packet(ds, dp);
|
|
310 }
|
|
311 }
|
|
312
|
|
313 return 1;
|
|
314 }
|
|
315
|
|
316
|
|
317 void demux_close_rtp(demuxer_t* demuxer)
|
|
318 {
|
|
319 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
|
|
320 rtsp_ctrl * ctl = ndsd->rtsp;
|
|
321 RTSP_Error err;
|
|
322
|
|
323 mp_msg(MSGT_DEMUX, MSGL_INFO, "Closing libNemesi RTSP Stream...\n");
|
|
324
|
|
325 if (ndsd == NULL)
|
|
326 return;
|
|
327
|
|
328 free(ndsd);
|
|
329
|
|
330 if (rtsp_close(ctl)) {
|
|
331 err = rtsp_wait(ctl);
|
|
332 if (err.got_error)
|
|
333 mp_msg(MSGT_DEMUX, MSGL_ERR,
|
|
334 "Error Closing Stream: %s\n",
|
|
335 err.message.reply_str);
|
|
336 }
|
|
337
|
|
338 rtsp_uninit(ctl);
|
|
339 }
|
|
340
|
|
341 static void demux_seek_rtp(demuxer_t *demuxer, float rel_seek_secs,
|
|
342 float audio_delay, int flags)
|
|
343 {
|
|
344 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
|
|
345 rtsp_ctrl * ctl = ndsd->rtsp;
|
|
346 sdp_attr * r_attr = NULL;
|
|
347 sdp_range r = {0, 0};
|
|
348 double time = ndsd->time[NEMESI_SESSION_VIDEO] ?
|
|
349 ndsd->time[NEMESI_SESSION_VIDEO] :
|
|
350 ndsd->time[NEMESI_SESSION_AUDIO];
|
|
351
|
|
352 if (!ctl->rtsp_queue)
|
|
353 return;
|
|
354
|
|
355 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range");
|
|
356 if (r_attr)
|
|
357 r = sdp_parse_range(r_attr->value);
|
|
358
|
|
359 //flags & 1 -> absolute seek
|
|
360 //flags & 2 -> percent seek
|
|
361 if (flags == 0) {
|
|
362 time += rel_seek_secs;
|
|
363 if (time < r.begin)
|
|
364 time = r.begin;
|
|
365 else if (time > r.end)
|
|
366 time = r.end;
|
|
367 ndsd->seek = time;
|
|
368
|
|
369 mp_msg(MSGT_DEMUX,MSGL_WARN,"libNemesi SEEK %f on %f - %f)\n",
|
|
370 time, r.begin, r.end);
|
|
371
|
|
372 if (!rtsp_seek(ctl, time, 0)) {
|
|
373 RTSP_Error err = rtsp_wait(ctl);
|
|
374 if (err.got_error) {
|
|
375 mp_msg(MSGT_DEMUX, MSGL_ERR,
|
|
376 "Error Performing Seek: %s\n",
|
|
377 err.message.reply_str);
|
|
378 demuxer->stream->eof = 1;
|
|
379 }
|
|
380 else
|
|
381 mp_msg(MSGT_DEMUX, MSGL_INFO, "Seek, performed\n");
|
|
382 }
|
|
383 else {
|
|
384 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unable to pause stream to perform seek\n");
|
|
385 demuxer->stream->eof = 1;
|
|
386 }
|
|
387 }
|
|
388 else
|
|
389 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported seek type\n");
|
|
390 }
|
|
391
|
|
392 static int demux_rtp_control(struct demuxer_st *demuxer, int cmd, void *arg)
|
|
393 {
|
|
394 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
|
|
395 rtsp_ctrl * ctl = ndsd->rtsp;
|
|
396 sdp_attr * r_attr = NULL;
|
|
397 sdp_range r = {0, 0};
|
|
398 double time = ndsd->time[NEMESI_SESSION_VIDEO] ?
|
|
399 ndsd->time[NEMESI_SESSION_VIDEO] :
|
|
400 ndsd->time[NEMESI_SESSION_AUDIO];
|
|
401
|
|
402 if (!ctl->rtsp_queue)
|
|
403 return DEMUXER_CTRL_DONTKNOW;
|
|
404
|
|
405 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range");
|
|
406 if (r_attr)
|
|
407 r = sdp_parse_range(r_attr->value);
|
|
408
|
|
409 switch (cmd) {
|
|
410 case DEMUXER_CTRL_GET_TIME_LENGTH:
|
|
411 if (r.end == 0)
|
|
412 return DEMUXER_CTRL_DONTKNOW;
|
|
413
|
|
414 *((double *)arg) = ((double)r.end) - ((double)r.begin);
|
|
415 return DEMUXER_CTRL_OK;
|
|
416
|
|
417 case DEMUXER_CTRL_GET_PERCENT_POS:
|
|
418 if (r.end == 0)
|
|
419 return DEMUXER_CTRL_DONTKNOW;
|
|
420
|
|
421 *((int *)arg) = (int)( time * 100 / (r.end - r.begin) );
|
|
422 return DEMUXER_CTRL_OK;
|
|
423 default:
|
|
424 return DEMUXER_CTRL_DONTKNOW;
|
|
425 }
|
|
426 }
|
|
427
|
|
428 demuxer_desc_t demuxer_desc_rtp = {
|
|
429 "libNemesi demuxer",
|
|
430 "rtp",
|
|
431 "",
|
|
432 "Alessandro Molina",
|
|
433 "requires libNemesi",
|
|
434 DEMUXER_TYPE_RTP,
|
|
435 0, // no autodetect
|
|
436 NULL,
|
|
437 demux_rtp_fill_buffer,
|
|
438 demux_open_rtp,
|
|
439 demux_close_rtp,
|
|
440 demux_seek_rtp,
|
|
441 demux_rtp_control
|
|
442 };
|