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