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>
45 #include <vlc_interrupt.h>
51 /*****************************************************************************
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)")
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
)
89 /*****************************************************************************
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
;
104 if( p_access
->b_preparsing
)
107 sys
= malloc( sizeof( *sys
) );
108 if( unlikely( sys
== NULL
) )
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
);
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
) )
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 */
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
);
171 msg_Err( p_access
, "cannot open socket" );
175 /* Revert to blocking I/O */
177 fcntl(sys
->fd
, F_SETFL
, fcntl(sys
->fd
, F_GETFL
) & ~O_NONBLOCK
);
179 ioctlsocket(sys
->fd
, FIONBIO
, &(unsigned long){ 0 });
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
);
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
);
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
);
230 /*****************************************************************************
232 *****************************************************************************/
233 static int Control( access_t
*p_access
, int i_query
, va_list args
)
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* );
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");
260 /*****************************************************************************
262 *****************************************************************************/
263 static block_t
*BlockUDP( access_t
*p_access
, bool *restrict eof
)
265 access_sys_t
*sys
= p_access
->p_sys
;
268 if (p_access
->info
.b_eof
)
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))
279 vlc_fifo_Unlock(sys
->fifo
);
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
;
294 block_t
*pkt
= block_Alloc(sys
->mtu
);
295 if (unlikely(pkt
== NULL
))
296 { /* OOM - dequeue and discard one packet */
298 recv(sys
->fd
, &dummy
, 1, 0);
303 .iov_base
= pkt
->p_buffer
,
306 struct msghdr msg
= {
310 .msg_flags
= MSG_TRUNC
,
315 block_cleanup_push(pkt
);
319 struct pollfd ufd
[1];
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
);
334 len
= recvmsg(sys
->fd
, &msg
, 0);
340 if (msg
.msg_flags
& MSG_TRUNC
)
342 msg_Err(access
, "%zd bytes packet truncated (MTU was %zu)",
344 pkt
->i_flags
|= BLOCK_FLAG_CORRUPTED
;
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
);