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
154 char *p_sdp; /* XXX mallocated */
155 char *psz_path; /* URL-encoded path */
159 TaskScheduler *scheduler;
160 UsageEnvironment *env ;
165 live_track_t **track; /* XXX mallocated */
173 int64_t i_pcr; /* The clock */
174 int64_t i_npt; /* The current time in the stream */
175 int64_t i_npt_length;
178 /* timeout thread information */
179 int i_timeout; /* session timeout value in seconds */
180 vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
181 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
184 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
185 vlc_bool_t b_no_data; /* true if we never receive any data */
186 int i_no_data_ti; /* consecutive number of TaskInterrupt */
191 static int Demux ( demux_t * );
192 static int Control( demux_t *, int, va_list );
194 static int Connect ( demux_t * );
195 static int SessionsSetup( demux_t * );
196 static int Play ( demux_t *);
197 static int ParseASF ( demux_t * );
198 static int RollOverTcp ( demux_t * );
200 static void StreamRead ( void *, unsigned int, unsigned int,
201 struct timeval, unsigned int );
202 static void StreamClose ( void * );
203 static void TaskInterrupt( void * );
205 static void TimeoutPrevention( timeout_thread_t * );
207 static unsigned char* parseH264ConfigStr( char const* configStr,
208 unsigned int& configSize );
210 /*****************************************************************************
212 *****************************************************************************/
213 static int Open ( vlc_object_t *p_this )
215 demux_t *p_demux = (demux_t*)p_this;
216 demux_sys_t *p_sys = NULL;
218 MediaSubsessionIterator *iter = NULL;
219 MediaSubsession *sub = NULL;
224 /* See if it looks like a SDP
225 v, o, s fields are mandatory and in this order */
226 const uint8_t *p_peek;
227 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
229 if( memcmp( p_peek, "v=0\r\n", 5 ) &&
230 memcmp( p_peek, "v=0\n", 4 ) &&
231 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
238 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
241 p_demux->pf_demux = Demux;
242 p_demux->pf_control= Control;
243 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
244 if( !p_sys ) return VLC_ENOMEM;
247 p_sys->scheduler = NULL;
255 p_sys->i_npt_start = 0;
256 p_sys->i_npt_length = 0;
257 p_sys->p_out_asf = NULL;
258 p_sys->b_no_data = VLC_TRUE;
259 p_sys->i_no_data_ti = 0;
260 p_sys->p_timeout = NULL;
261 p_sys->i_timeout = 0;
262 p_sys->b_timeout_call = VLC_FALSE;
263 p_sys->b_multicast = VLC_FALSE;
264 p_sys->b_real = VLC_FALSE;
265 p_sys->psz_path = strdup( p_demux->psz_path );
267 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
268 vlc_UrlParse( &p_sys->url, p_sys->psz_path, 0 );
270 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
272 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
275 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
277 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
281 if( strcasecmp( p_demux->psz_access, "sdp" ) )
283 char *p = p_sys->psz_path;
284 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
287 if( p_demux->s != NULL )
289 /* Gather the complete sdp file */
291 int i_sdp_max = 1000;
292 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
296 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
297 i_sdp_max - i_sdp - 1 );
301 msg_Err( p_demux, "failed to read SDP" );
308 if( i_read < i_sdp_max - i_sdp - 1 )
315 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
317 p_sys->p_sdp = (char*)p_sdp;
319 else if( ( p_demux->s == NULL ) &&
320 !strcasecmp( p_demux->psz_access, "sdp" ) )
322 /* sdp:// link from SAP */
323 p_sys->p_sdp = strdup( p_sys->psz_path );
325 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
327 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
331 if( p_sys->p_sdp == NULL )
333 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
337 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
339 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
343 if( p_sys->b_real ) goto error;
345 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
348 if( p_sys->p_out_asf && ParseASF( p_demux ) )
350 msg_Err( p_demux, "cannot find a usable asf header" );
351 /* TODO Clean tracks */
355 if( p_sys->i_track <= 0 )
361 for( i = 0; i < p_sys->i_track; i++ )
363 live_track_t *tk = p_sys->track[i];
365 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
366 free( tk->p_buffer );
370 if( p_sys->i_track ) free( p_sys->track );
371 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
372 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
373 if( p_sys->ms ) Medium::close( p_sys->ms );
374 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
375 if( p_sys->env ) p_sys->env->reclaim();
376 if( p_sys->p_timeout )
378 vlc_object_kill( p_sys->p_timeout );
379 vlc_thread_join( p_sys->p_timeout );
380 vlc_object_detach( p_sys->p_timeout );
381 vlc_object_destroy( p_sys->p_timeout );
383 if( p_sys->scheduler ) delete p_sys->scheduler;
384 if( p_sys->p_sdp ) free( p_sys->p_sdp );
385 if( p_sys->psz_path ) free( p_sys->psz_path );
387 vlc_UrlClean( &p_sys->url );
393 /*****************************************************************************
395 *****************************************************************************/
396 static void Close( vlc_object_t *p_this )
398 demux_t *p_demux = (demux_t*)p_this;
399 demux_sys_t *p_sys = p_demux->p_sys;
402 for( i = 0; i < p_sys->i_track; i++ )
404 live_track_t *tk = p_sys->track[i];
406 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
407 free( tk->p_buffer );
411 if( p_sys->i_track ) free( p_sys->track );
412 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
413 if( p_sys->rtsp && p_sys->ms ) p_sys->rtsp->teardownMediaSession( *p_sys->ms );
414 if( p_sys->ms ) Medium::close( p_sys->ms );
415 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
416 if( p_sys->env ) p_sys->env->reclaim();
417 if( p_sys->p_timeout )
419 vlc_object_kill( p_sys->p_timeout );
420 vlc_thread_join( p_sys->p_timeout );
421 vlc_object_detach( p_sys->p_timeout );
422 vlc_object_destroy( p_sys->p_timeout );
424 if( p_sys->scheduler ) delete p_sys->scheduler;
425 if( p_sys->p_sdp ) free( p_sys->p_sdp );
426 if( p_sys->psz_path ) free( p_sys->psz_path );
428 vlc_UrlClean( &p_sys->url );
433 /*****************************************************************************
434 * Connect: connects to the RTSP server to setup the session DESCRIBE
435 *****************************************************************************/
436 static int Connect( demux_t *p_demux )
438 demux_sys_t *p_sys = p_demux->p_sys;
439 Authenticator authenticator;
441 char *psz_user = NULL;
442 char *psz_pwd = NULL;
443 char *psz_url = NULL;
444 char *psz_options = NULL;
447 int i_ret = VLC_SUCCESS;
450 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
451 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
453 if( ( p_sys->rtsp = RTSPClient::createNew( *p_sys->env,
454 p_demux->p_libvlc->i_verbose > 1,
455 "VLC media player", i_http_port ) ) == NULL )
457 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
458 p_sys->env->getResultMsg() );
462 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
463 if( !psz_url ) return VLC_ENOMEM;
465 if( p_sys->url.psz_username || p_sys->url.psz_password )
467 sprintf( psz_url, "rtsp://%s%s", p_sys->url.psz_host,
468 p_sys->url.psz_path );
470 psz_user = strdup( p_sys->url.psz_username );
471 psz_pwd = strdup( p_sys->url.psz_password );
475 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
477 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
478 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
482 msg_Info( p_demux, "[%s] user=%s pwd=%s", psz_url, psz_user, psz_pwd );
483 authenticator.setUsernameAndPassword( (const char*)psz_user,
484 (const char*)psz_pwd );
486 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url, psz_user, psz_pwd,
488 if( psz_options ) delete [] psz_options;
490 p_sdp = p_sys->rtsp->describeURL( psz_url,
491 &authenticator, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
493 if( psz_user ) free( psz_user );
494 if( psz_pwd ) free( psz_pwd );
498 /* failure occurred */
500 const char *psz_error = p_sys->env->getResultMsg();
502 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
503 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
504 sscanf( psz_error, "%*s %*s HTTP GET %*s HTTP/%*u.%*u %3u %*s", &i_code );
505 else sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
509 char *psz_login = NULL; char *psz_password = NULL;
510 msg_Dbg( p_demux, "authentication failed" );
512 i_ret = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
513 _("Please enter a valid login name and a password."),
514 &psz_login, &psz_password );
515 if( i_ret == DIALOG_OK_YES )
517 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
518 psz_login, psz_password );
519 if( psz_login ) psz_user = psz_login;
520 if( psz_password ) psz_pwd = psz_password;
524 if( psz_login ) free( psz_login );
525 if( psz_password ) free( psz_password );
527 else if( !var_GetBool( p_demux, "rtsp-http" ) )
529 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
531 val.b_bool = VLC_TRUE;
532 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
533 var_Set( p_demux, "rtsp-http", val );
534 if( psz_url ) free( psz_url );
535 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
538 i_ret = VLC_EGENERIC;
540 if( psz_url ) free( psz_url );
542 /* malloc-ated copy */
543 if( p_sys->p_sdp ) free( p_sys->p_sdp );
545 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
551 /*****************************************************************************
552 * SessionsSetup: prepares the subsessions and does the SETUP
553 *****************************************************************************/
554 static int SessionsSetup( demux_t *p_demux )
556 demux_sys_t *p_sys = p_demux->p_sys;
557 MediaSubsessionIterator *iter = NULL;
558 MediaSubsession *sub = NULL;
560 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
562 int i_return = VLC_SUCCESS;
563 unsigned int i_buffer = 0;
564 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
566 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" ) ||
567 var_GetBool( p_demux, "rtsp-http" );
568 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
570 /* Create the session from the SDP */
571 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
573 msg_Err( p_demux, "Could not create the RTSP Session: %s",
574 p_sys->env->getResultMsg() );
578 /* Initialise each media subsession */
579 iter = new MediaSubsessionIterator( *p_sys->ms );
580 while( ( sub = iter->next() ) != NULL )
585 /* Value taken from mplayer */
586 if( !strcmp( sub->mediumName(), "audio" ) )
588 else if( !strcmp( sub->mediumName(), "video" ) )
592 if( i_client_port != -1 )
594 sub->setClientPortNum( i_client_port );
598 if( strcasestr( sub->codecName(), "REAL" ) )
600 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
601 p_sys->b_real = VLC_TRUE; /* This is a problem, we'll handle it later */
605 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
606 bInit = sub->initiate( 4 ); /* Constant ? */
608 bInit = sub->initiate();
612 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
613 sub->mediumName(), sub->codecName(),
614 p_sys->env->getResultMsg() );
618 if( sub->rtpSource() != NULL )
620 int fd = sub->rtpSource()->RTPgs()->socketNum();
622 /* Increase the buffer size */
624 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
626 /* Increase the RTP reorder timebuffer just a bit */
627 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
629 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
632 /* Issue the SETUP */
635 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
636 b_rtsp_tcp ? True : False ) ) )
638 /* if we get an unsupported transport error, toggle TCP use and try again */
639 if( !strstr(p_sys->env->getResultMsg(), "461 Unsupported Transport")
640 || !( p_sys->rtsp->setupMediaSubsession( *sub, False,
641 b_rtsp_tcp ? False : True ) ) )
643 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
644 sub->codecName(), p_sys->env->getResultMsg() );
650 /* Check if we will receive data from this subsession for this track */
651 if( sub->readSource() == NULL ) continue;
652 if( !p_sys->b_multicast )
654 /* Check, because we need diff. rollover behaviour for multicast */
655 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
658 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
659 tk->p_demux = p_demux;
662 tk->b_quicktime = VLC_FALSE;
663 tk->b_asf = VLC_FALSE;
664 tk->b_muxed = VLC_FALSE;
665 tk->p_out_muxed = NULL;
667 tk->b_rtcp_sync = VLC_FALSE;
669 tk->i_buffer = 65536;
670 tk->p_buffer = (uint8_t *)malloc( 65536 );
672 /* Value taken from mplayer */
673 if( !strcmp( sub->mediumName(), "audio" ) )
675 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
676 tk->fmt.audio.i_channels = sub->numChannels();
677 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
679 if( !strcmp( sub->codecName(), "MPA" ) ||
680 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
681 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
683 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
684 tk->fmt.audio.i_rate = 0;
686 else if( !strcmp( sub->codecName(), "AC3" ) )
688 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
689 tk->fmt.audio.i_rate = 0;
691 else if( !strcmp( sub->codecName(), "L16" ) )
693 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
694 tk->fmt.audio.i_bitspersample = 16;
696 else if( !strcmp( sub->codecName(), "L8" ) )
698 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
699 tk->fmt.audio.i_bitspersample = 8;
701 else if( !strcmp( sub->codecName(), "PCMU" ) )
703 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
705 else if( !strcmp( sub->codecName(), "PCMA" ) )
707 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
709 else if( !strncmp( sub->codecName(), "G726", 4 ) )
711 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
712 tk->fmt.audio.i_rate = 8000;
713 tk->fmt.audio.i_channels = 1;
714 if( !strcmp( sub->codecName()+5, "40" ) )
715 tk->fmt.i_bitrate = 40000;
716 else if( !strcmp( sub->codecName()+5, "32" ) )
717 tk->fmt.i_bitrate = 32000;
718 else if( !strcmp( sub->codecName()+5, "24" ) )
719 tk->fmt.i_bitrate = 24000;
720 else if( !strcmp( sub->codecName()+5, "16" ) )
721 tk->fmt.i_bitrate = 16000;
723 else if( !strcmp( sub->codecName(), "AMR" ) )
725 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
727 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
729 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
731 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
733 unsigned int i_extra;
736 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
738 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
741 tk->fmt.i_extra = i_extra;
742 tk->fmt.p_extra = malloc( i_extra );
743 memcpy( tk->fmt.p_extra, p_extra, i_extra );
746 /* Because the "faad" decoder does not handle the LATM data length field
747 at the start of each returned LATM frame, tell the RTP source to omit it. */
748 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
750 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
752 unsigned int i_extra;
755 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
757 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
760 tk->fmt.i_extra = i_extra;
761 tk->fmt.p_extra = malloc( i_extra );
762 memcpy( tk->fmt.p_extra, p_extra, i_extra );
766 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
768 tk->b_asf = VLC_TRUE;
769 if( p_sys->p_out_asf == NULL )
770 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
773 else if( !strcmp( sub->codecName(), "X-QT" ) ||
774 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
776 tk->b_quicktime = VLC_TRUE;
778 else if( !strcmp( sub->codecName(), "SPEEX" ) )
780 tk->fmt.i_codec = VLC_FOURCC( 's', 'p', 'x', 'r' );
781 if ( sub->rtpTimestampFrequency() )
782 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
785 msg_Warn( p_demux,"Using 8kHz as default sample rate." );
786 tk->fmt.audio.i_rate = 8000;
790 else if( !strcmp( sub->mediumName(), "video" ) )
792 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
793 if( !strcmp( sub->codecName(), "MPV" ) )
795 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
797 else if( !strcmp( sub->codecName(), "H263" ) ||
798 !strcmp( sub->codecName(), "H263-1998" ) ||
799 !strcmp( sub->codecName(), "H263-2000" ) )
801 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
803 else if( !strcmp( sub->codecName(), "H261" ) )
805 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
807 else if( !strcmp( sub->codecName(), "H264" ) )
809 unsigned int i_extra = 0;
810 uint8_t *p_extra = NULL;
812 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
813 tk->fmt.b_packetized = VLC_FALSE;
815 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
818 tk->fmt.i_extra = i_extra;
819 tk->fmt.p_extra = malloc( i_extra );
820 memcpy( tk->fmt.p_extra, p_extra, i_extra );
825 else if( !strcmp( sub->codecName(), "JPEG" ) )
827 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
829 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
831 unsigned int i_extra;
834 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
836 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
839 tk->fmt.i_extra = i_extra;
840 tk->fmt.p_extra = malloc( i_extra );
841 memcpy( tk->fmt.p_extra, p_extra, i_extra );
845 else if( !strcmp( sub->codecName(), "X-QT" ) ||
846 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
847 !strcmp( sub->codecName(), "X-QDM" ) ||
848 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
849 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
851 tk->b_quicktime = VLC_TRUE;
853 else if( !strcmp( sub->codecName(), "MP2T" ) )
855 tk->b_muxed = VLC_TRUE;
856 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
858 else if( !strcmp( sub->codecName(), "MP2P" ) ||
859 !strcmp( sub->codecName(), "MP1S" ) )
861 tk->b_muxed = VLC_TRUE;
862 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
865 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
867 tk->b_asf = VLC_TRUE;
868 if( p_sys->p_out_asf == NULL )
869 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
874 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
876 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
879 if( sub->rtcpInstance() != NULL )
881 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
884 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
887 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
888 p_sys->track[p_sys->i_track++] = tk;
893 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
899 if( p_sys->i_track <= 0 ) i_return = VLC_EGENERIC;
901 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
902 /* Retrieve the starttime if possible */
903 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
905 p_sys->i_npt_start = (int64_t) -1;
907 if( p_sys->i_npt_start < 0 )
908 p_sys->i_npt_start = -1;
910 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
911 /* Retrieve the duration if possible */
912 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
914 p_sys->i_npt_length = (int64_t) -1;
916 if( p_sys->i_npt_length < 0 )
917 p_sys->i_npt_length = -1;
919 msg_Dbg( p_demux, "setup start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
923 /*****************************************************************************
924 * Play: starts the actual playback of the stream
925 *****************************************************************************/
926 static int Play( demux_t *p_demux )
928 demux_sys_t *p_sys = p_demux->p_sys;
934 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, p_sys->i_npt_start, -1, 1 ) )
936 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
940 /* Retrieve the timeout value and set up a timeout prevention thread */
941 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
942 if( p_sys->i_timeout <= 0 )
943 p_sys->i_timeout = 60; /* default value from RFC2326 */
944 if( !p_sys->p_timeout )
946 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
947 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
948 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
949 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
950 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
952 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
953 vlc_object_destroy( p_sys->p_timeout );
955 msg_Dbg( p_demux, "spawned timeout thread" );
956 vlc_object_attach( p_sys->p_timeout, p_demux );
961 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
963 for( i = 0; i < p_sys->i_track; i++ )
965 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
966 p_sys->track[i]->i_start_seq = (int)p_sys->track[i]->sub->rtpInfo.seqNum;
967 msg_Info( p_demux, "set startseq: %u", p_sys->track[i]->i_start_seq );
971 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
972 /* Retrieve the starttime if possible */
973 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
975 p_sys->i_npt_start = -1;
977 if( p_sys->i_npt_start < 0 )
979 p_sys->i_npt_start = -1;
983 p_sys->i_npt = p_sys->i_npt_start;
985 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
986 /* Retrieve the duration if possible */
987 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
989 p_sys->i_npt_length = -1;
991 if( p_sys->i_npt_length < 0 )
992 p_sys->i_npt_length = -1;
994 msg_Dbg( p_demux, "play start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
999 /*****************************************************************************
1001 *****************************************************************************/
1002 static int Demux( demux_t *p_demux )
1004 demux_sys_t *p_sys = p_demux->p_sys;
1007 vlc_bool_t b_send_pcr = VLC_TRUE;
1011 /* Check if we need to send the server a Keep-A-Live signal */
1012 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
1014 p_sys->rtsp->setMediaSessionParameter( *p_sys->ms, "vlc" , "1" );
1015 p_sys->b_timeout_call = VLC_FALSE;
1019 for( i = 0; i < p_sys->i_track; i++ )
1021 live_track_t *tk = p_sys->track[i];
1023 if( tk->b_asf || tk->b_muxed )
1024 b_send_pcr = VLC_FALSE;
1030 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
1036 if( p_sys->i_pcr > 0 )
1039 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1042 /* First warn we want to read data */
1044 for( i = 0; i < p_sys->i_track; i++ )
1046 live_track_t *tk = p_sys->track[i];
1048 if( tk->waiting == 0 )
1051 tk->sub->readSource()->getNextFrame( tk->p_buffer, tk->i_buffer,
1052 StreamRead, tk, StreamClose, tk );
1055 /* Create a task that will be called if we wait more than 300ms */
1056 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1059 p_sys->scheduler->doEventLoop( &p_sys->event );
1061 /* remove the task */
1062 p_sys->scheduler->unscheduleDelayedTask( task );
1064 /* Check for gap in pts value */
1065 for( i = 0; i < p_sys->i_track; i++ )
1067 live_track_t *tk = p_sys->track[i];
1069 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1070 tk->sub->rtpSource() && tk->sub->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1072 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1074 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1075 tk->b_rtcp_sync = VLC_TRUE;
1083 if( p_sys->b_multicast && p_sys->b_no_data &&
1084 ( p_sys->i_no_data_ti > 120 ) )
1086 /* FIXME Make this configurable
1087 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1091 else if( !p_sys->b_multicast && p_sys->b_no_data &&
1092 ( p_sys->i_no_data_ti > 34 ) )
1094 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" ) ||
1095 var_GetBool( p_demux, "rtsp-http" );
1097 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1099 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1100 if( RollOverTcp( p_demux ) )
1102 msg_Err( p_demux, "TCP rollover failed, aborting" );
1106 else if( p_sys->i_no_data_ti > 34 )
1108 msg_Err( p_demux, "no data received in 10s, aborting" );
1112 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1115 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1118 return p_demux->b_error ? 0 : 1;
1121 /*****************************************************************************
1123 *****************************************************************************/
1124 static int Control( demux_t *p_demux, int i_query, va_list args )
1126 demux_sys_t *p_sys = p_demux->p_sys;
1129 vlc_bool_t *pb, b_bool;
1133 case DEMUX_GET_TIME:
1134 pi64 = (int64_t*)va_arg( args, int64_t * );
1135 if( p_sys->i_npt > 0 )
1137 *pi64 = p_sys->i_npt;
1140 return VLC_EGENERIC;
1142 case DEMUX_GET_LENGTH:
1143 pi64 = (int64_t*)va_arg( args, int64_t * );
1144 if( p_sys->i_npt_length > 0 )
1146 *pi64 = p_sys->i_npt_length;
1149 return VLC_EGENERIC;
1151 case DEMUX_GET_POSITION:
1152 pf = (double*)va_arg( args, double* );
1153 if( p_sys->i_npt_length > 0 && p_sys->i_npt > 0 )
1155 *pf = (double)p_sys->i_npt / (double)p_sys->i_npt_length;
1158 return VLC_EGENERIC;
1160 case DEMUX_SET_POSITION:
1164 f = (double)va_arg( args, double );
1165 if( p_sys->rtsp && p_sys->i_npt_length > 0 )
1168 time = f * (double)p_sys->i_npt_length / 1000000.0; /* in second */
1169 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time, -1, 1 ) )
1171 msg_Err( p_demux, "PLAY failed %s",
1172 p_sys->env->getResultMsg() );
1173 return VLC_EGENERIC;
1175 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1177 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1178 /* Retrieve RTP-Info values */
1179 for( i = 0; i < p_sys->i_track; i++ )
1181 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1182 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1183 msg_Info( p_demux, "set pos startseq: %u", p_sys->track[i]->i_start_seq );
1186 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1187 /* Retrieve the starttime if possible */
1188 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1190 p_sys->i_npt_start = -1;
1192 if( p_sys->i_npt_start < 0 )
1194 p_sys->i_npt_start = -1;
1198 p_sys->i_npt = p_sys->i_npt_start;
1200 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1201 /* Retrieve the duration if possible */
1202 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1204 p_sys->i_npt_length = -1;
1206 if( p_sys->i_npt_length < 0 )
1207 p_sys->i_npt_length = -1;
1209 msg_Dbg( p_demux, "seek start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1212 return VLC_EGENERIC;
1214 /* Special for access_demux */
1215 case DEMUX_CAN_PAUSE:
1216 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1217 if( p_sys->rtsp && p_sys->i_npt_length )
1218 /* Not always true, but will be handled in SET_PAUSE_STATE */
1224 case DEMUX_CAN_CONTROL_PACE:
1225 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1227 #if 1 /* Disable for now until we have a clock synchro algo
1228 * which works with something else than MPEG over UDP */
1235 case DEMUX_SET_PAUSE_STATE:
1237 double d_npt = (double) p_sys->i_npt / I64C(1000000);
1240 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1241 if( p_sys->rtsp == NULL )
1242 return VLC_EGENERIC;
1245 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1246 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1247 d_npt > 0 ? d_npt : -1 ) ) )
1249 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1250 return VLC_EGENERIC;
1252 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1254 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 1195257600)
1255 for( i = 0; i < p_sys->i_track; i++ )
1257 p_sys->track[i]->i_pts = (int64_t) ( p_sys->track[i]->sub->rtpInfo.timestamp * (double)1000000.0 );
1258 p_sys->track[i]->i_start_seq = p_sys->track[i]->sub->rtpInfo.seqNum;
1259 msg_Info( p_demux, "set pause startseq: %u", p_sys->track[i]->i_start_seq );
1262 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1263 /* Retrieve the starttime if possible */
1264 p_sys->i_npt_start = (int64_t)( p_sys->ms->playStartTime() * (double)1000000.0 );
1266 p_sys->i_npt_start = -1;
1268 if( p_sys->i_npt_start < 0 )
1270 p_sys->i_npt_start = -1;
1274 p_sys->i_npt = p_sys->i_npt_start;
1276 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1277 /* Retrieve the duration if possible */
1278 p_sys->i_npt_length = (int64_t)( p_sys->ms->playEndTime() * (double)1000000.0 );
1280 p_sys->i_npt_length = -1;
1282 if( p_sys->i_npt_length < 0 )
1283 p_sys->i_npt_length = -1;
1285 msg_Dbg( p_demux, "pause start: %lld stop:%lld", p_sys->i_npt_start, p_sys->i_npt_length );
1288 case DEMUX_GET_TITLE_INFO:
1289 case DEMUX_SET_TITLE:
1290 case DEMUX_SET_SEEKPOINT:
1291 return VLC_EGENERIC;
1293 case DEMUX_GET_PTS_DELAY:
1294 pi64 = (int64_t*)va_arg( args, int64_t * );
1295 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1299 return VLC_EGENERIC;
1303 /*****************************************************************************
1304 * RollOverTcp: reopen the rtsp into TCP mode
1305 * XXX: ugly, a lot of code are duplicated from Open()
1306 * This should REALLY be fixed
1307 *****************************************************************************/
1308 static int RollOverTcp( demux_t *p_demux )
1310 demux_sys_t *p_sys = p_demux->p_sys;
1313 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1315 /* We close the old RTSP session */
1316 for( i = 0; i < p_sys->i_track; i++ )
1318 live_track_t *tk = p_sys->track[i];
1320 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
1321 free( tk->p_buffer );
1324 if( p_sys->i_track ) free( p_sys->track );
1325 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
1327 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1328 Medium::close( p_sys->ms );
1329 RTSPClient::close( p_sys->rtsp );
1333 p_sys->track = NULL;
1336 /* Reopen rtsp client */
1337 if( ( p_demux->s != NULL ) &&
1338 ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1340 msg_Err( p_demux, "Failed to connect with rtsp://%s",
1345 if( p_sys->p_sdp == NULL )
1347 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1351 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1353 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1357 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1363 return VLC_EGENERIC;
1367 /*****************************************************************************
1369 *****************************************************************************/
1370 static void StreamRead( void *p_private, unsigned int i_size,
1371 unsigned int i_truncated_bytes, struct timeval pts,
1372 unsigned int duration )
1374 live_track_t *tk = (live_track_t*)p_private;
1375 demux_t *p_demux = tk->p_demux;
1376 demux_sys_t *p_sys = p_demux->p_sys;
1379 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1381 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1382 (uint64_t)pts.tv_usec;
1384 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1385 i_pts &= UI64C(0x00ffffffffffffff);
1387 if( tk->b_quicktime && tk->p_es == NULL )
1389 QuickTimeGenericRTPSource *qtRTPSource =
1390 (QuickTimeGenericRTPSource*)tk->sub->rtpSource();
1391 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1392 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1394 if( tk->fmt.i_cat == VIDEO_ES ) {
1395 if( qtState.sdAtomSize < 16 + 32 )
1398 p_sys->event = 0xff;
1402 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1403 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1404 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1406 if( tk->fmt.i_codec == VLC_FOURCC('a', 'v', 'c', '1') )
1408 uint8_t *pos = (uint8_t*)qtRTPSource->qtState.sdAtom + 86;
1409 uint8_t *endpos = (uint8_t*)qtRTPSource->qtState.sdAtom
1410 + qtRTPSource->qtState.sdAtomSize;
1411 while (pos+8 < endpos) {
1412 unsigned atomLength = pos[0]<<24 | pos[1]<<16 | pos[2]<<8 | pos[3];
1413 if( atomLength == 0 || atomLength > endpos-pos) break;
1414 if( memcmp(pos+4, "avcC", 4) == 0 &&
1416 atomLength <= INT_MAX )
1418 tk->fmt.i_extra = atomLength-8;
1419 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1420 memcpy(tk->fmt.p_extra, pos+8, atomLength-8);
1428 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1429 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1430 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1434 if( qtState.sdAtomSize < 4 )
1437 p_sys->event = 0xff;
1441 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1443 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1447 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1449 pts.tv_sec * 1000000LL + pts.tv_usec );
1452 /* grow buffer if it looks like buffer is too small, but don't eat
1453 * up all the memory on strange streams */
1454 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1457 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1458 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1460 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1463 msg_Warn( p_demux, "realloc failed" );
1467 tk->p_buffer = (uint8_t*)p_tmp;
1470 if( i_size > tk->i_buffer )
1472 msg_Warn( p_demux, "buffer overflow" );
1474 /* FIXME could i_size be > buffer size ? */
1475 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1476 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1478 AMRAudioSource *amrSource = (AMRAudioSource*)tk->sub->readSource();
1480 p_block = block_New( p_demux, i_size + 1 );
1481 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1482 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1484 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1486 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1487 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->sub->rtpSource();
1488 uint32_t header = h261Source->lastSpecialHeader();
1490 uint32_t header = 0;
1491 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1493 p_block = block_New( p_demux, i_size + 4 );
1494 memcpy( p_block->p_buffer, &header, 4 );
1495 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1497 if( tk->sub->rtpSource()->curPacketMarkerBit() )
1498 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1500 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1502 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1503 msg_Warn( p_demux, "unsupported NAL type for H264" );
1505 /* Normal NAL type */
1506 p_block = block_New( p_demux, i_size + 4 );
1507 p_block->p_buffer[0] = 0x00;
1508 p_block->p_buffer[1] = 0x00;
1509 p_block->p_buffer[2] = 0x00;
1510 p_block->p_buffer[3] = 0x01;
1511 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1513 else if( tk->b_asf )
1515 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1516 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1518 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1522 p_block = block_New( p_demux, i_size );
1523 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1527 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1528 if( (tk->fmt.i_cat == VIDEO_ES) && (p_sys->i_pcr < i_pts) &&
1529 (i_pts > 0) && (p_sys->i_pcr > 0) )
1531 p_sys->i_npt += __MAX( 0, i_pts - p_sys->i_pcr );
1532 p_sys->i_pcr = i_pts;
1533 //msg_Dbg( p_demux, "npt update" );
1535 else if( /*tk->fmt.i_cat == VIDEO_ES &&*/ p_sys->i_pcr < i_pts )
1537 p_sys->i_pcr = i_pts;
1539 //msg_Dbg( p_demux, "npt %lld", p_sys->i_npt );
1541 if( (i_pts != tk->i_pts) && (!tk->b_muxed) )
1543 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1544 p_block->i_pts = i_pts;
1549 stream_DemuxSend( tk->p_out_muxed, p_block );
1551 else if( tk->b_asf )
1553 stream_DemuxSend( p_sys->p_out_asf, p_block );
1557 es_out_Send( p_demux->out, tk->p_es, p_block );
1560 /* warn that's ok */
1561 p_sys->event = 0xff;
1563 /* we have read data */
1565 p_demux->p_sys->b_no_data = VLC_FALSE;
1566 p_demux->p_sys->i_no_data_ti = 0;
1568 if( i_pts > 0 && !tk->b_muxed )
1574 /*****************************************************************************
1576 *****************************************************************************/
1577 static void StreamClose( void *p_private )
1579 live_track_t *tk = (live_track_t*)p_private;
1580 demux_t *p_demux = tk->p_demux;
1581 demux_sys_t *p_sys = p_demux->p_sys;
1583 msg_Dbg( p_demux, "StreamClose" );
1585 p_sys->event = 0xff;
1586 p_demux->b_error = VLC_TRUE;
1590 /*****************************************************************************
1592 *****************************************************************************/
1593 static void TaskInterrupt( void *p_private )
1595 demux_t *p_demux = (demux_t*)p_private;
1597 p_demux->p_sys->i_no_data_ti++;
1600 p_demux->p_sys->event = 0xff;
1603 /*****************************************************************************
1605 *****************************************************************************/
1606 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1608 p_timeout->b_die = VLC_FALSE;
1609 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1610 p_timeout->i_remain *= 1000000;
1612 vlc_thread_ready( p_timeout );
1615 while( !p_timeout->b_die )
1617 if( p_timeout->i_remain <= 0 )
1619 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1620 p_timeout->i_remain *= 1000000;
1621 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1622 msg_Dbg( p_timeout, "reset the timeout timer" );
1624 p_timeout->i_remain -= 200000;
1625 msleep( 200000 ); /* 200 ms */
1629 /*****************************************************************************
1631 *****************************************************************************/
1632 static int b64_decode( char *dest, char *src );
1634 static int ParseASF( demux_t *p_demux )
1636 demux_sys_t *p_sys = p_demux->p_sys;
1638 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1639 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1643 /* Parse the asf header */
1644 if( psz_asf == NULL )
1645 return VLC_EGENERIC;
1647 psz_asf += strlen( psz_marker );
1648 psz_asf = strdup( psz_asf ); /* Duplicate it */
1649 psz_end = strchr( psz_asf, '\n' );
1651 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1654 if( psz_asf >= psz_end )
1657 return VLC_EGENERIC;
1660 /* Always smaller */
1661 p_header = block_New( p_demux, psz_end - psz_asf );
1662 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1663 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1664 if( p_header->i_buffer <= 0 )
1667 return VLC_EGENERIC;
1670 /* Parse it to get packet size */
1671 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1673 /* Send it to demuxer */
1674 stream_DemuxSend( p_sys->p_out_asf, p_header );
1681 static unsigned char* parseH264ConfigStr( char const* configStr,
1682 unsigned int& configSize )
1685 int i, i_records = 1;
1690 if( configStr == NULL || *configStr == '\0' )
1693 psz = dup = strdup( configStr );
1695 /* Count the number of comma's */
1696 for( psz = dup; *psz != '\0'; ++psz )
1705 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1707 for( i = 0; i < i_records; i++ )
1709 cfg[configSize++] = 0x00;
1710 cfg[configSize++] = 0x00;
1711 cfg[configSize++] = 0x00;
1712 cfg[configSize++] = 0x01;
1714 configSize += b64_decode( (char*)&cfg[configSize], psz );
1715 psz += strlen(psz)+1;
1718 if( dup ) free( dup );
1722 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1723 static int b64_decode( char *dest, char *src )
1725 const char *dest_start = dest;
1729 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1730 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1731 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1732 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1733 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1734 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1735 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1736 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1737 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1738 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1739 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1740 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1741 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1742 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1743 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1744 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1747 for( i_level = 0; *src != '\0'; src++ )
1751 c = b64[(unsigned int)*src];
1763 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1767 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1771 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1779 return dest - dest_start;