]> git.sesse.net Git - vlc/blob - modules/access/tcp.c
362d824bf14ca0298d0465c7d9a9ae8d08bc3dad
[vlc] / modules / access / tcp.c
1 /*****************************************************************************
2  * tcp.c: TCP access plug-in
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: tcp.c,v 1.3 2004/01/05 15:07:16 fenrir Exp $
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 udp streams. This " \
40     "value should be set in miliseconds 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     add_category_hint( N_("TCP"), NULL , VLC_TRUE );
48     add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
49     set_capability( "access", 0 );
50     add_shortcut( "tcp" );
51     set_callbacks( Open, Close );
52 vlc_module_end();
53
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 struct access_sys_t
59 {
60     int fd;
61 };
62
63 static ssize_t  Read ( input_thread_t *, byte_t *, size_t );
64
65 /*****************************************************************************
66  * Open: open the socket
67  *****************************************************************************/
68 static int Open( vlc_object_t *p_this )
69 {
70     input_thread_t *p_input = (input_thread_t *)p_this;
71     access_sys_t   *p_sys;
72
73     char           *psz_dup = strdup(p_input->psz_name);
74     char           *psz_parser = psz_dup;
75
76     vlc_value_t    val;
77
78     /* Parse server:port */
79     while( *psz_parser && *psz_parser != ':' )
80     {
81         if( *psz_parser == '[' )
82         {
83             /* IPV6 */
84             while( *psz_parser && *psz_parser  != ']' )
85             {
86                 psz_parser++;
87             }
88         }
89         psz_parser++;
90     }
91     if( *psz_parser != ':' || psz_parser == psz_dup )
92     {
93         msg_Err( p_input, "you have to provide server:port addresse" );
94         free( psz_dup );
95         return VLC_EGENERIC;
96     }
97     *psz_parser++ = '\0';
98
99     if( atoi( psz_parser ) <= 0 )
100     {
101         msg_Err( p_input, "invalid port number (%d)", atoi( psz_parser ) );
102         free( psz_dup );
103         return VLC_EGENERIC;
104     }
105
106     /* Connect */
107     p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
108     p_sys->fd = net_OpenTCP( p_input, psz_dup, atoi( psz_parser ) );
109     free( psz_dup );
110
111     if( p_sys->fd < 0 )
112     {
113         free( p_sys );
114         return VLC_EGENERIC;
115     }
116
117     p_input->pf_read = Read;
118     p_input->pf_set_program = input_SetProgram;
119     p_input->pf_set_area = NULL;
120     p_input->pf_seek = NULL;
121
122     vlc_mutex_lock( &p_input->stream.stream_lock );
123     p_input->stream.b_pace_control = VLC_TRUE;  /* FIXME ? */
124     p_input->stream.b_seekable = 0;
125     p_input->stream.p_selected_area->i_tell = 0;
126     p_input->stream.i_method = INPUT_METHOD_NETWORK;
127     p_input->i_mtu = 0;
128     vlc_mutex_unlock( &p_input->stream.stream_lock );
129
130     /* Update default_pts to a suitable value for udp access */
131     var_Create( p_input, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
132     var_Get( p_input, "tcp-caching", &val );
133     p_input->i_pts_delay = val.i_int * 1000;
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     input_thread_t *p_input = (input_thread_t *)p_this;
144     access_sys_t   *p_sys = p_input->p_access_data;
145
146     msg_Info( p_input, "closing TCP target `%s'", p_input->psz_source );
147
148     net_Close( p_sys->fd );
149     free( p_sys );
150 }
151
152 /*****************************************************************************
153  * Read: read on a file descriptor, checking b_die periodically
154  *****************************************************************************/
155 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
156 {
157     access_sys_t   *p_sys = p_input->p_access_data;
158
159     return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
160 }
161