]> git.sesse.net Git - vlc/blob - modules/access/tcp.c
* modules/access/*: strings review + coding style fixes.
[vlc] / modules / access / tcp.c
1 /*****************************************************************************
2  * tcp.c: TCP input module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id: tcp.c,v 1.4 2004/01/25 17:31:22 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "network.h"
33
34 /*****************************************************************************
35  * Module descriptor
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 udp streams. This " \
40     "value should be set in miliseconds units." )
41
42 static int  Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44
45 vlc_module_begin();
46     set_description( _("TCP input") );
47
48     add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
49                  CACHING_LONGTEXT, VLC_TRUE );
50
51     set_capability( "access", 0 );
52     add_shortcut( "tcp" );
53     set_callbacks( Open, Close );
54 vlc_module_end();
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 struct access_sys_t
60 {
61     int fd;
62 };
63
64 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
65
66 /*****************************************************************************
67  * Open: open the socket
68  *****************************************************************************/
69 static int Open( vlc_object_t *p_this )
70 {
71     input_thread_t *p_input = (input_thread_t *)p_this;
72     access_sys_t   *p_sys;
73
74     char           *psz_dup = strdup(p_input->psz_name);
75     char           *psz_parser = psz_dup;
76
77     vlc_value_t    val;
78
79     /* Parse server:port */
80     while( *psz_parser && *psz_parser != ':' )
81     {
82         if( *psz_parser == '[' )
83         {
84             /* IPV6 */
85             while( *psz_parser && *psz_parser  != ']' )
86             {
87                 psz_parser++;
88             }
89         }
90         psz_parser++;
91     }
92     if( *psz_parser != ':' || psz_parser == psz_dup )
93     {
94         msg_Err( p_input, "you have to provide server:port addresse" );
95         free( psz_dup );
96         return VLC_EGENERIC;
97     }
98     *psz_parser++ = '\0';
99
100     if( atoi( psz_parser ) <= 0 )
101     {
102         msg_Err( p_input, "invalid port number (%d)", atoi( psz_parser ) );
103         free( psz_dup );
104         return VLC_EGENERIC;
105     }
106
107     /* Connect */
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 ) );
110     free( psz_dup );
111
112     if( p_sys->fd < 0 )
113     {
114         free( p_sys );
115         return VLC_EGENERIC;
116     }
117
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;
122
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;
128     p_input->i_mtu = 0;
129     vlc_mutex_unlock( &p_input->stream.stream_lock );
130
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;
135
136     return VLC_SUCCESS;
137 }
138
139 /*****************************************************************************
140  * Close: free unused data structures
141  *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
143 {
144     input_thread_t *p_input = (input_thread_t *)p_this;
145     access_sys_t   *p_sys = p_input->p_access_data;
146
147     msg_Info( p_input, "closing TCP target `%s'", p_input->psz_source );
148
149     net_Close( p_sys->fd );
150     free( p_sys );
151 }
152
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 )
157 {
158     access_sys_t   *p_sys = p_input->p_access_data;
159
160     return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
161 }