add_savefile: remove callback parameter
[vlc/asuraparaju-public.git] / modules / access_output / udp.c
blob21ff9a4de49f2423a6150c2508f3a296c546b0f7
1 /*****************************************************************************
2 * udp.c
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <sys/types.h>
36 #include <assert.h>
38 #include <vlc_sout.h>
39 #include <vlc_block.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
45 #ifdef WIN32
46 # include <winsock2.h>
47 # include <ws2tcpip.h>
48 #else
49 # include <sys/socket.h>
50 #endif
52 #include <vlc_network.h>
54 #define MAX_EMPTY_BLOCKS 200
56 /*****************************************************************************
57 * Module descriptor
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." )
76 vlc_module_begin ()
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, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
82 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
83 true )
84 add_obsolete_integer( SOUT_CFG_PREFIX "late" )
85 add_obsolete_bool( SOUT_CFG_PREFIX "raw" )
87 set_capability( "sout access", 0 )
88 add_shortcut( "udp" )
89 set_callbacks( Open, Close )
90 vlc_module_end ()
92 /*****************************************************************************
93 * Exported prototypes
94 *****************************************************************************/
96 static const char *const ppsz_sout_options[] = {
97 "caching",
98 "group",
99 NULL
102 /* Options handled by the libvlc network core */
103 static const char *const ppsz_core_options[] = {
104 "dscp",
105 "ttl",
106 "miface",
107 "miface-addr",
108 NULL
111 static ssize_t Write ( sout_access_out_t *, block_t * );
112 static int Seek ( sout_access_out_t *, off_t );
113 static int Control( sout_access_out_t *, int, va_list );
115 static void* ThreadWrite( void * );
116 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
118 struct sout_access_out_sys_t
120 mtime_t i_caching;
121 int i_handle;
122 bool b_mtu_warning;
123 size_t i_mtu;
125 block_fifo_t *p_fifo;
126 block_fifo_t *p_empty_blocks;
127 block_t *p_buffer;
129 vlc_thread_t thread;
132 #define DEFAULT_PORT 1234
134 /*****************************************************************************
135 * Open: open the file
136 *****************************************************************************/
137 static int Open( vlc_object_t *p_this )
139 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
140 sout_access_out_sys_t *p_sys;
142 char *psz_dst_addr = NULL;
143 int i_dst_port;
145 int i_handle;
147 config_ChainParse( p_access, SOUT_CFG_PREFIX,
148 ppsz_sout_options, p_access->p_cfg );
149 config_ChainParse( p_access, "",
150 ppsz_core_options, p_access->p_cfg );
152 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
153 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
154 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
155 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
157 return VLC_ENOMEM;
160 if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
161 return VLC_ENOMEM;
162 p_access->p_sys = p_sys;
164 i_dst_port = DEFAULT_PORT;
165 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
166 if( !psz_dst_addr )
168 free( p_sys );
169 return VLC_ENOMEM;
172 if (psz_parser[0] == '[')
173 psz_parser = strchr (psz_parser, ']');
175 psz_parser = strchr (psz_parser ? psz_parser : psz_dst_addr, ':');
176 if (psz_parser != NULL)
178 *psz_parser++ = '\0';
179 i_dst_port = atoi (psz_parser);
182 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1,
183 IPPROTO_UDP );
184 free (psz_dst_addr);
186 if( i_handle == -1 )
188 msg_Err( p_access, "failed to create raw UDP socket" );
189 free (p_sys);
190 return VLC_EGENERIC;
192 else
194 char addr[NI_MAXNUMERICHOST];
195 int port;
197 if (net_GetSockAddress (i_handle, addr, &port) == 0)
199 msg_Dbg (p_access, "source: %s port %d", addr, port);
200 var_SetString (p_access, "src-addr", addr);
201 var_SetInteger (p_access, "src-port", port);
204 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
206 msg_Dbg (p_access, "destination: %s port %d", addr, port);
207 var_SetString (p_access, "dst-addr", addr);
208 var_SetInteger (p_access, "dst-port", port);
211 shutdown( i_handle, SHUT_RD );
213 p_sys->i_caching = UINT64_C(1000)
214 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
215 p_sys->i_handle = i_handle;
216 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
217 p_sys->b_mtu_warning = false;
218 p_sys->p_fifo = block_FifoNew();
219 p_sys->p_empty_blocks = block_FifoNew();
220 p_sys->p_buffer = NULL;
222 if( vlc_clone( &p_sys->thread, ThreadWrite, p_access,
223 VLC_THREAD_PRIORITY_HIGHEST ) )
225 msg_Err( p_access, "cannot spawn sout access thread" );
226 block_FifoRelease( p_sys->p_fifo );
227 block_FifoRelease( p_sys->p_empty_blocks );
228 net_Close (i_handle);
229 free (p_sys);
230 return VLC_EGENERIC;
233 p_access->pf_write = Write;
234 p_access->pf_seek = Seek;
235 p_access->pf_control = Control;
237 return VLC_SUCCESS;
240 /*****************************************************************************
241 * Close: close the target
242 *****************************************************************************/
243 static void Close( vlc_object_t * p_this )
245 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
246 sout_access_out_sys_t *p_sys = p_access->p_sys;
248 vlc_cancel( p_sys->thread );
249 vlc_join( p_sys->thread, NULL );
250 block_FifoRelease( p_sys->p_fifo );
251 block_FifoRelease( p_sys->p_empty_blocks );
253 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
255 net_Close( p_sys->i_handle );
256 free( p_sys );
259 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
261 (void)p_access;
263 switch( i_query )
265 case ACCESS_OUT_CONTROLS_PACE:
266 *va_arg( args, bool * ) = false;
267 break;
269 default:
270 return VLC_EGENERIC;
272 return VLC_SUCCESS;
275 /*****************************************************************************
276 * Write: standard write on a file descriptor.
277 *****************************************************************************/
278 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
280 sout_access_out_sys_t *p_sys = p_access->p_sys;
281 int i_len = 0;
283 while( p_buffer )
285 block_t *p_next;
286 int i_packets = 0;
287 mtime_t now = mdate();
289 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
291 msg_Warn( p_access, "packet size > MTU, you should probably "
292 "increase the MTU" );
293 p_sys->b_mtu_warning = true;
296 /* Check if there is enough space in the buffer */
297 if( p_sys->p_buffer &&
298 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
300 if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
302 msg_Dbg( p_access, "late packet for UDP input (%"PRId64 ")",
303 now - p_sys->p_buffer->i_dts
304 - p_sys->i_caching );
306 block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
307 p_sys->p_buffer = NULL;
310 i_len += p_buffer->i_buffer;
311 while( p_buffer->i_buffer )
313 size_t i_payload_size = p_sys->i_mtu;
314 size_t i_write = __MIN( p_buffer->i_buffer, i_payload_size );
316 i_packets++;
318 if( !p_sys->p_buffer )
320 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
321 if( !p_sys->p_buffer ) break;
324 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
325 p_buffer->p_buffer, i_write );
327 p_sys->p_buffer->i_buffer += i_write;
328 p_buffer->p_buffer += i_write;
329 p_buffer->i_buffer -= i_write;
330 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
332 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
333 msg_Warn( p_access, "putting two PCRs at once" );
334 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
337 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
339 /* Flush */
340 if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
342 msg_Dbg( p_access, "late packet for udp input (%"PRId64 ")",
343 mdate() - p_sys->p_buffer->i_dts
344 - p_sys->i_caching );
346 block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
347 p_sys->p_buffer = NULL;
351 p_next = p_buffer->p_next;
352 block_Release( p_buffer );
353 p_buffer = p_next;
356 return i_len;
359 /*****************************************************************************
360 * Seek: seek to a specific location in a file
361 *****************************************************************************/
362 static int Seek( sout_access_out_t *p_access, off_t i_pos )
364 (void) i_pos;
365 msg_Err( p_access, "UDP sout access cannot seek" );
366 return -1;
369 /*****************************************************************************
370 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
371 *****************************************************************************/
372 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
374 sout_access_out_sys_t *p_sys = p_access->p_sys;
375 block_t *p_buffer;
377 while ( block_FifoCount( p_sys->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
379 p_buffer = block_FifoGet( p_sys->p_empty_blocks );
380 block_Release( p_buffer );
383 if( block_FifoCount( p_sys->p_empty_blocks ) == 0 )
385 p_buffer = block_Alloc( p_sys->i_mtu );
387 else
389 p_buffer = block_FifoGet(p_sys->p_empty_blocks );
390 p_buffer->i_flags = 0;
391 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
394 p_buffer->i_dts = i_dts;
395 p_buffer->i_buffer = 0;
397 return p_buffer;
400 /*****************************************************************************
401 * ThreadWrite: Write a packet on the network at the good time.
402 *****************************************************************************/
403 static void* ThreadWrite( void *data )
405 sout_access_out_t *p_access = data;
406 sout_access_out_sys_t *p_sys = p_access->p_sys;
407 mtime_t i_date_last = -1;
408 const unsigned i_group = var_GetInteger( p_access,
409 SOUT_CFG_PREFIX "group" );
410 mtime_t i_to_send = i_group;
411 unsigned i_dropped_packets = 0;
413 for (;;)
415 block_t *p_pk = block_FifoGet( p_sys->p_fifo );
416 mtime_t i_date, i_sent;
418 i_date = p_sys->i_caching + p_pk->i_dts;
419 if( i_date_last > 0 )
421 if( i_date - i_date_last > 2000000 )
423 if( !i_dropped_packets )
424 msg_Dbg( p_access, "mmh, hole (%"PRId64" > 2s) -> drop",
425 i_date - i_date_last );
427 block_FifoPut( p_sys->p_empty_blocks, p_pk );
429 i_date_last = i_date;
430 i_dropped_packets++;
431 continue;
433 else if( i_date - i_date_last < -1000 )
435 if( !i_dropped_packets )
436 msg_Dbg( p_access, "mmh, packets in the past (%"PRId64")",
437 i_date_last - i_date );
441 block_cleanup_push( p_pk );
442 i_to_send--;
443 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
445 mwait( i_date );
446 i_to_send = i_group;
448 if ( send( p_sys->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 ) == -1 )
449 msg_Warn( p_access, "send error: %m" );
450 vlc_cleanup_pop();
452 if( i_dropped_packets )
454 msg_Dbg( p_access, "dropped %i packets", i_dropped_packets );
455 i_dropped_packets = 0;
458 #if 1
459 i_sent = mdate();
460 if ( i_sent > i_date + 20000 )
462 msg_Dbg( p_access, "packet has been sent too late (%"PRId64 ")",
463 i_sent - i_date );
465 #endif
467 block_FifoPut( p_sys->p_empty_blocks, p_pk );
469 i_date_last = i_date;
471 return NULL;