]> git.sesse.net Git - vlc/blob - modules/access/tcp.c
Trailing ;
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35
36 #include <vlc_network.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43     "Caching value for TCP streams. This " \
44     "value should be set in milliseconds." )
45
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 vlc_module_begin ()
50     set_shortname( N_("TCP") )
51     set_description( N_("TCP input") )
52     set_category( CAT_INPUT )
53     set_subcategory( SUBCAT_INPUT_ACCESS )
54
55     add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
56                  CACHING_LONGTEXT, true )
57
58     set_capability( "access", 0 )
59     add_shortcut( "tcp" )
60     set_callbacks( Open, Close )
61 vlc_module_end ()
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 struct access_sys_t
67 {
68     int        fd;
69 };
70
71
72 static ssize_t Read( access_t *, uint8_t *, size_t );
73 static int Control( access_t *, int, va_list );
74
75 /*****************************************************************************
76  * Open: open the socket
77  *****************************************************************************/
78 static int Open( vlc_object_t *p_this )
79 {
80     access_t     *p_access = (access_t *)p_this;
81     access_sys_t *p_sys;
82
83     char         *psz_dup = strdup(p_access->psz_path);
84     char         *psz_parser = psz_dup;
85
86     /* Parse server:port */
87     if( *psz_parser == '[' )
88     {
89         psz_parser = strchr( psz_parser, ']' );
90         if( psz_parser == NULL )
91             psz_parser = psz_dup;
92     }
93     psz_parser = strchr( psz_parser, ':' );
94
95     if( psz_parser == NULL )
96     {
97         msg_Err( p_access, "missing port number : %s", psz_dup );
98         free( psz_dup );
99         return VLC_EGENERIC;
100     }
101
102     *psz_parser++ = '\0';
103
104     /* Init p_access */
105     access_InitFields( p_access );
106     ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL );
107     p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ) );
108     if( !p_sys )
109     {
110         free( psz_dup );
111         return VLC_ENOMEM;
112     }
113
114     p_sys->fd = net_ConnectTCP( p_access, psz_dup, atoi( psz_parser ) );
115     free( psz_dup );
116
117     if( p_sys->fd < 0 )
118     {
119         free( p_sys );
120         return VLC_EGENERIC;
121     }
122
123     /* Update default_pts to a suitable value for udp access */
124     var_Create( p_access, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
125
126     return VLC_SUCCESS;
127 }
128
129 /*****************************************************************************
130  * Close: free unused data structures
131  *****************************************************************************/
132 static void Close( vlc_object_t *p_this )
133 {
134     access_t     *p_access = (access_t *)p_this;
135     access_sys_t *p_sys = p_access->p_sys;
136
137     net_Close( p_sys->fd );
138     free( p_sys );
139 }
140
141 /*****************************************************************************
142  * Read: read on a file descriptor
143  *****************************************************************************/
144 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
145 {
146     access_sys_t *p_sys = p_access->p_sys;
147     int i_read;
148
149     if( p_access->info.b_eof )
150         return 0;
151
152     i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len,
153                        false );
154     if( i_read == 0 )
155         p_access->info.b_eof = true;
156     else if( i_read > 0 )
157         p_access->info.i_pos += i_read;
158
159     return i_read;
160 }
161
162 /*****************************************************************************
163  * Control:
164  *****************************************************************************/
165 static int Control( access_t *p_access, int i_query, va_list args )
166 {
167     bool    *pb_bool;
168     int64_t *pi_64;
169
170     switch( i_query )
171     {
172         /* */
173         case ACCESS_CAN_SEEK:
174         case ACCESS_CAN_FASTSEEK:
175             pb_bool = (bool*)va_arg( args, bool* );
176             *pb_bool = false;
177             break;
178         case ACCESS_CAN_PAUSE:
179             pb_bool = (bool*)va_arg( args, bool* );
180             *pb_bool = true;    /* FIXME */
181             break;
182         case ACCESS_CAN_CONTROL_PACE:
183             pb_bool = (bool*)va_arg( args, bool* );
184             *pb_bool = true;    /* FIXME */
185             break;
186
187         case ACCESS_GET_PTS_DELAY:
188             pi_64 = (int64_t*)va_arg( args, int64_t * );
189             *pi_64 = (int64_t)var_GetInteger( p_access, "tcp-caching" ) * INT64_C(1000);
190             break;
191
192         /* */
193         case ACCESS_SET_PAUSE_STATE:
194             /* Nothing to do */
195             break;
196
197         case ACCESS_GET_TITLE_INFO:
198         case ACCESS_SET_TITLE:
199         case ACCESS_SET_SEEKPOINT:
200         case ACCESS_SET_PRIVATE_ID_STATE:
201         case ACCESS_GET_CONTENT_TYPE:
202             return VLC_EGENERIC;
203
204         default:
205             msg_Warn( p_access, "unimplemented query in control" );
206             return VLC_EGENERIC;
207
208     }
209     return VLC_SUCCESS;
210 }