udp.c 18.8 KB
Newer Older
1
/*****************************************************************************
gbazin's avatar
   
gbazin committed
2
 * udp.c: raw UDP & RTP input module
3
 *****************************************************************************
4
 * Copyright (C) 2001-2005 the VideoLAN team
hartman's avatar
hartman committed
5
 * $Id$
6
7
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8
 *          Tristan Leteurtre <tooney@via.ecp.fr>
Laurent Aimar's avatar
Laurent Aimar committed
9
 *          Laurent Aimar <fenrir@via.ecp.fr>
10
 *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
11
 *
Jean-Paul Saman's avatar
Jean-Paul Saman committed
12
13
 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
 *
14
15
16
17
 * 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.
Laurent Aimar's avatar
Laurent Aimar committed
18
 *
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 * 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>

#include <vlc/vlc.h>
#include <vlc/input.h>

#include "network.h"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
42
#define CACHING_TEXT N_("Caching value in ms")
gbazin's avatar
   
gbazin committed
43
#define CACHING_LONGTEXT N_( \
44
45
    "Allows you to modify the default caching value for UDP streams. This " \
    "value should be set in millisecond units." )
gbazin's avatar
   
gbazin committed
46

47
48
49
50
#define AUTO_MTU_TEXT N_("Autodetection of MTU")
#define AUTO_MTU_LONGTEXT N_( \
    "Allows growing the MTU if truncated packets are found" )

51
#define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
52
#define RTP_LATE_LONGTEXT N_( \
53
54
55
    "Allows you to modify the RTP reordering behaviour. " \
    "RTP input will wait for late packets upto " \
    "the specified timeout in milisecond units." )
56

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

60
vlc_module_begin();
zorglub's avatar
zorglub committed
61
    set_shortname( _("UDP/RTP" ) );
gbazin's avatar
   
gbazin committed
62
    set_description( _("UDP/RTP input") );
zorglub's avatar
zorglub committed
63
64
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
gbazin's avatar
   
gbazin committed
65
66
67

    add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
                 CACHING_LONGTEXT, VLC_TRUE );
68
69
    add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );

70
    add_bool( "udp-auto-mtu", 1, NULL,
71
              AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
gbazin's avatar
   
gbazin committed
72

73
    set_capability( "access2", 0 );
74
    add_shortcut( "udp" );
75
76
77
    add_shortcut( "udpstream" );
    add_shortcut( "udp4" );
    add_shortcut( "udp6" );
78
79
80
    add_shortcut( "rtp" );
    add_shortcut( "rtp4" );
    add_shortcut( "rtp6" );
81
    set_callbacks( Open, Close );
82
83
vlc_module_end();

Laurent Aimar's avatar
Laurent Aimar committed
84
85
86
87
88
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define RTP_HEADER_LEN 12

89
90
91
92
static block_t *BlockUDP( access_t * );
static block_t *BlockRTP( access_t * );
static block_t *BlockChoose( access_t * );
static int Control( access_t *, int, va_list );
Laurent Aimar's avatar
Laurent Aimar committed
93
94
95
96

struct access_sys_t
{
    int fd;
97
98
99

    int i_mtu;
    vlc_bool_t b_auto_mtu;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
100

101
    /* reorder rtp packets when out-of-sequence */
Marian Durkovic's avatar
Marian Durkovic committed
102
    mtime_t i_rtp_late;
103
104
105
    uint16_t i_last_seqno;
    block_t *p_list;
    block_t *p_end;
Laurent Aimar's avatar
Laurent Aimar committed
106
107
};

108
109
110
111
112
/*****************************************************************************
 * Open: open the socket
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
113
114
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
115

116
    char *psz_name = strdup( p_access->psz_path );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
117
118
    char *psz_parser, *psz_server_addr, *psz_bind_addr = "";
    int  i_bind_port, i_server_port = 0;
119

Laurent Aimar's avatar
Laurent Aimar committed
120
    /* First set ipv4/ipv6 */
121
122
    var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
123

124
    if( *p_access->psz_access )
125
    {
126
        vlc_value_t val;
127
        /* Find out which shortcut was used */
128
129
        if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
            !strncmp( p_access->psz_access, "rtp4", 6 ))
130
        {
Laurent Aimar's avatar
Laurent Aimar committed
131
            val.b_bool = VLC_TRUE;
132
            var_Set( p_access, "ipv4", val );
Laurent Aimar's avatar
Laurent Aimar committed
133
134

            val.b_bool = VLC_FALSE;
135
            var_Set( p_access, "ipv6", val );
136
        }
137
138
        else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
                 !strncmp( p_access->psz_access, "rtp6", 6 ) )
139
        {
Laurent Aimar's avatar
Laurent Aimar committed
140
            val.b_bool = VLC_TRUE;
141
            var_Set( p_access, "ipv6", val );
Laurent Aimar's avatar
Laurent Aimar committed
142
143

            val.b_bool = VLC_FALSE;
144
            var_Set( p_access, "ipv4", val );
145
146
147
        }
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
148
149
    i_bind_port = var_CreateGetInteger( p_access, "server-port" );

150
151
    /* Parse psz_name syntax :
     * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
152
153
    psz_parser = strchr( psz_name, '@' );
    if( psz_parser != NULL )
154
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
155
156
157
        /* Found bind address and/or bind port */
        *psz_parser++ = '\0';
        psz_bind_addr = psz_parser;
158

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
159
160
161
        if( *psz_parser == '[' )
            /* skips bracket'd IPv6 address */
            psz_parser = strchr( psz_parser, ']' );
162

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
163
        if( psz_parser != NULL )
164
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
165
166
            psz_parser = strchr( psz_parser, ':' );
            if( psz_parser != NULL )
167
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
168
169
                *psz_parser++ = '\0';
                i_bind_port = atoi( psz_parser );
170
171
172
173
            }
        }
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
174
175
176
177
    psz_server_addr = psz_name;
    if( *psz_server_addr == '[' )
        /* skips bracket'd IPv6 address */
        psz_parser = strchr( psz_name, ']' );
178

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
179
180
    if( psz_parser != NULL )
    {
181
        psz_parser = strchr( psz_parser, ':' );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182
        if( psz_parser != NULL )
183
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
184
185
            *psz_parser++ = '\0';
            i_server_port = atoi( psz_parser );
186
187
188
        }
    }

189
    msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
190
191
             psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );

192
193
    /* Set up p_access */
    p_access->pf_read = NULL;
194
195
196
197
198
199
200
201
202
203
    if( !strcasecmp( p_access->psz_access, "rtp" )
          || !strcasecmp( p_access->psz_access, "rtp4" )
          || !strcasecmp( p_access->psz_access, "rtp6" ) )
    {
        p_access->pf_block = BlockRTP;
    }
    else
    {
        p_access->pf_block = BlockChoose;
    }
204
205
206
207
208
209
    p_access->pf_control = Control;
    p_access->pf_seek = NULL;
    p_access->info.i_update = 0;
    p_access->info.i_size = 0;
    p_access->info.i_pos = 0;
    p_access->info.b_eof = VLC_FALSE;
210
    p_access->info.b_prebuffered = VLC_FALSE;
211
212
213
214
215
    p_access->info.i_title = 0;
    p_access->info.i_seekpoint = 0;

    p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
    p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
Laurent Aimar's avatar
Laurent Aimar committed
216
217
                                      psz_server_addr, i_server_port );
    if( p_sys->fd < 0 )
218
    {
219
        msg_Err( p_access, "cannot open socket" );
Laurent Aimar's avatar
Laurent Aimar committed
220
221
222
        free( psz_name );
        free( p_sys );
        return VLC_EGENERIC;
223
    }
Laurent Aimar's avatar
Laurent Aimar committed
224
    free( psz_name );
225

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
226
227
    net_StopSend( p_sys->fd );

Laurent Aimar's avatar
Laurent Aimar committed
228
    /* FIXME */
229
230
231
    p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
    if( p_sys->i_mtu <= 1 )
        p_sys->i_mtu  = 1500;   /* Avoid problem */
232

233
    p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
234

gbazin's avatar
   
gbazin committed
235
    /* Update default_pts to a suitable value for udp access */
236
    var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
gbazin's avatar
   
gbazin committed
237

238

239
240
241
    /* RTP reordering for out-of-sequence packets */
    p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
    p_sys->i_last_seqno = 0;
242
243
    p_sys->p_list = NULL;
    p_sys->p_end = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
244
    return VLC_SUCCESS;
245
}
246
247
248
249
250
251

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
252
253
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
254

255
    block_ChainRelease( p_sys->p_list );
Laurent Aimar's avatar
Laurent Aimar committed
256
257
    net_Close( p_sys->fd );
    free( p_sys );
258
259
260
}

/*****************************************************************************
261
 * Control:
262
 *****************************************************************************/
263
static int Control( access_t *p_access, int i_query, va_list args )
264
{
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    access_sys_t *p_sys = p_access->p_sys;
    vlc_bool_t   *pb_bool;
    int          *pi_int;
    int64_t      *pi_64;

    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = VLC_FALSE;
            break;
        /* */
        case ACCESS_GET_MTU:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = p_sys->i_mtu;
            break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
288
            *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
289
290
291
292
293
294
295
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
296
        case ACCESS_SET_PRIVATE_ID_STATE:
297
298
299
            return VLC_EGENERIC;

        default:
300
            msg_Warn( p_access, "unimplemented query in control" );
301
            return VLC_EGENERIC;
302

303
304
    }
    return VLC_SUCCESS;
305
306
}

307
/*****************************************************************************
308
 * BlockUDP:
309
 *****************************************************************************/
310
static block_t *BlockUDP( access_t *p_access )
311
{
312
313
    access_sys_t *p_sys = p_access->p_sys;
    block_t      *p_block;
314

315
316
    /* Read data */
    p_block = block_New( p_access, p_sys->i_mtu );
317
318
319
    p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
                                  p_block->p_buffer, p_sys->i_mtu,
                                  VLC_FALSE );
320
321
322
323
324
    if( p_block->i_buffer <= 0 )
    {
        block_Release( p_block );
        return NULL;
    }
325

Jean-Paul Saman's avatar
Jean-Paul Saman committed
326
    if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
327
        p_sys->i_mtu < 32767 )
328
    {
329
330
        /* Increase by 100% */
        p_sys->i_mtu *= 2;
331
332
        msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
    }
333

334
335
336
    return p_block;
}

337
338
339
340
/*
 * rtp_ChainInsert - insert a p_block in the chain and
 * look at the sequence numbers.
 */
341
static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
342
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
343
    access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
344
345
    block_t *p_prev = NULL;
    block_t *p = p_sys->p_end;
346
    uint16_t i_new = (uint16_t) p_block->i_dts;
347
    uint16_t i_tmp = 0;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
348

349
    if( !p_sys->p_list )
350
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
351
        p_sys->p_list = p_block;
352
353
        p_sys->p_end = p_block;
        return VLC_TRUE;
354
    }
355
356
357
358
    /* walk through the queue from top down since the new packet is in 
    most cases just appended to the end */

    for( ;; )
359
    {
360
        i_tmp = i_new - (uint16_t) p->i_dts;
361

362
363
        if( !i_tmp )   /* trash duplicate */
            break; 
Jean-Paul Saman's avatar
Jean-Paul Saman committed
364

365
        if ( i_tmp < 32768 )
366
        {   /* insert after this block ( i_new > p->i_dts ) */
367
            p_block->p_next = p->p_next;
368
            p->p_next = p_block;
369
370
            p_block->p_prev = p;
            if (p_prev)
371
            {
372
373
                p_prev->p_prev = p_block;
                msg_Dbg(p_access, "RTP reordering: insert after %d, new %d", 
374
                    (uint16_t) p->i_dts, i_new );
375
            }
376
            else 
377
            {
378
                p_sys->p_end = p_block;
379
            }
380
            return VLC_TRUE;
381
        }
382
383
384
385
386
387
        if( p == p_sys->p_list )
        {   /* we've reached bottom of chain */
            i_tmp = p_sys->i_last_seqno - i_new;
            if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
            {
                msg_Dbg(p_access, "RTP reordering: prepend %d before %d", 
388
                        i_new, (uint16_t) p->i_dts );
389
390
391
392
393
                p_block->p_next = p;
                p->p_prev = p_block;
                p_sys->p_list = p_block;
                return VLC_TRUE;
            }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
394

395
396
397
398
399
            if( !i_tmp )   /* trash duplicate */
                break;    

            /* reordering failed - append the packet to the end of queue */
            msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
400
401
                "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts, 
                (uint16_t) p_sys->p_end->i_dts);
402
403
404
405
406
407
408
            p_sys->p_end->p_next = p_block;
            p_block->p_prev = p_sys->p_end;
            p_sys->p_end = p_block;
            return VLC_TRUE;
        }
        p_prev = p;
        p = p->p_prev;
409
    }
410
411
    block_Release( p_block );
    return VLC_FALSE;
412
413
}

414
415
416
417
418
/*****************************************************************************
 * BlockParseRTP/BlockRTP:
 *****************************************************************************/
static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
{
419
420
421
422
    int      i_rtp_version;
    int      i_CSRC_count;
    int      i_payload_type;
    int      i_skip = 0;
423
    int      i_extension_flag = 0;
424
    int      i_extension_length = 0;
425
    uint16_t i_sequence_number = 0;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
426

427
428
    if( p_block == NULL )
        return NULL;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
429

430
    if( p_block->i_buffer < RTP_HEADER_LEN )
431
432
        goto trash;

433
    /* Parse the header and make some verifications.
434
     * See RFC 3550. */
435
436
    i_rtp_version     = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
    i_CSRC_count      = p_block->p_buffer[0] & 0x0F;
437
    i_extension_flag  = p_block->p_buffer[0] & 0x10;
438
    i_payload_type    = p_block->p_buffer[1] & 0x7F;
439
    i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
440
441

    if( i_rtp_version != 2 )
442
        msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
443

444
    if( i_payload_type == 14 || i_payload_type == 32)
445
        i_skip = 4;
446
    else if( i_payload_type !=  33 )
447
        msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
448
    if( i_extension_flag )
449
        i_extension_length = 4 +
450
            4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
451

452
    /* Skip header + CSRC extension field n*(32 bits) + extension */
453
    i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
454

455
    if( i_skip >= p_block->i_buffer )
456
        goto trash;
457

458
    /* Return the packet without the RTP header, remember seqno in i_dts */
459
460
    p_block->i_buffer -= i_skip;
    p_block->p_buffer += i_skip;
Marian Durkovic's avatar
Marian Durkovic committed
461
    p_block->i_pts = mdate();
462
    p_block->i_dts = (mtime_t) i_sequence_number;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
463

464
465
466
467
468
469
470
471
472
#if 0
    /* Emulate packet loss */
    if ( (i_sequence_number % 4000) == 0)
    {
        msg_Warn( p_access, "Emulating packet drop" );
        block_Release( p_block );
        return NULL;
    }
#endif
Jean-Paul Saman's avatar
Jean-Paul Saman committed
473

474
    return p_block;
475

476

477
478
479
480
481
482
483
484
485
trash:
    msg_Warn( p_access, "received a too short packet for RTP" );
    block_Release( p_block );
    return NULL;
}

static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
{
    access_sys_t *p_sys = p_access->p_sys;
Marian Durkovic's avatar
Marian Durkovic committed
486
    mtime_t   i_first = mdate();
487
488
489
490
    int       i_count = 0;
    block_t   *p = p_block;

    for( ;; )
491
    {
Marian Durkovic's avatar
Marian Durkovic committed
492
        mtime_t i_date = mdate();
493
494
495
496

        if( p && rtp_ChainInsert( p_access, p ))
            i_count++;

Marian Durkovic's avatar
Marian Durkovic committed
497
498
        /* Require at least 2 packets in the buffer */
        if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
499
500
501
502
            break;

        p = BlockParseRTP( p_access, BlockUDP( p_access ));
        if( !p && (i_date - i_first) > p_sys->i_rtp_late ) 
503
        {
504
505
            msg_Err( p_access, "Error in RTP prebuffering!" );
            break;
506
507
        }
    }
508

509
510
511
512
    msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
    p_access->info.b_prebuffered = VLC_TRUE;
    p = p_sys->p_list;
    p_sys->p_list = p_sys->p_list->p_next;
513
    p_sys->i_last_seqno = (uint16_t) p->i_dts;
514
515
    p->p_next = NULL;
    return p;
516
}
517

518
519
static block_t *BlockRTP( access_t *p_access )
{
520
521
    access_sys_t *p_sys = p_access->p_sys;
    block_t *p;
522

Marian Durkovic's avatar
Marian Durkovic committed
523
524
525
526
    while ( !p_sys->p_list || 
             ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
    {
        p = BlockParseRTP( p_access, BlockUDP( p_access ));
527

Marian Durkovic's avatar
Marian Durkovic committed
528
529
        if ( !p ) 
            return NULL;
530

Marian Durkovic's avatar
Marian Durkovic committed
531
532
533
534
535
        if ( !p_access->info.b_prebuffered )
            return BlockPrebufferRTP( p_access, p );
 
        rtp_ChainInsert( p_access, p );
    }
536
537
538
539

    p = p_sys->p_list;
    p_sys->p_list = p_sys->p_list->p_next;
    p_sys->i_last_seqno++;
540
    if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
541
542
    {
        msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
543
544
                 p_sys->i_last_seqno, (uint16_t) p->i_dts );
        p_sys->i_last_seqno = (uint16_t) p->i_dts;
545
546
547
    }
    p->p_next = NULL;
    return p;
548
549
550
}

/*****************************************************************************
551
 * BlockChoose: decide between RTP and UDP
552
 *****************************************************************************/
553
static block_t *BlockChoose( access_t *p_access )
554
{
555
556
557
558
    block_t *p_block;
    int     i_rtp_version;
    int     i_CSRC_count;
    int     i_payload_type;
559

560
561
    if( ( p_block = BlockUDP( p_access ) ) == NULL )
        return NULL;
562

563
    if( p_block->p_buffer[0] == 0x47 )
564
    {
565
566
        msg_Dbg( p_access, "detected TS over raw UDP" );
        p_access->pf_block = BlockUDP;
567
        p_access->info.b_prebuffered = VLC_TRUE;
568
        return p_block;
569
570
    }

571
572
573
    if( p_block->i_buffer < RTP_HEADER_LEN )
        return p_block;

574
    /* Parse the header and make some verifications.
575
     * See RFC 3550. */
576

577
578
579
    i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
    i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
    i_payload_type = ( p_block->p_buffer[1] & 0x7F );
580

581
    if( i_rtp_version != 2 )
582
    {
583
584
        msg_Dbg( p_access, "no supported RTP header detected" );
        p_access->pf_block = BlockUDP;
585
        p_access->info.b_prebuffered = VLC_TRUE;
586
        return p_block;
587
588
    }

589
    switch( i_payload_type )
590
    {
591
592
        case 33:
            msg_Dbg( p_access, "detected TS over RTP" );
593
            p_access->psz_demux = strdup( "ts" );
594
595
596
597
            break;

        case 14:
            msg_Dbg( p_access, "detected MPEG audio over RTP" );
598
            p_access->psz_demux = strdup( "mpga" );
599
600
601
602
            break;

        case 32:
            msg_Dbg( p_access, "detected MPEG video over RTP" );
603
            p_access->psz_demux = strdup( "mpgv" );
604
605
606
607
608
            break;

        default:
            msg_Dbg( p_access, "no RTP header detected" );
            p_access->pf_block = BlockUDP;
609
            p_access->info.b_prebuffered = VLC_TRUE;
610
            return p_block;
611
612
    }

613
614
    if( !BlockParseRTP( p_access, p_block )) return NULL;

615
    p_access->pf_block = BlockRTP;
616

617
    return BlockPrebufferRTP( p_access, p_block );
618
}