]> git.sesse.net Git - vlc/blob - modules/demux/livedotcom.cpp
* livedotcom.cpp: nULL -> UI64C(n).
[vlc] / modules / demux / livedotcom.cpp
1 /*****************************************************************************
2  * live.cpp : live.com support.
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: livedotcom.cpp,v 1.17 2004/02/23 20:45:52 fenrir Exp $
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 <codecs.h>                        /* BITMAPINFOHEADER, WAVEFORMATEX */
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 using namespace std;
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  DemuxOpen ( vlc_object_t * );
49 static void DemuxClose( vlc_object_t * );
50
51 static int  AccessOpen ( vlc_object_t * );
52 static void AccessClose( 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 miliseconds units." )
58
59 vlc_module_begin();
60     set_description( _("live.com (RTSP/RTP/SDP) demuxer" ) );
61     set_capability( "demux", 50 );
62     set_callbacks( DemuxOpen, DemuxClose );
63     add_shortcut( "live" );
64
65     add_submodule();
66         set_description( _("RTSP/RTP describe") );
67         add_shortcut( "rtsp" );
68         add_shortcut( "sdp" );
69         set_capability( "access", 0 );
70         set_callbacks( AccessOpen, AccessClose );
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  *  - Support PS/TS (need to rework the TS/PS demuxer a lot).
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 struct access_sys_t
91 {
92     int     i_sdp;
93     char    *p_sdp;
94
95     int     i_pos;
96 };
97
98 typedef struct
99 {
100     input_thread_t *p_input;
101
102     vlc_bool_t   b_quicktime;
103
104     es_format_t  fmt;
105     es_out_id_t  *p_es;
106
107     RTPSource    *rtpSource;
108     FramedSource *readSource;
109     vlc_bool_t   b_rtcp_sync;
110
111     uint8_t      buffer[65536];
112
113     char         waiting;
114
115     mtime_t      i_pts;
116 } live_track_t;
117
118 struct demux_sys_t
119 {
120     char         *p_sdp;    /* XXX mallocated */
121
122     MediaSession     *ms;
123     TaskScheduler    *scheduler;
124     UsageEnvironment *env ;
125     RTSPClient       *rtsp;
126
127     int              i_track;
128     live_track_t     **track;   /* XXX mallocated */
129     mtime_t          i_pcr;
130     mtime_t          i_pcr_start;
131
132     mtime_t          i_length;
133     mtime_t          i_start;
134
135     char             event;
136 };
137
138 static ssize_t Read   ( input_thread_t *, byte_t *, size_t );
139 static ssize_t MRLRead( input_thread_t *, byte_t *, size_t );
140
141 static int     Demux  ( input_thread_t * );
142 static int     Control( input_thread_t *, int, va_list );
143
144
145
146 /*****************************************************************************
147  * AccessOpen:
148  *****************************************************************************/
149 static int  AccessOpen( vlc_object_t *p_this )
150 {
151     input_thread_t *p_input = (input_thread_t *)p_this;
152     access_sys_t   *p_sys;
153
154     TaskScheduler    *scheduler = NULL;
155     UsageEnvironment *env       = NULL;
156     RTSPClient       *rtsp      = NULL;
157
158     vlc_value_t      val;
159     char             *psz_url;
160
161     if( p_input->psz_access == NULL || ( strcasecmp( p_input->psz_access, "rtsp" ) && strcasecmp( p_input->psz_access, "sdp" ) ) )
162     {
163         msg_Warn( p_input, "RTSP access discarded" );
164         return VLC_EGENERIC;
165     }
166     if( !strcasecmp( p_input->psz_access, "rtsp" ) )
167     {
168         if( ( scheduler = BasicTaskScheduler::createNew() ) == NULL )
169         {
170             msg_Err( p_input, "BasicTaskScheduler::createNew failed" );
171             return VLC_EGENERIC;
172         }
173         if( ( env = BasicUsageEnvironment::createNew(*scheduler) ) == NULL )
174         {
175             delete scheduler;
176             msg_Err( p_input, "BasicUsageEnvironment::createNew failed" );
177             return VLC_EGENERIC;
178         }
179         if( ( rtsp = RTSPClient::createNew(*env, 1/*verbose*/, "VLC Media Player" ) ) == NULL )
180         {
181             delete env;
182             delete scheduler;
183             msg_Err( p_input, "RTSPClient::createNew failed" );
184             return VLC_EGENERIC;
185         }
186
187         psz_url = (char*)malloc( strlen( p_input->psz_name ) + 8 );
188         sprintf( psz_url, "rtsp://%s", p_input->psz_name );
189
190         p_sys = (access_sys_t*)malloc( sizeof( access_sys_t ) );
191         p_sys->p_sdp = rtsp->describeURL( psz_url );
192
193         if( p_sys->p_sdp == NULL )
194         {
195             msg_Err( p_input, "describeURL failed (%s)", env->getResultMsg() );
196
197             free( psz_url );
198             delete env;
199             delete scheduler;
200             free( p_sys );
201             return VLC_EGENERIC;
202         }
203         free( psz_url );
204         p_sys->i_sdp = strlen( p_sys->p_sdp );
205         p_sys->i_pos = 0;
206
207         //fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
208
209         delete env;
210         delete scheduler;
211
212         var_Create( p_input, "rtsp-tcp", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
213         var_Get( p_input, "rtsp-tcp", &val );
214
215         p_input->p_access_data = p_sys;
216         p_input->i_mtu = 0;
217
218         /* Set exported functions */
219         p_input->pf_read = Read;
220         p_input->pf_seek = NULL;
221         p_input->pf_set_program = input_SetProgram;
222         p_input->pf_set_area = NULL;
223         p_input->p_private = NULL;
224
225         p_input->psz_demux = "live";
226
227         /* Finished to set some variable */
228         vlc_mutex_lock( &p_input->stream.stream_lock );
229         /* FIXME that's not true but eg over tcp, server send data too fast */
230         p_input->stream.b_pace_control = val.b_bool;
231         p_input->stream.p_selected_area->i_tell = 0;
232         p_input->stream.b_seekable = 1; /* Hack to display time */
233         p_input->stream.p_selected_area->i_size = 0;
234         p_input->stream.i_method = INPUT_METHOD_NETWORK;
235         vlc_mutex_unlock( &p_input->stream.stream_lock );
236
237         /* Update default_pts to a suitable value for RTSP access */
238         var_Create( p_input, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
239         var_Get( p_input, "rtsp-caching", &val );
240         p_input->i_pts_delay = val.i_int * 1000;
241
242         return VLC_SUCCESS;
243     }
244     else
245     {
246         p_input->p_access_data = (access_sys_t*)0;
247         p_input->i_mtu = 0;
248         p_input->pf_read = MRLRead;
249         p_input->pf_seek = NULL;
250         p_input->pf_set_program = input_SetProgram;
251         p_input->pf_set_area = NULL;
252         p_input->p_private = NULL;
253         p_input->psz_demux = "live";
254         /* Finished to set some variable */
255         vlc_mutex_lock( &p_input->stream.stream_lock );
256         p_input->stream.b_pace_control = VLC_TRUE;
257         p_input->stream.p_selected_area->i_tell = 0;
258         p_input->stream.b_seekable = VLC_FALSE;
259         p_input->stream.p_selected_area->i_size = 0;
260         p_input->stream.i_method = INPUT_METHOD_NETWORK;
261         vlc_mutex_unlock( &p_input->stream.stream_lock );
262
263         return VLC_SUCCESS;
264     }
265 }
266
267 /*****************************************************************************
268  * AccessClose:
269  *****************************************************************************/
270 static void AccessClose( vlc_object_t *p_this )
271 {
272     input_thread_t *p_input = (input_thread_t *)p_this;
273     access_sys_t   *p_sys = p_input->p_access_data;
274     if( !strcasecmp( p_input->psz_access, "rtsp" ) )
275     {
276         delete[] p_sys->p_sdp;
277         free( p_sys );
278     }
279 }
280
281 /*****************************************************************************
282  * Read:
283  *****************************************************************************/
284 static ssize_t Read ( input_thread_t *p_input, byte_t *p_buffer, size_t i_len )
285 {
286     access_sys_t   *p_sys   = p_input->p_access_data;
287     int            i_copy = __MIN( (int)i_len, p_sys->i_sdp - p_sys->i_pos );
288
289     if( i_copy > 0 )
290     {
291         memcpy( p_buffer, &p_sys->p_sdp[p_sys->i_pos], i_copy );
292         p_sys->i_pos += i_copy;
293     }
294     return i_copy;
295 }
296 /*****************************************************************************
297  * MRLRead: read data from the mrl
298  *****************************************************************************/
299 static ssize_t MRLRead ( input_thread_t *p_input, byte_t *p_buffer, size_t i_len )
300 {
301     int i_done = (int)p_input->p_access_data;
302     int            i_copy = __MIN( (int)i_len, (int)strlen(p_input->psz_name) - i_done );
303
304     if( i_copy > 0 )
305     {
306         memcpy( p_buffer, &p_input->psz_name[i_done], i_copy );
307         i_done += i_copy;
308         p_input->p_access_data = (access_sys_t*)i_done;
309     }
310     return i_copy;
311 }
312
313
314 /*****************************************************************************
315  * DemuxOpen:
316  *****************************************************************************/
317 static int  DemuxOpen ( vlc_object_t *p_this )
318 {
319     input_thread_t *p_input = (input_thread_t *)p_this;
320     demux_sys_t    *p_sys;
321
322     MediaSubsessionIterator *iter;
323     MediaSubsession *sub;
324
325     vlc_value_t val;
326
327     uint8_t *p_peek;
328
329     int     i_sdp;
330     int     i_sdp_max;
331     uint8_t *p_sdp;
332
333     /* See if it looks like a SDP
334        v, o, s fields are mandatory and in this order */
335     if( stream_Peek( p_input->s, &p_peek, 7 ) < 7 )
336     {
337         msg_Err( p_input, "cannot peek" );
338         return VLC_EGENERIC;
339     }
340     if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) && strncmp( (char*)p_peek, "v=0\n", 4 ) &&
341         ( p_input->psz_access == NULL || strcasecmp( p_input->psz_access, "rtsp" ) ||
342           p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
343     {
344         msg_Warn( p_input, "SDP module discarded" );
345         return VLC_EGENERIC;
346     }
347
348     p_input->pf_demux = Demux;
349     p_input->pf_demux_control = Control;
350     p_input->p_demux_data = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
351     p_sys->p_sdp = NULL;
352     p_sys->scheduler = NULL;
353     p_sys->env = NULL;
354     p_sys->ms = NULL;
355     p_sys->rtsp = NULL;
356     p_sys->i_track = 0;
357     p_sys->track   = NULL;
358     p_sys->i_pcr   = 0;
359     p_sys->i_pcr_start = 0;
360     p_sys->i_length = 0;
361     p_sys->i_start = 0;
362
363     /* Gather the complete sdp file */
364     vlc_mutex_lock( &p_input->stream.stream_lock );
365     if( input_InitStream( p_input, 0 ) == -1)
366     {
367         vlc_mutex_unlock( &p_input->stream.stream_lock );
368         msg_Err( p_input, "cannot init stream" );
369         goto error;
370     }
371     vlc_mutex_unlock( &p_input->stream.stream_lock );
372
373     i_sdp = 0;
374     i_sdp_max = 1000;
375     p_sdp = (uint8_t*)malloc( i_sdp_max );
376     for( ;; )
377     {
378         int i_read = stream_Read( p_input->s, &p_sdp[i_sdp], i_sdp_max - i_sdp - 1 );
379
380         if( i_read < 0 )
381         {
382             msg_Err( p_input, "failed to read SDP" );
383             free( p_sys );
384             return VLC_EGENERIC;
385         }
386
387         i_sdp += i_read;
388
389         if( i_read < i_sdp_max - i_sdp - 1 )
390         {
391             p_sdp[i_sdp] = '\0';
392             break;
393         }
394
395         i_sdp_max += 1000;
396         p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
397     }
398     p_sys->p_sdp = (char*)p_sdp;
399
400     fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
401
402     if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
403     {
404         msg_Err( p_input, "BasicTaskScheduler::createNew failed" );
405         goto error;
406     }
407     if( ( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) == NULL )
408     {
409         msg_Err( p_input, "BasicUsageEnvironment::createNew failed" );
410         goto error;
411     }
412     if( p_input->psz_access != NULL && !strcasecmp( p_input->psz_access, "rtsp" ) )
413     {
414         char *psz_url;
415         char *psz_options;
416
417         if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/, "VLC Media Player" ) ) == NULL )
418         {
419             msg_Err( p_input, "RTSPClient::createNew failed (%s)", p_sys->env->getResultMsg() );
420             goto error;
421         }
422         psz_url = (char*)malloc( strlen( p_input->psz_name ) + 8 );
423         sprintf( psz_url, "rtsp://%s", p_input->psz_name );
424
425         psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
426         /* FIXME psz_options -> delete or free */
427         free( psz_url );
428     }
429     if( ( p_sys->ms = MediaSession::createNew(*p_sys->env, p_sys->p_sdp ) ) == NULL )
430     {
431         msg_Err( p_input, "MediaSession::createNew failed" );
432         goto error;
433     }
434
435     var_Create( p_input, "rtsp-tcp", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
436     var_Get( p_input, "rtsp-tcp", &val );
437
438     /* Initialise each media subsession */
439     iter = new MediaSubsessionIterator( *p_sys->ms );
440     while( ( sub = iter->next() ) != NULL )
441     {
442         unsigned int i_buffer = 0;
443
444         /* Value taken from mplayer */
445         if( !strcmp( sub->mediumName(), "audio" ) )
446         {
447             i_buffer = 100000;
448         }
449         else if( !strcmp( sub->mediumName(), "video" ) )
450         {
451             i_buffer = 2000000;
452         }
453         else
454         {
455             continue;
456         }
457
458         if( !sub->initiate() )
459         {
460             msg_Warn( p_input, "RTP subsession '%s/%s' failed(%s)", sub->mediumName(), sub->codecName(), p_sys->env->getResultMsg() );
461         }
462         else
463         {
464             int fd = sub->rtpSource()->RTPgs()->socketNum();
465
466             msg_Dbg( p_input, "RTP subsession '%s/%s'", sub->mediumName(), sub->codecName() );
467
468             /* Increase the buffer size */
469             increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
470             /* Issue the SETUP */
471             if( p_sys->rtsp )
472             {
473                 p_sys->rtsp->setupMediaSubsession( *sub, False, val.b_bool ? True : False );
474             }
475         }
476     }
477
478     if( p_sys->rtsp )
479     {
480         /* The PLAY */
481         if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
482         {
483             msg_Err( p_input, "PLAY failed %s", p_sys->env->getResultMsg() );
484             goto error;
485         }
486     }
487
488     /* Create all es struct */
489     iter->reset();
490     while( ( sub = iter->next() ) != NULL )
491     {
492         live_track_t *tk;
493
494         if( sub->readSource() == NULL )
495         {
496             continue;
497         }
498
499         tk = (live_track_t*)malloc( sizeof( live_track_t ) );
500         tk->p_input = p_input;
501         tk->waiting = 0;
502         tk->i_pts   = 0;
503         tk->b_quicktime = VLC_FALSE;
504         tk->b_rtcp_sync = VLC_FALSE;
505
506         /* Value taken from mplayer */
507         if( !strcmp( sub->mediumName(), "audio" ) )
508         {
509             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
510             tk->fmt.audio.i_channels = sub->numChannels();
511             tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
512
513             if( !strcmp( sub->codecName(), "MPA" ) ||
514                 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
515                 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
516             {
517                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
518                 tk->fmt.audio.i_rate = 0;
519             }
520             else if( !strcmp( sub->codecName(), "AC3" ) )
521             {
522                 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
523                 tk->fmt.audio.i_rate = 0;
524             }
525             else if( !strcmp( sub->codecName(), "L16" ) )
526             {
527                 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
528                 tk->fmt.audio.i_bitspersample = 16;
529             }
530             else if( !strcmp( sub->codecName(), "L8" ) )
531             {
532                 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
533                 tk->fmt.audio.i_bitspersample = 8;
534             }
535             else if( !strcmp( sub->codecName(), "PCMU" ) )
536             {
537                 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
538             }
539             else if( !strcmp( sub->codecName(), "PCMA" ) )
540             {
541                 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
542             }
543             else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
544             {
545                 unsigned int i_extra;
546                 uint8_t      *p_extra;
547
548                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
549
550                 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(), i_extra ) ) )
551                 {
552                     tk->fmt.i_extra = i_extra;
553                     tk->fmt.p_extra = malloc( i_extra );
554                     memcpy( tk->fmt.p_extra, p_extra, i_extra );
555                     delete[] p_extra;
556                 }
557             }
558             else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
559             {
560                 unsigned int i_extra;
561                 uint8_t      *p_extra;
562
563                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
564
565                 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
566                 {
567                     tk->fmt.i_extra = i_extra;
568                     tk->fmt.p_extra = malloc( i_extra );
569                     memcpy( tk->fmt.p_extra, p_extra, i_extra );
570                     delete[] p_extra;
571                 }
572             }
573         }
574         else if( !strcmp( sub->mediumName(), "video" ) )
575         {
576             es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
577             if( !strcmp( sub->codecName(), "MPV" ) )
578             {
579                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
580             }
581             else if( !strcmp( sub->codecName(), "H263" ) ||
582                      !strcmp( sub->codecName(), "H263-1998" ) )
583             {
584                 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
585             }
586             else if( !strcmp( sub->codecName(), "H261" ) )
587             {
588                 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '1' );
589             }
590             else if( !strcmp( sub->codecName(), "JPEG" ) )
591             {
592                 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
593             }
594             else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
595             {
596                 unsigned int i_extra;
597                 uint8_t      *p_extra;
598
599                 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
600
601                 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
602                 {
603                     tk->fmt.i_extra = i_extra;
604                     tk->fmt.p_extra = malloc( i_extra );
605                     memcpy( tk->fmt.p_extra, p_extra, i_extra );
606                     delete[] p_extra;
607                 }
608             }
609             else if( !strcmp( sub->codecName(), "X-QT" ) || !strcmp( sub->codecName(), "X-QUICKTIME" ) )
610             {
611                 tk->b_quicktime = VLC_TRUE;
612             }
613         }
614
615         if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
616         {
617             tk->p_es = es_out_Add( p_input->p_es_out, &tk->fmt );
618         }
619         else
620         {
621             tk->p_es = NULL;
622         }
623
624         if( tk->p_es || tk->b_quicktime )
625         {
626             TAB_APPEND( p_sys->i_track, p_sys->track, tk );
627             tk->readSource = sub->readSource();
628             tk->rtpSource  = sub->rtpSource();
629         }
630         else
631         {
632             free( tk );
633         }
634     }
635
636     delete iter;
637
638     p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
639     if( p_sys->i_length < 0 )
640     {
641         p_sys->i_length = 0;
642     }
643     else if( p_sys->i_length > 0 )
644     {
645         p_input->stream.p_selected_area->i_size = 1000; /* needed for now */
646     }
647
648     if( p_sys->i_track <= 0 )
649     {
650         msg_Err( p_input, "no codec supported, aborting" );
651         goto error;
652     }
653
654     return VLC_SUCCESS;
655
656 error:
657     if( p_sys->ms )
658     {
659         Medium::close( p_sys->ms );
660     }
661     if( p_sys->rtsp )
662     {
663         Medium::close( p_sys->rtsp );
664     }
665     if( p_sys->env )
666     {
667         delete p_sys->env;
668     }
669     if( p_sys->scheduler )
670     {
671         delete p_sys->scheduler;
672     }
673     if( p_sys->p_sdp )
674     {
675         free( p_sys->p_sdp );
676     }
677     free( p_sys );
678     return VLC_EGENERIC;
679 }
680
681
682
683 /*****************************************************************************
684  * DemuxClose:
685  *****************************************************************************/
686 static void DemuxClose( vlc_object_t *p_this )
687 {
688     input_thread_t *p_input = (input_thread_t *)p_this;
689     demux_sys_t    *p_sys = p_input->p_demux_data;
690     int            i;
691
692     for( i = 0; i < p_sys->i_track; i++ )
693     {
694         live_track_t *tk = p_sys->track[i];
695
696         free( tk );
697     }
698     if( p_sys->i_track )
699     {
700         free( p_sys->track );
701     }
702
703     if( p_sys->rtsp && p_sys->ms )
704     {
705         /* TEARDOWN */
706         p_sys->rtsp->teardownMediaSession( *p_sys->ms );
707     }
708     Medium::close( p_sys->ms );
709     if( p_sys->rtsp )
710     {
711         Medium::close( p_sys->rtsp );
712     }
713
714     if( p_sys->env )
715     {
716         delete p_sys->env;
717     }
718     if( p_sys->scheduler )
719     {
720         delete p_sys->scheduler;
721     }
722     if( p_sys->p_sdp )
723     {
724         free( p_sys->p_sdp );
725     }
726     free( p_sys );
727 }
728
729
730 static void StreamRead( void *p_private, unsigned int i_size, struct timeval pts );
731 static void StreamClose( void *p_private );
732 static void TaskInterrupt( void *p_private );
733
734 /*****************************************************************************
735  * Demux:
736  *****************************************************************************/
737 static int  Demux   ( input_thread_t *p_input )
738 {
739     demux_sys_t    *p_sys = p_input->p_demux_data;
740     TaskToken      task;
741
742     mtime_t         i_pcr = 0;
743     int             i;
744
745     for( i = 0; i < p_sys->i_track; i++ )
746     {
747         live_track_t *tk = p_sys->track[i];
748
749         if( i_pcr == 0 )
750         {
751             i_pcr = tk->i_pts;
752         }
753         else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
754         {
755             i_pcr = tk->i_pts ;
756         }
757     }
758     if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
759     {
760         input_ClockManageRef( p_input,
761                               p_input->stream.p_selected_program,
762                               i_pcr * 9 / 100 );
763         p_sys->i_pcr = i_pcr;
764         if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr )
765         {
766             p_sys->i_pcr_start = i_pcr;
767         }
768     }
769
770     /* First warm we want to read data */
771     p_sys->event = 0;
772     for( i = 0; i < p_sys->i_track; i++ )
773     {
774         live_track_t *tk = p_sys->track[i];
775
776         if( tk->waiting == 0 )
777         {
778             tk->waiting = 1;
779             tk->readSource->getNextFrame( tk->buffer, 65536,
780                                           StreamRead, tk,
781                                           StreamClose, tk );
782         }
783     }
784     /* Create a task that will be called if we wait more than 300ms */
785     task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_input );
786
787     /* Do the read */
788     p_sys->scheduler->doEventLoop( &p_sys->event );
789
790     /* remove the task */
791     p_sys->scheduler->unscheduleDelayedTask( task );
792
793     /* Check for gap in pts value */
794     for( i = 0; i < p_sys->i_track; i++ )
795     {
796         live_track_t *tk = p_sys->track[i];
797
798         if( !tk->b_rtcp_sync && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
799         {
800             msg_Dbg( p_input, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
801             p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
802             tk->b_rtcp_sync = VLC_TRUE;
803
804             /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
805             tk->i_pts = 0;
806             p_sys->i_pcr_start = 0;
807             p_sys->i_pcr = 0;
808             i_pcr = 0;
809         }
810     }
811
812     return p_input->b_error ? 0 : 1;
813 }
814
815 static int    Control( input_thread_t *p_input, int i_query, va_list args )
816 {
817     demux_sys_t *p_sys = p_input->p_demux_data;
818     int64_t *pi64;
819     double  *pf, f;
820
821     switch( i_query )
822     {
823         case DEMUX_GET_TIME:
824             pi64 = (int64_t*)va_arg( args, int64_t * );
825             *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
826             return VLC_SUCCESS;
827
828         case DEMUX_GET_LENGTH:
829             pi64 = (int64_t*)va_arg( args, int64_t * );
830             *pi64 = p_sys->i_length;
831             return VLC_SUCCESS;
832
833         case DEMUX_GET_POSITION:
834             pf = (double*)va_arg( args, double* );
835             if( p_sys->i_length > 0 )
836             {
837                 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start)/
838                       (double)(p_sys->i_length);
839             }
840             else
841             {
842                 *pf = 0;
843             }
844             return VLC_SUCCESS;
845
846         case DEMUX_SET_POSITION:
847         {
848             float time;
849
850             f = (double)va_arg( args, double );
851             time = f * (double)p_sys->i_length / 1000000.0;   /* in second */
852
853             if( p_sys->rtsp && p_sys->i_length > 0 )
854             {
855                 MediaSubsessionIterator *iter = new MediaSubsessionIterator( *p_sys->ms );
856                 MediaSubsession         *sub;
857                 int i;
858
859                 while( ( sub = iter->next() ) != NULL )
860                 {
861                     p_sys->rtsp->playMediaSubsession( *sub, time );
862                 }
863                 delete iter;
864                 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
865                 p_sys->i_pcr_start = 0;
866                 p_sys->i_pcr       = 0;
867                 for( i = 0; i < p_sys->i_track; i++ )
868                 {
869                     p_sys->track[i]->i_pts = 0;
870                 }
871                 return VLC_SUCCESS;
872             }
873             return VLC_EGENERIC;
874         }
875
876         default:
877             return demux_vaControlDefault( p_input, i_query, args );
878     }
879 }
880
881 /*****************************************************************************
882  *
883  *****************************************************************************/
884 static void StreamRead( void *p_private, unsigned int i_size, struct timeval pts )
885 {
886     live_track_t   *tk = (live_track_t*)p_private;
887     input_thread_t *p_input = tk->p_input;
888     demux_sys_t    *p_sys = p_input->p_demux_data;
889     block_t        *p_block;
890
891     mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) + (uint64_t)pts.tv_usec;
892
893     /* XXX Beurk beurk beurk Avoid having negative value XXX */
894     i_pts &= UI64C(0x00ffffffffffffff);
895
896     if( tk->b_quicktime && tk->p_es == NULL )
897     {
898         QuickTimeGenericRTPSource *qtRTPSource = (QuickTimeGenericRTPSource*)tk->rtpSource;
899         QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
900         uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
901
902         if( qtState.sdAtomSize < 16 + 32 )
903         {
904             /* invalid */
905             p_sys->event = 0xff;
906             tk->waiting = 0;
907             return;
908         }
909         tk->fmt.i_codec = VLC_FOURCC( sdAtom[0], sdAtom[1], sdAtom[2], sdAtom[3] );
910         tk->fmt.video.i_width  = (sdAtom[28] << 8) | sdAtom[29];
911         tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
912
913         tk->fmt.i_extra        = qtState.sdAtomSize - 16;
914         tk->fmt.p_extra        = malloc( tk->fmt.i_extra );
915         memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
916
917         tk->p_es = es_out_Add( p_input->p_es_out, &tk->fmt );
918     }
919
920 #if 0
921     fprintf( stderr, "StreamRead size=%d pts=%lld\n",
922              i_size,
923              pts.tv_sec * 1000000LL + pts.tv_usec );
924 #endif
925     if( i_size > 65536 )
926     {
927         msg_Warn( p_input, "buffer overflow" );
928     }
929     /* FIXME could i_size be > buffer size ? */
930     p_block = block_New( p_input, i_size );
931
932     memcpy( p_block->p_buffer, tk->buffer, i_size );
933     //p_block->i_rate = p_input->stream.control.i_rate;
934
935     if( i_pts != tk->i_pts )
936     {
937         p_block->i_dts =
938         p_block->i_pts = input_ClockGetTS( p_input,
939                                          p_input->stream.p_selected_program,
940                                          i_pts * 9 / 100 );
941     }
942     else
943     {
944         p_block->i_dts = 0;
945         p_block->i_pts = 0;
946     }
947     //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
948
949     es_out_Send( p_input->p_es_out, tk->p_es, p_block );
950
951     /* warm that's ok */
952     p_sys->event = 0xff;
953
954     /* we have read data */
955     tk->waiting = 0;
956
957     if( i_pts > 0 )
958     {
959         tk->i_pts = i_pts;
960     }
961 }
962
963 /*****************************************************************************
964  *
965  *****************************************************************************/
966 static void StreamClose( void *p_private )
967 {
968     live_track_t   *tk = (live_track_t*)p_private;
969     input_thread_t *p_input = tk->p_input;
970     demux_sys_t    *p_sys = p_input->p_demux_data;
971
972     fprintf( stderr, "StreamClose\n" );
973
974     p_sys->event = 0xff;
975     p_input->b_error = VLC_TRUE;
976 }
977
978
979 /*****************************************************************************
980  *
981  *****************************************************************************/
982 static void TaskInterrupt( void *p_private )
983 {
984     input_thread_t *p_input = (input_thread_t*)p_private;
985
986     fprintf( stderr, "TaskInterrupt\n" );
987
988     /* Avoid lock */
989     p_input->p_demux_data->event = 0xff;
990 }
991