1 /*****************************************************************************
2 * udp.c: raw UDP & RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.11 2003/02/04 10:07:40 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Tristan Leteurtre <tooney@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <sys/types.h>
36 #include <vlc/input.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
44 #elif defined( _MSC_VER ) && defined( _WIN32 )
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( _("raw UDP access module") );
81 add_category_hint( N_("udp"), NULL );
82 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
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;
112 if( config_GetInt( p_input, "ipv4" ) )
114 psz_network = "ipv4";
116 if( config_GetInt( p_input, "ipv6" ) )
118 psz_network = "ipv6";
121 if( *p_input->psz_access )
123 /* Find out which shortcut was used */
124 if( !strncmp( p_input->psz_access, "udp6", 5 ) )
126 psz_network = "ipv6";
128 else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
130 psz_network = "ipv4";
134 /* Parse psz_name syntax :
135 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
137 if( *psz_parser && *psz_parser != '@' )
140 psz_server_addr = psz_parser;
142 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
144 if( *psz_parser == '[' )
147 while( *psz_parser && *psz_parser != ']' )
155 if( *psz_parser == ':' )
157 /* Found server port */
158 *psz_parser = '\0'; /* Terminate server name */
160 psz_server_port = psz_parser;
162 while( *psz_parser && *psz_parser != '@' )
169 if( *psz_parser == '@' )
171 /* Found bind address or bind port */
172 *psz_parser = '\0'; /* Terminate server port or name if necessary */
175 if( *psz_parser && *psz_parser != ':' )
177 /* Found bind address */
178 psz_bind_addr = psz_parser;
180 while( *psz_parser && *psz_parser != ':' )
182 if( *psz_parser == '[' )
185 while( *psz_parser && *psz_parser != ']' )
194 if( *psz_parser == ':' )
196 /* Found bind port */
197 *psz_parser = '\0'; /* Terminate bind address if necessary */
200 psz_bind_port = psz_parser;
204 /* Convert ports format */
205 if( *psz_server_port )
207 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
210 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
218 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
221 msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
227 if( i_bind_port == 0 )
229 i_bind_port = config_GetInt( p_this, "server-port" );
232 p_input->pf_read = RTPChoose;
233 p_input->pf_set_program = input_SetProgram;
234 p_input->pf_set_area = NULL;
235 p_input->pf_seek = NULL;
237 vlc_mutex_lock( &p_input->stream.stream_lock );
238 p_input->stream.b_pace_control = 0;
239 p_input->stream.b_seekable = 0;
240 p_input->stream.b_connected = 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;
268 /* Find an appropriate network module */
269 p_input->p_private = (void*) &socket_desc;
270 p_network = module_Need( p_input, "network", psz_network );
272 if( p_network == NULL )
276 module_Unneed( p_input, p_network );
278 p_access_data = malloc( sizeof(input_socket_t) );
279 p_input->p_access_data = (access_sys_t *)p_access_data;
281 if( p_access_data == NULL )
283 msg_Err( p_input, "out of memory" );
287 p_access_data->i_handle = socket_desc.i_handle;
288 p_input->i_mtu = socket_desc.i_mtu;
290 /* Update default_pts to a suitable value for udp access */
291 p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
296 /*****************************************************************************
297 * Close: free unused data structures
298 *****************************************************************************/
299 static void Close( vlc_object_t *p_this )
301 input_thread_t * p_input = (input_thread_t *)p_this;
302 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
304 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
307 CloseHandle( (HANDLE)p_access_data->i_handle );
308 #elif defined( WIN32 )
309 closesocket( p_access_data->i_handle );
311 close( p_access_data->i_handle );
314 free( p_access_data );
317 /*****************************************************************************
318 * Read: read on a file descriptor, checking b_die periodically
319 *****************************************************************************/
320 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
326 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
327 struct timeval timeout;
331 /* Initialize file descriptor set */
333 FD_SET( p_access_data->i_handle, &fds );
335 /* We'll wait 0.5 second if nothing happens */
337 timeout.tv_usec = 500000;
339 /* Find if some data is available */
340 i_ret = select( p_access_data->i_handle + 1, &fds,
341 NULL, NULL, &timeout );
343 if( i_ret == -1 && errno != EINTR )
345 msg_Err( p_input, "network select error (%s)", strerror(errno) );
349 ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
353 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
364 /*****************************************************************************
365 * RTPRead : read from the network, and parse the RTP header
366 *****************************************************************************/
367 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
374 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
376 /* Get the raw data from the socket.
377 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
378 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
380 if ( !i_ret ) return 0;
382 /* Parse the header and make some verifications.
383 * See RFC 1889 & RFC 2250. */
385 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
386 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
387 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
389 if ( i_rtp_version != 2 )
390 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
392 if ( i_payload_type != 33 && i_payload_type != 14
393 && i_payload_type != 32 )
394 msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
396 /* Return the packet without the RTP header. */
397 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
399 if ( (size_t)i_ret > i_len )
401 /* This should NOT happen. */
402 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
406 p_input->p_vlc->pf_memcpy( p_buffer,
407 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
413 /*****************************************************************************
414 * RTPChoose : read from the network, and decide whether it's UDP or RTP
415 *****************************************************************************/
416 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
423 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
425 /* Get the raw data from the socket.
426 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
427 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
429 if ( !i_ret ) return 0;
431 /* Check that it's not TS. */
432 if ( p_tmp_buffer[0] == 0x47 )
434 msg_Dbg( p_input, "detected TS over raw UDP" );
435 p_input->pf_read = Read;
436 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
440 /* Parse the header and make some verifications.
441 * See RFC 1889 & RFC 2250. */
443 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
444 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
445 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
447 if ( i_rtp_version != 2 )
449 msg_Dbg( p_input, "no RTP header detected" );
450 p_input->pf_read = Read;
451 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
455 switch ( i_payload_type )
458 msg_Dbg( p_input, "detected TS over RTP" );
462 msg_Dbg( p_input, "detected MPEG audio over RTP" );
466 msg_Dbg( p_input, "detected MPEG video over RTP" );
470 msg_Dbg( p_input, "no RTP header detected" );
471 p_input->pf_read = Read;
472 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
476 /* Return the packet without the RTP header. */
477 p_input->pf_read = RTPRead;
478 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
480 if ( (size_t)i_ret > i_len )
482 /* This should NOT happen. */
483 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
487 p_input->p_vlc->pf_memcpy( p_buffer,
488 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,