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