]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Remove server-port and mark it obsolete
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * Copyright (C) 2007 Remi Denis-Courmont
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Tristan Leteurtre <tooney@via.ecp.fr>
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12  *          Remi Denis-Courmont
13  *
14  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_access.h>
42 #include <vlc_network.h>
43
44 #define MTU 65535
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 #define CACHING_TEXT N_("Caching value in ms")
50 #define CACHING_LONGTEXT N_( \
51     "Caching value for UDP streams. This " \
52     "value should be set in milliseconds." )
53
54 static int  Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
56
57 vlc_module_begin ()
58     set_shortname( N_("UDP" ) )
59     set_description( N_("UDP input") )
60     set_category( CAT_INPUT )
61     set_subcategory( SUBCAT_INPUT_ACCESS )
62
63     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, CACHING_TEXT,
64                  CACHING_LONGTEXT, true )
65         change_safe()
66     add_obsolete_integer( "server-port" ) /* since 1.2.0 */
67
68     set_capability( "access", 0 )
69     add_shortcut( "udp", "udpstream", "udp4", "udp6" )
70
71     set_callbacks( Open, Close )
72 vlc_module_end ()
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 static block_t *BlockUDP( access_t * );
78 static int Control( access_t *, int, va_list );
79
80 /*****************************************************************************
81  * Open: open the socket
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     access_t     *p_access = (access_t*)p_this;
86
87     char *psz_name = strdup( p_access->psz_location );
88     char *psz_parser;
89     const char *psz_server_addr, *psz_bind_addr = "";
90     int  i_bind_port = 1234, i_server_port = 0;
91     int fam = AF_UNSPEC;
92     int fd;
93
94     /* Set up p_access */
95     access_InitFields( p_access );
96     ACCESS_SET_CALLBACKS( NULL, BlockUDP, Control, NULL );
97
98     if (strlen (p_access->psz_access) > 0)
99     {
100         switch (p_access->psz_access[strlen (p_access->psz_access) - 1])
101         {
102             case '4':
103                 fam = AF_INET;
104                 break;
105
106             case '6':
107                 fam = AF_INET6;
108                 break;
109         }
110     }
111
112     /* Parse psz_name syntax :
113      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
114     psz_parser = strchr( psz_name, '@' );
115     if( psz_parser != NULL )
116     {
117         /* Found bind address and/or bind port */
118         *psz_parser++ = '\0';
119         psz_bind_addr = psz_parser;
120
121         if( psz_bind_addr[0] == '[' )
122             /* skips bracket'd IPv6 address */
123             psz_parser = strchr( psz_parser, ']' );
124
125         if( psz_parser != NULL )
126         {
127             psz_parser = strchr( psz_parser, ':' );
128             if( psz_parser != NULL )
129             {
130                 *psz_parser++ = '\0';
131                 i_bind_port = atoi( psz_parser );
132             }
133         }
134     }
135
136     psz_server_addr = psz_name;
137     psz_parser = ( psz_server_addr[0] == '[' )
138         ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
139         : psz_name;
140
141     if( psz_parser != NULL )
142     {
143         psz_parser = strchr( psz_parser, ':' );
144         if( psz_parser != NULL )
145         {
146             *psz_parser++ = '\0';
147             i_server_port = atoi( psz_parser );
148         }
149     }
150
151     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
152              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
153
154     fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
155                         psz_server_addr, i_server_port, fam, IPPROTO_UDP );
156     free (psz_name);
157     if( fd == -1 )
158     {
159         msg_Err( p_access, "cannot open socket" );
160         return VLC_EGENERIC;
161     }
162     p_access->p_sys = (void *)(intptr_t)fd;
163
164     /* Update default_pts to a suitable value for udp access */
165     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * Close: free unused data structures
171  *****************************************************************************/
172 static void Close( vlc_object_t *p_this )
173 {
174     access_t     *p_access = (access_t*)p_this;
175
176     net_Close( (intptr_t)p_access->p_sys );
177 }
178
179 /*****************************************************************************
180  * Control:
181  *****************************************************************************/
182 static int Control( access_t *p_access, int i_query, va_list args )
183 {
184     bool    *pb_bool;
185     int64_t *pi_64;
186
187     switch( i_query )
188     {
189         /* */
190         case ACCESS_CAN_SEEK:
191         case ACCESS_CAN_FASTSEEK:
192         case ACCESS_CAN_PAUSE:
193         case ACCESS_CAN_CONTROL_PACE:
194             pb_bool = (bool*)va_arg( args, bool* );
195             *pb_bool = false;
196             break;
197         /* */
198         case ACCESS_GET_PTS_DELAY:
199             pi_64 = (int64_t*)va_arg( args, int64_t * );
200             *pi_64 = var_GetInteger(p_access,"udp-caching") * 1000;
201             break;
202
203         /* */
204         case ACCESS_SET_PAUSE_STATE:
205         case ACCESS_GET_TITLE_INFO:
206         case ACCESS_SET_TITLE:
207         case ACCESS_SET_SEEKPOINT:
208         case ACCESS_SET_PRIVATE_ID_STATE:
209         case ACCESS_GET_CONTENT_TYPE:
210             return VLC_EGENERIC;
211
212         default:
213             msg_Warn( p_access, "unimplemented query in control" );
214             return VLC_EGENERIC;
215
216     }
217     return VLC_SUCCESS;
218 }
219
220 /*****************************************************************************
221  * BlockUDP:
222  *****************************************************************************/
223 static block_t *BlockUDP( access_t *p_access )
224 {
225     access_sys_t *p_sys = p_access->p_sys;
226     block_t      *p_block;
227     ssize_t len;
228
229     if( p_access->info.b_eof )
230         return NULL;
231
232     /* Read data */
233     p_block = block_New( p_access, MTU );
234     len = net_Read( p_access, (intptr_t)p_sys, NULL,
235                     p_block->p_buffer, MTU, false );
236     if( len < 0 )
237     {
238         block_Release( p_block );
239         return NULL;
240     }
241
242     return block_Realloc( p_block, 0, len );
243 }