1 /*****************************************************************************
2 * tcp.c: TCP input module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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>
34 /*****************************************************************************
36 *****************************************************************************/
37 #define CACHING_TEXT N_("Caching value in ms")
38 #define CACHING_LONGTEXT N_( \
39 "Allows you to modify the default caching value for TCP streams. This " \
40 "value should be set in millisecond units." )
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
46 set_description( _("TCP input") );
48 add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
49 CACHING_LONGTEXT, VLC_TRUE );
51 set_capability( "access", 0 );
52 add_shortcut( "tcp" );
53 set_callbacks( Open, Close );
56 /*****************************************************************************
58 *****************************************************************************/
64 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
66 /*****************************************************************************
67 * Open: open the socket
68 *****************************************************************************/
69 static int Open( vlc_object_t *p_this )
71 input_thread_t *p_input = (input_thread_t *)p_this;
74 char *psz_dup = strdup(p_input->psz_name);
75 char *psz_parser = psz_dup;
79 /* Parse server:port */
80 while( *psz_parser && *psz_parser != ':' )
82 if( *psz_parser == '[' )
85 while( *psz_parser && *psz_parser != ']' )
92 if( *psz_parser != ':' || psz_parser == psz_dup )
94 msg_Err( p_input, "you have to provide server:port addresse" );
100 if( atoi( psz_parser ) <= 0 )
102 msg_Err( p_input, "invalid port number (%d)", atoi( psz_parser ) );
108 p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
109 p_sys->fd = net_OpenTCP( p_input, psz_dup, atoi( psz_parser ) );
118 p_input->pf_read = Read;
119 p_input->pf_set_program = input_SetProgram;
120 p_input->pf_set_area = NULL;
121 p_input->pf_seek = NULL;
123 vlc_mutex_lock( &p_input->stream.stream_lock );
124 p_input->stream.b_pace_control = VLC_TRUE; /* FIXME ? */
125 p_input->stream.b_seekable = 0;
126 p_input->stream.p_selected_area->i_tell = 0;
127 p_input->stream.i_method = INPUT_METHOD_NETWORK;
129 vlc_mutex_unlock( &p_input->stream.stream_lock );
131 /* Update default_pts to a suitable value for udp access */
132 var_Create( p_input, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
133 var_Get( p_input, "tcp-caching", &val );
134 p_input->i_pts_delay = val.i_int * 1000;
139 /*****************************************************************************
140 * Close: free unused data structures
141 *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
144 input_thread_t *p_input = (input_thread_t *)p_this;
145 access_sys_t *p_sys = p_input->p_access_data;
147 msg_Info( p_input, "closing TCP target `%s'", p_input->psz_source );
149 net_Close( p_sys->fd );
153 /*****************************************************************************
154 * Read: read on a file descriptor, checking b_die periodically
155 *****************************************************************************/
156 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
158 access_sys_t *p_sys = p_input->p_access_data;
160 return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );