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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
32 #include "vlc_interaction.h"
38 # include <winsock2.h>
41 #include "BasicUsageEnvironment.hh"
42 #include "GroupsockHelper.hh"
43 #include "liveMedia.hh"
46 #include "../access/mms/asf.h" /* Who said ugly ? */
49 #if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
50 #define RECLAIM_ENV(env) delete (env)
52 #define RECLAIM_ENV(env) (env)->reclaim()
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define CACHING_TEXT N_("Caching value (ms)")
64 #define CACHING_LONGTEXT N_( \
65 "Allows you to modify the default caching value for RTSP streams. This " \
66 "value should be set in millisecond units." )
68 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
69 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
70 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
71 "for communication. In this mode you cannot connect to normal RTSP servers." )
73 #define USER_TEXT N_("RTSP user name")
74 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
75 "be used for authenticating the connection.")
76 #define PASS_TEXT N_("RTSP password")
77 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
78 "used for the connection.")
81 set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) );
82 set_capability( "demux2", 50 );
83 set_shortname( "RTP/RTSP");
84 set_callbacks( Open, Close );
85 add_shortcut( "live" );
86 add_shortcut( "livedotcom" );
87 set_category( CAT_INPUT );
88 set_subcategory( SUBCAT_INPUT_DEMUX );
91 set_description( _("RTSP/RTP access and demux") );
92 add_shortcut( "rtsp" );
93 add_shortcut( "sdp" );
94 set_capability( "access_demux", 0 );
95 set_callbacks( Open, Close );
96 add_bool( "rtsp-tcp", 0, NULL,
97 N_("Use RTP over RTSP (TCP)"),
98 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
99 add_integer( "rtp-client-port", -1, NULL,
101 N_("Port to use for the RTP source of the session"), VLC_TRUE );
102 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
103 add_bool( "rtsp-http", 0, NULL,
104 N_("Tunnel RTSP and RTP over HTTP"),
105 N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE );
106 add_integer( "rtsp-http-port", 80, NULL,
107 N_("HTTP tunnel port"),
108 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
111 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
112 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
113 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
114 KASENNA_LONGTEXT, VLC_TRUE );
115 add_string( "rtsp-user", NULL, NULL, USER_TEXT,
116 USER_LONGTEXT, VLC_TRUE );
117 add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
118 PASS_LONGTEXT, VLC_TRUE );
122 /*****************************************************************************
124 *****************************************************************************/
130 vlc_bool_t b_quicktime;
137 stream_t *p_out_muxed; /* for muxed stream */
139 RTPSource *rtpSource;
140 FramedSource *readSource;
141 vlc_bool_t b_rtcp_sync;
144 unsigned int i_buffer;
152 struct timeout_thread_t
162 char *p_sdp; /* XXX mallocated */
163 char *psz_path; /* URL-encoded path */
166 TaskScheduler *scheduler;
167 UsageEnvironment *env ;
172 live_track_t **track; /* XXX mallocated */
175 int64_t i_pcr_previous;
176 int64_t i_pcr_repeatdate;
187 /* timeout thread information */
188 int i_timeout; /* session timeout value in seconds */
189 vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
190 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
193 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
194 vlc_bool_t b_no_data; /* true if we never receive any data */
195 int i_no_data_ti; /* consecutive number of TaskInterrupt */
200 static int Demux ( demux_t * );
201 static int Control( demux_t *, int, va_list );
203 static int Connect ( demux_t * );
204 static int SessionsSetup( demux_t * );
205 static int Play ( demux_t *);
206 static int ParseASF ( demux_t * );
207 static int RollOverTcp ( demux_t * );
209 static void StreamRead ( void *, unsigned int, unsigned int,
210 struct timeval, unsigned int );
211 static void StreamClose ( void * );
212 static void TaskInterrupt( void * );
214 static void TimeoutPrevention( timeout_thread_t * );
216 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
217 static unsigned char* parseH264ConfigStr( char const* configStr,
218 unsigned int& configSize );
221 /*****************************************************************************
223 *****************************************************************************/
224 static int Open ( vlc_object_t *p_this )
226 demux_t *p_demux = (demux_t*)p_this;
227 demux_sys_t *p_sys = NULL;
229 MediaSubsessionIterator *iter = NULL;
230 MediaSubsession *sub = NULL;
235 /* See if it looks like a SDP
236 v, o, s fields are mandatory and in this order */
238 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
240 if( memcmp( (char*)p_peek, "v=0\r\n", 5 ) &&
241 memcmp( (char*)p_peek, "v=0\n", 4 ) &&
242 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
249 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
252 p_demux->pf_demux = Demux;
253 p_demux->pf_control= Control;
254 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
255 if( !p_sys ) return VLC_ENOMEM;
258 p_sys->scheduler = NULL;
265 p_sys->i_pcr_start = 0;
266 p_sys->i_pcr_previous = 0;
267 p_sys->i_pcr_repeatdate = 0;
268 p_sys->i_pcr_repeats = 0;
271 p_sys->p_out_asf = NULL;
272 p_sys->b_no_data = VLC_TRUE;
273 p_sys->i_no_data_ti = 0;
274 p_sys->p_timeout = NULL;
275 p_sys->i_timeout = 0;
276 p_sys->b_timeout_call = VLC_FALSE;
277 p_sys->b_multicast = VLC_FALSE;
278 p_sys->psz_path = strdup( p_demux->psz_path );
280 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
282 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
285 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
287 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
291 if( strcasecmp( p_demux->psz_access, "sdp" ) )
293 char *p = p_sys->psz_path;
294 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
297 if( p_demux->s != NULL )
299 /* Gather the complete sdp file */
301 int i_sdp_max = 1000;
302 uint8_t *p_sdp = (uint8_t*) malloc( i_sdp_max );
306 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
307 i_sdp_max - i_sdp - 1 );
311 msg_Err( p_demux, "failed to read SDP" );
318 if( i_read < i_sdp_max - i_sdp - 1 )
325 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
327 p_sys->p_sdp = (char*)p_sdp;
329 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
331 /* sdp:// link from SAP */
332 p_sys->p_sdp = strdup( p_sys->psz_path );
334 else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
336 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
340 if( p_sys->p_sdp == NULL )
342 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
346 /* Create the session from the SDP */
347 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
349 msg_Err( p_demux, "Could not create the RTSP Session: %s",
350 p_sys->env->getResultMsg() );
354 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
356 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
360 /* Retrieve the duration if possible */
361 p_sys->i_length = (int64_t)( p_sys->ms->playEndTime() * 1000000.0 );
362 if( p_sys->i_length < 0 )
363 p_sys->i_length = -1;
365 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
368 /* Create all es struct */
369 iter = new MediaSubsessionIterator( *p_sys->ms );
370 while( ( sub = iter->next() ) != NULL )
374 /* Check if we will receive data from this subsession for this track */
375 if( sub->readSource() == NULL ) continue;
377 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
378 tk->p_demux = p_demux;
381 tk->b_quicktime = VLC_FALSE;
382 tk->b_muxed = VLC_FALSE;
383 tk->b_asf = VLC_FALSE;
384 tk->b_rtcp_sync = VLC_FALSE;
385 tk->p_out_muxed = NULL;
387 tk->i_buffer = 65536;
388 tk->p_buffer = (uint8_t *)malloc( 65536 );
390 /* Value taken from mplayer */
391 if( !strcmp( sub->mediumName(), "audio" ) )
393 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
394 tk->fmt.audio.i_channels = sub->numChannels();
395 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
397 if( !strcmp( sub->codecName(), "MPA" ) ||
398 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
399 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
401 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
402 tk->fmt.audio.i_rate = 0;
404 else if( !strcmp( sub->codecName(), "AC3" ) )
406 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
407 tk->fmt.audio.i_rate = 0;
409 else if( !strcmp( sub->codecName(), "L16" ) )
411 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
412 tk->fmt.audio.i_bitspersample = 16;
414 else if( !strcmp( sub->codecName(), "L8" ) )
416 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
417 tk->fmt.audio.i_bitspersample = 8;
419 else if( !strcmp( sub->codecName(), "PCMU" ) )
421 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
423 else if( !strcmp( sub->codecName(), "PCMA" ) )
425 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
427 else if( !strncmp( sub->codecName(), "G726", 4 ) )
429 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
430 tk->fmt.audio.i_rate = 8000;
431 tk->fmt.audio.i_channels = 1;
432 if( !strcmp( sub->codecName()+5, "40" ) )
433 tk->fmt.i_bitrate = 40000;
434 else if( !strcmp( sub->codecName()+5, "32" ) )
435 tk->fmt.i_bitrate = 32000;
436 else if( !strcmp( sub->codecName()+5, "24" ) )
437 tk->fmt.i_bitrate = 24000;
438 else if( !strcmp( sub->codecName()+5, "16" ) )
439 tk->fmt.i_bitrate = 16000;
441 else if( !strcmp( sub->codecName(), "AMR" ) )
443 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
445 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
447 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
449 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
451 unsigned int i_extra;
454 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
456 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
459 tk->fmt.i_extra = i_extra;
460 tk->fmt.p_extra = malloc( i_extra );
461 memcpy( tk->fmt.p_extra, p_extra, i_extra );
465 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1141257600
466 /* Because the "faad" decoder does not handle the LATM data length field
467 at the start of each returned LATM frame, tell the RTP source to omit it. */
468 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
471 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
473 unsigned int i_extra;
476 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
478 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
481 tk->fmt.i_extra = i_extra;
482 tk->fmt.p_extra = malloc( i_extra );
483 memcpy( tk->fmt.p_extra, p_extra, i_extra );
487 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
489 tk->b_asf = VLC_TRUE;
490 if( p_sys->p_out_asf == NULL )
491 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
495 else if( !strcmp( sub->mediumName(), "video" ) )
497 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
498 if( !strcmp( sub->codecName(), "MPV" ) )
500 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
502 else if( !strcmp( sub->codecName(), "H263" ) ||
503 !strcmp( sub->codecName(), "H263-1998" ) ||
504 !strcmp( sub->codecName(), "H263-2000" ) )
506 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
508 else if( !strcmp( sub->codecName(), "H261" ) )
510 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
512 else if( !strcmp( sub->codecName(), "H264" ) )
514 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
515 unsigned int i_extra = 0;
516 uint8_t *p_extra = NULL;
518 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
519 tk->fmt.b_packetized = VLC_FALSE;
521 /* XXX not the right minimal version I fear */
522 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
523 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
526 tk->fmt.i_extra = i_extra;
527 tk->fmt.p_extra = malloc( i_extra );
528 memcpy( tk->fmt.p_extra, p_extra, i_extra );
534 else if( !strcmp( sub->codecName(), "JPEG" ) )
536 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
538 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
540 unsigned int i_extra;
543 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
545 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
548 tk->fmt.i_extra = i_extra;
549 tk->fmt.p_extra = malloc( i_extra );
550 memcpy( tk->fmt.p_extra, p_extra, i_extra );
554 else if( !strcmp( sub->codecName(), "X-QT" ) ||
555 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
556 !strcmp( sub->codecName(), "X-QDM" ) ||
557 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
558 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
560 tk->b_quicktime = VLC_TRUE;
562 else if( !strcmp( sub->codecName(), "MP2T" ) )
564 tk->b_muxed = VLC_TRUE;
565 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
567 else if( !strcmp( sub->codecName(), "MP2P" ) ||
568 !strcmp( sub->codecName(), "MP1S" ) )
570 tk->b_muxed = VLC_TRUE;
571 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
574 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
576 tk->b_asf = VLC_TRUE;
577 if( p_sys->p_out_asf == NULL )
578 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
583 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
585 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
588 if( sub->rtcpInstance() != NULL )
590 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
593 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
595 tk->readSource = sub->readSource();
596 tk->rtpSource = sub->rtpSource();
599 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
600 p_sys->track[p_sys->i_track++] = tk;
605 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
612 if( p_sys->p_out_asf && ParseASF( p_demux ) )
614 msg_Err( p_demux, "cannot find a usable asf header" );
615 /* TODO Clean tracks */
619 if( p_sys->i_track <= 0 )
625 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
626 if( p_sys->ms ) Medium::close( p_sys->ms );
627 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
628 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
629 if( p_sys->p_timeout )
631 p_sys->p_timeout->b_die = VLC_TRUE;
632 vlc_thread_join( p_sys->p_timeout );
633 vlc_object_detach( p_sys->p_timeout );
634 vlc_object_destroy( p_sys->p_timeout );
636 if( p_sys->scheduler ) delete p_sys->scheduler;
637 if( p_sys->p_sdp ) free( p_sys->p_sdp );
638 if( p_sys->psz_path ) free( p_sys->psz_path );
644 /*****************************************************************************
646 *****************************************************************************/
647 static void Close( vlc_object_t *p_this )
649 demux_t *p_demux = (demux_t*)p_this;
650 demux_sys_t *p_sys = p_demux->p_sys;
653 for( i = 0; i < p_sys->i_track; i++ )
655 live_track_t *tk = p_sys->track[i];
657 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
658 free( tk->p_buffer );
662 if( p_sys->i_track ) free( p_sys->track );
663 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
665 if( p_sys->rtsp && p_sys->ms )
668 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
671 Medium::close( p_sys->ms );
673 if( p_sys->p_timeout )
675 p_sys->p_timeout->b_die = VLC_TRUE;
676 vlc_thread_join( p_sys->p_timeout );
677 vlc_object_detach( p_sys->p_timeout );
678 vlc_object_destroy( p_sys->p_timeout );
681 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
682 if( p_sys->env ) RECLAIM_ENV( p_sys->env );
683 if( p_sys->scheduler ) delete p_sys->scheduler;
684 if( p_sys->p_sdp ) free( p_sys->p_sdp );
685 if( p_sys->psz_path ) free( p_sys->psz_path );
689 /*****************************************************************************
690 * Connect: connects to the RTSP server to setup the session DESCRIBE
691 *****************************************************************************/
692 static int Connect( demux_t *p_demux )
694 demux_sys_t *p_sys = p_demux->p_sys;
695 Authenticator authenticator;
697 char *psz_user = NULL;
698 char *psz_pwd = NULL;
699 char *psz_url = NULL;
700 char *psz_options = NULL;
703 int i_ret = VLC_SUCCESS;
706 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
707 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
709 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
710 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
711 "VLC media player", i_http_port ) ) == NULL )
713 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
714 "VLC media player" ) ) == NULL )
717 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
718 p_sys->env->getResultMsg() );
721 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
722 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
724 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
725 if( psz_options ) delete [] psz_options;
727 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
728 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
731 authenticator.setUsernameAndPassword( (const char*)psz_user, (const char*)psz_pwd );
732 p_sdp = p_sys->rtsp->describeURL( psz_url,
733 &authenticator, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
735 if( psz_user ) free( psz_user );
736 if( psz_pwd ) free( psz_pwd );
740 /* failure occurred */
742 const char *psz_error = p_sys->env->getResultMsg();
744 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
745 sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
749 char *psz_login = NULL; char *psz_password = NULL;
750 msg_Dbg( p_demux, "authentication failed" );
752 i_ret = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
753 _("Please enter a valid login name and a password."),
754 &psz_login, &psz_password );
755 if( i_ret == DIALOG_OK_YES )
757 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
758 psz_login, psz_password );
760 if( psz_login ) psz_user = psz_login;
761 if( psz_password ) psz_pwd = psz_password;
764 if( psz_login ) free( psz_login );
765 if( psz_password ) free( psz_password );
767 else if( !var_CreateGetBool( p_demux, "rtsp-http" ) )
769 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
771 val.b_bool = VLC_TRUE;
772 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
773 var_Set( p_demux, "rtsp-http", val );
774 if( psz_url ) free( psz_url );
775 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
778 i_ret = VLC_EGENERIC;
780 if( psz_url ) free( psz_url );
782 /* malloc-ated copy */
783 if( p_sys->p_sdp ) free( p_sys->p_sdp );
784 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
790 /*****************************************************************************
791 * Connect: prepares the subsessions and does the SETUP
792 *****************************************************************************/
793 static int SessionsSetup( demux_t *p_demux )
795 demux_sys_t *p_sys = p_demux->p_sys;
796 MediaSubsessionIterator *iter = NULL;
797 MediaSubsession *sub = NULL;
799 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
801 int i_active_sessions = 0;
802 int i_return = VLC_SUCCESS;
803 unsigned int i_buffer = 0;
804 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
806 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
807 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
809 /* Initialise each media subsession */
810 iter = new MediaSubsessionIterator( *p_sys->ms );
811 while( ( sub = iter->next() ) != NULL )
815 /* Value taken from mplayer */
816 if( !strcmp( sub->mediumName(), "audio" ) )
818 else if( !strcmp( sub->mediumName(), "video" ) )
822 if( i_client_port != -1 )
824 sub->setClientPortNum( i_client_port );
828 if( strcasestr( sub->codecName(), "REAL" ) )
830 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
831 i_return = VLC_EGENERIC;
835 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
836 bInit = sub->initiate( 4 ); /* Constant ? */
838 bInit = sub->initiate();
842 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
843 sub->mediumName(), sub->codecName(),
844 p_sys->env->getResultMsg() );
848 if( sub->rtpSource() != NULL )
850 int fd = sub->rtpSource()->RTPgs()->socketNum();
852 /* Increase the buffer size */
854 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
856 /* Increase the RTP reorder timebuffer just a bit */
857 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
860 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
863 /* Issue the SETUP */
866 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
867 b_rtsp_tcp ? True : False ) ) )
869 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
870 sub->codecName(), p_sys->env->getResultMsg() );
872 else i_active_sessions++;
873 } else i_active_sessions++; /* we don't really know, let's just hope it's there */
875 if( !p_sys->b_multicast )
877 /* Check, because we need diff. rollover behaviour for multicast */
878 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
883 if( i_active_sessions <= 0 ) i_return = VLC_EGENERIC;
887 /*****************************************************************************
888 * Play: starts the actual playback of the stream
889 *****************************************************************************/
890 static int Play( demux_t *p_demux )
892 demux_sys_t *p_sys = p_demux->p_sys;
897 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
899 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
903 /* Retrieve the timeout value and set up a timeout prevention thread */
904 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
905 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
907 if( p_sys->i_timeout > 0 && !p_sys->p_timeout )
909 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
910 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
911 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
912 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
913 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
915 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
916 vlc_object_destroy( p_sys->p_timeout );
918 msg_Dbg( p_demux, "spawned timeout thread" );
919 vlc_object_attach( p_sys->p_timeout, p_demux );
926 /*****************************************************************************
928 *****************************************************************************/
929 static int Demux( demux_t *p_demux )
931 demux_sys_t *p_sys = p_demux->p_sys;
934 vlc_bool_t b_send_pcr = VLC_TRUE;
938 /* Check if we need to send the server a Keep-A-Live signal */
939 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
941 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
942 char *psz_bye = NULL;
943 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
945 p_sys->b_timeout_call = VLC_FALSE;
948 for( i = 0; i < p_sys->i_track; i++ )
950 live_track_t *tk = p_sys->track[i];
952 if( tk->b_asf || tk->b_muxed )
953 b_send_pcr = VLC_FALSE;
959 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
964 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
966 p_sys->i_pcr = i_pcr;
969 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
970 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
971 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
973 p_sys->i_pcr_start = i_pcr;
978 /* Disabled because it's simply not reliable enough */
980 /* When a On Demand QT stream ends, the last frame keeps going with the same PCR/PTS value */
981 /* This tests for that, so we can later decide to end this session */
982 if( i_pcr > 0 && p_sys->i_pcr == p_sys->i_pcr_previous )
984 if( p_sys->i_pcr_repeats == 0 )
985 p_sys->i_pcr_repeatdate = mdate();
986 p_sys->i_pcr_repeats++;
990 p_sys->i_pcr_previous = p_sys->i_pcr;
991 p_sys->i_pcr_repeatdate = 0;
992 p_sys->i_pcr_repeats = 0;
995 if( p_sys->i_pcr_repeats > 5 && mdate() > p_sys->i_pcr_repeatdate + 1000000 )
997 /* We need at least 5 repeats over at least a second of time before we EOF */
998 msg_Dbg( p_demux, "suspect EOF due to end of VoD session" );
1003 /* First warn we want to read data */
1005 for( i = 0; i < p_sys->i_track; i++ )
1007 live_track_t *tk = p_sys->track[i];
1009 if( tk->waiting == 0 )
1012 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
1013 StreamRead, tk, StreamClose, tk );
1016 /* Create a task that will be called if we wait more than 300ms */
1017 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1020 p_sys->scheduler->doEventLoop( &p_sys->event );
1022 /* remove the task */
1023 p_sys->scheduler->unscheduleDelayedTask( task );
1025 /* Check for gap in pts value */
1026 for( i = 0; i < p_sys->i_track; i++ )
1028 live_track_t *tk = p_sys->track[i];
1030 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1031 tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
1033 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1035 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1036 tk->b_rtcp_sync = VLC_TRUE;
1038 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
1040 p_sys->i_pcr_start = 0;
1046 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
1048 /* FIXME Make this configurable
1049 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1053 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1055 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
1057 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1059 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1060 if( RollOverTcp( p_demux ) )
1062 msg_Err( p_demux, "TCP rollover failed, aborting" );
1066 else if( p_sys->i_no_data_ti > 34 )
1068 msg_Err( p_demux, "no data received in 10s, aborting" );
1072 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1075 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1079 return p_demux->b_error ? 0 : 1;
1082 /*****************************************************************************
1084 *****************************************************************************/
1085 static int Control( demux_t *p_demux, int i_query, va_list args )
1087 demux_sys_t *p_sys = p_demux->p_sys;
1090 vlc_bool_t *pb, b_bool;
1094 case DEMUX_GET_TIME:
1095 pi64 = (int64_t*)va_arg( args, int64_t * );
1096 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
1099 case DEMUX_GET_LENGTH:
1100 pi64 = (int64_t*)va_arg( args, int64_t * );
1101 *pi64 = p_sys->i_length;
1104 case DEMUX_GET_POSITION:
1105 pf = (double*)va_arg( args, double* );
1106 if( p_sys->i_length > 0 )
1108 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
1109 p_sys->i_start ) / (double)(p_sys->i_length);
1117 case DEMUX_SET_POSITION:
1120 f = (double)va_arg( args, double );
1122 if( p_sys->rtsp && p_sys->i_length > 0 )
1124 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
1125 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
1127 msg_Err( p_demux, "PLAY failed %s",
1128 p_sys->env->getResultMsg() );
1129 return VLC_EGENERIC;
1131 p_sys->i_start = (int64_t)(f * (double)p_sys->i_length);
1132 p_sys->i_pcr_start = 0;
1139 /* Special for access_demux */
1140 case DEMUX_CAN_PAUSE:
1141 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1142 if( p_sys->rtsp && p_sys->i_length )
1143 /* Not always true, but will be handled in SET_PAUSE_STATE */
1149 case DEMUX_CAN_CONTROL_PACE:
1150 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1152 #if 0 /* Disable for now until we have a clock synchro algo
1153 * which works with something else than MPEG over UDP */
1160 case DEMUX_SET_PAUSE_STATE:
1163 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
1164 p_sys->i_start ) ) / 1000000.00;
1166 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1167 if( p_sys->rtsp == NULL )
1168 return VLC_EGENERIC;
1170 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1171 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1172 d_npt > 0 ? d_npt : -1 ) ) )
1174 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1175 return VLC_EGENERIC;
1178 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
1179 for( i = 0; i < p_sys->i_track; i++ )
1181 p_sys->track[i]->i_pts = 0;
1183 p_sys->i_pcr_start = 0; /* FIXME Wrong */
1188 case DEMUX_GET_TITLE_INFO:
1189 case DEMUX_SET_TITLE:
1190 case DEMUX_SET_SEEKPOINT:
1191 return VLC_EGENERIC;
1193 case DEMUX_GET_PTS_DELAY:
1194 pi64 = (int64_t*)va_arg( args, int64_t * );
1195 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1199 return VLC_EGENERIC;
1203 /*****************************************************************************
1204 * RollOverTcp: reopen the rtsp into TCP mode
1205 * XXX: ugly, a lot of code are duplicated from Open()
1206 *****************************************************************************/
1207 static int RollOverTcp( demux_t *p_demux )
1209 demux_sys_t *p_sys = p_demux->p_sys;
1210 MediaSubsessionIterator *iter = 0;
1211 MediaSubsession *sub;
1215 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1217 /* We close the old RTSP session */
1218 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1220 Medium::close( p_sys->ms );
1221 RTSPClient::close( p_sys->rtsp );
1226 /* Reopen rtsp client */
1227 if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1229 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
1233 if( p_sys->p_sdp == NULL )
1235 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1239 /* Create the session from the SDP */
1240 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
1242 msg_Err( p_demux, "Could not create the RTSP Session: %s",
1243 p_sys->env->getResultMsg() );
1247 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1249 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1253 /* Retrieve the duration if possible */
1254 p_sys->i_length = (int64_t)( p_sys->ms->playEndTime() * 1000000.0 );
1255 if( p_sys->i_length < 0 )
1256 p_sys->i_length = -1;
1258 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1261 /* Update all tracks */
1262 iter = new MediaSubsessionIterator( *p_sys->ms );
1264 while( ( sub = iter->next() ) != NULL )
1268 if( sub->readSource() == NULL )
1270 if( i_tk >= p_sys->i_track )
1272 msg_Err( p_demux, "WTF !" );
1276 tk = p_sys->track[i_tk];
1281 tk->b_rtcp_sync = VLC_FALSE;
1283 if( sub->rtcpInstance() != NULL )
1284 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1286 tk->readSource = sub->readSource();
1287 tk->rtpSource = sub->rtpSource();
1296 if( iter ) delete iter;
1297 return VLC_EGENERIC;
1301 /*****************************************************************************
1303 *****************************************************************************/
1304 static void StreamRead( void *p_private, unsigned int i_size,
1305 unsigned int i_truncated_bytes, struct timeval pts,
1306 unsigned int duration )
1308 live_track_t *tk = (live_track_t*)p_private;
1309 demux_t *p_demux = tk->p_demux;
1310 demux_sys_t *p_sys = p_demux->p_sys;
1313 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1314 (uint64_t)pts.tv_usec;
1316 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1317 i_pts &= UI64C(0x00ffffffffffffff);
1319 if( tk->b_quicktime && tk->p_es == NULL )
1321 QuickTimeGenericRTPSource *qtRTPSource =
1322 (QuickTimeGenericRTPSource*)tk->rtpSource;
1323 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1324 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1326 if( qtState.sdAtomSize < 16 + 32 )
1329 p_sys->event = 0xff;
1333 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1334 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1335 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1337 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1338 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1339 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1341 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1345 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1347 pts.tv_sec * 1000000LL + pts.tv_usec );
1350 /* grow buffer if it looks like buffer is too small, but don't eat
1351 * up all the memory on strange streams */
1352 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1355 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1356 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1358 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1361 msg_Warn( p_demux, "realloc failed" );
1365 tk->p_buffer = (uint8_t*)p_tmp;
1368 if( i_size > tk->i_buffer )
1370 msg_Warn( p_demux, "buffer overflow" );
1372 /* FIXME could i_size be > buffer size ? */
1373 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1374 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1376 AMRAudioSource *amrSource = (AMRAudioSource*)tk->readSource;
1378 p_block = block_New( p_demux, i_size + 1 );
1379 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1380 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1382 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1384 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1385 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1386 uint32_t header = h261Source->lastSpecialHeader();
1388 uint32_t header = 0;
1389 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1391 p_block = block_New( p_demux, i_size + 4 );
1392 memcpy( p_block->p_buffer, &header, 4 );
1393 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1395 if( tk->rtpSource->curPacketMarkerBit() )
1396 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1398 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1400 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1401 msg_Warn( p_demux, "unsupported NAL type for H264" );
1403 /* Normal NAL type */
1404 p_block = block_New( p_demux, i_size + 4 );
1405 p_block->p_buffer[0] = 0x00;
1406 p_block->p_buffer[1] = 0x00;
1407 p_block->p_buffer[2] = 0x00;
1408 p_block->p_buffer[3] = 0x01;
1409 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1411 else if( tk->b_asf )
1413 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1414 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1416 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1420 p_block = block_New( p_demux, i_size );
1421 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1424 if( i_pts != tk->i_pts && !tk->b_muxed )
1426 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1427 p_block->i_pts = i_pts;
1432 stream_DemuxSend( tk->p_out_muxed, p_block );
1434 else if( tk->b_asf )
1436 stream_DemuxSend( p_sys->p_out_asf, p_block );
1440 es_out_Send( p_demux->out, tk->p_es, p_block );
1443 /* warn that's ok */
1444 p_sys->event = 0xff;
1446 /* we have read data */
1448 p_demux->p_sys->b_no_data = VLC_FALSE;
1449 p_demux->p_sys->i_no_data_ti = 0;
1451 if( i_pts > 0 && !tk->b_muxed )
1457 /*****************************************************************************
1459 *****************************************************************************/
1460 static void StreamClose( void *p_private )
1462 live_track_t *tk = (live_track_t*)p_private;
1463 demux_t *p_demux = tk->p_demux;
1464 demux_sys_t *p_sys = p_demux->p_sys;
1466 msg_Dbg( p_demux, "StreamClose" );
1468 p_sys->event = 0xff;
1469 p_demux->b_error = VLC_TRUE;
1473 /*****************************************************************************
1475 *****************************************************************************/
1476 static void TaskInterrupt( void *p_private )
1478 demux_t *p_demux = (demux_t*)p_private;
1480 p_demux->p_sys->i_no_data_ti++;
1483 p_demux->p_sys->event = 0xff;
1486 /*****************************************************************************
1488 *****************************************************************************/
1489 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1491 p_timeout->b_die = VLC_FALSE;
1492 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1493 p_timeout->i_remain *= 1000000;
1495 vlc_thread_ready( p_timeout );
1498 while( !p_timeout->b_die )
1500 if( p_timeout->i_remain <= 0 )
1502 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1503 p_timeout->i_remain *= 1000000;
1504 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1505 msg_Dbg( p_timeout, "reset the timeout timer" );
1507 p_timeout->i_remain -= 200000;
1508 msleep( 200000 ); /* 200 ms */
1512 /*****************************************************************************
1514 *****************************************************************************/
1515 static int b64_decode( char *dest, char *src );
1517 static int ParseASF( demux_t *p_demux )
1519 demux_sys_t *p_sys = p_demux->p_sys;
1521 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1522 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1526 /* Parse the asf header */
1527 if( psz_asf == NULL )
1528 return VLC_EGENERIC;
1530 psz_asf += strlen( psz_marker );
1531 psz_asf = strdup( psz_asf ); /* Duplicate it */
1532 psz_end = strchr( psz_asf, '\n' );
1534 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1537 if( psz_asf >= psz_end )
1540 return VLC_EGENERIC;
1543 /* Always smaller */
1544 p_header = block_New( p_demux, psz_end - psz_asf );
1545 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1546 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1547 if( p_header->i_buffer <= 0 )
1550 return VLC_EGENERIC;
1553 /* Parse it to get packet size */
1554 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1556 /* Send it to demuxer */
1557 stream_DemuxSend( p_sys->p_out_asf, p_header );
1563 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
1564 static unsigned char* parseH264ConfigStr( char const* configStr,
1565 unsigned int& configSize )
1568 int i, i_records = 1;
1573 if( configStr == NULL || *configStr == '\0' )
1576 psz = dup = strdup( configStr );
1578 /* Count the number of comma's */
1579 for( psz = dup; *psz != '\0'; ++psz )
1588 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1590 for( i = 0; i < i_records; i++ )
1592 cfg[configSize++] = 0x00;
1593 cfg[configSize++] = 0x00;
1594 cfg[configSize++] = 0x00;
1595 cfg[configSize++] = 0x01;
1597 configSize += b64_decode( (char*)&cfg[configSize], psz );
1598 psz += strlen(psz)+1;
1601 if( dup ) free( dup );
1606 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1607 static int b64_decode( char *dest, char *src )
1609 const char *dest_start = dest;
1613 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1614 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1615 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1616 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1617 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1618 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1619 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1620 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1621 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1622 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1623 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1624 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1625 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1626 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1627 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1628 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1631 for( i_level = 0; *src != '\0'; src++ )
1635 c = b64[(unsigned int)*src];
1647 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1651 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1655 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1663 return dest - dest_start;