livedotcom.cpp 30.2 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
42
43
44
45
46
47
48
49
50
51
52
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"
#include "liveMedia.hh"

using namespace std;

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  DemuxOpen ( vlc_object_t * );
static void DemuxClose( vlc_object_t * );

static int  AccessOpen ( vlc_object_t * );
static void AccessClose( vlc_object_t * );

hartman's avatar
hartman committed
53
#define CACHING_TEXT N_("Caching value (ms)")
Laurent Aimar's avatar
Laurent Aimar committed
54
#define CACHING_LONGTEXT N_( \
Carlo Calabrò's avatar
Carlo Calabrò committed
55
    "Allows you to modify the default caching value for RTSP streams. This " \
Laurent Aimar's avatar
Laurent Aimar committed
56
57
    "value should be set in miliseconds units." )

58
59
vlc_module_begin();
    set_description( _("live.com (RTSP/RTP/SDP) demuxer" ) );
60
    set_capability( "demux2", 50 );
61
62
63
64
65
66
    set_callbacks( DemuxOpen, DemuxClose );
    add_shortcut( "live" );

    add_submodule();
        set_description( _("RTSP/RTP describe") );
        add_shortcut( "rtsp" );
sigmunau's avatar
sigmunau committed
67
        add_shortcut( "sdp" );
68
69
        set_capability( "access", 0 );
        set_callbacks( AccessOpen, AccessClose );
hartman's avatar
hartman committed
70
        add_bool( "rtsp-tcp", 0, NULL,
Carlo Calabrò's avatar
Carlo Calabrò committed
71
72
                  N_("Use RTP over RTSP (TCP)"),
                  N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
hartman's avatar
hartman committed
73
74
        add_integer( "rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
            CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
75
76
77
vlc_module_end();

/* TODO:
78
 *  - Improve support of PS/TS
Laurent Aimar's avatar
Laurent Aimar committed
79
 *  - Support X-QT/X-QUICKTIME generic codec for audio.
80
 *
Laurent Aimar's avatar
Laurent Aimar committed
81
82
 *  - Check memory leak, delete/free -> still one when using rtsp-tcp but I'm
 *  not sure if it comes from me.
83
84
85
86
 *
 */

/*****************************************************************************
87
 * Local prototypes for access
88
89
90
91
92
93
94
95
96
 *****************************************************************************/
struct access_sys_t
{
    int     i_sdp;
    char    *p_sdp;

    int     i_pos;
};

Laurent Aimar's avatar
Laurent Aimar committed
97
static ssize_t Read   ( input_thread_t *, byte_t *, size_t );
sigmunau's avatar
sigmunau committed
98
static ssize_t MRLRead( input_thread_t *, byte_t *, size_t );
Laurent Aimar's avatar
Laurent Aimar committed
99

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*****************************************************************************
 * AccessOpen:
 *****************************************************************************/
static int  AccessOpen( vlc_object_t *p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
    access_sys_t   *p_sys;

    TaskScheduler    *scheduler = NULL;
    UsageEnvironment *env       = NULL;
    RTSPClient       *rtsp      = NULL;

    vlc_value_t      val;
    char             *psz_url;

sigmunau's avatar
sigmunau committed
115
    if( p_input->psz_access == NULL || ( strcasecmp( p_input->psz_access, "rtsp" ) && strcasecmp( p_input->psz_access, "sdp" ) ) )
116
117
118
119
    {
        msg_Warn( p_input, "RTSP access discarded" );
        return VLC_EGENERIC;
    }
sigmunau's avatar
sigmunau committed
120
    if( !strcasecmp( p_input->psz_access, "rtsp" ) )
121
    {
sigmunau's avatar
sigmunau committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        if( ( scheduler = BasicTaskScheduler::createNew() ) == NULL )
        {
            msg_Err( p_input, "BasicTaskScheduler::createNew failed" );
            return VLC_EGENERIC;
        }
        if( ( env = BasicUsageEnvironment::createNew(*scheduler) ) == NULL )
        {
            delete scheduler;
            msg_Err( p_input, "BasicUsageEnvironment::createNew failed" );
            return VLC_EGENERIC;
        }
        if( ( rtsp = RTSPClient::createNew(*env, 1/*verbose*/, "VLC Media Player" ) ) == NULL )
        {
            delete env;
            delete scheduler;
            msg_Err( p_input, "RTSPClient::createNew failed" );
            return VLC_EGENERIC;
        }

        psz_url = (char*)malloc( strlen( p_input->psz_name ) + 8 );
        sprintf( psz_url, "rtsp://%s", p_input->psz_name );

        p_sys = (access_sys_t*)malloc( sizeof( access_sys_t ) );
        p_sys->p_sdp = rtsp->describeURL( psz_url );

        if( p_sys->p_sdp == NULL )
        {
            msg_Err( p_input, "describeURL failed (%s)", env->getResultMsg() );

            free( psz_url );
            delete env;
            delete scheduler;
            free( p_sys );
            return VLC_EGENERIC;
        }
        free( psz_url );
        p_sys->i_sdp = strlen( p_sys->p_sdp );
        p_sys->i_pos = 0;
160

sigmunau's avatar
sigmunau committed
161
        //fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
162
163
164

        delete env;
        delete scheduler;
sigmunau's avatar
sigmunau committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

        var_Create( p_input, "rtsp-tcp", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
        var_Get( p_input, "rtsp-tcp", &val );

        p_input->p_access_data = p_sys;
        p_input->i_mtu = 0;

        /* Set exported functions */
        p_input->pf_read = Read;
        p_input->pf_seek = NULL;
        p_input->pf_set_program = input_SetProgram;
        p_input->pf_set_area = NULL;
        p_input->p_private = NULL;

        p_input->psz_demux = "live";

        /* Finished to set some variable */
        vlc_mutex_lock( &p_input->stream.stream_lock );
        /* FIXME that's not true but eg over tcp, server send data too fast */
        p_input->stream.b_pace_control = val.b_bool;
        p_input->stream.p_selected_area->i_tell = 0;
        p_input->stream.b_seekable = 1; /* Hack to display time */
187
        p_input->stream.p_selected_area->i_size = p_sys->i_sdp;
sigmunau's avatar
sigmunau committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        p_input->stream.i_method = INPUT_METHOD_NETWORK;
        vlc_mutex_unlock( &p_input->stream.stream_lock );

        /* Update default_pts to a suitable value for RTSP access */
        var_Create( p_input, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
        var_Get( p_input, "rtsp-caching", &val );
        p_input->i_pts_delay = val.i_int * 1000;

        return VLC_SUCCESS;
    }
    else
    {
        p_input->p_access_data = (access_sys_t*)0;
        p_input->i_mtu = 0;
        p_input->pf_read = MRLRead;
        p_input->pf_seek = NULL;
        p_input->pf_set_program = input_SetProgram;
        p_input->pf_set_area = NULL;
        p_input->p_private = NULL;
        p_input->psz_demux = "live";
        /* Finished to set some variable */
        vlc_mutex_lock( &p_input->stream.stream_lock );
        p_input->stream.b_pace_control = VLC_TRUE;
        p_input->stream.p_selected_area->i_tell = 0;
        p_input->stream.b_seekable = VLC_FALSE;
213
        p_input->stream.p_selected_area->i_size = strlen(p_input->psz_name);
sigmunau's avatar
sigmunau committed
214
215
        p_input->stream.i_method = INPUT_METHOD_NETWORK;
        vlc_mutex_unlock( &p_input->stream.stream_lock );
216
217

        return VLC_SUCCESS;
218
219
220
221
222
223
224
225
226
227
    }
}

/*****************************************************************************
 * AccessClose:
 *****************************************************************************/
static void AccessClose( vlc_object_t *p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
    access_sys_t   *p_sys = p_input->p_access_data;
sigmunau's avatar
sigmunau committed
228
229
230
231
232
    if( !strcasecmp( p_input->psz_access, "rtsp" ) )
    {
        delete[] p_sys->p_sdp;
        free( p_sys );
    }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
}

/*****************************************************************************
 * Read:
 *****************************************************************************/
static ssize_t Read ( input_thread_t *p_input, byte_t *p_buffer, size_t i_len )
{
    access_sys_t   *p_sys   = p_input->p_access_data;
    int            i_copy = __MIN( (int)i_len, p_sys->i_sdp - p_sys->i_pos );

    if( i_copy > 0 )
    {
        memcpy( p_buffer, &p_sys->p_sdp[p_sys->i_pos], i_copy );
        p_sys->i_pos += i_copy;
    }
    return i_copy;
}
sigmunau's avatar
sigmunau committed
250
251
252
253
254
255
/*****************************************************************************
 * MRLRead: read data from the mrl
 *****************************************************************************/
static ssize_t MRLRead ( input_thread_t *p_input, byte_t *p_buffer, size_t i_len )
{
    int i_done = (int)p_input->p_access_data;
256
    int            i_copy = __MIN( (int)i_len, (int)strlen(p_input->psz_name) - i_done );
sigmunau's avatar
sigmunau committed
257
258
259
260
261
262
263
264
265

    if( i_copy > 0 )
    {
        memcpy( p_buffer, &p_input->psz_name[i_done], i_copy );
        i_done += i_copy;
        p_input->p_access_data = (access_sys_t*)i_done;
    }
    return i_copy;
}
266
267


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*****************************************************************************
 * Local prototypes for demux2
 *****************************************************************************/
typedef struct
{
    demux_t     *p_demux;

    vlc_bool_t   b_quicktime;
    vlc_bool_t   b_muxed;

    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;

    uint8_t      buffer[65536];

    char         waiting;

    mtime_t      i_pts;
} live_track_t;

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

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

    int              i_track;
    live_track_t     **track;   /* XXX mallocated */
    mtime_t          i_pcr;
    mtime_t          i_pcr_start;

    mtime_t          i_length;
    mtime_t          i_start;

    char             event;
};

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

317
318
319
320
321
/*****************************************************************************
 * DemuxOpen:
 *****************************************************************************/
static int  DemuxOpen ( vlc_object_t *p_this )
{
322
323
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
324
325
326
327
328
329
330
331
332
333
334
335
336
337

    MediaSubsessionIterator *iter;
    MediaSubsession *sub;

    vlc_value_t val;

    uint8_t *p_peek;

    int     i_sdp;
    int     i_sdp_max;
    uint8_t *p_sdp;

    /* See if it looks like a SDP
       v, o, s fields are mandatory and in this order */
338
    if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 )
339
    {
340
        msg_Err( p_demux, "cannot peek" );
341
342
        return VLC_EGENERIC;
    }
343
    if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) && strncmp( (char*)p_peek, "v=0\n", 4 ) &&
344
        ( strcasecmp( p_demux->psz_access, "rtsp" ) || p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
345
    {
346
        msg_Warn( p_demux, "SDP module discarded" );
347
348
349
        return VLC_EGENERIC;
    }

350
351
352
    p_demux->pf_demux  = Demux;
    p_demux->pf_control= Control;
    p_demux->p_sys     = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
353
354
355
356
357
358
359
    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
360
361
    p_sys->i_pcr   = 0;
    p_sys->i_pcr_start = 0;
Laurent Aimar's avatar
Laurent Aimar committed
362
363
    p_sys->i_length = 0;
    p_sys->i_start = 0;
364
365
366
367
368
369
370

    /* Gather the complete sdp file */
    i_sdp = 0;
    i_sdp_max = 1000;
    p_sdp = (uint8_t*)malloc( i_sdp_max );
    for( ;; )
    {
371
        int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp], i_sdp_max - i_sdp - 1 );
372
373
374

        if( i_read < 0 )
        {
375
            msg_Err( p_demux, "failed to read SDP" );
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
            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;

    fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );

    if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
    {
397
        msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
398
399
400
401
        goto error;
    }
    if( ( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) == NULL )
    {
402
        msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
403
404
        goto error;
    }
405
    if( !strcasecmp( p_demux->psz_access, "rtsp" ) )
406
407
408
409
410
411
    {
        char *psz_url;
        char *psz_options;

        if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/, "VLC Media Player" ) ) == NULL )
        {
412
            msg_Err( p_demux, "RTSPClient::createNew failed (%s)", p_sys->env->getResultMsg() );
413
414
            goto error;
        }
415
416
        psz_url = (char*)malloc( strlen( p_demux->psz_path ) + 8 );
        sprintf( psz_url, "rtsp://%s", p_demux->psz_path );
417
418
419
420
421
422
423

        psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
        /* FIXME psz_options -> delete or free */
        free( psz_url );
    }
    if( ( p_sys->ms = MediaSession::createNew(*p_sys->env, p_sys->p_sdp ) ) == NULL )
    {
424
        msg_Err( p_demux, "MediaSession::createNew failed" );
425
426
427
        goto error;
    }

428
429
    var_Create( p_demux, "rtsp-tcp", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
    var_Get( p_demux, "rtsp-tcp", &val );
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

    /* Initialise each media subsession */
    iter = new MediaSubsessionIterator( *p_sys->ms );
    while( ( sub = iter->next() ) != NULL )
    {
        unsigned int i_buffer = 0;

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

        if( !sub->initiate() )
        {
453
            msg_Warn( p_demux, "RTP subsession '%s/%s' failed(%s)", sub->mediumName(), sub->codecName(), p_sys->env->getResultMsg() );
454
455
456
457
458
        }
        else
        {
            int fd = sub->rtpSource()->RTPgs()->socketNum();

459
            msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(), sub->codecName() );
460
461
462
463
464
465

            /* Increase the buffer size */
            increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
            /* Issue the SETUP */
            if( p_sys->rtsp )
            {
Laurent Aimar's avatar
Laurent Aimar committed
466
                p_sys->rtsp->setupMediaSubsession( *sub, False, val.b_bool ? True : False );
467
468
469
470
471
472
473
474
475
            }
        }
    }

    if( p_sys->rtsp )
    {
        /* The PLAY */
        if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
        {
476
            msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
            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 ) );
493
        tk->p_demux = p_demux;
494
        tk->waiting = 0;
495
        tk->i_pts   = 0;
Laurent Aimar's avatar
Laurent Aimar committed
496
        tk->b_quicktime = VLC_FALSE;
497
        tk->b_muxed     = VLC_FALSE;
498
        tk->b_rtcp_sync = VLC_FALSE;
499
500
        tk->p_out_muxed = NULL;
        tk->p_es        = NULL;
501
502
503
504
505
506

        /* Value taken from mplayer */
        if( !strcmp( sub->mediumName(), "audio" ) )
        {
            es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
            tk->fmt.audio.i_channels = sub->numChannels();
gbazin's avatar
   
gbazin committed
507
            tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
508
509
510
511
512
513

            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
514
                tk->fmt.audio.i_rate = 0;
515
516
517
518
            }
            else if( !strcmp( sub->codecName(), "AC3" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
gbazin's avatar
   
gbazin committed
519
                tk->fmt.audio.i_rate = 0;
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
            }
            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' );

                if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(), i_extra ) ) )
                {
                    tk->fmt.i_extra = i_extra;
549
                    tk->fmt.p_extra = malloc( i_extra );
550
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
551
                    delete[] p_extra;
552
553
554
555
556
557
558
559
560
561
562
563
564
565
                }
            }
            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' );

                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
566
                    delete[] p_extra;
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
                }
            }
        }
        else if( !strcmp( sub->mediumName(), "video" ) )
        {
            es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
            if( !strcmp( sub->codecName(), "MPV" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
            }
            else if( !strcmp( sub->codecName(), "H263" ) ||
                     !strcmp( sub->codecName(), "H263-1998" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
            }
            else if( !strcmp( sub->codecName(), "H261" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '1' );
            }
            else if( !strcmp( sub->codecName(), "JPEG" ) )
            {
Laurent Aimar's avatar
Laurent Aimar committed
588
                tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
589
590
591
592
593
594
595
596
597
598
599
600
601
            }
            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' );

                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
602
                    delete[] p_extra;
603
604
                }
            }
Laurent Aimar's avatar
Laurent Aimar committed
605
606
607
608
            else if( !strcmp( sub->codecName(), "X-QT" ) || !strcmp( sub->codecName(), "X-QUICKTIME" ) )
            {
                tk->b_quicktime = VLC_TRUE;
            }
609
610
611
            else if( !strcmp( sub->codecName(), "MP2T" ) )
            {
                tk->b_muxed = VLC_TRUE;
612
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ts2", p_demux->out );
613
614
615
616
            }
            else if( !strcmp( sub->codecName(), "MP2P" ) || !strcmp( sub->codecName(), "MP1S" ) )   /* FIXME check MP1S */
            {
                tk->b_muxed = VLC_TRUE;
617
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ps2", p_demux->out );
618
            }
619
620
621
622
        }

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

626
        if( tk->p_es || tk->b_quicktime || tk->b_muxed )
627
628
        {
            tk->readSource = sub->readSource();
Laurent Aimar's avatar
Laurent Aimar committed
629
            tk->rtpSource  = sub->rtpSource();
630
631
632
633

            /* 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;
634
635
636
637
638
639
640
641
642
        }
        else
        {
            free( tk );
        }
    }

    delete iter;

Laurent Aimar's avatar
Laurent Aimar committed
643
644
645
646
647
648
649
    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 )
    {
650
651
        /* FIXME */
        /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
Laurent Aimar's avatar
Laurent Aimar committed
652
653
654
655
    }

    if( p_sys->i_track <= 0 )
    {
656
        msg_Err( p_demux, "no codec supported, aborting" );
Laurent Aimar's avatar
Laurent Aimar committed
657
658
659
        goto error;
    }

660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
    return VLC_SUCCESS;

error:
    if( p_sys->ms )
    {
        Medium::close( p_sys->ms );
    }
    if( p_sys->rtsp )
    {
        Medium::close( p_sys->rtsp );
    }
    if( p_sys->env )
    {
        delete p_sys->env;
    }
    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:
 *****************************************************************************/
static void DemuxClose( vlc_object_t *p_this )
{
694
695
    demux_t *p_demux = (demux_t*)p_this;
    demux_sys_t    *p_sys = p_demux->p_sys;
696
697
698
699
700
701
    int            i;

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

702
703
704
705
706
        if( tk->b_muxed )
        {
            stream_DemuxDelete( tk->p_out_muxed );
        }

707
708
709
710
711
712
713
        free( tk );
    }
    if( p_sys->i_track )
    {
        free( p_sys->track );
    }

714
    if( p_sys->rtsp && p_sys->ms )
715
716
    {
        /* TEARDOWN */
717
        p_sys->rtsp->teardownMediaSession( *p_sys->ms );
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
    }
    Medium::close( p_sys->ms );
    if( p_sys->rtsp )
    {
        Medium::close( p_sys->rtsp );
    }

    if( p_sys->env )
    {
        delete p_sys->env;
    }
    if( p_sys->scheduler )
    {
        delete p_sys->scheduler;
    }
    if( p_sys->p_sdp )
    {
        free( p_sys->p_sdp );
    }
    free( p_sys );
}


static void StreamRead( void *p_private, unsigned int i_size, struct timeval pts );
static void StreamClose( void *p_private );
static void TaskInterrupt( void *p_private );

/*****************************************************************************
 * Demux:
 *****************************************************************************/
748
static int Demux( demux_t *p_demux )
749
{
750
    demux_sys_t    *p_sys = p_demux->p_sys;
751
752
    TaskToken      task;

Laurent Aimar's avatar
Laurent Aimar committed
753
    mtime_t         i_pcr = 0;
754
755
    int             i;

Laurent Aimar's avatar
Laurent Aimar committed
756
757
758
759
760
761
762
763
764
765
766
767
768
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

        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
769
    if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
770
771
    {
        p_sys->i_pcr = i_pcr;
772
773

        es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
Laurent Aimar's avatar
Laurent Aimar committed
774
775
776
777
778
779
        if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr )
        {
            p_sys->i_pcr_start = i_pcr;
        }
    }

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
    /* 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;
            tk->readSource->getNextFrame( tk->buffer, 65536,
                                          StreamRead, tk,
                                          StreamClose, tk );
        }
    }
    /* Create a task that will be called if we wait more than 300ms */
795
    task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
796
797
798
799
800
801
802

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

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

803
804
805
806
807
    /* Check for gap in pts value */
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

808
        if( !tk->b_muxed && !tk->b_rtcp_sync && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
809
        {
810
811
812
            msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );

            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
813
814
815
816
817
818
819
820
821
822
            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;
        }
    }

823
    return p_demux->b_error ? 0 : 1;
824
825
}

826
827
828
829
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
Laurent Aimar's avatar
Laurent Aimar committed
830
{
831
    demux_sys_t *p_sys = p_demux->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
832
    int64_t *pi64;
Laurent Aimar's avatar
Laurent Aimar committed
833
    double  *pf, f;
834

Laurent Aimar's avatar
Laurent Aimar committed
835
836
837
838
    switch( i_query )
    {
        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
Laurent Aimar's avatar
Laurent Aimar committed
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
            *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 )
            {
                *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start)/
                      (double)(p_sys->i_length);
            }
            else
            {
                *pf = 0;
            }
Laurent Aimar's avatar
Laurent Aimar committed
858
859
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
        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 )
            {
                MediaSubsessionIterator *iter = new MediaSubsessionIterator( *p_sys->ms );
                MediaSubsession         *sub;
                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;
                for( i = 0; i < p_sys->i_track; i++ )
                {
                    p_sys->track[i]->i_pts = 0;
                }
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;
        }

Laurent Aimar's avatar
Laurent Aimar committed
890
        default:
891
            return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
892
893
    }
}
894
895
896
897
898
899
900

/*****************************************************************************
 *
 *****************************************************************************/
static void StreamRead( void *p_private, unsigned int i_size, struct timeval pts )
{
    live_track_t   *tk = (live_track_t*)p_private;
901
902
    demux_t        *p_demux = tk->p_demux;
    demux_sys_t    *p_sys = p_demux->p_sys;
gbazin's avatar
   
gbazin committed
903
    block_t        *p_block;
904

905
    mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) + (uint64_t)pts.tv_usec;
906
907

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

Laurent Aimar's avatar
Laurent Aimar committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
    if( tk->b_quicktime && tk->p_es == NULL )
    {
        QuickTimeGenericRTPSource *qtRTPSource = (QuickTimeGenericRTPSource*)tk->rtpSource;
        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;
        }
        tk->fmt.i_codec = VLC_FOURCC( sdAtom[0], sdAtom[1], sdAtom[2], sdAtom[3] );
        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 );

931
        tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
Laurent Aimar's avatar
Laurent Aimar committed
932
933
    }

934
935
936
937
938
#if 0
    fprintf( stderr, "StreamRead size=%d pts=%lld\n",
             i_size,
             pts.tv_sec * 1000000LL + pts.tv_usec );
#endif
Laurent Aimar's avatar
Laurent Aimar committed
939
940
    if( i_size > 65536 )
    {
941
        msg_Warn( p_demux, "buffer overflow" );
Laurent Aimar's avatar
Laurent Aimar committed
942
    }
943
    /* FIXME could i_size be > buffer size ? */
944
    p_block = block_New( p_demux, i_size );
945

gbazin's avatar
   
gbazin committed
946
947
    memcpy( p_block->p_buffer, tk->buffer, i_size );
    //p_block->i_rate = p_input->stream.control.i_rate;
948

949
    if( i_pts != tk->i_pts && !tk->b_muxed )
950
    {
951
952
        p_block->i_dts = i_pts;
        p_block->i_pts = i_pts;
953
    }
954
955
956
957
958
959
    //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );

    if( tk->b_muxed )
    {
        stream_DemuxSend( tk->p_out_muxed, p_block );
    }
960
961
    else
    {
962
        es_out_Send( p_demux->out, tk->p_es, p_block );
963
    }
964
965
966
967
968
969

    /* warm that's ok */
    p_sys->event = 0xff;

    /* we have read data */
    tk->waiting = 0;
970

971
    if( i_pts > 0 && !tk->b_muxed )
Laurent Aimar's avatar
Laurent Aimar committed
972
973
974
    {
        tk->i_pts = i_pts;
    }
975
976
977
978
979
980
981
982
}

/*****************************************************************************
 *
 *****************************************************************************/
static void StreamClose( void *p_private )
{
    live_track_t   *tk = (live_track_t*)p_private;
983
984
    demux_t        *p_demux = tk->p_demux;
    demux_sys_t    *p_sys = p_demux->p_sys;
985
986
987
988

    fprintf( stderr, "StreamClose\n" );

    p_sys->event = 0xff;
989
    p_demux->b_error = VLC_TRUE;
990
991
992
993
994
995
996
997
}


/*****************************************************************************
 *
 *****************************************************************************/
static void TaskInterrupt( void *p_private )
{
998
    demux_t *p_demux = (demux_t*)p_private;
999
1000

    fprintf( stderr, "TaskInterrupt\n" );
For faster browsing, not all history is shown. View entire blame