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