1 /*****************************************************************************
2 * tcp.c: TCP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: tcp.c,v 1.1 2003/11/30 14:28:07 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@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 *****************************************************************************/
30 #include <vlc/input.h>
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
45 # include <winsock2.h>
46 # include <ws2tcpip.h>
48 # define IN_MULTICAST(a) IN_CLASSD(a)
51 # include <sys/socket.h>
56 /*****************************************************************************
58 *****************************************************************************/
59 #define CACHING_TEXT N_("caching value in ms")
60 #define CACHING_LONGTEXT N_( \
61 "Allows you to modify the default caching value for udp streams. This " \
62 "value should be set in miliseconds units." )
64 static int Open ( vlc_object_t * );
65 static void Close( vlc_object_t * );
68 set_description( _("TCP input") );
69 add_category_hint( N_("tcp"), NULL , VLC_TRUE );
70 add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
71 set_capability( "access", 0 );
72 add_shortcut( "tcp" );
73 add_shortcut( "tcp4" );
74 add_shortcut( "tcp6" );
75 set_callbacks( Open, Close );
79 /*****************************************************************************
81 *****************************************************************************/
87 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
89 /*****************************************************************************
90 * Open: open the socket
91 *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
94 input_thread_t *p_input = (input_thread_t *)p_this;
98 char *psz_dup = strdup(p_input->psz_name);
99 char *psz_parser = psz_dup;
101 network_socket_t sock;
106 /* Select ip version */
108 if( config_GetInt( p_input, "ipv4" ) )
110 psz_network = "ipv4";
112 if( config_GetInt( p_input, "ipv6" ) )
114 psz_network = "ipv6";
116 if( *p_input->psz_access )
118 /* Find out which shortcut was used */
119 if( !strncmp( p_input->psz_access, "tcp6", 5 ) )
121 psz_network = "ipv6";
123 else if( !strncmp( p_input->psz_access, "tcp4", 5 ) )
125 psz_network = "ipv4";
129 /* Parse server:port */
130 while( *psz_parser && *psz_parser != ':' )
132 if( *psz_parser == '[' )
135 while( *psz_parser && *psz_parser != ']' )
143 if( *psz_parser != ':' || psz_parser == psz_dup )
145 msg_Err( p_input, "you have to provide server:port addresse" );
150 *psz_parser++ = '\0';
152 /* Prepare the network_socket_t structure */
153 sock.i_type = NETWORK_TCP;
154 sock.psz_bind_addr = "";
155 sock.i_bind_port = 0;
156 sock.psz_server_addr = psz_dup;
157 sock.i_server_port = atoi( psz_parser );
160 if( sock.i_server_port <= 0 )
162 msg_Err( p_input, "invalid port number (%d)", sock.i_server_port );
168 msg_Dbg( p_input, "opening server=%s:%d",
169 sock.psz_server_addr, sock.i_server_port );
170 p_input->p_private = (void*)&sock;
171 p_network = module_Need( p_input, "network", psz_network );
173 if( p_network == NULL )
177 module_Unneed( p_input, p_network );
179 p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
180 p_sys->fd = sock.i_handle;
182 p_input->pf_read = Read;
183 p_input->pf_set_program = input_SetProgram;
184 p_input->pf_set_area = NULL;
185 p_input->pf_seek = NULL;
187 vlc_mutex_lock( &p_input->stream.stream_lock );
188 p_input->stream.b_pace_control = VLC_TRUE; /* FIXME ? */
189 p_input->stream.b_seekable = 0;
190 p_input->stream.p_selected_area->i_tell = 0;
191 p_input->stream.i_method = INPUT_METHOD_NETWORK;
193 vlc_mutex_unlock( &p_input->stream.stream_lock );
195 /* Update default_pts to a suitable value for udp access */
196 var_Create( p_input, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
197 var_Get( p_input, "tcp-caching", &val );
198 p_input->i_pts_delay = val.i_int * 1000;
203 /*****************************************************************************
204 * Close: free unused data structures
205 *****************************************************************************/
206 static void Close( vlc_object_t *p_this )
208 input_thread_t *p_input = (input_thread_t *)p_this;
209 access_sys_t *p_sys = p_input->p_access_data;
211 msg_Info( p_input, "closing TCP target `%s'", p_input->psz_source );
214 CloseHandle( (HANDLE)p_sys->fd );
215 #elif defined( WIN32 )
216 closesocket( p_sys->fd );
224 /*****************************************************************************
225 * Read: read on a file descriptor, checking b_die periodically
226 *****************************************************************************/
227 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
232 access_sys_t *p_sys = p_input->p_access_data;
233 struct timeval timeout;
240 if( p_input->b_die || p_input->b_error )
245 /* Initialize file descriptor set */
247 FD_SET( p_sys->fd, &fds );
249 /* We'll wait 0.5 second if nothing happens */
251 timeout.tv_usec = 500000;
252 } while( ( i_ret = select( p_sys->fd + 1, &fds, NULL, NULL, &timeout )) == 0 ||
253 ( i_ret < 0 && errno == EINTR ) );
257 msg_Err( p_input, "network select error (%s)", strerror(errno) );
261 if( ( i_recv = recv( p_sys->fd, p_buffer, i_len, 0 ) ) < 0 )
263 msg_Err( p_input, "recv failed (%s)", strerror(errno) );