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 *****************************************************************************/
34 #define __STDC_CONSTANT_MACROS 1
38 #include <vlc_plugin.h>
40 #include <vlc_demux.h>
41 #include <vlc_interface.h>
42 #include <vlc_network.h>
47 #include <stdint.h> /* UINT64_C */
50 # include <winsock2.h>
53 #include "UsageEnvironment.hh"
54 #include "BasicUsageEnvironment.hh"
55 #include "GroupsockHelper.hh"
56 #include "liveMedia.hh"
59 #include "../access/mms/asf.h" /* Who said ugly ? */
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Open ( vlc_object_t * );
68 static void Close( vlc_object_t * );
70 #define CACHING_TEXT N_("Caching value (ms)")
71 #define CACHING_LONGTEXT N_( \
72 "Allows you to modify the default caching value for RTSP streams. This " \
73 "value should be set in millisecond units." )
75 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
76 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
77 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
78 "for communication. In this mode you cannot connect to normal RTSP servers." )
80 #define USER_TEXT N_("RTSP user name")
81 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
82 "be used for authenticating the connection.")
83 #define PASS_TEXT N_("RTSP password")
84 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
85 "used for the connection.")
88 set_description( N_("RTP/RTSP/SDP demuxer (using Live555)" ) );
89 set_capability( "demux", 50 );
90 set_shortname( "RTP/RTSP");
91 set_callbacks( Open, Close );
92 add_shortcut( "live" );
93 add_shortcut( "livedotcom" );
94 set_category( CAT_INPUT );
95 set_subcategory( SUBCAT_INPUT_DEMUX );
98 set_description( N_("RTSP/RTP access and demux") );
99 add_shortcut( "rtsp" );
100 add_shortcut( "sdp" );
101 set_capability( "access_demux", 0 );
102 set_callbacks( Open, Close );
103 add_bool( "rtsp-tcp", 0, NULL,
104 N_("Use RTP over RTSP (TCP)"),
105 N_("Use RTP over RTSP (TCP)"), true );
106 add_integer( "rtp-client-port", -1, NULL,
108 N_("Port to use for the RTP source of the session"), true );
109 add_bool( "rtsp-http", 0, NULL,
110 N_("Tunnel RTSP and RTP over HTTP"),
111 N_("Tunnel RTSP and RTP over HTTP"), true );
112 add_integer( "rtsp-http-port", 80, NULL,
113 N_("HTTP tunnel port"),
114 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
116 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
117 CACHING_TEXT, CACHING_LONGTEXT, true );
118 add_bool( "rtsp-kasenna", false, NULL, KASENNA_TEXT,
119 KASENNA_LONGTEXT, true );
120 add_string( "rtsp-user", NULL, NULL, USER_TEXT,
121 USER_LONGTEXT, true );
122 add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
123 PASS_LONGTEXT, true );
127 /*****************************************************************************
129 *****************************************************************************/
134 MediaSubsession *sub;
142 stream_t *p_out_muxed; /* for muxed stream */
145 unsigned int i_buffer;
150 u_int32_t i_start_seq;
154 struct timeout_thread_t
159 bool b_handle_keep_alive;
165 char *p_sdp; /* XXX mallocated */
166 char *psz_path; /* URL-encoded path */
170 TaskScheduler *scheduler;
171 UsageEnvironment *env ;
176 live_track_t **track; /* XXX mallocated */
184 int64_t i_pcr; /* The clock */
185 int64_t i_npt; /* The current time in the stream */
186 int64_t i_npt_length;
189 /* timeout thread information */
190 int i_timeout; /* session timeout value in seconds */
191 bool b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
192 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
195 bool b_multicast; /* true if one of the tracks is multicasted */
196 bool b_no_data; /* true if we never receive any data */
197 int i_no_data_ti; /* consecutive number of TaskInterrupt */
202 static int Demux ( demux_t * );
203 static int Control( demux_t *, int, va_list );
205 static int Connect ( demux_t * );
206 static int SessionsSetup( demux_t * );
207 static int Play ( demux_t *);
208 static int ParseASF ( demux_t * );
209 static int RollOverTcp ( demux_t * );
211 static void StreamRead ( void *, unsigned int, unsigned int,
212 struct timeval, unsigned int );
213 static void StreamClose ( void * );
214 static void TaskInterrupt( void * );
216 static void TimeoutPrevention( timeout_thread_t * );
218 static unsigned char* parseH264ConfigStr( char const* configStr,
219 unsigned int& configSize );
221 /*****************************************************************************
223 *****************************************************************************/
224 static int Open ( vlc_object_t *p_this )
226 demux_t *p_demux = (demux_t*)p_this;
227 demux_sys_t *p_sys = NULL;
229 MediaSubsessionIterator *iter = NULL;
230 MediaSubsession *sub = NULL;
232 int i_error = VLC_EGENERIC;
236 /* See if it looks like a SDP
237 v, o, s fields are mandatory and in this order */
238 const uint8_t *p_peek;
239 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
241 if( memcmp( p_peek, "v=0\r\n", 5 ) &&
242 memcmp( p_peek, "v=0\n", 4 ) &&
243 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
250 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
253 p_demux->pf_demux = Demux;
254 p_demux->pf_control= Control;
255 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
256 if( !p_sys ) return VLC_ENOMEM;
259 p_sys->scheduler = NULL;
267 p_sys->i_npt_start = 0;
268 p_sys->i_npt_length = 0;
269 p_sys->p_out_asf = NULL;
270 p_sys->b_no_data = true;
271 p_sys->i_no_data_ti = 0;
272 p_sys->p_timeout = NULL;
273 p_sys->i_timeout = 0;
274 p_sys->b_timeout_call = false;
275 p_sys->b_multicast = false;
276 p_sys->b_real = false;
277 p_sys->psz_path = strdup( p_demux->psz_path );
279 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
280 vlc_UrlParse( &p_sys->url, p_sys->psz_path, 0 );
282 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
284 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
287 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
289 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
293 if( strcasecmp( p_demux->psz_access, "sdp" ) )
295 char *p = p_sys->psz_path;
296 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
299 if( p_demux->s != NULL )
301 /* Gather the complete sdp file */
303 int i_sdp_max = 1000;
304 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
308 i_error = VLC_ENOMEM;
314 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
315 i_sdp_max - i_sdp - 1 );
317 if( p_demux->b_die || p_demux->b_error )
325 msg_Err( p_demux, "failed to read SDP" );
332 if( i_read < i_sdp_max - i_sdp - 1 )
339 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
341 p_sys->p_sdp = (char*)p_sdp;
343 else if( ( p_demux->s == NULL ) &&
344 !strcasecmp( p_demux->psz_access, "sdp" ) )
346 /* sdp:// link from SAP */
347 p_sys->p_sdp = strdup( p_sys->psz_path );
349 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
351 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
355 if( p_sys->p_sdp == NULL )
357 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
358 i_error = VLC_ENOMEM;
362 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
364 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
368 if( p_sys->b_real ) goto error;
370 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
373 if( p_sys->p_out_asf && ParseASF( p_demux ) )
375 msg_Err( p_demux, "cannot find a usable asf header" );
376 /* TODO Clean tracks */
380 if( p_sys->i_track <= 0 )
386 for( i = 0; i < p_sys->i_track; i++ )
388 live_track_t *tk = p_sys->track[i];
390 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
391 es_format_Clean( &tk->fmt );
392 free( tk->p_buffer );
396 if( p_sys->i_track ) free( p_sys->track );
397 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
398 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
399 if( p_sys->ms ) Medium::close( p_sys->ms );
400 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
401 if( p_sys->env ) p_sys->env->reclaim();
402 if( p_sys->p_timeout )
404 vlc_object_kill( p_sys->p_timeout );
405 vlc_thread_join( p_sys->p_timeout );
406 vlc_object_detach( p_sys->p_timeout );
407 vlc_object_release( p_sys->p_timeout );
409 delete p_sys->scheduler;
410 free( p_sys->p_sdp );
411 free( p_sys->psz_path );
413 vlc_UrlClean( &p_sys->url );
419 /*****************************************************************************
421 *****************************************************************************/
422 static void Close( vlc_object_t *p_this )
424 demux_t *p_demux = (demux_t*)p_this;
425 demux_sys_t *p_sys = p_demux->p_sys;
428 for( i = 0; i < p_sys->i_track; i++ )
430 live_track_t *tk = p_sys->track[i];
432 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
433 es_format_Clean( &tk->fmt );
434 free( tk->p_buffer );
438 if( p_sys->i_track ) free( p_sys->track );
439 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
440 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
441 if( p_sys->ms ) Medium::close( p_sys->ms );
442 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
443 if( p_sys->env ) p_sys->env->reclaim();
444 if( p_sys->p_timeout )
446 vlc_object_kill( p_sys->p_timeout );
447 vlc_thread_join( p_sys->p_timeout );
448 vlc_object_detach( p_sys->p_timeout );
449 vlc_object_release( p_sys->p_timeout );
451 delete p_sys->scheduler;
452 free( p_sys->p_sdp );
453 free( p_sys->psz_path );
455 vlc_UrlClean( &p_sys->url );
460 /*****************************************************************************
461 * Connect: connects to the RTSP server to setup the session DESCRIBE
462 *****************************************************************************/
463 static int Connect( demux_t *p_demux )
465 demux_sys_t *p_sys = p_demux->p_sys;
466 Authenticator authenticator;
467 bool b_firstpass = true;
468 char *psz_user = NULL;
469 char *psz_pwd = NULL;
470 char *psz_url = NULL;
471 char *psz_options = NULL;
474 int i_ret = VLC_SUCCESS;
477 if( p_sys->url.i_port == 0 ) p_sys->url.i_port = 554;
478 if( p_sys->url.psz_username || p_sys->url.psz_password )
481 err = asprintf( &psz_url, "rtsp://%s:%d%s", p_sys->url.psz_host,
482 p_sys->url.i_port, p_sys->url.psz_path );
483 if( err == -1 ) return VLC_ENOMEM;
485 psz_user = strdup( p_sys->url.psz_username );
486 psz_pwd = strdup( p_sys->url.psz_password );
491 err = asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
492 if( err == -1 ) return VLC_ENOMEM;
494 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
495 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
501 if( p_demux->b_die || p_demux->b_error )
509 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
510 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
512 if( ( p_sys->rtsp = RTSPClient::createNew( *p_sys->env,
513 var_CreateGetInteger( p_demux, "verbose" ) > 1,
514 "VLC media player", i_http_port ) ) == NULL )
516 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
517 p_sys->env->getResultMsg() );
524 /* Kasenna enables KeepAlive by analysing the User-Agent string.
525 * Appending _KA to the string should be enough to enable this feature,
526 * however, there is a bug where the _KA doesn't get parsed from the
527 * default User-Agent as created by VLC/Live555 code. This is probably due
528 * to spaces in the string or the string being too long. Here we override
529 * the default string with a more compact version.
531 if( var_CreateGetBool( p_demux, "rtsp-kasenna" ))
533 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
534 p_sys->rtsp->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
539 authenticator.setUsernameAndPassword( (const char*)psz_user,
540 (const char*)psz_pwd );
542 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url, psz_user, psz_pwd,
544 delete [] psz_options;
546 p_sdp = p_sys->rtsp->describeURL( psz_url, &authenticator,
547 var_GetBool( p_demux, "rtsp-kasenna" ) );
550 /* failure occurred */
552 const char *psz_error = p_sys->env->getResultMsg();
554 if( var_GetBool( p_demux, "rtsp-http" ) )
555 sscanf( psz_error, "%*s %*s HTTP GET %*s HTTP/%*u.%*u %3u %*s",
559 const char *psz_tmp = strstr( psz_error, "RTSP" );
561 sscanf( psz_tmp, "RTSP/%*s%3u", &i_code );
565 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
568 { /* describeURL always returns an "RTSP/1.0 401 Unauthorized" the
569 * first time. This is a workaround to avoid asking for a
570 * user/passwd the first time the code passess here. */
578 msg_Dbg( p_demux, "authentication failed" );
582 psz_user = psz_pwd = NULL;
584 i_result = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
585 _("Please enter a valid login name and a password."),
586 &psz_user, &psz_pwd );
587 if( i_result == DIALOG_OK_YES )
589 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
594 else if( (i_code != 0) && !var_GetBool( p_demux, "rtsp-http" ) )
596 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
599 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
600 var_Set( p_demux, "rtsp-http", val );
601 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
607 msg_Dbg( p_demux, "connection timeout, retrying" );
608 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
610 if( i_lefttries > 0 )
613 i_ret = VLC_EGENERIC;
616 /* malloc-ated copy */
621 free( p_sys->p_sdp );
623 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
629 /*****************************************************************************
630 * SessionsSetup: prepares the subsessions and does the SETUP
631 *****************************************************************************/
632 static int SessionsSetup( demux_t *p_demux )
634 demux_sys_t *p_sys = p_demux->p_sys;
635 MediaSubsessionIterator *iter = NULL;
636 MediaSubsession *sub = NULL;
638 bool b_rtsp_tcp = false;
640 int i_return = VLC_SUCCESS;
641 unsigned int i_buffer = 0;
642 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
644 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" ) ||
645 var_GetBool( p_demux, "rtsp-http" );
646 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
648 /* Create the session from the SDP */
649 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
651 msg_Err( p_demux, "Could not create the RTSP Session: %s",
652 p_sys->env->getResultMsg() );
656 /* Initialise each media subsession */
657 iter = new MediaSubsessionIterator( *p_sys->ms );
658 while( ( sub = iter->next() ) != NULL )
663 if( p_demux->b_die || p_demux->b_error )
669 /* Value taken from mplayer */
670 if( !strcmp( sub->mediumName(), "audio" ) )
672 else if( !strcmp( sub->mediumName(), "video" ) )
676 if( i_client_port != -1 )
678 sub->setClientPortNum( i_client_port );
682 if( strcasestr( sub->codecName(), "REAL" ) )
684 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
685 p_sys->b_real = true; /* This is a problem, we'll handle it later */
689 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
690 bInit = sub->initiate( 4 ); /* Constant ? */
692 bInit = sub->initiate();
696 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
697 sub->mediumName(), sub->codecName(),
698 p_sys->env->getResultMsg() );
702 if( sub->rtpSource() != NULL )
704 int fd = sub->rtpSource()->RTPgs()->socketNum();
706 /* Increase the buffer size */
708 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
710 /* Increase the RTP reorder timebuffer just a bit */
711 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
713 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
716 /* Issue the SETUP */
719 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
720 b_rtsp_tcp ? True : False ) ) )
722 /* if we get an unsupported transport error, toggle TCP use and try again */
723 if( !strstr(p_sys->env->getResultMsg(), "461 Unsupported Transport")
724 || !( p_sys->rtsp->setupMediaSubsession( *sub, False,
725 b_rtsp_tcp ? False : True ) ) )
727 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
728 sub->codecName(), p_sys->env->getResultMsg() );
734 /* Check if we will receive data from this subsession for this track */
735 if( sub->readSource() == NULL ) continue;
736 if( !p_sys->b_multicast )
738 /* Check, because we need diff. rollover behaviour for multicast */
739 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
742 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
748 tk->p_demux = p_demux;
751 tk->b_quicktime = false;
754 tk->p_out_muxed = NULL;
756 tk->b_rtcp_sync = false;
758 tk->i_buffer = 65536;
759 tk->p_buffer = (uint8_t *)malloc( 65536 );
766 /* Value taken from mplayer */
767 if( !strcmp( sub->mediumName(), "audio" ) )
769 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
770 tk->fmt.audio.i_channels = sub->numChannels();
771 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
773 if( !strcmp( sub->codecName(), "MPA" ) ||
774 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
775 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
777 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
778 tk->fmt.audio.i_rate = 0;
780 else if( !strcmp( sub->codecName(), "AC3" ) )
782 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
783 tk->fmt.audio.i_rate = 0;
785 else if( !strcmp( sub->codecName(), "L16" ) )
787 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
788 tk->fmt.audio.i_bitspersample = 16;
790 else if( !strcmp( sub->codecName(), "L8" ) )
792 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
793 tk->fmt.audio.i_bitspersample = 8;
795 else if( !strcmp( sub->codecName(), "PCMU" ) )
797 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
799 else if( !strcmp( sub->codecName(), "PCMA" ) )
801 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
803 else if( !strncmp( sub->codecName(), "G726", 4 ) )
805 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
806 tk->fmt.audio.i_rate = 8000;
807 tk->fmt.audio.i_channels = 1;
808 if( !strcmp( sub->codecName()+5, "40" ) )
809 tk->fmt.i_bitrate = 40000;
810 else if( !strcmp( sub->codecName()+5, "32" ) )
811 tk->fmt.i_bitrate = 32000;
812 else if( !strcmp( sub->codecName()+5, "24" ) )
813 tk->fmt.i_bitrate = 24000;
814 else if( !strcmp( sub->codecName()+5, "16" ) )
815 tk->fmt.i_bitrate = 16000;
817 else if( !strcmp( sub->codecName(), "AMR" ) )
819 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
821 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
823 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
825 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
827 unsigned int i_extra;
830 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
832 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
835 tk->fmt.i_extra = i_extra;
836 tk->fmt.p_extra = malloc( i_extra );
837 memcpy( tk->fmt.p_extra, p_extra, i_extra );
840 /* Because the "faad" decoder does not handle the LATM data length field
841 at the start of each returned LATM frame, tell the RTP source to omit it. */
842 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
844 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
846 unsigned int i_extra;
849 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
851 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
854 tk->fmt.i_extra = i_extra;
855 tk->fmt.p_extra = malloc( i_extra );
856 memcpy( tk->fmt.p_extra, p_extra, i_extra );
860 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
863 if( p_sys->p_out_asf == NULL )
864 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
867 else if( !strcmp( sub->codecName(), "X-QT" ) ||
868 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
870 tk->b_quicktime = true;
872 else if( !strcmp( sub->codecName(), "SPEEX" ) )
874 tk->fmt.i_codec = VLC_FOURCC( 's', 'p', 'x', 'r' );
875 if ( sub->rtpTimestampFrequency() )
876 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
879 msg_Warn( p_demux,"Using 8kHz as default sample rate." );
880 tk->fmt.audio.i_rate = 8000;
884 else if( !strcmp( sub->mediumName(), "video" ) )
886 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
887 if( !strcmp( sub->codecName(), "MPV" ) )
889 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
891 else if( !strcmp( sub->codecName(), "H263" ) ||
892 !strcmp( sub->codecName(), "H263-1998" ) ||
893 !strcmp( sub->codecName(), "H263-2000" ) )
895 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
897 else if( !strcmp( sub->codecName(), "H261" ) )
899 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
901 else if( !strcmp( sub->codecName(), "H264" ) )
903 unsigned int i_extra = 0;
904 uint8_t *p_extra = NULL;
906 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
907 tk->fmt.b_packetized = false;
909 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
912 tk->fmt.i_extra = i_extra;
913 tk->fmt.p_extra = malloc( i_extra );
914 memcpy( tk->fmt.p_extra, p_extra, i_extra );
919 else if( !strcmp( sub->codecName(), "JPEG" ) )
921 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
923 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
925 unsigned int i_extra;
928 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
930 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
933 tk->fmt.i_extra = i_extra;
934 tk->fmt.p_extra = malloc( i_extra );
935 memcpy( tk->fmt.p_extra, p_extra, i_extra );
939 else if( !strcmp( sub->codecName(), "X-QT" ) ||
940 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
941 !strcmp( sub->codecName(), "X-QDM" ) ||
942 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
943 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
945 tk->b_quicktime = true;
947 else if( !strcmp( sub->codecName(), "MP2T" ) )
950 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
952 else if( !strcmp( sub->codecName(), "MP2P" ) ||
953 !strcmp( sub->codecName(), "MP1S" ) )
956 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
959 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
962 if( p_sys->p_out_asf == NULL )
963 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
968 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
970 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
973 if( sub->rtcpInstance() != NULL )
975 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
978 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
981 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
982 p_sys->track[p_sys->i_track++] = tk;
987 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
988 es_format_Clean( &tk->fmt );
994 if( p_sys->i_track <= 0 ) i_return = VLC_EGENERIC;
996 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
997 /* Retrieve the starttime if possible */
998 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1000 p_sys->i_npt_start = (int64_t) -1;
1002 if( p_sys->i_npt_start < 0 )
1003 p_sys->i_npt_start = -1;
1005 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1006 /* Retrieve the duration if possible */
1007 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1009 p_sys->i_npt_length = (int64_t) -1;
1011 if( p_sys->i_npt_length < 0 )
1012 p_sys->i_npt_length = -1;
1014 msg_Dbg( p_demux, "setup start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1018 /*****************************************************************************
1019 * Play: starts the actual playback of the stream
1020 *****************************************************************************/
1021 static int Play( demux_t *p_demux )
1023 demux_sys_t *p_sys = p_demux->p_sys;
1029 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, p_sys->i_npt_start, -1, 1 ) )
1031 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
1032 return VLC_EGENERIC;
1035 /* Retrieve the timeout value and set up a timeout prevention thread */
1036 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
1037 if( p_sys->i_timeout <= 0 )
1038 p_sys->i_timeout = 60; /* default value from RFC2326 */
1040 /* start timeout-thread only on x-asf streams (wms), it has rtcp support but doesn't
1041 * seem to use it for liveness/keep-alive, get_parameter seems to work for it. get_parameter
1042 * doesn't work with dss 5.5.4 & 5.5.5, they seems to work with rtcp */
1043 if( !p_sys->p_timeout && p_sys->p_out_asf )
1045 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
1046 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
1047 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
1048 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
1049 VLC_THREAD_PRIORITY_LOW, true ) )
1051 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
1052 vlc_object_release( p_sys->p_timeout );
1054 msg_Dbg( p_demux, "spawned timeout thread" );
1055 vlc_object_attach( p_sys->p_timeout, p_demux );
1060 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1061 for( i = 0; i < p_sys->i_track; i++ )
1063 if( !p_sys->track[i]->b_rtcp_sync )
1064 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1065 p_sys->track[i]->i_start_seq = (int)p_sys->track[i]->sub->rtpInfo.seqNum;
1066 msg_Info( p_demux, "set startseq: %u", p_sys->track[i]->i_start_seq );
1070 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1071 /* Retrieve the starttime if possible */
1072 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1074 p_sys->i_npt_start = -1;
1076 if( p_sys->i_npt_start < 0 )
1078 p_sys->i_npt_start = -1;
1082 p_sys->i_npt = p_sys->i_npt_start;
1084 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1085 /* Retrieve the duration if possible */
1086 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1088 p_sys->i_npt_length = -1;
1090 if( p_sys->i_npt_length < 0 )
1091 p_sys->i_npt_length = -1;
1093 msg_Dbg( p_demux, "play start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1098 /*****************************************************************************
1100 *****************************************************************************/
1101 static int Demux( demux_t *p_demux )
1103 demux_sys_t *p_sys = p_demux->p_sys;
1106 bool b_send_pcr = true;
1110 /* Check if we need to send the server a Keep-A-Live signal */
1111 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
1113 char *psz_bye = NULL;
1114 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
1115 p_sys->b_timeout_call = false;
1119 for( i = 0; i < p_sys->i_track; i++ )
1121 live_track_t *tk = p_sys->track[i];
1123 if( tk->b_asf || tk->b_muxed )
1130 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
1136 if( p_sys->i_pcr > 0 )
1139 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1142 /* First warn we want to read data */
1144 for( i = 0; i < p_sys->i_track; i++ )
1146 live_track_t *tk = p_sys->track[i];
1148 if( tk->waiting == 0 )
1151 tk->sub->readSource()->getNextFrame( tk->p_buffer, tk->i_buffer,
1152 StreamRead, tk, StreamClose, tk );
1155 /* Create a task that will be called if we wait more than 300ms */
1156 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1159 p_sys->scheduler->doEventLoop( &p_sys->event );
1161 /* remove the task */
1162 p_sys->scheduler->unscheduleDelayedTask( task );
1164 /* Check for gap in pts value */
1165 for( i = 0; i < p_sys->i_track; i++ )
1167 live_track_t *tk = p_sys->track[i];
1169 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1170 tk->sub->rtpSource() && tk->sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1172 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1174 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1175 tk->b_rtcp_sync = true;
1183 if( p_sys->b_multicast && p_sys->b_no_data &&
1184 ( p_sys->i_no_data_ti > 120 ) )
1186 /* FIXME Make this configurable
1187 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1191 else if( !p_sys->b_multicast && p_sys->b_no_data &&
1192 ( p_sys->i_no_data_ti > 34 ) )
1194 bool b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" ) ||
1195 var_GetBool( p_demux, "rtsp-http" );
1197 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1199 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1200 if( RollOverTcp( p_demux ) )
1202 msg_Err( p_demux, "TCP rollover failed, aborting" );
1206 else if( p_sys->i_no_data_ti > 34 )
1208 msg_Err( p_demux, "no data received in 10s, aborting" );
1212 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1215 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1218 return p_demux->b_error ? 0 : 1;
1221 /*****************************************************************************
1223 *****************************************************************************/
1224 static int Control( demux_t *p_demux, int i_query, va_list args )
1226 demux_sys_t *p_sys = p_demux->p_sys;
1229 bool *pb, *pb2, b_bool;
1234 case DEMUX_GET_TIME:
1235 pi64 = (int64_t*)va_arg( args, int64_t * );
1236 if( p_sys->i_npt > 0 )
1238 *pi64 = p_sys->i_npt;
1241 return VLC_EGENERIC;
1243 case DEMUX_GET_LENGTH:
1244 pi64 = (int64_t*)va_arg( args, int64_t * );
1245 if( p_sys->i_npt_length > 0 )
1247 *pi64 = p_sys->i_npt_length;
1250 return VLC_EGENERIC;
1252 case DEMUX_GET_POSITION:
1253 pf = (double*)va_arg( args, double* );
1254 if( p_sys->i_npt_length > 0 && p_sys->i_npt > 0 )
1256 *pf = (double)p_sys->i_npt / (double)p_sys->i_npt_length;
1259 return VLC_EGENERIC;
1261 case DEMUX_SET_POSITION:
1265 f = (double)va_arg( args, double );
1266 if( p_sys->rtsp && p_sys->i_npt_length > 0 )
1269 time = f * (double)p_sys->i_npt_length / (double)1000000.0; /* in second */
1270 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time, -1, 1 ) )
1272 msg_Err( p_demux, "PLAY failed %s",
1273 p_sys->env->getResultMsg() );
1274 return VLC_EGENERIC;
1276 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1278 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1279 /* Retrieve RTP-Info values */
1280 for( i = 0; i < p_sys->i_track; i++ )
1282 //if( !p_sys->track[i]->b_rtcp_sync )
1283 p_sys->track[i]->b_rtcp_sync = false;
1284 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1285 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1286 msg_Info( p_demux, "set pos startseq: %u", p_sys->track[i]->i_start_seq );
1289 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1290 /* Retrieve the starttime if possible */
1291 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1293 p_sys->i_npt_start = -1;
1295 if( p_sys->i_npt_start < 0 )
1297 p_sys->i_npt_start = -1;
1301 p_sys->i_npt = p_sys->i_npt_start;
1303 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1304 /* Retrieve the duration if possible */
1305 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1307 p_sys->i_npt_length = -1;
1309 if( p_sys->i_npt_length < 0 )
1310 p_sys->i_npt_length = -1;
1312 msg_Dbg( p_demux, "seek start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1315 return VLC_EGENERIC;
1317 /* Special for access_demux */
1318 case DEMUX_CAN_PAUSE:
1319 case DEMUX_CAN_SEEK:
1320 pb = (bool*)va_arg( args, bool * );
1321 if( p_sys->rtsp && p_sys->i_npt_length )
1322 /* Not always true, but will be handled in SET_PAUSE_STATE */
1328 case DEMUX_CAN_CONTROL_PACE:
1329 pb = (bool*)va_arg( args, bool * );
1331 #if 1 /* Disable for now until we have a clock synchro algo
1332 * which works with something else than MPEG over UDP */
1340 case DEMUX_CAN_CONTROL_RATE:
1341 pb = (bool*)va_arg( args, bool * );
1342 pb2 = (bool*)va_arg( args, bool * );
1344 *pb = p_sys->rtsp != NULL && p_sys->i_npt_length > 0 && !var_GetBool( p_demux, "rtsp-kasenna" );
1348 case DEMUX_SET_RATE:
1352 if( !p_sys->rtsp || p_sys->i_npt_length <= 0 || var_GetBool( p_demux, "rtsp-kasenna" ) )
1353 return VLC_EGENERIC;
1355 /* TODO we might want to ensure that the new rate is different from
1356 * old rate after playMediaSession...
1357 * I have no idea how the server map the requested rate to the
1360 * current is x2 we request x1.5 if the server return x2 we will
1361 * never succeed to return to x1.
1362 * In this case we should retry with a lower rate until we have
1366 pi_int = (int*)va_arg( args, int * );
1367 f_scale = (double)INPUT_RATE_DEFAULT / (*p_int);
1369 /* Passing -1 for the start and end time will mean liveMedia won't
1370 * create a Range: section for the RTSP message. The server should
1371 * pick up from the current position */
1372 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, -1, -1, f_scale ) )
1374 msg_Err( p_demux, "PLAY with Scale %0.2f failed %s", f_scale,
1375 p_sys->env->getResultMsg() );
1376 return VLC_EGENERIC;
1378 /* ReSync the stream */
1379 p_sys->i_npt_start = 0;
1381 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1383 *pi_int = (int)( INPUT_RATE_DEFAULT / p_sys->ms->scale() + 0.5 );
1388 case DEMUX_SET_PAUSE_STATE:
1390 double d_npt = (double) p_sys->i_npt / INT64_C(1000000);
1393 b_bool = (bool)va_arg( args, int );
1394 if( p_sys->rtsp == NULL )
1395 return VLC_EGENERIC;
1398 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1399 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1400 d_npt > 0 ? d_npt : -1 ) ) )
1402 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1403 return VLC_EGENERIC;
1406 /* When we Pause, we'll need the TimeoutPrevention thread to
1407 * handle sending the "Keep Alive" message to the server.
1408 * Unfortunately Live555 isn't thread safe and so can't
1409 * do this normally while the main Demux thread is handling
1410 * a live stream. We end up with the Timeout thread blocking
1411 * waiting for a response from the server. So when we PAUSE
1412 * we set a flag that the TimeoutPrevention function will check
1413 * and if it's set, it will trigger the GET_PARAMETER message */
1414 if( b_bool && p_sys->p_timeout != NULL )
1415 p_sys->p_timeout->b_handle_keep_alive = true;
1416 else if( !b_bool && p_sys->p_timeout != NULL )
1417 p_sys->p_timeout->b_handle_keep_alive = false;
1419 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1421 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1422 for( i = 0; i < p_sys->i_track; i++ )
1424 if( !p_sys->track[i]->b_rtcp_sync )
1425 p_sys->track[i]->i_pts = 0; // (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1426 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1427 msg_Info( p_demux, "set pause startseq: %u", p_sys->track[i]->i_start_seq );
1430 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1431 /* Retrieve the starttime if possible */
1432 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1434 p_sys->i_npt_start = -1;
1436 if( p_sys->i_npt_start < 0 )
1438 p_sys->i_npt_start = -1;
1442 p_sys->i_npt = p_sys->i_npt_start;
1444 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1445 /* Retrieve the duration if possible */
1446 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1448 p_sys->i_npt_length = -1;
1450 if( p_sys->i_npt_length < 0 )
1451 p_sys->i_npt_length = -1;
1453 msg_Dbg( p_demux, "pause start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1456 case DEMUX_GET_TITLE_INFO:
1457 case DEMUX_SET_TITLE:
1458 case DEMUX_SET_SEEKPOINT:
1459 return VLC_EGENERIC;
1461 case DEMUX_GET_PTS_DELAY:
1462 pi64 = (int64_t*)va_arg( args, int64_t * );
1463 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1467 return VLC_EGENERIC;
1471 /*****************************************************************************
1472 * RollOverTcp: reopen the rtsp into TCP mode
1473 * XXX: ugly, a lot of code are duplicated from Open()
1474 * This should REALLY be fixed
1475 *****************************************************************************/
1476 static int RollOverTcp( demux_t *p_demux )
1478 demux_sys_t *p_sys = p_demux->p_sys;
1481 var_SetBool( p_demux, "rtsp-tcp", true );
1483 /* We close the old RTSP session */
1484 for( i = 0; i < p_sys->i_track; i++ )
1486 live_track_t *tk = p_sys->track[i];
1488 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
1489 es_format_Clean( &tk->fmt );
1490 free( tk->p_buffer );
1493 if( p_sys->i_track ) free( p_sys->track );
1494 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
1496 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1497 Medium::close( p_sys->ms );
1498 RTSPClient::close( p_sys->rtsp );
1502 p_sys->track = NULL;
1505 /* Reopen rtsp client */
1506 if( ( p_demux->s != NULL ) &&
1507 ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1509 msg_Err( p_demux, "Failed to connect with rtsp://%s",
1514 if( p_sys->p_sdp == NULL )
1516 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1520 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1522 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1526 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1532 return VLC_EGENERIC;
1536 /*****************************************************************************
1538 *****************************************************************************/
1539 static void StreamRead( void *p_private, unsigned int i_size,
1540 unsigned int i_truncated_bytes, struct timeval pts,
1541 unsigned int duration )
1543 live_track_t *tk = (live_track_t*)p_private;
1544 demux_t *p_demux = tk->p_demux;
1545 demux_sys_t *p_sys = p_demux->p_sys;
1548 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1550 int64_t i_pts = (uint64_t)pts.tv_sec * UINT64_C(1000000) +
1551 (uint64_t)pts.tv_usec;
1553 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1554 i_pts &= UINT64_C(0x00ffffffffffffff);
1556 if( tk->b_quicktime && tk->p_es == NULL )
1558 QuickTimeGenericRTPSource *qtRTPSource =
1559 (QuickTimeGenericRTPSource*)tk->sub->rtpSource();
1560 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1561 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1563 if( tk->fmt.i_cat == VIDEO_ES ) {
1564 if( qtState.sdAtomSize < 16 + 32 )
1567 p_sys->event = 0xff;
1571 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1572 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1573 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1575 if( tk->fmt.i_codec == VLC_FOURCC('a', 'v', 'c', '1') )
1577 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
1578 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
1579 + qtRTPSource->qtState.sdAtomSize;
1580 while (pos+8 < endpos) {
1581 unsigned int atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
1582 if( atomLength == 0 || atomLength > (unsigned int)(endpos-pos)) break;
1583 if( memcmp(pos+4, "avcC", 4) == 0 &&
1585 atomLength <= INT_MAX )
1587 tk->fmt.i_extra = atomLength-8;
1588 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1589 memcpy(tk->fmt.p_extra, pos+8, atomLength-8);
1597 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1598 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1599 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1603 if( qtState.sdAtomSize < 4 )
1606 p_sys->event = 0xff;
1610 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1612 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1616 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1618 pts.tv_sec * 1000000LL + pts.tv_usec );
1621 /* grow buffer if it looks like buffer is too small, but don't eat
1622 * up all the memory on strange streams */
1623 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1626 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1627 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1629 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1632 msg_Warn( p_demux, "realloc failed" );
1636 tk->p_buffer = (uint8_t*)p_tmp;
1639 if( i_size > tk->i_buffer )
1641 msg_Warn( p_demux, "buffer overflow" );
1643 /* FIXME could i_size be > buffer size ? */
1644 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1645 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1647 AMRAudioSource *amrSource = (AMRAudioSource*)tk->sub->readSource();
1649 p_block = block_New( p_demux, i_size + 1 );
1650 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1651 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1653 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1655 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1656 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->sub->rtpSource();
1657 uint32_t header = h261Source->lastSpecialHeader();
1659 uint32_t header = 0;
1660 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1662 p_block = block_New( p_demux, i_size + 4 );
1663 memcpy( p_block->p_buffer, &header, 4 );
1664 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1666 if( tk->sub->rtpSource()->curPacketMarkerBit() )
1667 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1669 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1671 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1672 msg_Warn( p_demux, "unsupported NAL type for H264" );
1674 /* Normal NAL type */
1675 p_block = block_New( p_demux, i_size + 4 );
1676 p_block->p_buffer[0] = 0x00;
1677 p_block->p_buffer[1] = 0x00;
1678 p_block->p_buffer[2] = 0x00;
1679 p_block->p_buffer[3] = 0x01;
1680 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1682 else if( tk->b_asf )
1684 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1685 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1687 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1691 p_block = block_New( p_demux, i_size );
1692 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1696 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1697 if( (tk->fmt.i_cat == VIDEO_ES) && (p_sys->i_pcr < i_pts) &&
1698 (i_pts > 0) && (p_sys->i_pcr > 0) )
1700 p_sys->i_npt += __MAX( 0, i_pts - p_sys->i_pcr );
1701 p_sys->i_pcr = i_pts;
1702 //msg_Dbg( p_demux, "npt update" );
1704 else if( /*tk->fmt.i_cat == VIDEO_ES &&*/ p_sys->i_pcr < i_pts )
1706 p_sys->i_pcr = i_pts;
1708 //msg_Dbg( p_demux, "npt %lld", p_sys->i_npt );
1710 if( (i_pts != tk->i_pts) && (!tk->b_muxed) )
1712 p_block->i_pts = i_pts;
1716 /*FIXME: for h264 you should check that packetization-mode=1 in sdp-file */
1717 p_block->i_dts = ( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ) ? 0 : i_pts;
1722 stream_DemuxSend( tk->p_out_muxed, p_block );
1724 else if( tk->b_asf )
1726 stream_DemuxSend( p_sys->p_out_asf, p_block );
1730 es_out_Send( p_demux->out, tk->p_es, p_block );
1733 /* warn that's ok */
1734 p_sys->event = 0xff;
1736 /* we have read data */
1738 p_demux->p_sys->b_no_data = false;
1739 p_demux->p_sys->i_no_data_ti = 0;
1741 if( i_pts > 0 && !tk->b_muxed )
1747 /*****************************************************************************
1749 *****************************************************************************/
1750 static void StreamClose( void *p_private )
1752 live_track_t *tk = (live_track_t*)p_private;
1753 demux_t *p_demux = tk->p_demux;
1754 demux_sys_t *p_sys = p_demux->p_sys;
1756 msg_Dbg( p_demux, "StreamClose" );
1758 p_sys->event = 0xff;
1759 p_demux->b_error = true;
1763 /*****************************************************************************
1765 *****************************************************************************/
1766 static void TaskInterrupt( void *p_private )
1768 demux_t *p_demux = (demux_t*)p_private;
1770 p_demux->p_sys->i_no_data_ti++;
1773 p_demux->p_sys->event = 0xff;
1776 /*****************************************************************************
1778 *****************************************************************************/
1779 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1781 p_timeout->b_die = false;
1782 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1783 p_timeout->i_remain *= 1000000;
1785 vlc_thread_ready( p_timeout );
1788 while( !p_timeout->b_die )
1790 if( p_timeout->i_remain <= 0 )
1792 char *psz_bye = NULL;
1793 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1794 p_timeout->i_remain *= 1000000;
1795 msg_Dbg( p_timeout, "reset the timeout timer" );
1796 if( p_timeout->b_handle_keep_alive == true )
1798 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
1799 p_timeout->p_sys->rtsp->getMediaSessionParameter( *p_timeout->p_sys->ms, NULL, psz_bye );
1801 p_timeout->p_sys->b_timeout_call = false;
1805 p_timeout->p_sys->b_timeout_call = true;
1808 p_timeout->i_remain -= 200000;
1809 msleep( 200000 ); /* 200 ms */
1813 /*****************************************************************************
1815 *****************************************************************************/
1816 static int b64_decode( char *dest, char *src );
1818 static int ParseASF( demux_t *p_demux )
1820 demux_sys_t *p_sys = p_demux->p_sys;
1822 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1823 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1827 /* Parse the asf header */
1828 if( psz_asf == NULL )
1829 return VLC_EGENERIC;
1831 psz_asf += strlen( psz_marker );
1832 psz_asf = strdup( psz_asf ); /* Duplicate it */
1833 psz_end = strchr( psz_asf, '\n' );
1835 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1838 if( psz_asf >= psz_end )
1841 return VLC_EGENERIC;
1844 /* Always smaller */
1845 p_header = block_New( p_demux, psz_end - psz_asf );
1846 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1847 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1848 if( p_header->i_buffer <= 0 )
1851 return VLC_EGENERIC;
1854 /* Parse it to get packet size */
1855 asf_HeaderParse( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1857 /* Send it to demuxer */
1858 stream_DemuxSend( p_sys->p_out_asf, p_header );
1865 static unsigned char* parseH264ConfigStr( char const* configStr,
1866 unsigned int& configSize )
1869 int i, i_records = 1;
1874 if( configStr == NULL || *configStr == '\0' )
1877 psz = dup = strdup( configStr );
1879 /* Count the number of comma's */
1880 for( psz = dup; *psz != '\0'; ++psz )
1889 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1891 for( i = 0; i < i_records; i++ )
1893 cfg[configSize++] = 0x00;
1894 cfg[configSize++] = 0x00;
1895 cfg[configSize++] = 0x00;
1896 cfg[configSize++] = 0x01;
1898 configSize += b64_decode( (char*)&cfg[configSize], psz );
1899 psz += strlen(psz)+1;
1906 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1907 static int b64_decode( char *dest, char *src )
1909 const char *dest_start = dest;
1913 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1914 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1915 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1916 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1917 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1918 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1919 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1920 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1921 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1922 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1923 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1924 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1925 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1926 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1927 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1928 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1931 for( i_level = 0; *src != '\0'; src++ )
1935 c = b64[(unsigned int)*src];
1947 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1951 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1955 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1963 return dest - dest_start;