1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <sys/types.h>
39 #include <vlc_block.h>
46 # include <winsock2.h>
47 # include <ws2tcpip.h>
49 # include <sys/socket.h>
52 #include <vlc_network.h>
54 #define MAX_EMPTY_BLOCKS 200
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 #define SOUT_CFG_PREFIX "sout-udp-"
64 #define CACHING_TEXT N_("Caching value (ms)")
65 #define CACHING_LONGTEXT N_( \
66 "Default caching value for outbound UDP streams. This " \
67 "value should be set in milliseconds." )
69 #define GROUP_TEXT N_("Group packets")
70 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
71 "or by groups. You can choose the number " \
72 "of packets that will be sent at a time. It " \
73 "helps reducing the scheduling load on " \
74 "heavily-loaded systems." )
77 set_description( N_("UDP stream output") )
78 set_shortname( "UDP" )
79 set_category( CAT_SOUT )
80 set_subcategory( SUBCAT_SOUT_ACO )
81 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, CACHING_TEXT, CACHING_LONGTEXT, true )
82 add_integer( SOUT_CFG_PREFIX "group", 1, GROUP_TEXT, GROUP_LONGTEXT,
85 set_capability( "sout access", 0 )
87 set_callbacks( Open, Close )
90 /*****************************************************************************
92 *****************************************************************************/
94 static const char *const ppsz_sout_options[] = {
100 /* Options handled by the libvlc network core */
101 static const char *const ppsz_core_options[] = {
109 static ssize_t Write ( sout_access_out_t *, block_t * );
110 static int Seek ( sout_access_out_t *, off_t );
111 static int Control( sout_access_out_t *, int, va_list );
113 static void* ThreadWrite( void * );
114 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
116 struct sout_access_out_sys_t
123 block_fifo_t *p_fifo;
124 block_fifo_t *p_empty_blocks;
130 #define DEFAULT_PORT 1234
132 /*****************************************************************************
133 * Open: open the file
134 *****************************************************************************/
135 static int Open( vlc_object_t *p_this )
137 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
138 sout_access_out_sys_t *p_sys;
140 char *psz_dst_addr = NULL;
145 config_ChainParse( p_access, SOUT_CFG_PREFIX,
146 ppsz_sout_options, p_access->p_cfg );
147 config_ChainParse( p_access, "",
148 ppsz_core_options, p_access->p_cfg );
150 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
151 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
152 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
153 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
158 if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
160 p_access->p_sys = p_sys;
162 i_dst_port = DEFAULT_PORT;
163 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
170 if (psz_parser[0] == '[')
171 psz_parser = strchr (psz_parser, ']');
173 psz_parser = strchr (psz_parser ? psz_parser : psz_dst_addr, ':');
174 if (psz_parser != NULL)
176 *psz_parser++ = '\0';
177 i_dst_port = atoi (psz_parser);
180 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1,
186 msg_Err( p_access, "failed to create raw UDP socket" );
192 char addr[NI_MAXNUMERICHOST];
195 if (net_GetSockAddress (i_handle, addr, &port) == 0)
197 msg_Dbg (p_access, "source: %s port %d", addr, port);
198 var_SetString (p_access, "src-addr", addr);
199 var_SetInteger (p_access, "src-port", port);
202 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
204 msg_Dbg (p_access, "destination: %s port %d", addr, port);
205 var_SetString (p_access, "dst-addr", addr);
206 var_SetInteger (p_access, "dst-port", port);
209 shutdown( i_handle, SHUT_RD );
211 p_sys->i_caching = UINT64_C(1000)
212 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
213 p_sys->i_handle = i_handle;
214 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
215 p_sys->b_mtu_warning = false;
216 p_sys->p_fifo = block_FifoNew();
217 p_sys->p_empty_blocks = block_FifoNew();
218 p_sys->p_buffer = NULL;
220 if( vlc_clone( &p_sys->thread, ThreadWrite, p_access,
221 VLC_THREAD_PRIORITY_HIGHEST ) )
223 msg_Err( p_access, "cannot spawn sout access thread" );
224 block_FifoRelease( p_sys->p_fifo );
225 block_FifoRelease( p_sys->p_empty_blocks );
226 net_Close (i_handle);
231 p_access->pf_write = Write;
232 p_access->pf_seek = Seek;
233 p_access->pf_control = Control;
238 /*****************************************************************************
239 * Close: close the target
240 *****************************************************************************/
241 static void Close( vlc_object_t * p_this )
243 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
244 sout_access_out_sys_t *p_sys = p_access->p_sys;
246 vlc_cancel( p_sys->thread );
247 vlc_join( p_sys->thread, NULL );
248 block_FifoRelease( p_sys->p_fifo );
249 block_FifoRelease( p_sys->p_empty_blocks );
251 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
253 net_Close( p_sys->i_handle );
257 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
263 case ACCESS_OUT_CONTROLS_PACE:
264 *va_arg( args, bool * ) = false;
273 /*****************************************************************************
274 * Write: standard write on a file descriptor.
275 *****************************************************************************/
276 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
278 sout_access_out_sys_t *p_sys = p_access->p_sys;
285 mtime_t now = mdate();
287 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
289 msg_Warn( p_access, "packet size > MTU, you should probably "
290 "increase the MTU" );
291 p_sys->b_mtu_warning = true;
294 /* Check if there is enough space in the buffer */
295 if( p_sys->p_buffer &&
296 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
298 if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
300 msg_Dbg( p_access, "late packet for UDP input (%"PRId64 ")",
301 now - p_sys->p_buffer->i_dts
302 - p_sys->i_caching );
304 block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
305 p_sys->p_buffer = NULL;
308 i_len += p_buffer->i_buffer;
309 while( p_buffer->i_buffer )
311 size_t i_payload_size = p_sys->i_mtu;
312 size_t i_write = __MIN( p_buffer->i_buffer, i_payload_size );
316 if( !p_sys->p_buffer )
318 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
319 if( !p_sys->p_buffer ) break;
322 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
323 p_buffer->p_buffer, i_write );
325 p_sys->p_buffer->i_buffer += i_write;
326 p_buffer->p_buffer += i_write;
327 p_buffer->i_buffer -= i_write;
328 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
330 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
331 msg_Warn( p_access, "putting two PCRs at once" );
332 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
335 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
338 if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
340 msg_Dbg( p_access, "late packet for udp input (%"PRId64 ")",
341 mdate() - p_sys->p_buffer->i_dts
342 - p_sys->i_caching );
344 block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
345 p_sys->p_buffer = NULL;
349 p_next = p_buffer->p_next;
350 block_Release( p_buffer );
357 /*****************************************************************************
358 * Seek: seek to a specific location in a file
359 *****************************************************************************/
360 static int Seek( sout_access_out_t *p_access, off_t i_pos )
363 msg_Err( p_access, "UDP sout access cannot seek" );
367 /*****************************************************************************
368 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
369 *****************************************************************************/
370 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
372 sout_access_out_sys_t *p_sys = p_access->p_sys;
375 while ( block_FifoCount( p_sys->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
377 p_buffer = block_FifoGet( p_sys->p_empty_blocks );
378 block_Release( p_buffer );
381 if( block_FifoCount( p_sys->p_empty_blocks ) == 0 )
383 p_buffer = block_Alloc( p_sys->i_mtu );
387 p_buffer = block_FifoGet(p_sys->p_empty_blocks );
388 p_buffer->i_flags = 0;
389 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
392 p_buffer->i_dts = i_dts;
393 p_buffer->i_buffer = 0;
398 /*****************************************************************************
399 * ThreadWrite: Write a packet on the network at the good time.
400 *****************************************************************************/
401 static void* ThreadWrite( void *data )
403 sout_access_out_t *p_access = data;
404 sout_access_out_sys_t *p_sys = p_access->p_sys;
405 mtime_t i_date_last = -1;
406 const unsigned i_group = var_GetInteger( p_access,
407 SOUT_CFG_PREFIX "group" );
408 mtime_t i_to_send = i_group;
409 unsigned i_dropped_packets = 0;
413 block_t *p_pk = block_FifoGet( p_sys->p_fifo );
414 mtime_t i_date, i_sent;
416 i_date = p_sys->i_caching + p_pk->i_dts;
417 if( i_date_last > 0 )
419 if( i_date - i_date_last > 2000000 )
421 if( !i_dropped_packets )
422 msg_Dbg( p_access, "mmh, hole (%"PRId64" > 2s) -> drop",
423 i_date - i_date_last );
425 block_FifoPut( p_sys->p_empty_blocks, p_pk );
427 i_date_last = i_date;
431 else if( i_date - i_date_last < -1000 )
433 if( !i_dropped_packets )
434 msg_Dbg( p_access, "mmh, packets in the past (%"PRId64")",
435 i_date_last - i_date );
439 block_cleanup_push( p_pk );
441 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
446 if ( send( p_sys->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 ) == -1 )
447 msg_Warn( p_access, "send error: %m" );
450 if( i_dropped_packets )
452 msg_Dbg( p_access, "dropped %i packets", i_dropped_packets );
453 i_dropped_packets = 0;
458 if ( i_sent > i_date + 20000 )
460 msg_Dbg( p_access, "packet has been sent too late (%"PRId64 ")",
465 block_FifoPut( p_sys->p_empty_blocks, p_pk );
467 i_date_last = i_date;