live555.cpp 55.5 KB
Newer Older
1
/*****************************************************************************
2
 * live555.cpp : LIVE555 Streaming Media support.
3
 *****************************************************************************
4
 * Copyright (C) 2003-2006 the VideoLAN team
Carlo Calabrò's avatar
Carlo Calabrò committed
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#include <vlc/vlc.h>
29
30
31
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

32
#include <vlc_codecs.h>
zorglub's avatar
zorglub committed
33
34
35
#include <vlc_demux.h>
#include <vlc_interface.h>
#include <vlc_network.h>
36
37
38

#include <iostream>

gbazin's avatar
   
gbazin committed
39
40
41
42
#if defined( WIN32 )
#   include <winsock2.h>
#endif

43
44
45
46
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"
#include "liveMedia.hh"

47
48
49
50
extern "C" {
#include "../access/mms/asf.h"  /* Who said ugly ? */
}

hartman's avatar
hartman committed
51
52
53
54
55
56
#if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
#define RECLAIM_ENV(env) delete (env)
#else
#define RECLAIM_ENV(env) (env)->reclaim()
#endif

57
58
59
60
61
using namespace std;

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
62
63
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
64

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

70
#define KASENNA_TEXT N_( "Kasenna RTSP dialect")
71
#define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
72
    "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
73
    "for communication. In this mode you cannot connect to normal RTSP servers." )
74

75
76
77
78
79
80
81
#define USER_TEXT N_("RTSP user name")
#define USER_LONGTEXT N_("Allows you to modify the user name that will " \
    "be used for authenticating the connection.")
#define PASS_TEXT N_("RTSP password")
#define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
    "used for the connection.")

82
vlc_module_begin();
83
    set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) );
84
    set_capability( "demux2", 50 );
zorglub's avatar
zorglub committed
85
    set_shortname( "RTP/RTSP");
Laurent Aimar's avatar
Laurent Aimar committed
86
    set_callbacks( Open, Close );
87
    add_shortcut( "live" );
88
    add_shortcut( "livedotcom" );
zorglub's avatar
zorglub committed
89
90
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
91
92

    add_submodule();
Laurent Aimar's avatar
Laurent Aimar committed
93
        set_description( _("RTSP/RTP access and demux") );
94
        add_shortcut( "rtsp" );
sigmunau's avatar
sigmunau committed
95
        add_shortcut( "sdp" );
Laurent Aimar's avatar
Laurent Aimar committed
96
97
        set_capability( "access_demux", 0 );
        set_callbacks( Open, Close );
hartman's avatar
hartman committed
98
        add_bool( "rtsp-tcp", 0, NULL,
Carlo Calabrò's avatar
Carlo Calabrò committed
99
100
                  N_("Use RTP over RTSP (TCP)"),
                  N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
101
102
103
        add_integer( "rtp-client-port", -1, NULL,
                  N_("Client port"),
                  N_("Port to use for the RTP source of the session"), VLC_TRUE );
104
105
106
107
108
109
#if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
        add_bool( "rtsp-http", 0, NULL,
                  N_("Tunnel RTSP and RTP over HTTP"),
                  N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE );
        add_integer( "rtsp-http-port", 80, NULL,
                  N_("HTTP tunnel port"),
110
111
                  N_("Port to use for tunneling the RTSP/RTP over HTTP."),
                  VLC_TRUE );
112
#endif
hartman's avatar
hartman committed
113
114
115
116
        add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
                    CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
        add_bool(   "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
                    KASENNA_LONGTEXT, VLC_TRUE );
117
        add_string( "rtsp-user", NULL, NULL, USER_TEXT,
hartman's avatar
hartman committed
118
                    USER_LONGTEXT, VLC_TRUE );
119
        add_string( "rtsp-pwd", NULL, NULL, PASS_TEXT,
hartman's avatar
hartman committed
120
                    PASS_LONGTEXT, VLC_TRUE );
121
122
123
124
vlc_module_end();


/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
125
 * Local prototypes
126
 *****************************************************************************/
127

128
129
130
131
132
133
typedef struct
{
    demux_t     *p_demux;

    vlc_bool_t   b_quicktime;
    vlc_bool_t   b_muxed;
134
    vlc_bool_t   b_asf;
135
136
137
138
139
140
141
142
143
144

    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;

145
146
    uint8_t      *p_buffer;
    unsigned int  i_buffer;
147
148
149

    char         waiting;

hartman's avatar
hartman committed
150
    int64_t      i_pts;
151

152
153
} live_track_t;

154
155
156
157
158
159
160
161
struct timeout_thread_t
{
    VLC_COMMON_MEMBERS

    int64_t      i_remain;
    demux_sys_t  *p_sys;
};

162
163
struct demux_sys_t
{
hartman's avatar
hartman committed
164
165
    char            *p_sdp;    /* XXX mallocated */
    char            *psz_path; /* URL-encoded path */
166
167
168
169
170
171

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

172
    /* */
173
174
    int              i_track;
    live_track_t     **track;   /* XXX mallocated */
hartman's avatar
hartman committed
175
176
177
178
    int64_t          i_pcr;
    int64_t          i_pcr_start;
    int64_t          i_pcr_previous;
    int64_t          i_pcr_repeatdate;
179
    int              i_pcr_repeats;
180

181
182
183
184
185
    /* Asf */
    asf_header_t     asfh;
    stream_t         *p_out_asf;

    /* */
hartman's avatar
hartman committed
186
187
    int64_t          i_length;
    int64_t          i_start;
188

189
190
191
192
193
    /* timeout thread information */
    int              i_timeout;     /* session timeout value in seconds */
    vlc_bool_t       b_timeout_call;/* mark to send an RTSP call to prevent server timeout */
    timeout_thread_t *p_timeout;    /* the actual thread that makes sure we don't timeout */

194
    /* */
195
    vlc_bool_t       b_multicast;   /* true if one of the tracks is multicasted */
196
197
198
    vlc_bool_t       b_no_data;     /* true if we never receive any data */
    int              i_no_data_ti;  /* consecutive number of TaskInterrupt */

199
200
201
202
203
204
    char             event;
};

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

hartman's avatar
hartman committed
205
206
207
208
209
static int Connect      ( demux_t * );
static int SessionsSetup( demux_t * );
static int Play         ( demux_t *);
static int ParseASF     ( demux_t * );
static int RollOverTcp  ( demux_t * );
210

hartman's avatar
hartman committed
211
212
213
static void StreamRead  ( void *, unsigned int, unsigned int,
                          struct timeval, unsigned int );
static void StreamClose ( void * );
214
static void TaskInterrupt( void * );
215

216
217
static void TimeoutPrevention( timeout_thread_t * );

218
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
219
static unsigned char* parseH264ConfigStr( char const* configStr,
220
221
                                          unsigned int& configSize );
#endif
222

223
224
225
/*****************************************************************************
 * DemuxOpen:
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
226
static int  Open ( vlc_object_t *p_this )
227
{
228
    demux_t     *p_demux = (demux_t*)p_this;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
229
    demux_sys_t *p_sys = NULL;
hartman's avatar
hartman committed
230
231
232
233
    
    MediaSubsessionIterator *iter   = NULL;
    MediaSubsession         *sub    = NULL;
    int i_return;
234

Laurent Aimar's avatar
Laurent Aimar committed
235
    if( p_demux->s )
236
    {
Laurent Aimar's avatar
Laurent Aimar committed
237
238
        /* See if it looks like a SDP
           v, o, s fields are mandatory and in this order */
hartman's avatar
hartman committed
239
        uint8_t *p_peek;
240
241
        if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;

242
243
        if( memcmp( (char*)p_peek, "v=0\r\n", 5 ) &&
            memcmp( (char*)p_peek, "v=0\n", 4 ) &&
Laurent Aimar's avatar
Laurent Aimar committed
244
245
246
247
            ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
        {
            return VLC_EGENERIC;
        }
248
    }
Laurent Aimar's avatar
Laurent Aimar committed
249
    else
250
    {
Laurent Aimar's avatar
Laurent Aimar committed
251
        var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
252
253
    }

254
255
256
    p_demux->pf_demux  = Demux;
    p_demux->pf_control= Control;
    p_demux->p_sys     = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
257
258
    if( !p_sys ) return VLC_ENOMEM;

259
260
261
262
263
264
265
    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
266
267
    p_sys->i_pcr   = 0;
    p_sys->i_pcr_start = 0;
268
    p_sys->i_pcr_previous = 0;
269
    p_sys->i_pcr_repeatdate = 0;
270
    p_sys->i_pcr_repeats = 0;
Laurent Aimar's avatar
Laurent Aimar committed
271
272
    p_sys->i_length = 0;
    p_sys->i_start = 0;
273
    p_sys->p_out_asf = NULL;
274
275
    p_sys->b_no_data = VLC_TRUE;
    p_sys->i_no_data_ti = 0;
276
277
278
    p_sys->p_timeout = NULL;
    p_sys->i_timeout = 0;
    p_sys->b_timeout_call = VLC_FALSE;
279
    p_sys->b_multicast = VLC_FALSE;
hartman's avatar
hartman committed
280
    p_sys->psz_path = strdup( p_demux->psz_path );
281
282
283

    if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
    {
284
        msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
285
286
        goto error;
    }
287
    if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
288
    {
289
        msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
290
291
        goto error;
    }
Laurent Aimar's avatar
Laurent Aimar committed
292

293
    if( strcasecmp( p_demux->psz_access, "sdp" ) )
294
    {
295
        char *p = p_sys->psz_path;
296
        while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
297
298
    }

hartman's avatar
hartman committed
299
    if( p_demux->s != NULL )
Laurent Aimar's avatar
Laurent Aimar committed
300
301
    {
        /* Gather the complete sdp file */
hartman's avatar
hartman committed
302
303
304
305
        int     i_sdp       = 0;
        int     i_sdp_max   = 1000;
        uint8_t *p_sdp      = (uint8_t*) malloc( i_sdp_max );
        
Laurent Aimar's avatar
Laurent Aimar committed
306
307
        for( ;; )
        {
308
309
            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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329

            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;
330
    }
hartman's avatar
hartman committed
331
332
333
334
335
336
    else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
    {
        /* sdp:// link from SAP */
        p_sys->p_sdp = strdup( p_sys->psz_path );
    }
    else if( ( i_return = Connect( p_demux ) ) != VLC_SUCCESS )
337
    {
hartman's avatar
hartman committed
338
        msg_Err( p_demux, "Failed to connect with rtsp://%s", p_sys->psz_path );
339
340
        goto error;
    }
hartman's avatar
hartman committed
341
342
    
    if( p_sys->p_sdp == NULL )
343
    {
hartman's avatar
hartman committed
344
345
        msg_Err( p_demux, "Failed to retrieve the RTSP Session Description" );
        goto error;
346
347
    }

hartman's avatar
hartman committed
348
349
    /* Create the session from the SDP */
    if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
350
    {
hartman's avatar
hartman committed
351
352
353
        msg_Err( p_demux, "Could not create the RTSP Session: %s",
            p_sys->env->getResultMsg() );
        goto error;
354
    }
hartman's avatar
hartman committed
355
356
357
358
359
360
    
    if( ( i_return = SessionsSetup( p_demux ) ) != VLC_SUCCESS )
    {
        msg_Err( p_demux, "Nothing to play for rtsp://%s", p_sys->psz_path );
        goto error;
    }
361
362
363
364
365
366

    /* Retrieve the duration if possible */
    p_sys->i_length = (int64_t)( p_sys->ms->playEndTime() * 1000000.0 );
    if( p_sys->i_length < 0 )
        p_sys->i_length = -1;

hartman's avatar
hartman committed
367
368
369
    if( ( i_return = Play( p_demux ) ) != VLC_SUCCESS )
        goto error;
    
370
    /* Create all es struct */
hartman's avatar
hartman committed
371
    iter = new MediaSubsessionIterator( *p_sys->ms );
372
373
374
375
    while( ( sub = iter->next() ) != NULL )
    {
        live_track_t *tk;

hartman's avatar
hartman committed
376
        /* Check if we will receive data from this subsession for this track */
377
        if( sub->readSource() == NULL ) continue;
378
379

        tk = (live_track_t*)malloc( sizeof( live_track_t ) );
380
        tk->p_demux = p_demux;
381
        tk->waiting = 0;
382
        tk->i_pts   = 0;
Laurent Aimar's avatar
Laurent Aimar committed
383
        tk->b_quicktime = VLC_FALSE;
384
        tk->b_muxed     = VLC_FALSE;
385
        tk->b_asf       = VLC_FALSE;
386
        tk->b_rtcp_sync = VLC_FALSE;
387
388
        tk->p_out_muxed = NULL;
        tk->p_es        = NULL;
389
390
        tk->i_buffer    = 65536;
        tk->p_buffer    = (uint8_t *)malloc( 65536 );
391
392
393
394

        /* Value taken from mplayer */
        if( !strcmp( sub->mediumName(), "audio" ) )
        {
395
            es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
396
            tk->fmt.audio.i_channels = sub->numChannels();
397
            tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
398
399
400
401
402
403

            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
404
                tk->fmt.audio.i_rate = 0;
405
406
407
408
            }
            else if( !strcmp( sub->codecName(), "AC3" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
gbazin's avatar
   
gbazin committed
409
                tk->fmt.audio.i_rate = 0;
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
            }
            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' );
            }
429
430
431
            else if( !strncmp( sub->codecName(), "G726", 4 ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'g', '7', '2', '6' ); 
432
433
                tk->fmt.audio.i_rate = 8000;
                tk->fmt.audio.i_channels = 1;
434
                if( !strcmp( sub->codecName()+5, "40" ) )
435
                    tk->fmt.i_bitrate = 40000;
436
                else if( !strcmp( sub->codecName()+5, "32" ) )
437
                    tk->fmt.i_bitrate = 32000;
438
                else if( !strcmp( sub->codecName()+5, "24" ) )
439
                    tk->fmt.i_bitrate = 24000;
440
                else if( !strcmp( sub->codecName()+5, "16" ) )
441
                    tk->fmt.i_bitrate = 16000;
442
            }
443
444
445
446
447
448
449
450
            else if( !strcmp( sub->codecName(), "AMR" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
            }
            else if( !strcmp( sub->codecName(), "AMR-WB" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
            }
451
452
453
454
455
456
457
            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' );

458
459
                if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
                                                         i_extra ) ) )
460
461
                {
                    tk->fmt.i_extra = i_extra;
462
                    tk->fmt.p_extra = malloc( i_extra );
463
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
464
                    delete[] p_extra;
465
                }
466
467

#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1141257600
468
469
470
                /* Because the "faad" decoder does not handle the LATM data length field
                   at the start of each returned LATM frame, tell the RTP source to omit it. */
                ((MPEG4LATMAudioRTPSource*)sub->rtpSource())->omitLATMDataLengthField();
471
#endif
472
473
474
475
476
477
478
479
            }
            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' );

480
481
                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
                                                       i_extra ) ) )
482
483
484
485
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
486
                    delete[] p_extra;
487
488
                }
            }
489
490
491
492
            else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            {
                tk->b_asf = VLC_TRUE;
                if( p_sys->p_out_asf == NULL )
493
494
                    p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
                                                        p_demux->out );
495
            }
496
497
498
499
500
            else if( !strcmp( sub->codecName(), "X-QT" ) ||
                     !strcmp( sub->codecName(), "X-QUICKTIME" ) )
            {
                tk->b_quicktime = VLC_TRUE;
            }
501
502
503
        }
        else if( !strcmp( sub->mediumName(), "video" ) )
        {
504
            es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
505
506
507
508
509
            if( !strcmp( sub->codecName(), "MPV" ) )
            {
                tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
            }
            else if( !strcmp( sub->codecName(), "H263" ) ||
510
511
                     !strcmp( sub->codecName(), "H263-1998" ) ||
                     !strcmp( sub->codecName(), "H263-2000" ) )
512
            {
hartman's avatar
hartman committed
513
                tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
514
515
516
            }
            else if( !strcmp( sub->codecName(), "H261" ) )
            {
hartman's avatar
hartman committed
517
                tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
518
            }
519
520
            else if( !strcmp( sub->codecName(), "H264" ) )
            {
521
522
523
524
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
                unsigned int i_extra = 0;
                uint8_t      *p_extra = NULL;
#endif
525
                tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
                tk->fmt.b_packetized = VLC_FALSE;

                /* XXX not the right minimal version I fear */
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
                if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
                                                i_extra ) ) )
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );

                    delete[] p_extra;
                }
#endif
            }
541
542
            else if( !strcmp( sub->codecName(), "JPEG" ) )
            {
Laurent Aimar's avatar
Laurent Aimar committed
543
                tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
544
545
546
547
548
549
550
551
            }
            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' );

552
553
                if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
                                                       i_extra ) ) )
554
555
556
557
                {
                    tk->fmt.i_extra = i_extra;
                    tk->fmt.p_extra = malloc( i_extra );
                    memcpy( tk->fmt.p_extra, p_extra, i_extra );
558
                    delete[] p_extra;
559
560
                }
            }
561
562
563
564
            else if( !strcmp( sub->codecName(), "X-QT" ) ||
                     !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
                     !strcmp( sub->codecName(), "X-QDM" ) ||
                     !strcmp( sub->codecName(), "X-SV3V-ES" )  ||
565
                     !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
Laurent Aimar's avatar
Laurent Aimar committed
566
567
568
            {
                tk->b_quicktime = VLC_TRUE;
            }
569
570
571
            else if( !strcmp( sub->codecName(), "MP2T" ) )
            {
                tk->b_muxed = VLC_TRUE;
572
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
573
            }
574
575
            else if( !strcmp( sub->codecName(), "MP2P" ) ||
                     !strcmp( sub->codecName(), "MP1S" ) )
576
577
            {
                tk->b_muxed = VLC_TRUE;
578
579
                tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
                                                   p_demux->out );
580
            }
581
582
583
584
            else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            {
                tk->b_asf = VLC_TRUE;
                if( p_sys->p_out_asf == NULL )
585
586
                    p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
                                                        p_demux->out );;
587
            }
588
589
        }

hartman's avatar
hartman committed
590
591
592
593
        if( !tk->b_quicktime && !tk->b_muxed && !tk->b_asf )
        {
            tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
        }
594

595
596
597
598
599
        if( sub->rtcpInstance() != NULL )
        {
            sub->rtcpInstance()->setByeHandler( StreamClose, tk );
        }

600
        if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
601
602
        {
            tk->readSource = sub->readSource();
Laurent Aimar's avatar
Laurent Aimar committed
603
            tk->rtpSource  = sub->rtpSource();
hartman's avatar
hartman committed
604
            
605
606
607
            /* 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;
608
609
610
        }
        else
        {
hartman's avatar
hartman committed
611
612
            /* BUG ??? */
            msg_Err( p_demux, "unusable RTSP track. this should not happen" );
613
614
615
616
617
618
            free( tk );
        }
    }

    delete iter;

619
620
621
622
623
624
625
    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
626
627
628
    if( p_sys->i_track <= 0 )
        goto error;

629
630
631
    return VLC_SUCCESS;

error:
632
633
    if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
    if( p_sys->ms ) Medium::close( p_sys->ms );
hartman's avatar
hartman committed
634
    if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
635
    if( p_sys->env ) RECLAIM_ENV(p_sys->env);
636
637
638
639
640
641
642
    if( p_sys->p_timeout )
    {
        p_sys->p_timeout->b_die = VLC_TRUE;
        vlc_thread_join( p_sys->p_timeout );
        vlc_object_detach( p_sys->p_timeout );
        vlc_object_destroy( p_sys->p_timeout );
    }
643
644
645
    if( p_sys->scheduler ) delete p_sys->scheduler;
    if( p_sys->p_sdp ) free( p_sys->p_sdp );
    if( p_sys->psz_path ) free( p_sys->psz_path );
646

647
648
649
650
651
652
653
    free( p_sys );
    return VLC_EGENERIC;
}

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

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

664
        if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
665
        free( tk->p_buffer );
666
667
        free( tk );
    }
668
669
670

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

672
    if( p_sys->rtsp && p_sys->ms )
673
674
    {
        /* TEARDOWN */
675
        p_sys->rtsp->teardownMediaSession( *p_sys->ms );
676
    }
677

678
679
    Medium::close( p_sys->ms );

680
681
682
683
684
685
686
687
    if( p_sys->p_timeout )
    {
        p_sys->p_timeout->b_die = VLC_TRUE;
        vlc_thread_join( p_sys->p_timeout );
        vlc_object_detach( p_sys->p_timeout );
        vlc_object_destroy( p_sys->p_timeout );
    }

hartman's avatar
hartman committed
688
689
    if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
    if( p_sys->env ) RECLAIM_ENV( p_sys->env );
690
691
    if( p_sys->scheduler ) delete p_sys->scheduler;
    if( p_sys->p_sdp ) free( p_sys->p_sdp );
692
    if( p_sys->psz_path ) free( p_sys->psz_path );
693
694
695
    free( p_sys );
}

hartman's avatar
hartman committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
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
/*****************************************************************************
 * Connect: connects to the RTSP server to setup the session DESCRIBE
 *****************************************************************************/
static int Connect( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    Authenticator authenticator;

    char *psz_user    = NULL;
    char *psz_pwd     = NULL;
    char *psz_url     = NULL;
    char *psz_options = NULL;
    char *p_sdp       = NULL;
    int  i_http_port  = 0;
    int  i_ret        = VLC_SUCCESS;

createnew:
    if( var_CreateGetBool( p_demux, "rtsp-http" ) )
        i_http_port = var_CreateGetInteger( p_demux, "rtsp-http-port" );

#if LIVEMEDIA_LIBRARY_VERSION_INT > 1130457500
    if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
          "VLC media player", i_http_port ) ) == NULL )
#else
    if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1 /*verbose*/,
          "VLC media player" ) ) == NULL )
#endif
    {
        msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
                 p_sys->env->getResultMsg() );
        return VLC_EGENERIC;
    }
    psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
    sprintf( psz_url, "rtsp://%s", p_sys->psz_path );

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

    psz_user = var_CreateGetString( p_demux, "rtsp-user" );
    psz_pwd  = var_CreateGetString( p_demux, "rtsp-pwd" );

describe:
    authenticator.setUsernameAndPassword( (const char*)psz_user, (const char*)psz_pwd );
    p_sdp = p_sys->rtsp->describeURL( psz_url,
                &authenticator, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );

    if( psz_user ) free( psz_user );
    if( psz_pwd ) free( psz_pwd );                
    
    if( p_sdp == NULL )
    {
747
        /* failure occurred */
hartman's avatar
hartman committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
        int i_code = 0;
        const char *psz_error = p_sys->env->getResultMsg();
                
        msg_Dbg( p_demux, "DESCRIBE failed with %d: %s", i_code, psz_error );
        sscanf( psz_error, "%*sRTSP/%*s%3u", &i_code );
        
        if( i_code == 401 )
        {
            char *psz_login = NULL; char *psz_password = NULL;
            msg_Dbg( p_demux, "authentication failed" );

            i_ret = intf_UserLoginPassword( p_demux, _("RTSP authentication"),
                           _("Please enter a valid login name and a password."),
                                                   &psz_login, &psz_password );
            if( i_ret == DIALOG_OK_YES )
            {
               msg_Dbg( p_demux, "retrying with user=%s, pwd=%s",
                           psz_login, psz_password );
               
               if( psz_login ) psz_user = psz_login;
               if( psz_password ) psz_pwd = psz_password;
               goto describe;
            }
            if( psz_login ) free( psz_login );
            if( psz_password ) free( psz_password );
        }
        else if( !var_CreateGetBool( p_demux, "rtsp-http" ) )
        {
            /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
            vlc_value_t val;
            val.b_bool = VLC_TRUE;
            msg_Dbg( p_demux, "we will now try HTTP tunneling mode" );
            var_Set( p_demux, "rtsp-http", val );
            if( psz_url ) free( psz_url );
            if( p_sys->rtsp ) RTSPClient::close( p_sys->rtsp );
            goto createnew;
        }
        i_ret = VLC_EGENERIC;
    }
    if( psz_url ) free( psz_url );

    /* malloc-ated copy */
    if( p_sys->p_sdp ) free( p_sys->p_sdp );
hartman's avatar
hartman committed
791
    if( p_sdp ) p_sys->p_sdp = strdup( (char*)p_sdp );
hartman's avatar
hartman committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
    delete[] p_sdp;

    return i_ret;
}

/*****************************************************************************
 * Connect: prepares the subsessions and does the SETUP
 *****************************************************************************/
static int SessionsSetup( demux_t *p_demux )
{
    demux_sys_t             *p_sys  = p_demux->p_sys;
    MediaSubsessionIterator *iter   = NULL;
    MediaSubsession         *sub    = NULL;

    vlc_bool_t     b_rtsp_tcp = VLC_FALSE;
    int            i_client_port;
    int            i_active_sessions = 0;
    int            i_return = VLC_SUCCESS;
    unsigned int   i_buffer = 0;
    unsigned const thresh = 200000; /* RTP reorder threshold .2 second (default .1) */

    b_rtsp_tcp    = var_CreateGetBool( p_demux, "rtsp-tcp" );
    i_client_port = var_CreateGetInteger( p_demux, "rtp-client-port" );

    /* Initialise each media subsession */
    iter = new MediaSubsessionIterator( *p_sys->ms );
    while( ( sub = iter->next() ) != NULL )
    {
        Boolean bInit;

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

        if( i_client_port != -1 )
        {
            sub->setClientPortNum( i_client_port );
            i_client_port += 2;
        }

        if( strcasestr( sub->codecName(), "REAL" ) )
        {
            msg_Info( p_demux, "real codec detected, using real-RTSP instead" );
            i_return = VLC_EGENERIC;
            break;
        }

        if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
            bInit = sub->initiate( 4 ); /* Constant ? */
        else
            bInit = sub->initiate();
        
        if( !bInit )
        {
            msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
                      sub->mediumName(), sub->codecName(),
                      p_sys->env->getResultMsg() );
        }
        else
        {
            if( sub->rtpSource() != NULL )
            {
                int fd = sub->rtpSource()->RTPgs()->socketNum();
          
                /* Increase the buffer size */
                if( i_buffer > 0 )
                    increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
                
                /* Increase the RTP reorder timebuffer just a bit */
                sub->rtpSource()->setPacketReorderingThresholdTime(thresh);
            }

            msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
                     sub->codecName() );

            /* Issue the SETUP */
            if( p_sys->rtsp )
            {
                if( !( p_sys->rtsp->setupMediaSubsession( *sub, False,
                                                   b_rtsp_tcp ? True : False ) ) )
                {
                    msg_Err( p_demux, "SETUP of'%s/%s' failed %s", sub->mediumName(),
                             sub->codecName(), p_sys->env->getResultMsg() );
                }
                else i_active_sessions++;
            } else i_active_sessions++; /* we don't really know, let's just hope it's there */

            if( !p_sys->b_multicast )
            {
                /* Check, because we need diff. rollover behaviour for multicast */
                p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
            }
        }
    }
    delete iter;
    if( i_active_sessions <= 0 ) i_return = VLC_EGENERIC;
    return i_return;
}

/*****************************************************************************
 * Play: starts the actual playback of the stream
 *****************************************************************************/
static int Play( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    
    if( p_sys->rtsp )
    {
        /* The PLAY */
        if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
        {
            msg_Err( p_demux, "RTSP PLAY failed %s", p_sys->env->getResultMsg() );
            return VLC_EGENERIC;
        }

        /* Retrieve the timeout value and set up a timeout prevention thread */
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
        p_sys->i_timeout = p_sys->rtsp->sessionTimeoutParameter();
#endif
        if( p_sys->i_timeout > 0 && !p_sys->p_timeout )
        {
            msg_Dbg( p_demux, "We have a timeout of %d seconds",  p_sys->i_timeout );
            p_sys->p_timeout = (timeout_thread_t *)vlc_object_create( p_demux, sizeof(timeout_thread_t) );
            p_sys->p_timeout->p_sys = p_demux->p_sys; /* lol, object recursion :D */
            if( vlc_thread_create( p_sys->p_timeout, "liveMedia-timeout", TimeoutPrevention,
                                   VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
            {
                msg_Err( p_demux, "cannot spawn liveMedia timeout thread" );
                vlc_object_destroy( p_sys->p_timeout );
            }
            msg_Dbg( p_demux, "spawned timeout thread" );
            vlc_object_attach( p_sys->p_timeout, p_demux );
        }
    }
    return VLC_SUCCESS;
}


933
934
935
/*****************************************************************************
 * Demux:
 *****************************************************************************/
936
static int Demux( demux_t *p_demux )
937
{
938
    demux_sys_t    *p_sys = p_demux->p_sys;
939
940
    TaskToken      task;

941
    vlc_bool_t      b_send_pcr = VLC_TRUE;
hartman's avatar
hartman committed
942
    int64_t         i_pcr = 0;
943
944
    int             i;

945
946
947
    /* Check if we need to send the server a Keep-A-Live signal */
    if( p_sys->b_timeout_call && p_sys->rtsp && p_sys->ms )
    {
hartman's avatar
hartman committed
948
#if LIVEMEDIA_LIBRARY_VERSION_INT >= 1138089600
949
        char *psz_bye = NULL;
950
        p_sys->rtsp->getMediaSessionParameter( *p_sys->ms, NULL, psz_bye );
hartman's avatar
hartman committed
951
#endif
952
953
954
        p_sys->b_timeout_call = VLC_FALSE;
    }

Laurent Aimar's avatar
Laurent Aimar committed
955
956
957
958
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

959
960
961
        if( tk->b_asf || tk->b_muxed )
            b_send_pcr = VLC_FALSE;

Laurent Aimar's avatar
Laurent Aimar committed
962
963
964
965
966
967
968
969
970
        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
971
    if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
972
973
    {
        p_sys->i_pcr = i_pcr;
974

975
976
        if( b_send_pcr )
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
Laurent Aimar's avatar
Laurent Aimar committed
977
978
        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
979
980
981
982
983
        {
            p_sys->i_pcr_start = i_pcr;
        }
    }

hartman's avatar
hartman committed
984
985
986
#if 0
    /* Disabled because it's simply not reliable enough */

987
988
    /* When a On Demand QT stream ends, the last frame keeps going with the same PCR/PTS value */
    /* This tests for that, so we can later decide to end this session */
989
    if( i_pcr > 0 && p_sys->i_pcr == p_sys->i_pcr_previous )
990
    {
991
992
        if( p_sys->i_pcr_repeats == 0 )
            p_sys->i_pcr_repeatdate = mdate();
993
994
995
996
997
        p_sys->i_pcr_repeats++;
    }
    else
    {
        p_sys->i_pcr_previous = p_sys->i_pcr;
998
        p_sys->i_pcr_repeatdate = 0;
999
1000
        p_sys->i_pcr_repeats = 0;
    }
1001
1002

    if( p_sys->i_pcr_repeats > 5 && mdate() > p_sys->i_pcr_repeatdate + 1000000 )
1003
    {
1004
1005
        /* We need at least 5 repeats over at least a second of time before we EOF */
        msg_Dbg( p_demux, "suspect EOF due to end of VoD session" );
1006
1007
        return 0;
    }
hartman's avatar
hartman committed
1008
#endif
1009

hartman's avatar
hartman committed
1010
    /* First warn we want to read data */
1011
1012
1013
1014
1015
1016
1017
1018
    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;
1019
            tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
1020
                                          StreamRead, tk, StreamClose, tk );
1021
1022
1023
        }
    }
    /* Create a task that will be called if we wait more than 300ms */
1024
    task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
1025
1026
1027
1028
1029
1030
1031

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

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

1032
1033
1034
1035
1036
    /* Check for gap in pts value */
    for( i = 0; i < p_sys->i_track; i++ )
    {
        live_track_t *tk = p_sys->track[i];

1037
        if( !tk->b_muxed && !tk->b_rtcp_sync &&
1038
            tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
1039
        {
1040
1041
1042
            msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );

            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
            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;
        }
    }

1053
1054
    if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
    {
1055
        /* FIXME Make this configurable
1056
1057
        msg_Err( p_demux, "no multicast data received in 36s, aborting" );
        return 0;
1058
        */
1059
    }
hartman's avatar
hartman committed
1060
    else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1061
1062
1063
1064
1065
    {
        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
1066
            msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
1067
1068
1069
1070
1071
1072
            if( RollOverTcp( p_demux ) )
            {
                msg_Err( p_demux, "TCP rollover failed, aborting" );
                return 0;
            }
        }
hartman's avatar
hartman committed
1073
        else if( p_sys->i_no_data_ti > 34 )
1074
        {
hartman's avatar
hartman committed
1075
            msg_Err( p_demux, "no data received in 10s, aborting" );
1076
1077
1078
            return 0;
        }
    }
hartman's avatar
hartman committed
1079
    else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
1080
1081
    {
        /* EOF ? */
hartman's avatar
hartman committed
1082
        msg_Warn( p_demux, "no data received in 10s, eof ?" );
hartman's avatar
hartman committed
1083
        return 0;
1084
1085
    }

1086
    return p_demux->b_error ? 0 : 1;
1087
1088
}

1089
1090
1091
1092
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
Laurent Aimar's avatar
Laurent Aimar committed
1093
{
1094
    demux_sys_t *p_sys = p_demux->p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
1095
    int64_t *pi64;
Laurent Aimar's avatar
Laurent Aimar committed
1096
    double  *pf, f;
1097
    vlc_bool_t *pb, b_bool;
1098

Laurent Aimar's avatar
Laurent Aimar committed
1099
1100
1101
1102
    switch( i_query )
    {
        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
Laurent Aimar's avatar
Laurent Aimar committed
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
            *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 )
            {
1115
1116
                *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
1117
1118
1119
            }
            else
            {
hartman's avatar
hartman committed
1120
                *pf = 0.0;
Laurent Aimar's avatar
Laurent Aimar committed
1121
            }
Laurent Aimar's avatar
Laurent Aimar committed
1122
1123
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
1124
1125
1126
1127
        case DEMUX_SET_POSITION:
            float time;

            f = (double)va_arg( args, double );
hartman's avatar
hartman committed
1128
            
Laurent Aimar's avatar
Laurent Aimar committed
1129
1130
            if( p_sys->rtsp && p_sys->i_length > 0 )
            {
hartman's avatar
hartman committed
1131
                time = f * (double)p_sys->i_length / 1000000.0;   /* in second */
1132
                if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
Laurent Aimar's avatar
Laurent Aimar committed
1133
                {
1134
1135
                    msg_Err( p_demux, "PLAY failed %s",
                        p_sys->env->getResultMsg() );
1136
                    return VLC_EGENERIC;
Laurent Aimar's avatar
Laurent Aimar committed
1137
                }
hartman's avatar
hartman committed
1138
                p_sys->i_start = (int64_t)(f * (double)p_sys->i_length);
Laurent Aimar's avatar
Laurent Aimar committed
1139
1140
                p_sys->i_pcr_start = 0;
                p_sys->i_pcr       = 0;
1141

Laurent Aimar's avatar
Laurent Aimar committed
1142
1143
                return VLC_SUCCESS;
            }
1144
            return VLC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
1145

Laurent Aimar's avatar
Laurent Aimar committed
1146
1147
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
1148
            pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1149
            if( p_sys->rtsp && p_sys->i_length )
1150
1151
                /* Not always true, but will be handled in SET_PAUSE_STATE */
                *pb = VLC_TRUE;
1152
1153
1154
1155
            else
                *pb = VLC_FALSE;
            return VLC_SUCCESS;

Laurent Aimar's avatar
Laurent Aimar committed
1156
1157
        case DEMUX_CAN_CONTROL_PACE:
            pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1158
1159
1160

#if 0       /* Disable for now until we have a clock synchro algo
             * which works with something else than MPEG over UDP */