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.
22 #include "stream/stream.h"
25 #define HAVE_STRUCT_SOCKADDR_STORAGE
26 #include "nemesi/rtsp.h"
27 #include "nemesi/rtp.h"
30 int rtsp_transport_tcp
= 0;
31 int rtsp_transport_sctp
= 0;
39 #define NMS_MAX_FORMATS 16
41 MIMEto4CC supported_audio
[NMS_MAX_FORMATS
] = {
43 {"vorbis", mmioFOURCC('v','r','b','s')},
44 {"mpeg4-generic", mmioFOURCC('M','P','4','A')},
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')},
57 typedef enum { NEMESI_SESSION_VIDEO
,
58 NEMESI_SESSION_AUDIO
} Nemesi_SessionType
;
62 rtp_session
* session
[2];
63 rtp_frame first_pkt
[2];
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
)
81 for(i
= 0; i
< NMS_MAX_FORMATS
; ++i
) {
82 if (!supported_formats
[i
].mime
)
84 else if ( strcmp(supported_formats
[i
].mime
, format
) == 0 )
85 return supported_formats
[i
].fourcc
;
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 */
101 while ( !(ssrc
= rtp_session_get_ssrc(ndsd
->session
[stype
], ndsd
->rtsp
)) )
108 static void link_session_and_fetch_conf(Nemesi_DemuxerStreamData
* ndsd
,
109 Nemesi_SessionType stype
,
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
];
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
)) ) {
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
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
;
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
) {
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");
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
);
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
);
195 if (!ctl
->rtsp_queue
)
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
;
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
);
217 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
218 "libNemesi supported media: %s\n",
222 if (ptinfo
->type
== AU
) {
223 if (ndsd
->session
[NEMESI_SESSION_AUDIO
] == NULL
) {
224 sh_audio_t
* sh_audio
= new_sh_audio(demuxer
,0);
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
,
235 wf
= calloc(1,sizeof(WAVEFORMATEX
)+buff
.len
);
236 wf
->cbSize
= buff
.len
;
237 memcpy(wf
+1, buff
.data
, buff
.len
);
239 wf
= calloc(1,sizeof(WAVEFORMATEX
));
243 d_audio
->sh
= sh_audio
;
244 sh_audio
->ds
= d_audio
;
245 wf
->nSamplesPerSec
= 0;
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
);
254 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
255 "There is already an audio session registered,"
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
;
265 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Detected as VIDEO stream...\n");
267 link_session_and_fetch_conf(ndsd
, NEMESI_SESSION_VIDEO
,
271 bih
= calloc(1,sizeof(BITMAPINFOHEADER
)+buff
.len
);
272 bih
->biSize
= sizeof(BITMAPINFOHEADER
)+buff
.len
;
273 memcpy(bih
+1, buff
.data
, buff
.len
);
275 bih
= calloc(1,sizeof(BITMAPINFOHEADER
));
276 bih
->biSize
= sizeof(BITMAPINFOHEADER
);
279 sh_video
= new_sh_video(demuxer
,0);
281 d_video
= demuxer
->video
;
282 d_video
->sh
= sh_video
;
283 sh_video
->ds
= d_video
;
287 sh_video
->frametime
= 1.0/fps
;
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
);
298 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
299 "There is already a video session registered,"
303 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Unsupported media type\n");
307 demuxer
->stream
->eof
= 0;
312 static int get_data_for_session(Nemesi_DemuxerStreamData
* ndsd
,
313 Nemesi_SessionType stype
, rtp_ssrc
* ssrc
,
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;
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
;
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;
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;
363 if(!get_data_for_session(ndsd
, stype
, ssrc
, &fr
))
364 stream_add_packet(ndsd
, stype
, ds
, &fr
);
366 stype
= INVERT_STYPE(stype
);
368 //Must check if we actually have a stream of the other type
369 if (!ndsd
->session
[stype
])
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
);
383 static void demux_close_rtp(demuxer_t
* demuxer
)
385 Nemesi_DemuxerStreamData
* ndsd
= demuxer
->priv
;
386 rtsp_ctrl
* ctl
= ndsd
->rtsp
;
389 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Closing libNemesi RTSP Stream...\n");
396 if (rtsp_close(ctl
)) {
397 err
= rtsp_wait(ctl
);
399 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
400 "Error Closing Stream: %s\n",
401 err
.message
.reply_str
);
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
)
421 r_attr
= sdp_get_attr(ctl
->rtsp_queue
->info
->attr_list
, "range");
423 r
= sdp_parse_range(r_attr
->value
);
425 //flags & 1 -> absolute seek
426 //flags & 2 -> percent seek
428 time
+= rel_seek_secs
;
431 else if (time
> r
.end
)
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
);
441 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
442 "Error Performing Seek: %s\n",
443 err
.message
.reply_str
);
444 demuxer
->stream
->eof
= 1;
447 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Seek, performed\n");
450 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Unable to pause stream to perform seek\n");
451 demuxer
->stream
->eof
= 1;
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");
473 r
= sdp_parse_range(r_attr
->value
);
476 case DEMUXER_CTRL_GET_TIME_LENGTH
:
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
:
485 return DEMUXER_CTRL_DONTKNOW
;
487 *((int *)arg
) = (int)( time
* 100 / (r
.end
- r
.begin
) );
488 return DEMUXER_CTRL_OK
;
490 return DEMUXER_CTRL_DONTKNOW
;
494 const demuxer_desc_t demuxer_desc_rtp_nemesi
= {
495 "libnemesi RTP demuxer",
499 "requires libnemesi",
500 DEMUXER_TYPE_RTP_NEMESI
,
503 demux_rtp_fill_buffer
,