udp.c 13.5 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

Laurent Aimar's avatar
Laurent Aimar committed
46 47 48
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

49
vlc_module_begin();
Gildas Bazin's avatar
 
Gildas Bazin committed
50
    set_description( _("UDP/RTP input") );
Gildas Bazin's avatar
 
Gildas Bazin committed
51 52 53 54

    add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
                 CACHING_LONGTEXT, VLC_TRUE );

55
    set_capability( "access", 0 );
56
    add_shortcut( "udp" );
57 58 59
    add_shortcut( "udpstream" );
    add_shortcut( "udp4" );
    add_shortcut( "udp6" );
60 61 62
    add_shortcut( "rtp" );
    add_shortcut( "rtp4" );
    add_shortcut( "rtp6" );
63
    set_callbacks( Open, Close );
64 65
vlc_module_end();

Laurent Aimar's avatar
Laurent Aimar committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define RTP_HEADER_LEN 12

static ssize_t Read    ( input_thread_t *, byte_t *, size_t );
static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );

struct access_sys_t
{
    int fd;
};

80 81 82 83 84
/*****************************************************************************
 * Open: open the socket
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
85 86 87
    input_thread_t     *p_input = (input_thread_t *)p_this;
    access_sys_t       *p_sys;

88 89 90 91 92 93 94
    char *              psz_name = strdup(p_input->psz_name);
    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, i_server_port = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
95
    vlc_value_t         val;
96

Laurent Aimar's avatar
Laurent Aimar committed
97 98 99 100

    /* First set ipv4/ipv6 */
    var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
101 102 103 104

    if( *p_input->psz_access )
    {
        /* Find out which shortcut was used */
Gildas Bazin's avatar
 
Gildas Bazin committed
105 106
        if( !strncmp( p_input->psz_access, "udp4", 6 ) ||
            !strncmp( p_input->psz_access, "rtp4", 6 ))
107
        {
Laurent Aimar's avatar
Laurent Aimar committed
108 109 110 111 112
            val.b_bool = VLC_TRUE;
            var_Set( p_input, "ipv4", val );

            val.b_bool = VLC_FALSE;
            var_Set( p_input, "ipv6", val );
113
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
114 115
        else if( !strncmp( p_input->psz_access, "udp6", 6 ) ||
                 !strncmp( p_input->psz_access, "rtp6", 6 ) )
116
        {
Laurent Aimar's avatar
Laurent Aimar committed
117 118 119 120 121
            val.b_bool = VLC_TRUE;
            var_Set( p_input, "ipv6", val );

            val.b_bool = VLC_FALSE;
            var_Set( p_input, "ipv4", val );
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        }
    }

    /* 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
148
            *psz_parser++ = '\0'; /* Terminate server name */
149 150 151 152 153 154 155 156 157 158 159 160
            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
161
        *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

        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
185
            *psz_parser++ = '\0'; /* Terminate bind address if necessary */
186 187 188 189
            psz_bind_port = psz_parser;
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
190 191
    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
192 193 194 195
    {
        i_bind_port = config_GetInt( p_this, "server-port" );
    }

196 197 198
    msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
             psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );

Laurent Aimar's avatar
Laurent Aimar committed
199 200
    p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
    if( p_sys == NULL )
201
    {
Laurent Aimar's avatar
Laurent Aimar committed
202 203
        msg_Err( p_input, "out of memory" );
        return VLC_EGENERIC;
204
    }
205

Laurent Aimar's avatar
Laurent Aimar committed
206 207 208
    p_sys->fd = net_OpenUDP( p_input, psz_bind_addr, i_bind_port,
                                      psz_server_addr, i_server_port );
    if( p_sys->fd < 0 )
209
    {
Laurent Aimar's avatar
Laurent Aimar committed
210 211 212 213
        msg_Err( p_input, "cannot open socket" );
        free( psz_name );
        free( p_sys );
        return VLC_EGENERIC;
214
    }
Laurent Aimar's avatar
Laurent Aimar committed
215
    free( psz_name );
216

Laurent Aimar's avatar
Laurent Aimar committed
217
    /* FIXME */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
218 219 220
    var_Create( p_input, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_input, "mtu", &val);
    p_input->i_mtu = val.i_int;
Laurent Aimar's avatar
Laurent Aimar committed
221 222 223 224 225 226 227 228 229 230 231 232 233

    /* fill p_input fields */
    p_input->pf_read = RTPChoose;
    p_input->pf_set_program = input_SetProgram;
    p_input->pf_set_area = NULL;
    p_input->pf_seek = NULL;

    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_input->stream.b_pace_control = VLC_FALSE;
    p_input->stream.b_seekable = VLC_FALSE;
    p_input->stream.p_selected_area->i_tell = 0;
    p_input->stream.i_method = INPUT_METHOD_NETWORK;
    vlc_mutex_unlock( &p_input->stream.stream_lock );
234

Gildas Bazin's avatar
 
Gildas Bazin committed
235
    /* Update default_pts to a suitable value for udp access */
Gildas Bazin's avatar
 
Gildas Bazin committed
236 237 238
    var_Create( p_input, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_input, "udp-caching", &val );
    p_input->i_pts_delay = val.i_int * 1000;
Gildas Bazin's avatar
 
Gildas Bazin committed
239

Laurent Aimar's avatar
Laurent Aimar committed
240
    return VLC_SUCCESS;
241
}
242 243 244 245 246 247

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
248 249
    input_thread_t *p_input = (input_thread_t *)p_this;
    access_sys_t   *p_sys = p_input->p_access_data;
250 251 252

    msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );

Laurent Aimar's avatar
Laurent Aimar committed
253
    net_Close( p_sys->fd );
254

Laurent Aimar's avatar
Laurent Aimar committed
255
    free( p_sys );
256 257 258 259 260 261 262
}

/*****************************************************************************
 * Read: read on a file descriptor, checking b_die periodically
 *****************************************************************************/
static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
{
Laurent Aimar's avatar
Laurent Aimar committed
263
    access_sys_t   *p_sys = p_input->p_access_data;
264

Laurent Aimar's avatar
Laurent Aimar committed
265
    return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
266 267
}

268 269 270 271 272 273 274 275 276
/*****************************************************************************
 * RTPRead : read from the network, and parse the RTP header
 *****************************************************************************/
static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
                        size_t i_len )
{
    int         i_rtp_version;
    int         i_CSRC_count;
    int         i_payload_type;
277
    int         i_skip = 0;
278 279 280 281 282 283 284

    byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );

    /* Get the raw data from the socket.
     * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
    ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );

Jean-Paul Saman's avatar
Jean-Paul Saman committed
285
    if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
286 287 288 289 290 291 292 293 294 295 296

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

    i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
    i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
    i_payload_type = ( p_tmp_buffer[1] & 0x7F );

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

297 298 299 300 301 302
    if( i_payload_type == 14 )
    {
        i_skip = 4;
    }
    else if( i_payload_type !=  33 && i_payload_type != 32 )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
303 304
        msg_Dbg( p_input, "unsupported RTP payload type (%u)",
                 i_payload_type );
305 306
    }
    i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
307

Jean-Paul Saman's avatar
Jean-Paul Saman committed
308
    /* A CSRC extension field is 32 bits in size (4 bytes) */
309
    if ( i_ret < i_skip )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
310 311 312 313 314 315 316 317
    {
        /* Packet is not big enough to hold the complete RTP_HEADER with
         * CSRC extensions.
         */
        msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
        return 0;
    }

318
    /* Return the packet without the RTP header. */
319
    i_ret -= i_skip;
320 321 322 323 324 325 326 327

    if ( (size_t)i_ret > i_len )
    {
        /* This should NOT happen. */
        msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
        i_ret = i_len;
    }

328
    p_input->p_vlc->pf_memcpy( p_buffer, &p_tmp_buffer[i_skip], i_ret );
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

    return i_ret;
}

/*****************************************************************************
 * RTPChoose : read from the network, and decide whether it's UDP or RTP
 *****************************************************************************/
static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
                          size_t i_len )
{
    int         i_rtp_version;
    int         i_CSRC_count;
    int         i_payload_type;

    byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );

    /* Get the raw data from the socket.
     * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
    ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );

Jean-Paul Saman's avatar
Jean-Paul Saman committed
349
    if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    /* Check that it's not TS. */
    if ( p_tmp_buffer[0] == 0x47 )
    {
        msg_Dbg( p_input, "detected TS over raw UDP" );
        p_input->pf_read = Read;
        p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
        return i_ret;
    }

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

    i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
    i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
    i_payload_type = ( p_tmp_buffer[1] & 0x7F );

    if ( i_rtp_version != 2 )
    {
369
        msg_Dbg( p_input, "no supported RTP header detected" );
370 371 372 373 374 375 376 377 378 379 380 381 382
        p_input->pf_read = Read;
        p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
        return i_ret;
    }

    switch ( i_payload_type )
    {
    case 33:
        msg_Dbg( p_input, "detected TS over RTP" );
        break;

    case 14:
        msg_Dbg( p_input, "detected MPEG audio over RTP" );
383 384 385 386
        if( !p_input->psz_demux || *p_input->psz_demux == '\0' )
        {
            p_input->psz_demux = "mp3";
        }
387 388 389 390 391 392 393 394 395 396 397 398 399 400
        break;

    case 32:
        msg_Dbg( p_input, "detected MPEG video over RTP" );
        break;

    default:
        msg_Dbg( p_input, "no RTP header detected" );
        p_input->pf_read = Read;
        p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
        return i_ret;
    }

    p_input->pf_read = RTPRead;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
401 402

    /* A CSRC extension field is 32 bits in size (4 bytes) */
403
    if( i_ret < RTP_HEADER_LEN + 4*i_CSRC_count )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
404 405 406 407 408 409 410 411 412
    {
        /* Packet is not big enough to hold the complete RTP_HEADER with
         * CSRC extensions.
         */
        msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
        return 0;
    }

    /* Return the packet without the RTP header. */
413
    i_ret -= RTP_HEADER_LEN + 4*i_CSRC_count;
414 415 416 417 418 419 420 421

    if ( (size_t)i_ret > i_len )
    {
        /* This should NOT happen. */
        msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
        i_ret = i_len;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
422 423 424
    p_input->p_vlc->pf_memcpy( p_buffer,
                               &p_tmp_buffer[RTP_HEADER_LEN + 4*i_CSRC_count],
                               i_ret );
425 426 427

    return i_ret;
}