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