udp.c 21.1 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
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
5
 * Copyright (C) 2007 Remi Denis-Courmont
hartman's avatar
hartman committed
6
 * $Id$
7
8
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9
 *          Tristan Leteurtre <tooney@via.ecp.fr>
Laurent Aimar's avatar
Laurent Aimar committed
10
 *          Laurent Aimar <fenrir@via.ecp.fr>
11
 *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
12
 *          Remi Denis-Courmont
13
 *
Jean-Paul Saman's avatar
Jean-Paul Saman committed
14
 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
15
 *
16
17
18
19
 * 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
20
 *
21
22
23
24
25
26
27
 * 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
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29
30
31
32
33
34
35
 *****************************************************************************/

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

#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
36
37
#include <vlc_access.h>
#include <vlc_network.h>
38

39
40
41
42
43
44
45
46
47
48
#ifndef SOCK_DCCP /* provisional API */
# ifdef __linux__
#  define SOCK_DCCP 6
# endif
#endif

#ifndef IPPROTO_DCCP
# define IPPROTO_DCCP 33 /* IANA */
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
49
50
51
52
#ifndef IPPROTO_UDPLITE
# define IPPROTO_UDPLITE 136 /* from IANA */
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
53
#define MTU 65535
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54

55
56
57
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
58
#define CACHING_TEXT N_("Caching value in ms")
gbazin's avatar
   
gbazin committed
59
#define CACHING_LONGTEXT N_( \
60
    "Caching value for UDP streams. This " \
zorglub's avatar
zorglub committed
61
    "value should be set in milliseconds." )
gbazin's avatar
   
gbazin committed
62

63
#define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
64
#define RTP_LATE_LONGTEXT N_( \
zorglub's avatar
zorglub committed
65
66
    "VLC reorders RTP packets. The input will wait for late packets at most "\
    "the time specified here (in milliseconds)." )
67

Laurent Aimar's avatar
Laurent Aimar committed
68
69
70
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

71
vlc_module_begin();
zorglub's avatar
zorglub committed
72
    set_shortname( _("UDP/RTP" ) );
gbazin's avatar
   
gbazin committed
73
    set_description( _("UDP/RTP input") );
zorglub's avatar
zorglub committed
74
75
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
gbazin's avatar
   
gbazin committed
76
77
78

    add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
                 CACHING_LONGTEXT, VLC_TRUE );
79
    add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
80
    add_obsolete_bool( "udp-auto-mtu" );
gbazin's avatar
   
gbazin committed
81

82
    set_capability( "access2", 0 );
83
    add_shortcut( "udp" );
84
85
86
    add_shortcut( "udpstream" );
    add_shortcut( "udp4" );
    add_shortcut( "udp6" );
87
88
89
    add_shortcut( "rtp" );
    add_shortcut( "rtp4" );
    add_shortcut( "rtp6" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
90
    add_shortcut( "udplite" );
91
    add_shortcut( "rtptcp" );
92
    add_shortcut( "dccp" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
93

94
    set_callbacks( Open, Close );
95
96
vlc_module_end();

Laurent Aimar's avatar
Laurent Aimar committed
97
98
99
100
101
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define RTP_HEADER_LEN 12

102
static block_t *BlockUDP( access_t * );
103
static block_t *BlockTCP( access_t * );
104
105
106
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
107
108
109
110

struct access_sys_t
{
    int fd;
111

112
    vlc_bool_t b_framed_rtp;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
113

114
115
    /* reorder rtp packets when out-of-sequence */
    uint16_t i_last_seqno;
116
    mtime_t i_rtp_late;
117
118
    block_t *p_list;
    block_t *p_end;
119
    block_t *p_partial_frame; /* Partial Framed RTP packet */
Laurent Aimar's avatar
Laurent Aimar committed
120
121
};

122
123
124
125
126
/*****************************************************************************
 * Open: open the socket
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
127
128
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
129

130
    char *psz_name = strdup( p_access->psz_path );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
131
132
    char *psz_parser;
    const char *psz_server_addr, *psz_bind_addr = "";
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
133
    int  i_bind_port, i_server_port = 0;
134
    int fam = AF_UNSPEC, proto = IPPROTO_UDP;
135

136
137
    if (strlen (p_access->psz_access) >= 3)
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
138
        switch (p_access->psz_access[3])
139
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
140
141
142
            case '4':
                fam = AF_INET;
                break;
Laurent Aimar's avatar
Laurent Aimar committed
143

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
144
145
146
            case '6':
                fam = AF_INET6;
                break;
147
        }
148
149
        if (strcmp (p_access->psz_access + 3, "lite") == 0)
            proto = IPPROTO_UDPLITE;
150
        else
151
152
        if (strcmp (p_access->psz_access + 3, "tcp") == 0)
            proto = IPPROTO_TCP;
153
154
155
        else
        if (strcmp (p_access->psz_access, "dccp") == 0)
            proto = IPPROTO_DCCP;
156
    }
157

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

160
161
    /* Parse psz_name syntax :
     * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
162
163
    psz_parser = strchr( psz_name, '@' );
    if( psz_parser != NULL )
164
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
165
166
167
        /* Found bind address and/or bind port */
        *psz_parser++ = '\0';
        psz_bind_addr = psz_parser;
168

169
        if( psz_bind_addr[0] == '[' )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
170
171
            /* skips bracket'd IPv6 address */
            psz_parser = strchr( psz_parser, ']' );
172

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
173
        if( psz_parser != NULL )
174
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
175
176
            psz_parser = strchr( psz_parser, ':' );
            if( psz_parser != NULL )
177
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
178
179
                *psz_parser++ = '\0';
                i_bind_port = atoi( psz_parser );
180
181
182
183
            }
        }
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
184
    psz_server_addr = psz_name;
185
186
187
    psz_parser = ( psz_server_addr[0] == '[' )
        ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
        : psz_name;
188

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
189
190
    if( psz_parser != NULL )
    {
191
        psz_parser = strchr( psz_parser, ':' );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
192
        if( psz_parser != NULL )
193
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
194
195
            *psz_parser++ = '\0';
            i_server_port = atoi( psz_parser );
196
197
198
        }
    }

199
    msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
200
201
             psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );

202
    /* Set up p_access */
203
204
    access_InitFields( p_access );
    ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
205
    p_access->info.b_prebuffered = VLC_FALSE;
206
    MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
207
    memset (p_sys, 0, sizeof (*p_sys));
208

209
210
211
212
213
214
215
216
217
218
    switch (proto)
    {
        case IPPROTO_UDP:
        case IPPROTO_UDPLITE:
            p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
                                       psz_server_addr, i_server_port, fam,
                                       proto );
            break;

        case IPPROTO_TCP:
219
            p_sys->fd = net_ConnectTCP( p_access, psz_server_addr, i_server_port );
220
221
222
223
224
225
226
227
228
229
230
231
232
            p_sys->b_framed_rtp = VLC_TRUE;
            break;

        case IPPROTO_DCCP:
#ifdef SOCK_DCCP
            p_sys->fd = net_Connect( p_access, psz_server_addr, i_server_port,
                                     SOCK_DCCP, IPPROTO_DCCP );
#else
            p_sys->fd = -1;
            msg_Err( p_access, "DCCP support not compiled-in!" );
#endif
            break;
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
233
234
    free (psz_name);
    if( p_sys->fd == -1 )
235
    {
236
        msg_Err( p_access, "cannot open socket" );
Laurent Aimar's avatar
Laurent Aimar committed
237
238
        free( p_sys );
        return VLC_EGENERIC;
239
240
    }

241
    shutdown( p_sys->fd, SHUT_WR );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
242

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
243
    net_SetCSCov (p_sys->fd, -1, 12);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
244

245
    if (p_sys->b_framed_rtp)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
246
247
248
    {
        /* We don't do autodetection and prebuffering in case of framing */
        p_access->pf_block = BlockRTP;
249
    }
250

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

254
255
256
    /* 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;
257
258
    p_sys->p_list = NULL;
    p_sys->p_end = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
259
    return VLC_SUCCESS;
260
}
261
262
263
264
265
266

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
267
268
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
269

270
    block_ChainRelease( p_sys->p_list );
Laurent Aimar's avatar
Laurent Aimar committed
271
272
    net_Close( p_sys->fd );
    free( p_sys );
273
274
275
}

/*****************************************************************************
276
 * Control:
277
 *****************************************************************************/
278
static int Control( access_t *p_access, int i_query, va_list args )
279
{
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    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 * );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
297
            *pi_int = MTU;
298
299
300
301
            break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
302
            *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
303
304
305
306
307
308
309
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
310
        case ACCESS_SET_PRIVATE_ID_STATE:
311
312
313
            return VLC_EGENERIC;

        default:
314
            msg_Warn( p_access, "unimplemented query in control" );
315
            return VLC_EGENERIC;
316

317
318
    }
    return VLC_SUCCESS;
319
320
}

321
/*****************************************************************************
322
 * BlockUDP:
323
 *****************************************************************************/
324
static block_t *BlockUDP( access_t *p_access )
325
{
326
327
    access_sys_t *p_sys = p_access->p_sys;
    block_t      *p_block;
328

329
    /* Read data */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
330
    p_block = block_New( p_access, MTU );
331
    p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
332
                                  p_block->p_buffer, MTU, VLC_FALSE );
333
    if( p_block->i_buffer < 0 )
334
335
336
337
    {
        block_Release( p_block );
        return NULL;
    }
338

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
339
    return block_Realloc( p_block, 0, p_block->i_buffer );
340
341
}

342
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
343
 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
344
345
346
347
348
 *****************************************************************************/
static block_t *BlockTCP( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    block_t      *p_block = p_sys->p_partial_frame;
349
350
351

    if( p_access->info.b_eof )
        return NULL;
352
353
354
355

    if( p_block == NULL )
    {
        /* MTU should always be 65535 in this case */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
356
        p_sys->p_partial_frame = p_block = block_New( p_access, 2 + MTU );
357
358
359
360
361
362
363
364
        if (p_block == NULL)
            return NULL;
    }

    /* Read RTP framing */
    if (p_block->i_buffer < 2)
    {
        /* FIXME: not very efficient */
365
366
367
        int i_read = net_Read( p_access, p_sys->fd, NULL,
                               p_block->p_buffer + p_block->i_buffer,
                               2 - p_block->i_buffer, VLC_FALSE );
368
369
370
371
372
373
374
375
376
377
378
379
        if( i_read <= 0 )
            goto error;

        p_block->i_buffer += i_read;
        if (p_block->i_buffer < 2)
            return NULL;
    }

    uint16_t framelen = GetWLE( p_block->p_buffer );
    /* Read RTP frame */
    if( framelen > 0 )
    {
380
381
382
        int i_read = net_Read( p_access, p_sys->fd, NULL,
                               p_block->p_buffer + p_block->i_buffer,
                               2 + framelen - p_block->i_buffer, VLC_FALSE );
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
        if( i_read <= 0 )
            goto error;

        p_block->i_buffer += i_read;
    }

    if( p_block->i_buffer < (2 + framelen) )
        return NULL; // incomplete frame

    /* Hide framing from RTP layer */
    p_block->p_buffer += 2;
    p_block->i_buffer -= 2;
    p_sys->p_partial_frame = NULL;
    return p_block;

error:
399
    p_access->info.b_eof = VLC_TRUE;
400
401
402
403
404
405
    block_Release( p_block );
    p_sys->p_partial_frame = NULL;
    return NULL;
}


406
407
408
409
/*
 * rtp_ChainInsert - insert a p_block in the chain and
 * look at the sequence numbers.
 */
410
static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
411
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
412
    access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
413
414
    block_t *p_prev = NULL;
    block_t *p = p_sys->p_end;
415
    uint16_t i_new = (uint16_t) p_block->i_dts;
416
    uint16_t i_tmp = 0;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
417

418
    if( !p_sys->p_list )
419
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
420
        p_sys->p_list = p_block;
421
422
        p_sys->p_end = p_block;
        return VLC_TRUE;
423
    }
424
    /* walk through the queue from top down since the new packet is in
425
426
427
    most cases just appended to the end */

    for( ;; )
428
    {
429
        i_tmp = i_new - (uint16_t) p->i_dts;
430

431
        if( !i_tmp )   /* trash duplicate */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
432
            break;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
433

434
        if ( i_tmp < 32768 )
435
        {   /* insert after this block ( i_new > p->i_dts ) */
436
            p_block->p_next = p->p_next;
437
            p->p_next = p_block;
438
439
            p_block->p_prev = p;
            if (p_prev)
440
            {
441
                p_prev->p_prev = p_block;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
442
443
                msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
                        (uint16_t) p->i_dts, i_new );
444
            }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
445
            else
446
            {
447
                p_sys->p_end = p_block;
448
            }
449
            return VLC_TRUE;
450
        }
451
452
453
454
455
        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) )
            {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
456
                msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
457
                        i_new, (uint16_t) p->i_dts );
458
459
460
461
462
                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
463

464
            if( !i_tmp )   /* trash duplicate */
465
                break;
466
467
468

            /* reordering failed - append the packet to the end of queue */
            msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
469
                    "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
470
                (uint16_t) p_sys->p_end->i_dts);
471
472
473
474
475
476
477
            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;
478
    }
479
480
    block_Release( p_block );
    return VLC_FALSE;
481
482
}

483
484
485
486
487
/*****************************************************************************
 * BlockParseRTP/BlockRTP:
 *****************************************************************************/
static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
{
488
    int      i_payload_type;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
489
    size_t   i_skip = RTP_HEADER_LEN;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
490

491
492
    if( p_block == NULL )
        return NULL;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
493

494
    if( p_block->i_buffer < RTP_HEADER_LEN )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
495
496
    {
        msg_Dbg( p_access, "short RTP packet received" );
497
        goto trash;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
498
    }
499

500
    /* Parse the header and make some verifications.
501
     * See RFC 3550. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
502
503
504
505
506
507
508
509
510
511
    // Version number:
    if( ( p_block->p_buffer[0] >> 6 ) != 2)
    {
        msg_Dbg( p_access, "RTP version is %u instead of 2",
                 p_block->p_buffer[0] >> 6 );
        goto trash;
    }
    // Padding bit:
    uint8_t pad = (p_block->p_buffer[0] & 0x20)
                    ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
512
513
    // CSRC count:
    i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
514
    // Extension header:
515
    if (p_block->p_buffer[0] & 0x10) /* Extension header */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
516
517
518
519
520
    {
        i_skip += 4;
        if ((size_t)p_block->i_buffer < i_skip)
            goto trash;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
521
        i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
522
523
    }

524
    i_payload_type    = p_block->p_buffer[1] & 0x7F;
525

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
526
527
528
    /* Remember sequence number in i_dts */
    p_block->i_pts = mdate();
    p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 );
529

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
530
    /* FIXME: use rtpmap */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
531
    switch( i_payload_type )
532
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
        case 14: // MPA: MPEG Audio (RFC2250, §3.4)
            i_skip += 4; // 32 bits RTP/MPA header
            break;

        case 32: // MPV: MPEG Video (RFC2250, §3.5)
            i_skip += 4; // 32 bits RTP/MPV header
            if( (size_t)p_block->i_buffer < i_skip )
                goto trash;
            if( p_block->p_buffer[i_skip - 3] & 0x4 )
            {
                /* MPEG2 Video extension header */
                /* TODO: shouldn't we skip this too ? */
            }
            break;

        case 33: // MP2: MPEG TS (RFC2250, §2)
            /* plain TS over RTP */
            break;

        default:
            msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
554
555
            goto trash;
    }
556

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
557
    if( (size_t)p_block->i_buffer < (i_skip + pad) )
558
        goto trash;
559

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
560
    /* Remove the RTP header */
561
562
    p_block->i_buffer -= i_skip;
    p_block->p_buffer += i_skip;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
563
564
565
566
567

    /* This is the place for deciphering and authentication */

    /* Remove padding (at the end) */
    p_block->i_buffer -= pad;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
568

569
570
571
572
573
574
575
576
577
#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
578

579
    return p_block;
580

581
582
583
584
585
586
587
588
trash:
    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
589
    mtime_t   i_first = mdate();
590
591
592
593
    int       i_count = 0;
    block_t   *p = p_block;

    for( ;; )
594
    {
Marian Durkovic's avatar
Marian Durkovic committed
595
        mtime_t i_date = mdate();
596
597
598
599

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

Marian Durkovic's avatar
Marian Durkovic committed
600
601
        /* Require at least 2 packets in the buffer */
        if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
602
603
            break;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
604
        p = BlockParseRTP( p_access, BlockUDP( p_access ) );
605
        if( !p && (i_date - i_first) > p_sys->i_rtp_late )
606
        {
607
            msg_Err( p_access, "error in RTP prebuffering!" );
608
            break;
609
610
        }
    }
611

612
613
614
615
    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;
616
    p_sys->i_last_seqno = (uint16_t) p->i_dts;
617
618
    p->p_next = NULL;
    return p;
619
}
620

621
622
static block_t *BlockRTP( access_t *p_access )
{
623
624
    access_sys_t *p_sys = p_access->p_sys;
    block_t *p;
625

626
    while ( !p_sys->p_list ||
Marian Durkovic's avatar
Marian Durkovic committed
627
628
             ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
629
630
631
        p = BlockParseRTP( p_access,
                           p_sys->b_framed_rtp ? BlockTCP( p_access )
                                               : BlockUDP( p_access ) );
632
        if ( !p )
Marian Durkovic's avatar
Marian Durkovic committed
633
            return NULL;
634

Marian Durkovic's avatar
Marian Durkovic committed
635
636
        rtp_ChainInsert( p_access, p );
    }
637
638
639
640

    p = p_sys->p_list;
    p_sys->p_list = p_sys->p_list->p_next;
    p_sys->i_last_seqno++;
641
    if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
642
643
    {
        msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
644
645
                 p_sys->i_last_seqno, (uint16_t) p->i_dts );
        p_sys->i_last_seqno = (uint16_t) p->i_dts;
646
647
648
    }
    p->p_next = NULL;
    return p;
649
650
651
}

/*****************************************************************************
652
 * BlockChoose: decide between RTP and UDP
653
 *****************************************************************************/
654
static block_t *BlockChoose( access_t *p_access )
655
{
656
657
658
    block_t *p_block;
    int     i_rtp_version;
    int     i_payload_type;
659

660
661
    if( ( p_block = BlockUDP( p_access ) ) == NULL )
        return NULL;
662

663
    if( p_block->p_buffer[0] == 0x47 )
664
    {
665
666
        msg_Dbg( p_access, "detected TS over raw UDP" );
        p_access->pf_block = BlockUDP;
667
        p_access->info.b_prebuffered = VLC_TRUE;
668
        return p_block;
669
670
    }

671
672
673
    if( p_block->i_buffer < RTP_HEADER_LEN )
        return p_block;

674
    /* Parse the header and make some verifications.
675
     * See RFC 3550. */
676

677
    i_rtp_version  = p_block->p_buffer[0] >> 6;
678
    i_payload_type = ( p_block->p_buffer[1] & 0x7F );
679

680
    if( i_rtp_version != 2 )
681
    {
682
683
        msg_Dbg( p_access, "no supported RTP header detected" );
        p_access->pf_block = BlockUDP;
684
        p_access->info.b_prebuffered = VLC_TRUE;
685
        return p_block;
686
687
    }

688
    switch( i_payload_type )
689
    {
690
        case 33:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
691
            msg_Dbg( p_access, "detected MPEG2 TS over RTP" );
692
            p_access->psz_demux = strdup( "ts" );
693
694
695
            break;

        case 14:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
696
            msg_Dbg( p_access, "detected MPEG Audio over RTP" );
697
            p_access->psz_demux = strdup( "mpga" );
698
699
700
            break;

        case 32:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
701
            msg_Dbg( p_access, "detected MPEG Video over RTP" );
702
            p_access->psz_demux = strdup( "mpgv" );
703
704
705
706
707
            break;

        default:
            msg_Dbg( p_access, "no RTP header detected" );
            p_access->pf_block = BlockUDP;
708
            p_access->info.b_prebuffered = VLC_TRUE;
709
            return p_block;
710
711
    }

712
713
    if( !BlockParseRTP( p_access, p_block )) return NULL;

714
    p_access->pf_block = BlockRTP;
715

716
    return BlockPrebufferRTP( p_access, p_block );
717
}