Try gmake before make
[mplayer/kovensky.git] / libmpdemux / demux_nemesi.c
blob704c31e4b14dd5bc761cbd0d1f5fba438510dda7
1 /*
2 * Copyright (C) 2007 Alessandro Molina <amol.wrk@gmail.com>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "stream/stream.h"
23 #include "demuxer.h"
24 #include "stheader.h"
25 #define HAVE_STRUCT_SOCKADDR_STORAGE
26 #include "nemesi/rtsp.h"
27 #include "nemesi/rtp.h"
28 #include <sched.h>
30 int rtsp_transport_tcp = 0;
31 int rtsp_transport_sctp = 0;
32 int rtsp_port = 0;
34 typedef struct {
35 char * mime;
36 unsigned int fourcc;
37 } MIMEto4CC;
39 #define NMS_MAX_FORMATS 16
41 MIMEto4CC supported_audio[NMS_MAX_FORMATS] = {
42 {"MPA", 0x55},
43 {"vorbis", mmioFOURCC('v','r','b','s')},
44 {"mpeg4-generic", mmioFOURCC('M','P','4','A')},
45 {NULL, 0},
48 MIMEto4CC supported_video[NMS_MAX_FORMATS] = {
49 {"MPV", mmioFOURCC('M','P','E','G')},
50 {"theora",mmioFOURCC('t','h','e','o')},
51 {"H264", mmioFOURCC('H','2','6','4')},
52 {"H263-1998", mmioFOURCC('H','2','6','3')},
53 {"MP4V-ES", mmioFOURCC('M','P','4','V')},
54 {NULL, 0},
57 typedef enum { NEMESI_SESSION_VIDEO,
58 NEMESI_SESSION_AUDIO } Nemesi_SessionType;
60 typedef struct {
61 rtsp_ctrl * rtsp;
62 rtp_session * session[2];
63 rtp_frame first_pkt[2];
64 double time[2];
65 double seek;
66 } Nemesi_DemuxerStreamData;
69 #define STYPE_TO_DS(demuxer, stype) \
70 ((stype) == NEMESI_SESSION_VIDEO ? (demuxer)->video : (demuxer)->audio)
72 #define DS_TO_STYPE(demuxer, ds) \
73 ((ds) == (demuxer)->video ? NEMESI_SESSION_VIDEO : NEMESI_SESSION_AUDIO)
75 #define INVERT_STYPE(stype) ((stype + 1) % 2)
77 static unsigned int get4CC(MIMEto4CC * supported_formats, char const * format)
79 unsigned i;
81 for(i = 0; i < NMS_MAX_FORMATS; ++i) {
82 if (!supported_formats[i].mime)
83 return 0;
84 else if ( strcmp(supported_formats[i].mime, format) == 0 )
85 return supported_formats[i].fourcc;
88 return 0;
91 static rtp_ssrc *wait_for_packets(Nemesi_DemuxerStreamData * ndsd, Nemesi_SessionType stype)
93 rtp_ssrc *ssrc = NULL;
95 /* Wait for prebuffering (prebuffering must be enabled in nemesi) */
96 int terminated = rtp_fill_buffers(rtsp_get_rtp_th(ndsd->rtsp));
98 /* Wait for the ssrc to be registered, if prebuffering is on in nemesi
99 this will just get immediatly the correct ssrc */
100 if (!terminated) {
101 while ( !(ssrc = rtp_session_get_ssrc(ndsd->session[stype], ndsd->rtsp)) )
102 sched_yield();
105 return ssrc;
108 static void link_session_and_fetch_conf(Nemesi_DemuxerStreamData * ndsd,
109 Nemesi_SessionType stype,
110 rtp_session * sess,
111 rtp_buff * buff, unsigned int * fps)
113 extern double force_fps;
114 rtp_ssrc *ssrc = NULL;
115 rtp_frame * fr = &ndsd->first_pkt[stype];
116 rtp_buff trash_buff;
117 int must_prefetch = ((fps != NULL) || (buff != NULL)) ? 1 : 0;
119 ndsd->session[stype] = sess;
121 ssrc = wait_for_packets(ndsd, stype);
123 if ( ((ssrc) && (must_prefetch)) ) {
124 if (buff == NULL)
125 buff = &trash_buff;
127 rtp_fill_buffer(ssrc, fr, buff); //Prefetch the first packet
129 /* Packet prefecthing must be done anyway or we won't be
130 able to get the metadata, but fps calculation happens
131 only if the user didn't specify the FPS */
132 if ( ((!force_fps) && (fps != NULL)) ) {
133 while ( *fps <= 0 ) {
134 //Wait more pkts to calculate FPS and try again
135 sched_yield();
136 *fps = rtp_get_fps(ssrc);
142 static demuxer_t* demux_open_rtp(demuxer_t* demuxer)
144 nms_rtsp_hints hints;
145 char * url = demuxer->stream->streaming_ctrl->url->url;
146 rtsp_ctrl * ctl;
147 RTSP_Error reply;
148 rtsp_medium * media;
149 Nemesi_DemuxerStreamData * ndsd = calloc(1, sizeof(Nemesi_DemuxerStreamData));
151 memset(&hints,0,sizeof(hints));
152 if (rtsp_port) hints.first_rtp_port = rtsp_port;
153 if (rtsp_transport_tcp) {
154 hints.pref_rtsp_proto = TCP;
155 hints.pref_rtp_proto = TCP;
157 if (rtsp_transport_sctp) {
158 hints.pref_rtsp_proto = SCTP;
159 hints.pref_rtp_proto = SCTP;
162 mp_msg(MSGT_DEMUX, MSGL_INFO, "Initializing libNemesi\n");
163 if ((ctl = rtsp_init(&hints)) == NULL) {
164 free(ndsd);
165 return STREAM_ERROR;
168 ndsd->rtsp = ctl;
169 demuxer->priv = ndsd;
170 //nms_verbosity_set(1);
172 mp_msg(MSGT_DEMUX, MSGL_INFO, "Opening: %s\n", url);
173 if (rtsp_open(ctl, url)) {
174 mp_msg(MSGT_DEMUX, MSGL_ERR, "rtsp_open failed.\n");
175 return demuxer;
178 reply = rtsp_wait(ctl);
179 if (reply.got_error) {
180 mp_msg(MSGT_DEMUX, MSGL_ERR,
181 "OPEN Error from the server: %s\n",
182 reply.message.reply_str);
183 return demuxer;
186 rtsp_play(ctl, 0, 0);
187 reply = rtsp_wait(ctl);
188 if (reply.got_error) {
189 mp_msg(MSGT_DEMUX, MSGL_ERR,
190 "PLAY Error from the server: %s\n",
191 reply.message.reply_str);
192 return demuxer;
195 if (!ctl->rtsp_queue)
196 return demuxer;
198 media = ctl->rtsp_queue->media_queue;
199 for (; media; media=media->next) {
200 sdp_medium_info * info = media->medium_info;
201 rtp_session * sess = media->rtp_sess;
202 rtp_buff buff;
204 int media_format = atoi(info->fmts);
205 rtp_pt * ptinfo = rtp_get_pt_info(sess, media_format);
206 char const * format_name = ptinfo ? ptinfo->name : NULL;
208 memset(&buff, 0, sizeof(rtp_buff));
210 if (sess->parsers[media_format] == NULL) {
211 mp_msg(MSGT_DEMUX, MSGL_ERR,
212 "libNemesi unsupported media format: %s\n",
213 format_name ? format_name : info->fmts);
214 continue;
216 else {
217 mp_msg(MSGT_DEMUX, MSGL_INFO,
218 "libNemesi supported media: %s\n",
219 format_name);
222 if (ptinfo->type == AU) {
223 if (ndsd->session[NEMESI_SESSION_AUDIO] == NULL) {
224 sh_audio_t* sh_audio = new_sh_audio(demuxer,0);
225 WAVEFORMATEX* wf;
226 demux_stream_t* d_audio = demuxer->audio;
227 demuxer->audio->id = 0;
229 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as AUDIO stream...\n");
231 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_AUDIO,
232 sess, &buff, NULL);
234 if (buff.len) {
235 wf = calloc(1,sizeof(WAVEFORMATEX)+buff.len);
236 wf->cbSize = buff.len;
237 memcpy(wf+1, buff.data, buff.len);
238 } else {
239 wf = calloc(1,sizeof(WAVEFORMATEX));
242 sh_audio->wf = wf;
243 d_audio->sh = sh_audio;
244 sh_audio->ds = d_audio;
245 wf->nSamplesPerSec = 0;
247 wf->wFormatTag =
248 sh_audio->format = get4CC(supported_audio, format_name);
249 if ( !(wf->wFormatTag) )
250 mp_msg(MSGT_DEMUX, MSGL_WARN,
251 "Unknown MPlayer format code for MIME"
252 " type \"audio/%s\"\n", format_name);
253 } else {
254 mp_msg(MSGT_DEMUX, MSGL_ERR,
255 "There is already an audio session registered,"
256 " ignoring...\n");
258 } else if (ptinfo->type == VI) {
259 if (ndsd->session[NEMESI_SESSION_VIDEO] == NULL) {
260 sh_video_t* sh_video;
261 BITMAPINFOHEADER* bih;
262 demux_stream_t* d_video;
263 int fps = 0;
265 mp_msg(MSGT_DEMUX, MSGL_INFO, "Detected as VIDEO stream...\n");
267 link_session_and_fetch_conf(ndsd, NEMESI_SESSION_VIDEO,
268 sess, &buff, &fps);
270 if (buff.len) {
271 bih = calloc(1,sizeof(BITMAPINFOHEADER)+buff.len);
272 bih->biSize = sizeof(BITMAPINFOHEADER)+buff.len;
273 memcpy(bih+1, buff.data, buff.len);
274 } else {
275 bih = calloc(1,sizeof(BITMAPINFOHEADER));
276 bih->biSize = sizeof(BITMAPINFOHEADER);
279 sh_video = new_sh_video(demuxer,0);
280 sh_video->bih = bih;
281 d_video = demuxer->video;
282 d_video->sh = sh_video;
283 sh_video->ds = d_video;
285 if (fps) {
286 sh_video->fps = fps;
287 sh_video->frametime = 1.0/fps;
290 bih->biCompression =
291 sh_video->format = get4CC(supported_video, format_name);
292 if ( !(bih->biCompression) ) {
293 mp_msg(MSGT_DEMUX, MSGL_WARN,
294 "Unknown MPlayer format code for MIME"
295 " type \"video/%s\"\n", format_name);
297 } else {
298 mp_msg(MSGT_DEMUX, MSGL_ERR,
299 "There is already a video session registered,"
300 " ignoring...\n");
302 } else {
303 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported media type\n");
307 demuxer->stream->eof = 0;
309 return demuxer;
312 static int get_data_for_session(Nemesi_DemuxerStreamData * ndsd,
313 Nemesi_SessionType stype, rtp_ssrc * ssrc,
314 rtp_frame * fr)
316 if (ndsd->first_pkt[stype].len != 0) {
317 fr->data = ndsd->first_pkt[stype].data;
318 fr->time_sec = ndsd->first_pkt[stype].time_sec;
319 fr->len = ndsd->first_pkt[stype].len;
320 ndsd->first_pkt[stype].len = 0;
321 return RTP_FILL_OK;
322 } else {
323 rtp_buff buff;
324 return rtp_fill_buffer(ssrc, fr, &buff);
328 static void stream_add_packet(Nemesi_DemuxerStreamData * ndsd,
329 Nemesi_SessionType stype,
330 demux_stream_t* ds, rtp_frame * fr)
332 demux_packet_t* dp = new_demux_packet(fr->len);
333 memcpy(dp->buffer, fr->data, fr->len);
335 fr->time_sec += ndsd->seek;
336 ndsd->time[stype] = dp->pts = fr->time_sec;
338 ds_add_packet(ds, dp);
341 static int demux_rtp_fill_buffer(demuxer_t* demuxer, demux_stream_t* ds)
343 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
344 Nemesi_SessionType stype;
345 rtp_ssrc * ssrc;
346 rtp_frame fr;
348 if ( (!ndsd->rtsp->rtsp_queue) || (demuxer->stream->eof) ) {
349 mp_msg(MSGT_DEMUX, MSGL_INFO, "End of Stream...\n");
350 demuxer->stream->eof = 1;
351 return 0;
354 memset(&fr, 0, sizeof(fr));
356 stype = DS_TO_STYPE(demuxer, ds);
357 if ( (ssrc = wait_for_packets(ndsd, stype)) == NULL ) {
358 mp_msg(MSGT_DEMUX, MSGL_INFO, "Bye...\n");
359 demuxer->stream->eof = 1;
360 return 0;
363 if(!get_data_for_session(ndsd, stype, ssrc, &fr))
364 stream_add_packet(ndsd, stype, ds, &fr);
365 else {
366 stype = INVERT_STYPE(stype);
368 //Must check if we actually have a stream of the other type
369 if (!ndsd->session[stype])
370 return 1;
372 ds = STYPE_TO_DS(demuxer, stype);
373 ssrc = wait_for_packets(ndsd, stype);
375 if(!get_data_for_session(ndsd, stype, ssrc, &fr))
376 stream_add_packet(ndsd, stype, ds, &fr);
379 return 1;
383 static void demux_close_rtp(demuxer_t* demuxer)
385 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
386 rtsp_ctrl * ctl = ndsd->rtsp;
387 RTSP_Error err;
389 mp_msg(MSGT_DEMUX, MSGL_INFO, "Closing libNemesi RTSP Stream...\n");
391 if (ndsd == NULL)
392 return;
394 free(ndsd);
396 if (rtsp_close(ctl)) {
397 err = rtsp_wait(ctl);
398 if (err.got_error)
399 mp_msg(MSGT_DEMUX, MSGL_ERR,
400 "Error Closing Stream: %s\n",
401 err.message.reply_str);
404 rtsp_uninit(ctl);
407 static void demux_seek_rtp(demuxer_t *demuxer, float rel_seek_secs,
408 float audio_delay, int flags)
410 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
411 rtsp_ctrl * ctl = ndsd->rtsp;
412 sdp_attr * r_attr = NULL;
413 sdp_range r = {0, 0};
414 double time = ndsd->time[NEMESI_SESSION_VIDEO] ?
415 ndsd->time[NEMESI_SESSION_VIDEO] :
416 ndsd->time[NEMESI_SESSION_AUDIO];
418 if (!ctl->rtsp_queue)
419 return;
421 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range");
422 if (r_attr)
423 r = sdp_parse_range(r_attr->value);
425 //flags & 1 -> absolute seek
426 //flags & 2 -> percent seek
427 if (flags == 0) {
428 time += rel_seek_secs;
429 if (time < r.begin)
430 time = r.begin;
431 else if (time > r.end)
432 time = r.end;
433 ndsd->seek = time;
435 mp_msg(MSGT_DEMUX,MSGL_WARN,"libNemesi SEEK %f on %f - %f)\n",
436 time, r.begin, r.end);
438 if (!rtsp_seek(ctl, time, 0)) {
439 RTSP_Error err = rtsp_wait(ctl);
440 if (err.got_error) {
441 mp_msg(MSGT_DEMUX, MSGL_ERR,
442 "Error Performing Seek: %s\n",
443 err.message.reply_str);
444 demuxer->stream->eof = 1;
446 else
447 mp_msg(MSGT_DEMUX, MSGL_INFO, "Seek, performed\n");
449 else {
450 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unable to pause stream to perform seek\n");
451 demuxer->stream->eof = 1;
454 else
455 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unsupported seek type\n");
458 static int demux_rtp_control(struct demuxer *demuxer, int cmd, void *arg)
460 Nemesi_DemuxerStreamData * ndsd = demuxer->priv;
461 rtsp_ctrl * ctl = ndsd->rtsp;
462 sdp_attr * r_attr = NULL;
463 sdp_range r = {0, 0};
464 double time = ndsd->time[NEMESI_SESSION_VIDEO] ?
465 ndsd->time[NEMESI_SESSION_VIDEO] :
466 ndsd->time[NEMESI_SESSION_AUDIO];
468 if (!ctl->rtsp_queue)
469 return DEMUXER_CTRL_DONTKNOW;
471 r_attr = sdp_get_attr(ctl->rtsp_queue->info->attr_list, "range");
472 if (r_attr)
473 r = sdp_parse_range(r_attr->value);
475 switch (cmd) {
476 case DEMUXER_CTRL_GET_TIME_LENGTH:
477 if (r.end == 0)
478 return DEMUXER_CTRL_DONTKNOW;
480 *((double *)arg) = ((double)r.end) - ((double)r.begin);
481 return DEMUXER_CTRL_OK;
483 case DEMUXER_CTRL_GET_PERCENT_POS:
484 if (r.end == 0)
485 return DEMUXER_CTRL_DONTKNOW;
487 *((int *)arg) = (int)( time * 100 / (r.end - r.begin) );
488 return DEMUXER_CTRL_OK;
489 default:
490 return DEMUXER_CTRL_DONTKNOW;
494 const demuxer_desc_t demuxer_desc_rtp_nemesi = {
495 "libnemesi RTP demuxer",
496 "nemesi",
498 "Alessandro Molina",
499 "requires libnemesi",
500 DEMUXER_TYPE_RTP_NEMESI,
501 0, // no autodetect
502 NULL,
503 demux_rtp_fill_buffer,
504 demux_open_rtp,
505 demux_close_rtp,
506 demux_seek_rtp,
507 demux_rtp_control