access: info.b_eof is always false during probe
[vlc.git] / modules / access / udp.c
blob9ab9c424bfc0abe81ea4be1c70d46b4d04db2b9e
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
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 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 /*****************************************************************************
32 * Preamble
33 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include <errno.h>
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>
45 #include <vlc_interrupt.h>
46 #ifdef HAVE_POLL
47 # include <poll.h>
48 #endif
49 #include <fcntl.h>
51 /*****************************************************************************
52 * Module descriptor
53 *****************************************************************************/
54 static int Open( vlc_object_t * );
55 static void Close( vlc_object_t * );
57 #define BUFFER_TEXT N_("Receive buffer")
58 #define BUFFER_LONGTEXT N_("UDP receive buffer size (bytes)" )
59 #define TIMEOUT_TEXT N_("UDP Source timeout (sec)")
61 vlc_module_begin ()
62 set_shortname( N_("UDP" ) )
63 set_description( N_("UDP input") )
64 set_category( CAT_INPUT )
65 set_subcategory( SUBCAT_INPUT_ACCESS )
67 add_obsolete_integer( "server-port" ) /* since 2.0.0 */
68 add_integer( "udp-buffer", 0x400000, BUFFER_TEXT, BUFFER_LONGTEXT, true )
69 add_integer( "udp-timeout", -1, TIMEOUT_TEXT, NULL, true )
71 set_capability( "access", 0 )
72 add_shortcut( "udp", "udpstream", "udp4", "udp6" )
74 set_callbacks( Open, Close )
75 vlc_module_end ()
77 struct access_sys_t
79 int fd;
80 int timeout;
81 size_t mtu;
82 size_t fifo_size;
83 block_fifo_t *fifo;
84 vlc_sem_t semaphore;
85 vlc_thread_t thread;
86 bool timeout_reached;
89 /*****************************************************************************
90 * Local prototypes
91 *****************************************************************************/
92 static block_t *BlockUDP( access_t *, bool * );
93 static int Control( access_t *, int, va_list );
94 static void* ThreadRead( void *data );
96 /*****************************************************************************
97 * Open: open the socket
98 *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 access_t *p_access = (access_t*)p_this;
102 access_sys_t *sys;
104 if( p_access->b_preparsing )
105 return VLC_EGENERIC;
107 sys = malloc( sizeof( *sys ) );
108 if( unlikely( sys == NULL ) )
109 return VLC_ENOMEM;
111 p_access->p_sys = sys;
113 /* Set up p_access */
114 ACCESS_SET_CALLBACKS( NULL, BlockUDP, Control, NULL );
116 char *psz_name = strdup( p_access->psz_location );
117 char *psz_parser;
118 const char *psz_server_addr, *psz_bind_addr = "";
119 int i_bind_port = 1234, i_server_port = 0;
121 if( unlikely(psz_name == NULL) )
122 goto error;
124 /* Parse psz_name syntax :
125 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
126 psz_parser = strchr( psz_name, '@' );
127 if( psz_parser != NULL )
129 /* Found bind address and/or bind port */
130 *psz_parser++ = '\0';
131 psz_bind_addr = psz_parser;
133 if( psz_bind_addr[0] == '[' )
134 /* skips bracket'd IPv6 address */
135 psz_parser = strchr( psz_parser, ']' );
137 if( psz_parser != NULL )
139 psz_parser = strchr( psz_parser, ':' );
140 if( psz_parser != NULL )
142 *psz_parser++ = '\0';
143 i_bind_port = atoi( psz_parser );
148 psz_server_addr = psz_name;
149 psz_parser = ( psz_server_addr[0] == '[' )
150 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
151 : psz_name;
153 if( psz_parser != NULL )
155 psz_parser = strchr( psz_parser, ':' );
156 if( psz_parser != NULL )
158 *psz_parser++ = '\0';
159 i_server_port = atoi( psz_parser );
163 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
164 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
166 sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
167 psz_server_addr, i_server_port, IPPROTO_UDP );
168 free( psz_name );
169 if( sys->fd == -1 )
171 msg_Err( p_access, "cannot open socket" );
172 goto error;
175 /* Revert to blocking I/O */
176 #ifndef _WIN32
177 fcntl(sys->fd, F_SETFL, fcntl(sys->fd, F_GETFL) & ~O_NONBLOCK);
178 #else
179 ioctlsocket(sys->fd, FIONBIO, &(unsigned long){ 0 });
180 #endif
182 /* FIXME: There are no particular reasons to create a FIFO and thread here.
183 * Those are just working around bugs in the stream cache. */
184 sys->fifo = block_FifoNew();
185 if( unlikely( sys->fifo == NULL ) )
187 net_Close( sys->fd );
188 goto error;
191 sys->mtu = 7 * 188;
192 sys->fifo_size = var_InheritInteger( p_access, "udp-buffer");
193 vlc_sem_init( &sys->semaphore, 0 );
195 sys->timeout = var_InheritInteger( p_access, "udp-timeout");
196 sys->timeout_reached = false;
197 if( sys->timeout > 0)
198 sys->timeout *= 1000;
200 if( vlc_clone( &sys->thread, ThreadRead, p_access,
201 VLC_THREAD_PRIORITY_INPUT ) )
203 vlc_sem_destroy( &sys->semaphore );
204 block_FifoRelease( sys->fifo );
205 net_Close( sys->fd );
206 error:
207 free( sys );
208 return VLC_EGENERIC;
211 return VLC_SUCCESS;
214 /*****************************************************************************
215 * Close: free unused data structures
216 *****************************************************************************/
217 static void Close( vlc_object_t *p_this )
219 access_t *p_access = (access_t*)p_this;
220 access_sys_t *sys = p_access->p_sys;
222 vlc_cancel( sys->thread );
223 vlc_join( sys->thread, NULL );
224 vlc_sem_destroy( &sys->semaphore );
225 block_FifoRelease( sys->fifo );
226 net_Close( sys->fd );
227 free( sys );
230 /*****************************************************************************
231 * Control:
232 *****************************************************************************/
233 static int Control( access_t *p_access, int i_query, va_list args )
235 bool *pb_bool;
236 int64_t *pi_64;
238 switch( i_query )
240 case ACCESS_CAN_SEEK:
241 case ACCESS_CAN_FASTSEEK:
242 case ACCESS_CAN_PAUSE:
243 case ACCESS_CAN_CONTROL_PACE:
244 pb_bool = (bool*)va_arg( args, bool* );
245 *pb_bool = false;
246 break;
248 case ACCESS_GET_PTS_DELAY:
249 pi_64 = (int64_t*)va_arg( args, int64_t * );
250 *pi_64 = INT64_C(1000)
251 * var_InheritInteger(p_access, "network-caching");
252 break;
254 default:
255 return VLC_EGENERIC;
257 return VLC_SUCCESS;
260 /*****************************************************************************
261 * BlockUDP:
262 *****************************************************************************/
263 static block_t *BlockUDP( access_t *p_access, bool *restrict eof )
265 access_sys_t *sys = p_access->p_sys;
266 block_t *block;
268 if (p_access->info.b_eof)
269 return NULL;
271 vlc_sem_wait_i11e(&sys->semaphore);
272 vlc_fifo_Lock(sys->fifo);
274 block = vlc_fifo_DequeueAllUnlocked(sys->fifo);
276 if (unlikely(sys->timeout_reached == true))
277 *eof = true;
279 vlc_fifo_Unlock(sys->fifo);
281 return block;
284 /*****************************************************************************
285 * ThreadRead: Pull packets from socket as soon as possible.
286 *****************************************************************************/
287 static void* ThreadRead( void *data )
289 access_t *access = data;
290 access_sys_t *sys = access->p_sys;
292 for(;;)
294 block_t *pkt = block_Alloc(sys->mtu);
295 if (unlikely(pkt == NULL))
296 { /* OOM - dequeue and discard one packet */
297 char dummy;
298 recv(sys->fd, &dummy, 1, 0);
299 continue;
302 struct iovec iov = {
303 .iov_base = pkt->p_buffer,
304 .iov_len = sys->mtu,
306 struct msghdr msg = {
307 .msg_iov = &iov,
308 .msg_iovlen = 1,
309 #ifdef __linux__
310 .msg_flags = MSG_TRUNC,
311 #endif
313 ssize_t len;
315 block_cleanup_push(pkt);
318 int poll_return=0;
319 struct pollfd ufd[1];
320 ufd[0].fd = sys->fd;
321 ufd[0].events = POLLIN;
323 while ((poll_return = poll(ufd, 1, sys->timeout)) < 0); /* cancellation point */
324 if (unlikely( poll_return == 0))
326 msg_Err( access, "Timeout on receiving, timeout %d seconds", sys->timeout/1000 );
327 vlc_fifo_Lock(sys->fifo);
328 sys->timeout_reached=true;
329 vlc_fifo_Unlock(sys->fifo);
330 vlc_sem_post(&sys->semaphore);
331 len=0;
332 break;
334 len = recvmsg(sys->fd, &msg, 0);
336 while (len == -1);
337 vlc_cleanup_pop();
339 #ifdef MSG_TRUNC
340 if (msg.msg_flags & MSG_TRUNC)
342 msg_Err(access, "%zd bytes packet truncated (MTU was %zu)",
343 len, sys->mtu);
344 pkt->i_flags |= BLOCK_FLAG_CORRUPTED;
345 sys->mtu = len;
347 else
348 #endif
349 pkt->i_buffer = len;
351 vlc_fifo_Lock(sys->fifo);
352 /* Discard old buffers on overflow */
353 while (vlc_fifo_GetBytes(sys->fifo) + len > sys->fifo_size)
355 int canc = vlc_savecancel();
356 block_Release(vlc_fifo_DequeueUnlocked(sys->fifo));
357 vlc_restorecancel(canc);
360 vlc_fifo_QueueUnlocked(sys->fifo, pkt);
361 vlc_fifo_Unlock(sys->fifo);
362 vlc_sem_post(&sys->semaphore);
365 return NULL;