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",
496 else if( !strcmp( sub->mediumName(), "video" ) )
498 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
499 if( !strcmp( sub->codecName(), "MPV" ) )
501 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
503 else if( !strcmp( sub->codecName(), "H263" ) ||
504 !strcmp( sub->codecName(), "H263-1998" ) ||
505 !strcmp( sub->codecName(), "H263-2000" ) )
507 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
509 else if( !strcmp( sub->codecName(), "H261" ) )
511 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
513 else if( !strcmp( sub->codecName(), "H264" ) )
515 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
516 unsigned int i_extra = 0;
517 uint8_t *p_extra = NULL;
519 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
520 tk->fmt.b_packetized = VLC_FALSE;
522 /* XXX not the right minimal version I fear */
523 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
524 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
527 tk->fmt.i_extra = i_extra;
528 tk->fmt.p_extra = malloc( i_extra );
529 memcpy( tk->fmt.p_extra, p_extra, i_extra );
535 else if( !strcmp( sub->codecName(), "JPEG" ) )
537 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
539 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
541 unsigned int i_extra;
544 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
546 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
549 tk->fmt.i_extra = i_extra;
550 tk->fmt.p_extra = malloc( i_extra );
551 memcpy( tk->fmt.p_extra, p_extra, i_extra );
555 else if( !strcmp( sub->codecName(), "X-QT" ) ||
556 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
557 !strcmp( sub->codecName(), "X-QDM" ) ||
558 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
559 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
561 tk->b_quicktime = VLC_TRUE;
563 else if( !strcmp( sub->codecName(), "MP2T" ) )
565 tk->b_muxed = VLC_TRUE;
566 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
568 else if( !strcmp( sub->codecName(), "MP2P" ) ||
569 !strcmp( sub->codecName(), "MP1S" ) )
571 tk->b_muxed = VLC_TRUE;
572 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
575 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
577 tk->b_asf = VLC_TRUE;
578 if( p_sys->p_out_asf == NULL )
579 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
584 if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
586 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
589 if( sub->rtcpInstance() != NULL )
591 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
594 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
596 tk->readSource = sub->readSource();
597 tk->rtpSource = sub->rtpSource();
600 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
601 p_sys->track[p_sys->i_track++] = tk;
606 msg_Err( p_demux, "unusable RTSP track. this should not happen" );
613 if( p_sys->p_out_asf && ParseASF( p_demux ) )
615 msg_Err( p_demux, "cannot find a usable asf header" );
616 /* TODO Clean tracks */
620 if( p_sys->i_track <= 0 )
626 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
627 if( p_sys->ms ) Medium::close( p_sys->ms );
628 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
629 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
630 if( p_sys->p_timeout )
632 p_sys->p_timeout->b_die = VLC_TRUE;
633 vlc_thread_join( p_sys->p_timeout );
634 vlc_object_detach( p_sys->p_timeout );
635 vlc_object_destroy( p_sys->p_timeout );
637 if( p_sys->scheduler ) delete p_sys->scheduler;
638 if( p_sys->p_sdp ) free( p_sys->p_sdp );
639 if( p_sys->psz_path ) free( p_sys->psz_path );
645 /*****************************************************************************
647 *****************************************************************************/
648 static void Close( vlc_object_t *p_this )
650 demux_t *p_demux = (demux_t*)p_this;
651 demux_sys_t *p_sys = p_demux->p_sys;
654 for( i = 0; i < p_sys->i_track; i++ )
656 live_track_t *tk = p_sys->track[i];
658 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
659 free( tk->p_buffer );
663 if( p_sys->i_track ) free( p_sys->track );
664 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
666 if( p_sys->rtsp && p_sys->ms )
669 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
672 Medium::close( p_sys->ms );
674 if( p_sys->p_timeout )
676 p_sys->p_timeout->b_die = VLC_TRUE;
677 vlc_thread_join( p_sys->p_timeout );
678 vlc_object_detach( p_sys->p_timeout );
679 vlc_object_destroy( p_sys->p_timeout );
682 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
683 if( p_sys->env ) RECLAIM_ENV( p_sys->env );
684 if( p_sys->scheduler ) delete p_sys->scheduler;
685 if( p_sys->p_sdp ) free( p_sys->p_sdp );
686 if( p_sys->psz_path ) free( p_sys->psz_path );
690 /*****************************************************************************
691 * Connect: connects to the RTSP server to setup the session DESCRIBE
692 *****************************************************************************/
693 static int Connect( demux_t *p_demux )
695 demux_sys_t *p_sys = p_demux->p_sys;
696 Authenticator authenticator;
698 char *psz_user = NULL;
699 char *psz_pwd = NULL;
700 char *psz_url = NULL;
701 char *psz_options = NULL;
704 int i_ret = VLC_SUCCESS;
707 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
708 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
710 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
711 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
712 "VLC media player", i_http_port ) ) == NULL )
714 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
715 "VLC media player" ) ) == NULL )
718 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
719 p_sys->env->getResultMsg() );
722 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
723 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
725 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
726 if( psz_options ) delete [] psz_options;
728 psz_user = var_CreateGetString( p_demux, "rtsp-user" );
729 psz_pwd = var_CreateGetString( p_demux, "rtsp-pwd" );
732 authenticator.setUsernameAndPassword( (const char*)psz_user, (const char*)psz_pwd );
733 p_sdp = p_sys->rtsp->describeURL( psz_url,
734 &authenticator, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
736 if( psz_user ) free( psz_user );
737 if( psz_pwd ) free( psz_pwd );
741 /* failure occurred */
743 const char *psz_error = p_sys->env->getResultMsg();
745 msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
746 sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
750 char *psz_login = NULL; char *psz_password = NULL;
751 msg_Dbg( p_demux, "authentication failed" );
753 i_ret = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
754 _("Please enter a valid login name and a password."),
755 &psz_login, &psz_password );
756 if( i_ret == DIALOG_OK_YES )
758 msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
759 psz_login, psz_password );
761 if( psz_login ) psz_user = psz_login;
762 if( psz_password ) psz_pwd = psz_password;
765 if( psz_login ) free( psz_login );
766 if( psz_password ) free( psz_password );
768 else if( !var_CreateGetBool( p_demux, "rtsp-http" ) )
770 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
772 val.b_bool = VLC_TRUE;
773 msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
774 var_Set( p_demux, "rtsp-http", val );
775 if( psz_url ) free( psz_url );
776 if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
779 i_ret = VLC_EGENERIC;
781 if( psz_url ) free( psz_url );
783 /* malloc-ated copy */
784 if( p_sys->p_sdp ) free( p_sys->p_sdp );
785 if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
791 /*****************************************************************************
792 * Connect: prepares the subsessions and does the SETUP
793 *****************************************************************************/
794 static int SessionsSetup( demux_t *p_demux )
796 demux_sys_t *p_sys = p_demux->p_sys;
797 MediaSubsessionIterator *iter = NULL;
798 MediaSubsession *sub = NULL;
800 vlc_bool_t b_rtsp_tcp = VLC_FALSE;
802 int i_active_sessions = 0;
803 int i_return = VLC_SUCCESS;
804 unsigned int i_buffer = 0;
805 unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */
807 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
808 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
810 /* Initialise each media subsession */
811 iter = new MediaSubsessionIterator( *p_sys->ms );
812 while( ( sub = iter->next() ) != NULL )
816 /* Value taken from mplayer */
817 if( !strcmp( sub->mediumName(), "audio" ) )
819 else if( !strcmp( sub->mediumName(), "video" ) )
823 if( i_client_port != -1 )
825 sub->setClientPortNum( i_client_port );
829 if( strcasestr( sub->codecName(), "REAL" ) )
831 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
832 i_return = VLC_EGENERIC;
836 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
837 bInit = sub->initiate( 4 ); /* Constant ? */
839 bInit = sub->initiate();
843 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
844 sub->mediumName(), sub->codecName(),
845 p_sys->env->getResultMsg() );
849 if( sub->rtpSource() != NULL )
851 int fd = sub->rtpSource()->RTPgs()->socketNum();
853 /* Increase the buffer size */
855 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
857 /* Increase the RTP reorder timebuffer just a bit */
858 sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
861 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
864 /* Issue the SETUP */
867 if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
868 b_rtsp_tcp ? True : False ) ) )
870 msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
871 sub->codecName(), p_sys->env->getResultMsg() );
873 else i_active_sessions++;
874 } else i_active_sessions++; /* we don't really know, let's just hope it's there */
876 if( !p_sys->b_multicast )
878 /* Check, because we need diff. rollover behaviour for multicast */
879 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
884 if( i_active_sessions <= 0 ) i_return = VLC_EGENERIC;
888 /*****************************************************************************
889 * Play: starts the actual playback of the stream
890 *****************************************************************************/
891 static int Play( demux_t *p_demux )
893 demux_sys_t *p_sys = p_demux->p_sys;
898 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
900 msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
904 /* Retrieve the timeout value and set up a timeout prevention thread */
905 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
906 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
908 if( p_sys->i_timeout > 0 && !p_sys->p_timeout )
910 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
911 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
912 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
913 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
914 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
916 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
917 vlc_object_destroy( p_sys->p_timeout );
919 msg_Dbg( p_demux, "spawned timeout thread" );
920 vlc_object_attach( p_sys->p_timeout, p_demux );
927 /*****************************************************************************
929 *****************************************************************************/
930 static int Demux( demux_t *p_demux )
932 demux_sys_t *p_sys = p_demux->p_sys;
935 vlc_bool_t b_send_pcr = VLC_TRUE;
939 /* Check if we need to send the server a Keep-A-Live signal */
940 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
942 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
943 char *psz_bye = NULL;
944 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
946 p_sys->b_timeout_call = VLC_FALSE;
949 for( i = 0; i < p_sys->i_track; i++ )
951 live_track_t *tk = p_sys->track[i];
953 if( tk->b_asf || tk->b_muxed )
954 b_send_pcr = VLC_FALSE;
960 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
965 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
967 p_sys->i_pcr = i_pcr;
970 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
971 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
972 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
974 p_sys->i_pcr_start = i_pcr;
979 /* Disabled because it's simply not reliable enough */
981 /* When a On Demand QT stream ends, the last frame keeps going with the same PCR/PTS value */
982 /* This tests for that, so we can later decide to end this session */
983 if( i_pcr > 0 && p_sys->i_pcr == p_sys->i_pcr_previous )
985 if( p_sys->i_pcr_repeats == 0 )
986 p_sys->i_pcr_repeatdate = mdate();
987 p_sys->i_pcr_repeats++;
991 p_sys->i_pcr_previous = p_sys->i_pcr;
992 p_sys->i_pcr_repeatdate = 0;
993 p_sys->i_pcr_repeats = 0;
996 if( p_sys->i_pcr_repeats > 5 && mdate() > p_sys->i_pcr_repeatdate + 1000000 )
998 /* We need at least 5 repeats over at least a second of time before we EOF */
999 msg_Dbg( p_demux, "suspect EOF due to end of VoD session" );
1004 /* First warn we want to read data */
1006 for( i = 0; i < p_sys->i_track; i++ )
1008 live_track_t *tk = p_sys->track[i];
1010 if( tk->waiting == 0 )
1013 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
1014 StreamRead, tk, StreamClose, tk );
1017 /* Create a task that will be called if we wait more than 300ms */
1018 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1021 p_sys->scheduler->doEventLoop( &p_sys->event );
1023 /* remove the task */
1024 p_sys->scheduler->unscheduleDelayedTask( task );
1026 /* Check for gap in pts value */
1027 for( i = 0; i < p_sys->i_track; i++ )
1029 live_track_t *tk = p_sys->track[i];
1031 if( !tk->b_muxed && !tk->b_rtcp_sync &&
1032 tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
1034 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1036 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1037 tk->b_rtcp_sync = VLC_TRUE;
1039 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
1041 p_sys->i_pcr_start = 0;
1047 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
1049 /* FIXME Make this configurable
1050 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1054 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1056 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
1058 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
1060 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1061 if( RollOverTcp( p_demux ) )
1063 msg_Err( p_demux, "TCP rollover failed, aborting" );
1067 else if( p_sys->i_no_data_ti > 34 )
1069 msg_Err( p_demux, "no data received in 10s, aborting" );
1073 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1076 msg_Warn( p_demux, "no data received in 10s, eof ?" );
1080 return p_demux->b_error ? 0 : 1;
1083 /*****************************************************************************
1085 *****************************************************************************/
1086 static int Control( demux_t *p_demux, int i_query, va_list args )
1088 demux_sys_t *p_sys = p_demux->p_sys;
1091 vlc_bool_t *pb, b_bool;
1095 case DEMUX_GET_TIME:
1096 pi64 = (int64_t*)va_arg( args, int64_t * );
1097 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
1100 case DEMUX_GET_LENGTH:
1101 pi64 = (int64_t*)va_arg( args, int64_t * );
1102 *pi64 = p_sys->i_length;
1105 case DEMUX_GET_POSITION:
1106 pf = (double*)va_arg( args, double* );
1107 if( p_sys->i_length > 0 )
1109 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
1110 p_sys->i_start ) / (double)(p_sys->i_length);
1118 case DEMUX_SET_POSITION:
1121 f = (double)va_arg( args, double );
1123 if( p_sys->rtsp && p_sys->i_length > 0 )
1125 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
1126 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
1128 msg_Err( p_demux, "PLAY failed %s",
1129 p_sys->env->getResultMsg() );
1130 return VLC_EGENERIC;
1132 p_sys->i_start = (int64_t)(f * (double)p_sys->i_length);
1133 p_sys->i_pcr_start = 0;
1140 /* Special for access_demux */
1141 case DEMUX_CAN_PAUSE:
1142 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1143 if( p_sys->rtsp && p_sys->i_length )
1144 /* Not always true, but will be handled in SET_PAUSE_STATE */
1150 case DEMUX_CAN_CONTROL_PACE:
1151 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1153 #if 0 /* Disable for now until we have a clock synchro algo
1154 * which works with something else than MPEG over UDP */
1161 case DEMUX_SET_PAUSE_STATE:
1164 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
1165 p_sys->i_start ) ) / 1000000.00;
1167 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1168 if( p_sys->rtsp == NULL )
1169 return VLC_EGENERIC;
1171 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1172 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1173 d_npt > 0 ? d_npt : -1 ) ) )
1175 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1176 return VLC_EGENERIC;
1179 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
1180 for( i = 0; i < p_sys->i_track; i++ )
1182 p_sys->track[i]->i_pts = 0;
1184 p_sys->i_pcr_start = 0; /* FIXME Wrong */
1189 case DEMUX_GET_TITLE_INFO:
1190 case DEMUX_SET_TITLE:
1191 case DEMUX_SET_SEEKPOINT:
1192 return VLC_EGENERIC;
1194 case DEMUX_GET_PTS_DELAY:
1195 pi64 = (int64_t*)va_arg( args, int64_t * );
1196 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1200 return VLC_EGENERIC;
1204 /*****************************************************************************
1205 * RollOverTcp: reopen the rtsp into TCP mode
1206 * XXX: ugly, a lot of code are duplicated from Open()
1207 *****************************************************************************/
1208 static int RollOverTcp( demux_t *p_demux )
1210 demux_sys_t *p_sys = p_demux->p_sys;
1211 MediaSubsessionIterator *iter = 0;
1212 MediaSubsession *sub;
1216 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
1218 /* We close the old RTSP session */
1219 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1221 Medium::close( p_sys->ms );
1222 RTSPClient::close( p_sys->rtsp );
1227 /* Reopen rtsp client */
1228 if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
1230 msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
1234 if( p_sys->p_sdp == NULL )
1236 msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
1240 /* Create the session from the SDP */
1241 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
1243 msg_Err( p_demux, "Could not create the RTSP Session: %s",
1244 p_sys->env->getResultMsg() );
1248 if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
1250 msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
1254 /* Retrieve the duration if possible */
1255 p_sys->i_length = (int64_t)( p_sys->ms->playEndTime() * 1000000.0 );
1256 if( p_sys->i_length < 0 )
1257 p_sys->i_length = -1;
1259 if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
1262 /* Update all tracks */
1263 iter = new MediaSubsessionIterator( *p_sys->ms );
1265 while( ( sub = iter->next() ) != NULL )
1269 if( sub->readSource() == NULL )
1271 if( i_tk >= p_sys->i_track )
1273 msg_Err( p_demux, "WTF !" );
1277 tk = p_sys->track[i_tk];
1282 tk->b_rtcp_sync = VLC_FALSE;
1284 if( sub->rtcpInstance() != NULL )
1285 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1287 tk->readSource = sub->readSource();
1288 tk->rtpSource = sub->rtpSource();
1297 if( iter ) delete iter;
1298 return VLC_EGENERIC;
1302 /*****************************************************************************
1304 *****************************************************************************/
1305 static void StreamRead( void *p_private, unsigned int i_size,
1306 unsigned int i_truncated_bytes, struct timeval pts,
1307 unsigned int duration )
1309 live_track_t *tk = (live_track_t*)p_private;
1310 demux_t *p_demux = tk->p_demux;
1311 demux_sys_t *p_sys = p_demux->p_sys;
1314 int64_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1315 (uint64_t)pts.tv_usec;
1317 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1318 i_pts &= UI64C(0x00ffffffffffffff);
1320 if( tk->b_quicktime && tk->p_es == NULL )
1322 QuickTimeGenericRTPSource *qtRTPSource =
1323 (QuickTimeGenericRTPSource*)tk->rtpSource;
1324 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1325 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1327 if( qtState.sdAtomSize < 16 + 32 )
1330 p_sys->event = 0xff;
1334 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1335 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1336 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1338 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1339 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1340 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1342 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1346 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1348 pts.tv_sec * 1000000LL + pts.tv_usec );
1351 /* grow buffer if it looks like buffer is too small, but don't eat
1352 * up all the memory on strange streams */
1353 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1356 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1357 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1359 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1362 msg_Warn( p_demux, "realloc failed" );
1366 tk->p_buffer = (uint8_t*)p_tmp;
1369 if( i_size > tk->i_buffer )
1371 msg_Warn( p_demux, "buffer overflow" );
1373 /* FIXME could i_size be > buffer size ? */
1374 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1375 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1377 AMRAudioSource *amrSource = (AMRAudioSource*)tk->readSource;
1379 p_block = block_New( p_demux, i_size + 1 );
1380 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1381 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1383 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1385 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1386 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1387 uint32_t header = h261Source->lastSpecialHeader();
1389 uint32_t header = 0;
1390 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1392 p_block = block_New( p_demux, i_size + 4 );
1393 memcpy( p_block->p_buffer, &header, 4 );
1394 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1396 if( tk->rtpSource->curPacketMarkerBit() )
1397 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1399 else if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','4') )
1401 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1402 msg_Warn( p_demux, "unsupported NAL type for H264" );
1404 /* Normal NAL type */
1405 p_block = block_New( p_demux, i_size + 4 );
1406 p_block->p_buffer[0] = 0x00;
1407 p_block->p_buffer[1] = 0x00;
1408 p_block->p_buffer[2] = 0x00;
1409 p_block->p_buffer[3] = 0x01;
1410 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1412 else if( tk->b_asf )
1414 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1415 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1417 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1421 p_block = block_New( p_demux, i_size );
1422 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1425 if( i_pts != tk->i_pts && !tk->b_muxed )
1427 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1428 p_block->i_pts = i_pts;
1433 stream_DemuxSend( tk->p_out_muxed, p_block );
1435 else if( tk->b_asf )
1437 stream_DemuxSend( p_sys->p_out_asf, p_block );
1441 es_out_Send( p_demux->out, tk->p_es, p_block );
1444 /* warn that's ok */
1445 p_sys->event = 0xff;
1447 /* we have read data */
1449 p_demux->p_sys->b_no_data = VLC_FALSE;
1450 p_demux->p_sys->i_no_data_ti = 0;
1452 if( i_pts > 0 && !tk->b_muxed )
1458 /*****************************************************************************
1460 *****************************************************************************/
1461 static void StreamClose( void *p_private )
1463 live_track_t *tk = (live_track_t*)p_private;
1464 demux_t *p_demux = tk->p_demux;
1465 demux_sys_t *p_sys = p_demux->p_sys;
1467 msg_Dbg( p_demux, "StreamClose" );
1469 p_sys->event = 0xff;
1470 p_demux->b_error = VLC_TRUE;
1474 /*****************************************************************************
1476 *****************************************************************************/
1477 static void TaskInterrupt( void *p_private )
1479 demux_t *p_demux = (demux_t*)p_private;
1481 p_demux->p_sys->i_no_data_ti++;
1484 p_demux->p_sys->event = 0xff;
1487 /*****************************************************************************
1489 *****************************************************************************/
1490 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1492 p_timeout->b_die = VLC_FALSE;
1493 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1494 p_timeout->i_remain *= 1000000;
1496 vlc_thread_ready( p_timeout );
1499 while( !p_timeout->b_die )
1501 if( p_timeout->i_remain <= 0 )
1503 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1504 p_timeout->i_remain *= 1000000;
1505 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1506 msg_Dbg( p_timeout, "reset the timeout timer" );
1508 p_timeout->i_remain -= 200000;
1509 msleep( 200000 ); /* 200 ms */
1513 /*****************************************************************************
1515 *****************************************************************************/
1516 static int b64_decode( char *dest, char *src );
1518 static int ParseASF( demux_t *p_demux )
1520 demux_sys_t *p_sys = p_demux->p_sys;
1522 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1523 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1527 /* Parse the asf header */
1528 if( psz_asf == NULL )
1529 return VLC_EGENERIC;
1531 psz_asf += strlen( psz_marker );
1532 psz_asf = strdup( psz_asf ); /* Duplicate it */
1533 psz_end = strchr( psz_asf, '\n' );
1535 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1538 if( psz_asf >= psz_end )
1541 return VLC_EGENERIC;
1544 /* Always smaller */
1545 p_header = block_New( p_demux, psz_end - psz_asf );
1546 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1547 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1548 if( p_header->i_buffer <= 0 )
1551 return VLC_EGENERIC;
1554 /* Parse it to get packet size */
1555 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1557 /* Send it to demuxer */
1558 stream_DemuxSend( p_sys->p_out_asf, p_header );
1564 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
1565 static unsigned char* parseH264ConfigStr( char const* configStr,
1566 unsigned int& configSize )
1569 int i, i_records = 1;
1574 if( configStr == NULL || *configStr == '\0' )
1577 psz = dup = strdup( configStr );
1579 /* Count the number of comma's */
1580 for( psz = dup; *psz != '\0'; ++psz )
1589 unsigned char *cfg = new unsigned char[5 * strlen(dup)];
1591 for( i = 0; i < i_records; i++ )
1593 cfg[configSize++] = 0x00;
1594 cfg[configSize++] = 0x00;
1595 cfg[configSize++] = 0x00;
1596 cfg[configSize++] = 0x01;
1598 configSize += b64_decode( (char*)&cfg[configSize], psz );
1599 psz += strlen(psz)+1;
1602 if( dup ) free( dup );
1607 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1608 static int b64_decode( char *dest, char *src )
1610 const char *dest_start = dest;
1614 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1615 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1616 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1617 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1618 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1619 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1620 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1621 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1622 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1623 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1624 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1625 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1626 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1627 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1628 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1629 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1632 for( i_level = 0; *src != '\0'; src++ )
1636 c = b64[(unsigned int)*src];
1648 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1652 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1656 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1664 return dest - dest_start;