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 *****************************************************************************/
32 #include <vlc_demux.h>
33 #include <vlc_interface.h>
34 #include <vlc_network.h>
40 # include <winsock2.h>
43 #include "UsageEnvironment.hh"
44 #include "BasicUsageEnvironment.hh"
45 #include "GroupsockHelper.hh"
46 #include "liveMedia.hh"
49 #include "../access/mms/asf.h" /* Who said ugly ? */
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 #define CACHING_TEXT N_("Caching value (ms)")
61 #define CACHING_LONGTEXT N_( \
62 "Allows you to modify the default caching value for RTSP streams. This " \
63 "value should be set in millisecond units." )
65 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
66 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
67 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
68 "for communication. In this mode you cannot connect to normal RTSP servers." )
70 #define USER_TEXT N_("RTSP user name")
71 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
72 "be used for authenticating the connection.")
73 #define PASS_TEXT N_("RTSP password")
74 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
75 "used for the connection.")
78 set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) );
79 set_capability( "demux2", 50 );
80 set_shortname( "RTP/RTSP");
81 set_callbacks( Open, Close );
82 add_shortcut( "live" );
83 add_shortcut( "livedotcom" );
84 set_category( CAT_INPUT );
85 set_subcategory( SUBCAT_INPUT_DEMUX );
88 set_description( _("RTSP/RTP access and demux") );
89 add_shortcut( "rtsp" );
90 add_shortcut( "sdp" );
91 set_capability( "access_demux", 0 );
92 set_callbacks( Open, Close );
93 add_bool( "rtsp-tcp", 0, NULL,
94 N_("Use RTP over RTSP (TCP)"),
95 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
96 add_integer( "rtp-client-port", -1, NULL,
98 N_("Port to use for the RTP source of the session"), VLC_TRUE );
99 add_bool( "rtsp-http", 0, NULL,
100 N_("Tunnel RTSP and RTP over HTTP"),
101 N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE );
102 add_integer( "rtsp-http-port", 80, NULL,
103 N_("HTTP tunnel port"),
104 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
106 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
107 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
108 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
109 KASENNA_LONGTEXT, VLC_TRUE );
110 add_string( "rtsp-user", NULL, NULL, USER_TEXT,
111 USER_LONGTEXT, VLC_TRUE );
112 add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
113 PASS_LONGTEXT, VLC_TRUE );
117 /*****************************************************************************
119 *****************************************************************************/
124 MediaSubsession *sub;
130 vlc_bool_t b_quicktime;
132 stream_t *p_out_muxed; /* for muxed stream */
135 unsigned int i_buffer;
137 vlc_bool_t b_rtcp_sync;
140 u_int32_t i_start_seq;
144 struct timeout_thread_t
149 vlc_bool_t b_handle_keep_alive;
155 char *p_sdp; /* XXX mallocated */
156 char *psz_path; /* URL-encoded path */
160 TaskScheduler *scheduler;
161 UsageEnvironment *env ;
166 live_track_t **track; /* XXX mallocated */
174 int64_t i_pcr; /* The clock */
175 int64_t i_npt; /* The current time in the stream */
176 int64_t i_npt_length;
179 /* timeout thread information */
180 int i_timeout; /* session timeout value in seconds */
181 vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
182 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
185 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
186 vlc_bool_t b_no_data; /* true if we never receive any data */
187 int i_no_data_ti; /* consecutive number of TaskInterrupt */
192 static int Demux ( demux_t * );
193 static int Control( demux_t *, int, va_list );
195 static int Connect ( demux_t * );
196 static int SessionsSetup( demux_t * );
197 static int Play ( demux_t *);
198 static int ParseASF ( demux_t * );
199 static int RollOverTcp ( demux_t * );
201 static void StreamRead ( void *, unsigned int, unsigned int,
202 struct timeval, unsigned int );
203 static void StreamClose ( void * );
204 static void TaskInterrupt( void * );
206 static void TimeoutPrevention( timeout_thread_t * );
208 static unsigned char* parseH264ConfigStr( char const* configStr,
209 unsigned int& configSize );
211 /*****************************************************************************
213 *****************************************************************************/
214 static int Open ( vlc_object_t *p_this )
216 demux_t *p_demux = (demux_t*)p_this;
217 demux_sys_t *p_sys = NULL;
219 MediaSubsessionIterator *iter = NULL;
220 MediaSubsession *sub = NULL;
222 int i_error = VLC_EGENERIC;
226 /* See if it looks like a SDP
227 v, o, s fields are mandatory and in this order */
228 const uint8_t *p_peek;
229 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
231 if( memcmp( p_peek, "v=0\r\n", 5 ) &&
232 memcmp( p_peek, "v=0\n", 4 ) &&
233 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
240 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
243 p_demux->pf_demux = Demux;
244 p_demux->pf_control= Control;
245 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
246 if( !p_sys ) return VLC_ENOMEM;
249 p_sys->scheduler = NULL;
257 p_sys->i_npt_start = 0;
258 p_sys->i_npt_length = 0;
259 p_sys->p_out_asf = NULL;
260 p_sys->b_no_data = VLC_TRUE;
261 p_sys->i_no_data_ti = 0;
262 p_sys->p_timeout = NULL;
263 p_sys->i_timeout = 0;
264 p_sys->b_timeout_call = VLC_FALSE;
265 p_sys->b_multicast = VLC_FALSE;
266 p_sys->b_real = VLC_FALSE;
267 p_sys->psz_path = strdup( p_demux->psz_path );
269 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
270 vlc_UrlParse( &p_sys->url, p_sys->psz_path, 0 );
272 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
274 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
277 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
279 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
283 if( strcasecmp( p_demux->psz_access, "sdp" ) )
285 char *p = p_sys->psz_path;
286 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
289 if( p_demux->s != NULL )
291 /* Gather the complete sdp file */
293 int i_sdp_max = 1000;
294 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
298 i_error = VLC_ENOMEM;
304 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
305 i_sdp_max - i_sdp - 1 );
309 msg_Err( p_demux, "failed to read SDP" );
316 if( i_read < i_sdp_max - i_sdp - 1 )
323 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
325 p_sys->p_sdp = (char*)p_sdp;
327 else if( ( p_demux->s == NULL ) &&
328 !strcasecmp( p_demux->psz_access, "sdp" ) )
330 /* sdp:// link from SAP */
331 p_sys->p_sdp = strdup( p_sys->psz_path );
333 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
335 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
339 if( p_sys->p_sdp == NULL )
341 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
342 i_error = VLC_ENOMEM;
346 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
348 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
352 if( p_sys->b_real ) goto error;
354 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
357 if( p_sys->p_out_asf && ParseASF( p_demux ) )
359 msg_Err( p_demux, "cannot find a usable asf header" );
360 /* TODO Clean tracks */
364 if( p_sys->i_track <= 0 )
370 for( i = 0; i < p_sys->i_track; i++ )
372 live_track_t *tk = p_sys->track[i];
374 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
375 free( tk->p_buffer );
379 if( p_sys->i_track ) free( p_sys->track );
380 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
381 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
382 if( p_sys->ms ) Medium::close( p_sys->ms );
383 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
384 if( p_sys->env ) p_sys->env->reclaim();
385 if( p_sys->p_timeout )
387 vlc_object_kill( p_sys->p_timeout );
388 vlc_thread_join( p_sys->p_timeout );
389 vlc_object_detach( p_sys->p_timeout );
390 vlc_object_destroy( p_sys->p_timeout );
392 if( p_sys->scheduler ) delete p_sys->scheduler;
393 if( p_sys->p_sdp ) free( p_sys->p_sdp );
394 if( p_sys->psz_path ) free( p_sys->psz_path );
396 vlc_UrlClean( &p_sys->url );
402 /*****************************************************************************
404 *****************************************************************************/
405 static void Close( vlc_object_t *p_this )
407 demux_t *p_demux = (demux_t*)p_this;
408 demux_sys_t *p_sys = p_demux->p_sys;
411 for( i = 0; i < p_sys->i_track; i++ )
413 live_track_t *tk = p_sys->track[i];
415 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
416 free( tk->p_buffer );
420 if( p_sys->i_track ) free( p_sys->track );
421 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
422 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
423 if( p_sys->ms ) Medium::close( p_sys->ms );
424 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
425 if( p_sys->env ) p_sys->env->reclaim();
426 if( p_sys->p_timeout )
428 vlc_object_kill( p_sys->p_timeout );
429 vlc_thread_join( p_sys->p_timeout );
430 vlc_object_detach( p_sys->p_timeout );
431 vlc_object_destroy( p_sys->p_timeout );
433 if( p_sys->scheduler ) delete p_sys->scheduler;
434 if( p_sys->p_sdp ) free( p_sys->p_sdp );
435 if( p_sys->psz_path ) free( p_sys->psz_path );
437 vlc_UrlClean( &p_sys->url );
442 /*****************************************************************************
443 * Connect: connects to the RTSP server to setup the session DESCRIBE
444 *****************************************************************************/
445 static int Connect( demux_t *p_demux )
447 demux_sys_t *p_sys = p_demux->p_sys;
448 Authenticator authenticator;
450 char *psz_user = NULL;
451 char *psz_pwd = NULL;
452 char *psz_url = NULL;
453 char *psz_options = NULL;
456 int i_ret = VLC_SUCCESS;
458 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
459 if( !psz_url ) return VLC_ENOMEM;
461 if( p_sys->url.psz_username || p_sys->url.psz_password )
463 sprintf( psz_url, "rtsp://%s%s", p_sys->url.psz_host,
464 p_sys->url.psz_path );
466 psz_user = strdup( p_sys->url.psz_username );
467 psz_pwd = strdup( p_sys->url.psz_password );
471 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
473 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
474 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
478 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
479 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
481 if( ( p_sys->rtsp = RTSPClient::createNew( *p_sys->env,
482 p_demux->p_libvlc->i_verbose > 1,
483 "VLC media player", i_http_port ) ) == NULL )
485 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
486 p_sys->env->getResultMsg() );
490 /* Kasenna enables KeepAlive by analysing the User-Agent string.
491 * Appending _KA to the string should be enough to enable this feature,
492 * however, there is a bug where the _KA doesn't get parsed from the
493 * default User-Agent as created by VLC/Live555 code. This is probably due
494 * to spaces in the string or the string being too long. Here we override
495 * the default string with a more compact version.
497 if( var_CreateGetBool( p_demux, "rtsp-kasenna" ))
499 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
500 p_sys->rtsp->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
505 authenticator.setUsernameAndPassword( (const char*)psz_user,
506 (const char*)psz_pwd );
508 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url, psz_user, psz_pwd,
510 if( psz_options ) delete [] psz_options;
512 p_sdp = p_sys->rtsp->describeURL( psz_url, &authenticator,
513 var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
516 /* failure occurred */
518 const char *psz_error = p_sys->env->getResultMsg();
520 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
521 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
522 sscanf( psz_error, "%*s %*s HTTP GET %*s HTTP/%*u.%*u %3u %*s",
524 else sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
529 msg_Dbg( p_demux, "authentication failed" );
531 if( psz_user ) free( psz_user );
532 if( psz_pwd ) free( psz_pwd );
533 psz_user = psz_pwd = NULL;
535 i_result = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
536 _("Please enter a valid login name and a password."),
537 &psz_user, &psz_pwd );
538 if( i_result == DIALOG_OK_YES )
540 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
545 else if( !var_GetBool( p_demux, "rtsp-http" ) )
547 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
549 val.b_bool = VLC_TRUE;
550 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
551 var_Set( p_demux, "rtsp-http", val );
552 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
557 msg_Dbg( p_demux, "connection timeout, retrying" );
558 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
561 i_ret = VLC_EGENERIC;
563 if( psz_url ) free( psz_url );
565 /* malloc-ated copy */
566 if( psz_user ) free( psz_user );
567 if( psz_pwd ) free( psz_pwd );
569 if( p_sys->p_sdp ) free( p_sys->p_sdp );
571 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
577 /*****************************************************************************
578 * SessionsSetup: prepares the subsessions and does the SETUP
579 *****************************************************************************/
580 static int SessionsSetup( demux_t *p_demux )
582 demux_sys_t *p_sys = p_demux->p_sys;
583 MediaSubsessionIterator *iter = NULL;
584 MediaSubsession *sub = NULL;
586 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
588 int i_return = VLC_SUCCESS;
589 unsigned int i_buffer = 0;
590 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
592 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" ) ||
593 var_GetBool( p_demux, "rtsp-http" );
594 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
596 /* Create the session from the SDP */
597 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
599 msg_Err( p_demux, "Could not create the RTSP Session: %s",
600 p_sys->env->getResultMsg() );
604 /* Initialise each media subsession */
605 iter = new MediaSubsessionIterator( *p_sys->ms );
606 while( ( sub = iter->next() ) != NULL )
611 /* Value taken from mplayer */
612 if( !strcmp( sub->mediumName(), "audio" ) )
614 else if( !strcmp( sub->mediumName(), "video" ) )
618 if( i_client_port != -1 )
620 sub->setClientPortNum( i_client_port );
624 if( strcasestr( sub->codecName(), "REAL" ) )
626 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
627 p_sys->b_real = VLC_TRUE; /* This is a problem, we'll handle it later */
631 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
632 bInit = sub->initiate( 4 ); /* Constant ? */
634 bInit = sub->initiate();
638 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
639 sub->mediumName(), sub->codecName(),
640 p_sys->env->getResultMsg() );
644 if( sub->rtpSource() != NULL )
646 int fd = sub->rtpSource()->RTPgs()->socketNum();
648 /* Increase the buffer size */
650 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
652 /* Increase the RTP reorder timebuffer just a bit */
653 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
655 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
658 /* Issue the SETUP */
661 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
662 b_rtsp_tcp ? True : False ) ) )
664 /* if we get an unsupported transport error, toggle TCP use and try again */
665 if( !strstr(p_sys->env->getResultMsg(), "461 Unsupported Transport")
666 || !( p_sys->rtsp->setupMediaSubsession( *sub, False,
667 b_rtsp_tcp ? False : True ) ) )
669 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
670 sub->codecName(), p_sys->env->getResultMsg() );
676 /* Check if we will receive data from this subsession for this track */
677 if( sub->readSource() == NULL ) continue;
678 if( !p_sys->b_multicast )
680 /* Check, because we need diff. rollover behaviour for multicast */
681 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
684 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
685 tk->p_demux = p_demux;
688 tk->b_quicktime = VLC_FALSE;
689 tk->b_asf = VLC_FALSE;
690 tk->b_muxed = VLC_FALSE;
691 tk->p_out_muxed = NULL;
693 tk->b_rtcp_sync = VLC_FALSE;
695 tk->i_buffer = 65536;
696 tk->p_buffer = (uint8_t *)malloc( 65536 );
698 /* Value taken from mplayer */
699 if( !strcmp( sub->mediumName(), "audio" ) )
701 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
702 tk->fmt.audio.i_channels = sub->numChannels();
703 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
705 if( !strcmp( sub->codecName(), "MPA" ) ||
706 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
707 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
709 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
710 tk->fmt.audio.i_rate = 0;
712 else if( !strcmp( sub->codecName(), "AC3" ) )
714 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
715 tk->fmt.audio.i_rate = 0;
717 else if( !strcmp( sub->codecName(), "L16" ) )
719 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
720 tk->fmt.audio.i_bitspersample = 16;
722 else if( !strcmp( sub->codecName(), "L8" ) )
724 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
725 tk->fmt.audio.i_bitspersample = 8;
727 else if( !strcmp( sub->codecName(), "PCMU" ) )
729 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
731 else if( !strcmp( sub->codecName(), "PCMA" ) )
733 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
735 else if( !strncmp( sub->codecName(), "G726", 4 ) )
737 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
738 tk->fmt.audio.i_rate = 8000;
739 tk->fmt.audio.i_channels = 1;
740 if( !strcmp( sub->codecName()+5, "40" ) )
741 tk->fmt.i_bitrate = 40000;
742 else if( !strcmp( sub->codecName()+5, "32" ) )
743 tk->fmt.i_bitrate = 32000;
744 else if( !strcmp( sub->codecName()+5, "24" ) )
745 tk->fmt.i_bitrate = 24000;
746 else if( !strcmp( sub->codecName()+5, "16" ) )
747 tk->fmt.i_bitrate = 16000;
749 else if( !strcmp( sub->codecName(), "AMR" ) )
751 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
753 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
755 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
757 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
759 unsigned int i_extra;
762 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
764 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
767 tk->fmt.i_extra = i_extra;
768 tk->fmt.p_extra = malloc( i_extra );
769 memcpy( tk->fmt.p_extra, p_extra, i_extra );
772 /* Because the "faad" decoder does not handle the LATM data length field
773 at the start of each returned LATM frame, tell the RTP source to omit it. */
774 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
776 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
778 unsigned int i_extra;
781 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
783 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
786 tk->fmt.i_extra = i_extra;
787 tk->fmt.p_extra = malloc( i_extra );
788 memcpy( tk->fmt.p_extra, p_extra, i_extra );
792 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
794 tk->b_asf = VLC_TRUE;
795 if( p_sys->p_out_asf == NULL )
796 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
799 else if( !strcmp( sub->codecName(), "X-QT" ) ||
800 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
802 tk->b_quicktime = VLC_TRUE;
804 else if( !strcmp( sub->codecName(), "SPEEX" ) )
806 tk->fmt.i_codec = VLC_FOURCC( 's', 'p', 'x', 'r' );
807 if ( sub->rtpTimestampFrequency() )
808 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
811 msg_Warn( p_demux,"Using 8kHz as default sample rate." );
812 tk->fmt.audio.i_rate = 8000;
816 else if( !strcmp( sub->mediumName(), "video" ) )
818 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
819 if( !strcmp( sub->codecName(), "MPV" ) )
821 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
823 else if( !strcmp( sub->codecName(), "H263" ) ||
824 !strcmp( sub->codecName(), "H263-1998" ) ||
825 !strcmp( sub->codecName(), "H263-2000" ) )
827 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
829 else if( !strcmp( sub->codecName(), "H261" ) )
831 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
833 else if( !strcmp( sub->codecName(), "H264" ) )
835 unsigned int i_extra = 0;
836 uint8_t *p_extra = NULL;
838 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
839 tk->fmt.b_packetized = VLC_FALSE;
841 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
844 tk->fmt.i_extra = i_extra;
845 tk->fmt.p_extra = malloc( i_extra );
846 memcpy( tk->fmt.p_extra, p_extra, i_extra );
851 else if( !strcmp( sub->codecName(), "JPEG" ) )
853 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
855 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
857 unsigned int i_extra;
860 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
862 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
865 tk->fmt.i_extra = i_extra;
866 tk->fmt.p_extra = malloc( i_extra );
867 memcpy( tk->fmt.p_extra, p_extra, i_extra );
871 else if( !strcmp( sub->codecName(), "X-QT" ) ||
872 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
873 !strcmp( sub->codecName(), "X-QDM" ) ||
874 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
875 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
877 tk->b_quicktime = VLC_TRUE;
879 else if( !strcmp( sub->codecName(), "MP2T" ) )
881 tk->b_muxed = VLC_TRUE;
882 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
884 else if( !strcmp( sub->codecName(), "MP2P" ) ||
885 !strcmp( sub->codecName(), "MP1S" ) )
887 tk->b_muxed = VLC_TRUE;
888 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
891 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
893 tk->b_asf = VLC_TRUE;
894 if( p_sys->p_out_asf == NULL )
895 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
900 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
902 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
905 if( sub->rtcpInstance() != NULL )
907 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
910 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
913 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
914 p_sys->track[p_sys->i_track++] = tk;
919 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
925 if( p_sys->i_track <= 0 ) i_return = VLC_EGENERIC;
927 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
928 /* Retrieve the starttime if possible */
929 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
931 p_sys->i_npt_start = (int64_t) -1;
933 if( p_sys->i_npt_start < 0 )
934 p_sys->i_npt_start = -1;
936 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
937 /* Retrieve the duration if possible */
938 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
940 p_sys->i_npt_length = (int64_t) -1;
942 if( p_sys->i_npt_length < 0 )
943 p_sys->i_npt_length = -1;
945 msg_Dbg( p_demux, "setup start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
949 /*****************************************************************************
950 * Play: starts the actual playback of the stream
951 *****************************************************************************/
952 static int Play( demux_t *p_demux )
954 demux_sys_t *p_sys = p_demux->p_sys;
960 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, p_sys->i_npt_start, -1, 1 ) )
962 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
966 /* Retrieve the timeout value and set up a timeout prevention thread */
967 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
968 if( p_sys->i_timeout <= 0 )
969 p_sys->i_timeout = 60; /* default value from RFC2326 */
970 if( !p_sys->p_timeout )
972 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
973 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
974 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
975 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
976 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
978 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
979 vlc_object_destroy( p_sys->p_timeout );
981 msg_Dbg( p_demux, "spawned timeout thread" );
982 vlc_object_attach( p_sys->p_timeout, p_demux );
987 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
988 for( i = 0; i < p_sys->i_track; i++ )
990 if( !p_sys->track[i]->b_rtcp_sync )
991 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
992 p_sys->track[i]->i_start_seq = (int)p_sys->track[i]->sub->rtpInfo.seqNum;
993 msg_Info( p_demux, "set startseq: %u", p_sys->track[i]->i_start_seq );
997 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
998 /* Retrieve the starttime if possible */
999 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1001 p_sys->i_npt_start = -1;
1003 if( p_sys->i_npt_start < 0 )
1005 p_sys->i_npt_start = -1;
1009 p_sys->i_npt = p_sys->i_npt_start;
1011 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1012 /* Retrieve the duration if possible */
1013 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1015 p_sys->i_npt_length = -1;
1017 if( p_sys->i_npt_length < 0 )
1018 p_sys->i_npt_length = -1;
1020 msg_Dbg( p_demux, "play start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1025 /*****************************************************************************
1027 *****************************************************************************/
1028 static int Demux( demux_t *p_demux )
1030 demux_sys_t *p_sys = p_demux->p_sys;
1033 vlc_bool_t b_send_pcr = VLC_TRUE;
1037 /* Check if we need to send the server a Keep-A-Live signal */
1038 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
1040 p_sys->rtsp->setMediaSessionParameter( *p_sys->ms, "vlc" , "1" );
1041 p_sys->b_timeout_call = VLC_FALSE;
1045 for( i = 0; i < p_sys->i_track; i++ )
1047 live_track_t *tk = p_sys->track[i];
1049 if( tk->b_asf || tk->b_muxed )
1050 b_send_pcr = VLC_FALSE;
1056 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
1062 if( p_sys->i_pcr > 0 )
1065 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1068 /* First warn we want to read data */
1070 for( i = 0; i < p_sys->i_track; i++ )
1072 live_track_t *tk = p_sys->track[i];
1074 if( tk->waiting == 0 )
1077 tk->sub->readSource()->getNextFrame( tk->p_buffer, tk->i_buffer,
1078 StreamRead, tk, StreamClose, tk );
1081 /* Create a task that will be called if we wait more than 300ms */
1082 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1085 p_sys->scheduler->doEventLoop( &p_sys->event );
1087 /* remove the task */
1088 p_sys->scheduler->unscheduleDelayedTask( task );
1090 /* Check for gap in pts value */
1091 for( i = 0; i < p_sys->i_track; i++ )
1093 live_track_t *tk = p_sys->track[i];
1095 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1096 tk->sub->rtpSource() && tk->sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1098 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1100 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1101 tk->b_rtcp_sync = VLC_TRUE;
1109 if( p_sys->b_multicast && p_sys->b_no_data &&
1110 ( p_sys->i_no_data_ti > 120 ) )
1112 /* FIXME Make this configurable
1113 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1117 else if( !p_sys->b_multicast && p_sys->b_no_data &&
1118 ( p_sys->i_no_data_ti > 34 ) )
1120 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" ) ||
1121 var_GetBool( p_demux, "rtsp-http" );
1123 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1125 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1126 if( RollOverTcp( p_demux ) )
1128 msg_Err( p_demux, "TCP rollover failed, aborting" );
1132 else if( p_sys->i_no_data_ti > 34 )
1134 msg_Err( p_demux, "no data received in 10s, aborting" );
1138 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1141 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1144 return p_demux->b_error ? 0 : 1;
1147 /*****************************************************************************
1149 *****************************************************************************/
1150 static int Control( demux_t *p_demux, int i_query, va_list args )
1152 demux_sys_t *p_sys = p_demux->p_sys;
1155 vlc_bool_t *pb, b_bool;
1159 case DEMUX_GET_TIME:
1160 pi64 = (int64_t*)va_arg( args, int64_t * );
1161 if( p_sys->i_npt > 0 )
1163 *pi64 = p_sys->i_npt;
1166 return VLC_EGENERIC;
1168 case DEMUX_GET_LENGTH:
1169 pi64 = (int64_t*)va_arg( args, int64_t * );
1170 if( p_sys->i_npt_length > 0 )
1172 *pi64 = p_sys->i_npt_length;
1175 return VLC_EGENERIC;
1177 case DEMUX_GET_POSITION:
1178 pf = (double*)va_arg( args, double* );
1179 if( p_sys->i_npt_length > 0 && p_sys->i_npt > 0 )
1181 *pf = (double)p_sys->i_npt / (double)p_sys->i_npt_length;
1184 return VLC_EGENERIC;
1186 case DEMUX_SET_POSITION:
1190 f = (double)va_arg( args, double );
1191 if( p_sys->rtsp && p_sys->i_npt_length > 0 )
1194 time = f * (double)p_sys->i_npt_length / (double)1000000.0; /* in second */
1195 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time, -1, 1 ) )
1197 msg_Err( p_demux, "PLAY failed %s",
1198 p_sys->env->getResultMsg() );
1199 return VLC_EGENERIC;
1201 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1203 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1204 /* Retrieve RTP-Info values */
1205 for( i = 0; i < p_sys->i_track; i++ )
1207 //if( !p_sys->track[i]->b_rtcp_sync )
1208 p_sys->track[i]->b_rtcp_sync = VLC_FALSE;
1209 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1210 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1211 msg_Info( p_demux, "set pos startseq: %u", p_sys->track[i]->i_start_seq );
1214 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1215 /* Retrieve the starttime if possible */
1216 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1218 p_sys->i_npt_start = -1;
1220 if( p_sys->i_npt_start < 0 )
1222 p_sys->i_npt_start = -1;
1226 p_sys->i_npt = p_sys->i_npt_start;
1228 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1229 /* Retrieve the duration if possible */
1230 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1232 p_sys->i_npt_length = -1;
1234 if( p_sys->i_npt_length < 0 )
1235 p_sys->i_npt_length = -1;
1237 msg_Dbg( p_demux, "seek start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1240 return VLC_EGENERIC;
1242 /* Special for access_demux */
1243 case DEMUX_CAN_PAUSE:
1244 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1245 if( p_sys->rtsp && p_sys->i_npt_length )
1246 /* Not always true, but will be handled in SET_PAUSE_STATE */
1252 case DEMUX_CAN_CONTROL_PACE:
1253 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1255 #if 1 /* Disable for now until we have a clock synchro algo
1256 * which works with something else than MPEG over UDP */
1263 case DEMUX_SET_PAUSE_STATE:
1265 double d_npt = (double) p_sys->i_npt / I64C(1000000);
1268 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1269 if( p_sys->rtsp == NULL )
1270 return VLC_EGENERIC;
1273 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1274 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1275 d_npt > 0 ? d_npt : -1 ) ) )
1277 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1278 return VLC_EGENERIC;
1281 /* When we Pause, we'll need the TimeoutPrevention thread to
1282 * handle sending the "Keep Alive" message to the server.
1283 * Unfortunately Live555 isn't thread safe and so can't
1284 * do this normally while the main Demux thread is handling
1285 * a live stream. We end up with the Timeout thread blocking
1286 * waiting for a response from the server. So when we PAUSE
1287 * we set a flag that the TimeoutPrevention function will check
1288 * and if it's set, it will trigger the GET_PARAMETER message */
1289 if( b_bool && p_sys->p_timeout != NULL )
1290 p_sys->p_timeout->b_handle_keep_alive = VLC_TRUE;
1291 else if( !b_bool && p_sys->p_timeout != NULL )
1292 p_sys->p_timeout->b_handle_keep_alive = VLC_FALSE;
1294 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1296 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1297 for( i = 0; i < p_sys->i_track; i++ )
1299 if( !p_sys->track[i]->b_rtcp_sync )
1300 p_sys->track[i]->i_pts = 0; // (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1301 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1302 msg_Info( p_demux, "set pause startseq: %u", p_sys->track[i]->i_start_seq );
1305 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1306 /* Retrieve the starttime if possible */
1307 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1309 p_sys->i_npt_start = -1;
1311 if( p_sys->i_npt_start < 0 )
1313 p_sys->i_npt_start = -1;
1317 p_sys->i_npt = p_sys->i_npt_start;
1319 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1320 /* Retrieve the duration if possible */
1321 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1323 p_sys->i_npt_length = -1;
1325 if( p_sys->i_npt_length < 0 )
1326 p_sys->i_npt_length = -1;
1328 msg_Dbg( p_demux, "pause start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1331 case DEMUX_GET_TITLE_INFO:
1332 case DEMUX_SET_TITLE:
1333 case DEMUX_SET_SEEKPOINT:
1334 return VLC_EGENERIC;
1336 case DEMUX_GET_PTS_DELAY:
1337 pi64 = (int64_t*)va_arg( args, int64_t * );
1338 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1342 return VLC_EGENERIC;
1346 /*****************************************************************************
1347 * RollOverTcp: reopen the rtsp into TCP mode
1348 * XXX: ugly, a lot of code are duplicated from Open()
1349 * This should REALLY be fixed
1350 *****************************************************************************/
1351 static int RollOverTcp( demux_t *p_demux )
1353 demux_sys_t *p_sys = p_demux->p_sys;
1356 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1358 /* We close the old RTSP session */
1359 for( i = 0; i < p_sys->i_track; i++ )
1361 live_track_t *tk = p_sys->track[i];
1363 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
1364 free( tk->p_buffer );
1367 if( p_sys->i_track ) free( p_sys->track );
1368 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
1370 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1371 Medium::close( p_sys->ms );
1372 RTSPClient::close( p_sys->rtsp );
1376 p_sys->track = NULL;
1379 /* Reopen rtsp client */
1380 if( ( p_demux->s != NULL ) &&
1381 ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1383 msg_Err( p_demux, "Failed to connect with rtsp://%s",
1388 if( p_sys->p_sdp == NULL )
1390 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1394 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1396 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1400 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1406 return VLC_EGENERIC;
1410 /*****************************************************************************
1412 *****************************************************************************/
1413 static void StreamRead( void *p_private, unsigned int i_size,
1414 unsigned int i_truncated_bytes, struct timeval pts,
1415 unsigned int duration )
1417 live_track_t *tk = (live_track_t*)p_private;
1418 demux_t *p_demux = tk->p_demux;
1419 demux_sys_t *p_sys = p_demux->p_sys;
1422 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1424 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1425 (uint64_t)pts.tv_usec;
1427 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1428 i_pts &= UI64C(0x00ffffffffffffff);
1430 if( tk->b_quicktime && tk->p_es == NULL )
1432 QuickTimeGenericRTPSource *qtRTPSource =
1433 (QuickTimeGenericRTPSource*)tk->sub->rtpSource();
1434 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1435 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1437 if( tk->fmt.i_cat == VIDEO_ES ) {
1438 if( qtState.sdAtomSize < 16 + 32 )
1441 p_sys->event = 0xff;
1445 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1446 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1447 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1449 if( tk->fmt.i_codec == VLC_FOURCC('a', 'v', 'c', '1') )
1451 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
1452 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
1453 + qtRTPSource->qtState.sdAtomSize;
1454 while (pos+8 < endpos) {
1455 unsigned atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
1456 if( atomLength == 0 || atomLength > endpos-pos) break;
1457 if( memcmp(pos+4, "avcC", 4) == 0 &&
1459 atomLength <= INT_MAX )
1461 tk->fmt.i_extra = atomLength-8;
1462 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1463 memcpy(tk->fmt.p_extra, pos+8, atomLength-8);
1471 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1472 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1473 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1477 if( qtState.sdAtomSize < 4 )
1480 p_sys->event = 0xff;
1484 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1486 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1490 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1492 pts.tv_sec * 1000000LL + pts.tv_usec );
1495 /* grow buffer if it looks like buffer is too small, but don't eat
1496 * up all the memory on strange streams */
1497 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1500 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1501 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1503 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1506 msg_Warn( p_demux, "realloc failed" );
1510 tk->p_buffer = (uint8_t*)p_tmp;
1513 if( i_size > tk->i_buffer )
1515 msg_Warn( p_demux, "buffer overflow" );
1517 /* FIXME could i_size be > buffer size ? */
1518 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1519 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1521 AMRAudioSource *amrSource = (AMRAudioSource*)tk->sub->readSource();
1523 p_block = block_New( p_demux, i_size + 1 );
1524 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1525 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1527 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1529 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1530 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->sub->rtpSource();
1531 uint32_t header = h261Source->lastSpecialHeader();
1533 uint32_t header = 0;
1534 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1536 p_block = block_New( p_demux, i_size + 4 );
1537 memcpy( p_block->p_buffer, &header, 4 );
1538 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1540 if( tk->sub->rtpSource()->curPacketMarkerBit() )
1541 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1543 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1545 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1546 msg_Warn( p_demux, "unsupported NAL type for H264" );
1548 /* Normal NAL type */
1549 p_block = block_New( p_demux, i_size + 4 );
1550 p_block->p_buffer[0] = 0x00;
1551 p_block->p_buffer[1] = 0x00;
1552 p_block->p_buffer[2] = 0x00;
1553 p_block->p_buffer[3] = 0x01;
1554 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1556 else if( tk->b_asf )
1558 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1559 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1561 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1565 p_block = block_New( p_demux, i_size );
1566 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1570 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1571 if( (tk->fmt.i_cat == VIDEO_ES) && (p_sys->i_pcr < i_pts) &&
1572 (i_pts > 0) && (p_sys->i_pcr > 0) )
1574 p_sys->i_npt += __MAX( 0, i_pts - p_sys->i_pcr );
1575 p_sys->i_pcr = i_pts;
1576 //msg_Dbg( p_demux, "npt update" );
1578 else if( /*tk->fmt.i_cat == VIDEO_ES &&*/ p_sys->i_pcr < i_pts )
1580 p_sys->i_pcr = i_pts;
1582 //msg_Dbg( p_demux, "npt %lld", p_sys->i_npt );
1584 if( (i_pts != tk->i_pts) && (!tk->b_muxed) )
1586 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1587 p_block->i_pts = i_pts;
1592 stream_DemuxSend( tk->p_out_muxed, p_block );
1594 else if( tk->b_asf )
1596 stream_DemuxSend( p_sys->p_out_asf, p_block );
1600 es_out_Send( p_demux->out, tk->p_es, p_block );
1603 /* warn that's ok */
1604 p_sys->event = 0xff;
1606 /* we have read data */
1608 p_demux->p_sys->b_no_data = VLC_FALSE;
1609 p_demux->p_sys->i_no_data_ti = 0;
1611 if( i_pts > 0 && !tk->b_muxed )
1617 /*****************************************************************************
1619 *****************************************************************************/
1620 static void StreamClose( void *p_private )
1622 live_track_t *tk = (live_track_t*)p_private;
1623 demux_t *p_demux = tk->p_demux;
1624 demux_sys_t *p_sys = p_demux->p_sys;
1626 msg_Dbg( p_demux, "StreamClose" );
1628 p_sys->event = 0xff;
1629 p_demux->b_error = VLC_TRUE;
1633 /*****************************************************************************
1635 *****************************************************************************/
1636 static void TaskInterrupt( void *p_private )
1638 demux_t *p_demux = (demux_t*)p_private;
1640 p_demux->p_sys->i_no_data_ti++;
1643 p_demux->p_sys->event = 0xff;
1646 /*****************************************************************************
1648 *****************************************************************************/
1649 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1651 p_timeout->b_die = VLC_FALSE;
1652 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1653 p_timeout->i_remain *= 1000000;
1655 vlc_thread_ready( p_timeout );
1658 while( !p_timeout->b_die )
1660 if( p_timeout->i_remain <= 0 )
1662 char *psz_bye = NULL;
1663 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1664 p_timeout->i_remain *= 1000000;
1665 msg_Dbg( p_timeout, "reset the timeout timer" );
1666 if( p_timeout->b_handle_keep_alive == VLC_TRUE )
1668 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
1669 p_timeout->p_sys->rtsp->getMediaSessionParameter( *p_timeout->p_sys->ms, NULL, psz_bye );
1671 p_timeout->p_sys->b_timeout_call = VLC_FALSE;
1675 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1678 p_timeout->i_remain -= 200000;
1679 msleep( 200000 ); /* 200 ms */
1683 /*****************************************************************************
1685 *****************************************************************************/
1686 static int b64_decode( char *dest, char *src );
1688 static int ParseASF( demux_t *p_demux )
1690 demux_sys_t *p_sys = p_demux->p_sys;
1692 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1693 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1697 /* Parse the asf header */
1698 if( psz_asf == NULL )
1699 return VLC_EGENERIC;
1701 psz_asf += strlen( psz_marker );
1702 psz_asf = strdup( psz_asf ); /* Duplicate it */
1703 psz_end = strchr( psz_asf, '\n' );
1705 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1708 if( psz_asf >= psz_end )
1711 return VLC_EGENERIC;
1714 /* Always smaller */
1715 p_header = block_New( p_demux, psz_end - psz_asf );
1716 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1717 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1718 if( p_header->i_buffer <= 0 )
1721 return VLC_EGENERIC;
1724 /* Parse it to get packet size */
1725 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1727 /* Send it to demuxer */
1728 stream_DemuxSend( p_sys->p_out_asf, p_header );
1735 static unsigned char* parseH264ConfigStr( char const* configStr,
1736 unsigned int& configSize )
1739 int i, i_records = 1;
1744 if( configStr == NULL || *configStr == '\0' )
1747 psz = dup = strdup( configStr );
1749 /* Count the number of comma's */
1750 for( psz = dup; *psz != '\0'; ++psz )
1759 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1761 for( i = 0; i < i_records; i++ )
1763 cfg[configSize++] = 0x00;
1764 cfg[configSize++] = 0x00;
1765 cfg[configSize++] = 0x00;
1766 cfg[configSize++] = 0x01;
1768 configSize += b64_decode( (char*)&cfg[configSize], psz );
1769 psz += strlen(psz)+1;
1772 if( dup ) free( dup );
1776 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1777 static int b64_decode( char *dest, char *src )
1779 const char *dest_start = dest;
1783 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1784 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1785 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1786 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1787 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1788 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1789 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1790 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1791 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1792 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1793 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1794 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1795 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1796 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1797 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1798 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1801 for( i_level = 0; *src != '\0'; src++ )
1805 c = b64[(unsigned int)*src];
1817 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1821 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1825 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1833 return dest - dest_start;