1 /*****************************************************************************
2 * udp.c: raw UDP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 VLC authors and VideoLAN
5 * Copyright (C) 2007 Remi Denis-Courmont
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>
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 it
17 * under the terms of the GNU Lesser General Public License as published by
18 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_access.h>
43 #include <vlc_network.h>
44 #include <vlc_block.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open( vlc_object_t * );
52 static void Close( vlc_object_t * );
54 #define BUFFER_TEXT N_("Receive buffer")
55 #define BUFFER_LONGTEXT N_("UDP receive buffer size (bytes)" )
58 set_shortname( N_("UDP" ) )
59 set_description( N_("UDP input") )
60 set_category( CAT_INPUT )
61 set_subcategory( SUBCAT_INPUT_ACCESS )
63 add_obsolete_integer( "server-port" ) /* since 2.0.0 */
64 add_integer( "udp-buffer", 0x400000, BUFFER_TEXT, BUFFER_LONGTEXT, true )
66 set_capability( "access", 0 )
67 add_shortcut( "udp", "udpstream", "udp4", "udp6" )
69 set_callbacks( Open, Close )
80 /*****************************************************************************
82 *****************************************************************************/
83 static block_t *BlockUDP( access_t * );
84 static int Control( access_t *, int, va_list );
85 static void* ThreadRead( void *data );
87 /*****************************************************************************
88 * Open: open the socket
89 *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
92 access_t *p_access = (access_t*)p_this;
93 access_sys_t *sys = malloc( sizeof( *sys ) );
94 if( unlikely( sys == NULL ) )
97 p_access->p_sys = sys;
100 access_InitFields( p_access );
101 ACCESS_SET_CALLBACKS( NULL, BlockUDP, Control, NULL );
103 char *psz_name = strdup( p_access->psz_location );
105 const char *psz_server_addr, *psz_bind_addr = "";
106 int i_bind_port = 1234, i_server_port = 0;
108 if( unlikely(psz_name == NULL) )
111 /* Parse psz_name syntax :
112 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
113 psz_parser = strchr( psz_name, '@' );
114 if( psz_parser != NULL )
116 /* Found bind address and/or bind port */
117 *psz_parser++ = '\0';
118 psz_bind_addr = psz_parser;
120 if( psz_bind_addr[0] == '[' )
121 /* skips bracket'd IPv6 address */
122 psz_parser = strchr( psz_parser, ']' );
124 if( psz_parser != NULL )
126 psz_parser = strchr( psz_parser, ':' );
127 if( psz_parser != NULL )
129 *psz_parser++ = '\0';
130 i_bind_port = atoi( psz_parser );
135 psz_server_addr = psz_name;
136 psz_parser = ( psz_server_addr[0] == '[' )
137 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
140 if( psz_parser != NULL )
142 psz_parser = strchr( psz_parser, ':' );
143 if( psz_parser != NULL )
145 *psz_parser++ = '\0';
146 i_server_port = atoi( psz_parser );
150 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
151 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
153 sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
154 psz_server_addr, i_server_port, IPPROTO_UDP );
158 msg_Err( p_access, "cannot open socket" );
162 sys->fifo = block_FifoNew();
163 if( unlikely( sys->fifo == NULL ) )
165 net_Close( sys->fd );
169 sys->fifo_size = var_InheritInteger( p_access, "udp-buffer");
171 if( vlc_clone( &sys->thread, ThreadRead, p_access,
172 VLC_THREAD_PRIORITY_INPUT ) )
174 block_FifoRelease( sys->fifo );
175 net_Close( sys->fd );
184 /*****************************************************************************
185 * Close: free unused data structures
186 *****************************************************************************/
187 static void Close( vlc_object_t *p_this )
189 access_t *p_access = (access_t*)p_this;
190 access_sys_t *sys = p_access->p_sys;
192 vlc_cancel( sys->thread );
193 vlc_join( sys->thread, NULL );
194 block_FifoRelease( sys->fifo );
195 net_Close( sys->fd );
199 /*****************************************************************************
201 *****************************************************************************/
202 static int Control( access_t *p_access, int i_query, va_list args )
209 case ACCESS_CAN_SEEK:
210 case ACCESS_CAN_FASTSEEK:
211 case ACCESS_CAN_PAUSE:
212 case ACCESS_CAN_CONTROL_PACE:
213 pb_bool = (bool*)va_arg( args, bool* );
217 case ACCESS_GET_PTS_DELAY:
218 pi_64 = (int64_t*)va_arg( args, int64_t * );
219 *pi_64 = INT64_C(1000)
220 * var_InheritInteger(p_access, "network-caching");
229 /*****************************************************************************
231 *****************************************************************************/
232 static block_t *BlockUDP( access_t *p_access )
234 access_sys_t *sys = p_access->p_sys;
237 if( p_access->info.b_eof )
240 block = block_FifoGet( sys->fifo );
241 p_access->info.b_eof = block == NULL;
245 /*****************************************************************************
246 * ThreadRead: Pull packets from socket as soon as possible.
247 *****************************************************************************/
248 static void* ThreadRead( void *data )
250 access_t *access = data;
251 access_sys_t *sys = access->p_sys;
255 block_t *pkt = block_Alloc(MTU);
256 if (unlikely(pkt == NULL))
261 block_cleanup_push(pkt);
263 len = net_Read(access, sys->fd, NULL, pkt->p_buffer, MTU, false);
264 while (len == -1 && errno != EINTR);
274 block_FifoPace(sys->fifo, SIZE_MAX, sys->fifo_size - len);
275 block_FifoPut(sys->fifo, pkt);
278 block_FifoWake( sys->fifo );