livedotcom.cpp 41.1 KB
Newer Older
1
2
3
/*****************************************************************************
 * live.cpp : live.com support.
 *****************************************************************************
4
 * Copyright (C) 2003-2004 VideoLAN
Carlo Calabrò's avatar
Carlo Calabrò committed
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/input.h>
31
#include "network.h"
32
33
34

#include <iostream>

gbazin's avatar
   
gbazin committed
35
36
37
38
#if defined( WIN32 )
#   include <winsock2.h>
#endif

39
40
41
42
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"
#include "liveMedia.hh"

43
44
45
46
extern "C" {
#include "../access/mms/asf.h"  /* Who said ugly ? */
}

hartman's avatar
hartman committed
47
48
49
50
51
52
#if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
#define RECLAIM_ENV(env) delete (env)
#else
#define RECLAIM_ENV(env) (env)->reclaim()
#endif

53
54
55
56
57
using namespace std;

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
58
59
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
60

hartman's avatar
hartman committed
61
#define CACHING_TEXT N_("Caching value (ms)")
Laurent Aimar's avatar
Laurent Aimar committed
62
#define CACHING_LONGTEXT N_( \
Carlo Calabrò's avatar
Carlo Calabrò committed
63
    "Allows you to modify the default caching value for RTSP streams. This " \
64
    "value should be set in millisecond units." )
Laurent Aimar's avatar
Laurent Aimar committed
65

66
#define KASENNA_TEXT N_( "Kasenna RTSP dialect")
67
68
69
#define KASENNA_LONGTEXT N_( "Kasenna server speak an old and unstandard " \
    "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
    "for communication. In this mode you cannot talk to normal RTSP servers." )
70

71
72
vlc_module_begin();
    set_description( _("live.com (RTSP/RTP/SDP) demuxer" ) );
73
    set_capability( "demux2", 50 );
zorglub's avatar
zorglub committed
74
    set_shortname( "Live.com RTP/RTSP");
Laurent Aimar's avatar
Laurent Aimar committed
75
    set_callbacks( Open, Close );
76
    add_shortcut( "live" );
zorglub's avatar
zorglub committed
77
78
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
79
80

    add_submodule();
Laurent Aimar's avatar
Laurent Aimar committed
81
        set_description( _("RTSP/RTP access and demux") );
82
        add_shortcut( "rtsp" );
sigmunau's avatar
sigmunau committed
83
        add_shortcut( "sdp" );
Laurent Aimar's avatar
Laurent Aimar committed
84
85
        set_capability( "access_demux", 0 );
        set_callbacks( Open, Close );
hartman's avatar
hartman committed
86
        add_bool( "rtsp-tcp", 0, NULL,
Carlo Calabrò's avatar
Carlo Calabrò committed
87
88
                  N_("Use RTP over RTSP (TCP)"),
                  N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
hartman's avatar
hartman committed
89
90
        add_integer( "rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
            CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
91
92
        add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
                  KASENNA_LONGTEXT, VLC_TRUE );
93
94
95
vlc_module_end();

/* TODO:
96
 *  - Improve support of PS/TS
Laurent Aimar's avatar
Laurent Aimar committed
97
 *  - Support X-QT/X-QUICKTIME generic codec for audio.
98
 *
Laurent Aimar's avatar
Laurent Aimar committed
99
100
 *  - Check memory leak, delete/free -> still one when using rtsp-tcp but I'm
 *  not sure if it comes from me.
101
102
103
104
 *
 */

/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
105
 * Local prototypes
106
107
108
109
110
111
112
 *****************************************************************************/
typedef struct
{
    demux_t     *p_demux;

    vlc_bool_t   b_quicktime;
    vlc_bool_t   b_muxed;
113
    vlc_bool_t   b_asf;
114
115
116
117
118
119
120
121
122
123

    es_format_t  fmt;
    es_out_id_t  *p_es;

    stream_t     *p_out_muxed;    /* for muxed stream */

    RTPSource    *rtpSource;
    FramedSource *readSource;
    vlc_bool_t   b_rtcp_sync;

124
125
    uint8_t      *p_buffer;
    unsigned int  i_buffer;
126
127
128
129

    char         waiting;

    mtime_t      i_pts;
130

131
132
133
134
135
} live_track_t;

struct demux_sys_t
{
    char         *p_sdp;    /* XXX mallocated */
136
    char         *psz_path; /* URL-encoded path */
137
138
139
140
141
142

    MediaSession     *ms;
    TaskScheduler    *scheduler;
    UsageEnvironment *env ;
    RTSPClient       *rtsp;

143
    /* */
144
145
146
147
148
    int              i_track;
    live_track_t     **track;   /* XXX mallocated */
    mtime_t          i_pcr;
    mtime_t          i_pcr_start;

149
150
151
152
153
    /* Asf */
    asf_header_t     asfh;
    stream_t         *p_out_asf;

    /* */
154
155
156
    mtime_t          i_length;
    mtime_t          i_start;

157
    /* */
158
    vlc_bool_t       b_multicast;   /* true if one of the tracks is multicasted */
159
160
161
    vlc_bool_t       b_no_data;     /* true if we never receive any data */
    int              i_no_data_ti;  /* consecutive number of TaskInterrupt */

162
163
164
165
166
167
    char             event;
};

static int Demux  ( demux_t * );
static int Control( demux_t *, int, va_list );

168
169
static int ParseASF( demux_t * );

170
171
static int RollOverTcp( demux_t * );

172
173
174
175
static void StreamRead( void *, unsigned int, unsigned int,
                        struct timeval, unsigned int );
static void StreamClose( void * );
static void TaskInterrupt( void * );
176

177
178
179
/*****************************************************************************
 * DemuxOpen:
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
180
static int  Open ( vlc_object_t *p_this )
181
{
182
183
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
184
185
186
187

    MediaSubsessionIterator *iter;
    MediaSubsession *sub;

Laurent Aimar's avatar
Laurent Aimar committed
188
    vlc_bool_t b_rtsp_tcp;
189
190
191
192
193
194
    uint8_t *p_peek;

    int     i_sdp;
    int     i_sdp_max;
    uint8_t *p_sdp;

Laurent Aimar's avatar
Laurent Aimar committed
195
    if( p_demux->s )
196
    {
Laurent Aimar's avatar
Laurent Aimar committed
197
198
        /* See if it looks like a SDP
           v, o, s fields are mandatory and in this order */
199
200
        if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;

201
202
        if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
            strncmp( (char*)p_peek, "v=0\n", 4 ) &&
Laurent Aimar's avatar
Laurent Aimar committed
203
204
205
206
            ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
        {
            return VLC_EGENERIC;
        }
207
    }
Laurent Aimar's avatar
Laurent Aimar committed
208
    else
209
    {
Laurent Aimar's avatar
Laurent Aimar committed
210
        var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
211
212
    }

213
214
215
    p_demux->pf_demux  = Demux;
    p_demux->pf_control= Control;
    p_demux->p_sys     = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
216
217
218
219
220
221
222
    p_sys->p_sdp = NULL;
    p_sys->scheduler = NULL;
    p_sys->env = NULL;
    p_sys->ms = NULL;
    p_sys->rtsp = NULL;
    p_sys->i_track = 0;
    p_sys->track   = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
223
224
    p_sys->i_pcr   = 0;
    p_sys->i_pcr_start = 0;
Laurent Aimar's avatar
Laurent Aimar committed
225
226
    p_sys->i_length = 0;
    p_sys->i_start = 0;
227
    p_sys->p_out_asf = NULL;
228
229
    p_sys->b_no_data = VLC_TRUE;
    p_sys->i_no_data_ti = 0;
230
    p_sys->b_multicast = VLC_FALSE;
231
    p_sys->psz_path = p_demux->psz_path;
232
233
234
235


    if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
    {
236
        msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
237
238
        goto error;
    }
239
    if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
240
    {
241
        msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
242
243
        goto error;
    }
Laurent Aimar's avatar
Laurent Aimar committed
244

245
246
    if( strcasecmp( p_demux->psz_access, "sdp" ) && 
	vlc_UrlIsNotEncoded( p_sys->psz_path ) )
247
248
249
250
251
252
    {
        p_sys->psz_path = vlc_UrlEncode( p_sys->psz_path );
        if( p_sys->psz_path == NULL )
            goto error;
    }

Laurent Aimar's avatar
Laurent Aimar committed
253
    if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
254
255
256
257
    {
        char *psz_url;
        char *psz_options;

258
259
        if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
              "VLC Media Player" ) ) == NULL )
260
        {
261
262
            msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
                     p_sys->env->getResultMsg() );
263
264
            goto error;
        }
265
266
        psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
        sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
267
268

        psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
269
270
271
        if( psz_options )
            delete [] psz_options;

272
273
        p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
                              NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
Laurent Aimar's avatar
Laurent Aimar committed
274
275
        if( p_sdp == NULL )
        {
276
277
            msg_Err( p_demux, "describeURL failed (%s)",
                     p_sys->env->getResultMsg() );
Laurent Aimar's avatar
Laurent Aimar committed
278
279
280
            free( psz_url );
            goto error;
        }
281
        free( psz_url );
Laurent Aimar's avatar
Laurent Aimar committed
282
283
284
285
286
287
288
289

        /* malloc-ated copy */
        p_sys->p_sdp = strdup( (char*)p_sdp );
        delete[] p_sdp;
        fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
    }
    else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
    {
290
        p_sys->p_sdp = strdup( p_sys->psz_path );
Laurent Aimar's avatar
Laurent Aimar committed
291
292
293
294
295
296
297
298
299
    }
    else
    {
        /* Gather the complete sdp file */
        i_sdp = 0;
        i_sdp_max = 1000;
        p_sdp = (uint8_t*)malloc( i_sdp_max );
        for( ;; )
        {
300
301
            int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
                                      i_sdp_max - i_sdp - 1 );
Laurent Aimar's avatar
Laurent Aimar committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

            if( i_read < 0 )
            {
                msg_Err( p_demux, "failed to read SDP" );
                free( p_sys );
                return VLC_EGENERIC;
            }

            i_sdp += i_read;

            if( i_read < i_sdp_max - i_sdp - 1 )
            {
                p_sdp[i_sdp] = '\0';
                break;
            }

            i_sdp_max += 1000;
            p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
        }
        p_sys->p_sdp = (char*)p_sdp;

323
        msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
324
    }
325
    if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
326
    {
327
        msg_Err( p_demux, "MediaSession::createNew failed" );
328
329
330
        goto error;
    }

Laurent Aimar's avatar
Laurent Aimar committed
331
    b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
332
333
334
335
336
337

    /* Initialise each media subsession */
    iter = new MediaSubsessionIterator( *p_sys->ms );
    while( ( sub = iter->next() ) != NULL )
    {
        unsigned int i_buffer = 0;
338
        Boolean bInit;
339
340
341
342
343
344
345
346
347

        /* Value taken from mplayer */
        if( !strcmp( sub->mediumName(), "audio" ) )
            i_buffer = 100000;
        else if( !strcmp( sub->mediumName(), "video" ) )
            i_buffer = 2000000;
        else
            continue;

348
349
350
351
352
353
        if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            bInit = sub->initiate( 4 ); /* Constant ? */
        else
            bInit = sub->initiate();

        if( !bInit )
354
        {
355
356
357
            msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
                      sub->mediumName(), sub->codecName(),
                      p_sys->env->getResultMsg() );
358
359
360
        }
        else
        {
361
362
363
364
365
366
            if( sub->rtpSource() )
            {
                int fd = sub->rtpSource()->RTPgs()->socketNum();
                /* Increase the buffer size */
                increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
            }
367

368
369
            msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
                     sub->codecName() );
370
371
372
373

            /* Issue the SETUP */
            if( p_sys->rtsp )
            {
374
375
                p_sys->rtsp->setupMediaSubsession( *sub, False,
                                                   b_rtsp_tcp ? True : False );
376
            }
377
378
379
380
381
            if( !p_sys->b_multicast )
            {
                /* Check, because we need diff. rollover behaviour for multicast */
                p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
            }
382
383
384
385
386
387
388
389
        }
    }

    if( p_sys->rtsp )
    {
        /* The PLAY */
        if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
        {
390
            msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
            goto error;
        }
    }

    /* Create all es struct */
    iter->reset();
    while( ( sub = iter->next() ) != NULL )
    {
        live_track_t *tk;

        if( sub->readSource() == NULL )
        {
            continue;
        }

        tk = (live_track_t*)malloc( sizeof( live_track_t ) );
407
        tk->p_demux = p_demux;
408
        tk->waiting = 0;
409
        tk->i_pts   = 0;
Laurent Aimar's avatar
Laurent Aimar committed
410
        tk->b_quicktime = VLC_FALSE;
411
        tk->b_muxed     = VLC_FALSE;
412
        tk->b_asf       = VLC_FALSE;
413
        tk->b_rtcp_sync = VLC_FALSE;
414
415
        tk->p_out_muxed = NULL;
        tk->p_es        = NULL;
416
417
        tk->i_buffer    = 65536;
        tk->p_buffer    = (uint8_t *)malloc( 65536 );
418
419
420
421

        /* Value taken from mplayer */
        if( !strcmp( sub->mediumName(), "audio" ) )
        {
422
            es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
423
            tk->fmt.audio.i_channels = sub->numChannels();
gbazin's avatar
   
gbazin committed
424
            tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
425
426
427
428
429
430

            if( !strcmp( sub->codecName(), "MPA" ) ||
                !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
                !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
gbazin's avatar
   
gbazin committed
431
                tk->fmt.audio.i_rate = 0;
432
433
434
435
            }
            else if( !strcmp( sub->codecName(), "AC3" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
gbazin's avatar
   
gbazin committed
436
                tk->fmt.audio.i_rate = 0;
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
            }
            else if( !strcmp( sub->codecName(), "L16" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
                tk->fmt.audio.i_bitspersample = 16;
            }
            else if( !strcmp( sub->codecName(), "L8" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
                tk->fmt.audio.i_bitspersample = 8;
            }
            else if( !strcmp( sub->codecName(), "PCMU" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
            }
            else if( !strcmp( sub->codecName(), "PCMA" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
            }
            else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
            {
                unsigned int i_extra;
                uint8_t      *p_extra;

                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );

463
464
                if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
                                                         i_extra ) ) )
465
466
                {
                    tk->fmt.i_extra = i_extra;
467
                    tk->fmt.p_extra = malloc( i_extra );
468
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
469
                    delete[] p_extra;
470
471
472
473
474
475
476
477
478
                }
            }
            else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
            {
                unsigned int i_extra;
                uint8_t      *p_extra;

                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );

479
480
                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
                                                       i_extra ) ) )
481
482
483
484
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
485
                    delete[] p_extra;
486
487
                }
            }
488
489
490
491
            else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            {
                tk->b_asf = VLC_TRUE;
                if( p_sys->p_out_asf == NULL )
492
493
                    p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
                                                        p_demux->out );
494
            }
495
496
497
        }
        else if( !strcmp( sub->mediumName(), "video" ) )
        {
498
            es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
499
500
501
502
503
            if( !strcmp( sub->codecName(), "MPV" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
            }
            else if( !strcmp( sub->codecName(), "H263" ) ||
504
505
                     !strcmp( sub->codecName(), "H263-1998" ) ||
                     !strcmp( sub->codecName(), "H263-2000" ) )
506
            {
hartman's avatar
hartman committed
507
                tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
508
509
510
            }
            else if( !strcmp( sub->codecName(), "H261" ) )
            {
hartman's avatar
hartman committed
511
                tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
512
513
514
            }
            else if( !strcmp( sub->codecName(), "JPEG" ) )
            {
Laurent Aimar's avatar
Laurent Aimar committed
515
                tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
516
517
518
519
520
521
522
523
            }
            else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
            {
                unsigned int i_extra;
                uint8_t      *p_extra;

                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );

524
525
                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
                                                       i_extra ) ) )
526
527
528
529
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
530
                    delete[] p_extra;
531
532
                }
            }
533
534
535
536
            else if( !strcmp( sub->codecName(), "X-QT" ) ||
                     !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
                     !strcmp( sub->codecName(), "X-QDM" ) ||
                     !strcmp( sub->codecName(), "X-SV3V-ES" )  ||
537
                     !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
Laurent Aimar's avatar
Laurent Aimar committed
538
539
540
            {
                tk->b_quicktime = VLC_TRUE;
            }
541
542
543
            else if( !strcmp( sub->codecName(), "MP2T" ) )
            {
                tk->b_muxed = VLC_TRUE;
544
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
545
            }
546
547
            else if( !strcmp( sub->codecName(), "MP2P" ) ||
                     !strcmp( sub->codecName(), "MP1S" ) )
548
549
            {
                tk->b_muxed = VLC_TRUE;
550
551
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
                                                   p_demux->out );
552
            }
553
554
555
556
            else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            {
                tk->b_asf = VLC_TRUE;
                if( p_sys->p_out_asf == NULL )
557
558
                    p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
                                                        p_demux->out );;
559
            }
560
561
562
563
        }

        if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
        {
564
            tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
565
566
        }

567
568
569
570
571
        if( sub->rtcpInstance() != NULL )
        {
            sub->rtcpInstance()->setByeHandler( StreamClose, tk );
        }

572
        if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
573
574
        {
            tk->readSource = sub->readSource();
Laurent Aimar's avatar
Laurent Aimar committed
575
            tk->rtpSource  = sub->rtpSource();
576
577
578
579

            /* Append */
            p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
            p_sys->track[p_sys->i_track++] = tk;
580
581
582
583
584
585
586
587
588
        }
        else
        {
            free( tk );
        }
    }

    delete iter;

589
590
591
592
593
594
595
    if( p_sys->p_out_asf && ParseASF( p_demux ) )
    {
        msg_Err( p_demux, "cannot find a usable asf header" );
        /* TODO Clean tracks */
        goto error;
    }

Laurent Aimar's avatar
Laurent Aimar committed
596
597
598
599
600
601
602
    p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
    if( p_sys->i_length < 0 )
    {
        p_sys->i_length = 0;
    }
    else if( p_sys->i_length > 0 )
    {
603
604
        /* FIXME */
        /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
Laurent Aimar's avatar
Laurent Aimar committed
605
606
607
608
    }

    if( p_sys->i_track <= 0 )
    {
609
        msg_Err( p_demux, "no codec supported, aborting" );
Laurent Aimar's avatar
Laurent Aimar committed
610
611
612
        goto error;
    }

613
614
615
    return VLC_SUCCESS;

error:
616
617
618
619
    if( p_sys->p_out_asf )
    {
        stream_DemuxDelete( p_sys->p_out_asf );
    }
620
621
622
623
624
625
626
627
628
629
    if( p_sys->ms )
    {
        Medium::close( p_sys->ms );
    }
    if( p_sys->rtsp )
    {
        Medium::close( p_sys->rtsp );
    }
    if( p_sys->env )
    {
hartman's avatar
hartman committed
630
        RECLAIM_ENV(p_sys->env);
631
632
633
634
635
636
637
638
639
    }
    if( p_sys->scheduler )
    {
        delete p_sys->scheduler;
    }
    if( p_sys->p_sdp )
    {
        free( p_sys->p_sdp );
    }
640
641
642
643
    if( ( p_sys->psz_path != NULL )
     && ( p_sys->psz_path != p_demux->psz_path ) )
        free( p_sys->psz_path );

644
645
646
647
648
649
650
    free( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * DemuxClose:
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
651
static void Close( vlc_object_t *p_this )
652
{
653
    demux_t *p_demux = (demux_t*)p_this;
654
655
    demux_sys_t *p_sys = p_demux->p_sys;
    int i;
656
657
658
659
660

    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

661
        if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
662
        free( tk->p_buffer );
663
664
        free( tk );
    }
665
666
667

    if( p_sys->i_track ) free( p_sys->track );
    if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
668

669
    if( p_sys->rtsp && p_sys->ms )
670
671
    {
        /* TEARDOWN */
672
        p_sys->rtsp->teardownMediaSession( *p_sys->ms );
673
    }
674

675
676
    Medium::close( p_sys->ms );

677
678
679
680
    if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
    if( p_sys->env ) RECLAIM_ENV(p_sys->env);
    if( p_sys->scheduler ) delete p_sys->scheduler;
    if( p_sys->p_sdp ) free( p_sys->p_sdp );
681
682
    if( p_sys->psz_path != p_demux->psz_path )
        free( p_sys->psz_path );
683
684
685
686
687
688
    free( p_sys );
}

/*****************************************************************************
 * Demux:
 *****************************************************************************/
689
static int Demux( demux_t *p_demux )
690
{
691
    demux_sys_t    *p_sys = p_demux->p_sys;
692
693
    TaskToken      task;

694
    vlc_bool_t      b_send_pcr = VLC_TRUE;
Laurent Aimar's avatar
Laurent Aimar committed
695
    mtime_t         i_pcr = 0;
696
697
    int             i;

Laurent Aimar's avatar
Laurent Aimar committed
698
699
700
701
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

702
703
704
        if( tk->b_asf || tk->b_muxed )
            b_send_pcr = VLC_FALSE;

Laurent Aimar's avatar
Laurent Aimar committed
705
706
707
708
709
710
711
712
713
        if( i_pcr == 0 )
        {
            i_pcr = tk->i_pts;
        }
        else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
        {
            i_pcr = tk->i_pts ;
        }
    }
Laurent Aimar's avatar
Laurent Aimar committed
714
    if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
715
716
    {
        p_sys->i_pcr = i_pcr;
717

718
719
        if( b_send_pcr )
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
Laurent Aimar's avatar
Laurent Aimar committed
720
721
        if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
            ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
Laurent Aimar's avatar
Laurent Aimar committed
722
723
724
725
726
        {
            p_sys->i_pcr_start = i_pcr;
        }
    }

727
728
729
730
731
732
733
734
735
    /* First warm we want to read data */
    p_sys->event = 0;
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

        if( tk->waiting == 0 )
        {
            tk->waiting = 1;
736
            tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
737
                                          StreamRead, tk, StreamClose, tk );
738
739
740
        }
    }
    /* Create a task that will be called if we wait more than 300ms */
741
    task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
742
743
744
745
746
747
748

    /* Do the read */
    p_sys->scheduler->doEventLoop( &p_sys->event );

    /* remove the task */
    p_sys->scheduler->unscheduleDelayedTask( task );

749
750
751
752
753
    /* Check for gap in pts value */
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

754
755
        if( !tk->b_muxed && !tk->b_rtcp_sync &&
            tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
756
        {
757
758
759
            msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );

            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
760
761
762
763
764
765
766
767
768
769
            tk->b_rtcp_sync = VLC_TRUE;

            /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
            tk->i_pts = 0;
            p_sys->i_pcr_start = 0;
            p_sys->i_pcr = 0;
            i_pcr = 0;
        }
    }

770
771
    if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
    {
772
        /* FIXME Make this configurable
773
774
        msg_Err( p_demux, "no multicast data received in 36s, aborting" );
        return 0;
775
        */
776
    }
hartman's avatar
hartman committed
777
    else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
778
779
780
781
782
    {
        vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );

        if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
        {
hartman's avatar
hartman committed
783
            msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
784
785
786
787
788
789
790
            if( RollOverTcp( p_demux ) )
            {
                msg_Err( p_demux, "TCP rollover failed, aborting" );
                return 0;
            }
            var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
        }
hartman's avatar
hartman committed
791
        else if( p_sys->i_no_data_ti > 34 )
792
        {
hartman's avatar
hartman committed
793
            msg_Err( p_demux, "no data received in 10s, aborting" );
794
795
796
            return 0;
        }
    }
hartman's avatar
hartman committed
797
    else if( !p_sys->b_multicast && p_sys->b_no_data&& p_sys->i_no_data_ti > 34 )
798
799
    {
        /* EOF ? */
hartman's avatar
hartman committed
800
        msg_Warn( p_demux, "no data received in 10s, eof ?" );
801
802
803
	return 0;
    }

804
    return p_demux->b_error ? 0 : 1;
805
806
}

807
808
809
810
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
Laurent Aimar's avatar
Laurent Aimar committed
811
{
812
    demux_sys_t *p_sys = p_demux->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
813
    int64_t *pi64;
Laurent Aimar's avatar
Laurent Aimar committed
814
    double  *pf, f;
815
    vlc_bool_t *pb, b_bool;
816

Laurent Aimar's avatar
Laurent Aimar committed
817
818
819
820
    switch( i_query )
    {
        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
Laurent Aimar's avatar
Laurent Aimar committed
821
822
823
824
825
826
827
828
829
830
831
832
            *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
            return VLC_SUCCESS;

        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            *pi64 = p_sys->i_length;
            return VLC_SUCCESS;

        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double* );
            if( p_sys->i_length > 0 )
            {
833
834
                *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
                                p_sys->i_start ) / (double)(p_sys->i_length);
Laurent Aimar's avatar
Laurent Aimar committed
835
836
837
838
839
            }
            else
            {
                *pf = 0;
            }
Laurent Aimar's avatar
Laurent Aimar committed
840
841
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
842
843
844
845
846
847
848
849
850
        case DEMUX_SET_POSITION:
        {
            float time;

            f = (double)va_arg( args, double );
            time = f * (double)p_sys->i_length / 1000000.0;   /* in second */

            if( p_sys->rtsp && p_sys->i_length > 0 )
            {
851
852
853
                MediaSubsessionIterator *iter =
                    new MediaSubsessionIterator( *p_sys->ms );
                MediaSubsession *sub;
Laurent Aimar's avatar
Laurent Aimar committed
854
855
856
857
858
859
860
861
862
863
                int i;

                while( ( sub = iter->next() ) != NULL )
                {
                    p_sys->rtsp->playMediaSubsession( *sub, time );
                }
                delete iter;
                p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
                p_sys->i_pcr_start = 0;
                p_sys->i_pcr       = 0;
864
                
Laurent Aimar's avatar
Laurent Aimar committed
865
866
867
868
                for( i = 0; i < p_sys->i_track; i++ )
                {
                    p_sys->track[i]->i_pts = 0;
                }
869

Laurent Aimar's avatar
Laurent Aimar committed
870
871
                return VLC_SUCCESS;
            }
872
            return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
873
874
        }

Laurent Aimar's avatar
Laurent Aimar committed
875
876
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
877
            pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
878
            if( p_sys->rtsp && p_sys->i_length )
879
880
                /* Not always true, but will be handled in SET_PAUSE_STATE */
                *pb = VLC_TRUE;
881
882
883
884
            else
                *pb = VLC_FALSE;
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
885
886
        case DEMUX_CAN_CONTROL_PACE:
            pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
887
888
889

#if 0       /* Disable for now until we have a clock synchro algo
             * which works with something else than MPEG over UDP */
Laurent Aimar's avatar
Laurent Aimar committed
890
            *pb = VLC_FALSE;
891
892
#endif
            *pb = VLC_TRUE;
Laurent Aimar's avatar
Laurent Aimar committed
893
894
895
            return VLC_SUCCESS;

        case DEMUX_SET_PAUSE_STATE:
896
            double d_npt;
897
898
899
            MediaSubsessionIterator *iter;
            MediaSubsession *sub;

900
901
            d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
                                p_sys->i_start ) ) / 1000000.00;
902

903
904
905
906
907
908
909
910
            b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
            if( p_sys->rtsp == NULL )
                return VLC_EGENERIC;

            iter = new MediaSubsessionIterator( *p_sys->ms );
            while( ( sub = iter->next() ) != NULL )
            {
                if( ( b_bool && !p_sys->rtsp->pauseMediaSubsession( *sub ) ) ||
911
912
                    ( !b_bool && !p_sys->rtsp->playMediaSubsession( *sub,
                      d_npt > 0 ? d_npt : -1 ) ) )
913
914
915
916
917
918
                {
                    delete iter;
                    return VLC_EGENERIC;
                }
            }
            delete iter;
919
#if 0
920
921
922
            /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
            for( i = 0; i < p_sys->i_track; i++ )
            {
923
                p_sys->track[i]->i_pts = 0;
924
925
926
            }
            p_sys->i_pcr_start = 0; /* FIXME Wrong */
            p_sys->i_pcr = 0;
927
#endif
928
929
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
930
931
932
933
934
935
936
        case DEMUX_GET_TITLE_INFO:
        case DEMUX_SET_TITLE:
        case DEMUX_SET_SEEKPOINT:
            return VLC_EGENERIC;

        case DEMUX_GET_PTS_DELAY:
            pi64 = (int64_t*)va_arg( args, int64_t * );
937
            *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
Laurent Aimar's avatar
Laurent Aimar committed
938
939
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
940
        default:
941
            return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
942
943
    }
}
944

945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*****************************************************************************
 * RollOverTcp: reopen the rtsp into TCP mode
 * XXX: ugly, a lot of code are duplicated from Open()
 *****************************************************************************/
static int RollOverTcp( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    MediaSubsessionIterator *iter;
    MediaSubsession *sub;
    char *psz_url;
    char *psz_options;
    uint8_t *p_sdp;
    int i_tk;

    /* We close the old RTSP session */
    p_sys->rtsp->teardownMediaSession( *p_sys->ms );

    Medium::close( p_sys->ms );
    Medium::close( p_sys->rtsp );

    p_sys->ms = NULL;
    p_sys->rtsp = NULL;

    /* Reopen rtsp client */
    if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
          "VLC Media Player" ) ) == NULL )
    {
        msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
                 p_sys->env->getResultMsg() );
        return VLC_EGENERIC;
    }

977
    asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

    if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
        delete [] psz_options;

    p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
                          NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
    free( psz_url );
    if( p_sdp == NULL )
    {
        msg_Err( p_demux, "describeURL failed (%s)",
                 p_sys->env->getResultMsg() );
        return VLC_EGENERIC;
    }

    /* malloc-ated copy */
    p_sys->p_sdp = strdup( (char*)p_sdp );
    delete[] p_sdp;

    if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
    {
        msg_Err( p_demux, "MediaSession::createNew failed" );
        return VLC_EGENERIC;
    }