1 /*****************************************************************************
2 * live555.cpp : LIVE555 Streaming Media support.
3 *****************************************************************************
4 * Copyright (C) 2003-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc_demux.h>
33 #include <vlc_interface.h>
34 #include <vlc_network.h>
39 # include <winsock2.h>
42 #include "BasicUsageEnvironment.hh"
43 #include "GroupsockHelper.hh"
44 #include "liveMedia.hh"
47 #include "../access/mms/asf.h" /* Who said ugly ? */
50 #if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
51 #define RECLAIM_ENV(env) delete (env)
53 #define RECLAIM_ENV(env) (env)->reclaim()
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
64 #define CACHING_TEXT N_("Caching value (ms)")
65 #define CACHING_LONGTEXT N_( \
66 "Allows you to modify the default caching value for RTSP streams. This " \
67 "value should be set in millisecond units." )
69 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
70 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
71 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
72 "for communication. In this mode you cannot connect to normal RTSP servers." )
74 #define USER_TEXT N_("RTSP user name")
75 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
76 "be used for authenticating the connection.")
77 #define PASS_TEXT N_("RTSP password")
78 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
79 "used for the connection.")
82 set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) );
83 set_capability( "demux2", 50 );
84 set_shortname( "RTP/RTSP");
85 set_callbacks( Open, Close );
86 add_shortcut( "live" );
87 add_shortcut( "livedotcom" );
88 set_category( CAT_INPUT );
89 set_subcategory( SUBCAT_INPUT_DEMUX );
92 set_description( _("RTSP/RTP access and demux") );
93 add_shortcut( "rtsp" );
94 add_shortcut( "sdp" );
95 set_capability( "access_demux", 0 );
96 set_callbacks( Open, Close );
97 add_bool( "rtsp-tcp", 0, NULL,
98 N_("Use RTP over RTSP (TCP)"),
99 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
100 add_integer( "rtp-client-port", -1, NULL,
102 N_("Port to use for the RTP source of the session"), VLC_TRUE );
103 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
104 add_bool( "rtsp-http", 0, NULL,
105 N_("Tunnel RTSP and RTP over HTTP"),
106 N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE );
107 add_integer( "rtsp-http-port", 80, NULL,
108 N_("HTTP tunnel port"),
109 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
112 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
113 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
114 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
115 KASENNA_LONGTEXT, VLC_TRUE );
116 add_string( "rtsp-user", NULL, NULL, USER_TEXT,
117 USER_LONGTEXT, VLC_TRUE );
118 add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
119 PASS_LONGTEXT, VLC_TRUE );
123 /*****************************************************************************
125 *****************************************************************************/
131 vlc_bool_t b_quicktime;
138 stream_t *p_out_muxed; /* for muxed stream */
140 RTPSource *rtpSource;
141 FramedSource *readSource;
142 vlc_bool_t b_rtcp_sync;
145 unsigned int i_buffer;
153 struct timeout_thread_t
163 char *p_sdp; /* XXX mallocated */
164 char *psz_path; /* URL-encoded path */
167 TaskScheduler *scheduler;
168 UsageEnvironment *env ;
173 live_track_t **track; /* XXX mallocated */
176 int64_t i_pcr_previous;
177 int64_t i_pcr_repeatdate;
188 /* timeout thread information */
189 int i_timeout; /* session timeout value in seconds */
190 vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
191 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
194 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
195 vlc_bool_t b_no_data; /* true if we never receive any data */
196 int i_no_data_ti; /* consecutive number of TaskInterrupt */
201 static int Demux ( demux_t * );
202 static int Control( demux_t *, int, va_list );
204 static int Connect ( demux_t * );
205 static int SessionsSetup( demux_t * );
206 static int Play ( demux_t *);
207 static int ParseASF ( demux_t * );
208 static int RollOverTcp ( demux_t * );
210 static void StreamRead ( void *, unsigned int, unsigned int,
211 struct timeval, unsigned int );
212 static void StreamClose ( void * );
213 static void TaskInterrupt( void * );
215 static void TimeoutPrevention( timeout_thread_t * );
217 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
218 static unsigned char* parseH264ConfigStr( char const* configStr,
219 unsigned int& configSize );
222 /*****************************************************************************
224 *****************************************************************************/
225 static int Open ( vlc_object_t *p_this )
227 demux_t *p_demux = (demux_t*)p_this;
228 demux_sys_t *p_sys = NULL;
230 MediaSubsessionIterator *iter = NULL;
231 MediaSubsession *sub = NULL;
236 /* See if it looks like a SDP
237 v, o, s fields are mandatory and in this order */
239 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
241 if( memcmp( (char*)p_peek, "v=0\r\n", 5 ) &&
242 memcmp( (char*)p_peek, "v=0\n", 4 ) &&
243 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
250 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
253 p_demux->pf_demux = Demux;
254 p_demux->pf_control= Control;
255 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
256 if( !p_sys ) return VLC_ENOMEM;
259 p_sys->scheduler = NULL;
266 p_sys->i_pcr_start = 0;
267 p_sys->i_pcr_previous = 0;
268 p_sys->i_pcr_repeatdate = 0;
269 p_sys->i_pcr_repeats = 0;
272 p_sys->p_out_asf = NULL;
273 p_sys->b_no_data = VLC_TRUE;
274 p_sys->i_no_data_ti = 0;
275 p_sys->p_timeout = NULL;
276 p_sys->i_timeout = 0;
277 p_sys->b_timeout_call = VLC_FALSE;
278 p_sys->b_multicast = VLC_FALSE;
279 p_sys->psz_path = strdup( p_demux->psz_path );
281 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
283 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
286 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
288 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
292 if( strcasecmp( p_demux->psz_access, "sdp" ) )
294 char *p = p_sys->psz_path;
295 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
298 if( p_demux->s != NULL )
300 /* Gather the complete sdp file */
302 int i_sdp_max = 1000;
303 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
307 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
308 i_sdp_max - i_sdp - 1 );
312 msg_Err( p_demux, "failed to read SDP" );
319 if( i_read < i_sdp_max - i_sdp - 1 )
326 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
328 p_sys->p_sdp = (char*)p_sdp;
330 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
332 /* sdp:// link from SAP */
333 p_sys->p_sdp = strdup( p_sys->psz_path );
335 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
337 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
341 if( p_sys->p_sdp == NULL )
343 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
347 /* Create the session from the SDP */
348 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
350 msg_Err( p_demux, "Could not create the RTSP Session: %s",
351 p_sys->env->getResultMsg() );
355 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
357 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
361 /* Retrieve the duration if possible */
362 p_sys->i_length = (int64_t)( p_sys->ms->playEndTime() * 1000000.0 );
363 if( p_sys->i_length < 0 )
364 p_sys->i_length = -1;
366 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
369 /* Create all es struct */
370 iter = new MediaSubsessionIterator( *p_sys->ms );
371 while( ( sub = iter->next() ) != NULL )
375 /* Check if we will receive data from this subsession for this track */
376 if( sub->readSource() == NULL ) continue;
378 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
379 tk->p_demux = p_demux;
382 tk->b_quicktime = VLC_FALSE;
383 tk->b_muxed = VLC_FALSE;
384 tk->b_asf = VLC_FALSE;
385 tk->b_rtcp_sync = VLC_FALSE;
386 tk->p_out_muxed = NULL;
388 tk->i_buffer = 65536;
389 tk->p_buffer = (uint8_t *)malloc( 65536 );
391 /* Value taken from mplayer */
392 if( !strcmp( sub->mediumName(), "audio" ) )
394 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
395 tk->fmt.audio.i_channels = sub->numChannels();
396 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
398 if( !strcmp( sub->codecName(), "MPA" ) ||
399 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
400 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
402 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
403 tk->fmt.audio.i_rate = 0;
405 else if( !strcmp( sub->codecName(), "AC3" ) )
407 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
408 tk->fmt.audio.i_rate = 0;
410 else if( !strcmp( sub->codecName(), "L16" ) )
412 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
413 tk->fmt.audio.i_bitspersample = 16;
415 else if( !strcmp( sub->codecName(), "L8" ) )
417 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
418 tk->fmt.audio.i_bitspersample = 8;
420 else if( !strcmp( sub->codecName(), "PCMU" ) )
422 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
424 else if( !strcmp( sub->codecName(), "PCMA" ) )
426 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
428 else if( !strncmp( sub->codecName(), "G726", 4 ) )
430 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
431 tk->fmt.audio.i_rate = 8000;
432 tk->fmt.audio.i_channels = 1;
433 if( !strcmp( sub->codecName()+5, "40" ) )
434 tk->fmt.i_bitrate = 40000;
435 else if( !strcmp( sub->codecName()+5, "32" ) )
436 tk->fmt.i_bitrate = 32000;
437 else if( !strcmp( sub->codecName()+5, "24" ) )
438 tk->fmt.i_bitrate = 24000;
439 else if( !strcmp( sub->codecName()+5, "16" ) )
440 tk->fmt.i_bitrate = 16000;
442 else if( !strcmp( sub->codecName(), "AMR" ) )
444 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
446 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
448 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
450 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
452 unsigned int i_extra;
455 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
457 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
460 tk->fmt.i_extra = i_extra;
461 tk->fmt.p_extra = malloc( i_extra );
462 memcpy( tk->fmt.p_extra, p_extra, i_extra );
466 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1141257600
467 /* Because the "faad" decoder does not handle the LATM data length field
468 at the start of each returned LATM frame, tell the RTP source to omit it. */
469 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
472 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
474 unsigned int i_extra;
477 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
479 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
482 tk->fmt.i_extra = i_extra;
483 tk->fmt.p_extra = malloc( i_extra );
484 memcpy( tk->fmt.p_extra, p_extra, i_extra );
488 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
490 tk->b_asf = VLC_TRUE;
491 if( p_sys->p_out_asf == NULL )
492 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
495 else if( !strcmp( sub->codecName(), "X-QT" ) ||
496 !strcmp( sub->codecName(), "X-QUICKTIME" ) )
498 tk->b_quicktime = VLC_TRUE;
501 else if( !strcmp( sub->mediumName(), "video" ) )
503 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
504 if( !strcmp( sub->codecName(), "MPV" ) )
506 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
508 else if( !strcmp( sub->codecName(), "H263" ) ||
509 !strcmp( sub->codecName(), "H263-1998" ) ||
510 !strcmp( sub->codecName(), "H263-2000" ) )
512 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
514 else if( !strcmp( sub->codecName(), "H261" ) )
516 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
518 else if( !strcmp( sub->codecName(), "H264" ) )
520 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
521 unsigned int i_extra = 0;
522 uint8_t *p_extra = NULL;
524 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
525 tk->fmt.b_packetized = VLC_FALSE;
527 /* XXX not the right minimal version I fear */
528 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
529 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
532 tk->fmt.i_extra = i_extra;
533 tk->fmt.p_extra = malloc( i_extra );
534 memcpy( tk->fmt.p_extra, p_extra, i_extra );
540 else if( !strcmp( sub->codecName(), "JPEG" ) )
542 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
544 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
546 unsigned int i_extra;
549 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
551 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
554 tk->fmt.i_extra = i_extra;
555 tk->fmt.p_extra = malloc( i_extra );
556 memcpy( tk->fmt.p_extra, p_extra, i_extra );
560 else if( !strcmp( sub->codecName(), "X-QT" ) ||
561 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
562 !strcmp( sub->codecName(), "X-QDM" ) ||
563 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
564 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
566 tk->b_quicktime = VLC_TRUE;
568 else if( !strcmp( sub->codecName(), "MP2T" ) )
570 tk->b_muxed = VLC_TRUE;
571 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
573 else if( !strcmp( sub->codecName(), "MP2P" ) ||
574 !strcmp( sub->codecName(), "MP1S" ) )
576 tk->b_muxed = VLC_TRUE;
577 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
580 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
582 tk->b_asf = VLC_TRUE;
583 if( p_sys->p_out_asf == NULL )
584 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
589 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
591 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
594 if( sub->rtcpInstance() != NULL )
596 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
599 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
601 tk->readSource = sub->readSource();
602 tk->rtpSource = sub->rtpSource();
605 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
606 p_sys->track[p_sys->i_track++] = tk;
611 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
618 if( p_sys->p_out_asf && ParseASF( p_demux ) )
620 msg_Err( p_demux, "cannot find a usable asf header" );
621 /* TODO Clean tracks */
625 if( p_sys->i_track <= 0 )
631 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
632 if( p_sys->ms ) Medium::close( p_sys->ms );
633 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
634 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
635 if( p_sys->p_timeout )
637 p_sys->p_timeout->b_die = VLC_TRUE;
638 vlc_thread_join( p_sys->p_timeout );
639 vlc_object_detach( p_sys->p_timeout );
640 vlc_object_destroy( p_sys->p_timeout );
642 if( p_sys->scheduler ) delete p_sys->scheduler;
643 if( p_sys->p_sdp ) free( p_sys->p_sdp );
644 if( p_sys->psz_path ) free( p_sys->psz_path );
650 /*****************************************************************************
652 *****************************************************************************/
653 static void Close( vlc_object_t *p_this )
655 demux_t *p_demux = (demux_t*)p_this;
656 demux_sys_t *p_sys = p_demux->p_sys;
659 for( i = 0; i < p_sys->i_track; i++ )
661 live_track_t *tk = p_sys->track[i];
663 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
664 free( tk->p_buffer );
668 if( p_sys->i_track ) free( p_sys->track );
669 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
671 if( p_sys->rtsp && p_sys->ms )
674 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
677 Medium::close( p_sys->ms );
679 if( p_sys->p_timeout )
681 p_sys->p_timeout->b_die = VLC_TRUE;
682 vlc_thread_join( p_sys->p_timeout );
683 vlc_object_detach( p_sys->p_timeout );
684 vlc_object_destroy( p_sys->p_timeout );
687 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
688 if( p_sys->env ) RECLAIM_ENV( p_sys->env );
689 if( p_sys->scheduler ) delete p_sys->scheduler;
690 if( p_sys->p_sdp ) free( p_sys->p_sdp );
691 if( p_sys->psz_path ) free( p_sys->psz_path );
695 /*****************************************************************************
696 * Connect: connects to the RTSP server to setup the session DESCRIBE
697 *****************************************************************************/
698 static int Connect( demux_t *p_demux )
700 demux_sys_t *p_sys = p_demux->p_sys;
701 Authenticator authenticator;
703 char *psz_user = NULL;
704 char *psz_pwd = NULL;
705 char *psz_url = NULL;
706 char *psz_options = NULL;
709 int i_ret = VLC_SUCCESS;
712 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
713 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
715 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
716 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
717 "VLC media player", i_http_port ) ) == NULL )
719 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
720 "VLC media player" ) ) == NULL )
723 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
724 p_sys->env->getResultMsg() );
727 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
728 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
730 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
731 if( psz_options ) delete [] psz_options;
733 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
734 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
737 authenticator.setUsernameAndPassword( (const char*)psz_user, (const char*)psz_pwd );
738 p_sdp = p_sys->rtsp->describeURL( psz_url,
739 &authenticator, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
741 if( psz_user ) free( psz_user );
742 if( psz_pwd ) free( psz_pwd );
746 /* failure occurred */
748 const char *psz_error = p_sys->env->getResultMsg();
750 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
751 sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
755 char *psz_login = NULL; char *psz_password = NULL;
756 msg_Dbg( p_demux, "authentication failed" );
758 i_ret = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
759 _("Please enter a valid login name and a password."),
760 &psz_login, &psz_password );
761 if( i_ret == DIALOG_OK_YES )
763 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
764 psz_login, psz_password );
766 if( psz_login ) psz_user = psz_login;
767 if( psz_password ) psz_pwd = psz_password;
770 if( psz_login ) free( psz_login );
771 if( psz_password ) free( psz_password );
773 else if( !var_CreateGetBool( p_demux, "rtsp-http" ) )
775 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
777 val.b_bool = VLC_TRUE;
778 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
779 var_Set( p_demux, "rtsp-http", val );
780 if( psz_url ) free( psz_url );
781 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
784 i_ret = VLC_EGENERIC;
786 if( psz_url ) free( psz_url );
788 /* malloc-ated copy */
789 if( p_sys->p_sdp ) free( p_sys->p_sdp );
790 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
796 /*****************************************************************************
797 * Connect: prepares the subsessions and does the SETUP
798 *****************************************************************************/
799 static int SessionsSetup( demux_t *p_demux )
801 demux_sys_t *p_sys = p_demux->p_sys;
802 MediaSubsessionIterator *iter = NULL;
803 MediaSubsession *sub = NULL;
805 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
807 int i_active_sessions = 0;
808 int i_return = VLC_SUCCESS;
809 unsigned int i_buffer = 0;
810 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
812 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
813 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
815 /* Initialise each media subsession */
816 iter = new MediaSubsessionIterator( *p_sys->ms );
817 while( ( sub = iter->next() ) != NULL )
821 /* Value taken from mplayer */
822 if( !strcmp( sub->mediumName(), "audio" ) )
824 else if( !strcmp( sub->mediumName(), "video" ) )
828 if( i_client_port != -1 )
830 sub->setClientPortNum( i_client_port );
834 if( strcasestr( sub->codecName(), "REAL" ) )
836 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
837 i_return = VLC_EGENERIC;
841 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
842 bInit = sub->initiate( 4 ); /* Constant ? */
844 bInit = sub->initiate();
848 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
849 sub->mediumName(), sub->codecName(),
850 p_sys->env->getResultMsg() );
854 if( sub->rtpSource() != NULL )
856 int fd = sub->rtpSource()->RTPgs()->socketNum();
858 /* Increase the buffer size */
860 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
862 /* Increase the RTP reorder timebuffer just a bit */
863 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
866 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
869 /* Issue the SETUP */
872 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
873 b_rtsp_tcp ? True : False ) ) )
875 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
876 sub->codecName(), p_sys->env->getResultMsg() );
878 else i_active_sessions++;
879 } else i_active_sessions++; /* we don't really know, let's just hope it's there */
881 if( !p_sys->b_multicast )
883 /* Check, because we need diff. rollover behaviour for multicast */
884 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
889 if( i_active_sessions <= 0 ) i_return = VLC_EGENERIC;
893 /*****************************************************************************
894 * Play: starts the actual playback of the stream
895 *****************************************************************************/
896 static int Play( demux_t *p_demux )
898 demux_sys_t *p_sys = p_demux->p_sys;
903 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
905 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
909 /* Retrieve the timeout value and set up a timeout prevention thread */
910 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
911 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
913 if( p_sys->i_timeout > 0 && !p_sys->p_timeout )
915 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
916 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
917 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
918 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
919 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
921 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
922 vlc_object_destroy( p_sys->p_timeout );
924 msg_Dbg( p_demux, "spawned timeout thread" );
925 vlc_object_attach( p_sys->p_timeout, p_demux );
932 /*****************************************************************************
934 *****************************************************************************/
935 static int Demux( demux_t *p_demux )
937 demux_sys_t *p_sys = p_demux->p_sys;
940 vlc_bool_t b_send_pcr = VLC_TRUE;
944 /* Check if we need to send the server a Keep-A-Live signal */
945 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
947 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
948 char *psz_bye = NULL;
949 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
951 p_sys->b_timeout_call = VLC_FALSE;
954 for( i = 0; i < p_sys->i_track; i++ )
956 live_track_t *tk = p_sys->track[i];
958 if( tk->b_asf || tk->b_muxed )
959 b_send_pcr = VLC_FALSE;
965 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
970 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
972 p_sys->i_pcr = i_pcr;
975 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
976 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
977 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
979 p_sys->i_pcr_start = i_pcr;
984 /* Disabled because it's simply not reliable enough */
986 /* When a On Demand QT stream ends, the last frame keeps going with the same PCR/PTS value */
987 /* This tests for that, so we can later decide to end this session */
988 if( i_pcr > 0 && p_sys->i_pcr == p_sys->i_pcr_previous )
990 if( p_sys->i_pcr_repeats == 0 )
991 p_sys->i_pcr_repeatdate = mdate();
992 p_sys->i_pcr_repeats++;
996 p_sys->i_pcr_previous = p_sys->i_pcr;
997 p_sys->i_pcr_repeatdate = 0;
998 p_sys->i_pcr_repeats = 0;
1001 if( p_sys->i_pcr_repeats > 5 && mdate() > p_sys->i_pcr_repeatdate + 1000000 )
1003 /* We need at least 5 repeats over at least a second of time before we EOF */
1004 msg_Dbg( p_demux, "suspect EOF due to end of VoD session" );
1009 /* First warn we want to read data */
1011 for( i = 0; i < p_sys->i_track; i++ )
1013 live_track_t *tk = p_sys->track[i];
1015 if( tk->waiting == 0 )
1018 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
1019 StreamRead, tk, StreamClose, tk );
1022 /* Create a task that will be called if we wait more than 300ms */
1023 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1026 p_sys->scheduler->doEventLoop( &p_sys->event );
1028 /* remove the task */
1029 p_sys->scheduler->unscheduleDelayedTask( task );
1031 /* Check for gap in pts value */
1032 for( i = 0; i < p_sys->i_track; i++ )
1034 live_track_t *tk = p_sys->track[i];
1036 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1037 tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
1039 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1041 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1042 tk->b_rtcp_sync = VLC_TRUE;
1044 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
1046 p_sys->i_pcr_start = 0;
1052 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
1054 /* FIXME Make this configurable
1055 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1059 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1061 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
1063 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1065 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1066 if( RollOverTcp( p_demux ) )
1068 msg_Err( p_demux, "TCP rollover failed, aborting" );
1072 else if( p_sys->i_no_data_ti > 34 )
1074 msg_Err( p_demux, "no data received in 10s, aborting" );
1078 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1081 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1085 return p_demux->b_error ? 0 : 1;
1088 /*****************************************************************************
1090 *****************************************************************************/
1091 static int Control( demux_t *p_demux, int i_query, va_list args )
1093 demux_sys_t *p_sys = p_demux->p_sys;
1096 vlc_bool_t *pb, b_bool;
1100 case DEMUX_GET_TIME:
1101 pi64 = (int64_t*)va_arg( args, int64_t * );
1102 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
1105 case DEMUX_GET_LENGTH:
1106 pi64 = (int64_t*)va_arg( args, int64_t * );
1107 *pi64 = p_sys->i_length;
1110 case DEMUX_GET_POSITION:
1111 pf = (double*)va_arg( args, double* );
1112 if( p_sys->i_length > 0 )
1114 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
1115 p_sys->i_start ) / (double)(p_sys->i_length);
1123 case DEMUX_SET_POSITION:
1126 f = (double)va_arg( args, double );
1128 if( p_sys->rtsp && p_sys->i_length > 0 )
1130 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
1131 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
1133 msg_Err( p_demux, "PLAY failed %s",
1134 p_sys->env->getResultMsg() );
1135 return VLC_EGENERIC;
1137 p_sys->i_start = (int64_t)(f * (double)p_sys->i_length);
1138 p_sys->i_pcr_start = 0;
1145 /* Special for access_demux */
1146 case DEMUX_CAN_PAUSE:
1147 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1148 if( p_sys->rtsp && p_sys->i_length )
1149 /* Not always true, but will be handled in SET_PAUSE_STATE */
1155 case DEMUX_CAN_CONTROL_PACE:
1156 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1158 #if 0 /* Disable for now until we have a clock synchro algo
1159 * which works with something else than MPEG over UDP */
1166 case DEMUX_SET_PAUSE_STATE:
1169 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
1170 p_sys->i_start ) ) / 1000000.00;
1172 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1173 if( p_sys->rtsp == NULL )
1174 return VLC_EGENERIC;
1176 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1177 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1178 d_npt > 0 ? d_npt : -1 ) ) )
1180 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1181 return VLC_EGENERIC;
1184 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
1185 for( i = 0; i < p_sys->i_track; i++ )
1187 p_sys->track[i]->i_pts = 0;
1189 p_sys->i_pcr_start = 0; /* FIXME Wrong */
1194 case DEMUX_GET_TITLE_INFO:
1195 case DEMUX_SET_TITLE:
1196 case DEMUX_SET_SEEKPOINT:
1197 return VLC_EGENERIC;
1199 case DEMUX_GET_PTS_DELAY:
1200 pi64 = (int64_t*)va_arg( args, int64_t * );
1201 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1205 return VLC_EGENERIC;
1209 /*****************************************************************************
1210 * RollOverTcp: reopen the rtsp into TCP mode
1211 * XXX: ugly, a lot of code are duplicated from Open()
1212 *****************************************************************************/
1213 static int RollOverTcp( demux_t *p_demux )
1215 demux_sys_t *p_sys = p_demux->p_sys;
1216 MediaSubsessionIterator *iter = 0;
1217 MediaSubsession *sub;
1221 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1223 /* We close the old RTSP session */
1224 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1226 Medium::close( p_sys->ms );
1227 RTSPClient::close( p_sys->rtsp );
1232 /* Reopen rtsp client */
1233 if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1235 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
1239 if( p_sys->p_sdp == NULL )
1241 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1245 /* Create the session from the SDP */
1246 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
1248 msg_Err( p_demux, "Could not create the RTSP Session: %s",
1249 p_sys->env->getResultMsg() );
1253 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1255 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1259 /* Retrieve the duration if possible */
1260 p_sys->i_length = (int64_t)( p_sys->ms->playEndTime() * 1000000.0 );
1261 if( p_sys->i_length < 0 )
1262 p_sys->i_length = -1;
1264 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1267 /* Update all tracks */
1268 iter = new MediaSubsessionIterator( *p_sys->ms );
1270 while( ( sub = iter->next() ) != NULL )
1274 if( sub->readSource() == NULL )
1276 if( i_tk >= p_sys->i_track )
1278 msg_Err( p_demux, "WTF !" );
1282 tk = p_sys->track[i_tk];
1287 tk->b_rtcp_sync = VLC_FALSE;
1289 if( sub->rtcpInstance() != NULL )
1290 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1292 tk->readSource = sub->readSource();
1293 tk->rtpSource = sub->rtpSource();
1302 if( iter ) delete iter;
1303 return VLC_EGENERIC;
1307 /*****************************************************************************
1309 *****************************************************************************/
1310 static void StreamRead( void *p_private, unsigned int i_size,
1311 unsigned int i_truncated_bytes, struct timeval pts,
1312 unsigned int duration )
1314 live_track_t *tk = (live_track_t*)p_private;
1315 demux_t *p_demux = tk->p_demux;
1316 demux_sys_t *p_sys = p_demux->p_sys;
1319 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1320 (uint64_t)pts.tv_usec;
1322 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1323 i_pts &= UI64C(0x00ffffffffffffff);
1325 if( tk->b_quicktime && tk->p_es == NULL )
1327 QuickTimeGenericRTPSource *qtRTPSource =
1328 (QuickTimeGenericRTPSource*)tk->rtpSource;
1329 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1330 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1332 if( tk->fmt.i_cat == VIDEO_ES ) {
1333 if( qtState.sdAtomSize < 16 + 32 )
1336 p_sys->event = 0xff;
1340 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1341 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1342 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1344 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1345 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1346 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1350 if( qtState.sdAtomSize < 4 )
1353 p_sys->event = 0xff;
1357 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1359 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1363 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1365 pts.tv_sec * 1000000LL + pts.tv_usec );
1368 /* grow buffer if it looks like buffer is too small, but don't eat
1369 * up all the memory on strange streams */
1370 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1373 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1374 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1376 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1379 msg_Warn( p_demux, "realloc failed" );
1383 tk->p_buffer = (uint8_t*)p_tmp;
1386 if( i_size > tk->i_buffer )
1388 msg_Warn( p_demux, "buffer overflow" );
1390 /* FIXME could i_size be > buffer size ? */
1391 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1392 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1394 AMRAudioSource *amrSource = (AMRAudioSource*)tk->readSource;
1396 p_block = block_New( p_demux, i_size + 1 );
1397 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1398 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1400 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1402 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1403 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1404 uint32_t header = h261Source->lastSpecialHeader();
1406 uint32_t header = 0;
1407 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1409 p_block = block_New( p_demux, i_size + 4 );
1410 memcpy( p_block->p_buffer, &header, 4 );
1411 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1413 if( tk->rtpSource->curPacketMarkerBit() )
1414 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1416 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1418 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1419 msg_Warn( p_demux, "unsupported NAL type for H264" );
1421 /* Normal NAL type */
1422 p_block = block_New( p_demux, i_size + 4 );
1423 p_block->p_buffer[0] = 0x00;
1424 p_block->p_buffer[1] = 0x00;
1425 p_block->p_buffer[2] = 0x00;
1426 p_block->p_buffer[3] = 0x01;
1427 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1429 else if( tk->b_asf )
1431 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1432 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1434 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1438 p_block = block_New( p_demux, i_size );
1439 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1442 if( i_pts != tk->i_pts && !tk->b_muxed )
1444 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1445 p_block->i_pts = i_pts;
1450 stream_DemuxSend( tk->p_out_muxed, p_block );
1452 else if( tk->b_asf )
1454 stream_DemuxSend( p_sys->p_out_asf, p_block );
1458 es_out_Send( p_demux->out, tk->p_es, p_block );
1461 /* warn that's ok */
1462 p_sys->event = 0xff;
1464 /* we have read data */
1466 p_demux->p_sys->b_no_data = VLC_FALSE;
1467 p_demux->p_sys->i_no_data_ti = 0;
1469 if( i_pts > 0 && !tk->b_muxed )
1475 /*****************************************************************************
1477 *****************************************************************************/
1478 static void StreamClose( void *p_private )
1480 live_track_t *tk = (live_track_t*)p_private;
1481 demux_t *p_demux = tk->p_demux;
1482 demux_sys_t *p_sys = p_demux->p_sys;
1484 msg_Dbg( p_demux, "StreamClose" );
1486 p_sys->event = 0xff;
1487 p_demux->b_error = VLC_TRUE;
1491 /*****************************************************************************
1493 *****************************************************************************/
1494 static void TaskInterrupt( void *p_private )
1496 demux_t *p_demux = (demux_t*)p_private;
1498 p_demux->p_sys->i_no_data_ti++;
1501 p_demux->p_sys->event = 0xff;
1504 /*****************************************************************************
1506 *****************************************************************************/
1507 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1509 p_timeout->b_die = VLC_FALSE;
1510 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1511 p_timeout->i_remain *= 1000000;
1513 vlc_thread_ready( p_timeout );
1516 while( !p_timeout->b_die )
1518 if( p_timeout->i_remain <= 0 )
1520 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1521 p_timeout->i_remain *= 1000000;
1522 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1523 msg_Dbg( p_timeout, "reset the timeout timer" );
1525 p_timeout->i_remain -= 200000;
1526 msleep( 200000 ); /* 200 ms */
1530 /*****************************************************************************
1532 *****************************************************************************/
1533 static int b64_decode( char *dest, char *src );
1535 static int ParseASF( demux_t *p_demux )
1537 demux_sys_t *p_sys = p_demux->p_sys;
1539 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1540 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1544 /* Parse the asf header */
1545 if( psz_asf == NULL )
1546 return VLC_EGENERIC;
1548 psz_asf += strlen( psz_marker );
1549 psz_asf = strdup( psz_asf ); /* Duplicate it */
1550 psz_end = strchr( psz_asf, '\n' );
1552 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1555 if( psz_asf >= psz_end )
1558 return VLC_EGENERIC;
1561 /* Always smaller */
1562 p_header = block_New( p_demux, psz_end - psz_asf );
1563 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1564 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1565 if( p_header->i_buffer <= 0 )
1568 return VLC_EGENERIC;
1571 /* Parse it to get packet size */
1572 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1574 /* Send it to demuxer */
1575 stream_DemuxSend( p_sys->p_out_asf, p_header );
1581 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
1582 static unsigned char* parseH264ConfigStr( char const* configStr,
1583 unsigned int& configSize )
1586 int i, i_records = 1;
1591 if( configStr == NULL || *configStr == '\0' )
1594 psz = dup = strdup( configStr );
1596 /* Count the number of comma's */
1597 for( psz = dup; *psz != '\0'; ++psz )
1606 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1608 for( i = 0; i < i_records; i++ )
1610 cfg[configSize++] = 0x00;
1611 cfg[configSize++] = 0x00;
1612 cfg[configSize++] = 0x00;
1613 cfg[configSize++] = 0x01;
1615 configSize += b64_decode( (char*)&cfg[configSize], psz );
1616 psz += strlen(psz)+1;
1619 if( dup ) free( dup );
1624 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1625 static int b64_decode( char *dest, char *src )
1627 const char *dest_start = dest;
1631 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1632 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1633 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1634 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1635 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1636 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1637 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1638 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1639 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1640 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1641 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1642 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1643 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1644 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1645 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1646 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1649 for( i_level = 0; *src != '\0'; src++ )
1653 c = b64[(unsigned int)*src];
1665 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1669 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1673 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1681 return dest - dest_start;