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>
45 # include <winsock2.h>
48 #include "UsageEnvironment.hh"
49 #include "BasicUsageEnvironment.hh"
50 #include "GroupsockHelper.hh"
51 #include "liveMedia.hh"
54 #include "../access/mms/asf.h" /* Who said ugly ? */
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close( vlc_object_t * );
65 #define CACHING_TEXT N_("Caching value (ms)")
66 #define CACHING_LONGTEXT N_( \
67 "Allows you to modify the default caching value for RTSP streams. This " \
68 "value should be set in millisecond units." )
70 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
71 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
72 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
73 "for communication. In this mode you cannot connect to normal RTSP servers." )
75 #define USER_TEXT N_("RTSP user name")
76 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
77 "be used for authenticating the connection.")
78 #define PASS_TEXT N_("RTSP password")
79 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
80 "used for the connection.")
83 set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) );
84 set_capability( "demux2", 50 );
85 set_shortname( "RTP/RTSP");
86 set_callbacks( Open, Close );
87 add_shortcut( "live" );
88 add_shortcut( "livedotcom" );
89 set_category( CAT_INPUT );
90 set_subcategory( SUBCAT_INPUT_DEMUX );
93 set_description( _("RTSP/RTP access and demux") );
94 add_shortcut( "rtsp" );
95 add_shortcut( "sdp" );
96 set_capability( "access_demux", 0 );
97 set_callbacks( Open, Close );
98 add_bool( "rtsp-tcp", 0, NULL,
99 N_("Use RTP over RTSP (TCP)"),
100 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
101 add_integer( "rtp-client-port", -1, NULL,
103 N_("Port to use for the RTP source of the session"), VLC_TRUE );
104 add_bool( "rtsp-http", 0, NULL,
105 N_("Tunnel RTSP and RTP over HTTP"),
106 N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE );
107 add_integer( "rtsp-http-port", 80, NULL,
108 N_("HTTP tunnel port"),
109 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
111 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
112 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
113 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
114 KASENNA_LONGTEXT, VLC_TRUE );
115 add_string( "rtsp-user", NULL, NULL, USER_TEXT,
116 USER_LONGTEXT, VLC_TRUE );
117 add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
118 PASS_LONGTEXT, VLC_TRUE );
122 /*****************************************************************************
124 *****************************************************************************/
129 MediaSubsession *sub;
135 vlc_bool_t b_quicktime;
137 stream_t *p_out_muxed; /* for muxed stream */
140 unsigned int i_buffer;
142 vlc_bool_t b_rtcp_sync;
145 u_int32_t i_start_seq;
149 struct timeout_thread_t
154 vlc_bool_t b_handle_keep_alive;
160 char *p_sdp; /* XXX mallocated */
161 char *psz_path; /* URL-encoded path */
165 TaskScheduler *scheduler;
166 UsageEnvironment *env ;
171 live_track_t **track; /* XXX mallocated */
179 int64_t i_pcr; /* The clock */
180 int64_t i_npt; /* The current time in the stream */
181 int64_t i_npt_length;
184 /* timeout thread information */
185 int i_timeout; /* session timeout value in seconds */
186 vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
187 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
190 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
191 vlc_bool_t b_no_data; /* true if we never receive any data */
192 int i_no_data_ti; /* consecutive number of TaskInterrupt */
197 static int Demux ( demux_t * );
198 static int Control( demux_t *, int, va_list );
200 static int Connect ( demux_t * );
201 static int SessionsSetup( demux_t * );
202 static int Play ( demux_t *);
203 static int ParseASF ( demux_t * );
204 static int RollOverTcp ( demux_t * );
206 static void StreamRead ( void *, unsigned int, unsigned int,
207 struct timeval, unsigned int );
208 static void StreamClose ( void * );
209 static void TaskInterrupt( void * );
211 static void TimeoutPrevention( timeout_thread_t * );
213 static unsigned char* parseH264ConfigStr( char const* configStr,
214 unsigned int& configSize );
216 /*****************************************************************************
218 *****************************************************************************/
219 static int Open ( vlc_object_t *p_this )
221 demux_t *p_demux = (demux_t*)p_this;
222 demux_sys_t *p_sys = NULL;
224 MediaSubsessionIterator *iter = NULL;
225 MediaSubsession *sub = NULL;
227 int i_error = VLC_EGENERIC;
231 /* See if it looks like a SDP
232 v, o, s fields are mandatory and in this order */
233 const uint8_t *p_peek;
234 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
236 if( memcmp( p_peek, "v=0\r\n", 5 ) &&
237 memcmp( p_peek, "v=0\n", 4 ) &&
238 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
245 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
248 p_demux->pf_demux = Demux;
249 p_demux->pf_control= Control;
250 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
251 if( !p_sys ) return VLC_ENOMEM;
254 p_sys->scheduler = NULL;
262 p_sys->i_npt_start = 0;
263 p_sys->i_npt_length = 0;
264 p_sys->p_out_asf = NULL;
265 p_sys->b_no_data = VLC_TRUE;
266 p_sys->i_no_data_ti = 0;
267 p_sys->p_timeout = NULL;
268 p_sys->i_timeout = 0;
269 p_sys->b_timeout_call = VLC_FALSE;
270 p_sys->b_multicast = VLC_FALSE;
271 p_sys->b_real = VLC_FALSE;
272 p_sys->psz_path = strdup( p_demux->psz_path );
274 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
275 vlc_UrlParse( &p_sys->url, p_sys->psz_path, 0 );
277 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
279 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
282 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
284 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
288 if( strcasecmp( p_demux->psz_access, "sdp" ) )
290 char *p = p_sys->psz_path;
291 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
294 if( p_demux->s != NULL )
296 /* Gather the complete sdp file */
298 int i_sdp_max = 1000;
299 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
303 i_error = VLC_ENOMEM;
309 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
310 i_sdp_max - i_sdp - 1 );
312 if( p_demux->b_die || p_demux->b_error )
320 msg_Err( p_demux, "failed to read SDP" );
327 if( i_read < i_sdp_max - i_sdp - 1 )
334 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
336 p_sys->p_sdp = (char*)p_sdp;
338 else if( ( p_demux->s == NULL ) &&
339 !strcasecmp( p_demux->psz_access, "sdp" ) )
341 /* sdp:// link from SAP */
342 p_sys->p_sdp = strdup( p_sys->psz_path );
344 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
346 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
350 if( p_sys->p_sdp == NULL )
352 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
353 i_error = VLC_ENOMEM;
357 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
359 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
363 if( p_sys->b_real ) goto error;
365 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
368 if( p_sys->p_out_asf && ParseASF( p_demux ) )
370 msg_Err( p_demux, "cannot find a usable asf header" );
371 /* TODO Clean tracks */
375 if( p_sys->i_track <= 0 )
381 for( i = 0; i < p_sys->i_track; i++ )
383 live_track_t *tk = p_sys->track[i];
385 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
386 free( tk->p_buffer );
390 if( p_sys->i_track ) free( p_sys->track );
391 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
392 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
393 if( p_sys->ms ) Medium::close( p_sys->ms );
394 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
395 if( p_sys->env ) p_sys->env->reclaim();
396 if( p_sys->p_timeout )
398 vlc_object_kill( p_sys->p_timeout );
399 vlc_thread_join( p_sys->p_timeout );
400 vlc_object_detach( p_sys->p_timeout );
401 vlc_object_release( p_sys->p_timeout );
403 delete p_sys->scheduler;
404 free( p_sys->p_sdp );
405 free( p_sys->psz_path );
407 vlc_UrlClean( &p_sys->url );
413 /*****************************************************************************
415 *****************************************************************************/
416 static void Close( vlc_object_t *p_this )
418 demux_t *p_demux = (demux_t*)p_this;
419 demux_sys_t *p_sys = p_demux->p_sys;
422 for( i = 0; i < p_sys->i_track; i++ )
424 live_track_t *tk = p_sys->track[i];
426 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
427 free( tk->p_buffer );
431 if( p_sys->i_track ) free( p_sys->track );
432 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
433 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
434 if( p_sys->ms ) Medium::close( p_sys->ms );
435 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
436 if( p_sys->env ) p_sys->env->reclaim();
437 if( p_sys->p_timeout )
439 vlc_object_kill( p_sys->p_timeout );
440 vlc_thread_join( p_sys->p_timeout );
441 vlc_object_detach( p_sys->p_timeout );
442 vlc_object_release( p_sys->p_timeout );
444 delete p_sys->scheduler;
445 free( p_sys->p_sdp );
446 free( p_sys->psz_path );
448 vlc_UrlClean( &p_sys->url );
453 /*****************************************************************************
454 * Connect: connects to the RTSP server to setup the session DESCRIBE
455 *****************************************************************************/
456 static int Connect( demux_t *p_demux )
458 demux_sys_t *p_sys = p_demux->p_sys;
459 Authenticator authenticator;
460 vlc_bool_t b_firstpass = VLC_TRUE;
462 char *psz_user = NULL;
463 char *psz_pwd = NULL;
464 char *psz_url = NULL;
465 char *psz_options = NULL;
468 int i_ret = VLC_SUCCESS;
470 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
471 if( !psz_url ) return VLC_ENOMEM;
473 if( p_sys->url.psz_username || p_sys->url.psz_password )
475 sprintf( psz_url, "rtsp://%s%s", p_sys->url.psz_host,
476 p_sys->url.psz_path );
478 psz_user = strdup( p_sys->url.psz_username );
479 psz_pwd = strdup( p_sys->url.psz_password );
483 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
485 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
486 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
490 if( p_demux->b_die || p_demux->b_error )
498 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
499 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
501 if( ( p_sys->rtsp = RTSPClient::createNew( *p_sys->env,
502 p_demux->p_libvlc->i_verbose > 1,
503 "VLC media player", i_http_port ) ) == NULL )
505 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
506 p_sys->env->getResultMsg() );
513 /* Kasenna enables KeepAlive by analysing the User-Agent string.
514 * Appending _KA to the string should be enough to enable this feature,
515 * however, there is a bug where the _KA doesn't get parsed from the
516 * default User-Agent as created by VLC/Live555 code. This is probably due
517 * to spaces in the string or the string being too long. Here we override
518 * the default string with a more compact version.
520 if( var_CreateGetBool( p_demux, "rtsp-kasenna" ))
522 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
523 p_sys->rtsp->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
528 authenticator.setUsernameAndPassword( (const char*)psz_user,
529 (const char*)psz_pwd );
531 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url, psz_user, psz_pwd,
533 if( psz_options ) delete [] psz_options;
535 p_sdp = p_sys->rtsp->describeURL( psz_url, &authenticator,
536 var_GetBool( p_demux, "rtsp-kasenna" ) );
539 /* failure occurred */
541 const char *psz_error = p_sys->env->getResultMsg();
543 if( var_GetBool( p_demux, "rtsp-http" ) )
544 sscanf( psz_error, "%*s %*s HTTP GET %*s HTTP/%*u.%*u %3u %*s",
548 const char *psz_tmp = strstr( psz_error, "RTSP" );
549 sscanf( psz_tmp, "RTSP/%*s%3u", &i_code );
551 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
554 { /* describeURL always returns an "RTSP/1.0 401 Unauthorized" the
555 * first time. This is a workaround to avoid asking for a
556 * user/passwd the first time the code passess here. */
558 b_firstpass = VLC_FALSE;
564 msg_Dbg( p_demux, "authentication failed" );
568 psz_user = psz_pwd = NULL;
570 i_result = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
571 _("Please enter a valid login name and a password."),
572 &psz_user, &psz_pwd );
573 if( i_result == DIALOG_OK_YES )
575 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
580 else if( (i_code != 0) && !var_GetBool( p_demux, "rtsp-http" ) )
582 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
584 val.b_bool = VLC_TRUE;
585 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
586 var_Set( p_demux, "rtsp-http", val );
587 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
593 msg_Dbg( p_demux, "connection timeout, retrying" );
594 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
598 i_ret = VLC_EGENERIC;
601 /* malloc-ated copy */
606 free( p_sys->p_sdp );
608 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
614 /*****************************************************************************
615 * SessionsSetup: prepares the subsessions and does the SETUP
616 *****************************************************************************/
617 static int SessionsSetup( demux_t *p_demux )
619 demux_sys_t *p_sys = p_demux->p_sys;
620 MediaSubsessionIterator *iter = NULL;
621 MediaSubsession *sub = NULL;
623 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
625 int i_return = VLC_SUCCESS;
626 unsigned int i_buffer = 0;
627 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
629 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" ) ||
630 var_GetBool( p_demux, "rtsp-http" );
631 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
633 /* Create the session from the SDP */
634 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
636 msg_Err( p_demux, "Could not create the RTSP Session: %s",
637 p_sys->env->getResultMsg() );
641 /* Initialise each media subsession */
642 iter = new MediaSubsessionIterator( *p_sys->ms );
643 while( ( sub = iter->next() ) != NULL )
648 if( p_demux->b_die || p_demux->b_error )
654 /* Value taken from mplayer */
655 if( !strcmp( sub->mediumName(), "audio" ) )
657 else if( !strcmp( sub->mediumName(), "video" ) )
661 if( i_client_port != -1 )
663 sub->setClientPortNum( i_client_port );
667 if( strcasestr( sub->codecName(), "REAL" ) )
669 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
670 p_sys->b_real = VLC_TRUE; /* This is a problem, we'll handle it later */
674 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
675 bInit = sub->initiate( 4 ); /* Constant ? */
677 bInit = sub->initiate();
681 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
682 sub->mediumName(), sub->codecName(),
683 p_sys->env->getResultMsg() );
687 if( sub->rtpSource() != NULL )
689 int fd = sub->rtpSource()->RTPgs()->socketNum();
691 /* Increase the buffer size */
693 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
695 /* Increase the RTP reorder timebuffer just a bit */
696 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
698 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
701 /* Issue the SETUP */
704 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
705 b_rtsp_tcp ? True : False ) ) )
707 /* if we get an unsupported transport error, toggle TCP use and try again */
708 if( !strstr(p_sys->env->getResultMsg(), "461 Unsupported Transport")
709 || !( p_sys->rtsp->setupMediaSubsession( *sub, False,
710 b_rtsp_tcp ? False : True ) ) )
712 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
713 sub->codecName(), p_sys->env->getResultMsg() );
719 /* Check if we will receive data from this subsession for this track */
720 if( sub->readSource() == NULL ) continue;
721 if( !p_sys->b_multicast )
723 /* Check, because we need diff. rollover behaviour for multicast */
724 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
727 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
733 tk->p_demux = p_demux;
736 tk->b_quicktime = VLC_FALSE;
737 tk->b_asf = VLC_FALSE;
738 tk->b_muxed = VLC_FALSE;
739 tk->p_out_muxed = NULL;
741 tk->b_rtcp_sync = VLC_FALSE;
743 tk->i_buffer = 65536;
744 tk->p_buffer = (uint8_t *)malloc( 65536 );
751 /* Value taken from mplayer */
752 if( !strcmp( sub->mediumName(), "audio" ) )
754 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
755 tk->fmt.audio.i_channels = sub->numChannels();
756 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
758 if( !strcmp( sub->codecName(), "MPA" ) ||
759 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
760 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
762 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
763 tk->fmt.audio.i_rate = 0;
765 else if( !strcmp( sub->codecName(), "AC3" ) )
767 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
768 tk->fmt.audio.i_rate = 0;
770 else if( !strcmp( sub->codecName(), "L16" ) )
772 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
773 tk->fmt.audio.i_bitspersample = 16;
775 else if( !strcmp( sub->codecName(), "L8" ) )
777 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
778 tk->fmt.audio.i_bitspersample = 8;
780 else if( !strcmp( sub->codecName(), "PCMU" ) )
782 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
784 else if( !strcmp( sub->codecName(), "PCMA" ) )
786 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
788 else if( !strncmp( sub->codecName(), "G726", 4 ) )
790 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
791 tk->fmt.audio.i_rate = 8000;
792 tk->fmt.audio.i_channels = 1;
793 if( !strcmp( sub->codecName()+5, "40" ) )
794 tk->fmt.i_bitrate = 40000;
795 else if( !strcmp( sub->codecName()+5, "32" ) )
796 tk->fmt.i_bitrate = 32000;
797 else if( !strcmp( sub->codecName()+5, "24" ) )
798 tk->fmt.i_bitrate = 24000;
799 else if( !strcmp( sub->codecName()+5, "16" ) )
800 tk->fmt.i_bitrate = 16000;
802 else if( !strcmp( sub->codecName(), "AMR" ) )
804 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
806 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
808 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
810 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
812 unsigned int i_extra;
815 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
817 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
820 tk->fmt.i_extra = i_extra;
821 tk->fmt.p_extra = malloc( i_extra );
822 memcpy( tk->fmt.p_extra, p_extra, i_extra );
825 /* Because the "faad" decoder does not handle the LATM data length field
826 at the start of each returned LATM frame, tell the RTP source to omit it. */
827 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
829 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
831 unsigned int i_extra;
834 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
836 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
839 tk->fmt.i_extra = i_extra;
840 tk->fmt.p_extra = malloc( i_extra );
841 memcpy( tk->fmt.p_extra, p_extra, i_extra );
845 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
847 tk->b_asf = VLC_TRUE;
848 if( p_sys->p_out_asf == NULL )
849 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
852 else if( !strcmp( sub->codecName(), "X-QT" ) ||
853 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
855 tk->b_quicktime = VLC_TRUE;
857 else if( !strcmp( sub->codecName(), "SPEEX" ) )
859 tk->fmt.i_codec = VLC_FOURCC( 's', 'p', 'x', 'r' );
860 if ( sub->rtpTimestampFrequency() )
861 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
864 msg_Warn( p_demux,"Using 8kHz as default sample rate." );
865 tk->fmt.audio.i_rate = 8000;
869 else if( !strcmp( sub->mediumName(), "video" ) )
871 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
872 if( !strcmp( sub->codecName(), "MPV" ) )
874 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
876 else if( !strcmp( sub->codecName(), "H263" ) ||
877 !strcmp( sub->codecName(), "H263-1998" ) ||
878 !strcmp( sub->codecName(), "H263-2000" ) )
880 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
882 else if( !strcmp( sub->codecName(), "H261" ) )
884 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
886 else if( !strcmp( sub->codecName(), "H264" ) )
888 unsigned int i_extra = 0;
889 uint8_t *p_extra = NULL;
891 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
892 tk->fmt.b_packetized = VLC_FALSE;
894 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
897 tk->fmt.i_extra = i_extra;
898 tk->fmt.p_extra = malloc( i_extra );
899 memcpy( tk->fmt.p_extra, p_extra, i_extra );
904 else if( !strcmp( sub->codecName(), "JPEG" ) )
906 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
908 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
910 unsigned int i_extra;
913 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
915 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
918 tk->fmt.i_extra = i_extra;
919 tk->fmt.p_extra = malloc( i_extra );
920 memcpy( tk->fmt.p_extra, p_extra, i_extra );
924 else if( !strcmp( sub->codecName(), "X-QT" ) ||
925 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
926 !strcmp( sub->codecName(), "X-QDM" ) ||
927 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
928 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
930 tk->b_quicktime = VLC_TRUE;
932 else if( !strcmp( sub->codecName(), "MP2T" ) )
934 tk->b_muxed = VLC_TRUE;
935 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
937 else if( !strcmp( sub->codecName(), "MP2P" ) ||
938 !strcmp( sub->codecName(), "MP1S" ) )
940 tk->b_muxed = VLC_TRUE;
941 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
944 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
946 tk->b_asf = VLC_TRUE;
947 if( p_sys->p_out_asf == NULL )
948 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
953 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
955 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
958 if( sub->rtcpInstance() != NULL )
960 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
963 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
966 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
967 p_sys->track[p_sys->i_track++] = tk;
972 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
978 if( p_sys->i_track <= 0 ) i_return = VLC_EGENERIC;
980 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
981 /* Retrieve the starttime if possible */
982 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
984 p_sys->i_npt_start = (int64_t) -1;
986 if( p_sys->i_npt_start < 0 )
987 p_sys->i_npt_start = -1;
989 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
990 /* Retrieve the duration if possible */
991 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
993 p_sys->i_npt_length = (int64_t) -1;
995 if( p_sys->i_npt_length < 0 )
996 p_sys->i_npt_length = -1;
998 msg_Dbg( p_demux, "setup start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1002 /*****************************************************************************
1003 * Play: starts the actual playback of the stream
1004 *****************************************************************************/
1005 static int Play( demux_t *p_demux )
1007 demux_sys_t *p_sys = p_demux->p_sys;
1013 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, p_sys->i_npt_start, -1, 1 ) )
1015 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
1016 return VLC_EGENERIC;
1019 /* Retrieve the timeout value and set up a timeout prevention thread */
1020 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
1021 if( p_sys->i_timeout <= 0 )
1022 p_sys->i_timeout = 60; /* default value from RFC2326 */
1024 /* start timeout-thread only on x-asf streams (wms), it has rtcp support but doesn't
1025 * seem to use it for liveness/keep-alive, get_parameter seems to work for it. get_parameter
1026 * doesn't work with dss 5.5.4 & 5.5.5, they seems to work with rtcp */
1027 if( !p_sys->p_timeout && p_sys->p_out_asf )
1029 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
1030 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
1031 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
1032 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
1033 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
1035 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
1036 vlc_object_release( p_sys->p_timeout );
1038 msg_Dbg( p_demux, "spawned timeout thread" );
1039 vlc_object_attach( p_sys->p_timeout, p_demux );
1044 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1045 for( i = 0; i < p_sys->i_track; i++ )
1047 if( !p_sys->track[i]->b_rtcp_sync )
1048 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1049 p_sys->track[i]->i_start_seq = (int)p_sys->track[i]->sub->rtpInfo.seqNum;
1050 msg_Info( p_demux, "set startseq: %u", p_sys->track[i]->i_start_seq );
1054 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1055 /* Retrieve the starttime if possible */
1056 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1058 p_sys->i_npt_start = -1;
1060 if( p_sys->i_npt_start < 0 )
1062 p_sys->i_npt_start = -1;
1066 p_sys->i_npt = p_sys->i_npt_start;
1068 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1069 /* Retrieve the duration if possible */
1070 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1072 p_sys->i_npt_length = -1;
1074 if( p_sys->i_npt_length < 0 )
1075 p_sys->i_npt_length = -1;
1077 msg_Dbg( p_demux, "play start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1082 /*****************************************************************************
1084 *****************************************************************************/
1085 static int Demux( demux_t *p_demux )
1087 demux_sys_t *p_sys = p_demux->p_sys;
1090 vlc_bool_t b_send_pcr = VLC_TRUE;
1094 /* Check if we need to send the server a Keep-A-Live signal */
1095 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
1097 char *psz_bye = NULL;
1098 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
1099 p_sys->b_timeout_call = VLC_FALSE;
1103 for( i = 0; i < p_sys->i_track; i++ )
1105 live_track_t *tk = p_sys->track[i];
1107 if( tk->b_asf || tk->b_muxed )
1108 b_send_pcr = VLC_FALSE;
1114 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
1120 if( p_sys->i_pcr > 0 )
1123 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1126 /* First warn we want to read data */
1128 for( i = 0; i < p_sys->i_track; i++ )
1130 live_track_t *tk = p_sys->track[i];
1132 if( tk->waiting == 0 )
1135 tk->sub->readSource()->getNextFrame( tk->p_buffer, tk->i_buffer,
1136 StreamRead, tk, StreamClose, tk );
1139 /* Create a task that will be called if we wait more than 300ms */
1140 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1143 p_sys->scheduler->doEventLoop( &p_sys->event );
1145 /* remove the task */
1146 p_sys->scheduler->unscheduleDelayedTask( task );
1148 /* Check for gap in pts value */
1149 for( i = 0; i < p_sys->i_track; i++ )
1151 live_track_t *tk = p_sys->track[i];
1153 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1154 tk->sub->rtpSource() && tk->sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1156 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1158 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1159 tk->b_rtcp_sync = VLC_TRUE;
1167 if( p_sys->b_multicast && p_sys->b_no_data &&
1168 ( p_sys->i_no_data_ti > 120 ) )
1170 /* FIXME Make this configurable
1171 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1175 else if( !p_sys->b_multicast && p_sys->b_no_data &&
1176 ( p_sys->i_no_data_ti > 34 ) )
1178 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" ) ||
1179 var_GetBool( p_demux, "rtsp-http" );
1181 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1183 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1184 if( RollOverTcp( p_demux ) )
1186 msg_Err( p_demux, "TCP rollover failed, aborting" );
1190 else if( p_sys->i_no_data_ti > 34 )
1192 msg_Err( p_demux, "no data received in 10s, aborting" );
1196 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1199 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1202 return p_demux->b_error ? 0 : 1;
1205 /*****************************************************************************
1207 *****************************************************************************/
1208 static int Control( demux_t *p_demux, int i_query, va_list args )
1210 demux_sys_t *p_sys = p_demux->p_sys;
1213 vlc_bool_t *pb, *pb2, b_bool;
1218 case DEMUX_GET_TIME:
1219 pi64 = (int64_t*)va_arg( args, int64_t * );
1220 if( p_sys->i_npt > 0 )
1222 *pi64 = p_sys->i_npt;
1225 return VLC_EGENERIC;
1227 case DEMUX_GET_LENGTH:
1228 pi64 = (int64_t*)va_arg( args, int64_t * );
1229 if( p_sys->i_npt_length > 0 )
1231 *pi64 = p_sys->i_npt_length;
1234 return VLC_EGENERIC;
1236 case DEMUX_GET_POSITION:
1237 pf = (double*)va_arg( args, double* );
1238 if( p_sys->i_npt_length > 0 && p_sys->i_npt > 0 )
1240 *pf = (double)p_sys->i_npt / (double)p_sys->i_npt_length;
1243 return VLC_EGENERIC;
1245 case DEMUX_SET_POSITION:
1249 f = (double)va_arg( args, double );
1250 if( p_sys->rtsp && p_sys->i_npt_length > 0 )
1253 time = f * (double)p_sys->i_npt_length / (double)1000000.0; /* in second */
1254 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time, -1, 1 ) )
1256 msg_Err( p_demux, "PLAY failed %s",
1257 p_sys->env->getResultMsg() );
1258 return VLC_EGENERIC;
1260 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1262 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1263 /* Retrieve RTP-Info values */
1264 for( i = 0; i < p_sys->i_track; i++ )
1266 //if( !p_sys->track[i]->b_rtcp_sync )
1267 p_sys->track[i]->b_rtcp_sync = VLC_FALSE;
1268 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1269 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1270 msg_Info( p_demux, "set pos startseq: %u", p_sys->track[i]->i_start_seq );
1273 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1274 /* Retrieve the starttime if possible */
1275 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1277 p_sys->i_npt_start = -1;
1279 if( p_sys->i_npt_start < 0 )
1281 p_sys->i_npt_start = -1;
1285 p_sys->i_npt = p_sys->i_npt_start;
1287 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1288 /* Retrieve the duration if possible */
1289 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1291 p_sys->i_npt_length = -1;
1293 if( p_sys->i_npt_length < 0 )
1294 p_sys->i_npt_length = -1;
1296 msg_Dbg( p_demux, "seek start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1299 return VLC_EGENERIC;
1301 /* Special for access_demux */
1302 case DEMUX_CAN_PAUSE:
1303 case DEMUX_CAN_SEEK:
1304 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1305 if( p_sys->rtsp && p_sys->i_npt_length )
1306 /* Not always true, but will be handled in SET_PAUSE_STATE */
1312 case DEMUX_CAN_CONTROL_PACE:
1313 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1315 #if 1 /* Disable for now until we have a clock synchro algo
1316 * which works with something else than MPEG over UDP */
1324 case DEMUX_CAN_CONTROL_RATE:
1325 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1326 pb2 = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1328 *pb = p_sys->rtsp != NULL && p_sys->i_npt_length > 0 && !var_GetBool( p_demux, "rtsp-kasenna" );
1332 case DEMUX_SET_RATE:
1336 if( !p_sys->rtsp || p_sys->i_npt_length <= 0 || var_GetBool( p_demux, "rtsp-kasenna" ) )
1337 return VLC_EGENERIC;
1339 /* TODO we might want to ensure that the new rate is different from
1340 * old rate after playMediaSession...
1341 * I have no idea how the server map the requested rate to the
1344 * current is x2 we request x1.5 if the server return x2 we will
1345 * never succeed to return to x1.
1346 * In this case we should retry with a lower rate until we have
1350 pi_int = (int*)va_arg( args, int * );
1351 f_scale = (double)INPUT_RATE_DEFAULT / (*p_int);
1353 /* Passing -1 for the start and end time will mean liveMedia won't
1354 * create a Range: section for the RTSP message. The server should
1355 * pick up from the current position */
1356 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, -1, -1, f_scale ) )
1358 msg_Err( p_demux, "PLAY with Scale %0.2f failed %s", f_scale,
1359 p_sys->env->getResultMsg() );
1360 return VLC_EGENERIC;
1362 /* ReSync the stream */
1363 p_sys->i_npt_start = 0;
1365 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1367 *pi_int = (int)( INPUT_RATE_DEFAULT / p_sys->ms->scale() + 0.5 );
1372 case DEMUX_SET_PAUSE_STATE:
1374 double d_npt = (double) p_sys->i_npt / I64C(1000000);
1377 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1378 if( p_sys->rtsp == NULL )
1379 return VLC_EGENERIC;
1382 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1383 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1384 d_npt > 0 ? d_npt : -1 ) ) )
1386 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1387 return VLC_EGENERIC;
1390 /* When we Pause, we'll need the TimeoutPrevention thread to
1391 * handle sending the "Keep Alive" message to the server.
1392 * Unfortunately Live555 isn't thread safe and so can't
1393 * do this normally while the main Demux thread is handling
1394 * a live stream. We end up with the Timeout thread blocking
1395 * waiting for a response from the server. So when we PAUSE
1396 * we set a flag that the TimeoutPrevention function will check
1397 * and if it's set, it will trigger the GET_PARAMETER message */
1398 if( b_bool && p_sys->p_timeout != NULL )
1399 p_sys->p_timeout->b_handle_keep_alive = VLC_TRUE;
1400 else if( !b_bool && p_sys->p_timeout != NULL )
1401 p_sys->p_timeout->b_handle_keep_alive = VLC_FALSE;
1403 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1405 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1406 for( i = 0; i < p_sys->i_track; i++ )
1408 if( !p_sys->track[i]->b_rtcp_sync )
1409 p_sys->track[i]->i_pts = 0; // (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1410 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1411 msg_Info( p_demux, "set pause startseq: %u", p_sys->track[i]->i_start_seq );
1414 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1415 /* Retrieve the starttime if possible */
1416 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1418 p_sys->i_npt_start = -1;
1420 if( p_sys->i_npt_start < 0 )
1422 p_sys->i_npt_start = -1;
1426 p_sys->i_npt = p_sys->i_npt_start;
1428 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1429 /* Retrieve the duration if possible */
1430 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1432 p_sys->i_npt_length = -1;
1434 if( p_sys->i_npt_length < 0 )
1435 p_sys->i_npt_length = -1;
1437 msg_Dbg( p_demux, "pause start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1440 case DEMUX_GET_TITLE_INFO:
1441 case DEMUX_SET_TITLE:
1442 case DEMUX_SET_SEEKPOINT:
1443 return VLC_EGENERIC;
1445 case DEMUX_GET_PTS_DELAY:
1446 pi64 = (int64_t*)va_arg( args, int64_t * );
1447 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1451 return VLC_EGENERIC;
1455 /*****************************************************************************
1456 * RollOverTcp: reopen the rtsp into TCP mode
1457 * XXX: ugly, a lot of code are duplicated from Open()
1458 * This should REALLY be fixed
1459 *****************************************************************************/
1460 static int RollOverTcp( demux_t *p_demux )
1462 demux_sys_t *p_sys = p_demux->p_sys;
1465 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1467 /* We close the old RTSP session */
1468 for( i = 0; i < p_sys->i_track; i++ )
1470 live_track_t *tk = p_sys->track[i];
1472 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
1473 free( tk->p_buffer );
1476 if( p_sys->i_track ) free( p_sys->track );
1477 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
1479 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1480 Medium::close( p_sys->ms );
1481 RTSPClient::close( p_sys->rtsp );
1485 p_sys->track = NULL;
1488 /* Reopen rtsp client */
1489 if( ( p_demux->s != NULL ) &&
1490 ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1492 msg_Err( p_demux, "Failed to connect with rtsp://%s",
1497 if( p_sys->p_sdp == NULL )
1499 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1503 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1505 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1509 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1515 return VLC_EGENERIC;
1519 /*****************************************************************************
1521 *****************************************************************************/
1522 static void StreamRead( void *p_private, unsigned int i_size,
1523 unsigned int i_truncated_bytes, struct timeval pts,
1524 unsigned int duration )
1526 live_track_t *tk = (live_track_t*)p_private;
1527 demux_t *p_demux = tk->p_demux;
1528 demux_sys_t *p_sys = p_demux->p_sys;
1531 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1533 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1534 (uint64_t)pts.tv_usec;
1536 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1537 i_pts &= UI64C(0x00ffffffffffffff);
1539 if( tk->b_quicktime && tk->p_es == NULL )
1541 QuickTimeGenericRTPSource *qtRTPSource =
1542 (QuickTimeGenericRTPSource*)tk->sub->rtpSource();
1543 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1544 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1546 if( tk->fmt.i_cat == VIDEO_ES ) {
1547 if( qtState.sdAtomSize < 16 + 32 )
1550 p_sys->event = 0xff;
1554 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1555 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1556 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1558 if( tk->fmt.i_codec == VLC_FOURCC('a', 'v', 'c', '1') )
1560 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
1561 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
1562 + qtRTPSource->qtState.sdAtomSize;
1563 while (pos+8 < endpos) {
1564 unsigned int atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
1565 if( atomLength == 0 || atomLength > (unsigned int)(endpos-pos)) break;
1566 if( memcmp(pos+4, "avcC", 4) == 0 &&
1568 atomLength <= INT_MAX )
1570 tk->fmt.i_extra = atomLength-8;
1571 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1572 memcpy(tk->fmt.p_extra, pos+8, atomLength-8);
1580 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1581 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1582 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1586 if( qtState.sdAtomSize < 4 )
1589 p_sys->event = 0xff;
1593 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1595 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1599 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1601 pts.tv_sec * 1000000LL + pts.tv_usec );
1604 /* grow buffer if it looks like buffer is too small, but don't eat
1605 * up all the memory on strange streams */
1606 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1609 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1610 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1612 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1615 msg_Warn( p_demux, "realloc failed" );
1619 tk->p_buffer = (uint8_t*)p_tmp;
1622 if( i_size > tk->i_buffer )
1624 msg_Warn( p_demux, "buffer overflow" );
1626 /* FIXME could i_size be > buffer size ? */
1627 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1628 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1630 AMRAudioSource *amrSource = (AMRAudioSource*)tk->sub->readSource();
1632 p_block = block_New( p_demux, i_size + 1 );
1633 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1634 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1636 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1638 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1639 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->sub->rtpSource();
1640 uint32_t header = h261Source->lastSpecialHeader();
1642 uint32_t header = 0;
1643 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1645 p_block = block_New( p_demux, i_size + 4 );
1646 memcpy( p_block->p_buffer, &header, 4 );
1647 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1649 if( tk->sub->rtpSource()->curPacketMarkerBit() )
1650 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1652 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1654 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1655 msg_Warn( p_demux, "unsupported NAL type for H264" );
1657 /* Normal NAL type */
1658 p_block = block_New( p_demux, i_size + 4 );
1659 p_block->p_buffer[0] = 0x00;
1660 p_block->p_buffer[1] = 0x00;
1661 p_block->p_buffer[2] = 0x00;
1662 p_block->p_buffer[3] = 0x01;
1663 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1665 else if( tk->b_asf )
1667 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1668 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1670 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1674 p_block = block_New( p_demux, i_size );
1675 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1679 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1680 if( (tk->fmt.i_cat == VIDEO_ES) && (p_sys->i_pcr < i_pts) &&
1681 (i_pts > 0) && (p_sys->i_pcr > 0) )
1683 p_sys->i_npt += __MAX( 0, i_pts - p_sys->i_pcr );
1684 p_sys->i_pcr = i_pts;
1685 //msg_Dbg( p_demux, "npt update" );
1687 else if( /*tk->fmt.i_cat == VIDEO_ES &&*/ p_sys->i_pcr < i_pts )
1689 p_sys->i_pcr = i_pts;
1691 //msg_Dbg( p_demux, "npt %lld", p_sys->i_npt );
1693 if( (i_pts != tk->i_pts) && (!tk->b_muxed) )
1695 p_block->i_pts = i_pts;
1699 /*FIXME: for h264 you should check that packetization-mode=1 in sdp-file */
1700 p_block->i_dts = ( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ) ? 0 : i_pts;
1705 stream_DemuxSend( tk->p_out_muxed, p_block );
1707 else if( tk->b_asf )
1709 stream_DemuxSend( p_sys->p_out_asf, p_block );
1713 es_out_Send( p_demux->out, tk->p_es, p_block );
1716 /* warn that's ok */
1717 p_sys->event = 0xff;
1719 /* we have read data */
1721 p_demux->p_sys->b_no_data = VLC_FALSE;
1722 p_demux->p_sys->i_no_data_ti = 0;
1724 if( i_pts > 0 && !tk->b_muxed )
1730 /*****************************************************************************
1732 *****************************************************************************/
1733 static void StreamClose( void *p_private )
1735 live_track_t *tk = (live_track_t*)p_private;
1736 demux_t *p_demux = tk->p_demux;
1737 demux_sys_t *p_sys = p_demux->p_sys;
1739 msg_Dbg( p_demux, "StreamClose" );
1741 p_sys->event = 0xff;
1742 p_demux->b_error = VLC_TRUE;
1746 /*****************************************************************************
1748 *****************************************************************************/
1749 static void TaskInterrupt( void *p_private )
1751 demux_t *p_demux = (demux_t*)p_private;
1753 p_demux->p_sys->i_no_data_ti++;
1756 p_demux->p_sys->event = 0xff;
1759 /*****************************************************************************
1761 *****************************************************************************/
1762 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1764 p_timeout->b_die = VLC_FALSE;
1765 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1766 p_timeout->i_remain *= 1000000;
1768 vlc_thread_ready( p_timeout );
1771 while( !p_timeout->b_die )
1773 if( p_timeout->i_remain <= 0 )
1775 char *psz_bye = NULL;
1776 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1777 p_timeout->i_remain *= 1000000;
1778 msg_Dbg( p_timeout, "reset the timeout timer" );
1779 if( p_timeout->b_handle_keep_alive == VLC_TRUE )
1781 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
1782 p_timeout->p_sys->rtsp->getMediaSessionParameter( *p_timeout->p_sys->ms, NULL, psz_bye );
1784 p_timeout->p_sys->b_timeout_call = VLC_FALSE;
1788 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1791 p_timeout->i_remain -= 200000;
1792 msleep( 200000 ); /* 200 ms */
1796 /*****************************************************************************
1798 *****************************************************************************/
1799 static int b64_decode( char *dest, char *src );
1801 static int ParseASF( demux_t *p_demux )
1803 demux_sys_t *p_sys = p_demux->p_sys;
1805 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1806 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1810 /* Parse the asf header */
1811 if( psz_asf == NULL )
1812 return VLC_EGENERIC;
1814 psz_asf += strlen( psz_marker );
1815 psz_asf = strdup( psz_asf ); /* Duplicate it */
1816 psz_end = strchr( psz_asf, '\n' );
1818 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1821 if( psz_asf >= psz_end )
1824 return VLC_EGENERIC;
1827 /* Always smaller */
1828 p_header = block_New( p_demux, psz_end - psz_asf );
1829 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1830 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1831 if( p_header->i_buffer <= 0 )
1834 return VLC_EGENERIC;
1837 /* Parse it to get packet size */
1838 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1840 /* Send it to demuxer */
1841 stream_DemuxSend( p_sys->p_out_asf, p_header );
1848 static unsigned char* parseH264ConfigStr( char const* configStr,
1849 unsigned int& configSize )
1852 int i, i_records = 1;
1857 if( configStr == NULL || *configStr == '\0' )
1860 psz = dup = strdup( configStr );
1862 /* Count the number of comma's */
1863 for( psz = dup; *psz != '\0'; ++psz )
1872 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1874 for( i = 0; i < i_records; i++ )
1876 cfg[configSize++] = 0x00;
1877 cfg[configSize++] = 0x00;
1878 cfg[configSize++] = 0x00;
1879 cfg[configSize++] = 0x01;
1881 configSize += b64_decode( (char*)&cfg[configSize], psz );
1882 psz += strlen(psz)+1;
1889 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1890 static int b64_decode( char *dest, char *src )
1892 const char *dest_start = dest;
1896 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1897 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1898 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1899 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1900 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1901 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1902 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1903 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1904 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1905 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1906 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1907 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1908 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1909 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1910 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1911 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1914 for( i_level = 0; *src != '\0'; src++ )
1918 c = b64[(unsigned int)*src];
1930 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1934 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1938 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1946 return dest - dest_start;