]> git.sesse.net Git - vlc/blob - modules/access/tcp.c
* tcp: convertd to access2.
[vlc] / modules / access / tcp.c
1 /*****************************************************************************
2  * tcp.c: TCP input module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
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 TCP streams. This " \
40     "value should be set in millisecond 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( "access2", 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     int64_t    i_tell;
63     vlc_bool_t b_eof;
64 };
65
66
67 static int Read( access_t *, uint8_t *, int );
68 static int Control( access_t *, int, va_list );
69
70 /*****************************************************************************
71  * Open: open the socket
72  *****************************************************************************/
73 static int Open( vlc_object_t *p_this )
74 {
75     access_t     *p_access = (access_t *)p_this;
76     access_sys_t *p_sys;
77
78     char         *psz_dup = strdup(p_access->psz_path);
79     char         *psz_parser = psz_dup;
80
81     /* Parse server:port */
82     while( *psz_parser && *psz_parser != ':' )
83     {
84         if( *psz_parser == '[' )
85         {
86             /* IPV6 */
87             while( *psz_parser && *psz_parser  != ']' )
88             {
89                 psz_parser++;
90             }
91         }
92         psz_parser++;
93     }
94     if( *psz_parser != ':' || psz_parser == psz_dup )
95     {
96         msg_Err( p_access, "you have to provide server:port addresse" );
97         free( psz_dup );
98         return VLC_EGENERIC;
99     }
100     *psz_parser++ = '\0';
101
102     if( atoi( psz_parser ) <= 0 )
103     {
104         msg_Err( p_access, "invalid port number (%d)", atoi( psz_parser ) );
105         free( psz_dup );
106         return VLC_EGENERIC;
107     }
108
109     /* Connect */
110     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
111     p_sys->fd = net_OpenTCP( p_access, psz_dup, atoi( psz_parser ) );
112     p_sys->i_tell = 0;
113     p_sys->b_eof = VLC_FALSE;
114     free( psz_dup );
115
116     if( p_sys->fd < 0 )
117     {
118         free( p_sys );
119         return VLC_EGENERIC;
120     }
121
122     p_access->pf_read    = Read;
123     p_access->pf_block   = NULL;
124     p_access->pf_seek    = NULL;
125     p_access->pf_control = Control;
126
127     /* Update default_pts to a suitable value for udp access */
128     var_Create( p_access, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
129
130     return VLC_SUCCESS;
131 }
132
133 /*****************************************************************************
134  * Close: free unused data structures
135  *****************************************************************************/
136 static void Close( vlc_object_t *p_this )
137 {
138     access_t     *p_access = (access_t *)p_this;
139     access_sys_t *p_sys = p_access->p_sys;
140
141     net_Close( p_sys->fd );
142     free( p_sys );
143 }
144
145 /*****************************************************************************
146  * Read: read on a file descriptor, checking b_die periodically
147  *****************************************************************************/
148 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
149 {
150     access_sys_t *p_sys = p_access->p_sys;
151     int i_read;
152
153     if( p_sys->b_eof )
154         return 0;
155
156     i_read = net_Read( p_access, p_sys->fd, p_buffer, i_len, VLC_FALSE );
157     if( i_read == 0 )
158         p_sys->b_eof = VLC_TRUE;
159     else if( i_read > 0 )
160         p_sys->i_tell += i_read;
161
162     return i_read;
163 }
164
165 /*****************************************************************************
166  * Control:
167  *****************************************************************************/
168 static int Control( access_t *p_access, int i_query, va_list args )
169 {
170     access_sys_t *p_sys = p_access->p_sys;
171     vlc_bool_t   *pb_bool;
172     int          *pi_int;
173     int64_t      *pi_64;
174     vlc_value_t  val;
175
176     switch( i_query )
177     {
178         /* */
179         case ACCESS_CAN_SEEK:
180         case ACCESS_CAN_FASTSEEK:
181             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
182             *pb_bool = VLC_FALSE;
183             break;
184         case ACCESS_CAN_PAUSE:
185             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
186             *pb_bool = VLC_TRUE;    /* FIXME */
187             break;
188         case ACCESS_CAN_CONTROL_PACE:
189             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
190             *pb_bool = VLC_TRUE;    /* FIXME */
191             break;
192
193         /* */
194         case ACCESS_GET_MTU:
195             pi_int = (int*)va_arg( args, int * );
196             *pi_int = 0;
197             break;
198         case ACCESS_GET_SIZE:
199             pi_64 = (int64_t*)va_arg( args, int64_t * ); 
200             *pi_64 = 0;
201             break;
202         case ACCESS_GET_POS:
203             pi_64 = (int64_t*)va_arg( args, int64_t * );
204             *pi_64 = p_sys->i_tell;
205             break;
206         case ACCESS_GET_EOF:
207             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
208             *pb_bool = p_sys->b_eof;
209             break;
210
211         case ACCESS_GET_PTS_DELAY:
212             pi_64 = (int64_t*)va_arg( args, int64_t * );
213             var_Get( p_access, "tcp-caching", &val );
214             *pi_64 = val.i_int * 1000;
215             break;
216
217         /* */
218         case ACCESS_SET_PAUSE_STATE:
219             /* Nothing to do */
220             break;
221
222         default:
223             msg_Err( p_access, "unimplemented query in control" );
224             return VLC_EGENERIC;
225
226     }
227     return VLC_SUCCESS;
228 }