1 /*****************************************************************************
2 * udp.c: raw UDP & RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.13 2003/02/12 13:42:43 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 *****************************************************************************/
30 #include <sys/types.h>
37 #include <vlc/input.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
45 #elif defined( _MSC_VER ) && defined( _WIN32 )
50 # include <winsock2.h>
51 # include <ws2tcpip.h>
53 # define IN_MULTICAST(a) IN_CLASSD(a)
56 # include <sys/socket.h>
61 #define RTP_HEADER_LEN 12
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
68 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
69 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
70 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
72 /*****************************************************************************
74 *****************************************************************************/
75 #define CACHING_TEXT N_("caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77 "Allows you to modify the default caching value for udp streams. This " \
78 "value should be set in miliseconds units." )
81 set_description( _("raw UDP access module") );
82 add_category_hint( N_("udp"), NULL );
83 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
84 set_capability( "access", 0 );
85 add_shortcut( "udp" );
86 add_shortcut( "udpstream" );
87 add_shortcut( "udp4" );
88 add_shortcut( "udp6" );
89 add_shortcut( "rtp" );
90 add_shortcut( "rtp4" );
91 add_shortcut( "rtp6" );
92 set_callbacks( Open, Close );
95 /*****************************************************************************
96 * Open: open the socket
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
100 input_thread_t * p_input = (input_thread_t *)p_this;
101 input_socket_t * p_access_data;
102 module_t * p_network;
103 char * psz_network = "";
104 char * psz_name = strdup(p_input->psz_name);
105 char * psz_parser = psz_name;
106 char * psz_server_addr = "";
107 char * psz_server_port = "";
108 char * psz_bind_addr = "";
109 char * psz_bind_port = "";
110 int i_bind_port = 0, i_server_port = 0;
111 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.b_connected = 0;
242 p_input->stream.p_selected_area->i_tell = 0;
243 p_input->stream.i_method = INPUT_METHOD_NETWORK;
244 vlc_mutex_unlock( &p_input->stream.stream_lock );
246 if( *psz_server_addr || i_server_port )
248 msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
249 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
250 psz_server_addr, i_server_port);
251 msg_Err( p_input, "or local port, type : %s:@%s:%d",
252 *p_input->psz_access ? p_input->psz_access : "udp",
253 psz_server_addr, i_server_port );
256 psz_server_addr = "";
259 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
260 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
262 /* Prepare the network_socket_t structure */
263 socket_desc.i_type = NETWORK_UDP;
264 socket_desc.psz_bind_addr = psz_bind_addr;
265 socket_desc.i_bind_port = i_bind_port;
266 socket_desc.psz_server_addr = psz_server_addr;
267 socket_desc.i_server_port = i_server_port;
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 p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
297 /*****************************************************************************
298 * Close: free unused data structures
299 *****************************************************************************/
300 static void Close( vlc_object_t *p_this )
302 input_thread_t * p_input = (input_thread_t *)p_this;
303 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
305 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
308 CloseHandle( (HANDLE)p_access_data->i_handle );
309 #elif defined( WIN32 )
310 closesocket( p_access_data->i_handle );
312 close( p_access_data->i_handle );
315 free( p_access_data );
318 /*****************************************************************************
319 * Read: read on a file descriptor, checking b_die periodically
320 *****************************************************************************/
321 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
327 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
328 struct timeval timeout;
332 /* Initialize file descriptor set */
334 FD_SET( p_access_data->i_handle, &fds );
336 /* We'll wait 0.5 second if nothing happens */
338 timeout.tv_usec = 500000;
340 /* Find if some data is available */
341 i_ret = select( p_access_data->i_handle + 1, &fds,
342 NULL, NULL, &timeout );
344 if( i_ret == -1 && errno != EINTR )
346 msg_Err( p_input, "network select error (%s)", strerror(errno) );
350 ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
355 /* On win32 recv() will fail if the datagram doesn't fit inside
356 * the passed buffer, even though the buffer will be filled with
357 * the first part of the datagram. */
358 if( WSAGetLastError() == WSAEMSGSIZE )
360 msg_Err( p_input, "recv() failed. "
361 "Increase the mtu size (--mtu option)" );
366 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
377 /*****************************************************************************
378 * RTPRead : read from the network, and parse the RTP header
379 *****************************************************************************/
380 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
387 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
389 /* Get the raw data from the socket.
390 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
391 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
393 if ( !i_ret ) return 0;
395 /* Parse the header and make some verifications.
396 * See RFC 1889 & RFC 2250. */
398 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
399 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
400 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
402 if ( i_rtp_version != 2 )
403 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
405 if ( i_payload_type != 33 && i_payload_type != 14
406 && i_payload_type != 32 )
407 msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
409 /* Return the packet without the RTP header. */
410 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
412 if ( (size_t)i_ret > i_len )
414 /* This should NOT happen. */
415 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
419 p_input->p_vlc->pf_memcpy( p_buffer,
420 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
426 /*****************************************************************************
427 * RTPChoose : read from the network, and decide whether it's UDP or RTP
428 *****************************************************************************/
429 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
436 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
438 /* Get the raw data from the socket.
439 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
440 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
442 if ( !i_ret ) return 0;
444 /* Check that it's not TS. */
445 if ( p_tmp_buffer[0] == 0x47 )
447 msg_Dbg( p_input, "detected TS over raw UDP" );
448 p_input->pf_read = Read;
449 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
453 /* Parse the header and make some verifications.
454 * See RFC 1889 & RFC 2250. */
456 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
457 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
458 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
460 if ( i_rtp_version != 2 )
462 msg_Dbg( p_input, "no RTP header detected" );
463 p_input->pf_read = Read;
464 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
468 switch ( i_payload_type )
471 msg_Dbg( p_input, "detected TS over RTP" );
475 msg_Dbg( p_input, "detected MPEG audio over RTP" );
479 msg_Dbg( p_input, "detected MPEG video over RTP" );
483 msg_Dbg( p_input, "no RTP header detected" );
484 p_input->pf_read = Read;
485 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
489 /* Return the packet without the RTP header. */
490 p_input->pf_read = RTPRead;
491 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
493 if ( (size_t)i_ret > i_len )
495 /* This should NOT happen. */
496 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
500 p_input->p_vlc->pf_memcpy( p_buffer,
501 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,