]> git.sesse.net Git - vlc/blob - modules/demux/livedotcom.cpp
* modules/demux/*: removed useless probing messages.
[vlc] / modules / demux / livedotcom.cpp
1 /*****************************************************************************
2  * live.cpp : live.com support.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include <iostream>
33
34 #if defined( WIN32 )
35 #   include <winsock2.h>
36 #endif
37
38 #include "BasicUsageEnvironment.hh"
39 #include "GroupsockHelper.hh"
40 #include "liveMedia.hh"
41
42 extern "C" {
43 #include "../access/mms/asf.h"  /* Who said ugly ? */
44 }
45
46 #if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
47 #define RECLAIM_ENV(env) delete (env)
48 #else
49 #define RECLAIM_ENV(env) (env)->reclaim()
50 #endif
51
52 using namespace std;
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 static int  Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
59
60 #define CACHING_TEXT N_("Caching value (ms)")
61 #define CACHING_LONGTEXT N_( \
62     "Allows you to modify the default caching value for RTSP streams. This " \
63     "value should be set in millisecond units." )
64
65 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
66 #define KASENNA_LONGTEXT N_( "Kasenna server speak an old and unstandard " \
67     "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
68     "for communication. In this mode you cannot talk to normal RTSP servers." )
69
70 vlc_module_begin();
71     set_description( _("live.com (RTSP/RTP/SDP) demuxer" ) );
72     set_capability( "demux2", 50 );
73     set_shortname( "Live.com RTP/RTSP");
74     set_callbacks( Open, Close );
75     add_shortcut( "live" );
76     set_category( CAT_INPUT );
77     set_subcategory( SUBCAT_INPUT_DEMUX );
78
79     add_submodule();
80         set_description( _("RTSP/RTP access and demux") );
81         add_shortcut( "rtsp" );
82         add_shortcut( "sdp" );
83         set_capability( "access_demux", 0 );
84         set_callbacks( Open, Close );
85         add_bool( "rtsp-tcp", 0, NULL,
86                   N_("Use RTP over RTSP (TCP)"),
87                   N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
88         add_integer( "rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
89             CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
90         add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
91                   KASENNA_LONGTEXT, VLC_TRUE );
92 vlc_module_end();
93
94 /* TODO:
95  *  - Improve support of PS/TS
96  *  - Support X-QT/X-QUICKTIME generic codec for audio.
97  *
98  *  - Check memory leak, delete/free -> still one when using rtsp-tcp but I'm
99  *  not sure if it comes from me.
100  *
101  */
102
103 /*****************************************************************************
104  * Local prototypes
105  *****************************************************************************/
106 typedef struct
107 {
108     demux_t     *p_demux;
109
110     vlc_bool_t   b_quicktime;
111     vlc_bool_t   b_muxed;
112     vlc_bool_t   b_asf;
113
114     es_format_t  fmt;
115     es_out_id_t  *p_es;
116
117     stream_t     *p_out_muxed;    /* for muxed stream */
118
119     RTPSource    *rtpSource;
120     FramedSource *readSource;
121     vlc_bool_t   b_rtcp_sync;
122
123     uint8_t      *p_buffer;
124     unsigned int  i_buffer;
125
126     char         waiting;
127
128     mtime_t      i_pts;
129
130 } live_track_t;
131
132 struct demux_sys_t
133 {
134     char         *p_sdp;    /* XXX mallocated */
135
136     MediaSession     *ms;
137     TaskScheduler    *scheduler;
138     UsageEnvironment *env ;
139     RTSPClient       *rtsp;
140
141     /* */
142     int              i_track;
143     live_track_t     **track;   /* XXX mallocated */
144     mtime_t          i_pcr;
145     mtime_t          i_pcr_start;
146
147     /* Asf */
148     asf_header_t     asfh;
149     stream_t         *p_out_asf;
150
151     /* */
152     mtime_t          i_length;
153     mtime_t          i_start;
154
155     /* */
156     vlc_bool_t       b_no_data;     /* true if we never receive any data */
157     int              i_no_data_ti;  /* consecutive number of TaskInterrupt */
158
159     char             event;
160 };
161
162 static int Demux  ( demux_t * );
163 static int Control( demux_t *, int, va_list );
164
165 static int ParseASF( demux_t * );
166
167 static int RollOverTcp( demux_t * );
168
169 static void StreamRead( void *, unsigned int, unsigned int,
170                         struct timeval, unsigned int );
171 static void StreamClose( void * );
172 static void TaskInterrupt( void * );
173
174 /*****************************************************************************
175  * DemuxOpen:
176  *****************************************************************************/
177 static int  Open ( vlc_object_t *p_this )
178 {
179     demux_t     *p_demux = (demux_t*)p_this;
180     demux_sys_t *p_sys;
181
182     MediaSubsessionIterator *iter;
183     MediaSubsession *sub;
184
185     vlc_bool_t b_rtsp_tcp;
186     uint8_t *p_peek;
187
188     int     i_sdp;
189     int     i_sdp_max;
190     uint8_t *p_sdp;
191
192     if( p_demux->s )
193     {
194         /* See if it looks like a SDP
195            v, o, s fields are mandatory and in this order */
196         if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
197
198         if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
199             strncmp( (char*)p_peek, "v=0\n", 4 ) &&
200             ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
201         {
202             return VLC_EGENERIC;
203         }
204     }
205     else
206     {
207         var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
208     }
209
210     p_demux->pf_demux  = Demux;
211     p_demux->pf_control= Control;
212     p_demux->p_sys     = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
213     p_sys->p_sdp = NULL;
214     p_sys->scheduler = NULL;
215     p_sys->env = NULL;
216     p_sys->ms = NULL;
217     p_sys->rtsp = NULL;
218     p_sys->i_track = 0;
219     p_sys->track   = NULL;
220     p_sys->i_pcr   = 0;
221     p_sys->i_pcr_start = 0;
222     p_sys->i_length = 0;
223     p_sys->i_start = 0;
224     p_sys->p_out_asf = NULL;
225     p_sys->b_no_data = VLC_TRUE;
226     p_sys->i_no_data_ti = 0;
227
228
229     if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
230     {
231         msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
232         goto error;
233     }
234     if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
235     {
236         msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
237         goto error;
238     }
239
240     if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
241     {
242         char *psz_url;
243         char *psz_options;
244
245         if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
246               "VLC Media Player" ) ) == NULL )
247         {
248             msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
249                      p_sys->env->getResultMsg() );
250             goto error;
251         }
252         psz_url = (char*)malloc( strlen( p_demux->psz_path ) + 8 );
253         sprintf( psz_url, "rtsp://%s", p_demux->psz_path );
254
255         psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
256         if( psz_options )
257             delete [] psz_options;
258
259         p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
260                               NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
261         if( p_sdp == NULL )
262         {
263             msg_Err( p_demux, "describeURL failed (%s)",
264                      p_sys->env->getResultMsg() );
265             free( psz_url );
266             goto error;
267         }
268         free( psz_url );
269
270         /* malloc-ated copy */
271         p_sys->p_sdp = strdup( (char*)p_sdp );
272         delete[] p_sdp;
273         fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
274     }
275     else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
276     {
277         p_sys->p_sdp = strdup( p_demux->psz_path );
278     }
279     else
280     {
281         /* Gather the complete sdp file */
282         i_sdp = 0;
283         i_sdp_max = 1000;
284         p_sdp = (uint8_t*)malloc( i_sdp_max );
285         for( ;; )
286         {
287             int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
288                                       i_sdp_max - i_sdp - 1 );
289
290             if( i_read < 0 )
291             {
292                 msg_Err( p_demux, "failed to read SDP" );
293                 free( p_sys );
294                 return VLC_EGENERIC;
295             }
296
297             i_sdp += i_read;
298
299             if( i_read < i_sdp_max - i_sdp - 1 )
300             {
301                 p_sdp[i_sdp] = '\0';
302                 break;
303             }
304
305             i_sdp_max += 1000;
306             p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
307         }
308         p_sys->p_sdp = (char*)p_sdp;
309
310         msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
311     }
312     if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
313     {
314         msg_Err( p_demux, "MediaSession::createNew failed" );
315         goto error;
316     }
317
318     b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
319
320     /* Initialise each media subsession */
321     iter = new MediaSubsessionIterator( *p_sys->ms );
322     while( ( sub = iter->next() ) != NULL )
323     {
324         unsigned int i_buffer = 0;
325         Boolean bInit;
326
327         /* Value taken from mplayer */
328         if( !strcmp( sub->mediumName(), "audio" ) )
329             i_buffer = 100000;
330         else if( !strcmp( sub->mediumName(), "video" ) )
331             i_buffer = 2000000;
332         else
333             continue;
334
335         if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
336             bInit = sub->initiate( 4 ); /* Constant ? */
337         else
338             bInit = sub->initiate();
339
340         if( !bInit )
341         {
342             msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
343                       sub->mediumName(), sub->codecName(),
344                       p_sys->env->getResultMsg() );
345         }
346         else
347         {
348             if( sub->rtpSource() )
349             {
350                 int fd = sub->rtpSource()->RTPgs()->socketNum();
351                 /* Increase the buffer size */
352                 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
353             }
354
355             msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
356                      sub->codecName() );
357
358             /* Issue the SETUP */
359             if( p_sys->rtsp )
360             {
361                 p_sys->rtsp->setupMediaSubsession( *sub, False,
362                                                    b_rtsp_tcp ? True : False );
363             }
364         }
365     }
366
367     if( p_sys->rtsp )
368     {
369         /* The PLAY */
370         if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
371         {
372             msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
373             goto error;
374         }
375     }
376
377     /* Create all es struct */
378     iter->reset();
379     while( ( sub = iter->next() ) != NULL )
380     {
381         live_track_t *tk;
382
383         if( sub->readSource() == NULL )
384         {
385             continue;
386         }
387
388         tk = (live_track_t*)malloc( sizeof( live_track_t ) );
389         tk->p_demux = p_demux;
390         tk->waiting = 0;
391         tk->i_pts   = 0;
392         tk->b_quicktime = VLC_FALSE;
393         tk->b_muxed     = VLC_FALSE;
394         tk->b_asf       = VLC_FALSE;
395         tk->b_rtcp_sync = VLC_FALSE;
396         tk->p_out_muxed = NULL;
397         tk->p_es        = NULL;
398         tk->i_buffer    = 65536;
399         tk->p_buffer    = (uint8_t *)malloc( 65536 );
400
401         /* Value taken from mplayer */
402         if( !strcmp( sub->mediumName(), "audio" ) )
403         {
404             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
405             tk->fmt.audio.i_channels = sub->numChannels();
406             tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
407
408             if( !strcmp( sub->codecName(), "MPA" ) ||
409                 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
410                 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
411             {
412                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
413                 tk->fmt.audio.i_rate = 0;
414             }
415             else if( !strcmp( sub->codecName(), "AC3" ) )
416             {
417                 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
418                 tk->fmt.audio.i_rate = 0;
419             }
420             else if( !strcmp( sub->codecName(), "L16" ) )
421             {
422                 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
423                 tk->fmt.audio.i_bitspersample = 16;
424             }
425             else if( !strcmp( sub->codecName(), "L8" ) )
426             {
427                 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
428                 tk->fmt.audio.i_bitspersample = 8;
429             }
430             else if( !strcmp( sub->codecName(), "PCMU" ) )
431             {
432                 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
433             }
434             else if( !strcmp( sub->codecName(), "PCMA" ) )
435             {
436                 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
437             }
438             else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
439             {
440                 unsigned int i_extra;
441                 uint8_t      *p_extra;
442
443                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
444
445                 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
446                                                          i_extra ) ) )
447                 {
448                     tk->fmt.i_extra = i_extra;
449                     tk->fmt.p_extra = malloc( i_extra );
450                     memcpy( tk->fmt.p_extra, p_extra, i_extra );
451                     delete[] p_extra;
452                 }
453             }
454             else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
455             {
456                 unsigned int i_extra;
457                 uint8_t      *p_extra;
458
459                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
460
461                 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
462                                                        i_extra ) ) )
463                 {
464                     tk->fmt.i_extra = i_extra;
465                     tk->fmt.p_extra = malloc( i_extra );
466                     memcpy( tk->fmt.p_extra, p_extra, i_extra );
467                     delete[] p_extra;
468                 }
469             }
470             else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
471             {
472                 tk->b_asf = VLC_TRUE;
473                 if( p_sys->p_out_asf == NULL )
474                     p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
475                                                         p_demux->out );
476             }
477         }
478         else if( !strcmp( sub->mediumName(), "video" ) )
479         {
480             es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
481             if( !strcmp( sub->codecName(), "MPV" ) )
482             {
483                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
484             }
485             else if( !strcmp( sub->codecName(), "H263" ) ||
486                      !strcmp( sub->codecName(), "H263-1998" ) ||
487                      !strcmp( sub->codecName(), "H263-2000" ) )
488             {
489                 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
490             }
491             else if( !strcmp( sub->codecName(), "H261" ) )
492             {
493                 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
494             }
495             else if( !strcmp( sub->codecName(), "JPEG" ) )
496             {
497                 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
498             }
499             else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
500             {
501                 unsigned int i_extra;
502                 uint8_t      *p_extra;
503
504                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
505
506                 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
507                                                        i_extra ) ) )
508                 {
509                     tk->fmt.i_extra = i_extra;
510                     tk->fmt.p_extra = malloc( i_extra );
511                     memcpy( tk->fmt.p_extra, p_extra, i_extra );
512                     delete[] p_extra;
513                 }
514             }
515             else if( !strcmp( sub->codecName(), "X-QT" ) ||
516                      !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
517                      !strcmp( sub->codecName(), "X-QDM" ) ||
518                      !strcmp( sub->codecName(), "X-SV3V-ES" )  ||
519                      !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
520             {
521                 tk->b_quicktime = VLC_TRUE;
522             }
523             else if( !strcmp( sub->codecName(), "MP2T" ) )
524             {
525                 tk->b_muxed = VLC_TRUE;
526                 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
527             }
528             else if( !strcmp( sub->codecName(), "MP2P" ) ||
529                      !strcmp( sub->codecName(), "MP1S" ) )
530             {
531                 tk->b_muxed = VLC_TRUE;
532                 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
533                                                    p_demux->out );
534             }
535             else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
536             {
537                 tk->b_asf = VLC_TRUE;
538                 if( p_sys->p_out_asf == NULL )
539                     p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
540                                                         p_demux->out );;
541             }
542         }
543
544         if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
545         {
546             tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
547         }
548
549         if( sub->rtcpInstance() != NULL )
550         {
551             sub->rtcpInstance()->setByeHandler( StreamClose, tk );
552         }
553
554         if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
555         {
556             tk->readSource = sub->readSource();
557             tk->rtpSource  = sub->rtpSource();
558
559             /* Append */
560             p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
561             p_sys->track[p_sys->i_track++] = tk;
562         }
563         else
564         {
565             free( tk );
566         }
567     }
568
569     delete iter;
570
571     if( p_sys->p_out_asf && ParseASF( p_demux ) )
572     {
573         msg_Err( p_demux, "cannot find a usable asf header" );
574         /* TODO Clean tracks */
575         goto error;
576     }
577
578     p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
579     if( p_sys->i_length < 0 )
580     {
581         p_sys->i_length = 0;
582     }
583     else if( p_sys->i_length > 0 )
584     {
585         /* FIXME */
586         /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
587     }
588
589     if( p_sys->i_track <= 0 )
590     {
591         msg_Err( p_demux, "no codec supported, aborting" );
592         goto error;
593     }
594
595     return VLC_SUCCESS;
596
597 error:
598     if( p_sys->p_out_asf )
599     {
600         stream_DemuxDelete( p_sys->p_out_asf );
601     }
602     if( p_sys->ms )
603     {
604         Medium::close( p_sys->ms );
605     }
606     if( p_sys->rtsp )
607     {
608         Medium::close( p_sys->rtsp );
609     }
610     if( p_sys->env )
611     {
612         RECLAIM_ENV(p_sys->env);
613     }
614     if( p_sys->scheduler )
615     {
616         delete p_sys->scheduler;
617     }
618     if( p_sys->p_sdp )
619     {
620         free( p_sys->p_sdp );
621     }
622     free( p_sys );
623     return VLC_EGENERIC;
624 }
625
626 /*****************************************************************************
627  * DemuxClose:
628  *****************************************************************************/
629 static void Close( vlc_object_t *p_this )
630 {
631     demux_t *p_demux = (demux_t*)p_this;
632     demux_sys_t *p_sys = p_demux->p_sys;
633     int i;
634
635     for( i = 0; i < p_sys->i_track; i++ )
636     {
637         live_track_t *tk = p_sys->track[i];
638
639         if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
640         free( tk->p_buffer );
641         free( tk );
642     }
643
644     if( p_sys->i_track ) free( p_sys->track );
645     if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
646
647     if( p_sys->rtsp && p_sys->ms )
648     {
649         /* TEARDOWN */
650         p_sys->rtsp->teardownMediaSession( *p_sys->ms );
651     }
652
653     Medium::close( p_sys->ms );
654
655     if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
656     if( p_sys->env ) RECLAIM_ENV(p_sys->env);
657     if( p_sys->scheduler ) delete p_sys->scheduler;
658     if( p_sys->p_sdp ) free( p_sys->p_sdp );
659     free( p_sys );
660 }
661
662 /*****************************************************************************
663  * Demux:
664  *****************************************************************************/
665 static int Demux( demux_t *p_demux )
666 {
667     demux_sys_t    *p_sys = p_demux->p_sys;
668     TaskToken      task;
669
670     vlc_bool_t      b_send_pcr = VLC_TRUE;
671     mtime_t         i_pcr = 0;
672     int             i;
673
674     for( i = 0; i < p_sys->i_track; i++ )
675     {
676         live_track_t *tk = p_sys->track[i];
677
678         if( tk->b_asf || tk->b_muxed )
679             b_send_pcr = VLC_FALSE;
680
681         if( i_pcr == 0 )
682         {
683             i_pcr = tk->i_pts;
684         }
685         else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
686         {
687             i_pcr = tk->i_pts ;
688         }
689     }
690     if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
691     {
692         p_sys->i_pcr = i_pcr;
693
694         if( b_send_pcr )
695             es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
696         if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
697             ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
698         {
699             p_sys->i_pcr_start = i_pcr;
700         }
701     }
702
703     /* First warm we want to read data */
704     p_sys->event = 0;
705     for( i = 0; i < p_sys->i_track; i++ )
706     {
707         live_track_t *tk = p_sys->track[i];
708
709         if( tk->waiting == 0 )
710         {
711             tk->waiting = 1;
712             tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
713                                           StreamRead, tk, StreamClose, tk );
714         }
715     }
716     /* Create a task that will be called if we wait more than 300ms */
717     task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
718
719     /* Do the read */
720     p_sys->scheduler->doEventLoop( &p_sys->event );
721
722     /* remove the task */
723     p_sys->scheduler->unscheduleDelayedTask( task );
724
725     /* Check for gap in pts value */
726     for( i = 0; i < p_sys->i_track; i++ )
727     {
728         live_track_t *tk = p_sys->track[i];
729
730         if( !tk->b_muxed && !tk->b_rtcp_sync &&
731             tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
732         {
733             msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
734
735             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
736             tk->b_rtcp_sync = VLC_TRUE;
737
738             /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
739             tk->i_pts = 0;
740             p_sys->i_pcr_start = 0;
741             p_sys->i_pcr = 0;
742             i_pcr = 0;
743         }
744     }
745
746     if( p_sys->b_no_data && p_sys->i_no_data_ti > 3 )
747     {
748         vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
749
750         if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
751         {
752             msg_Warn( p_demux, "no data received in 900ms. Switching to TCP" );
753             if( RollOverTcp( p_demux ) )
754             {
755                 msg_Err( p_demux, "TCP rollover failed, aborting" );
756                 return 0;
757             }
758             var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
759         }
760         else if( p_sys->i_no_data_ti > 10 )
761         {
762             msg_Err( p_demux, "no data received in 3s, aborting" );
763             return 0;
764         }
765     }
766     else if( p_sys->i_no_data_ti > 10 )
767     {
768         /* EOF ? */
769         msg_Warn( p_demux, "no data received in 3s, eof ?" );
770         return 0;
771     }
772
773     return p_demux->b_error ? 0 : 1;
774 }
775
776 /*****************************************************************************
777  * Control:
778  *****************************************************************************/
779 static int Control( demux_t *p_demux, int i_query, va_list args )
780 {
781     demux_sys_t *p_sys = p_demux->p_sys;
782     int64_t *pi64;
783     double  *pf, f;
784     vlc_bool_t *pb, b_bool;
785
786     switch( i_query )
787     {
788         case DEMUX_GET_TIME:
789             pi64 = (int64_t*)va_arg( args, int64_t * );
790             *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
791             return VLC_SUCCESS;
792
793         case DEMUX_GET_LENGTH:
794             pi64 = (int64_t*)va_arg( args, int64_t * );
795             *pi64 = p_sys->i_length;
796             return VLC_SUCCESS;
797
798         case DEMUX_GET_POSITION:
799             pf = (double*)va_arg( args, double* );
800             if( p_sys->i_length > 0 )
801             {
802                 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
803                                 p_sys->i_start ) / (double)(p_sys->i_length);
804             }
805             else
806             {
807                 *pf = 0;
808             }
809             return VLC_SUCCESS;
810
811         case DEMUX_SET_POSITION:
812         {
813             float time;
814
815             f = (double)va_arg( args, double );
816             time = f * (double)p_sys->i_length / 1000000.0;   /* in second */
817
818             if( p_sys->rtsp && p_sys->i_length > 0 )
819             {
820                 MediaSubsessionIterator *iter =
821                     new MediaSubsessionIterator( *p_sys->ms );
822                 MediaSubsession *sub;
823                 int i;
824
825                 while( ( sub = iter->next() ) != NULL )
826                 {
827                     p_sys->rtsp->playMediaSubsession( *sub, time );
828                 }
829                 delete iter;
830                 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
831                 p_sys->i_pcr_start = 0;
832                 p_sys->i_pcr       = 0;
833                 
834                 for( i = 0; i < p_sys->i_track; i++ )
835                 {
836                     p_sys->track[i]->i_pts = 0;
837                 }
838
839                 return VLC_SUCCESS;
840             }
841             return VLC_SUCCESS;
842         }
843
844         /* Special for access_demux */
845         case DEMUX_CAN_PAUSE:
846             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
847             if( p_sys->rtsp && p_sys->i_length )
848                 /* Not always true, but will be handled in SET_PAUSE_STATE */
849                 *pb = VLC_TRUE;
850             else
851                 *pb = VLC_FALSE;
852             return VLC_SUCCESS;
853
854         case DEMUX_CAN_CONTROL_PACE:
855             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
856
857 #if 0       /* Disable for now until we have a clock synchro algo
858              * which works with something else than MPEG over UDP */
859             *pb = VLC_FALSE;
860 #endif
861             *pb = VLC_TRUE;
862             return VLC_SUCCESS;
863
864         case DEMUX_SET_PAUSE_STATE:
865             double d_npt;
866             MediaSubsessionIterator *iter;
867             MediaSubsession *sub;
868
869             d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
870                                 p_sys->i_start ) ) / 1000000.00;
871
872             b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
873             if( p_sys->rtsp == NULL )
874                 return VLC_EGENERIC;
875
876             iter = new MediaSubsessionIterator( *p_sys->ms );
877             while( ( sub = iter->next() ) != NULL )
878             {
879                 if( ( b_bool && !p_sys->rtsp->pauseMediaSubsession( *sub ) ) ||
880                     ( !b_bool && !p_sys->rtsp->playMediaSubsession( *sub,
881                       d_npt > 0 ? d_npt : -1 ) ) )
882                 {
883                     delete iter;
884                     return VLC_EGENERIC;
885                 }
886             }
887             delete iter;
888 #if 0
889             /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
890             for( i = 0; i < p_sys->i_track; i++ )
891             {
892                 p_sys->track[i]->i_pts = 0;
893             }
894             p_sys->i_pcr_start = 0; /* FIXME Wrong */
895             p_sys->i_pcr = 0;
896 #endif
897             return VLC_SUCCESS;
898
899         case DEMUX_GET_TITLE_INFO:
900         case DEMUX_SET_TITLE:
901         case DEMUX_SET_SEEKPOINT:
902             return VLC_EGENERIC;
903
904         case DEMUX_GET_PTS_DELAY:
905             pi64 = (int64_t*)va_arg( args, int64_t * );
906             *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
907             return VLC_SUCCESS;
908
909         default:
910             return VLC_EGENERIC;
911     }
912 }
913
914 /*****************************************************************************
915  * RollOverTcp: reopen the rtsp into TCP mode
916  * XXX: ugly, a lot of code are duplicated from Open()
917  *****************************************************************************/
918 static int RollOverTcp( demux_t *p_demux )
919 {
920     demux_sys_t *p_sys = p_demux->p_sys;
921     MediaSubsessionIterator *iter;
922     MediaSubsession *sub;
923     char *psz_url;
924     char *psz_options;
925     uint8_t *p_sdp;
926     int i_tk;
927
928     /* We close the old RTSP session */
929     p_sys->rtsp->teardownMediaSession( *p_sys->ms );
930
931     Medium::close( p_sys->ms );
932     Medium::close( p_sys->rtsp );
933
934     p_sys->ms = NULL;
935     p_sys->rtsp = NULL;
936
937     /* Reopen rtsp client */
938     if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
939           "VLC Media Player" ) ) == NULL )
940     {
941         msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
942                  p_sys->env->getResultMsg() );
943         return VLC_EGENERIC;
944     }
945
946     asprintf( &psz_url, "rtsp://%s", p_demux->psz_path );
947
948     if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
949         delete [] psz_options;
950
951     p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
952                           NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
953     free( psz_url );
954     if( p_sdp == NULL )
955     {
956         msg_Err( p_demux, "describeURL failed (%s)",
957                  p_sys->env->getResultMsg() );
958         return VLC_EGENERIC;
959     }
960
961     /* malloc-ated copy */
962     p_sys->p_sdp = strdup( (char*)p_sdp );
963     delete[] p_sdp;
964
965     if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
966     {
967         msg_Err( p_demux, "MediaSession::createNew failed" );
968         return VLC_EGENERIC;
969     }
970
971     /* Initialise each media subsession */
972     iter = new MediaSubsessionIterator( *p_sys->ms );
973     while( ( sub = iter->next() ) != NULL )
974     {
975         Boolean bInit;
976
977         if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
978             bInit = sub->initiate( 4 ); /* Constant ? */
979         else
980             bInit = sub->initiate();
981
982         if( !bInit )
983         {
984             msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
985                       sub->mediumName(), sub->codecName(),
986                       p_sys->env->getResultMsg() );
987             continue;
988         }
989         msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
990                  sub->codecName() );
991
992         /* Issue the SETUP */
993         p_sys->rtsp->setupMediaSubsession( *sub, False, True /* tcp */ );
994     }
995
996     /* The PLAY */
997     if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
998     {
999         msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
1000         return VLC_EGENERIC;
1001     }
1002
1003     /* Update all tracks */
1004     iter->reset();
1005     i_tk = 0;
1006     while( ( sub = iter->next() ) != NULL )
1007     {
1008         live_track_t *tk;
1009
1010         if( sub->readSource() == NULL )
1011             continue;
1012         if( i_tk >= p_sys->i_track )
1013         {
1014             msg_Err( p_demux, "WTF !" );
1015             break;
1016         }
1017
1018         tk = p_sys->track[i_tk];
1019
1020         /* Reset state */
1021         tk->waiting = 0;
1022         tk->i_pts   = 0;
1023         tk->b_rtcp_sync = VLC_FALSE;
1024
1025         if( sub->rtcpInstance() != NULL )
1026             sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1027
1028         tk->readSource = sub->readSource();
1029         tk->rtpSource  = sub->rtpSource();
1030
1031         i_tk++;
1032     }
1033
1034     delete iter;
1035
1036     return VLC_SUCCESS;
1037 }
1038
1039
1040 /*****************************************************************************
1041  *
1042  *****************************************************************************/
1043 static void StreamRead( void *p_private, unsigned int i_size,
1044                         unsigned int i_truncated_bytes, struct timeval pts,
1045                         unsigned int duration )
1046 {
1047     live_track_t   *tk = (live_track_t*)p_private;
1048     demux_t        *p_demux = tk->p_demux;
1049     demux_sys_t    *p_sys = p_demux->p_sys;
1050     block_t        *p_block;
1051
1052     mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1053         (uint64_t)pts.tv_usec;
1054
1055     /* XXX Beurk beurk beurk Avoid having negative value XXX */
1056     i_pts &= UI64C(0x00ffffffffffffff);
1057
1058     if( tk->b_quicktime && tk->p_es == NULL )
1059     {
1060         QuickTimeGenericRTPSource *qtRTPSource =
1061             (QuickTimeGenericRTPSource*)tk->rtpSource;
1062         QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1063         uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1064
1065         if( qtState.sdAtomSize < 16 + 32 )
1066         {
1067             /* invalid */
1068             p_sys->event = 0xff;
1069             tk->waiting = 0;
1070             return;
1071         }
1072         tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1073         tk->fmt.video.i_width  = (sdAtom[28] << 8) | sdAtom[29];
1074         tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1075
1076         tk->fmt.i_extra        = qtState.sdAtomSize - 16;
1077         tk->fmt.p_extra        = malloc( tk->fmt.i_extra );
1078         memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1079
1080         tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1081     }
1082
1083 #if 0
1084     fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1085              i_size,
1086              pts.tv_sec * 1000000LL + pts.tv_usec );
1087 #endif
1088
1089     /* grow buffer if it looks like buffer is too small, but don't eat
1090      * up all the memory on strange streams */
1091     if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1092     {
1093         void *p_tmp;
1094         msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1095         msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1096         tk->i_buffer *= 2;
1097         p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1098         if (p_tmp == NULL)
1099         {
1100             msg_Warn( p_demux, "realloc failed" );
1101         }
1102         else
1103         {
1104             tk->p_buffer = (uint8_t*)p_tmp;
1105         }
1106     }
1107     if( i_size > tk->i_buffer )
1108     {
1109         msg_Warn( p_demux, "buffer overflow" );
1110     }
1111     /* FIXME could i_size be > buffer size ? */
1112     if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') )
1113     {
1114 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1115         H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1116         uint32_t header = h261Source->lastSpecialHeader();
1117 #else
1118         uint32_t header = 0;
1119         msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1120 #endif
1121         p_block = block_New( p_demux, i_size + 4 );
1122         memcpy( p_block->p_buffer, &header, 4 );
1123         memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1124     }
1125     else if( tk->b_asf )
1126     {
1127         int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1128         p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1129
1130         memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1131     }
1132     else
1133     {
1134         p_block = block_New( p_demux, i_size );
1135         memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1136     }
1137     if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') &&
1138         tk->rtpSource->curPacketMarkerBit() )
1139     {
1140         p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1141     }
1142     //p_block->i_rate = p_input->stream.control.i_rate;
1143
1144     if( i_pts != tk->i_pts && !tk->b_muxed )
1145     {
1146         p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1147         p_block->i_pts = i_pts;
1148     }
1149     //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
1150
1151     if( tk->b_muxed )
1152     {
1153         stream_DemuxSend( tk->p_out_muxed, p_block );
1154     }
1155     else if( tk->b_asf )
1156     {
1157         stream_DemuxSend( p_sys->p_out_asf, p_block );
1158     }
1159     else
1160     {
1161         es_out_Send( p_demux->out, tk->p_es, p_block );
1162     }
1163
1164     /* warm that's ok */
1165     p_sys->event = 0xff;
1166
1167     /* we have read data */
1168     tk->waiting = 0;
1169     p_demux->p_sys->b_no_data = VLC_FALSE;
1170     p_demux->p_sys->i_no_data_ti = 0;
1171
1172     if( i_pts > 0 && !tk->b_muxed )
1173     {
1174         tk->i_pts = i_pts;
1175     }
1176 }
1177
1178 /*****************************************************************************
1179  *
1180  *****************************************************************************/
1181 static void StreamClose( void *p_private )
1182 {
1183     live_track_t   *tk = (live_track_t*)p_private;
1184     demux_t        *p_demux = tk->p_demux;
1185     demux_sys_t    *p_sys = p_demux->p_sys;
1186
1187     fprintf( stderr, "StreamClose\n" );
1188
1189     p_sys->event = 0xff;
1190     p_demux->b_error = VLC_TRUE;
1191 }
1192
1193
1194 /*****************************************************************************
1195  *
1196  *****************************************************************************/
1197 static void TaskInterrupt( void *p_private )
1198 {
1199     demux_t *p_demux = (demux_t*)p_private;
1200
1201     fprintf( stderr, "TaskInterrupt\n" );
1202
1203     p_demux->p_sys->i_no_data_ti++;
1204
1205     /* Avoid lock */
1206     p_demux->p_sys->event = 0xff;
1207 }
1208
1209 /*****************************************************************************
1210  *
1211  *****************************************************************************/
1212 static int b64_decode( char *dest, char *src );
1213
1214 static int ParseASF( demux_t *p_demux )
1215 {
1216     demux_sys_t    *p_sys = p_demux->p_sys;
1217
1218     const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1219     char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1220     char *psz_end;
1221     block_t *p_header;
1222
1223     /* Parse the asf header */
1224     if( psz_asf == NULL )
1225         return VLC_EGENERIC;
1226
1227     psz_asf += strlen( psz_marker );
1228     psz_asf = strdup( psz_asf );    /* Duplicate it */
1229     psz_end = strchr( psz_asf, '\n' );
1230
1231     while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1232         *psz_end-- = '\0';
1233
1234     if( psz_asf >= psz_end )
1235     {
1236         free( psz_asf );
1237         return VLC_EGENERIC;
1238     }
1239
1240     /* Always smaller */
1241     p_header = block_New( p_demux, psz_end - psz_asf );
1242     p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1243     fprintf( stderr, "Size=%d Hdrb64=%s\n", p_header->i_buffer, psz_asf );
1244     if( p_header->i_buffer <= 0 )
1245     {
1246         free( psz_asf );
1247         return VLC_EGENERIC;
1248     }
1249
1250     /* Parse it to get packet size */
1251     E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1252
1253     /* Send it to demuxer */
1254     stream_DemuxSend( p_sys->p_out_asf, p_header );
1255
1256     free( psz_asf );
1257     return VLC_SUCCESS;
1258 }
1259 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1260 static int b64_decode( char *dest, char *src )
1261 {
1262     const char *dest_start = dest;
1263     int  i_level;
1264     int  last = 0;
1265     int  b64[256] = {
1266         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
1267         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
1268         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
1269         52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
1270         -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
1271         15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
1272         -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
1273         41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
1274         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
1275         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
1276         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
1277         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
1278         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
1279         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
1280         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
1281         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
1282         };
1283
1284     for( i_level = 0; *src != '\0'; src++ )
1285     {
1286         int  c;
1287
1288         c = b64[(unsigned int)*src];
1289         if( c == -1 )
1290         {
1291             continue;
1292         }
1293
1294         switch( i_level )
1295         {
1296             case 0:
1297                 i_level++;
1298                 break;
1299             case 1:
1300                 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1301                 i_level++;
1302                 break;
1303             case 2:
1304                 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1305                 i_level++;
1306                 break;
1307             case 3:
1308                 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1309                 i_level = 0;
1310         }
1311         last = c;
1312     }
1313
1314     *dest = '\0';
1315
1316     return dest - dest_start;
1317 }