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