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 );
313 msg_Err( p_demux, "failed to read SDP" );
320 if( i_read < i_sdp_max - i_sdp - 1 )
327 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
329 p_sys->p_sdp = (char*)p_sdp;
331 else if( ( p_demux->s == NULL ) &&
332 !strcasecmp( p_demux->psz_access, "sdp" ) )
334 /* sdp:// link from SAP */
335 p_sys->p_sdp = strdup( p_sys->psz_path );
337 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
339 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
343 if( p_sys->p_sdp == NULL )
345 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
346 i_error = VLC_ENOMEM;
350 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
352 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
356 if( p_sys->b_real ) goto error;
358 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
361 if( p_sys->p_out_asf && ParseASF( p_demux ) )
363 msg_Err( p_demux, "cannot find a usable asf header" );
364 /* TODO Clean tracks */
368 if( p_sys->i_track <= 0 )
374 for( i = 0; i < p_sys->i_track; i++ )
376 live_track_t *tk = p_sys->track[i];
378 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
379 free( tk->p_buffer );
383 if( p_sys->i_track ) free( p_sys->track );
384 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
385 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
386 if( p_sys->ms ) Medium::close( p_sys->ms );
387 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
388 if( p_sys->env ) p_sys->env->reclaim();
389 if( p_sys->p_timeout )
391 vlc_object_kill( p_sys->p_timeout );
392 vlc_thread_join( p_sys->p_timeout );
393 vlc_object_detach( p_sys->p_timeout );
394 vlc_object_destroy( p_sys->p_timeout );
396 if( p_sys->scheduler ) delete p_sys->scheduler;
397 if( p_sys->p_sdp ) free( p_sys->p_sdp );
398 if( p_sys->psz_path ) free( p_sys->psz_path );
400 vlc_UrlClean( &p_sys->url );
406 /*****************************************************************************
408 *****************************************************************************/
409 static void Close( vlc_object_t *p_this )
411 demux_t *p_demux = (demux_t*)p_this;
412 demux_sys_t *p_sys = p_demux->p_sys;
415 for( i = 0; i < p_sys->i_track; i++ )
417 live_track_t *tk = p_sys->track[i];
419 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
420 free( tk->p_buffer );
424 if( p_sys->i_track ) free( p_sys->track );
425 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
426 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
427 if( p_sys->ms ) Medium::close( p_sys->ms );
428 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
429 if( p_sys->env ) p_sys->env->reclaim();
430 if( p_sys->p_timeout )
432 vlc_object_kill( p_sys->p_timeout );
433 vlc_thread_join( p_sys->p_timeout );
434 vlc_object_detach( p_sys->p_timeout );
435 vlc_object_destroy( p_sys->p_timeout );
437 if( p_sys->scheduler ) delete p_sys->scheduler;
438 if( p_sys->p_sdp ) free( p_sys->p_sdp );
439 if( p_sys->psz_path ) free( p_sys->psz_path );
441 vlc_UrlClean( &p_sys->url );
446 /*****************************************************************************
447 * Connect: connects to the RTSP server to setup the session DESCRIBE
448 *****************************************************************************/
449 static int Connect( demux_t *p_demux )
451 demux_sys_t *p_sys = p_demux->p_sys;
452 Authenticator authenticator;
454 char *psz_user = NULL;
455 char *psz_pwd = NULL;
456 char *psz_url = NULL;
457 char *psz_options = NULL;
460 int i_ret = VLC_SUCCESS;
462 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
463 if( !psz_url ) return VLC_ENOMEM;
465 if( p_sys->url.psz_username || p_sys->url.psz_password )
467 sprintf( psz_url, "rtsp://%s%s", p_sys->url.psz_host,
468 p_sys->url.psz_path );
470 psz_user = strdup( p_sys->url.psz_username );
471 psz_pwd = strdup( p_sys->url.psz_password );
475 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
477 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
478 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
482 if( p_demux->b_die || p_demux->b_error )
490 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
491 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
493 if( ( p_sys->rtsp = RTSPClient::createNew( *p_sys->env,
494 p_demux->p_libvlc->i_verbose > 1,
495 "VLC media player", i_http_port ) ) == NULL )
497 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
498 p_sys->env->getResultMsg() );
505 /* Kasenna enables KeepAlive by analysing the User-Agent string.
506 * Appending _KA to the string should be enough to enable this feature,
507 * however, there is a bug where the _KA doesn't get parsed from the
508 * default User-Agent as created by VLC/Live555 code. This is probably due
509 * to spaces in the string or the string being too long. Here we override
510 * the default string with a more compact version.
512 if( var_CreateGetBool( p_demux, "rtsp-kasenna" ))
514 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
515 p_sys->rtsp->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
520 authenticator.setUsernameAndPassword( (const char*)psz_user,
521 (const char*)psz_pwd );
523 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url, psz_user, psz_pwd,
525 if( psz_options ) delete [] psz_options;
527 p_sdp = p_sys->rtsp->describeURL( psz_url, &authenticator,
528 var_GetBool( p_demux, "rtsp-kasenna" ) );
531 /* failure occurred */
533 const char *psz_error = p_sys->env->getResultMsg();
535 if( var_GetBool( p_demux, "rtsp-http" ) )
536 sscanf( psz_error, "%*s %*s HTTP GET %*s HTTP/%*u.%*u %3u %*s",
538 else sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
539 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
544 msg_Dbg( p_demux, "authentication failed" );
546 if( psz_user ) free( psz_user );
547 if( psz_pwd ) free( psz_pwd );
548 psz_user = psz_pwd = NULL;
550 i_result = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
551 _("Please enter a valid login name and a password."),
552 &psz_user, &psz_pwd );
553 if( i_result == DIALOG_OK_YES )
555 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
560 else if( (i_code != 0) && !var_GetBool( p_demux, "rtsp-http" ) )
562 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
564 val.b_bool = VLC_TRUE;
565 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
566 var_Set( p_demux, "rtsp-http", val );
567 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
573 msg_Dbg( p_demux, "connection timeout, retrying" );
574 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
578 i_ret = VLC_EGENERIC;
581 /* malloc-ated copy */
582 if( psz_url ) free( psz_url );
583 if( psz_user ) free( psz_user );
584 if( psz_pwd ) free( psz_pwd );
586 if( p_sys->p_sdp ) free( p_sys->p_sdp );
588 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
594 /*****************************************************************************
595 * SessionsSetup: prepares the subsessions and does the SETUP
596 *****************************************************************************/
597 static int SessionsSetup( demux_t *p_demux )
599 demux_sys_t *p_sys = p_demux->p_sys;
600 MediaSubsessionIterator *iter = NULL;
601 MediaSubsession *sub = NULL;
603 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
605 int i_return = VLC_SUCCESS;
606 unsigned int i_buffer = 0;
607 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
609 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" ) ||
610 var_GetBool( p_demux, "rtsp-http" );
611 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
613 /* Create the session from the SDP */
614 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
616 msg_Err( p_demux, "Could not create the RTSP Session: %s",
617 p_sys->env->getResultMsg() );
621 /* Initialise each media subsession */
622 iter = new MediaSubsessionIterator( *p_sys->ms );
623 while( ( sub = iter->next() ) != NULL )
628 /* Value taken from mplayer */
629 if( !strcmp( sub->mediumName(), "audio" ) )
631 else if( !strcmp( sub->mediumName(), "video" ) )
635 if( i_client_port != -1 )
637 sub->setClientPortNum( i_client_port );
641 if( strcasestr( sub->codecName(), "REAL" ) )
643 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
644 p_sys->b_real = VLC_TRUE; /* This is a problem, we'll handle it later */
648 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
649 bInit = sub->initiate( 4 ); /* Constant ? */
651 bInit = sub->initiate();
655 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
656 sub->mediumName(), sub->codecName(),
657 p_sys->env->getResultMsg() );
661 if( sub->rtpSource() != NULL )
663 int fd = sub->rtpSource()->RTPgs()->socketNum();
665 /* Increase the buffer size */
667 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
669 /* Increase the RTP reorder timebuffer just a bit */
670 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
672 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
675 /* Issue the SETUP */
678 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
679 b_rtsp_tcp ? True : False ) ) )
681 /* if we get an unsupported transport error, toggle TCP use and try again */
682 if( !strstr(p_sys->env->getResultMsg(), "461 Unsupported Transport")
683 || !( p_sys->rtsp->setupMediaSubsession( *sub, False,
684 b_rtsp_tcp ? False : True ) ) )
686 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
687 sub->codecName(), p_sys->env->getResultMsg() );
693 /* Check if we will receive data from this subsession for this track */
694 if( sub->readSource() == NULL ) continue;
695 if( !p_sys->b_multicast )
697 /* Check, because we need diff. rollover behaviour for multicast */
698 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
701 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
702 tk->p_demux = p_demux;
705 tk->b_quicktime = VLC_FALSE;
706 tk->b_asf = VLC_FALSE;
707 tk->b_muxed = VLC_FALSE;
708 tk->p_out_muxed = NULL;
710 tk->b_rtcp_sync = VLC_FALSE;
712 tk->i_buffer = 65536;
713 tk->p_buffer = (uint8_t *)malloc( 65536 );
715 /* Value taken from mplayer */
716 if( !strcmp( sub->mediumName(), "audio" ) )
718 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
719 tk->fmt.audio.i_channels = sub->numChannels();
720 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
722 if( !strcmp( sub->codecName(), "MPA" ) ||
723 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
724 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
726 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
727 tk->fmt.audio.i_rate = 0;
729 else if( !strcmp( sub->codecName(), "AC3" ) )
731 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
732 tk->fmt.audio.i_rate = 0;
734 else if( !strcmp( sub->codecName(), "L16" ) )
736 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
737 tk->fmt.audio.i_bitspersample = 16;
739 else if( !strcmp( sub->codecName(), "L8" ) )
741 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
742 tk->fmt.audio.i_bitspersample = 8;
744 else if( !strcmp( sub->codecName(), "PCMU" ) )
746 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
748 else if( !strcmp( sub->codecName(), "PCMA" ) )
750 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
752 else if( !strncmp( sub->codecName(), "G726", 4 ) )
754 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
755 tk->fmt.audio.i_rate = 8000;
756 tk->fmt.audio.i_channels = 1;
757 if( !strcmp( sub->codecName()+5, "40" ) )
758 tk->fmt.i_bitrate = 40000;
759 else if( !strcmp( sub->codecName()+5, "32" ) )
760 tk->fmt.i_bitrate = 32000;
761 else if( !strcmp( sub->codecName()+5, "24" ) )
762 tk->fmt.i_bitrate = 24000;
763 else if( !strcmp( sub->codecName()+5, "16" ) )
764 tk->fmt.i_bitrate = 16000;
766 else if( !strcmp( sub->codecName(), "AMR" ) )
768 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
770 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
772 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
774 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
776 unsigned int i_extra;
779 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
781 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
784 tk->fmt.i_extra = i_extra;
785 tk->fmt.p_extra = malloc( i_extra );
786 memcpy( tk->fmt.p_extra, p_extra, i_extra );
789 /* Because the "faad" decoder does not handle the LATM data length field
790 at the start of each returned LATM frame, tell the RTP source to omit it. */
791 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
793 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
795 unsigned int i_extra;
798 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
800 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
803 tk->fmt.i_extra = i_extra;
804 tk->fmt.p_extra = malloc( i_extra );
805 memcpy( tk->fmt.p_extra, p_extra, i_extra );
809 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
811 tk->b_asf = VLC_TRUE;
812 if( p_sys->p_out_asf == NULL )
813 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
816 else if( !strcmp( sub->codecName(), "X-QT" ) ||
817 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
819 tk->b_quicktime = VLC_TRUE;
821 else if( !strcmp( sub->codecName(), "SPEEX" ) )
823 tk->fmt.i_codec = VLC_FOURCC( 's', 'p', 'x', 'r' );
824 if ( sub->rtpTimestampFrequency() )
825 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
828 msg_Warn( p_demux,"Using 8kHz as default sample rate." );
829 tk->fmt.audio.i_rate = 8000;
833 else if( !strcmp( sub->mediumName(), "video" ) )
835 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
836 if( !strcmp( sub->codecName(), "MPV" ) )
838 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
840 else if( !strcmp( sub->codecName(), "H263" ) ||
841 !strcmp( sub->codecName(), "H263-1998" ) ||
842 !strcmp( sub->codecName(), "H263-2000" ) )
844 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
846 else if( !strcmp( sub->codecName(), "H261" ) )
848 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
850 else if( !strcmp( sub->codecName(), "H264" ) )
852 unsigned int i_extra = 0;
853 uint8_t *p_extra = NULL;
855 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
856 tk->fmt.b_packetized = VLC_FALSE;
858 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
861 tk->fmt.i_extra = i_extra;
862 tk->fmt.p_extra = malloc( i_extra );
863 memcpy( tk->fmt.p_extra, p_extra, i_extra );
868 else if( !strcmp( sub->codecName(), "JPEG" ) )
870 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
872 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
874 unsigned int i_extra;
877 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
879 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
882 tk->fmt.i_extra = i_extra;
883 tk->fmt.p_extra = malloc( i_extra );
884 memcpy( tk->fmt.p_extra, p_extra, i_extra );
888 else if( !strcmp( sub->codecName(), "X-QT" ) ||
889 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
890 !strcmp( sub->codecName(), "X-QDM" ) ||
891 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
892 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
894 tk->b_quicktime = VLC_TRUE;
896 else if( !strcmp( sub->codecName(), "MP2T" ) )
898 tk->b_muxed = VLC_TRUE;
899 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
901 else if( !strcmp( sub->codecName(), "MP2P" ) ||
902 !strcmp( sub->codecName(), "MP1S" ) )
904 tk->b_muxed = VLC_TRUE;
905 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
908 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
910 tk->b_asf = VLC_TRUE;
911 if( p_sys->p_out_asf == NULL )
912 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
917 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
919 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
922 if( sub->rtcpInstance() != NULL )
924 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
927 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
930 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
931 p_sys->track[p_sys->i_track++] = tk;
936 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
942 if( p_sys->i_track <= 0 ) i_return = VLC_EGENERIC;
944 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
945 /* Retrieve the starttime if possible */
946 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
948 p_sys->i_npt_start = (int64_t) -1;
950 if( p_sys->i_npt_start < 0 )
951 p_sys->i_npt_start = -1;
953 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
954 /* Retrieve the duration if possible */
955 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
957 p_sys->i_npt_length = (int64_t) -1;
959 if( p_sys->i_npt_length < 0 )
960 p_sys->i_npt_length = -1;
962 msg_Dbg( p_demux, "setup start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
966 /*****************************************************************************
967 * Play: starts the actual playback of the stream
968 *****************************************************************************/
969 static int Play( demux_t *p_demux )
971 demux_sys_t *p_sys = p_demux->p_sys;
977 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, p_sys->i_npt_start, -1, 1 ) )
979 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
983 /* Retrieve the timeout value and set up a timeout prevention thread */
984 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
985 if( p_sys->i_timeout <= 0 )
986 p_sys->i_timeout = 60; /* default value from RFC2326 */
988 /* start timeout-thread only on x-asf streams (wms), it has rtcp support but doesn't
989 * seem to use it for liveness/keep-alive, get_parameter seems to work for it. get_parameter
990 * doesn't work with dss 5.5.4 & 5.5.5, they seems to work with rtcp */
991 if( !p_sys->p_timeout && p_sys->p_out_asf )
993 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
994 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
995 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
996 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
997 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
999 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
1000 vlc_object_destroy( p_sys->p_timeout );
1002 msg_Dbg( p_demux, "spawned timeout thread" );
1003 vlc_object_attach( p_sys->p_timeout, p_demux );
1008 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1009 for( i = 0; i < p_sys->i_track; i++ )
1011 if( !p_sys->track[i]->b_rtcp_sync )
1012 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1013 p_sys->track[i]->i_start_seq = (int)p_sys->track[i]->sub->rtpInfo.seqNum;
1014 msg_Info( p_demux, "set startseq: %u", p_sys->track[i]->i_start_seq );
1018 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1019 /* Retrieve the starttime if possible */
1020 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1022 p_sys->i_npt_start = -1;
1024 if( p_sys->i_npt_start < 0 )
1026 p_sys->i_npt_start = -1;
1030 p_sys->i_npt = p_sys->i_npt_start;
1032 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1033 /* Retrieve the duration if possible */
1034 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1036 p_sys->i_npt_length = -1;
1038 if( p_sys->i_npt_length < 0 )
1039 p_sys->i_npt_length = -1;
1041 msg_Dbg( p_demux, "play start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1046 /*****************************************************************************
1048 *****************************************************************************/
1049 static int Demux( demux_t *p_demux )
1051 demux_sys_t *p_sys = p_demux->p_sys;
1054 vlc_bool_t b_send_pcr = VLC_TRUE;
1058 /* Check if we need to send the server a Keep-A-Live signal */
1059 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
1061 char *psz_bye = NULL;
1062 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
1063 p_sys->b_timeout_call = VLC_FALSE;
1067 for( i = 0; i < p_sys->i_track; i++ )
1069 live_track_t *tk = p_sys->track[i];
1071 if( tk->b_asf || tk->b_muxed )
1072 b_send_pcr = VLC_FALSE;
1078 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
1084 if( p_sys->i_pcr > 0 )
1087 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1090 /* First warn we want to read data */
1092 for( i = 0; i < p_sys->i_track; i++ )
1094 live_track_t *tk = p_sys->track[i];
1096 if( tk->waiting == 0 )
1099 tk->sub->readSource()->getNextFrame( tk->p_buffer, tk->i_buffer,
1100 StreamRead, tk, StreamClose, tk );
1103 /* Create a task that will be called if we wait more than 300ms */
1104 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1107 p_sys->scheduler->doEventLoop( &p_sys->event );
1109 /* remove the task */
1110 p_sys->scheduler->unscheduleDelayedTask( task );
1112 /* Check for gap in pts value */
1113 for( i = 0; i < p_sys->i_track; i++ )
1115 live_track_t *tk = p_sys->track[i];
1117 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1118 tk->sub->rtpSource() && tk->sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1120 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1122 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1123 tk->b_rtcp_sync = VLC_TRUE;
1131 if( p_sys->b_multicast && p_sys->b_no_data &&
1132 ( p_sys->i_no_data_ti > 120 ) )
1134 /* FIXME Make this configurable
1135 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1139 else if( !p_sys->b_multicast && p_sys->b_no_data &&
1140 ( p_sys->i_no_data_ti > 34 ) )
1142 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" ) ||
1143 var_GetBool( p_demux, "rtsp-http" );
1145 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1147 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1148 if( RollOverTcp( p_demux ) )
1150 msg_Err( p_demux, "TCP rollover failed, aborting" );
1154 else if( p_sys->i_no_data_ti > 34 )
1156 msg_Err( p_demux, "no data received in 10s, aborting" );
1160 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1163 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1166 return p_demux->b_error ? 0 : 1;
1169 /*****************************************************************************
1171 *****************************************************************************/
1172 static int Control( demux_t *p_demux, int i_query, va_list args )
1174 demux_sys_t *p_sys = p_demux->p_sys;
1177 vlc_bool_t *pb, *pb2, b_bool;
1182 case DEMUX_GET_TIME:
1183 pi64 = (int64_t*)va_arg( args, int64_t * );
1184 if( p_sys->i_npt > 0 )
1186 *pi64 = p_sys->i_npt;
1189 return VLC_EGENERIC;
1191 case DEMUX_GET_LENGTH:
1192 pi64 = (int64_t*)va_arg( args, int64_t * );
1193 if( p_sys->i_npt_length > 0 )
1195 *pi64 = p_sys->i_npt_length;
1198 return VLC_EGENERIC;
1200 case DEMUX_GET_POSITION:
1201 pf = (double*)va_arg( args, double* );
1202 if( p_sys->i_npt_length > 0 && p_sys->i_npt > 0 )
1204 *pf = (double)p_sys->i_npt / (double)p_sys->i_npt_length;
1207 return VLC_EGENERIC;
1209 case DEMUX_SET_POSITION:
1213 f = (double)va_arg( args, double );
1214 if( p_sys->rtsp && p_sys->i_npt_length > 0 )
1217 time = f * (double)p_sys->i_npt_length / (double)1000000.0; /* in second */
1218 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time, -1, 1 ) )
1220 msg_Err( p_demux, "PLAY failed %s",
1221 p_sys->env->getResultMsg() );
1222 return VLC_EGENERIC;
1224 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1226 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1227 /* Retrieve RTP-Info values */
1228 for( i = 0; i < p_sys->i_track; i++ )
1230 //if( !p_sys->track[i]->b_rtcp_sync )
1231 p_sys->track[i]->b_rtcp_sync = VLC_FALSE;
1232 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1233 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1234 msg_Info( p_demux, "set pos startseq: %u", p_sys->track[i]->i_start_seq );
1237 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1238 /* Retrieve the starttime if possible */
1239 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1241 p_sys->i_npt_start = -1;
1243 if( p_sys->i_npt_start < 0 )
1245 p_sys->i_npt_start = -1;
1249 p_sys->i_npt = p_sys->i_npt_start;
1251 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1252 /* Retrieve the duration if possible */
1253 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1255 p_sys->i_npt_length = -1;
1257 if( p_sys->i_npt_length < 0 )
1258 p_sys->i_npt_length = -1;
1260 msg_Dbg( p_demux, "seek start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1263 return VLC_EGENERIC;
1265 /* Special for access_demux */
1266 case DEMUX_CAN_PAUSE:
1267 case DEMUX_CAN_SEEK:
1268 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1269 if( p_sys->rtsp && p_sys->i_npt_length )
1270 /* Not always true, but will be handled in SET_PAUSE_STATE */
1276 case DEMUX_CAN_CONTROL_PACE:
1277 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1279 #if 1 /* Disable for now until we have a clock synchro algo
1280 * which works with something else than MPEG over UDP */
1288 case DEMUX_CAN_CONTROL_RATE:
1289 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1290 pb2 = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1292 *pb = p_sys->rtsp != NULL && p_sys->i_npt_length > 0 && !var_GetBool( p_demux, "rtsp-kasenna" );
1296 case DEMUX_SET_RATE:
1300 if( !p_sys->rtsp || p_sys->i_npt_length <= 0 || var_GetBool( p_demux, "rtsp-kasenna" ) )
1301 return VLC_EGENERIC;
1303 /* TODO we might want to ensure that the new rate is different from
1304 * old rate after playMediaSession...
1305 * I have no idea how the server map the requested rate to the
1308 * current is x2 we request x1.5 if the server return x2 we will
1309 * never succeed to return to x1.
1310 * In this case we should retry with a lower rate until we have
1314 pi_int = (int*)va_arg( args, int * );
1315 f_scale = (double)INPUT_RATE_DEFAULT / (*p_int);
1317 /* Passing -1 for the start and end time will mean liveMedia won't
1318 * create a Range: section for the RTSP message. The server should
1319 * pick up from the current position */
1320 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, -1, -1, f_scale ) )
1322 msg_Err( p_demux, "PLAY with Scale %0.2f failed %s", f_scale,
1323 p_sys->env->getResultMsg() );
1324 return VLC_EGENERIC;
1326 /* ReSync the stream */
1327 p_sys->i_npt_start = 0;
1329 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1331 *pi_int = (int)( INPUT_RATE_DEFAULT / p_sys->ms->scale() + 0.5 );
1336 case DEMUX_SET_PAUSE_STATE:
1338 double d_npt = (double) p_sys->i_npt / I64C(1000000);
1341 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1342 if( p_sys->rtsp == NULL )
1343 return VLC_EGENERIC;
1346 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1347 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1348 d_npt > 0 ? d_npt : -1 ) ) )
1350 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1351 return VLC_EGENERIC;
1354 /* When we Pause, we'll need the TimeoutPrevention thread to
1355 * handle sending the "Keep Alive" message to the server.
1356 * Unfortunately Live555 isn't thread safe and so can't
1357 * do this normally while the main Demux thread is handling
1358 * a live stream. We end up with the Timeout thread blocking
1359 * waiting for a response from the server. So when we PAUSE
1360 * we set a flag that the TimeoutPrevention function will check
1361 * and if it's set, it will trigger the GET_PARAMETER message */
1362 if( b_bool && p_sys->p_timeout != NULL )
1363 p_sys->p_timeout->b_handle_keep_alive = VLC_TRUE;
1364 else if( !b_bool && p_sys->p_timeout != NULL )
1365 p_sys->p_timeout->b_handle_keep_alive = VLC_FALSE;
1367 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1369 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1370 for( i = 0; i < p_sys->i_track; i++ )
1372 if( !p_sys->track[i]->b_rtcp_sync )
1373 p_sys->track[i]->i_pts = 0; // (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1374 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1375 msg_Info( p_demux, "set pause startseq: %u", p_sys->track[i]->i_start_seq );
1378 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1379 /* Retrieve the starttime if possible */
1380 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1382 p_sys->i_npt_start = -1;
1384 if( p_sys->i_npt_start < 0 )
1386 p_sys->i_npt_start = -1;
1390 p_sys->i_npt = p_sys->i_npt_start;
1392 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1393 /* Retrieve the duration if possible */
1394 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1396 p_sys->i_npt_length = -1;
1398 if( p_sys->i_npt_length < 0 )
1399 p_sys->i_npt_length = -1;
1401 msg_Dbg( p_demux, "pause start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1404 case DEMUX_GET_TITLE_INFO:
1405 case DEMUX_SET_TITLE:
1406 case DEMUX_SET_SEEKPOINT:
1407 return VLC_EGENERIC;
1409 case DEMUX_GET_PTS_DELAY:
1410 pi64 = (int64_t*)va_arg( args, int64_t * );
1411 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1415 return VLC_EGENERIC;
1419 /*****************************************************************************
1420 * RollOverTcp: reopen the rtsp into TCP mode
1421 * XXX: ugly, a lot of code are duplicated from Open()
1422 * This should REALLY be fixed
1423 *****************************************************************************/
1424 static int RollOverTcp( demux_t *p_demux )
1426 demux_sys_t *p_sys = p_demux->p_sys;
1429 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1431 /* We close the old RTSP session */
1432 for( i = 0; i < p_sys->i_track; i++ )
1434 live_track_t *tk = p_sys->track[i];
1436 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
1437 free( tk->p_buffer );
1440 if( p_sys->i_track ) free( p_sys->track );
1441 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
1443 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1444 Medium::close( p_sys->ms );
1445 RTSPClient::close( p_sys->rtsp );
1449 p_sys->track = NULL;
1452 /* Reopen rtsp client */
1453 if( ( p_demux->s != NULL ) &&
1454 ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1456 msg_Err( p_demux, "Failed to connect with rtsp://%s",
1461 if( p_sys->p_sdp == NULL )
1463 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1467 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1469 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1473 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1479 return VLC_EGENERIC;
1483 /*****************************************************************************
1485 *****************************************************************************/
1486 static void StreamRead( void *p_private, unsigned int i_size,
1487 unsigned int i_truncated_bytes, struct timeval pts,
1488 unsigned int duration )
1490 live_track_t *tk = (live_track_t*)p_private;
1491 demux_t *p_demux = tk->p_demux;
1492 demux_sys_t *p_sys = p_demux->p_sys;
1495 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1497 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1498 (uint64_t)pts.tv_usec;
1500 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1501 i_pts &= UI64C(0x00ffffffffffffff);
1503 if( tk->b_quicktime && tk->p_es == NULL )
1505 QuickTimeGenericRTPSource *qtRTPSource =
1506 (QuickTimeGenericRTPSource*)tk->sub->rtpSource();
1507 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1508 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1510 if( tk->fmt.i_cat == VIDEO_ES ) {
1511 if( qtState.sdAtomSize < 16 + 32 )
1514 p_sys->event = 0xff;
1518 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1519 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1520 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1522 if( tk->fmt.i_codec == VLC_FOURCC('a', 'v', 'c', '1') )
1524 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
1525 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
1526 + qtRTPSource->qtState.sdAtomSize;
1527 while (pos+8 < endpos) {
1528 unsigned int atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
1529 if( atomLength == 0 || atomLength > (unsigned int)(endpos-pos)) break;
1530 if( memcmp(pos+4, "avcC", 4) == 0 &&
1532 atomLength <= INT_MAX )
1534 tk->fmt.i_extra = atomLength-8;
1535 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1536 memcpy(tk->fmt.p_extra, pos+8, atomLength-8);
1544 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1545 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1546 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1550 if( qtState.sdAtomSize < 4 )
1553 p_sys->event = 0xff;
1557 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1559 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1563 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1565 pts.tv_sec * 1000000LL + pts.tv_usec );
1568 /* grow buffer if it looks like buffer is too small, but don't eat
1569 * up all the memory on strange streams */
1570 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1573 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1574 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1576 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1579 msg_Warn( p_demux, "realloc failed" );
1583 tk->p_buffer = (uint8_t*)p_tmp;
1586 if( i_size > tk->i_buffer )
1588 msg_Warn( p_demux, "buffer overflow" );
1590 /* FIXME could i_size be > buffer size ? */
1591 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1592 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1594 AMRAudioSource *amrSource = (AMRAudioSource*)tk->sub->readSource();
1596 p_block = block_New( p_demux, i_size + 1 );
1597 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1598 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1600 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1602 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1603 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->sub->rtpSource();
1604 uint32_t header = h261Source->lastSpecialHeader();
1606 uint32_t header = 0;
1607 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1609 p_block = block_New( p_demux, i_size + 4 );
1610 memcpy( p_block->p_buffer, &header, 4 );
1611 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1613 if( tk->sub->rtpSource()->curPacketMarkerBit() )
1614 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1616 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1618 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1619 msg_Warn( p_demux, "unsupported NAL type for H264" );
1621 /* Normal NAL type */
1622 p_block = block_New( p_demux, i_size + 4 );
1623 p_block->p_buffer[0] = 0x00;
1624 p_block->p_buffer[1] = 0x00;
1625 p_block->p_buffer[2] = 0x00;
1626 p_block->p_buffer[3] = 0x01;
1627 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1629 else if( tk->b_asf )
1631 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1632 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1634 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1638 p_block = block_New( p_demux, i_size );
1639 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1643 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1644 if( (tk->fmt.i_cat == VIDEO_ES) && (p_sys->i_pcr < i_pts) &&
1645 (i_pts > 0) && (p_sys->i_pcr > 0) )
1647 p_sys->i_npt += __MAX( 0, i_pts - p_sys->i_pcr );
1648 p_sys->i_pcr = i_pts;
1649 //msg_Dbg( p_demux, "npt update" );
1651 else if( /*tk->fmt.i_cat == VIDEO_ES &&*/ p_sys->i_pcr < i_pts )
1653 p_sys->i_pcr = i_pts;
1655 //msg_Dbg( p_demux, "npt %lld", p_sys->i_npt );
1657 if( (i_pts != tk->i_pts) && (!tk->b_muxed) )
1659 p_block->i_pts = i_pts;
1663 /*FIXME: for h264 you should check that packetization-mode=1 in sdp-file */
1664 p_block->i_dts = ( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ) ? 0 : i_pts;
1669 stream_DemuxSend( tk->p_out_muxed, p_block );
1671 else if( tk->b_asf )
1673 stream_DemuxSend( p_sys->p_out_asf, p_block );
1677 es_out_Send( p_demux->out, tk->p_es, p_block );
1680 /* warn that's ok */
1681 p_sys->event = 0xff;
1683 /* we have read data */
1685 p_demux->p_sys->b_no_data = VLC_FALSE;
1686 p_demux->p_sys->i_no_data_ti = 0;
1688 if( i_pts > 0 && !tk->b_muxed )
1694 /*****************************************************************************
1696 *****************************************************************************/
1697 static void StreamClose( void *p_private )
1699 live_track_t *tk = (live_track_t*)p_private;
1700 demux_t *p_demux = tk->p_demux;
1701 demux_sys_t *p_sys = p_demux->p_sys;
1703 msg_Dbg( p_demux, "StreamClose" );
1705 p_sys->event = 0xff;
1706 p_demux->b_error = VLC_TRUE;
1710 /*****************************************************************************
1712 *****************************************************************************/
1713 static void TaskInterrupt( void *p_private )
1715 demux_t *p_demux = (demux_t*)p_private;
1717 p_demux->p_sys->i_no_data_ti++;
1720 p_demux->p_sys->event = 0xff;
1723 /*****************************************************************************
1725 *****************************************************************************/
1726 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1728 p_timeout->b_die = VLC_FALSE;
1729 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1730 p_timeout->i_remain *= 1000000;
1732 vlc_thread_ready( p_timeout );
1735 while( !p_timeout->b_die )
1737 if( p_timeout->i_remain <= 0 )
1739 char *psz_bye = NULL;
1740 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1741 p_timeout->i_remain *= 1000000;
1742 msg_Dbg( p_timeout, "reset the timeout timer" );
1743 if( p_timeout->b_handle_keep_alive == VLC_TRUE )
1745 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
1746 p_timeout->p_sys->rtsp->getMediaSessionParameter( *p_timeout->p_sys->ms, NULL, psz_bye );
1748 p_timeout->p_sys->b_timeout_call = VLC_FALSE;
1752 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1755 p_timeout->i_remain -= 200000;
1756 msleep( 200000 ); /* 200 ms */
1760 /*****************************************************************************
1762 *****************************************************************************/
1763 static int b64_decode( char *dest, char *src );
1765 static int ParseASF( demux_t *p_demux )
1767 demux_sys_t *p_sys = p_demux->p_sys;
1769 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1770 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1774 /* Parse the asf header */
1775 if( psz_asf == NULL )
1776 return VLC_EGENERIC;
1778 psz_asf += strlen( psz_marker );
1779 psz_asf = strdup( psz_asf ); /* Duplicate it */
1780 psz_end = strchr( psz_asf, '\n' );
1782 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1785 if( psz_asf >= psz_end )
1788 return VLC_EGENERIC;
1791 /* Always smaller */
1792 p_header = block_New( p_demux, psz_end - psz_asf );
1793 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1794 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1795 if( p_header->i_buffer <= 0 )
1798 return VLC_EGENERIC;
1801 /* Parse it to get packet size */
1802 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1804 /* Send it to demuxer */
1805 stream_DemuxSend( p_sys->p_out_asf, p_header );
1812 static unsigned char* parseH264ConfigStr( char const* configStr,
1813 unsigned int& configSize )
1816 int i, i_records = 1;
1821 if( configStr == NULL || *configStr == '\0' )
1824 psz = dup = strdup( configStr );
1826 /* Count the number of comma's */
1827 for( psz = dup; *psz != '\0'; ++psz )
1836 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1838 for( i = 0; i < i_records; i++ )
1840 cfg[configSize++] = 0x00;
1841 cfg[configSize++] = 0x00;
1842 cfg[configSize++] = 0x00;
1843 cfg[configSize++] = 0x01;
1845 configSize += b64_decode( (char*)&cfg[configSize], psz );
1846 psz += strlen(psz)+1;
1849 if( dup ) free( dup );
1853 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1854 static int b64_decode( char *dest, char *src )
1856 const char *dest_start = dest;
1860 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1861 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1862 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1863 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1864 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1865 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1866 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1867 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1868 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1869 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1870 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1871 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1872 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1873 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1874 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1875 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1878 for( i_level = 0; *src != '\0'; src++ )
1882 c = b64[(unsigned int)*src];
1894 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1898 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1902 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1910 return dest - dest_start;