2 * Copyright (C) 2007 Alessandro Molina <amol.wrk@gmail.com>
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.
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.
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
20 #include "stream/stream.h"
23 #define HAVE_STRUCT_SOCKADDR_STORAGE
24 #include "nemesi/rtsp.h"
25 #include "nemesi/rtp.h"
28 int rtsp_transport_tcp
= 0;
29 int rtsp_transport_sctp
= 0;
37 #define NMS_MAX_FORMATS 16
39 MIMEto4CC supported_audio
[NMS_MAX_FORMATS
] = {
41 {"vorbis", mmioFOURCC('v','r','b','s')},
42 {"mpeg4-generic", mmioFOURCC('M','P','4','A')},
46 MIMEto4CC supported_video
[NMS_MAX_FORMATS
] = {
47 {"MPV", mmioFOURCC('M','P','E','G')},
48 {"theora",mmioFOURCC('t','h','e','o')},
49 {"H264", mmioFOURCC('H','2','6','4')},
50 {"H263-1998", mmioFOURCC('H','2','6','3')},
51 {"MP4V-ES", mmioFOURCC('M','P','4','V')},
55 typedef enum { NEMESI_SESSION_VIDEO
,
56 NEMESI_SESSION_AUDIO
} Nemesi_SessionType
;
60 rtp_session
* session
[2];
61 rtp_frame first_pkt
[2];
64 } Nemesi_DemuxerStreamData
;
67 #define STYPE_TO_DS(demuxer, stype) \
68 ((stype) == NEMESI_SESSION_VIDEO ? (demuxer)->video : (demuxer)->audio)
70 #define DS_TO_STYPE(demuxer, ds) \
71 ((ds) == (demuxer)->video ? NEMESI_SESSION_VIDEO : NEMESI_SESSION_AUDIO)
73 #define INVERT_STYPE(stype) ((stype + 1) % 2)
75 static unsigned int get4CC(MIMEto4CC
* supported_formats
, char const * format
)
79 for(i
= 0; i
< NMS_MAX_FORMATS
; ++i
) {
80 if (!supported_formats
[i
].mime
)
82 else if ( strcmp(supported_formats
[i
].mime
, format
) == 0 )
83 return supported_formats
[i
].fourcc
;
89 static rtp_ssrc
*wait_for_packets(Nemesi_DemuxerStreamData
* ndsd
, Nemesi_SessionType stype
)
91 rtp_ssrc
*ssrc
= NULL
;
93 /* Wait for prebuffering (prebuffering must be enabled in nemesi) */
94 int terminated
= rtp_fill_buffers(rtsp_get_rtp_th(ndsd
->rtsp
));
96 /* Wait for the ssrc to be registered, if prebuffering is on in nemesi
97 this will just get immediatly the correct ssrc */
99 while ( !(ssrc
= rtp_session_get_ssrc(ndsd
->session
[stype
], ndsd
->rtsp
)) )
106 static void link_session_and_fetch_conf(Nemesi_DemuxerStreamData
* ndsd
,
107 Nemesi_SessionType stype
,
109 rtp_buff
* buff
, unsigned int * fps
)
111 extern double force_fps
;
112 rtp_ssrc
*ssrc
= NULL
;
113 rtp_frame
* fr
= &ndsd
->first_pkt
[stype
];
115 int must_prefetch
= ((fps
!= NULL
) || (buff
!= NULL
)) ? 1 : 0;
117 ndsd
->session
[stype
] = sess
;
119 ssrc
= wait_for_packets(ndsd
, stype
);
121 if ( ((ssrc
) && (must_prefetch
)) ) {
125 rtp_fill_buffer(ssrc
, fr
, buff
); //Prefetch the first packet
127 /* Packet prefecthing must be done anyway or we won't be
128 able to get the metadata, but fps calculation happens
129 only if the user didn't specify the FPS */
130 if ( ((!force_fps
) && (fps
!= NULL
)) ) {
131 while ( *fps
<= 0 ) {
132 //Wait more pkts to calculate FPS and try again
134 *fps
= rtp_get_fps(ssrc
);
140 static demuxer_t
* demux_open_rtp(demuxer_t
* demuxer
)
142 nms_rtsp_hints hints
;
143 char * url
= demuxer
->stream
->streaming_ctrl
->url
->url
;
147 Nemesi_DemuxerStreamData
* ndsd
= calloc(1, sizeof(Nemesi_DemuxerStreamData
));
149 memset(&hints
,0,sizeof(hints
));
150 if (rtsp_port
) hints
.first_rtp_port
= rtsp_port
;
151 if (rtsp_transport_tcp
) {
152 hints
.pref_rtsp_proto
= TCP
;
153 hints
.pref_rtp_proto
= TCP
;
155 if (rtsp_transport_sctp
) {
156 hints
.pref_rtsp_proto
= SCTP
;
157 hints
.pref_rtp_proto
= SCTP
;
160 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Initializing libNemesi\n");
161 if ((ctl
= rtsp_init(&hints
)) == NULL
) {
167 demuxer
->priv
= ndsd
;
168 //nms_verbosity_set(1);
170 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Opening: %s\n", url
);
171 if (rtsp_open(ctl
, url
)) {
172 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "rtsp_open failed.\n");
176 reply
= rtsp_wait(ctl
);
177 if (reply
.got_error
) {
178 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
179 "OPEN Error from the server: %s\n",
180 reply
.message
.reply_str
);
184 rtsp_play(ctl
, 0, 0);
185 reply
= rtsp_wait(ctl
);
186 if (reply
.got_error
) {
187 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
188 "PLAY Error from the server: %s\n",
189 reply
.message
.reply_str
);
193 if (!ctl
->rtsp_queue
)
196 media
= ctl
->rtsp_queue
->media_queue
;
197 for (; media
; media
=media
->next
) {
198 sdp_medium_info
* info
= media
->medium_info
;
199 rtp_session
* sess
= media
->rtp_sess
;
202 int media_format
= atoi(info
->fmts
);
203 rtp_pt
* ptinfo
= rtp_get_pt_info(sess
, media_format
);
204 char const * format_name
= ptinfo
? ptinfo
->name
: NULL
;
206 memset(&buff
, 0, sizeof(rtp_buff
));
208 if (sess
->parsers
[media_format
] == NULL
) {
209 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
210 "libNemesi unsupported media format: %s\n",
211 format_name
? format_name
: info
->fmts
);
215 mp_msg(MSGT_DEMUX
, MSGL_INFO
,
216 "libNemesi supported media: %s\n",
220 if (ptinfo
->type
== AU
) {
221 if (ndsd
->session
[NEMESI_SESSION_AUDIO
] == NULL
) {
222 sh_audio_t
* sh_audio
= new_sh_audio(demuxer
,0);
224 demux_stream_t
* d_audio
= demuxer
->audio
;
226 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Detected as AUDIO stream...\n");
228 link_session_and_fetch_conf(ndsd
, NEMESI_SESSION_AUDIO
,
232 wf
= calloc(1,sizeof(WAVEFORMATEX
)+buff
.len
);
233 wf
->cbSize
= buff
.len
;
234 memcpy(wf
+1, buff
.data
, buff
.len
);
236 wf
= calloc(1,sizeof(WAVEFORMATEX
));
240 d_audio
->sh
= sh_audio
;
241 sh_audio
->ds
= d_audio
;
242 wf
->nSamplesPerSec
= 0;
245 sh_audio
->format
= get4CC(supported_audio
, format_name
);
246 if ( !(wf
->wFormatTag
) )
247 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
248 "Unknown MPlayer format code for MIME"
249 " type \"audio/%s\"\n", format_name
);
251 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
252 "There is already an audio session registered,"
255 } else if (ptinfo
->type
== VI
) {
256 if (ndsd
->session
[NEMESI_SESSION_VIDEO
] == NULL
) {
257 sh_video_t
* sh_video
;
258 BITMAPINFOHEADER
* bih
;
259 demux_stream_t
* d_video
;
262 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Detected as VIDEO stream...\n");
264 link_session_and_fetch_conf(ndsd
, NEMESI_SESSION_VIDEO
,
268 bih
= calloc(1,sizeof(BITMAPINFOHEADER
)+buff
.len
);
269 bih
->biSize
= sizeof(BITMAPINFOHEADER
)+buff
.len
;
270 memcpy(bih
+1, buff
.data
, buff
.len
);
272 bih
= calloc(1,sizeof(BITMAPINFOHEADER
));
273 bih
->biSize
= sizeof(BITMAPINFOHEADER
);
276 sh_video
= new_sh_video(demuxer
,0);
278 d_video
= demuxer
->video
;
279 d_video
->sh
= sh_video
;
280 sh_video
->ds
= d_video
;
284 sh_video
->frametime
= 1.0/fps
;
288 sh_video
->format
= get4CC(supported_video
, format_name
);
289 if ( !(bih
->biCompression
) ) {
290 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
291 "Unknown MPlayer format code for MIME"
292 " type \"video/%s\"\n", format_name
);
295 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
296 "There is already a video session registered,"
300 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Unsupported media type\n");
304 demuxer
->stream
->eof
= 0;
309 static int get_data_for_session(Nemesi_DemuxerStreamData
* ndsd
,
310 Nemesi_SessionType stype
, rtp_ssrc
* ssrc
,
313 if (ndsd
->first_pkt
[stype
].len
!= 0) {
314 fr
->data
= ndsd
->first_pkt
[stype
].data
;
315 fr
->time_sec
= ndsd
->first_pkt
[stype
].time_sec
;
316 fr
->len
= ndsd
->first_pkt
[stype
].len
;
317 ndsd
->first_pkt
[stype
].len
= 0;
321 return rtp_fill_buffer(ssrc
, fr
, &buff
);
325 static void stream_add_packet(Nemesi_DemuxerStreamData
* ndsd
,
326 Nemesi_SessionType stype
,
327 demux_stream_t
* ds
, rtp_frame
* fr
)
329 demux_packet_t
* dp
= new_demux_packet(fr
->len
);
330 memcpy(dp
->buffer
, fr
->data
, fr
->len
);
332 fr
->time_sec
+= ndsd
->seek
;
333 ndsd
->time
[stype
] = dp
->pts
= fr
->time_sec
;
335 ds_add_packet(ds
, dp
);
338 static int demux_rtp_fill_buffer(demuxer_t
* demuxer
, demux_stream_t
* ds
)
340 Nemesi_DemuxerStreamData
* ndsd
= demuxer
->priv
;
341 Nemesi_SessionType stype
;
345 if ( (!ndsd
->rtsp
->rtsp_queue
) || (demuxer
->stream
->eof
) ) {
346 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "End of Stream...\n");
347 demuxer
->stream
->eof
= 1;
351 memset(&fr
, 0, sizeof(fr
));
353 stype
= DS_TO_STYPE(demuxer
, ds
);
354 if ( (ssrc
= wait_for_packets(ndsd
, stype
)) == NULL
) {
355 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Bye...\n");
356 demuxer
->stream
->eof
= 1;
360 if(!get_data_for_session(ndsd
, stype
, ssrc
, &fr
))
361 stream_add_packet(ndsd
, stype
, ds
, &fr
);
363 stype
= INVERT_STYPE(stype
);
365 //Must check if we actually have a stream of the other type
366 if (!ndsd
->session
[stype
])
369 ds
= STYPE_TO_DS(demuxer
, stype
);
370 ssrc
= wait_for_packets(ndsd
, stype
);
372 if(!get_data_for_session(ndsd
, stype
, ssrc
, &fr
))
373 stream_add_packet(ndsd
, stype
, ds
, &fr
);
380 static void demux_close_rtp(demuxer_t
* demuxer
)
382 Nemesi_DemuxerStreamData
* ndsd
= demuxer
->priv
;
383 rtsp_ctrl
* ctl
= ndsd
->rtsp
;
386 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Closing libNemesi RTSP Stream...\n");
393 if (rtsp_close(ctl
)) {
394 err
= rtsp_wait(ctl
);
396 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
397 "Error Closing Stream: %s\n",
398 err
.message
.reply_str
);
404 static void demux_seek_rtp(demuxer_t
*demuxer
, float rel_seek_secs
,
405 float audio_delay
, int flags
)
407 Nemesi_DemuxerStreamData
* ndsd
= demuxer
->priv
;
408 rtsp_ctrl
* ctl
= ndsd
->rtsp
;
409 sdp_attr
* r_attr
= NULL
;
410 sdp_range r
= {0, 0};
411 double time
= ndsd
->time
[NEMESI_SESSION_VIDEO
] ?
412 ndsd
->time
[NEMESI_SESSION_VIDEO
] :
413 ndsd
->time
[NEMESI_SESSION_AUDIO
];
415 if (!ctl
->rtsp_queue
)
418 r_attr
= sdp_get_attr(ctl
->rtsp_queue
->info
->attr_list
, "range");
420 r
= sdp_parse_range(r_attr
->value
);
422 //flags & 1 -> absolute seek
423 //flags & 2 -> percent seek
425 time
+= rel_seek_secs
;
428 else if (time
> r
.end
)
432 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"libNemesi SEEK %f on %f - %f)\n",
433 time
, r
.begin
, r
.end
);
435 if (!rtsp_seek(ctl
, time
, 0)) {
436 RTSP_Error err
= rtsp_wait(ctl
);
438 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
439 "Error Performing Seek: %s\n",
440 err
.message
.reply_str
);
441 demuxer
->stream
->eof
= 1;
444 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "Seek, performed\n");
447 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Unable to pause stream to perform seek\n");
448 demuxer
->stream
->eof
= 1;
452 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "Unsupported seek type\n");
455 static int demux_rtp_control(struct demuxer
*demuxer
, int cmd
, void *arg
)
457 Nemesi_DemuxerStreamData
* ndsd
= demuxer
->priv
;
458 rtsp_ctrl
* ctl
= ndsd
->rtsp
;
459 sdp_attr
* r_attr
= NULL
;
460 sdp_range r
= {0, 0};
461 double time
= ndsd
->time
[NEMESI_SESSION_VIDEO
] ?
462 ndsd
->time
[NEMESI_SESSION_VIDEO
] :
463 ndsd
->time
[NEMESI_SESSION_AUDIO
];
465 if (!ctl
->rtsp_queue
)
466 return DEMUXER_CTRL_DONTKNOW
;
468 r_attr
= sdp_get_attr(ctl
->rtsp_queue
->info
->attr_list
, "range");
470 r
= sdp_parse_range(r_attr
->value
);
473 case DEMUXER_CTRL_GET_TIME_LENGTH
:
475 return DEMUXER_CTRL_DONTKNOW
;
477 *((double *)arg
) = ((double)r
.end
) - ((double)r
.begin
);
478 return DEMUXER_CTRL_OK
;
480 case DEMUXER_CTRL_GET_PERCENT_POS
:
482 return DEMUXER_CTRL_DONTKNOW
;
484 *((int *)arg
) = (int)( time
* 100 / (r
.end
- r
.begin
) );
485 return DEMUXER_CTRL_OK
;
487 return DEMUXER_CTRL_DONTKNOW
;
491 const demuxer_desc_t demuxer_desc_rtp_nemesi
= {
492 "libnemesi RTP demuxer",
496 "requires libnemesi",
497 DEMUXER_TYPE_RTP_NEMESI
,
500 demux_rtp_fill_buffer
,