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>
42 #include <vlc_block.h>
49 # include <winsock2.h>
50 # include <ws2tcpip.h>
52 # include <sys/socket.h>
55 #include <vlc_network.h>
57 #define MAX_EMPTY_BLOCKS 200
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close( vlc_object_t * );
65 #define SOUT_CFG_PREFIX "sout-udp-"
67 #define CACHING_TEXT N_("Caching value (ms)")
68 #define CACHING_LONGTEXT N_( \
69 "Default caching value for outbound UDP streams. This " \
70 "value should be set in milliseconds." )
72 #define GROUP_TEXT N_("Group packets")
73 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
74 "or by groups. You can choose the number " \
75 "of packets that will be sent at a time. It " \
76 "helps reducing the scheduling load on " \
77 "heavily-loaded systems." )
80 set_description( N_("UDP stream output") );
81 set_shortname( "UDP" );
82 set_category( CAT_SOUT );
83 set_subcategory( SUBCAT_SOUT_ACO );
84 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true );
85 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
87 add_obsolete_integer( SOUT_CFG_PREFIX "late" );
88 add_obsolete_bool( SOUT_CFG_PREFIX "raw" );
90 set_capability( "sout access", 100 );
91 add_shortcut( "udp" );
92 set_callbacks( Open, Close );
95 /*****************************************************************************
97 *****************************************************************************/
99 static const char *const ppsz_sout_options[] = {
105 /* Options handled by the libvlc network core */
106 static const char *const ppsz_core_options[] = {
114 static ssize_t Write ( sout_access_out_t *, block_t * );
115 static int Seek ( sout_access_out_t *, off_t );
117 static void ThreadWrite( vlc_object_t * );
118 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
119 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
121 typedef struct sout_access_thread_t
125 sout_instance_t *p_sout;
127 block_fifo_t *p_fifo;
134 block_fifo_t *p_empty_blocks;
135 } sout_access_thread_t;
137 struct sout_access_out_sys_t
144 sout_access_thread_t *p_thread;
148 #define DEFAULT_PORT 1234
150 /*****************************************************************************
151 * Open: open the file
152 *****************************************************************************/
153 static int Open( vlc_object_t *p_this )
155 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
156 sout_access_out_sys_t *p_sys;
158 char *psz_dst_addr = NULL;
163 config_ChainParse( p_access, SOUT_CFG_PREFIX,
164 ppsz_sout_options, p_access->p_cfg );
165 config_ChainParse( p_access, "",
166 ppsz_core_options, p_access->p_cfg );
168 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
169 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
170 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
171 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
176 if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
178 p_access->p_sys = p_sys;
180 i_dst_port = DEFAULT_PORT;
181 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
188 if (psz_parser[0] == '[')
189 psz_parser = strchr (psz_parser, ']');
191 psz_parser = strchr (psz_parser ?: psz_dst_addr, ':');
192 if (psz_parser != NULL)
194 *psz_parser++ = '\0';
195 i_dst_port = atoi (psz_parser);
199 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
200 if( !p_sys->p_thread )
207 vlc_object_attach( p_sys->p_thread, p_access );
208 p_sys->p_thread->p_sout = p_access->p_sout;
209 p_sys->p_thread->b_die = 0;
210 p_sys->p_thread->b_error= 0;
211 p_sys->p_thread->p_fifo = block_FifoNew();
212 p_sys->p_thread->p_empty_blocks = block_FifoNew();
214 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1,
220 msg_Err( p_access, "failed to create raw UDP socket" );
221 vlc_object_release (p_sys->p_thread);
227 char addr[NI_MAXNUMERICHOST];
230 if (net_GetSockAddress (i_handle, addr, &port) == 0)
232 msg_Dbg (p_access, "source: %s port %d", addr, port);
233 var_SetString (p_access, "src-addr", addr);
234 var_SetInteger (p_access, "src-port", port);
237 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
239 msg_Dbg (p_access, "destination: %s port %d", addr, port);
240 var_SetString (p_access, "dst-addr", addr);
241 var_SetInteger (p_access, "dst-port", port);
244 p_sys->p_thread->i_handle = i_handle;
245 shutdown( i_handle, SHUT_RD );
247 p_sys->p_thread->i_caching =
248 (int64_t)1000 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
249 p_sys->p_thread->i_group =
250 var_GetInteger( p_access, SOUT_CFG_PREFIX "group" );
252 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
253 p_sys->p_buffer = NULL;
255 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
256 VLC_THREAD_PRIORITY_HIGHEST, false ) )
258 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
259 net_Close (i_handle);
260 vlc_object_release( p_sys->p_thread );
265 p_access->pf_write = Write;
266 p_access->pf_seek = Seek;
268 /* update p_sout->i_out_pace_nocontrol */
269 p_access->p_sout->i_out_pace_nocontrol++;
274 /*****************************************************************************
275 * Close: close the target
276 *****************************************************************************/
277 static void Close( vlc_object_t * p_this )
279 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
280 sout_access_out_sys_t *p_sys = p_access->p_sys;
283 vlc_object_kill( p_sys->p_thread );
284 block_FifoWake( p_sys->p_thread->p_fifo );
286 for( i = 0; i < 10; i++ )
288 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
291 p_dummy->i_length = 0;
292 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
293 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
295 vlc_thread_join( p_sys->p_thread );
297 block_FifoRelease( p_sys->p_thread->p_fifo );
298 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
300 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
302 net_Close( p_sys->p_thread->i_handle );
304 vlc_object_detach( p_sys->p_thread );
305 vlc_object_release( p_sys->p_thread );
306 /* update p_sout->i_out_pace_nocontrol */
307 p_access->p_sout->i_out_pace_nocontrol--;
309 msg_Dbg( p_access, "UDP access output closed" );
313 /*****************************************************************************
314 * Write: standard write on a file descriptor.
315 *****************************************************************************/
316 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
318 sout_access_out_sys_t *p_sys = p_access->p_sys;
325 mtime_t now = mdate();
327 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
329 msg_Warn( p_access, "packet size > MTU, you should probably "
330 "increase the MTU" );
331 p_sys->b_mtu_warning = true;
334 /* Check if there is enough space in the buffer */
335 if( p_sys->p_buffer &&
336 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
338 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
340 msg_Dbg( p_access, "late packet for UDP input (%"PRId64 ")",
341 now - p_sys->p_buffer->i_dts
342 - p_sys->p_thread->i_caching );
344 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
345 p_sys->p_buffer = NULL;
348 i_len += p_buffer->i_buffer;
349 while( p_buffer->i_buffer )
351 int i_payload_size = p_sys->i_mtu;
353 int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
357 if( !p_sys->p_buffer )
359 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
360 if( !p_sys->p_buffer ) break;
363 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
364 p_buffer->p_buffer, i_write );
366 p_sys->p_buffer->i_buffer += i_write;
367 p_buffer->p_buffer += i_write;
368 p_buffer->i_buffer -= i_write;
369 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
371 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
372 msg_Warn( p_access, "putting two PCRs at once" );
373 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
376 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
379 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
381 msg_Dbg( p_access, "late packet for udp input (%"PRId64 ")",
382 mdate() - p_sys->p_buffer->i_dts
383 - p_sys->p_thread->i_caching );
385 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
386 p_sys->p_buffer = NULL;
390 p_next = p_buffer->p_next;
391 block_Release( p_buffer );
395 return( p_sys->p_thread->b_error ? -1 : i_len );
398 /*****************************************************************************
399 * Seek: seek to a specific location in a file
400 *****************************************************************************/
401 static int Seek( sout_access_out_t *p_access, off_t i_pos )
403 msg_Err( p_access, "UDP sout access cannot seek" );
407 /*****************************************************************************
408 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
409 *****************************************************************************/
410 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
412 sout_access_out_sys_t *p_sys = p_access->p_sys;
415 while ( block_FifoCount( p_sys->p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
417 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
418 block_Release( p_buffer );
421 if( block_FifoCount( p_sys->p_thread->p_empty_blocks ) == 0 )
423 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
427 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
428 p_buffer->i_flags = 0;
429 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
432 p_buffer->i_dts = i_dts;
433 p_buffer->i_buffer = 0;
438 /*****************************************************************************
439 * ThreadWrite: Write a packet on the network at the good time.
440 *****************************************************************************/
441 static void ThreadWrite( vlc_object_t *p_this )
443 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
444 mtime_t i_date_last = -1;
445 mtime_t i_to_send = p_thread->i_group;
446 int i_dropped_packets = 0;
448 while( !p_thread->b_die )
451 mtime_t i_date, i_sent;
453 if( (i++ % 1000)==0 ) {
456 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
457 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
458 p_tmp = p_thread->p_fifo->p_first;
459 while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
460 msg_Dbg( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
461 p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
464 p_pk = block_FifoGet( p_thread->p_fifo );
466 continue; /* forced wake-up */
468 i_date = p_thread->i_caching + p_pk->i_dts;
469 if( i_date_last > 0 )
471 if( i_date - i_date_last > 2000000 )
473 if( !i_dropped_packets )
474 msg_Dbg( p_thread, "mmh, hole (%"PRId64" > 2s) -> drop",
475 i_date - i_date_last );
477 block_FifoPut( p_thread->p_empty_blocks, p_pk );
479 i_date_last = i_date;
483 else if( i_date - i_date_last < -1000 )
485 if( !i_dropped_packets )
486 msg_Dbg( p_thread, "mmh, packets in the past (%"PRId64")",
487 i_date_last - i_date );
492 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
495 i_to_send = p_thread->i_group;
497 ssize_t val = send( p_thread->i_handle, p_pk->p_buffer,
501 msg_Warn( p_thread, "send error: %m" );
504 if( i_dropped_packets )
506 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
507 i_dropped_packets = 0;
512 if ( i_sent > i_date + 20000 )
514 msg_Dbg( p_thread, "packet has been sent too late (%"PRId64 ")",
519 block_FifoPut( p_thread->p_empty_blocks, p_pk );
521 i_date_last = i_date;