livedotcom.cpp 35.5 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
31
32
33
 *
 * 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>

#include <iostream>

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

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

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

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

52
53
54
55
56
using namespace std;

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

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

65
#define KASENNA_TEXT N_( "Kasenna RTSP dialect")
66
67
68
#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." )
69

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

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

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

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

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

    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;

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

    char         waiting;

    mtime_t      i_pts;
129

130
131
132
133
134
135
136
137
138
139
140
} live_track_t;

struct demux_sys_t
{
    char         *p_sdp;    /* XXX mallocated */

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

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

147
148
149
150
151
    /* Asf */
    asf_header_t     asfh;
    stream_t         *p_out_asf;

    /* */
152
153
154
155
156
157
158
159
160
    mtime_t          i_length;
    mtime_t          i_start;

    char             event;
};

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

161
162
static int ParseASF( demux_t * );

163
164
165
166
static void StreamRead( void *, unsigned int, unsigned int,
                        struct timeval, unsigned int );
static void StreamClose( void * );
static void TaskInterrupt( void * );
167

168
169
170
/*****************************************************************************
 * DemuxOpen:
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
171
static int  Open ( vlc_object_t *p_this )
172
{
173
174
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
175
176
177
178

    MediaSubsessionIterator *iter;
    MediaSubsession *sub;

Laurent Aimar's avatar
Laurent Aimar committed
179
    vlc_bool_t b_rtsp_tcp;
180
181
182
183
184
185
    uint8_t *p_peek;

    int     i_sdp;
    int     i_sdp_max;
    uint8_t *p_sdp;

Laurent Aimar's avatar
Laurent Aimar committed
186
    if( p_demux->s )
187
    {
Laurent Aimar's avatar
Laurent Aimar committed
188
189
190
191
192
193
194
        /* See if it looks like a SDP
           v, o, s fields are mandatory and in this order */
        if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 )
        {
            msg_Err( p_demux, "cannot peek" );
            return VLC_EGENERIC;
        }
195
196
        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
197
198
199
200
201
            ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
        {
            msg_Warn( p_demux, "SDP module discarded" );
            return VLC_EGENERIC;
        }
202
    }
Laurent Aimar's avatar
Laurent Aimar committed
203
    else
204
    {
Laurent Aimar's avatar
Laurent Aimar committed
205
        var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
206
207
    }

208
209
210
    p_demux->pf_demux  = Demux;
    p_demux->pf_control= Control;
    p_demux->p_sys     = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
211
212
213
214
215
216
217
    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
218
219
    p_sys->i_pcr   = 0;
    p_sys->i_pcr_start = 0;
Laurent Aimar's avatar
Laurent Aimar committed
220
221
    p_sys->i_length = 0;
    p_sys->i_start = 0;
222
    p_sys->p_out_asf = NULL;
223
224
225
226


    if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
    {
227
        msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
228
229
        goto error;
    }
230
    if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
231
    {
232
        msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
233
234
        goto error;
    }
Laurent Aimar's avatar
Laurent Aimar committed
235
236

    if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
237
238
239
240
    {
        char *psz_url;
        char *psz_options;

241
242
        if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
              "VLC Media Player" ) ) == NULL )
243
        {
244
245
            msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
                     p_sys->env->getResultMsg() );
246
247
            goto error;
        }
248
249
        psz_url = (char*)malloc( strlen( p_demux->psz_path ) + 8 );
        sprintf( psz_url, "rtsp://%s", p_demux->psz_path );
250
251

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

255
256
        p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
                              NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
Laurent Aimar's avatar
Laurent Aimar committed
257
258
        if( p_sdp == NULL )
        {
259
260
            msg_Err( p_demux, "describeURL failed (%s)",
                     p_sys->env->getResultMsg() );
Laurent Aimar's avatar
Laurent Aimar committed
261
262
263
            free( psz_url );
            goto error;
        }
264
        free( psz_url );
Laurent Aimar's avatar
Laurent Aimar committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

        /* 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" ) )
    {
        p_sys->p_sdp = strdup( p_demux->psz_path );
    }
    else
    {
        /* Gather the complete sdp file */
        i_sdp = 0;
        i_sdp_max = 1000;
        p_sdp = (uint8_t*)malloc( i_sdp_max );
        for( ;; )
        {
283
284
            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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

            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;

306
        msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
307
    }
308
    if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
309
    {
310
        msg_Err( p_demux, "MediaSession::createNew failed" );
311
312
313
        goto error;
    }

Laurent Aimar's avatar
Laurent Aimar committed
314
    b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
315
316
317
318
319
320

    /* Initialise each media subsession */
    iter = new MediaSubsessionIterator( *p_sys->ms );
    while( ( sub = iter->next() ) != NULL )
    {
        unsigned int i_buffer = 0;
321
        Boolean bInit;
322
323
324
325
326
327
328
329
330

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

331
332
333
334
335
336
        if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            bInit = sub->initiate( 4 ); /* Constant ? */
        else
            bInit = sub->initiate();

        if( !bInit )
337
        {
338
339
340
            msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
                      sub->mediumName(), sub->codecName(),
                      p_sys->env->getResultMsg() );
341
342
343
        }
        else
        {
344
345
346
347
348
349
            if( sub->rtpSource() )
            {
                int fd = sub->rtpSource()->RTPgs()->socketNum();
                /* Increase the buffer size */
                increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
            }
350

351
352
            msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
                     sub->codecName() );
353
354
355
356

            /* Issue the SETUP */
            if( p_sys->rtsp )
            {
357
358
                p_sys->rtsp->setupMediaSubsession( *sub, False,
                                                   b_rtsp_tcp ? True : False );
359
360
361
362
363
364
365
366
367
            }
        }
    }

    if( p_sys->rtsp )
    {
        /* The PLAY */
        if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
        {
368
            msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
            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 ) );
385
        tk->p_demux = p_demux;
386
        tk->waiting = 0;
387
        tk->i_pts   = 0;
Laurent Aimar's avatar
Laurent Aimar committed
388
        tk->b_quicktime = VLC_FALSE;
389
        tk->b_muxed     = VLC_FALSE;
390
        tk->b_asf       = VLC_FALSE;
391
        tk->b_rtcp_sync = VLC_FALSE;
392
393
        tk->p_out_muxed = NULL;
        tk->p_es        = NULL;
394
395
        tk->i_buffer    = 65536;
        tk->p_buffer    = (uint8_t *)malloc( 65536 );
396
397
398
399

        /* Value taken from mplayer */
        if( !strcmp( sub->mediumName(), "audio" ) )
        {
400
            es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
401
            tk->fmt.audio.i_channels = sub->numChannels();
gbazin's avatar
   
gbazin committed
402
            tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
403
404
405
406
407
408

            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
409
                tk->fmt.audio.i_rate = 0;
410
411
412
413
            }
            else if( !strcmp( sub->codecName(), "AC3" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
gbazin's avatar
   
gbazin committed
414
                tk->fmt.audio.i_rate = 0;
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
            }
            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' );

441
442
                if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
                                                         i_extra ) ) )
443
444
                {
                    tk->fmt.i_extra = i_extra;
445
                    tk->fmt.p_extra = malloc( i_extra );
446
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
447
                    delete[] p_extra;
448
449
450
451
452
453
454
455
456
                }
            }
            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' );

457
458
                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
                                                       i_extra ) ) )
459
460
461
462
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
463
                    delete[] p_extra;
464
465
                }
            }
466
467
468
469
            else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            {
                tk->b_asf = VLC_TRUE;
                if( p_sys->p_out_asf == NULL )
470
471
                    p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
                                                        p_demux->out );
472
            }
473
474
475
        }
        else if( !strcmp( sub->mediumName(), "video" ) )
        {
476
            es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
477
478
479
480
481
            if( !strcmp( sub->codecName(), "MPV" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
            }
            else if( !strcmp( sub->codecName(), "H263" ) ||
482
483
                     !strcmp( sub->codecName(), "H263-1998" ) ||
                     !strcmp( sub->codecName(), "H263-2000" ) )
484
            {
hartman's avatar
hartman committed
485
                tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
486
487
488
            }
            else if( !strcmp( sub->codecName(), "H261" ) )
            {
hartman's avatar
hartman committed
489
                tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
490
491
492
            }
            else if( !strcmp( sub->codecName(), "JPEG" ) )
            {
Laurent Aimar's avatar
Laurent Aimar committed
493
                tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
494
495
496
497
498
499
500
501
            }
            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' );

502
503
                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
                                                       i_extra ) ) )
504
505
506
507
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
508
                    delete[] p_extra;
509
510
                }
            }
511
512
513
514
            else if( !strcmp( sub->codecName(), "X-QT" ) ||
                     !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
                     !strcmp( sub->codecName(), "X-QDM" ) ||
                     !strcmp( sub->codecName(), "X-SV3V-ES" )  ||
515
                     !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
Laurent Aimar's avatar
Laurent Aimar committed
516
517
518
            {
                tk->b_quicktime = VLC_TRUE;
            }
519
520
521
            else if( !strcmp( sub->codecName(), "MP2T" ) )
            {
                tk->b_muxed = VLC_TRUE;
522
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
523
            }
524
525
            else if( !strcmp( sub->codecName(), "MP2P" ) ||
                     !strcmp( sub->codecName(), "MP1S" ) )
526
527
            {
                tk->b_muxed = VLC_TRUE;
528
529
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
                                                   p_demux->out );
530
            }
531
532
533
534
            else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            {
                tk->b_asf = VLC_TRUE;
                if( p_sys->p_out_asf == NULL )
535
536
                    p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
                                                        p_demux->out );;
537
            }
538
539
540
541
        }

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

545
546
547
548
549
        if( sub->rtcpInstance() != NULL )
        {
            sub->rtcpInstance()->setByeHandler( StreamClose, tk );
        }

550
        if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
551
552
        {
            tk->readSource = sub->readSource();
Laurent Aimar's avatar
Laurent Aimar committed
553
            tk->rtpSource  = sub->rtpSource();
554
555
556
557

            /* 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;
558
559
560
561
562
563
564
565
566
        }
        else
        {
            free( tk );
        }
    }

    delete iter;

567
568
569
570
571
572
573
    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
574
575
576
577
578
579
580
    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 )
    {
581
582
        /* FIXME */
        /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
Laurent Aimar's avatar
Laurent Aimar committed
583
584
585
586
    }

    if( p_sys->i_track <= 0 )
    {
587
        msg_Err( p_demux, "no codec supported, aborting" );
Laurent Aimar's avatar
Laurent Aimar committed
588
589
590
        goto error;
    }

591
592
593
    return VLC_SUCCESS;

error:
594
595
596
597
    if( p_sys->p_out_asf )
    {
        stream_DemuxDelete( p_sys->p_out_asf );
    }
598
599
600
601
602
603
604
605
606
607
    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
608
        RECLAIM_ENV(p_sys->env);
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
    }
    if( p_sys->scheduler )
    {
        delete p_sys->scheduler;
    }
    if( p_sys->p_sdp )
    {
        free( p_sys->p_sdp );
    }
    free( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * DemuxClose:
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
625
static void Close( vlc_object_t *p_this )
626
{
627
    demux_t *p_demux = (demux_t*)p_this;
628
629
    demux_sys_t *p_sys = p_demux->p_sys;
    int i;
630
631
632
633
634

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

635
        if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
636
        free( tk->p_buffer );
637
638
        free( tk );
    }
639
640
641

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

643
    if( p_sys->rtsp && p_sys->ms )
644
645
    {
        /* TEARDOWN */
646
        p_sys->rtsp->teardownMediaSession( *p_sys->ms );
647
    }
648

649
650
    Medium::close( p_sys->ms );

651
652
653
654
    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 );
655
656
657
658
659
660
    free( p_sys );
}

/*****************************************************************************
 * Demux:
 *****************************************************************************/
661
static int Demux( demux_t *p_demux )
662
{
663
    demux_sys_t    *p_sys = p_demux->p_sys;
664
665
    TaskToken      task;

666
    vlc_bool_t      b_send_pcr = VLC_TRUE;
Laurent Aimar's avatar
Laurent Aimar committed
667
    mtime_t         i_pcr = 0;
668
669
    int             i;

Laurent Aimar's avatar
Laurent Aimar committed
670
671
672
673
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

674
675
676
        if( tk->b_asf || tk->b_muxed )
            b_send_pcr = VLC_FALSE;

Laurent Aimar's avatar
Laurent Aimar committed
677
678
679
680
681
682
683
684
685
        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
686
    if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
687
688
    {
        p_sys->i_pcr = i_pcr;
689

690
691
        if( b_send_pcr )
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
Laurent Aimar's avatar
Laurent Aimar committed
692
693
        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
694
695
696
697
698
        {
            p_sys->i_pcr_start = i_pcr;
        }
    }

699
700
701
702
703
704
705
706
707
    /* 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;
708
            tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
709
                                          StreamRead, tk, StreamClose, tk );
710
711
712
        }
    }
    /* Create a task that will be called if we wait more than 300ms */
713
    task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
714
715
716
717
718
719
720

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

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

721
722
723
724
725
    /* Check for gap in pts value */
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

726
727
        if( !tk->b_muxed && !tk->b_rtcp_sync &&
            tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
728
        {
729
730
731
            msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );

            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
732
733
734
735
736
737
738
739
740
741
            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;
        }
    }

742
    return p_demux->b_error ? 0 : 1;
743
744
}

745
746
747
748
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
Laurent Aimar's avatar
Laurent Aimar committed
749
{
750
    demux_sys_t *p_sys = p_demux->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
751
    int64_t *pi64;
Laurent Aimar's avatar
Laurent Aimar committed
752
    double  *pf, f;
753
    vlc_bool_t *pb, b_bool;
754

Laurent Aimar's avatar
Laurent Aimar committed
755
756
757
758
    switch( i_query )
    {
        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
Laurent Aimar's avatar
Laurent Aimar committed
759
760
761
762
763
764
765
766
767
768
769
770
            *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 )
            {
771
772
                *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
773
774
775
776
777
            }
            else
            {
                *pf = 0;
            }
Laurent Aimar's avatar
Laurent Aimar committed
778
779
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
780
781
782
783
784
785
786
787
788
        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 )
            {
789
790
791
                MediaSubsessionIterator *iter =
                    new MediaSubsessionIterator( *p_sys->ms );
                MediaSubsession *sub;
Laurent Aimar's avatar
Laurent Aimar committed
792
793
794
795
796
797
798
799
800
801
                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;
802
                
Laurent Aimar's avatar
Laurent Aimar committed
803
804
805
806
                for( i = 0; i < p_sys->i_track; i++ )
                {
                    p_sys->track[i]->i_pts = 0;
                }
807

Laurent Aimar's avatar
Laurent Aimar committed
808
809
                return VLC_SUCCESS;
            }
810
            return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
811
812
        }

Laurent Aimar's avatar
Laurent Aimar committed
813
814
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
815
            pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
816
            if( p_sys->rtsp && p_sys->i_length )
817
818
                /* Not always true, but will be handled in SET_PAUSE_STATE */
                *pb = VLC_TRUE;
819
820
821
822
            else
                *pb = VLC_FALSE;
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
823
824
        case DEMUX_CAN_CONTROL_PACE:
            pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
825
826
827

#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
828
            *pb = VLC_FALSE;
829
830
#endif
            *pb = VLC_TRUE;
Laurent Aimar's avatar
Laurent Aimar committed
831
832
833
            return VLC_SUCCESS;

        case DEMUX_SET_PAUSE_STATE:
834
            double d_npt;
835
836
837
            MediaSubsessionIterator *iter;
            MediaSubsession *sub;

838
839
            d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
                                p_sys->i_start ) ) / 1000000.00;
840

841
842
843
844
845
846
847
848
            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 ) ) ||
849
850
                    ( !b_bool && !p_sys->rtsp->playMediaSubsession( *sub,
                      d_npt > 0 ? d_npt : -1 ) ) )
851
852
853
854
855
856
                {
                    delete iter;
                    return VLC_EGENERIC;
                }
            }
            delete iter;
857
#if 0
858
859
860
            /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
            for( i = 0; i < p_sys->i_track; i++ )
            {
861
                p_sys->track[i]->i_pts = 0;
862
863
864
            }
            p_sys->i_pcr_start = 0; /* FIXME Wrong */
            p_sys->i_pcr = 0;
865
#endif
866
867
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
868
869
870
871
872
873
874
        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 * );
875
            *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
Laurent Aimar's avatar
Laurent Aimar committed
876
877
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
878
        default:
879
            return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
880
881
    }
}
882
883
884
885

/*****************************************************************************
 *
 *****************************************************************************/
886
887
888
static void StreamRead( void *p_private, unsigned int i_size,
                        unsigned int i_truncated_bytes, struct timeval pts,
                        unsigned int duration )
889
890
{
    live_track_t   *tk = (live_track_t*)p_private;
891
892
    demux_t        *p_demux = tk->p_demux;
    demux_sys_t    *p_sys = p_demux->p_sys;
gbazin's avatar
   
gbazin committed
893
    block_t        *p_block;
894

895
896
    mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
        (uint64_t)pts.tv_usec;
897
898

    /* XXX Beurk beurk beurk Avoid having negative value XXX */
899
    i_pts &= UI64C(0x00ffffffffffffff);
900

Laurent Aimar's avatar
Laurent Aimar committed
901
902
    if( tk->b_quicktime && tk->p_es == NULL )
    {
903
904
        QuickTimeGenericRTPSource *qtRTPSource =
            (QuickTimeGenericRTPSource*)tk->rtpSource;
Laurent Aimar's avatar
Laurent Aimar committed
905
906
907
908
909
910
911
912
913
914
        QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
        uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];

        if( qtState.sdAtomSize < 16 + 32 )
        {
            /* invalid */
            p_sys->event = 0xff;
            tk->waiting = 0;
            return;
        }
915
        tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
Laurent Aimar's avatar
Laurent Aimar committed
916
917
918
919
920
921
922
        tk->fmt.video.i_width  = (sdAtom[28] << 8) | sdAtom[29];
        tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];

        tk->fmt.i_extra        = qtState.sdAtomSize - 16;
        tk->fmt.p_extra        = malloc( tk->fmt.i_extra );
        memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );

923
        tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
Laurent Aimar's avatar
Laurent Aimar committed
924
925
    }

926
927
928
929
930
#if 0
    fprintf( stderr, "StreamRead size=%d pts=%lld\n",
             i_size,
             pts.tv_sec * 1000000LL + pts.tv_usec );
#endif
931
932
933

    /* grow buffer if it looks like buffer is too small, but don't eat
     * up all the memory on strange streams */
934
    if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
935
936
    {
        void *p_tmp;
937
        msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
938
939
        msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
        tk->i_buffer *= 2;
940
941
942
943
944
945
946
947
948
949
950
        p_tmp = realloc( tk->p_buffer, tk->i_buffer );
        if (p_tmp == NULL)
        {
            msg_Warn( p_demux, "realloc failed" );
        }
        else
        {
            tk->p_buffer = (uint8_t*)p_tmp;
        }
    }
    if( i_size > tk->i_buffer )
Laurent Aimar's avatar
Laurent Aimar committed
951
    {
952
        msg_Warn( p_demux, "buffer overflow" );
Laurent Aimar's avatar
Laurent Aimar committed
953
    }
954
    /* FIXME could i_size be > buffer size ? */
sigmunau's avatar
sigmunau committed
955
956
    if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') )
    {
957
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
958
        H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
sigmunau's avatar
sigmunau committed
959
        uint32_t header = h261Source->lastSpecialHeader();
960
961
962
963
#else
        uint32_t header = 0;
        msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
#endif
964
        p_block = block_New( p_demux, i_size + 4 );
sigmunau's avatar
sigmunau committed
965
        memcpy( p_block->p_buffer, &header, 4 );
966
        memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
sigmunau's avatar
sigmunau committed
967
    }
968
969
    else if( tk->b_asf )
    {
970
        int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
971
972
        p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );

973
        memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
974
    }
sigmunau's avatar
sigmunau committed
975
976
    else
    {
977
        p_block = block_New( p_demux, i_size );
978
        memcpy( p_block->p_buffer, tk->p_buffer, i_size );
sigmunau's avatar
sigmunau committed
979
    }
sigmunau's avatar
sigmunau committed
980
981
    if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') &&
        tk->rtpSource->curPacketMarkerBit() )
982
    {
sigmunau's avatar
sigmunau committed
983
        p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
sigmunau's avatar
sigmunau committed
984
    }
gbazin's avatar
   
gbazin committed
985
    //p_block->i_rate = p_input->stream.control.i_rate;
986

987
    if( i_pts != tk->i_pts && !tk->b_muxed )
988
    {
989
990
        p_block->i_dts = i_pts;
        p_block->i_pts = i_pts;
991
    }
992
993
994
995
996
997
    //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );

    if( tk->b_muxed )
    {
        stream_DemuxSend( tk->p_out_muxed, p_block );
    }
998
999
1000
    else if( tk->b_asf )
    {
        stream_DemuxSend( p_sys->p_out_asf, p_block );
For faster browsing, not all history is shown. View entire blame