1 /*****************************************************************************
2 * udp.c: raw UDP & RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.26 2003/12/04 16:49:43 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Tristan Leteurtre <tooney@via.ecp.fr>
10 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
31 #include <sys/types.h>
38 #include <vlc/input.h>
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
49 # include <winsock2.h>
50 # include <ws2tcpip.h>
52 # define IN_MULTICAST(a) IN_CLASSD(a)
55 # include <sys/socket.h>
60 #define RTP_HEADER_LEN 12
62 /*****************************************************************************
64 *****************************************************************************/
65 static int Open ( vlc_object_t * );
66 static void Close ( vlc_object_t * );
67 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
68 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
69 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
71 /*****************************************************************************
73 *****************************************************************************/
74 #define CACHING_TEXT N_("caching value in ms")
75 #define CACHING_LONGTEXT N_( \
76 "Allows you to modify the default caching value for udp streams. This " \
77 "value should be set in miliseconds units." )
80 set_description( _("UDP/RTP input") );
81 add_category_hint( N_("UDP"), NULL , VLC_TRUE );
82 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
83 set_capability( "access", 0 );
84 add_shortcut( "udp" );
85 add_shortcut( "udpstream" );
86 add_shortcut( "udp4" );
87 add_shortcut( "udp6" );
88 add_shortcut( "rtp" );
89 add_shortcut( "rtp4" );
90 add_shortcut( "rtp6" );
91 set_callbacks( Open, Close );
94 /*****************************************************************************
95 * Open: open the socket
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 input_thread_t * p_input = (input_thread_t *)p_this;
100 input_socket_t * p_access_data;
101 module_t * p_network;
102 char * psz_network = "";
103 char * psz_name = strdup(p_input->psz_name);
104 char * psz_parser = psz_name;
105 char * psz_server_addr = "";
106 char * psz_server_port = "";
107 char * psz_bind_addr = "";
108 char * psz_bind_port = "";
109 int i_bind_port = 0, i_server_port = 0;
110 network_socket_t socket_desc;
113 if( config_GetInt( p_input, "ipv4" ) )
115 psz_network = "ipv4";
117 if( config_GetInt( p_input, "ipv6" ) )
119 psz_network = "ipv6";
122 if( *p_input->psz_access )
124 /* Find out which shortcut was used */
125 if( !strncmp( p_input->psz_access, "udp6", 5 ) )
127 psz_network = "ipv6";
129 else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
131 psz_network = "ipv4";
135 /* Parse psz_name syntax :
136 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
138 if( *psz_parser && *psz_parser != '@' )
141 psz_server_addr = psz_parser;
143 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
145 if( *psz_parser == '[' )
148 while( *psz_parser && *psz_parser != ']' )
156 if( *psz_parser == ':' )
158 /* Found server port */
159 *psz_parser = '\0'; /* Terminate server name */
161 psz_server_port = psz_parser;
163 while( *psz_parser && *psz_parser != '@' )
170 if( *psz_parser == '@' )
172 /* Found bind address or bind port */
173 *psz_parser = '\0'; /* Terminate server port or name if necessary */
176 if( *psz_parser && *psz_parser != ':' )
178 /* Found bind address */
179 psz_bind_addr = psz_parser;
181 while( *psz_parser && *psz_parser != ':' )
183 if( *psz_parser == '[' )
186 while( *psz_parser && *psz_parser != ']' )
195 if( *psz_parser == ':' )
197 /* Found bind port */
198 *psz_parser = '\0'; /* Terminate bind address if necessary */
201 psz_bind_port = psz_parser;
205 /* Convert ports format */
206 if( *psz_server_port )
208 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
211 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
219 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
222 msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
228 if( i_bind_port == 0 )
230 i_bind_port = config_GetInt( p_this, "server-port" );
233 p_input->pf_read = RTPChoose;
234 p_input->pf_set_program = input_SetProgram;
235 p_input->pf_set_area = NULL;
236 p_input->pf_seek = NULL;
238 vlc_mutex_lock( &p_input->stream.stream_lock );
239 p_input->stream.b_pace_control = 0;
240 p_input->stream.b_seekable = 0;
241 p_input->stream.p_selected_area->i_tell = 0;
242 p_input->stream.i_method = INPUT_METHOD_NETWORK;
243 vlc_mutex_unlock( &p_input->stream.stream_lock );
245 if( *psz_server_addr || i_server_port )
247 msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
248 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
249 psz_server_addr, i_server_port);
250 msg_Err( p_input, "or local port, type : %s:@%s:%d",
251 *p_input->psz_access ? p_input->psz_access : "udp",
252 psz_server_addr, i_server_port );
255 psz_server_addr = "";
258 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
259 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
261 /* Prepare the network_socket_t structure */
262 socket_desc.i_type = NETWORK_UDP;
263 socket_desc.psz_bind_addr = psz_bind_addr;
264 socket_desc.i_bind_port = i_bind_port;
265 socket_desc.psz_server_addr = psz_server_addr;
266 socket_desc.i_server_port = i_server_port;
267 socket_desc.i_ttl = 0;
269 /* Find an appropriate network module */
270 p_input->p_private = (void*) &socket_desc;
271 p_network = module_Need( p_input, "network", psz_network );
273 if( p_network == NULL )
277 module_Unneed( p_input, p_network );
279 p_access_data = malloc( sizeof(input_socket_t) );
280 p_input->p_access_data = (access_sys_t *)p_access_data;
282 if( p_access_data == NULL )
284 msg_Err( p_input, "out of memory" );
288 p_access_data->i_handle = socket_desc.i_handle;
289 p_input->i_mtu = socket_desc.i_mtu;
291 /* Update default_pts to a suitable value for udp access */
292 var_Create( p_input, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
293 var_Get( p_input, "udp-caching", &val );
294 p_input->i_pts_delay = val.i_int * 1000;
299 /*****************************************************************************
300 * Close: free unused data structures
301 *****************************************************************************/
302 static void Close( vlc_object_t *p_this )
304 input_thread_t * p_input = (input_thread_t *)p_this;
305 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
307 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
310 CloseHandle( (HANDLE)p_access_data->i_handle );
311 #elif defined( WIN32 )
312 closesocket( p_access_data->i_handle );
314 close( p_access_data->i_handle );
317 free( p_access_data );
320 /*****************************************************************************
321 * Read: read on a file descriptor, checking b_die periodically
322 *****************************************************************************/
323 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
329 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
330 struct timeval timeout;
335 /* Initialize file descriptor set */
337 FD_SET( p_access_data->i_handle, &fds );
339 /* We'll wait 0.5 second if nothing happens */
341 timeout.tv_usec = 500000;
343 /* Find if some data is available */
344 while( ((i_ret = select( p_access_data->i_handle + 1, &fds,
345 NULL, NULL, &timeout )) == 0)
346 || ((i_ret < 0) && (errno == EINTR)) )
349 FD_SET( p_access_data->i_handle, &fds );
351 timeout.tv_usec = 500000;
353 if( p_input->b_die || p_input->b_error )
361 msg_Err( p_input, "network select error (%s)", strerror(errno) );
365 i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
370 /* On win32 recv() will fail if the datagram doesn't fit inside
371 * the passed buffer, even though the buffer will be filled with
372 * the first part of the datagram. */
373 if( WSAGetLastError() == WSAEMSGSIZE )
375 msg_Err( p_input, "recv() failed. "
376 "Increase the mtu size (--mtu option)" );
381 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
389 /*****************************************************************************
390 * RTPRead : read from the network, and parse the RTP header
391 *****************************************************************************/
392 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
400 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
402 /* Get the raw data from the socket.
403 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
404 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
406 if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
408 /* Parse the header and make some verifications.
409 * See RFC 1889 & RFC 2250. */
411 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
412 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
413 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
415 if ( i_rtp_version != 2 )
416 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
418 if( i_payload_type == 14 )
422 else if( i_payload_type != 33 && i_payload_type != 32 )
424 msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
426 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
428 /* A CSRC extension field is 32 bits in size (4 bytes) */
429 if ( i_ret < i_skip )
431 /* Packet is not big enough to hold the complete RTP_HEADER with
434 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
438 /* Return the packet without the RTP header. */
441 if ( (size_t)i_ret > i_len )
443 /* This should NOT happen. */
444 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
448 p_input->p_vlc->pf_memcpy( p_buffer, &p_tmp_buffer[i_skip], i_ret );
453 /*****************************************************************************
454 * RTPChoose : read from the network, and decide whether it's UDP or RTP
455 *****************************************************************************/
456 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
463 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
465 /* Get the raw data from the socket.
466 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
467 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
469 if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
471 /* Check that it's not TS. */
472 if ( p_tmp_buffer[0] == 0x47 )
474 msg_Dbg( p_input, "detected TS over raw UDP" );
475 p_input->pf_read = Read;
476 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
480 /* Parse the header and make some verifications.
481 * See RFC 1889 & RFC 2250. */
483 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
484 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
485 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
487 if ( i_rtp_version != 2 )
489 msg_Dbg( p_input, "no supported RTP header detected" );
490 p_input->pf_read = Read;
491 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
495 switch ( i_payload_type )
498 msg_Dbg( p_input, "detected TS over RTP" );
502 msg_Dbg( p_input, "detected MPEG audio over RTP" );
503 if( !p_input->psz_demux || *p_input->psz_demux == '\0' )
505 p_input->psz_demux = "mp3";
510 msg_Dbg( p_input, "detected MPEG video over RTP" );
514 msg_Dbg( p_input, "no RTP header detected" );
515 p_input->pf_read = Read;
516 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
520 p_input->pf_read = RTPRead;
522 /* A CSRC extension field is 32 bits in size (4 bytes) */
523 if( i_ret < RTP_HEADER_LEN + 4*i_CSRC_count )
525 /* Packet is not big enough to hold the complete RTP_HEADER with
528 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
532 /* Return the packet without the RTP header. */
533 i_ret -= RTP_HEADER_LEN + 4*i_CSRC_count;
535 if ( (size_t)i_ret > i_len )
537 /* This should NOT happen. */
538 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
542 p_input->p_vlc->pf_memcpy( p_buffer, &p_tmp_buffer[RTP_HEADER_LEN + 4*i_CSRC_count], i_ret );