1 /*****************************************************************************
2 * udp.c: raw UDP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.7 2002/12/16 16:48:04 gbazin Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <sys/types.h>
35 #include <vlc/input.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
50 # include <sys/socket.h>
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close ( vlc_object_t * );
60 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define CACHING_TEXT N_("caching value in ms")
66 #define CACHING_LONGTEXT N_( \
67 "Allows you to modify the default caching value for udp streams. This " \
68 "value should be set in miliseconds units." )
71 set_description( _("raw UDP access module") );
72 add_category_hint( N_("udp"), NULL );
73 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
74 set_capability( "access", 0 );
75 add_shortcut( "udp" );
76 add_shortcut( "udpstream" );
77 add_shortcut( "udp4" );
78 add_shortcut( "udp6" );
79 set_callbacks( Open, Close );
82 /*****************************************************************************
83 * Open: open the socket
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 input_thread_t * p_input = (input_thread_t *)p_this;
88 input_socket_t * p_access_data;
90 char * psz_network = "";
91 char * psz_name = strdup(p_input->psz_name);
92 char * psz_parser = psz_name;
93 char * psz_server_addr = "";
94 char * psz_server_port = "";
95 char * psz_bind_addr = "";
96 char * psz_bind_port = "";
97 int i_bind_port = 0, i_server_port = 0;
98 network_socket_t socket_desc;
100 if( config_GetInt( p_input, "ipv4" ) )
102 psz_network = "ipv4";
104 if( config_GetInt( p_input, "ipv6" ) )
106 psz_network = "ipv6";
109 if( *p_input->psz_access )
111 /* Find out which shortcut was used */
112 if( !strncmp( p_input->psz_access, "udp6", 5 ) )
114 psz_network = "ipv6";
116 else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
118 psz_network = "ipv4";
122 /* Parse psz_name syntax :
123 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
125 if( *psz_parser && *psz_parser != '@' )
128 psz_server_addr = psz_parser;
130 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
132 if( *psz_parser == '[' )
135 while( *psz_parser && *psz_parser != ']' )
143 if( *psz_parser == ':' )
145 /* Found server port */
146 *psz_parser = '\0'; /* Terminate server name */
148 psz_server_port = psz_parser;
150 while( *psz_parser && *psz_parser != '@' )
157 if( *psz_parser == '@' )
159 /* Found bind address or bind port */
160 *psz_parser = '\0'; /* Terminate server port or name if necessary */
163 if( *psz_parser && *psz_parser != ':' )
165 /* Found bind address */
166 psz_bind_addr = psz_parser;
168 while( *psz_parser && *psz_parser != ':' )
170 if( *psz_parser == '[' )
173 while( *psz_parser && *psz_parser != ']' )
182 if( *psz_parser == ':' )
184 /* Found bind port */
185 *psz_parser = '\0'; /* Terminate bind address if necessary */
188 psz_bind_port = psz_parser;
192 /* Convert ports format */
193 if( *psz_server_port )
195 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
198 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
206 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
209 msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
215 if( i_bind_port == 0 )
217 i_bind_port = config_GetInt( p_this, "server-port" );
220 p_input->pf_read = Read;
221 p_input->pf_set_program = input_SetProgram;
222 p_input->pf_set_area = NULL;
223 p_input->pf_seek = NULL;
225 vlc_mutex_lock( &p_input->stream.stream_lock );
226 p_input->stream.b_pace_control = 0;
227 p_input->stream.b_seekable = 0;
228 p_input->stream.p_selected_area->i_tell = 0;
229 p_input->stream.i_method = INPUT_METHOD_NETWORK;
230 vlc_mutex_unlock( &p_input->stream.stream_lock );
232 if( *psz_server_addr || i_server_port )
234 msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
235 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
236 psz_server_addr, i_server_port);
237 msg_Err( p_input, "or local port, type : %s:@%s:%d",
238 *p_input->psz_access ? p_input->psz_access : "udp",
239 psz_server_addr, i_server_port );
242 psz_server_addr = "";
245 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
246 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
248 /* Prepare the network_socket_t structure */
249 socket_desc.i_type = NETWORK_UDP;
250 socket_desc.psz_bind_addr = psz_bind_addr;
251 socket_desc.i_bind_port = i_bind_port;
252 socket_desc.psz_server_addr = psz_server_addr;
253 socket_desc.i_server_port = i_server_port;
255 /* Find an appropriate network module */
256 p_input->p_private = (void*) &socket_desc;
257 p_network = module_Need( p_input, "network", psz_network );
259 if( p_network == NULL )
263 module_Unneed( p_input, p_network );
265 p_access_data = malloc( sizeof(input_socket_t) );
266 p_input->p_access_data = (access_sys_t *)p_access_data;
268 if( p_access_data == NULL )
270 msg_Err( p_input, "out of memory" );
274 p_access_data->i_handle = socket_desc.i_handle;
275 p_input->i_mtu = socket_desc.i_mtu;
277 /* Update default_pts to a suitable value for udp access */
278 p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
283 /*****************************************************************************
284 * Close: free unused data structures
285 *****************************************************************************/
286 static void Close( vlc_object_t *p_this )
288 input_thread_t * p_input = (input_thread_t *)p_this;
289 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
291 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
294 CloseHandle( (HANDLE)p_access_data->i_handle );
295 #elif defined( WIN32 )
296 closesocket( p_access_data->i_handle );
298 close( p_access_data->i_handle );
301 free( p_access_data );
304 /*****************************************************************************
305 * Read: read on a file descriptor, checking b_die periodically
306 *****************************************************************************/
307 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
313 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
314 struct timeval timeout;
318 /* Initialize file descriptor set */
320 FD_SET( p_access_data->i_handle, &fds );
322 /* We'll wait 0.5 second if nothing happens */
324 timeout.tv_usec = 500000;
326 /* Find if some data is available */
327 i_ret = select( p_access_data->i_handle + 1, &fds,
328 NULL, NULL, &timeout );
330 if( i_ret == -1 && errno != EINTR )
332 msg_Err( p_input, "network select error (%s)", strerror(errno) );
336 ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
340 msg_Err( p_input, "recv failed (%s)", strerror(errno) );