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