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>
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 ? */
62 /* XXX Ugly workaround: Buggy Mac OS X headers fail to define them */
64 # define INT64_C(v) (v ## LL)
67 # define UINT64_C(v) (v ## ULL)
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Open ( vlc_object_t * );
76 static void Close( vlc_object_t * );
78 #define CACHING_TEXT N_("Caching value (ms)")
79 #define CACHING_LONGTEXT N_( \
80 "Allows you to modify the default caching value for RTSP streams. This " \
81 "value should be set in millisecond units." )
83 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
84 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
85 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
86 "for communication. In this mode you cannot connect to normal RTSP servers." )
88 #define USER_TEXT N_("RTSP user name")
89 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
90 "be used for authenticating the connection.")
91 #define PASS_TEXT N_("RTSP password")
92 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
93 "used for the connection.")
96 set_description( N_("RTP/RTSP/SDP demuxer (using Live555)" ) );
97 set_capability( "demux", 50 );
98 set_shortname( "RTP/RTSP");
99 set_callbacks( Open, Close );
100 add_shortcut( "live" );
101 add_shortcut( "livedotcom" );
102 set_category( CAT_INPUT );
103 set_subcategory( SUBCAT_INPUT_DEMUX );
106 set_description( N_("RTSP/RTP access and demux") );
107 add_shortcut( "rtsp" );
108 add_shortcut( "sdp" );
109 set_capability( "access_demux", 0 );
110 set_callbacks( Open, Close );
111 add_bool( "rtsp-tcp", 0, NULL,
112 N_("Use RTP over RTSP (TCP)"),
113 N_("Use RTP over RTSP (TCP)"), true );
114 add_integer( "rtp-client-port", -1, NULL,
116 N_("Port to use for the RTP source of the session"), true );
117 add_bool( "rtsp-http", 0, NULL,
118 N_("Tunnel RTSP and RTP over HTTP"),
119 N_("Tunnel RTSP and RTP over HTTP"), true );
120 add_integer( "rtsp-http-port", 80, NULL,
121 N_("HTTP tunnel port"),
122 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
124 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
125 CACHING_TEXT, CACHING_LONGTEXT, true );
126 add_bool( "rtsp-kasenna", false, NULL, KASENNA_TEXT,
127 KASENNA_LONGTEXT, true );
128 add_string( "rtsp-user", NULL, NULL, USER_TEXT,
129 USER_LONGTEXT, true );
130 add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
131 PASS_LONGTEXT, true );
135 /*****************************************************************************
137 *****************************************************************************/
142 MediaSubsession *sub;
150 stream_t *p_out_muxed; /* for muxed stream */
153 unsigned int i_buffer;
158 u_int32_t i_start_seq;
162 struct timeout_thread_t
167 bool b_handle_keep_alive;
173 char *p_sdp; /* XXX mallocated */
174 char *psz_path; /* URL-encoded path */
178 TaskScheduler *scheduler;
179 UsageEnvironment *env ;
184 live_track_t **track; /* XXX mallocated */
192 int64_t i_pcr; /* The clock */
193 int64_t i_npt; /* The current time in the stream */
194 int64_t i_npt_length;
197 /* timeout thread information */
198 int i_timeout; /* session timeout value in seconds */
199 bool b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
200 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
203 bool b_multicast; /* true if one of the tracks is multicasted */
204 bool b_no_data; /* true if we never receive any data */
205 int i_no_data_ti; /* consecutive number of TaskInterrupt */
210 static int Demux ( demux_t * );
211 static int Control( demux_t *, int, va_list );
213 static int Connect ( demux_t * );
214 static int SessionsSetup( demux_t * );
215 static int Play ( demux_t *);
216 static int ParseASF ( demux_t * );
217 static int RollOverTcp ( demux_t * );
219 static void StreamRead ( void *, unsigned int, unsigned int,
220 struct timeval, unsigned int );
221 static void StreamClose ( void * );
222 static void TaskInterrupt( void * );
224 static void TimeoutPrevention( timeout_thread_t * );
226 static unsigned char* parseH264ConfigStr( char const* configStr,
227 unsigned int& configSize );
229 /*****************************************************************************
231 *****************************************************************************/
232 static int Open ( vlc_object_t *p_this )
234 demux_t *p_demux = (demux_t*)p_this;
235 demux_sys_t *p_sys = NULL;
237 MediaSubsessionIterator *iter = NULL;
238 MediaSubsession *sub = NULL;
240 int i_error = VLC_EGENERIC;
244 /* See if it looks like a SDP
245 v, o, s fields are mandatory and in this order */
246 const uint8_t *p_peek;
247 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
249 if( memcmp( p_peek, "v=0\r\n", 5 ) &&
250 memcmp( p_peek, "v=0\n", 4 ) &&
251 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
258 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
261 p_demux->pf_demux = Demux;
262 p_demux->pf_control= Control;
263 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
264 if( !p_sys ) return VLC_ENOMEM;
267 p_sys->scheduler = NULL;
275 p_sys->i_npt_start = 0;
276 p_sys->i_npt_length = 0;
277 p_sys->p_out_asf = NULL;
278 p_sys->b_no_data = true;
279 p_sys->i_no_data_ti = 0;
280 p_sys->p_timeout = NULL;
281 p_sys->i_timeout = 0;
282 p_sys->b_timeout_call = false;
283 p_sys->b_multicast = false;
284 p_sys->b_real = false;
285 p_sys->psz_path = strdup( p_demux->psz_path );
287 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
288 vlc_UrlParse( &p_sys->url, p_sys->psz_path, 0 );
290 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
292 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
295 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
297 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
301 if( strcasecmp( p_demux->psz_access, "sdp" ) )
303 char *p = p_sys->psz_path;
304 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
307 if( p_demux->s != NULL )
309 /* Gather the complete sdp file */
311 int i_sdp_max = 1000;
312 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
316 i_error = VLC_ENOMEM;
322 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
323 i_sdp_max - i_sdp - 1 );
325 if( p_demux->b_die || p_demux->b_error )
333 msg_Err( p_demux, "failed to read SDP" );
340 if( i_read < i_sdp_max - i_sdp - 1 )
347 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
349 p_sys->p_sdp = (char*)p_sdp;
351 else if( ( p_demux->s == NULL ) &&
352 !strcasecmp( p_demux->psz_access, "sdp" ) )
354 /* sdp:// link from SAP */
355 p_sys->p_sdp = strdup( p_sys->psz_path );
357 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
359 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
363 if( p_sys->p_sdp == NULL )
365 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
366 i_error = VLC_ENOMEM;
370 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
372 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
376 if( p_sys->b_real ) goto error;
378 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
381 if( p_sys->p_out_asf && ParseASF( p_demux ) )
383 msg_Err( p_demux, "cannot find a usable asf header" );
384 /* TODO Clean tracks */
388 if( p_sys->i_track <= 0 )
394 for( i = 0; i < p_sys->i_track; i++ )
396 live_track_t *tk = p_sys->track[i];
398 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
399 es_format_Clean( &tk->fmt );
400 free( tk->p_buffer );
404 if( p_sys->i_track ) free( p_sys->track );
405 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
406 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
407 if( p_sys->ms ) Medium::close( p_sys->ms );
408 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
409 if( p_sys->env ) p_sys->env->reclaim();
410 if( p_sys->p_timeout )
412 vlc_object_kill( p_sys->p_timeout );
413 vlc_thread_join( p_sys->p_timeout );
414 vlc_object_detach( p_sys->p_timeout );
415 vlc_object_release( p_sys->p_timeout );
417 delete p_sys->scheduler;
418 free( p_sys->p_sdp );
419 free( p_sys->psz_path );
421 vlc_UrlClean( &p_sys->url );
427 /*****************************************************************************
429 *****************************************************************************/
430 static void Close( vlc_object_t *p_this )
432 demux_t *p_demux = (demux_t*)p_this;
433 demux_sys_t *p_sys = p_demux->p_sys;
436 for( i = 0; i < p_sys->i_track; i++ )
438 live_track_t *tk = p_sys->track[i];
440 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
441 es_format_Clean( &tk->fmt );
442 free( tk->p_buffer );
446 if( p_sys->i_track ) free( p_sys->track );
447 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
448 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
449 if( p_sys->ms ) Medium::close( p_sys->ms );
450 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
451 if( p_sys->env ) p_sys->env->reclaim();
452 if( p_sys->p_timeout )
454 vlc_object_kill( p_sys->p_timeout );
455 vlc_thread_join( p_sys->p_timeout );
456 vlc_object_detach( p_sys->p_timeout );
457 vlc_object_release( p_sys->p_timeout );
459 delete p_sys->scheduler;
460 free( p_sys->p_sdp );
461 free( p_sys->psz_path );
463 vlc_UrlClean( &p_sys->url );
468 /*****************************************************************************
469 * Connect: connects to the RTSP server to setup the session DESCRIBE
470 *****************************************************************************/
471 static int Connect( demux_t *p_demux )
473 demux_sys_t *p_sys = p_demux->p_sys;
474 Authenticator authenticator;
475 bool b_firstpass = true;
476 char *psz_user = NULL;
477 char *psz_pwd = NULL;
478 char *psz_url = NULL;
479 char *psz_options = NULL;
482 int i_ret = VLC_SUCCESS;
485 if( p_sys->url.i_port == 0 ) p_sys->url.i_port = 554;
486 if( p_sys->url.psz_username || p_sys->url.psz_password )
489 err = asprintf( &psz_url, "rtsp://%s:%d%s", p_sys->url.psz_host,
490 p_sys->url.i_port, p_sys->url.psz_path );
491 if( err == -1 ) return VLC_ENOMEM;
493 psz_user = strdup( p_sys->url.psz_username );
494 psz_pwd = strdup( p_sys->url.psz_password );
499 err = asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
500 if( err == -1 ) return VLC_ENOMEM;
502 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
503 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
509 if( p_demux->b_die || p_demux->b_error )
517 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
518 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
520 if( ( p_sys->rtsp = RTSPClient::createNew( *p_sys->env,
521 var_CreateGetInteger( p_demux, "verbose" ) > 1,
522 "VLC media player", i_http_port ) ) == NULL )
524 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
525 p_sys->env->getResultMsg() );
532 /* Kasenna enables KeepAlive by analysing the User-Agent string.
533 * Appending _KA to the string should be enough to enable this feature,
534 * however, there is a bug where the _KA doesn't get parsed from the
535 * default User-Agent as created by VLC/Live555 code. This is probably due
536 * to spaces in the string or the string being too long. Here we override
537 * the default string with a more compact version.
539 if( var_CreateGetBool( p_demux, "rtsp-kasenna" ))
541 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
542 p_sys->rtsp->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
547 authenticator.setUsernameAndPassword( (const char*)psz_user,
548 (const char*)psz_pwd );
550 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url, psz_user, psz_pwd,
552 delete [] psz_options;
554 p_sdp = p_sys->rtsp->describeURL( psz_url, &authenticator,
555 var_GetBool( p_demux, "rtsp-kasenna" ) );
558 /* failure occurred */
560 const char *psz_error = p_sys->env->getResultMsg();
562 if( var_GetBool( p_demux, "rtsp-http" ) )
563 sscanf( psz_error, "%*s %*s HTTP GET %*s HTTP/%*u.%*u %3u %*s",
567 const char *psz_tmp = strstr( psz_error, "RTSP" );
569 sscanf( psz_tmp, "RTSP/%*s%3u", &i_code );
573 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
576 { /* describeURL always returns an "RTSP/1.0 401 Unauthorized" the
577 * first time. This is a workaround to avoid asking for a
578 * user/passwd the first time the code passess here. */
586 msg_Dbg( p_demux, "authentication failed" );
590 psz_user = psz_pwd = NULL;
592 i_result = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
593 _("Please enter a valid login name and a password."),
594 &psz_user, &psz_pwd );
595 if( i_result == DIALOG_OK_YES )
597 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
602 else if( (i_code != 0) && !var_GetBool( p_demux, "rtsp-http" ) )
604 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
607 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
608 var_Set( p_demux, "rtsp-http", val );
609 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
615 msg_Dbg( p_demux, "connection timeout, retrying" );
616 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
618 if( i_lefttries > 0 )
621 i_ret = VLC_EGENERIC;
624 /* malloc-ated copy */
629 free( p_sys->p_sdp );
631 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
637 /*****************************************************************************
638 * SessionsSetup: prepares the subsessions and does the SETUP
639 *****************************************************************************/
640 static int SessionsSetup( demux_t *p_demux )
642 demux_sys_t *p_sys = p_demux->p_sys;
643 MediaSubsessionIterator *iter = NULL;
644 MediaSubsession *sub = NULL;
646 bool b_rtsp_tcp = false;
648 int i_return = VLC_SUCCESS;
649 unsigned int i_buffer = 0;
650 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
652 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" ) ||
653 var_GetBool( p_demux, "rtsp-http" );
654 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
656 /* Create the session from the SDP */
657 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
659 msg_Err( p_demux, "Could not create the RTSP Session: %s",
660 p_sys->env->getResultMsg() );
664 /* Initialise each media subsession */
665 iter = new MediaSubsessionIterator( *p_sys->ms );
666 while( ( sub = iter->next() ) != NULL )
671 if( p_demux->b_die || p_demux->b_error )
677 /* Value taken from mplayer */
678 if( !strcmp( sub->mediumName(), "audio" ) )
680 else if( !strcmp( sub->mediumName(), "video" ) )
684 if( i_client_port != -1 )
686 sub->setClientPortNum( i_client_port );
690 if( strcasestr( sub->codecName(), "REAL" ) )
692 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
693 p_sys->b_real = true; /* This is a problem, we'll handle it later */
697 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
698 bInit = sub->initiate( 4 ); /* Constant ? */
700 bInit = sub->initiate();
704 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
705 sub->mediumName(), sub->codecName(),
706 p_sys->env->getResultMsg() );
710 if( sub->rtpSource() != NULL )
712 int fd = sub->rtpSource()->RTPgs()->socketNum();
714 /* Increase the buffer size */
716 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
718 /* Increase the RTP reorder timebuffer just a bit */
719 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
721 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
724 /* Issue the SETUP */
727 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
728 b_rtsp_tcp ? True : False ) ) )
730 /* if we get an unsupported transport error, toggle TCP use and try again */
731 if( !strstr(p_sys->env->getResultMsg(), "461 Unsupported Transport")
732 || !( p_sys->rtsp->setupMediaSubsession( *sub, False,
733 b_rtsp_tcp ? False : True ) ) )
735 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
736 sub->codecName(), p_sys->env->getResultMsg() );
742 /* Check if we will receive data from this subsession for this track */
743 if( sub->readSource() == NULL ) continue;
744 if( !p_sys->b_multicast )
746 /* Check, because we need diff. rollover behaviour for multicast */
747 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
750 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
756 tk->p_demux = p_demux;
759 tk->b_quicktime = false;
762 tk->p_out_muxed = NULL;
764 tk->b_rtcp_sync = false;
766 tk->i_buffer = 65536;
767 tk->p_buffer = (uint8_t *)malloc( 65536 );
774 /* Value taken from mplayer */
775 if( !strcmp( sub->mediumName(), "audio" ) )
777 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
778 tk->fmt.audio.i_channels = sub->numChannels();
779 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
781 if( !strcmp( sub->codecName(), "MPA" ) ||
782 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
783 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
785 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
786 tk->fmt.audio.i_rate = 0;
788 else if( !strcmp( sub->codecName(), "AC3" ) )
790 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
791 tk->fmt.audio.i_rate = 0;
793 else if( !strcmp( sub->codecName(), "L16" ) )
795 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
796 tk->fmt.audio.i_bitspersample = 16;
798 else if( !strcmp( sub->codecName(), "L8" ) )
800 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
801 tk->fmt.audio.i_bitspersample = 8;
803 else if( !strcmp( sub->codecName(), "PCMU" ) )
805 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
807 else if( !strcmp( sub->codecName(), "PCMA" ) )
809 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
811 else if( !strncmp( sub->codecName(), "G726", 4 ) )
813 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
814 tk->fmt.audio.i_rate = 8000;
815 tk->fmt.audio.i_channels = 1;
816 if( !strcmp( sub->codecName()+5, "40" ) )
817 tk->fmt.i_bitrate = 40000;
818 else if( !strcmp( sub->codecName()+5, "32" ) )
819 tk->fmt.i_bitrate = 32000;
820 else if( !strcmp( sub->codecName()+5, "24" ) )
821 tk->fmt.i_bitrate = 24000;
822 else if( !strcmp( sub->codecName()+5, "16" ) )
823 tk->fmt.i_bitrate = 16000;
825 else if( !strcmp( sub->codecName(), "AMR" ) )
827 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
829 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
831 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
833 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
835 unsigned int i_extra;
838 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
840 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
843 tk->fmt.i_extra = i_extra;
844 tk->fmt.p_extra = malloc( i_extra );
845 memcpy( tk->fmt.p_extra, p_extra, i_extra );
848 /* Because the "faad" decoder does not handle the LATM data length field
849 at the start of each returned LATM frame, tell the RTP source to omit it. */
850 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
852 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
854 unsigned int i_extra;
857 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
859 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
862 tk->fmt.i_extra = i_extra;
863 tk->fmt.p_extra = malloc( i_extra );
864 memcpy( tk->fmt.p_extra, p_extra, i_extra );
868 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
871 if( p_sys->p_out_asf == NULL )
872 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
875 else if( !strcmp( sub->codecName(), "X-QT" ) ||
876 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
878 tk->b_quicktime = true;
880 else if( !strcmp( sub->codecName(), "SPEEX" ) )
882 tk->fmt.i_codec = VLC_FOURCC( 's', 'p', 'x', 'r' );
883 if ( sub->rtpTimestampFrequency() )
884 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
887 msg_Warn( p_demux,"Using 8kHz as default sample rate." );
888 tk->fmt.audio.i_rate = 8000;
892 else if( !strcmp( sub->mediumName(), "video" ) )
894 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
895 if( !strcmp( sub->codecName(), "MPV" ) )
897 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
899 else if( !strcmp( sub->codecName(), "H263" ) ||
900 !strcmp( sub->codecName(), "H263-1998" ) ||
901 !strcmp( sub->codecName(), "H263-2000" ) )
903 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
905 else if( !strcmp( sub->codecName(), "H261" ) )
907 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
909 else if( !strcmp( sub->codecName(), "H264" ) )
911 unsigned int i_extra = 0;
912 uint8_t *p_extra = NULL;
914 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
915 tk->fmt.b_packetized = false;
917 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
920 tk->fmt.i_extra = i_extra;
921 tk->fmt.p_extra = malloc( i_extra );
922 memcpy( tk->fmt.p_extra, p_extra, i_extra );
927 else if( !strcmp( sub->codecName(), "JPEG" ) )
929 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
931 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
933 unsigned int i_extra;
936 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
938 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
941 tk->fmt.i_extra = i_extra;
942 tk->fmt.p_extra = malloc( i_extra );
943 memcpy( tk->fmt.p_extra, p_extra, i_extra );
947 else if( !strcmp( sub->codecName(), "X-QT" ) ||
948 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
949 !strcmp( sub->codecName(), "X-QDM" ) ||
950 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
951 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
953 tk->b_quicktime = true;
955 else if( !strcmp( sub->codecName(), "MP2T" ) )
958 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
960 else if( !strcmp( sub->codecName(), "MP2P" ) ||
961 !strcmp( sub->codecName(), "MP1S" ) )
964 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
967 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
970 if( p_sys->p_out_asf == NULL )
971 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
976 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
978 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
981 if( sub->rtcpInstance() != NULL )
983 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
986 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
989 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
990 p_sys->track[p_sys->i_track++] = tk;
995 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
996 es_format_Clean( &tk->fmt );
1002 if( p_sys->i_track <= 0 ) i_return = VLC_EGENERIC;
1004 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1005 /* Retrieve the starttime if possible */
1006 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1008 p_sys->i_npt_start = (int64_t) -1;
1010 if( p_sys->i_npt_start < 0 )
1011 p_sys->i_npt_start = -1;
1013 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1014 /* Retrieve the duration if possible */
1015 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1017 p_sys->i_npt_length = (int64_t) -1;
1019 if( p_sys->i_npt_length < 0 )
1020 p_sys->i_npt_length = -1;
1022 msg_Dbg( p_demux, "setup start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1026 /*****************************************************************************
1027 * Play: starts the actual playback of the stream
1028 *****************************************************************************/
1029 static int Play( demux_t *p_demux )
1031 demux_sys_t *p_sys = p_demux->p_sys;
1037 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, p_sys->i_npt_start, -1, 1 ) )
1039 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
1040 return VLC_EGENERIC;
1043 /* Retrieve the timeout value and set up a timeout prevention thread */
1044 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
1045 if( p_sys->i_timeout <= 0 )
1046 p_sys->i_timeout = 60; /* default value from RFC2326 */
1048 /* start timeout-thread only on x-asf streams (wms), it has rtcp support but doesn't
1049 * seem to use it for liveness/keep-alive, get_parameter seems to work for it. get_parameter
1050 * doesn't work with dss 5.5.4 & 5.5.5, they seems to work with rtcp */
1051 if( !p_sys->p_timeout && p_sys->p_out_asf )
1053 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
1054 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
1055 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
1056 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
1057 VLC_THREAD_PRIORITY_LOW, true ) )
1059 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
1060 vlc_object_release( p_sys->p_timeout );
1062 msg_Dbg( p_demux, "spawned timeout thread" );
1063 vlc_object_attach( p_sys->p_timeout, p_demux );
1068 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1069 for( i = 0; i < p_sys->i_track; i++ )
1071 if( !p_sys->track[i]->b_rtcp_sync )
1072 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1073 p_sys->track[i]->i_start_seq = (int)p_sys->track[i]->sub->rtpInfo.seqNum;
1074 msg_Info( p_demux, "set startseq: %u", p_sys->track[i]->i_start_seq );
1078 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1079 /* Retrieve the starttime if possible */
1080 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1082 p_sys->i_npt_start = -1;
1084 if( p_sys->i_npt_start < 0 )
1086 p_sys->i_npt_start = -1;
1090 p_sys->i_npt = p_sys->i_npt_start;
1092 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1093 /* Retrieve the duration if possible */
1094 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1096 p_sys->i_npt_length = -1;
1098 if( p_sys->i_npt_length < 0 )
1099 p_sys->i_npt_length = -1;
1101 msg_Dbg( p_demux, "play start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1106 /*****************************************************************************
1108 *****************************************************************************/
1109 static int Demux( demux_t *p_demux )
1111 demux_sys_t *p_sys = p_demux->p_sys;
1114 bool b_send_pcr = true;
1118 /* Check if we need to send the server a Keep-A-Live signal */
1119 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
1121 char *psz_bye = NULL;
1122 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
1123 p_sys->b_timeout_call = false;
1127 for( i = 0; i < p_sys->i_track; i++ )
1129 live_track_t *tk = p_sys->track[i];
1131 if( tk->b_asf || tk->b_muxed )
1138 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
1144 if( p_sys->i_pcr > 0 )
1147 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1150 /* First warn we want to read data */
1152 for( i = 0; i < p_sys->i_track; i++ )
1154 live_track_t *tk = p_sys->track[i];
1156 if( tk->waiting == 0 )
1159 tk->sub->readSource()->getNextFrame( tk->p_buffer, tk->i_buffer,
1160 StreamRead, tk, StreamClose, tk );
1163 /* Create a task that will be called if we wait more than 300ms */
1164 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1167 p_sys->scheduler->doEventLoop( &p_sys->event );
1169 /* remove the task */
1170 p_sys->scheduler->unscheduleDelayedTask( task );
1172 /* Check for gap in pts value */
1173 for( i = 0; i < p_sys->i_track; i++ )
1175 live_track_t *tk = p_sys->track[i];
1177 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1178 tk->sub->rtpSource() && tk->sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1180 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1182 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1183 tk->b_rtcp_sync = true;
1191 if( p_sys->b_multicast && p_sys->b_no_data &&
1192 ( p_sys->i_no_data_ti > 120 ) )
1194 /* FIXME Make this configurable
1195 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1199 else if( !p_sys->b_multicast && p_sys->b_no_data &&
1200 ( p_sys->i_no_data_ti > 34 ) )
1202 bool b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" ) ||
1203 var_GetBool( p_demux, "rtsp-http" );
1205 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1207 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1208 if( RollOverTcp( p_demux ) )
1210 msg_Err( p_demux, "TCP rollover failed, aborting" );
1214 else if( p_sys->i_no_data_ti > 34 )
1216 msg_Err( p_demux, "no data received in 10s, aborting" );
1220 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1223 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1226 return p_demux->b_error ? 0 : 1;
1229 /*****************************************************************************
1231 *****************************************************************************/
1232 static int Control( demux_t *p_demux, int i_query, va_list args )
1234 demux_sys_t *p_sys = p_demux->p_sys;
1237 bool *pb, *pb2, b_bool;
1242 case DEMUX_GET_TIME:
1243 pi64 = (int64_t*)va_arg( args, int64_t * );
1244 if( p_sys->i_npt > 0 )
1246 *pi64 = p_sys->i_npt;
1249 return VLC_EGENERIC;
1251 case DEMUX_GET_LENGTH:
1252 pi64 = (int64_t*)va_arg( args, int64_t * );
1253 if( p_sys->i_npt_length > 0 )
1255 *pi64 = p_sys->i_npt_length;
1258 return VLC_EGENERIC;
1260 case DEMUX_GET_POSITION:
1261 pf = (double*)va_arg( args, double* );
1262 if( p_sys->i_npt_length > 0 && p_sys->i_npt > 0 )
1264 *pf = (double)p_sys->i_npt / (double)p_sys->i_npt_length;
1267 return VLC_EGENERIC;
1269 case DEMUX_SET_POSITION:
1273 f = (double)va_arg( args, double );
1274 if( p_sys->rtsp && p_sys->i_npt_length > 0 )
1277 time = f * (double)p_sys->i_npt_length / (double)1000000.0; /* in second */
1278 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time, -1, 1 ) )
1280 msg_Err( p_demux, "PLAY failed %s",
1281 p_sys->env->getResultMsg() );
1282 return VLC_EGENERIC;
1284 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1286 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1287 /* Retrieve RTP-Info values */
1288 for( i = 0; i < p_sys->i_track; i++ )
1290 //if( !p_sys->track[i]->b_rtcp_sync )
1291 p_sys->track[i]->b_rtcp_sync = false;
1292 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1293 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1294 msg_Info( p_demux, "set pos startseq: %u", p_sys->track[i]->i_start_seq );
1297 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1298 /* Retrieve the starttime if possible */
1299 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1301 p_sys->i_npt_start = -1;
1303 if( p_sys->i_npt_start < 0 )
1305 p_sys->i_npt_start = -1;
1309 p_sys->i_npt = p_sys->i_npt_start;
1311 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1312 /* Retrieve the duration if possible */
1313 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1315 p_sys->i_npt_length = -1;
1317 if( p_sys->i_npt_length < 0 )
1318 p_sys->i_npt_length = -1;
1320 msg_Dbg( p_demux, "seek start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1323 return VLC_EGENERIC;
1325 /* Special for access_demux */
1326 case DEMUX_CAN_PAUSE:
1327 case DEMUX_CAN_SEEK:
1328 pb = (bool*)va_arg( args, bool * );
1329 if( p_sys->rtsp && p_sys->i_npt_length )
1330 /* Not always true, but will be handled in SET_PAUSE_STATE */
1336 case DEMUX_CAN_CONTROL_PACE:
1337 pb = (bool*)va_arg( args, bool * );
1339 #if 1 /* Disable for now until we have a clock synchro algo
1340 * which works with something else than MPEG over UDP */
1348 case DEMUX_CAN_CONTROL_RATE:
1349 pb = (bool*)va_arg( args, bool * );
1350 pb2 = (bool*)va_arg( args, bool * );
1352 *pb = p_sys->rtsp != NULL && p_sys->i_npt_length > 0 && !var_GetBool( p_demux, "rtsp-kasenna" );
1356 case DEMUX_SET_RATE:
1360 if( !p_sys->rtsp || p_sys->i_npt_length <= 0 || var_GetBool( p_demux, "rtsp-kasenna" ) )
1361 return VLC_EGENERIC;
1363 /* TODO we might want to ensure that the new rate is different from
1364 * old rate after playMediaSession...
1365 * I have no idea how the server map the requested rate to the
1368 * current is x2 we request x1.5 if the server return x2 we will
1369 * never succeed to return to x1.
1370 * In this case we should retry with a lower rate until we have
1374 pi_int = (int*)va_arg( args, int * );
1375 f_scale = (double)INPUT_RATE_DEFAULT / (*p_int);
1377 /* Passing -1 for the start and end time will mean liveMedia won't
1378 * create a Range: section for the RTSP message. The server should
1379 * pick up from the current position */
1380 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, -1, -1, f_scale ) )
1382 msg_Err( p_demux, "PLAY with Scale %0.2f failed %s", f_scale,
1383 p_sys->env->getResultMsg() );
1384 return VLC_EGENERIC;
1386 /* ReSync the stream */
1387 p_sys->i_npt_start = 0;
1389 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1391 *pi_int = (int)( INPUT_RATE_DEFAULT / p_sys->ms->scale() + 0.5 );
1396 case DEMUX_SET_PAUSE_STATE:
1398 double d_npt = (double) p_sys->i_npt / INT64_C(1000000);
1401 b_bool = (bool)va_arg( args, int );
1402 if( p_sys->rtsp == NULL )
1403 return VLC_EGENERIC;
1406 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1407 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1408 d_npt > 0 ? d_npt : -1 ) ) )
1410 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1411 return VLC_EGENERIC;
1414 /* When we Pause, we'll need the TimeoutPrevention thread to
1415 * handle sending the "Keep Alive" message to the server.
1416 * Unfortunately Live555 isn't thread safe and so can't
1417 * do this normally while the main Demux thread is handling
1418 * a live stream. We end up with the Timeout thread blocking
1419 * waiting for a response from the server. So when we PAUSE
1420 * we set a flag that the TimeoutPrevention function will check
1421 * and if it's set, it will trigger the GET_PARAMETER message */
1422 if( b_bool && p_sys->p_timeout != NULL )
1423 p_sys->p_timeout->b_handle_keep_alive = true;
1424 else if( !b_bool && p_sys->p_timeout != NULL )
1425 p_sys->p_timeout->b_handle_keep_alive = false;
1427 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1429 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1430 for( i = 0; i < p_sys->i_track; i++ )
1432 if( !p_sys->track[i]->b_rtcp_sync )
1433 p_sys->track[i]->i_pts = 0; // (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1434 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1435 msg_Info( p_demux, "set pause startseq: %u", p_sys->track[i]->i_start_seq );
1438 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1439 /* Retrieve the starttime if possible */
1440 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1442 p_sys->i_npt_start = -1;
1444 if( p_sys->i_npt_start < 0 )
1446 p_sys->i_npt_start = -1;
1450 p_sys->i_npt = p_sys->i_npt_start;
1452 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1199404800)
1453 /* Retrieve the duration if possible */
1454 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1456 p_sys->i_npt_length = -1;
1458 if( p_sys->i_npt_length < 0 )
1459 p_sys->i_npt_length = -1;
1461 msg_Dbg( p_demux, "pause start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1464 case DEMUX_GET_TITLE_INFO:
1465 case DEMUX_SET_TITLE:
1466 case DEMUX_SET_SEEKPOINT:
1467 return VLC_EGENERIC;
1469 case DEMUX_GET_PTS_DELAY:
1470 pi64 = (int64_t*)va_arg( args, int64_t * );
1471 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1475 return VLC_EGENERIC;
1479 /*****************************************************************************
1480 * RollOverTcp: reopen the rtsp into TCP mode
1481 * XXX: ugly, a lot of code are duplicated from Open()
1482 * This should REALLY be fixed
1483 *****************************************************************************/
1484 static int RollOverTcp( demux_t *p_demux )
1486 demux_sys_t *p_sys = p_demux->p_sys;
1489 var_SetBool( p_demux, "rtsp-tcp", true );
1491 /* We close the old RTSP session */
1492 for( i = 0; i < p_sys->i_track; i++ )
1494 live_track_t *tk = p_sys->track[i];
1496 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
1497 es_format_Clean( &tk->fmt );
1498 free( tk->p_buffer );
1501 if( p_sys->i_track ) free( p_sys->track );
1502 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
1504 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1505 Medium::close( p_sys->ms );
1506 RTSPClient::close( p_sys->rtsp );
1510 p_sys->track = NULL;
1513 /* Reopen rtsp client */
1514 if( ( p_demux->s != NULL ) &&
1515 ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1517 msg_Err( p_demux, "Failed to connect with rtsp://%s",
1522 if( p_sys->p_sdp == NULL )
1524 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1528 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1530 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1534 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1540 return VLC_EGENERIC;
1544 /*****************************************************************************
1546 *****************************************************************************/
1547 static void StreamRead( void *p_private, unsigned int i_size,
1548 unsigned int i_truncated_bytes, struct timeval pts,
1549 unsigned int duration )
1551 live_track_t *tk = (live_track_t*)p_private;
1552 demux_t *p_demux = tk->p_demux;
1553 demux_sys_t *p_sys = p_demux->p_sys;
1556 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1558 int64_t i_pts = (uint64_t)pts.tv_sec * UINT64_C(1000000) +
1559 (uint64_t)pts.tv_usec;
1561 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1562 i_pts &= UINT64_C(0x00ffffffffffffff);
1564 if( tk->b_quicktime && tk->p_es == NULL )
1566 QuickTimeGenericRTPSource *qtRTPSource =
1567 (QuickTimeGenericRTPSource*)tk->sub->rtpSource();
1568 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1569 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1571 if( tk->fmt.i_cat == VIDEO_ES ) {
1572 if( qtState.sdAtomSize < 16 + 32 )
1575 p_sys->event = 0xff;
1579 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1580 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1581 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1583 if( tk->fmt.i_codec == VLC_FOURCC('a', 'v', 'c', '1') )
1585 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
1586 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
1587 + qtRTPSource->qtState.sdAtomSize;
1588 while (pos+8 < endpos) {
1589 unsigned int atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
1590 if( atomLength == 0 || atomLength > (unsigned int)(endpos-pos)) break;
1591 if( memcmp(pos+4, "avcC", 4) == 0 &&
1593 atomLength <= INT_MAX )
1595 tk->fmt.i_extra = atomLength-8;
1596 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1597 memcpy(tk->fmt.p_extra, pos+8, atomLength-8);
1605 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1606 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1607 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1611 if( qtState.sdAtomSize < 4 )
1614 p_sys->event = 0xff;
1618 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1620 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1624 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1626 pts.tv_sec * 1000000LL + pts.tv_usec );
1629 /* grow buffer if it looks like buffer is too small, but don't eat
1630 * up all the memory on strange streams */
1631 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1634 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1635 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1637 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1640 msg_Warn( p_demux, "realloc failed" );
1644 tk->p_buffer = (uint8_t*)p_tmp;
1647 if( i_size > tk->i_buffer )
1649 msg_Warn( p_demux, "buffer overflow" );
1651 /* FIXME could i_size be > buffer size ? */
1652 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1653 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1655 AMRAudioSource *amrSource = (AMRAudioSource*)tk->sub->readSource();
1657 p_block = block_New( p_demux, i_size + 1 );
1658 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1659 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1661 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1663 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1664 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->sub->rtpSource();
1665 uint32_t header = h261Source->lastSpecialHeader();
1667 uint32_t header = 0;
1668 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1670 p_block = block_New( p_demux, i_size + 4 );
1671 memcpy( p_block->p_buffer, &header, 4 );
1672 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1674 if( tk->sub->rtpSource()->curPacketMarkerBit() )
1675 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1677 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1679 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1680 msg_Warn( p_demux, "unsupported NAL type for H264" );
1682 /* Normal NAL type */
1683 p_block = block_New( p_demux, i_size + 4 );
1684 p_block->p_buffer[0] = 0x00;
1685 p_block->p_buffer[1] = 0x00;
1686 p_block->p_buffer[2] = 0x00;
1687 p_block->p_buffer[3] = 0x01;
1688 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1690 else if( tk->b_asf )
1692 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1693 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1695 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1699 p_block = block_New( p_demux, i_size );
1700 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1704 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1705 if( (tk->fmt.i_cat == VIDEO_ES) && (p_sys->i_pcr < i_pts) &&
1706 (i_pts > 0) && (p_sys->i_pcr > 0) )
1708 p_sys->i_npt += __MAX( 0, i_pts - p_sys->i_pcr );
1709 p_sys->i_pcr = i_pts;
1710 //msg_Dbg( p_demux, "npt update" );
1712 else if( /*tk->fmt.i_cat == VIDEO_ES &&*/ p_sys->i_pcr < i_pts )
1714 p_sys->i_pcr = i_pts;
1716 //msg_Dbg( p_demux, "npt %lld", p_sys->i_npt );
1718 if( (i_pts != tk->i_pts) && (!tk->b_muxed) )
1720 p_block->i_pts = i_pts;
1724 /*FIXME: for h264 you should check that packetization-mode=1 in sdp-file */
1725 p_block->i_dts = ( tk->fmt.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'v' ) ) ? 0 : i_pts;
1730 stream_DemuxSend( tk->p_out_muxed, p_block );
1732 else if( tk->b_asf )
1734 stream_DemuxSend( p_sys->p_out_asf, p_block );
1738 es_out_Send( p_demux->out, tk->p_es, p_block );
1741 /* warn that's ok */
1742 p_sys->event = 0xff;
1744 /* we have read data */
1746 p_demux->p_sys->b_no_data = false;
1747 p_demux->p_sys->i_no_data_ti = 0;
1749 if( i_pts > 0 && !tk->b_muxed )
1755 /*****************************************************************************
1757 *****************************************************************************/
1758 static void StreamClose( void *p_private )
1760 live_track_t *tk = (live_track_t*)p_private;
1761 demux_t *p_demux = tk->p_demux;
1762 demux_sys_t *p_sys = p_demux->p_sys;
1764 msg_Dbg( p_demux, "StreamClose" );
1766 p_sys->event = 0xff;
1767 p_demux->b_error = true;
1771 /*****************************************************************************
1773 *****************************************************************************/
1774 static void TaskInterrupt( void *p_private )
1776 demux_t *p_demux = (demux_t*)p_private;
1778 p_demux->p_sys->i_no_data_ti++;
1781 p_demux->p_sys->event = 0xff;
1784 /*****************************************************************************
1786 *****************************************************************************/
1787 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1789 p_timeout->b_die = false;
1790 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1791 p_timeout->i_remain *= 1000000;
1793 vlc_thread_ready( p_timeout );
1796 while( !p_timeout->b_die )
1798 if( p_timeout->i_remain <= 0 )
1800 char *psz_bye = NULL;
1801 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1802 p_timeout->i_remain *= 1000000;
1803 msg_Dbg( p_timeout, "reset the timeout timer" );
1804 if( p_timeout->b_handle_keep_alive == true )
1806 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
1807 p_timeout->p_sys->rtsp->getMediaSessionParameter( *p_timeout->p_sys->ms, NULL, psz_bye );
1809 p_timeout->p_sys->b_timeout_call = false;
1813 p_timeout->p_sys->b_timeout_call = true;
1816 p_timeout->i_remain -= 200000;
1817 msleep( 200000 ); /* 200 ms */
1821 /*****************************************************************************
1823 *****************************************************************************/
1824 static int b64_decode( char *dest, char *src );
1826 static int ParseASF( demux_t *p_demux )
1828 demux_sys_t *p_sys = p_demux->p_sys;
1830 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1831 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1835 /* Parse the asf header */
1836 if( psz_asf == NULL )
1837 return VLC_EGENERIC;
1839 psz_asf += strlen( psz_marker );
1840 psz_asf = strdup( psz_asf ); /* Duplicate it */
1841 psz_end = strchr( psz_asf, '\n' );
1843 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1846 if( psz_asf >= psz_end )
1849 return VLC_EGENERIC;
1852 /* Always smaller */
1853 p_header = block_New( p_demux, psz_end - psz_asf );
1854 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1855 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1856 if( p_header->i_buffer <= 0 )
1859 return VLC_EGENERIC;
1862 /* Parse it to get packet size */
1863 asf_HeaderParse( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1865 /* Send it to demuxer */
1866 stream_DemuxSend( p_sys->p_out_asf, p_header );
1873 static unsigned char* parseH264ConfigStr( char const* configStr,
1874 unsigned int& configSize )
1877 int i, i_records = 1;
1882 if( configStr == NULL || *configStr == '\0' )
1885 psz = dup = strdup( configStr );
1887 /* Count the number of comma's */
1888 for( psz = dup; *psz != '\0'; ++psz )
1897 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1899 for( i = 0; i < i_records; i++ )
1901 cfg[configSize++] = 0x00;
1902 cfg[configSize++] = 0x00;
1903 cfg[configSize++] = 0x00;
1904 cfg[configSize++] = 0x01;
1906 configSize += b64_decode( (char*)&cfg[configSize], psz );
1907 psz += strlen(psz)+1;
1914 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1915 static int b64_decode( char *dest, char *src )
1917 const char *dest_start = dest;
1921 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1922 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1923 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1924 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1925 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1926 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1927 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1928 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1929 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1930 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1931 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1932 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1933 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1934 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1935 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1936 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1939 for( i_level = 0; *src != '\0'; src++ )
1943 c = b64[(unsigned int)*src];
1955 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1959 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1963 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1971 return dest - dest_start;