udp.c 13.7 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
2
 * udp.c: raw UDP & RTP input module
3
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
4
 * Copyright (C) 2001-2004 VideoLAN
Derk-Jan Hartman's avatar
Derk-Jan 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's avatar
Jean-Paul Saman committed
11 12
 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
 *
13 14 15 16
 * 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
17
 *
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * 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
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
41
#define CACHING_TEXT N_("Caching value in ms")
Gildas Bazin's avatar
 
Gildas Bazin committed
42
#define CACHING_LONGTEXT N_( \
43 44
    "Allows you to modify the default caching value for UDP streams. This " \
    "value should be set in millisecond units." )
Gildas Bazin's avatar
 
Gildas Bazin committed
45

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

Laurent Aimar's avatar
Laurent Aimar committed
50 51 52
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

53
vlc_module_begin();
54
    set_shortname( _("UDP/RTP" ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
55
    set_description( _("UDP/RTP input") );
56 57
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
Gildas Bazin's avatar
 
Gildas Bazin committed
58 59 60

    add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
                 CACHING_LONGTEXT, VLC_TRUE );
61
    add_bool( "udp-auto-mtu", 1, NULL,
62
              AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
63

64
    set_capability( "access2", 0 );
65
    add_shortcut( "udp" );
66 67 68
    add_shortcut( "udpstream" );
    add_shortcut( "udp4" );
    add_shortcut( "udp6" );
69 70 71
    add_shortcut( "rtp" );
    add_shortcut( "rtp4" );
    add_shortcut( "rtp6" );
72
    set_callbacks( Open, Close );
73 74
vlc_module_end();

Laurent Aimar's avatar
Laurent Aimar committed
75 76 77 78 79
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define RTP_HEADER_LEN 12

80 81 82 83
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
84 85 86 87

struct access_sys_t
{
    int fd;
88 89 90

    int i_mtu;
    vlc_bool_t b_auto_mtu;
Laurent Aimar's avatar
Laurent Aimar committed
91 92
};

93 94 95 96 97
/*****************************************************************************
 * Open: open the socket
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
98 99
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
100

101 102 103 104 105 106 107 108
    char *psz_name = strdup( p_access->psz_path );
    char *psz_parser = psz_name;
    char *psz_server_addr = "";
    char *psz_server_port = "";
    char *psz_bind_addr = "";
    char *psz_bind_port = "";
    int  i_bind_port = 0;
    int  i_server_port = 0;
109

Laurent Aimar's avatar
Laurent Aimar committed
110 111

    /* First set ipv4/ipv6 */
112 113
    var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
114

115
    if( *p_access->psz_access )
116
    {
117
        vlc_value_t val;
118
        /* Find out which shortcut was used */
119 120
        if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
            !strncmp( p_access->psz_access, "rtp4", 6 ))
121
        {
Laurent Aimar's avatar
Laurent Aimar committed
122
            val.b_bool = VLC_TRUE;
123
            var_Set( p_access, "ipv4", val );
Laurent Aimar's avatar
Laurent Aimar committed
124 125

            val.b_bool = VLC_FALSE;
126
            var_Set( p_access, "ipv6", val );
127
        }
128 129
        else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
                 !strncmp( p_access->psz_access, "rtp6", 6 ) )
130
        {
Laurent Aimar's avatar
Laurent Aimar committed
131
            val.b_bool = VLC_TRUE;
132
            var_Set( p_access, "ipv6", val );
Laurent Aimar's avatar
Laurent Aimar committed
133 134

            val.b_bool = VLC_FALSE;
135
            var_Set( p_access, "ipv4", val );
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        }
    }

    /* Parse psz_name syntax :
     * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
    if( *psz_parser && *psz_parser != '@' )
    {
        /* Found server */
        psz_server_addr = psz_parser;

        while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
        {
            if( *psz_parser == '[' )
            {
                /* IPv6 address */
                while( *psz_parser && *psz_parser != ']' )
                {
                    psz_parser++;
                }
            }
            psz_parser++;
        }

        if( *psz_parser == ':' )
        {
            /* Found server port */
Laurent Aimar's avatar
Laurent Aimar committed
162
            *psz_parser++ = '\0'; /* Terminate server name */
163 164 165 166 167 168 169 170 171 172 173 174
            psz_server_port = psz_parser;

            while( *psz_parser && *psz_parser != '@' )
            {
                psz_parser++;
            }
        }
    }

    if( *psz_parser == '@' )
    {
        /* Found bind address or bind port */
Laurent Aimar's avatar
Laurent Aimar committed
175
        *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

        if( *psz_parser && *psz_parser != ':' )
        {
            /* Found bind address */
            psz_bind_addr = psz_parser;

            while( *psz_parser && *psz_parser != ':' )
            {
                if( *psz_parser == '[' )
                {
                    /* IPv6 address */
                    while( *psz_parser && *psz_parser != ']' )
                    {
                        psz_parser++;
                    }
                }
                psz_parser++;
            }
        }

        if( *psz_parser == ':' )
        {
            /* Found bind port */
Laurent Aimar's avatar
Laurent Aimar committed
199
            *psz_parser++ = '\0'; /* Terminate bind address if necessary */
200 201 202 203
            psz_bind_port = psz_parser;
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
204 205
    i_server_port = strtol( psz_server_port, NULL, 10 );
    if( ( i_bind_port   = strtol( psz_bind_port,   NULL, 10 ) ) == 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
206
    {
207
        i_bind_port = var_CreateGetInteger( p_access, "server-port" );
Gildas Bazin's avatar
 
Gildas Bazin committed
208 209
    }

210
    msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
211 212
             psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );

213 214 215 216 217 218 219 220 221 222 223 224 225 226
    /* Set up p_access */
    p_access->pf_read = NULL;
    p_access->pf_block = BlockChoose;
    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;
    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
227 228
                                      psz_server_addr, i_server_port );
    if( p_sys->fd < 0 )
229
    {
230
        msg_Err( p_access, "cannot open socket" );
Laurent Aimar's avatar
Laurent Aimar committed
231 232 233
        free( psz_name );
        free( p_sys );
        return VLC_EGENERIC;
234
    }
Laurent Aimar's avatar
Laurent Aimar committed
235
    free( psz_name );
236

Laurent Aimar's avatar
Laurent Aimar committed
237
    /* FIXME */
238 239 240
    p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
    if( p_sys->i_mtu <= 1 )
        p_sys->i_mtu  = 1500;   /* Avoid problem */
241

242
    p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
243

Gildas Bazin's avatar
 
Gildas Bazin committed
244
    /* Update default_pts to a suitable value for udp access */
245
    var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
 
Gildas Bazin committed
246

Laurent Aimar's avatar
Laurent Aimar committed
247
    return VLC_SUCCESS;
248
}
249 250 251 252 253 254

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
255 256
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
257

Laurent Aimar's avatar
Laurent Aimar committed
258 259
    net_Close( p_sys->fd );
    free( p_sys );
260 261 262
}

/*****************************************************************************
263
 * Control:
264
 *****************************************************************************/
265
static int Control( access_t *p_access, int i_query, va_list args )
266
{
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    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 * );
290
            *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
291 292 293 294 295 296 297
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
298
        case ACCESS_SET_PRIVATE_ID_STATE:
299 300 301
            return VLC_EGENERIC;

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

305 306
    }
    return VLC_SUCCESS;
307 308
}

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

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

328 329
    if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
        p_sys->i_mtu < 32767 )
330
    {
331 332
        /* Increase by 100% */
        p_sys->i_mtu *= 2;
333 334
        msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
    }
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    return p_block;
}

/*****************************************************************************
 * BlockParseRTP/BlockRTP:
 *****************************************************************************/
static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
{
    int     i_rtp_version;
    int     i_CSRC_count;
    int     i_payload_type;
    int     i_skip = 0;

    if( p_block->i_buffer < RTP_HEADER_LEN )
        goto trash;
351 352 353

    /* Parse the header and make some verifications.
     * See RFC 1889 & RFC 2250. */
354 355 356
    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 );
357 358

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

361 362 363
    if( i_payload_type == 14 )
        i_skip = 4;
    else if( i_payload_type !=  33 && i_payload_type != 32 )
364 365
        msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );

366
    i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
367

Jean-Paul Saman's avatar
Jean-Paul Saman committed
368
    /* A CSRC extension field is 32 bits in size (4 bytes) */
369 370
    if( i_skip >= p_block->i_buffer )
        goto trash;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
371

372
    /* Return the packet without the RTP header. */
373 374
    p_block->i_buffer -= i_skip;
    p_block->p_buffer += i_skip;
375

376
    return p_block;
377

378 379 380 381 382
trash:
    msg_Warn( p_access, "received a too short packet for RTP" );
    block_Release( p_block );
    return NULL;
}
383

384 385 386
static block_t *BlockRTP( access_t *p_access )
{
    return BlockParseRTP( p_access, BlockUDP( p_access ) );
387 388 389
}

/*****************************************************************************
390
 * BlockChoose: decide between RTP and UDP
391
 *****************************************************************************/
392
static block_t *BlockChoose( access_t *p_access )
393
{
394 395 396 397
    block_t *p_block;
    int     i_rtp_version;
    int     i_CSRC_count;
    int     i_payload_type;
398

399 400
    if( ( p_block = BlockUDP( p_access ) ) == NULL )
        return NULL;
401

402
    if( p_block->p_buffer[0] == 0x47 )
403
    {
404 405 406
        msg_Dbg( p_access, "detected TS over raw UDP" );
        p_access->pf_block = BlockUDP;
        return p_block;
407 408
    }

409 410 411
    if( p_block->i_buffer < RTP_HEADER_LEN )
        return p_block;

412 413 414
    /* Parse the header and make some verifications.
     * See RFC 1889 & RFC 2250. */

415 416 417
    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 );
418

419
    if( i_rtp_version != 2 )
420
    {
421 422 423
        msg_Dbg( p_access, "no supported RTP header detected" );
        p_access->pf_block = BlockUDP;
        return p_block;
424 425
    }

426
    switch( i_payload_type )
427
    {
428 429
        case 33:
            msg_Dbg( p_access, "detected TS over RTP" );
430
            p_access->psz_demux = strdup( "ts" );
431 432 433 434
            break;

        case 14:
            msg_Dbg( p_access, "detected MPEG audio over RTP" );
435
            p_access->psz_demux = strdup( "mpga" );
436 437 438 439
            break;

        case 32:
            msg_Dbg( p_access, "detected MPEG video over RTP" );
440
            p_access->psz_demux = strdup( "mpgv" );
441 442 443 444 445 446
            break;

        default:
            msg_Dbg( p_access, "no RTP header detected" );
            p_access->pf_block = BlockUDP;
            return p_block;
447 448
    }

449
    p_access->pf_block = BlockRTP;
450

451
    return BlockParseRTP( p_access, p_block );
452
}