1 /*****************************************************************************
2 * live555.cpp : LIVE555 Streaming Media support.
3 *****************************************************************************
4 * Copyright (C) 2003-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Derk-Jan Hartman <hartman at videolan. org>
9 * Derk-Jan Hartman <djhartman at m2x .dot. nl> for M2X
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_demux.h>
37 #include <vlc_interface.h>
38 #include <vlc_network.h>
44 # include <winsock2.h>
47 #include "UsageEnvironment.hh"
48 #include "BasicUsageEnvironment.hh"
49 #include "GroupsockHelper.hh"
50 #include "liveMedia.hh"
53 #include "../access/mms/asf.h" /* Who said ugly ? */
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
64 #define CACHING_TEXT N_("Caching value (ms)")
65 #define CACHING_LONGTEXT N_( \
66 "Allows you to modify the default caching value for RTSP streams. This " \
67 "value should be set in millisecond units." )
69 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
70 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
71 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
72 "for communication. In this mode you cannot connect to normal RTSP servers." )
74 #define USER_TEXT N_("RTSP user name")
75 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
76 "be used for authenticating the connection.")
77 #define PASS_TEXT N_("RTSP password")
78 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
79 "used for the connection.")
82 set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) );
83 set_capability( "demux2", 50 );
84 set_shortname( "RTP/RTSP");
85 set_callbacks( Open, Close );
86 add_shortcut( "live" );
87 add_shortcut( "livedotcom" );
88 set_category( CAT_INPUT );
89 set_subcategory( SUBCAT_INPUT_DEMUX );
92 set_description( _("RTSP/RTP access and demux") );
93 add_shortcut( "rtsp" );
94 add_shortcut( "sdp" );
95 set_capability( "access_demux", 0 );
96 set_callbacks( Open, Close );
97 add_bool( "rtsp-tcp", 0, NULL,
98 N_("Use RTP over RTSP (TCP)"),
99 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
100 add_integer( "rtp-client-port", -1, NULL,
102 N_("Port to use for the RTP source of the session"), VLC_TRUE );
103 add_bool( "rtsp-http", 0, NULL,
104 N_("Tunnel RTSP and RTP over HTTP"),
105 N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE );
106 add_integer( "rtsp-http-port", 80, NULL,
107 N_("HTTP tunnel port"),
108 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
110 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
111 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
112 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
113 KASENNA_LONGTEXT, VLC_TRUE );
114 add_string( "rtsp-user", NULL, NULL, USER_TEXT,
115 USER_LONGTEXT, VLC_TRUE );
116 add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
117 PASS_LONGTEXT, VLC_TRUE );
121 /*****************************************************************************
123 *****************************************************************************/
128 MediaSubsession *sub;
134 vlc_bool_t b_quicktime;
136 stream_t *p_out_muxed; /* for muxed stream */
139 unsigned int i_buffer;
141 vlc_bool_t b_rtcp_sync;
144 u_int32_t i_start_seq;
148 struct timeout_thread_t
153 vlc_bool_t b_handle_keep_alive;
159 char *p_sdp; /* XXX mallocated */
160 char *psz_path; /* URL-encoded path */
164 TaskScheduler *scheduler;
165 UsageEnvironment *env ;
170 live_track_t **track; /* XXX mallocated */
178 int64_t i_pcr; /* The clock */
179 int64_t i_npt; /* The current time in the stream */
180 int64_t i_npt_length;
183 /* timeout thread information */
184 int i_timeout; /* session timeout value in seconds */
185 vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
186 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
189 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
190 vlc_bool_t b_no_data; /* true if we never receive any data */
191 int i_no_data_ti; /* consecutive number of TaskInterrupt */
196 static int Demux ( demux_t * );
197 static int Control( demux_t *, int, va_list );
199 static int Connect ( demux_t * );
200 static int SessionsSetup( demux_t * );
201 static int Play ( demux_t *);
202 static int ParseASF ( demux_t * );
203 static int RollOverTcp ( demux_t * );
205 static void StreamRead ( void *, unsigned int, unsigned int,
206 struct timeval, unsigned int );
207 static void StreamClose ( void * );
208 static void TaskInterrupt( void * );
210 static void TimeoutPrevention( timeout_thread_t * );
212 static unsigned char* parseH264ConfigStr( char const* configStr,
213 unsigned int& configSize );
215 /*****************************************************************************
217 *****************************************************************************/
218 static int Open ( vlc_object_t *p_this )
220 demux_t *p_demux = (demux_t*)p_this;
221 demux_sys_t *p_sys = NULL;
223 MediaSubsessionIterator *iter = NULL;
224 MediaSubsession *sub = NULL;
226 int i_error = VLC_EGENERIC;
230 /* See if it looks like a SDP
231 v, o, s fields are mandatory and in this order */
232 const uint8_t *p_peek;
233 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
235 if( memcmp( p_peek, "v=0\r\n", 5 ) &&
236 memcmp( p_peek, "v=0\n", 4 ) &&
237 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
244 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
247 p_demux->pf_demux = Demux;
248 p_demux->pf_control= Control;
249 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
250 if( !p_sys ) return VLC_ENOMEM;
253 p_sys->scheduler = NULL;
261 p_sys->i_npt_start = 0;
262 p_sys->i_npt_length = 0;
263 p_sys->p_out_asf = NULL;
264 p_sys->b_no_data = VLC_TRUE;
265 p_sys->i_no_data_ti = 0;
266 p_sys->p_timeout = NULL;
267 p_sys->i_timeout = 0;
268 p_sys->b_timeout_call = VLC_FALSE;
269 p_sys->b_multicast = VLC_FALSE;
270 p_sys->b_real = VLC_FALSE;
271 p_sys->psz_path = strdup( p_demux->psz_path );
273 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
274 vlc_UrlParse( &p_sys->url, p_sys->psz_path, 0 );
276 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
278 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
281 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
283 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
287 if( strcasecmp( p_demux->psz_access, "sdp" ) )
289 char *p = p_sys->psz_path;
290 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
293 if( p_demux->s != NULL )
295 /* Gather the complete sdp file */
297 int i_sdp_max = 1000;
298 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
302 i_error = VLC_ENOMEM;
308 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
309 i_sdp_max - i_sdp - 1 );
311 if( p_demux->b_die || p_demux->b_error )
316 msg_Err( p_demux, "failed to read SDP" );
322 if( i_read < i_sdp_max - i_sdp - 1 )
329 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
331 p_sys->p_sdp = (char*)p_sdp;
333 else if( ( p_demux->s == NULL ) &&
334 !strcasecmp( p_demux->psz_access, "sdp" ) )
336 /* sdp:// link from SAP */
337 p_sys->p_sdp = strdup( p_sys->psz_path );
339 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
341 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
345 if( p_sys->p_sdp == NULL )
347 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
348 i_error = VLC_ENOMEM;
352 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
354 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
358 if( p_sys->b_real ) goto error;
360 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
363 if( p_sys->p_out_asf && ParseASF( p_demux ) )
365 msg_Err( p_demux, "cannot find a usable asf header" );
366 /* TODO Clean tracks */
370 if( p_sys->i_track <= 0 )
376 for( i = 0; i < p_sys->i_track; i++ )
378 live_track_t *tk = p_sys->track[i];
380 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
381 free( tk->p_buffer );
385 if( p_sys->i_track ) free( p_sys->track );
386 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
387 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
388 if( p_sys->ms ) Medium::close( p_sys->ms );
389 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
390 if( p_sys->env ) p_sys->env->reclaim();
391 if( p_sys->p_timeout )
393 vlc_object_kill( p_sys->p_timeout );
394 vlc_thread_join( p_sys->p_timeout );
395 vlc_object_detach( p_sys->p_timeout );
396 vlc_object_destroy( p_sys->p_timeout );
398 if( p_sys->scheduler ) delete p_sys->scheduler;
399 if( p_sys->p_sdp ) free( p_sys->p_sdp );
400 if( p_sys->psz_path ) free( p_sys->psz_path );
402 vlc_UrlClean( &p_sys->url );
408 /*****************************************************************************
410 *****************************************************************************/
411 static void Close( vlc_object_t *p_this )
413 demux_t *p_demux = (demux_t*)p_this;
414 demux_sys_t *p_sys = p_demux->p_sys;
417 for( i = 0; i < p_sys->i_track; i++ )
419 live_track_t *tk = p_sys->track[i];
421 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
422 free( tk->p_buffer );
426 if( p_sys->i_track ) free( p_sys->track );
427 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
428 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
429 if( p_sys->ms ) Medium::close( p_sys->ms );
430 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
431 if( p_sys->env ) p_sys->env->reclaim();
432 if( p_sys->p_timeout )
434 vlc_object_kill( p_sys->p_timeout );
435 vlc_thread_join( p_sys->p_timeout );
436 vlc_object_detach( p_sys->p_timeout );
437 vlc_object_destroy( p_sys->p_timeout );
439 if( p_sys->scheduler ) delete p_sys->scheduler;
440 if( p_sys->p_sdp ) free( p_sys->p_sdp );
441 if( p_sys->psz_path ) free( p_sys->psz_path );
443 vlc_UrlClean( &p_sys->url );
448 /*****************************************************************************
449 * Connect: connects to the RTSP server to setup the session DESCRIBE
450 *****************************************************************************/
451 static int Connect( demux_t *p_demux )
453 demux_sys_t *p_sys = p_demux->p_sys;
454 Authenticator authenticator;
456 char *psz_user = NULL;
457 char *psz_pwd = NULL;
458 char *psz_url = NULL;
459 char *psz_options = NULL;
462 int i_ret = VLC_SUCCESS;
464 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
465 if( !psz_url ) return VLC_ENOMEM;
467 if( p_sys->url.psz_username || p_sys->url.psz_password )
469 sprintf( psz_url, "rtsp://%s%s", p_sys->url.psz_host,
470 p_sys->url.psz_path );
472 psz_user = strdup( p_sys->url.psz_username );
473 psz_pwd = strdup( p_sys->url.psz_password );
477 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
479 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
480 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
484 if( p_demux->b_die || p_demux->b_error )
492 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
493 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
495 if( ( p_sys->rtsp = RTSPClient::createNew( *p_sys->env,
496 p_demux->p_libvlc->i_verbose > 1,
497 "VLC media player", i_http_port ) ) == NULL )
499 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
500 p_sys->env->getResultMsg() );
507 /* Kasenna enables KeepAlive by analysing the User-Agent string.
508 * Appending _KA to the string should be enough to enable this feature,
509 * however, there is a bug where the _KA doesn't get parsed from the
510 * default User-Agent as created by VLC/Live555 code. This is probably due
511 * to spaces in the string or the string being too long. Here we override
512 * the default string with a more compact version.
514 if( var_CreateGetBool( p_demux, "rtsp-kasenna" ))
516 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
517 p_sys->rtsp->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
522 authenticator.setUsernameAndPassword( (const char*)psz_user,
523 (const char*)psz_pwd );
525 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url, psz_user, psz_pwd,
527 if( psz_options ) delete [] psz_options;
529 p_sdp = p_sys->rtsp->describeURL( psz_url, &authenticator,
530 var_GetBool( p_demux, "rtsp-kasenna" ) );
533 /* failure occurred */
535 const char *psz_error = p_sys->env->getResultMsg();
537 if( var_GetBool( p_demux, "rtsp-http" ) )
538 sscanf( psz_error, "%*s %*s HTTP GET %*s HTTP/%*u.%*u %3u %*s",
540 else sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
541 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
546 msg_Dbg( p_demux, "authentication failed" );
548 if( psz_user ) free( psz_user );
549 if( psz_pwd ) free( psz_pwd );
550 psz_user = psz_pwd = NULL;
552 i_result = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
553 _("Please enter a valid login name and a password."),
554 &psz_user, &psz_pwd );
555 if( i_result == DIALOG_OK_YES )
557 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
562 else if( (i_code != 0) && !var_GetBool( p_demux, "rtsp-http" ) )
564 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
566 val.b_bool = VLC_TRUE;
567 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
568 var_Set( p_demux, "rtsp-http", val );
569 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
575 msg_Dbg( p_demux, "connection timeout, retrying" );
576 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
580 i_ret = VLC_EGENERIC;
583 /* malloc-ated copy */
584 if( psz_url ) free( psz_url );
585 if( psz_user ) free( psz_user );
586 if( psz_pwd ) free( psz_pwd );
588 if( p_sys->p_sdp ) free( p_sys->p_sdp );
590 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
596 /*****************************************************************************
597 * SessionsSetup: prepares the subsessions and does the SETUP
598 *****************************************************************************/
599 static int SessionsSetup( demux_t *p_demux )
601 demux_sys_t *p_sys = p_demux->p_sys;
602 MediaSubsessionIterator *iter = NULL;
603 MediaSubsession *sub = NULL;
605 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
607 int i_return = VLC_SUCCESS;
608 unsigned int i_buffer = 0;
609 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
611 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" ) ||
612 var_GetBool( p_demux, "rtsp-http" );
613 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
615 /* Create the session from the SDP */
616 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
618 msg_Err( p_demux, "Could not create the RTSP Session: %s",
619 p_sys->env->getResultMsg() );
623 /* Initialise each media subsession */
624 iter = new MediaSubsessionIterator( *p_sys->ms );
625 while( ( sub = iter->next() ) != NULL )
630 /* Value taken from mplayer */
631 if( !strcmp( sub->mediumName(), "audio" ) )
633 else if( !strcmp( sub->mediumName(), "video" ) )
637 if( i_client_port != -1 )
639 sub->setClientPortNum( i_client_port );
643 if( strcasestr( sub->codecName(), "REAL" ) )
645 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
646 p_sys->b_real = VLC_TRUE; /* This is a problem, we'll handle it later */
650 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
651 bInit = sub->initiate( 4 ); /* Constant ? */
653 bInit = sub->initiate();
657 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
658 sub->mediumName(), sub->codecName(),
659 p_sys->env->getResultMsg() );
663 if( sub->rtpSource() != NULL )
665 int fd = sub->rtpSource()->RTPgs()->socketNum();
667 /* Increase the buffer size */
669 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
671 /* Increase the RTP reorder timebuffer just a bit */
672 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
674 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
677 /* Issue the SETUP */
680 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
681 b_rtsp_tcp ? True : False ) ) )
683 /* if we get an unsupported transport error, toggle TCP use and try again */
684 if( !strstr(p_sys->env->getResultMsg(), "461 Unsupported Transport")
685 || !( p_sys->rtsp->setupMediaSubsession( *sub, False,
686 b_rtsp_tcp ? False : True ) ) )
688 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
689 sub->codecName(), p_sys->env->getResultMsg() );
695 /* Check if we will receive data from this subsession for this track */
696 if( sub->readSource() == NULL ) continue;
697 if( !p_sys->b_multicast )
699 /* Check, because we need diff. rollover behaviour for multicast */
700 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
703 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
704 if( !tk ) return VLC_ENOMEM;
705 tk->p_demux = p_demux;
708 tk->b_quicktime = VLC_FALSE;
709 tk->b_asf = VLC_FALSE;
710 tk->b_muxed = VLC_FALSE;
711 tk->p_out_muxed = NULL;
713 tk->b_rtcp_sync = VLC_FALSE;
715 tk->i_buffer = 65536;
716 tk->p_buffer = (uint8_t *)malloc( 65536 );
717 if( !tk->p_buffer ) return VLC_ENOMEM;
719 /* Value taken from mplayer */
720 if( !strcmp( sub->mediumName(), "audio" ) )
722 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
723 tk->fmt.audio.i_channels = sub->numChannels();
724 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
726 if( !strcmp( sub->codecName(), "MPA" ) ||
727 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
728 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
730 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
731 tk->fmt.audio.i_rate = 0;
733 else if( !strcmp( sub->codecName(), "AC3" ) )
735 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
736 tk->fmt.audio.i_rate = 0;
738 else if( !strcmp( sub->codecName(), "L16" ) )
740 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
741 tk->fmt.audio.i_bitspersample = 16;
743 else if( !strcmp( sub->codecName(), "L8" ) )
745 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
746 tk->fmt.audio.i_bitspersample = 8;
748 else if( !strcmp( sub->codecName(), "PCMU" ) )
750 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
752 else if( !strcmp( sub->codecName(), "PCMA" ) )
754 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
756 else if( !strncmp( sub->codecName(), "G726", 4 ) )
758 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
759 tk->fmt.audio.i_rate = 8000;
760 tk->fmt.audio.i_channels = 1;
761 if( !strcmp( sub->codecName()+5, "40" ) )
762 tk->fmt.i_bitrate = 40000;
763 else if( !strcmp( sub->codecName()+5, "32" ) )
764 tk->fmt.i_bitrate = 32000;
765 else if( !strcmp( sub->codecName()+5, "24" ) )
766 tk->fmt.i_bitrate = 24000;
767 else if( !strcmp( sub->codecName()+5, "16" ) )
768 tk->fmt.i_bitrate = 16000;
770 else if( !strcmp( sub->codecName(), "AMR" ) )
772 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
774 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
776 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
778 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
780 unsigned int i_extra;
783 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
785 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
788 tk->fmt.i_extra = i_extra;
789 tk->fmt.p_extra = malloc( i_extra );
790 memcpy( tk->fmt.p_extra, p_extra, i_extra );
793 /* Because the "faad" decoder does not handle the LATM data length field
794 at the start of each returned LATM frame, tell the RTP source to omit it. */
795 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
797 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
799 unsigned int i_extra;
802 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
804 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
807 tk->fmt.i_extra = i_extra;
808 tk->fmt.p_extra = malloc( i_extra );
809 memcpy( tk->fmt.p_extra, p_extra, i_extra );
813 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
815 tk->b_asf = VLC_TRUE;
816 if( p_sys->p_out_asf == NULL )
817 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
820 else if( !strcmp( sub->codecName(), "X-QT" ) ||
821 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
823 tk->b_quicktime = VLC_TRUE;
825 else if( !strcmp( sub->codecName(), "SPEEX" ) )
827 tk->fmt.i_codec = VLC_FOURCC( 's', 'p', 'x', 'r' );
828 if ( sub->rtpTimestampFrequency() )
829 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
832 msg_Warn( p_demux,"Using 8kHz as default sample rate." );
833 tk->fmt.audio.i_rate = 8000;
837 else if( !strcmp( sub->mediumName(), "video" ) )
839 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
840 if( !strcmp( sub->codecName(), "MPV" ) )
842 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
844 else if( !strcmp( sub->codecName(), "H263" ) ||
845 !strcmp( sub->codecName(), "H263-1998" ) ||
846 !strcmp( sub->codecName(), "H263-2000" ) )
848 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
850 else if( !strcmp( sub->codecName(), "H261" ) )
852 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
854 else if( !strcmp( sub->codecName(), "H264" ) )
856 unsigned int i_extra = 0;
857 uint8_t *p_extra = NULL;
859 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
860 tk->fmt.b_packetized = VLC_FALSE;
862 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
865 tk->fmt.i_extra = i_extra;
866 tk->fmt.p_extra = malloc( i_extra );
867 memcpy( tk->fmt.p_extra, p_extra, i_extra );
872 else if( !strcmp( sub->codecName(), "JPEG" ) )
874 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
876 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
878 unsigned int i_extra;
881 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
883 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
886 tk->fmt.i_extra = i_extra;
887 tk->fmt.p_extra = malloc( i_extra );
888 memcpy( tk->fmt.p_extra, p_extra, i_extra );
892 else if( !strcmp( sub->codecName(), "X-QT" ) ||
893 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
894 !strcmp( sub->codecName(), "X-QDM" ) ||
895 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
896 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
898 tk->b_quicktime = VLC_TRUE;
900 else if( !strcmp( sub->codecName(), "MP2T" ) )
902 tk->b_muxed = VLC_TRUE;
903 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
905 else if( !strcmp( sub->codecName(), "MP2P" ) ||
906 !strcmp( sub->codecName(), "MP1S" ) )
908 tk->b_muxed = VLC_TRUE;
909 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
912 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
914 tk->b_asf = VLC_TRUE;
915 if( p_sys->p_out_asf == NULL )
916 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
921 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
923 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
926 if( sub->rtcpInstance() != NULL )
928 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
931 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
934 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
935 p_sys->track[p_sys->i_track++] = tk;
940 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
946 if( p_sys->i_track <= 0 ) i_return = VLC_EGENERIC;
948 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
949 /* Retrieve the starttime if possible */
950 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
952 p_sys->i_npt_start = (int64_t) -1;
954 if( p_sys->i_npt_start < 0 )
955 p_sys->i_npt_start = -1;
957 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
958 /* Retrieve the duration if possible */
959 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
961 p_sys->i_npt_length = (int64_t) -1;
963 if( p_sys->i_npt_length < 0 )
964 p_sys->i_npt_length = -1;
966 msg_Dbg( p_demux, "setup start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
970 /*****************************************************************************
971 * Play: starts the actual playback of the stream
972 *****************************************************************************/
973 static int Play( demux_t *p_demux )
975 demux_sys_t *p_sys = p_demux->p_sys;
981 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, p_sys->i_npt_start, -1, 1 ) )
983 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
987 /* Retrieve the timeout value and set up a timeout prevention thread */
988 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
989 if( p_sys->i_timeout <= 0 )
990 p_sys->i_timeout = 60; /* default value from RFC2326 */
992 /* start timeout-thread only on x-asf streams (wms), it has rtcp support but doesn't
993 * seem to use it for liveness/keep-alive, get_parameter seems to work for it. get_parameter
994 * doesn't work with dss 5.5.4 & 5.5.5, they seems to work with rtcp */
995 if( !p_sys->p_timeout && p_sys->p_out_asf )
997 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
998 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
999 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
1000 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
1001 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
1003 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
1004 vlc_object_destroy( p_sys->p_timeout );
1006 msg_Dbg( p_demux, "spawned timeout thread" );
1007 vlc_object_attach( p_sys->p_timeout, p_demux );
1012 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1013 for( i = 0; i < p_sys->i_track; i++ )
1015 if( !p_sys->track[i]->b_rtcp_sync )
1016 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1017 p_sys->track[i]->i_start_seq = (int)p_sys->track[i]->sub->rtpInfo.seqNum;
1018 msg_Info( p_demux, "set startseq: %u", p_sys->track[i]->i_start_seq );
1022 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1023 /* Retrieve the starttime if possible */
1024 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1026 p_sys->i_npt_start = -1;
1028 if( p_sys->i_npt_start < 0 )
1030 p_sys->i_npt_start = -1;
1034 p_sys->i_npt = p_sys->i_npt_start;
1036 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1037 /* Retrieve the duration if possible */
1038 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1040 p_sys->i_npt_length = -1;
1042 if( p_sys->i_npt_length < 0 )
1043 p_sys->i_npt_length = -1;
1045 msg_Dbg( p_demux, "play start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1050 /*****************************************************************************
1052 *****************************************************************************/
1053 static int Demux( demux_t *p_demux )
1055 demux_sys_t *p_sys = p_demux->p_sys;
1058 vlc_bool_t b_send_pcr = VLC_TRUE;
1062 /* Check if we need to send the server a Keep-A-Live signal */
1063 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
1065 char *psz_bye = NULL;
1066 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
1067 p_sys->b_timeout_call = VLC_FALSE;
1071 for( i = 0; i < p_sys->i_track; i++ )
1073 live_track_t *tk = p_sys->track[i];
1075 if( tk->b_asf || tk->b_muxed )
1076 b_send_pcr = VLC_FALSE;
1082 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
1088 if( p_sys->i_pcr > 0 )
1091 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1094 /* First warn we want to read data */
1096 for( i = 0; i < p_sys->i_track; i++ )
1098 live_track_t *tk = p_sys->track[i];
1100 if( tk->waiting == 0 )
1103 tk->sub->readSource()->getNextFrame( tk->p_buffer, tk->i_buffer,
1104 StreamRead, tk, StreamClose, tk );
1107 /* Create a task that will be called if we wait more than 300ms */
1108 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1111 p_sys->scheduler->doEventLoop( &p_sys->event );
1113 /* remove the task */
1114 p_sys->scheduler->unscheduleDelayedTask( task );
1116 /* Check for gap in pts value */
1117 for( i = 0; i < p_sys->i_track; i++ )
1119 live_track_t *tk = p_sys->track[i];
1121 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1122 tk->sub->rtpSource() && tk->sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1124 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1126 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1127 tk->b_rtcp_sync = VLC_TRUE;
1135 if( p_sys->b_multicast && p_sys->b_no_data &&
1136 ( p_sys->i_no_data_ti > 120 ) )
1138 /* FIXME Make this configurable
1139 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1143 else if( !p_sys->b_multicast && p_sys->b_no_data &&
1144 ( p_sys->i_no_data_ti > 34 ) )
1146 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" ) ||
1147 var_GetBool( p_demux, "rtsp-http" );
1149 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1151 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1152 if( RollOverTcp( p_demux ) )
1154 msg_Err( p_demux, "TCP rollover failed, aborting" );
1158 else if( p_sys->i_no_data_ti > 34 )
1160 msg_Err( p_demux, "no data received in 10s, aborting" );
1164 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1167 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1170 return p_demux->b_error ? 0 : 1;
1173 /*****************************************************************************
1175 *****************************************************************************/
1176 static int Control( demux_t *p_demux, int i_query, va_list args )
1178 demux_sys_t *p_sys = p_demux->p_sys;
1181 vlc_bool_t *pb, *pb2, b_bool;
1186 case DEMUX_GET_TIME:
1187 pi64 = (int64_t*)va_arg( args, int64_t * );
1188 if( p_sys->i_npt > 0 )
1190 *pi64 = p_sys->i_npt;
1193 return VLC_EGENERIC;
1195 case DEMUX_GET_LENGTH:
1196 pi64 = (int64_t*)va_arg( args, int64_t * );
1197 if( p_sys->i_npt_length > 0 )
1199 *pi64 = p_sys->i_npt_length;
1202 return VLC_EGENERIC;
1204 case DEMUX_GET_POSITION:
1205 pf = (double*)va_arg( args, double* );
1206 if( p_sys->i_npt_length > 0 && p_sys->i_npt > 0 )
1208 *pf = (double)p_sys->i_npt / (double)p_sys->i_npt_length;
1211 return VLC_EGENERIC;
1213 case DEMUX_SET_POSITION:
1217 f = (double)va_arg( args, double );
1218 if( p_sys->rtsp && p_sys->i_npt_length > 0 )
1221 time = f * (double)p_sys->i_npt_length / (double)1000000.0; /* in second */
1222 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time, -1, 1 ) )
1224 msg_Err( p_demux, "PLAY failed %s",
1225 p_sys->env->getResultMsg() );
1226 return VLC_EGENERIC;
1228 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1230 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1231 /* Retrieve RTP-Info values */
1232 for( i = 0; i < p_sys->i_track; i++ )
1234 //if( !p_sys->track[i]->b_rtcp_sync )
1235 p_sys->track[i]->b_rtcp_sync = VLC_FALSE;
1236 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1237 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1238 msg_Info( p_demux, "set pos startseq: %u", p_sys->track[i]->i_start_seq );
1241 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1242 /* Retrieve the starttime if possible */
1243 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1245 p_sys->i_npt_start = -1;
1247 if( p_sys->i_npt_start < 0 )
1249 p_sys->i_npt_start = -1;
1253 p_sys->i_npt = p_sys->i_npt_start;
1255 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1256 /* Retrieve the duration if possible */
1257 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1259 p_sys->i_npt_length = -1;
1261 if( p_sys->i_npt_length < 0 )
1262 p_sys->i_npt_length = -1;
1264 msg_Dbg( p_demux, "seek start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1267 return VLC_EGENERIC;
1269 /* Special for access_demux */
1270 case DEMUX_CAN_PAUSE:
1271 case DEMUX_CAN_SEEK:
1272 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1273 if( p_sys->rtsp && p_sys->i_npt_length )
1274 /* Not always true, but will be handled in SET_PAUSE_STATE */
1280 case DEMUX_CAN_CONTROL_PACE:
1281 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1283 #if 1 /* Disable for now until we have a clock synchro algo
1284 * which works with something else than MPEG over UDP */
1292 case DEMUX_CAN_CONTROL_RATE:
1293 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1294 pb2 = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1296 *pb = p_sys->rtsp != NULL && p_sys->i_npt_length > 0 && !var_GetBool( p_demux, "rtsp-kasenna" );
1300 case DEMUX_SET_RATE:
1304 if( !p_sys->rtsp || p_sys->i_npt_length <= 0 || var_GetBool( p_demux, "rtsp-kasenna" ) )
1305 return VLC_EGENERIC;
1307 /* TODO we might want to ensure that the new rate is different from
1308 * old rate after playMediaSession...
1309 * I have no idea how the server map the requested rate to the
1312 * current is x2 we request x1.5 if the server return x2 we will
1313 * never succeed to return to x1.
1314 * In this case we should retry with a lower rate until we have
1318 pi_int = (int*)va_arg( args, int * );
1319 f_scale = (double)INPUT_RATE_DEFAULT / (*p_int);
1321 /* Passing -1 for the start and end time will mean liveMedia won't
1322 * create a Range: section for the RTSP message. The server should
1323 * pick up from the current position */
1324 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, -1, -1, f_scale ) )
1326 msg_Err( p_demux, "PLAY with Scale %0.2f failed %s", f_scale,
1327 p_sys->env->getResultMsg() );
1328 return VLC_EGENERIC;
1330 /* ReSync the stream */
1331 p_sys->i_npt_start = 0;
1333 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1335 *pi_int = (int)( INPUT_RATE_DEFAULT / p_sys->ms->scale() + 0.5 );
1340 case DEMUX_SET_PAUSE_STATE:
1342 double d_npt = (double) p_sys->i_npt / I64C(1000000);
1345 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1346 if( p_sys->rtsp == NULL )
1347 return VLC_EGENERIC;
1350 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1351 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1352 d_npt > 0 ? d_npt : -1 ) ) )
1354 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1355 return VLC_EGENERIC;
1358 /* When we Pause, we'll need the TimeoutPrevention thread to
1359 * handle sending the "Keep Alive" message to the server.
1360 * Unfortunately Live555 isn't thread safe and so can't
1361 * do this normally while the main Demux thread is handling
1362 * a live stream. We end up with the Timeout thread blocking
1363 * waiting for a response from the server. So when we PAUSE
1364 * we set a flag that the TimeoutPrevention function will check
1365 * and if it's set, it will trigger the GET_PARAMETER message */
1366 if( b_bool && p_sys->p_timeout != NULL )
1367 p_sys->p_timeout->b_handle_keep_alive = VLC_TRUE;
1368 else if( !b_bool && p_sys->p_timeout != NULL )
1369 p_sys->p_timeout->b_handle_keep_alive = VLC_FALSE;
1371 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1373 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1374 for( i = 0; i < p_sys->i_track; i++ )
1376 if( !p_sys->track[i]->b_rtcp_sync )
1377 p_sys->track[i]->i_pts = 0; // (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1378 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1379 msg_Info( p_demux, "set pause startseq: %u", p_sys->track[i]->i_start_seq );
1382 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1383 /* Retrieve the starttime if possible */
1384 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1386 p_sys->i_npt_start = -1;
1388 if( p_sys->i_npt_start < 0 )
1390 p_sys->i_npt_start = -1;
1394 p_sys->i_npt = p_sys->i_npt_start;
1396 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1397 /* Retrieve the duration if possible */
1398 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1400 p_sys->i_npt_length = -1;
1402 if( p_sys->i_npt_length < 0 )
1403 p_sys->i_npt_length = -1;
1405 msg_Dbg( p_demux, "pause start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1408 case DEMUX_GET_TITLE_INFO:
1409 case DEMUX_SET_TITLE:
1410 case DEMUX_SET_SEEKPOINT:
1411 return VLC_EGENERIC;
1413 case DEMUX_GET_PTS_DELAY:
1414 pi64 = (int64_t*)va_arg( args, int64_t * );
1415 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1419 return VLC_EGENERIC;
1423 /*****************************************************************************
1424 * RollOverTcp: reopen the rtsp into TCP mode
1425 * XXX: ugly, a lot of code are duplicated from Open()
1426 * This should REALLY be fixed
1427 *****************************************************************************/
1428 static int RollOverTcp( demux_t *p_demux )
1430 demux_sys_t *p_sys = p_demux->p_sys;
1433 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1435 /* We close the old RTSP session */
1436 for( i = 0; i < p_sys->i_track; i++ )
1438 live_track_t *tk = p_sys->track[i];
1440 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
1441 free( tk->p_buffer );
1444 if( p_sys->i_track ) free( p_sys->track );
1445 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
1447 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1448 Medium::close( p_sys->ms );
1449 RTSPClient::close( p_sys->rtsp );
1453 p_sys->track = NULL;
1456 /* Reopen rtsp client */
1457 if( ( p_demux->s != NULL ) &&
1458 ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1460 msg_Err( p_demux, "Failed to connect with rtsp://%s",
1465 if( p_sys->p_sdp == NULL )
1467 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1471 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1473 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1477 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1483 return VLC_EGENERIC;
1487 /*****************************************************************************
1489 *****************************************************************************/
1490 static void StreamRead( void *p_private, unsigned int i_size,
1491 unsigned int i_truncated_bytes, struct timeval pts,
1492 unsigned int duration )
1494 live_track_t *tk = (live_track_t*)p_private;
1495 demux_t *p_demux = tk->p_demux;
1496 demux_sys_t *p_sys = p_demux->p_sys;
1499 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1501 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1502 (uint64_t)pts.tv_usec;
1504 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1505 i_pts &= UI64C(0x00ffffffffffffff);
1507 if( tk->b_quicktime && tk->p_es == NULL )
1509 QuickTimeGenericRTPSource *qtRTPSource =
1510 (QuickTimeGenericRTPSource*)tk->sub->rtpSource();
1511 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1512 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1514 if( tk->fmt.i_cat == VIDEO_ES ) {
1515 if( qtState.sdAtomSize < 16 + 32 )
1518 p_sys->event = 0xff;
1522 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1523 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1524 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1526 if( tk->fmt.i_codec == VLC_FOURCC('a', 'v', 'c', '1') )
1528 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
1529 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
1530 + qtRTPSource->qtState.sdAtomSize;
1531 while (pos+8 < endpos) {
1532 unsigned int atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
1533 if( atomLength == 0 || atomLength > (unsigned int)(endpos-pos)) break;
1534 if( memcmp(pos+4, "avcC", 4) == 0 &&
1536 atomLength <= INT_MAX )
1538 tk->fmt.i_extra = atomLength-8;
1539 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1540 memcpy(tk->fmt.p_extra, pos+8, atomLength-8);
1548 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1549 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1550 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1554 if( qtState.sdAtomSize < 4 )
1557 p_sys->event = 0xff;
1561 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1563 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1567 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1569 pts.tv_sec * 1000000LL + pts.tv_usec );
1572 /* grow buffer if it looks like buffer is too small, but don't eat
1573 * up all the memory on strange streams */
1574 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1577 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1578 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1580 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1583 msg_Warn( p_demux, "realloc failed" );
1587 tk->p_buffer = (uint8_t*)p_tmp;
1590 if( i_size > tk->i_buffer )
1592 msg_Warn( p_demux, "buffer overflow" );
1594 /* FIXME could i_size be > buffer size ? */
1595 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1596 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1598 AMRAudioSource *amrSource = (AMRAudioSource*)tk->sub->readSource();
1600 p_block = block_New( p_demux, i_size + 1 );
1601 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1602 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1604 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1606 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1607 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->sub->rtpSource();
1608 uint32_t header = h261Source->lastSpecialHeader();
1610 uint32_t header = 0;
1611 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1613 p_block = block_New( p_demux, i_size + 4 );
1614 memcpy( p_block->p_buffer, &header, 4 );
1615 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1617 if( tk->sub->rtpSource()->curPacketMarkerBit() )
1618 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1620 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1622 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1623 msg_Warn( p_demux, "unsupported NAL type for H264" );
1625 /* Normal NAL type */
1626 p_block = block_New( p_demux, i_size + 4 );
1627 p_block->p_buffer[0] = 0x00;
1628 p_block->p_buffer[1] = 0x00;
1629 p_block->p_buffer[2] = 0x00;
1630 p_block->p_buffer[3] = 0x01;
1631 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1633 else if( tk->b_asf )
1635 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1636 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1638 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1642 p_block = block_New( p_demux, i_size );
1643 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1647 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1648 if( (tk->fmt.i_cat == VIDEO_ES) && (p_sys->i_pcr < i_pts) &&
1649 (i_pts > 0) && (p_sys->i_pcr > 0) )
1651 p_sys->i_npt += __MAX( 0, i_pts - p_sys->i_pcr );
1652 p_sys->i_pcr = i_pts;
1653 //msg_Dbg( p_demux, "npt update" );
1655 else if( /*tk->fmt.i_cat == VIDEO_ES &&*/ p_sys->i_pcr < i_pts )
1657 p_sys->i_pcr = i_pts;
1659 //msg_Dbg( p_demux, "npt %lld", p_sys->i_npt );
1661 if( (i_pts != tk->i_pts) && (!tk->b_muxed) )
1663 p_block->i_pts = i_pts;
1667 /*FIXME: for h264 you should check that packetization-mode=1 in sdp-file */
1668 p_block->i_dts = ( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ) ? 0 : i_pts;
1673 stream_DemuxSend( tk->p_out_muxed, p_block );
1675 else if( tk->b_asf )
1677 stream_DemuxSend( p_sys->p_out_asf, p_block );
1681 es_out_Send( p_demux->out, tk->p_es, p_block );
1684 /* warn that's ok */
1685 p_sys->event = 0xff;
1687 /* we have read data */
1689 p_demux->p_sys->b_no_data = VLC_FALSE;
1690 p_demux->p_sys->i_no_data_ti = 0;
1692 if( i_pts > 0 && !tk->b_muxed )
1698 /*****************************************************************************
1700 *****************************************************************************/
1701 static void StreamClose( void *p_private )
1703 live_track_t *tk = (live_track_t*)p_private;
1704 demux_t *p_demux = tk->p_demux;
1705 demux_sys_t *p_sys = p_demux->p_sys;
1707 msg_Dbg( p_demux, "StreamClose" );
1709 p_sys->event = 0xff;
1710 p_demux->b_error = VLC_TRUE;
1714 /*****************************************************************************
1716 *****************************************************************************/
1717 static void TaskInterrupt( void *p_private )
1719 demux_t *p_demux = (demux_t*)p_private;
1721 p_demux->p_sys->i_no_data_ti++;
1724 p_demux->p_sys->event = 0xff;
1727 /*****************************************************************************
1729 *****************************************************************************/
1730 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1732 p_timeout->b_die = VLC_FALSE;
1733 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1734 p_timeout->i_remain *= 1000000;
1736 vlc_thread_ready( p_timeout );
1739 while( !p_timeout->b_die )
1741 if( p_timeout->i_remain <= 0 )
1743 char *psz_bye = NULL;
1744 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1745 p_timeout->i_remain *= 1000000;
1746 msg_Dbg( p_timeout, "reset the timeout timer" );
1747 if( p_timeout->b_handle_keep_alive == VLC_TRUE )
1749 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
1750 p_timeout->p_sys->rtsp->getMediaSessionParameter( *p_timeout->p_sys->ms, NULL, psz_bye );
1752 p_timeout->p_sys->b_timeout_call = VLC_FALSE;
1756 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1759 p_timeout->i_remain -= 200000;
1760 msleep( 200000 ); /* 200 ms */
1764 /*****************************************************************************
1766 *****************************************************************************/
1767 static int b64_decode( char *dest, char *src );
1769 static int ParseASF( demux_t *p_demux )
1771 demux_sys_t *p_sys = p_demux->p_sys;
1773 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1774 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1778 /* Parse the asf header */
1779 if( psz_asf == NULL )
1780 return VLC_EGENERIC;
1782 psz_asf += strlen( psz_marker );
1783 psz_asf = strdup( psz_asf ); /* Duplicate it */
1784 psz_end = strchr( psz_asf, '\n' );
1786 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1789 if( psz_asf >= psz_end )
1792 return VLC_EGENERIC;
1795 /* Always smaller */
1796 p_header = block_New( p_demux, psz_end - psz_asf );
1797 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1798 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1799 if( p_header->i_buffer <= 0 )
1802 return VLC_EGENERIC;
1805 /* Parse it to get packet size */
1806 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1808 /* Send it to demuxer */
1809 stream_DemuxSend( p_sys->p_out_asf, p_header );
1816 static unsigned char* parseH264ConfigStr( char const* configStr,
1817 unsigned int& configSize )
1820 int i, i_records = 1;
1825 if( configStr == NULL || *configStr == '\0' )
1828 psz = dup = strdup( configStr );
1830 /* Count the number of comma's */
1831 for( psz = dup; *psz != '\0'; ++psz )
1840 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1842 for( i = 0; i < i_records; i++ )
1844 cfg[configSize++] = 0x00;
1845 cfg[configSize++] = 0x00;
1846 cfg[configSize++] = 0x00;
1847 cfg[configSize++] = 0x01;
1849 configSize += b64_decode( (char*)&cfg[configSize], psz );
1850 psz += strlen(psz)+1;
1853 if( dup ) free( dup );
1857 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1858 static int b64_decode( char *dest, char *src )
1860 const char *dest_start = dest;
1864 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1865 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1866 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1867 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1868 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1869 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1870 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1871 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1872 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1873 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1874 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1875 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1876 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1877 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1878 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1879 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1882 for( i_level = 0; *src != '\0'; src++ )
1886 c = b64[(unsigned int)*src];
1898 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1902 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1906 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1914 return dest - dest_start;