1 /*****************************************************************************
2 * livedotcom.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>
36 # include <winsock2.h>
39 #include "BasicUsageEnvironment.hh"
40 #include "GroupsockHelper.hh"
41 #include "liveMedia.hh"
44 #include "../access/mms/asf.h" /* Who said ugly ? */
47 #if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
48 #define RECLAIM_ENV(env) delete (env)
50 #define RECLAIM_ENV(env) (env)->reclaim()
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 #define CACHING_TEXT N_("Caching value (ms)")
62 #define CACHING_LONGTEXT N_( \
63 "Allows you to modify the default caching value for RTSP streams. This " \
64 "value should be set in millisecond units." )
66 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
67 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
68 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
69 "for communication. In this mode you cannot connect to normal RTSP servers." )
72 /// \bug [String] s,live555.com,live555
73 set_description( _("RTP/RTSP/SDP demuxer (using Live555.com)" ) );
74 set_capability( "demux2", 50 );
75 set_shortname( "RTP/RTSP");
76 set_callbacks( Open, Close );
77 add_shortcut( "live" );
78 set_category( CAT_INPUT );
79 set_subcategory( SUBCAT_INPUT_DEMUX );
82 set_description( _("RTSP/RTP access and demux") );
83 add_shortcut( "rtsp" );
84 add_shortcut( "sdp" );
85 set_capability( "access_demux", 0 );
86 set_callbacks( Open, Close );
87 add_bool( "rtsp-tcp", 0, NULL,
88 N_("Use RTP over RTSP (TCP)"),
89 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
90 add_integer( "rtp-client-port", -1, NULL,
92 N_("Port to use for the RTP source of the session"), VLC_TRUE );
93 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
94 add_bool( "rtsp-http", 0, NULL,
95 N_("Tunnel RTSP and RTP over HTTP"),
96 N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE );
97 add_integer( "rtsp-http-port", 80, NULL,
98 N_("HTTP tunnel port"),
99 /// \bug [String] Missing .
100 N_("Port to use for tunneling the RTSP/RTP over HTTP"), VLC_TRUE );
102 add_integer( "rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
103 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
104 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
105 KASENNA_LONGTEXT, VLC_TRUE );
109 * - Improve support of PS/TS
110 * - Support X-QT/X-QUICKTIME generic codec for audio.
112 * - Check memory leak, delete/free -> still one when using rtsp-tcp but I'm
113 * not sure if it comes from me.
117 /*****************************************************************************
119 *****************************************************************************/
125 vlc_bool_t b_quicktime;
132 stream_t *p_out_muxed; /* for muxed stream */
134 RTPSource *rtpSource;
135 FramedSource *readSource;
136 vlc_bool_t b_rtcp_sync;
139 unsigned int i_buffer;
147 struct timeout_thread_t
157 char *p_sdp; /* XXX mallocated */
158 char *psz_path; /* URL-encoded path */
161 TaskScheduler *scheduler;
162 UsageEnvironment *env ;
167 live_track_t **track; /* XXX mallocated */
170 mtime_t i_pcr_previous;
171 mtime_t i_pcr_repeatdate;
182 /* timeout thread information */
183 int i_timeout; /* session timeout value in seconds */
184 vlc_bool_t b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
185 timeout_thread_t *p_timeout; /* the actual thread that makes sure we don't timeout */
188 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
189 vlc_bool_t b_no_data; /* true if we never receive any data */
190 int i_no_data_ti; /* consecutive number of TaskInterrupt */
195 static int Demux ( demux_t * );
196 static int Control( demux_t *, int, va_list );
198 static int ParseASF( demux_t * );
199 static int RollOverTcp( demux_t * );
201 static void StreamRead( void *, unsigned int, unsigned int,
202 struct timeval, unsigned int );
203 static void StreamClose( void * );
204 static void TaskInterrupt( void * );
206 static void TimeoutPrevention( timeout_thread_t * );
208 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
209 static unsigned char* parseH264ConfigStr( char const* configStr,
210 unsigned int& configSize );
213 /*****************************************************************************
215 *****************************************************************************/
216 static int Open ( vlc_object_t *p_this )
218 demux_t *p_demux = (demux_t*)p_this;
219 demux_sys_t *p_sys = NULL;
221 MediaSubsessionIterator *iter;
222 MediaSubsession *sub;
224 vlc_bool_t b_rtsp_tcp;
233 /* See if it looks like a SDP
234 v, o, s fields are mandatory and in this order */
235 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
237 if( memcmp( (char*)p_peek, "v=0\r\n", 5 ) &&
238 memcmp( (char*)p_peek, "v=0\n", 4 ) &&
239 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
246 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
249 p_demux->pf_demux = Demux;
250 p_demux->pf_control= Control;
251 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
252 if( !p_sys ) return VLC_ENOMEM;
255 p_sys->scheduler = NULL;
262 p_sys->i_pcr_start = 0;
263 p_sys->i_pcr_previous = 0;
264 p_sys->i_pcr_repeatdate = 0;
265 p_sys->i_pcr_repeats = 0;
268 p_sys->p_out_asf = NULL;
269 p_sys->b_no_data = VLC_TRUE;
270 p_sys->i_no_data_ti = 0;
271 p_sys->p_timeout = NULL;
272 p_sys->i_timeout = 0;
273 p_sys->b_timeout_call = VLC_FALSE;
274 p_sys->b_multicast = VLC_FALSE;
275 p_sys->psz_path = strdup(p_demux->psz_path);
278 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
280 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
283 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
285 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
289 if( strcasecmp( p_demux->psz_access, "sdp" ) )
291 char *p = p_sys->psz_path;
292 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
295 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
299 #if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
302 if( var_CreateGetBool( p_demux, "rtsp-http" ) )
303 i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );
305 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
306 "VLC media player", i_http_port ) ) == NULL )
308 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
309 "VLC media player" ) ) == NULL )
312 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
313 p_sys->env->getResultMsg() );
316 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
317 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
319 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
320 if( psz_options ) delete [] psz_options;
322 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
323 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) ) ;
326 msg_Err( p_demux, "describeURL failed (%s)",
327 p_sys->env->getResultMsg() );
333 /* malloc-ated copy */
334 p_sys->p_sdp = strdup( (char*)p_sdp );
336 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
338 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
340 p_sys->p_sdp = strdup( p_sys->psz_path );
344 /* Gather the complete sdp file */
347 p_sdp = (uint8_t*)malloc( i_sdp_max );
350 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
351 i_sdp_max - i_sdp - 1 );
355 msg_Err( p_demux, "failed to read SDP" );
362 if( i_read < i_sdp_max - i_sdp - 1 )
369 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
371 p_sys->p_sdp = (char*)p_sdp;
373 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
375 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
377 msg_Err( p_demux, "MediaSession::createNew failed" );
381 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
383 /* Initialise each media subsession */
384 iter = new MediaSubsessionIterator( *p_sys->ms );
385 while( ( sub = iter->next() ) != NULL )
387 unsigned int i_buffer = 0;
391 i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );
392 if( i_client_port != -1 )
393 sub->setClientPortNum( i_client_port );
395 /* Value taken from mplayer */
396 if( !strcmp( sub->mediumName(), "audio" ) )
398 else if( !strcmp( sub->mediumName(), "video" ) )
403 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
404 bInit = sub->initiate( 4 ); /* Constant ? */
406 bInit = sub->initiate();
408 if( strcasestr( sub->codecName(), "REAL" ) )
410 msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
417 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
418 sub->mediumName(), sub->codecName(),
419 p_sys->env->getResultMsg() );
423 if( sub->rtpSource() )
425 int fd = sub->rtpSource()->RTPgs()->socketNum();
426 /* Increase the buffer size */
427 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
430 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
433 /* Issue the SETUP */
436 p_sys->rtsp->setupMediaSubsession( *sub, False,
437 b_rtsp_tcp ? True : False );
439 if( !p_sys->b_multicast )
441 /* Check, because we need diff. rollover behaviour for multicast */
442 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
450 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
452 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
457 /* Retrieve the timeout value and set up a timeout prevention thread */
458 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
459 p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
461 if( p_sys->i_timeout > 0 )
463 msg_Dbg( p_demux, "We have a timeout of %d seconds", p_sys->i_timeout );
464 p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
465 p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
466 if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
467 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
469 msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
471 vlc_object_destroy( p_sys->p_timeout );
474 msg_Dbg( p_demux, "spawned timeout thread" );
475 vlc_object_attach( p_sys->p_timeout, p_demux );
479 /* Create all es struct */
481 while( ( sub = iter->next() ) != NULL )
485 if( sub->readSource() == NULL ) continue;
487 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
488 tk->p_demux = p_demux;
491 tk->b_quicktime = VLC_FALSE;
492 tk->b_muxed = VLC_FALSE;
493 tk->b_asf = VLC_FALSE;
494 tk->b_rtcp_sync = VLC_FALSE;
495 tk->p_out_muxed = NULL;
497 tk->i_buffer = 65536;
498 tk->p_buffer = (uint8_t *)malloc( 65536 );
500 /* Value taken from mplayer */
501 if( !strcmp( sub->mediumName(), "audio" ) )
503 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
504 tk->fmt.audio.i_channels = sub->numChannels();
505 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
507 if( !strcmp( sub->codecName(), "MPA" ) ||
508 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
509 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
511 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
512 tk->fmt.audio.i_rate = 0;
514 else if( !strcmp( sub->codecName(), "AC3" ) )
516 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
517 tk->fmt.audio.i_rate = 0;
519 else if( !strcmp( sub->codecName(), "L16" ) )
521 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
522 tk->fmt.audio.i_bitspersample = 16;
524 else if( !strcmp( sub->codecName(), "L8" ) )
526 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
527 tk->fmt.audio.i_bitspersample = 8;
529 else if( !strcmp( sub->codecName(), "PCMU" ) )
531 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
533 else if( !strcmp( sub->codecName(), "PCMA" ) )
535 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
537 else if( !strncmp( sub->codecName(), "G726", 4 ) )
539 tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' );
540 tk->fmt.audio.i_rate = 8000;
541 tk->fmt.audio.i_channels = 1;
542 if( !strcmp( sub->codecName()+5, "40" ) )
543 tk->fmt.i_bitrate = 40000;
544 else if( !strcmp( sub->codecName()+5, "32" ) )
545 tk->fmt.i_bitrate = 32000;
546 else if( !strcmp( sub->codecName()+5, "24" ) )
547 tk->fmt.i_bitrate = 24000;
548 else if( !strcmp( sub->codecName()+5, "16" ) )
549 tk->fmt.i_bitrate = 16000;
551 else if( !strcmp( sub->codecName(), "AMR" ) )
553 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
555 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
557 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
559 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
561 unsigned int i_extra;
564 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
566 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
569 tk->fmt.i_extra = i_extra;
570 tk->fmt.p_extra = malloc( i_extra );
571 memcpy( tk->fmt.p_extra, p_extra, i_extra );
575 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1141257600
576 /* Because the "faad" decoder does not handle the LATM data length field
577 at the start of each returned LATM frame, tell the RTP source to omit it. */
578 ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
581 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
583 unsigned int i_extra;
586 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
588 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
591 tk->fmt.i_extra = i_extra;
592 tk->fmt.p_extra = malloc( i_extra );
593 memcpy( tk->fmt.p_extra, p_extra, i_extra );
597 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
599 tk->b_asf = VLC_TRUE;
600 if( p_sys->p_out_asf == NULL )
601 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
605 else if( !strcmp( sub->mediumName(), "video" ) )
607 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
608 if( !strcmp( sub->codecName(), "MPV" ) )
610 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
612 else if( !strcmp( sub->codecName(), "H263" ) ||
613 !strcmp( sub->codecName(), "H263-1998" ) ||
614 !strcmp( sub->codecName(), "H263-2000" ) )
616 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
618 else if( !strcmp( sub->codecName(), "H261" ) )
620 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
622 else if( !strcmp( sub->codecName(), "H264" ) )
624 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
625 unsigned int i_extra = 0;
626 uint8_t *p_extra = NULL;
628 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '4' );
629 tk->fmt.b_packetized = VLC_FALSE;
631 /* XXX not the right minimal version I fear */
632 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
633 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
636 tk->fmt.i_extra = i_extra;
637 tk->fmt.p_extra = malloc( i_extra );
638 memcpy( tk->fmt.p_extra, p_extra, i_extra );
644 else if( !strcmp( sub->codecName(), "JPEG" ) )
646 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
648 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
650 unsigned int i_extra;
653 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
655 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
658 tk->fmt.i_extra = i_extra;
659 tk->fmt.p_extra = malloc( i_extra );
660 memcpy( tk->fmt.p_extra, p_extra, i_extra );
664 else if( !strcmp( sub->codecName(), "X-QT" ) ||
665 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
666 !strcmp( sub->codecName(), "X-QDM" ) ||
667 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
668 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
670 tk->b_quicktime = VLC_TRUE;
672 else if( !strcmp( sub->codecName(), "MP2T" ) )
674 tk->b_muxed = VLC_TRUE;
675 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
677 else if( !strcmp( sub->codecName(), "MP2P" ) ||
678 !strcmp( sub->codecName(), "MP1S" ) )
680 tk->b_muxed = VLC_TRUE;
681 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
684 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
686 tk->b_asf = VLC_TRUE;
687 if( p_sys->p_out_asf == NULL )
688 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
693 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
695 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
698 if( sub->rtcpInstance() != NULL )
700 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
703 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
705 tk->readSource = sub->readSource();
706 tk->rtpSource = sub->rtpSource();
709 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
710 p_sys->track[p_sys->i_track++] = tk;
720 if( p_sys->p_out_asf && ParseASF( p_demux ) )
722 msg_Err( p_demux, "cannot find a usable asf header" );
723 /* TODO Clean tracks */
727 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
728 if( p_sys->i_length < 0 )
732 else if( p_sys->i_length > 0 )
735 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
738 if( p_sys->i_track <= 0 )
740 msg_Err( p_demux, "no codec supported, aborting" );
747 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
748 if( p_sys->ms ) Medium::close( p_sys->ms );
749 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
750 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
751 if( p_sys->p_timeout )
753 p_sys->p_timeout->b_die = VLC_TRUE;
754 vlc_thread_join( p_sys->p_timeout );
755 vlc_object_detach( p_sys->p_timeout );
756 vlc_object_destroy( p_sys->p_timeout );
758 if( p_sys->scheduler ) delete p_sys->scheduler;
759 if( p_sys->p_sdp ) free( p_sys->p_sdp );
760 if( p_sys->psz_path ) free( p_sys->psz_path );
766 /*****************************************************************************
768 *****************************************************************************/
769 static void Close( vlc_object_t *p_this )
771 demux_t *p_demux = (demux_t*)p_this;
772 demux_sys_t *p_sys = p_demux->p_sys;
775 for( i = 0; i < p_sys->i_track; i++ )
777 live_track_t *tk = p_sys->track[i];
779 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
780 free( tk->p_buffer );
784 if( p_sys->i_track ) free( p_sys->track );
785 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
787 if( p_sys->rtsp && p_sys->ms )
790 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
793 Medium::close( p_sys->ms );
795 if( p_sys->p_timeout )
797 p_sys->p_timeout->b_die = VLC_TRUE;
798 vlc_thread_join( p_sys->p_timeout );
799 vlc_object_detach( p_sys->p_timeout );
800 vlc_object_destroy( p_sys->p_timeout );
803 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
804 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
805 if( p_sys->scheduler ) delete p_sys->scheduler;
806 if( p_sys->p_sdp ) free( p_sys->p_sdp );
807 if( p_sys->psz_path ) free( p_sys->psz_path );
811 /*****************************************************************************
813 *****************************************************************************/
814 static int Demux( demux_t *p_demux )
816 demux_sys_t *p_sys = p_demux->p_sys;
819 vlc_bool_t b_send_pcr = VLC_TRUE;
823 /* Check if we need to send the server a Keep-A-Live signal */
824 if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
826 char *psz_bye = NULL;
827 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
828 p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
830 p_sys->b_timeout_call = VLC_FALSE;
833 for( i = 0; i < p_sys->i_track; i++ )
835 live_track_t *tk = p_sys->track[i];
837 if( tk->b_asf || tk->b_muxed )
838 b_send_pcr = VLC_FALSE;
844 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
849 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
851 p_sys->i_pcr = i_pcr;
854 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
855 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
856 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
858 p_sys->i_pcr_start = i_pcr;
862 /* When a On Demand QT stream ends, the last frame keeps going with the same PCR/PTS value */
863 /* This tests for that, so we can later decide to end this session */
864 if( i_pcr > 0 && p_sys->i_pcr == p_sys->i_pcr_previous )
866 if( p_sys->i_pcr_repeats == 0 )
867 p_sys->i_pcr_repeatdate = mdate();
868 p_sys->i_pcr_repeats++;
872 p_sys->i_pcr_previous = p_sys->i_pcr;
873 p_sys->i_pcr_repeatdate = 0;
874 p_sys->i_pcr_repeats = 0;
877 if( p_sys->i_pcr_repeats > 5 && mdate() > p_sys->i_pcr_repeatdate + 1000000 )
879 /* We need at least 5 repeats over at least a second of time before we EOF */
880 msg_Dbg( p_demux, "suspect EOF due to end of VoD session" );
884 /* First warm we want to read data */
886 for( i = 0; i < p_sys->i_track; i++ )
888 live_track_t *tk = p_sys->track[i];
890 if( tk->waiting == 0 )
893 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
894 StreamRead, tk, StreamClose, tk );
897 /* Create a task that will be called if we wait more than 300ms */
898 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
901 p_sys->scheduler->doEventLoop( &p_sys->event );
903 /* remove the task */
904 p_sys->scheduler->unscheduleDelayedTask( task );
906 /* Check for gap in pts value */
907 for( i = 0; i < p_sys->i_track; i++ )
909 live_track_t *tk = p_sys->track[i];
911 if( !tk->b_muxed && !tk->b_rtcp_sync &&
912 tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
914 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
916 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
917 tk->b_rtcp_sync = VLC_TRUE;
919 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
921 p_sys->i_pcr_start = 0;
927 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
929 /* FIXME Make this configurable
930 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
934 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
936 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
938 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
940 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
941 if( RollOverTcp( p_demux ) )
943 msg_Err( p_demux, "TCP rollover failed, aborting" );
946 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
948 else if( p_sys->i_no_data_ti > 34 )
950 msg_Err( p_demux, "no data received in 10s, aborting" );
954 else if( !p_sys->b_multicast && p_sys->b_no_data&& p_sys->i_no_data_ti > 34 )
957 msg_Warn( p_demux, "no data received in 10s, eof ?" );
961 return p_demux->b_error ? 0 : 1;
964 /*****************************************************************************
966 *****************************************************************************/
967 static int Control( demux_t *p_demux, int i_query, va_list args )
969 demux_sys_t *p_sys = p_demux->p_sys;
972 vlc_bool_t *pb, b_bool;
977 pi64 = (int64_t*)va_arg( args, int64_t * );
978 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
981 case DEMUX_GET_LENGTH:
982 pi64 = (int64_t*)va_arg( args, int64_t * );
983 *pi64 = p_sys->i_length;
986 case DEMUX_GET_POSITION:
987 pf = (double*)va_arg( args, double* );
988 if( p_sys->i_length > 0 )
990 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
991 p_sys->i_start ) / (double)(p_sys->i_length);
999 case DEMUX_SET_POSITION:
1003 f = (double)va_arg( args, double );
1004 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
1006 if( p_sys->rtsp && p_sys->i_length > 0 )
1008 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
1010 msg_Err( p_demux, "PLAY failed %s",
1011 p_sys->env->getResultMsg() );
1012 return VLC_EGENERIC;
1014 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
1015 p_sys->i_pcr_start = 0;
1023 /* Special for access_demux */
1024 case DEMUX_CAN_PAUSE:
1025 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1026 if( p_sys->rtsp && p_sys->i_length )
1027 /* Not always true, but will be handled in SET_PAUSE_STATE */
1033 case DEMUX_CAN_CONTROL_PACE:
1034 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1036 #if 0 /* Disable for now until we have a clock synchro algo
1037 * which works with something else than MPEG over UDP */
1044 case DEMUX_SET_PAUSE_STATE:
1047 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
1048 p_sys->i_start ) ) / 1000000.00;
1050 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
1051 if( p_sys->rtsp == NULL )
1052 return VLC_EGENERIC;
1054 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
1055 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
1056 d_npt > 0 ? d_npt : -1 ) ) )
1058 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
1059 return VLC_EGENERIC;
1062 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
1063 for( i = 0; i < p_sys->i_track; i++ )
1065 p_sys->track[i]->i_pts = 0;
1067 p_sys->i_pcr_start = 0; /* FIXME Wrong */
1072 case DEMUX_GET_TITLE_INFO:
1073 case DEMUX_SET_TITLE:
1074 case DEMUX_SET_SEEKPOINT:
1075 return VLC_EGENERIC;
1077 case DEMUX_GET_PTS_DELAY:
1078 pi64 = (int64_t*)va_arg( args, int64_t * );
1079 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
1083 return VLC_EGENERIC;
1087 /*****************************************************************************
1088 * RollOverTcp: reopen the rtsp into TCP mode
1089 * XXX: ugly, a lot of code are duplicated from Open()
1090 *****************************************************************************/
1091 static int RollOverTcp( demux_t *p_demux )
1093 demux_sys_t *p_sys = p_demux->p_sys;
1094 MediaSubsessionIterator *iter;
1095 MediaSubsession *sub;
1101 /* We close the old RTSP session */
1102 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
1104 Medium::close( p_sys->ms );
1105 Medium::close( p_sys->rtsp );
1110 /* Reopen rtsp client */
1111 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
1112 "VLC media player" ) ) == NULL )
1114 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
1115 p_sys->env->getResultMsg() );
1116 return VLC_EGENERIC;
1119 asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
1121 if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
1122 delete [] psz_options;
1124 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
1125 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
1129 msg_Err( p_demux, "describeURL failed (%s)",
1130 p_sys->env->getResultMsg() );
1131 return VLC_EGENERIC;
1134 /* malloc-ated copy */
1135 p_sys->p_sdp = strdup( (char*)p_sdp );
1138 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
1140 msg_Err( p_demux, "MediaSession::createNew failed" );
1141 return VLC_EGENERIC;
1144 /* Initialise each media subsession */
1145 iter = new MediaSubsessionIterator( *p_sys->ms );
1146 while( ( sub = iter->next() ) != NULL )
1150 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
1151 bInit = sub->initiate( 4 ); /* Constant ? */
1153 bInit = sub->initiate();
1157 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
1158 sub->mediumName(), sub->codecName(),
1159 p_sys->env->getResultMsg() );
1162 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
1165 /* Issue the SETUP */
1166 p_sys->rtsp->setupMediaSubsession( *sub, False, True /* tcp */ );
1170 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
1172 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
1173 return VLC_EGENERIC;
1176 /* Update all tracks */
1179 while( ( sub = iter->next() ) != NULL )
1183 if( sub->readSource() == NULL )
1185 if( i_tk >= p_sys->i_track )
1187 msg_Err( p_demux, "WTF !" );
1191 tk = p_sys->track[i_tk];
1196 tk->b_rtcp_sync = VLC_FALSE;
1198 if( sub->rtcpInstance() != NULL )
1199 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1201 tk->readSource = sub->readSource();
1202 tk->rtpSource = sub->rtpSource();
1213 /*****************************************************************************
1215 *****************************************************************************/
1216 static void StreamRead( void *p_private, unsigned int i_size,
1217 unsigned int i_truncated_bytes, struct timeval pts,
1218 unsigned int duration )
1220 live_track_t *tk = (live_track_t*)p_private;
1221 demux_t *p_demux = tk->p_demux;
1222 demux_sys_t *p_sys = p_demux->p_sys;
1225 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1226 (uint64_t)pts.tv_usec;
1228 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1229 i_pts &= UI64C(0x00ffffffffffffff);
1231 if( tk->b_quicktime && tk->p_es == NULL )
1233 QuickTimeGenericRTPSource *qtRTPSource =
1234 (QuickTimeGenericRTPSource*)tk->rtpSource;
1235 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1236 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1238 if( qtState.sdAtomSize < 16 + 32 )
1241 p_sys->event = 0xff;
1245 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1246 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1247 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1249 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1250 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1251 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1253 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1257 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1259 pts.tv_sec * 1000000LL + pts.tv_usec );
1262 /* grow buffer if it looks like buffer is too small, but don't eat
1263 * up all the memory on strange streams */
1264 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1267 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1268 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1270 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1273 msg_Warn( p_demux, "realloc failed" );
1277 tk->p_buffer = (uint8_t*)p_tmp;
1280 if( i_size > tk->i_buffer )
1282 msg_Warn( p_demux, "buffer overflow" );
1284 /* FIXME could i_size be > buffer size ? */
1285 if( tk->fmt.i_codec == VLC_FOURCC('s','a','m','r') ||
1286 tk->fmt.i_codec == VLC_FOURCC('s','a','w','b') )
1288 AMRAudioSource *amrSource = (AMRAudioSource*)tk->readSource;
1290 p_block = block_New( p_demux, i_size + 1 );
1291 p_block->p_buffer[0] = amrSource->lastFrameHeader();
1292 memcpy( p_block->p_buffer + 1, tk->p_buffer, i_size );
1294 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1296 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1297 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1298 uint32_t header = h261Source->lastSpecialHeader();
1300 uint32_t header = 0;
1301 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1303 p_block = block_New( p_demux, i_size + 4 );
1304 memcpy( p_block->p_buffer, &header, 4 );
1305 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1307 if( tk->rtpSource->curPacketMarkerBit() )
1308 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1310 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','4') )
1312 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1313 msg_Warn( p_demux, "unsupported NAL type for H264" );
1315 /* Normal NAL type */
1316 p_block = block_New( p_demux, i_size + 4 );
1317 p_block->p_buffer[0] = 0x00;
1318 p_block->p_buffer[1] = 0x00;
1319 p_block->p_buffer[2] = 0x00;
1320 p_block->p_buffer[3] = 0x01;
1321 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1323 else if( tk->b_asf )
1325 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1326 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1328 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1332 p_block = block_New( p_demux, i_size );
1333 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1336 //p_block->i_rate = p_input->stream.control.i_rate;
1338 if( i_pts != tk->i_pts && !tk->b_muxed )
1340 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1341 p_block->i_pts = i_pts;
1346 stream_DemuxSend( tk->p_out_muxed, p_block );
1348 else if( tk->b_asf )
1350 stream_DemuxSend( p_sys->p_out_asf, p_block );
1354 es_out_Send( p_demux->out, tk->p_es, p_block );
1357 /* warm that's ok */
1358 p_sys->event = 0xff;
1360 /* we have read data */
1362 p_demux->p_sys->b_no_data = VLC_FALSE;
1363 p_demux->p_sys->i_no_data_ti = 0;
1365 if( i_pts > 0 && !tk->b_muxed )
1371 /*****************************************************************************
1373 *****************************************************************************/
1374 static void StreamClose( void *p_private )
1376 live_track_t *tk = (live_track_t*)p_private;
1377 demux_t *p_demux = tk->p_demux;
1378 demux_sys_t *p_sys = p_demux->p_sys;
1380 msg_Dbg( p_demux, "StreamClose" );
1382 p_sys->event = 0xff;
1383 p_demux->b_error = VLC_TRUE;
1387 /*****************************************************************************
1389 *****************************************************************************/
1390 static void TaskInterrupt( void *p_private )
1392 demux_t *p_demux = (demux_t*)p_private;
1394 p_demux->p_sys->i_no_data_ti++;
1397 p_demux->p_sys->event = 0xff;
1400 /*****************************************************************************
1402 *****************************************************************************/
1403 static void TimeoutPrevention( timeout_thread_t *p_timeout )
1405 p_timeout->b_die = VLC_FALSE;
1406 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1407 p_timeout->i_remain *= 1000000;
1409 vlc_thread_ready( p_timeout );
1412 while( !p_timeout->b_die )
1414 if( p_timeout->i_remain <= 0 )
1416 p_timeout->i_remain = (int64_t)p_timeout->p_sys->i_timeout -2;
1417 p_timeout->i_remain *= 1000000;
1418 p_timeout->p_sys->b_timeout_call = VLC_TRUE;
1419 msg_Dbg( p_timeout, "reset the timeout timer" );
1421 p_timeout->i_remain -= 200000;
1422 msleep( 200000 ); /* 200 ms */
1426 /*****************************************************************************
1428 *****************************************************************************/
1429 static int b64_decode( char *dest, char *src );
1431 static int ParseASF( demux_t *p_demux )
1433 demux_sys_t *p_sys = p_demux->p_sys;
1435 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1436 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1440 /* Parse the asf header */
1441 if( psz_asf == NULL )
1442 return VLC_EGENERIC;
1444 psz_asf += strlen( psz_marker );
1445 psz_asf = strdup( psz_asf ); /* Duplicate it */
1446 psz_end = strchr( psz_asf, '\n' );
1448 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1451 if( psz_asf >= psz_end )
1454 return VLC_EGENERIC;
1457 /* Always smaller */
1458 p_header = block_New( p_demux, psz_end - psz_asf );
1459 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1460 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1461 if( p_header->i_buffer <= 0 )
1464 return VLC_EGENERIC;
1467 /* Parse it to get packet size */
1468 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1470 /* Send it to demuxer */
1471 stream_DemuxSend( p_sys->p_out_asf, p_header );
1477 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
1478 static unsigned char* parseH264ConfigStr( char const* configStr,
1479 unsigned int& configSize )
1486 if( configStr == NULL || *configStr == '\0' )
1489 psz = dup = strdup( configStr );
1491 unsigned char *cfg = new unsigned char[5 * strlen(psz)];
1494 char *p = strchr( psz, ',' );
1498 cfg[configSize++] = 0x00;
1499 cfg[configSize++] = 0x00;
1500 cfg[configSize++] = 0x00;
1501 cfg[configSize++] = 0x01;
1502 configSize += b64_decode( (char*)&cfg[configSize], psz );
1509 if( dup ) free( dup );
1514 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1515 static int b64_decode( char *dest, char *src )
1517 const char *dest_start = dest;
1521 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1522 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1523 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1524 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1525 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1526 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1527 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1528 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1529 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1530 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1531 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1532 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1533 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1534 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1535 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1536 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1539 for( i_level = 0; *src != '\0'; src++ )
1543 c = b64[(unsigned int)*src];
1555 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1559 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1563 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1571 return dest - dest_start;