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