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