Remove legacy casts
[vlc/asuraparaju-public.git] / modules / access / udp.c
blob5cc485f9075664218de93fdbea4f446b43b2770a
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$
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
14 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
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.
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.
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 *****************************************************************************/
31 /*****************************************************************************
32 * Preamble
33 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_access.h>
42 #include <vlc_network.h>
44 #define MTU 65535
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." )
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
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 )
63 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
64 CACHING_LONGTEXT, true )
65 change_safe()
66 add_obsolete_integer( "rtp-late" )
67 add_obsolete_bool( "udp-auto-mtu" )
69 set_capability( "access", 0 )
70 add_shortcut( "udp" )
71 add_shortcut( "udpstream" )
72 add_shortcut( "udp4" )
73 add_shortcut( "udp6" )
75 set_callbacks( Open, Close )
76 vlc_module_end ()
78 /*****************************************************************************
79 * Local prototypes
80 *****************************************************************************/
81 #define RTP_HEADER_LEN 12
83 static block_t *BlockUDP( access_t * );
84 static int Control( access_t *, int, va_list );
86 /*****************************************************************************
87 * Open: open the socket
88 *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
91 access_t *p_access = (access_t*)p_this;
93 char *psz_name = strdup( p_access->psz_location );
94 char *psz_parser;
95 const char *psz_server_addr, *psz_bind_addr = "";
96 int i_bind_port, i_server_port = 0;
97 int fam = AF_UNSPEC;
98 int fd;
100 /* Set up p_access */
101 access_InitFields( p_access );
102 ACCESS_SET_CALLBACKS( NULL, BlockUDP, Control, NULL );
104 if (strlen (p_access->psz_access) > 0)
106 switch (p_access->psz_access[strlen (p_access->psz_access) - 1])
108 case '4':
109 fam = AF_INET;
110 break;
112 case '6':
113 fam = AF_INET6;
114 break;
118 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
120 /* Parse psz_name syntax :
121 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
122 psz_parser = strchr( psz_name, '@' );
123 if( psz_parser != NULL )
125 /* Found bind address and/or bind port */
126 *psz_parser++ = '\0';
127 psz_bind_addr = psz_parser;
129 if( psz_bind_addr[0] == '[' )
130 /* skips bracket'd IPv6 address */
131 psz_parser = strchr( psz_parser, ']' );
133 if( psz_parser != NULL )
135 psz_parser = strchr( psz_parser, ':' );
136 if( psz_parser != NULL )
138 *psz_parser++ = '\0';
139 i_bind_port = atoi( psz_parser );
144 psz_server_addr = psz_name;
145 psz_parser = ( psz_server_addr[0] == '[' )
146 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
147 : psz_name;
149 if( psz_parser != NULL )
151 psz_parser = strchr( psz_parser, ':' );
152 if( psz_parser != NULL )
154 *psz_parser++ = '\0';
155 i_server_port = atoi( psz_parser );
159 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
160 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
162 fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
163 psz_server_addr, i_server_port, fam, IPPROTO_UDP );
164 free (psz_name);
165 if( fd == -1 )
167 msg_Err( p_access, "cannot open socket" );
168 return VLC_EGENERIC;
170 p_access->p_sys = (void *)(intptr_t)fd;
172 /* Update default_pts to a suitable value for udp access */
173 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
174 return VLC_SUCCESS;
177 /*****************************************************************************
178 * Close: free unused data structures
179 *****************************************************************************/
180 static void Close( vlc_object_t *p_this )
182 access_t *p_access = (access_t*)p_this;
184 net_Close( (intptr_t)p_access->p_sys );
187 /*****************************************************************************
188 * Control:
189 *****************************************************************************/
190 static int Control( access_t *p_access, int i_query, va_list args )
192 bool *pb_bool;
193 int64_t *pi_64;
195 switch( i_query )
197 /* */
198 case ACCESS_CAN_SEEK:
199 case ACCESS_CAN_FASTSEEK:
200 case ACCESS_CAN_PAUSE:
201 case ACCESS_CAN_CONTROL_PACE:
202 pb_bool = (bool*)va_arg( args, bool* );
203 *pb_bool = false;
204 break;
205 /* */
206 case ACCESS_GET_PTS_DELAY:
207 pi_64 = (int64_t*)va_arg( args, int64_t * );
208 *pi_64 = var_GetInteger(p_access,"udp-caching") * 1000;
209 break;
211 /* */
212 case ACCESS_SET_PAUSE_STATE:
213 case ACCESS_GET_TITLE_INFO:
214 case ACCESS_SET_TITLE:
215 case ACCESS_SET_SEEKPOINT:
216 case ACCESS_SET_PRIVATE_ID_STATE:
217 case ACCESS_GET_CONTENT_TYPE:
218 return VLC_EGENERIC;
220 default:
221 msg_Warn( p_access, "unimplemented query in control" );
222 return VLC_EGENERIC;
225 return VLC_SUCCESS;
228 /*****************************************************************************
229 * BlockUDP:
230 *****************************************************************************/
231 static block_t *BlockUDP( access_t *p_access )
233 access_sys_t *p_sys = p_access->p_sys;
234 block_t *p_block;
235 ssize_t len;
237 if( p_access->info.b_eof )
238 return NULL;
240 /* Read data */
241 p_block = block_New( p_access, MTU );
242 len = net_Read( p_access, (intptr_t)p_sys, NULL,
243 p_block->p_buffer, MTU, false );
244 if( len < 0 )
246 block_Release( p_block );
247 return NULL;
250 return block_Realloc( p_block, 0, len );