]> git.sesse.net Git - vlc/blob - modules/access/tcp.c
direct3d11: implement the pixel format fallback
[vlc] / modules / access / tcp.c
1 /*****************************************************************************
2  * tcp.c: TCP input module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VLC authors and 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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 vlc_module_begin ()
45     set_shortname( N_("TCP") )
46     set_description( N_("TCP input") )
47     set_category( CAT_INPUT )
48     set_subcategory( SUBCAT_INPUT_ACCESS )
49
50     set_capability( "access", 0 )
51     add_shortcut( "tcp" )
52     set_callbacks( Open, Close )
53 vlc_module_end ()
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 struct access_sys_t
59 {
60     int        fd;
61 };
62
63
64 static ssize_t Read( access_t *, uint8_t *, size_t );
65 static int Control( access_t *, int, va_list );
66
67 /*****************************************************************************
68  * Open: open the socket
69  *****************************************************************************/
70 static int Open( vlc_object_t *p_this )
71 {
72     access_t     *p_access = (access_t *)p_this;
73     access_sys_t *p_sys;
74
75     char         *psz_dup = strdup(p_access->psz_location);
76     char         *psz_parser = psz_dup;
77
78     /* Parse server:port */
79     if( *psz_parser == '[' )
80     {
81         psz_parser = strchr( psz_parser, ']' );
82         if( psz_parser == NULL )
83             psz_parser = psz_dup;
84     }
85     psz_parser = strchr( psz_parser, ':' );
86
87     if( psz_parser == NULL )
88     {
89         msg_Err( p_access, "missing port number : %s", psz_dup );
90         free( psz_dup );
91         return VLC_EGENERIC;
92     }
93
94     *psz_parser++ = '\0';
95
96     /* Init p_access */
97     access_InitFields( p_access );
98     ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL );
99     p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ) );
100     if( !p_sys )
101     {
102         free( psz_dup );
103         return VLC_ENOMEM;
104     }
105
106     p_sys->fd = net_ConnectTCP( p_access, psz_dup, atoi( psz_parser ) );
107     free( psz_dup );
108
109     if( p_sys->fd < 0 )
110     {
111         free( p_sys );
112         return VLC_EGENERIC;
113     }
114
115     return VLC_SUCCESS;
116 }
117
118 /*****************************************************************************
119  * Close: free unused data structures
120  *****************************************************************************/
121 static void Close( vlc_object_t *p_this )
122 {
123     access_t     *p_access = (access_t *)p_this;
124     access_sys_t *p_sys = p_access->p_sys;
125
126     net_Close( p_sys->fd );
127     free( p_sys );
128 }
129
130 /*****************************************************************************
131  * Read: read on a file descriptor
132  *****************************************************************************/
133 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
134 {
135     access_sys_t *p_sys = p_access->p_sys;
136     int i_read;
137
138     if( p_access->info.b_eof )
139         return 0;
140
141     i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len,
142                        false );
143     if( i_read == 0 )
144         p_access->info.b_eof = true;
145     else if( i_read > 0 )
146         p_access->info.i_pos += i_read;
147
148     return i_read;
149 }
150
151 /*****************************************************************************
152  * Control:
153  *****************************************************************************/
154 static int Control( access_t *p_access, int i_query, va_list args )
155 {
156     bool    *pb_bool;
157     int64_t *pi_64;
158
159     switch( i_query )
160     {
161         case ACCESS_CAN_SEEK:
162         case ACCESS_CAN_FASTSEEK:
163             pb_bool = (bool*)va_arg( args, bool* );
164             *pb_bool = false;
165             break;
166         case ACCESS_CAN_PAUSE:
167             pb_bool = (bool*)va_arg( args, bool* );
168             *pb_bool = true;    /* FIXME */
169             break;
170         case ACCESS_CAN_CONTROL_PACE:
171             pb_bool = (bool*)va_arg( args, bool* );
172             *pb_bool = true;    /* FIXME */
173             break;
174
175         case ACCESS_GET_PTS_DELAY:
176             pi_64 = (int64_t*)va_arg( args, int64_t * );
177             *pi_64 = INT64_C(1000)
178                    * var_InheritInteger( p_access, "network-caching" );
179             break;
180
181         case ACCESS_SET_PAUSE_STATE:
182             /* Nothing to do */
183             break;
184
185         default:
186             return VLC_EGENERIC;
187     }
188     return VLC_SUCCESS;
189 }