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 *****************************************************************************/
34 #include <sys/types.h>
41 #include <vlc_block.h>
48 # include <winsock2.h>
49 # include <ws2tcpip.h>
51 # include <sys/socket.h>
54 #include <vlc_network.h>
56 #define MAX_EMPTY_BLOCKS 200
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
64 #define SOUT_CFG_PREFIX "sout-udp-"
66 #define CACHING_TEXT N_("Caching value (ms)")
67 #define CACHING_LONGTEXT N_( \
68 "Default caching value for outbound UDP streams. This " \
69 "value should be set in milliseconds." )
71 #define GROUP_TEXT N_("Group packets")
72 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
73 "or by groups. You can choose the number " \
74 "of packets that will be sent at a time. It " \
75 "helps reducing the scheduling load on " \
76 "heavily-loaded systems." )
77 #define AUTO_MCAST_TEXT N_("Automatic multicast streaming")
78 #define AUTO_MCAST_LONGTEXT N_("Allocates an outbound multicast address " \
82 set_description( _("UDP stream output") );
83 set_shortname( "UDP" );
84 set_category( CAT_SOUT );
85 set_subcategory( SUBCAT_SOUT_ACO );
86 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
87 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
89 add_obsolete_integer( SOUT_CFG_PREFIX "late" );
90 add_obsolete_bool( SOUT_CFG_PREFIX "raw" );
91 add_bool( SOUT_CFG_PREFIX "auto-mcast", VLC_FALSE, NULL, AUTO_MCAST_TEXT,
92 AUTO_MCAST_LONGTEXT, VLC_TRUE );
94 set_capability( "sout access", 100 );
95 add_shortcut( "udp" );
96 set_callbacks( Open, Close );
99 /*****************************************************************************
100 * Exported prototypes
101 *****************************************************************************/
103 static const char *const ppsz_sout_options[] = {
110 /* Options handled by the libvlc network core */
111 static const char *const ppsz_core_options[] = {
119 static ssize_t Write ( sout_access_out_t *, block_t * );
120 static int Seek ( sout_access_out_t *, off_t );
122 static void ThreadWrite( vlc_object_t * );
123 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
124 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
126 typedef struct sout_access_thread_t
130 sout_instance_t *p_sout;
132 block_fifo_t *p_fifo;
139 block_fifo_t *p_empty_blocks;
140 } sout_access_thread_t;
142 struct sout_access_out_sys_t
145 vlc_bool_t b_mtu_warning;
149 sout_access_thread_t *p_thread;
153 #define DEFAULT_PORT 1234
154 #define RTP_HEADER_LENGTH 12
156 /*****************************************************************************
157 * Open: open the file
158 *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
161 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
162 sout_access_out_sys_t *p_sys;
164 char *psz_dst_addr = NULL;
169 config_ChainParse( p_access, SOUT_CFG_PREFIX,
170 ppsz_sout_options, p_access->p_cfg );
171 config_ChainParse( p_access, "",
172 ppsz_core_options, p_access->p_cfg );
174 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
175 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
176 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
177 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
182 if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
184 msg_Err( p_access, "not enough memory" );
187 p_access->p_sys = p_sys;
189 i_dst_port = DEFAULT_PORT;
190 if (var_GetBool (p_access, SOUT_CFG_PREFIX"auto-mcast"))
192 char buf[INET6_ADDRSTRLEN];
193 if (MakeRandMulticast (AF_INET, buf, sizeof (buf)) != NULL)
194 psz_dst_addr = strdup (buf);
198 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
200 if (psz_parser[0] == '[')
201 psz_parser = strchr (psz_parser, ']');
203 psz_parser = strchr (psz_parser ?: psz_dst_addr, ':');
204 if (psz_parser != NULL)
206 *psz_parser++ = '\0';
207 i_dst_port = atoi (psz_parser);
212 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
213 if( !p_sys->p_thread )
215 msg_Err( p_access, "out of memory" );
221 vlc_object_attach( p_sys->p_thread, p_access );
222 p_sys->p_thread->p_sout = p_access->p_sout;
223 p_sys->p_thread->b_die = 0;
224 p_sys->p_thread->b_error= 0;
225 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
226 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
228 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1,
234 msg_Err( p_access, "failed to create raw UDP socket" );
235 vlc_object_release (p_sys->p_thread);
241 char addr[NI_MAXNUMERICHOST];
244 if (net_GetSockAddress (i_handle, addr, &port) == 0)
246 msg_Dbg (p_access, "source: %s port %d", addr, port);
247 var_SetString (p_access, "src-addr", addr);
248 var_SetInteger (p_access, "src-port", port);
251 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
253 msg_Dbg (p_access, "destination: %s port %d", addr, port);
254 var_SetString (p_access, "dst-addr", addr);
255 var_SetInteger (p_access, "dst-port", port);
258 p_sys->p_thread->i_handle = i_handle;
259 shutdown( i_handle, SHUT_RD );
261 p_sys->p_thread->i_caching =
262 (int64_t)1000 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
263 p_sys->p_thread->i_group =
264 var_GetInteger( p_access, SOUT_CFG_PREFIX "group" );
266 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
267 p_sys->p_buffer = NULL;
269 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
270 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
272 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
273 net_Close (i_handle);
274 vlc_object_release( p_sys->p_thread );
279 p_access->pf_write = Write;
280 p_access->pf_seek = Seek;
282 /* update p_sout->i_out_pace_nocontrol */
283 p_access->p_sout->i_out_pace_nocontrol++;
288 /*****************************************************************************
289 * Close: close the target
290 *****************************************************************************/
291 static void Close( vlc_object_t * p_this )
293 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
294 sout_access_out_sys_t *p_sys = p_access->p_sys;
297 vlc_object_kill( p_sys->p_thread );
298 block_FifoWake( p_sys->p_thread->p_fifo );
300 for( i = 0; i < 10; i++ )
302 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
305 p_dummy->i_length = 0;
306 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
307 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
309 vlc_thread_join( p_sys->p_thread );
311 block_FifoRelease( p_sys->p_thread->p_fifo );
312 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
314 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
316 net_Close( p_sys->p_thread->i_handle );
318 vlc_object_detach( p_sys->p_thread );
319 vlc_object_release( p_sys->p_thread );
320 /* update p_sout->i_out_pace_nocontrol */
321 p_access->p_sout->i_out_pace_nocontrol--;
323 msg_Dbg( p_access, "UDP access output closed" );
327 /*****************************************************************************
328 * Write: standard write on a file descriptor.
329 *****************************************************************************/
330 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
332 sout_access_out_sys_t *p_sys = p_access->p_sys;
339 mtime_t now = mdate();
341 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
343 msg_Warn( p_access, "packet size > MTU, you should probably "
344 "increase the MTU" );
345 p_sys->b_mtu_warning = VLC_TRUE;
348 /* Check if there is enough space in the buffer */
349 if( p_sys->p_buffer &&
350 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
352 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
354 msg_Dbg( p_access, "late packet for UDP input (" I64Fd ")",
355 now - p_sys->p_buffer->i_dts
356 - p_sys->p_thread->i_caching );
358 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
359 p_sys->p_buffer = NULL;
362 i_len += p_buffer->i_buffer;
363 while( p_buffer->i_buffer )
365 int i_payload_size = p_sys->i_mtu;
367 int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
371 if( !p_sys->p_buffer )
373 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
374 if( !p_sys->p_buffer ) break;
377 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
378 p_buffer->p_buffer, i_write );
380 p_sys->p_buffer->i_buffer += i_write;
381 p_buffer->p_buffer += i_write;
382 p_buffer->i_buffer -= i_write;
383 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
385 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
386 msg_Warn( p_access, "putting two PCRs at once" );
387 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
390 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
393 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
395 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
396 mdate() - p_sys->p_buffer->i_dts
397 - p_sys->p_thread->i_caching );
399 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
400 p_sys->p_buffer = NULL;
404 p_next = p_buffer->p_next;
405 block_Release( p_buffer );
409 return( p_sys->p_thread->b_error ? -1 : i_len );
412 /*****************************************************************************
413 * Seek: seek to a specific location in a file
414 *****************************************************************************/
415 static int Seek( sout_access_out_t *p_access, off_t i_pos )
417 msg_Err( p_access, "UDP sout access cannot seek" );
421 /*****************************************************************************
422 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
423 *****************************************************************************/
424 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
426 sout_access_out_sys_t *p_sys = p_access->p_sys;
429 while ( block_FifoCount( p_sys->p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
431 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
432 block_Release( p_buffer );
435 if( block_FifoCount( p_sys->p_thread->p_empty_blocks ) == 0 )
437 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
441 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
442 p_buffer->i_flags = 0;
443 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
446 p_buffer->i_dts = i_dts;
447 p_buffer->i_buffer = 0;
452 /*****************************************************************************
453 * ThreadWrite: Write a packet on the network at the good time.
454 *****************************************************************************/
455 static void ThreadWrite( vlc_object_t *p_this )
457 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
458 mtime_t i_date_last = -1;
459 mtime_t i_to_send = p_thread->i_group;
460 int i_dropped_packets = 0;
462 while( !p_thread->b_die )
465 mtime_t i_date, i_sent;
467 if( (i++ % 1000)==0 ) {
470 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
471 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
472 p_tmp = p_thread->p_fifo->p_first;
473 while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
474 msg_Dbg( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
475 p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
478 p_pk = block_FifoGet( p_thread->p_fifo );
480 continue; /* forced wake-up */
482 i_date = p_thread->i_caching + p_pk->i_dts;
483 if( i_date_last > 0 )
485 if( i_date - i_date_last > 2000000 )
487 if( !i_dropped_packets )
488 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
489 i_date - i_date_last );
491 block_FifoPut( p_thread->p_empty_blocks, p_pk );
493 i_date_last = i_date;
497 else if( i_date - i_date_last < -1000 )
499 if( !i_dropped_packets )
500 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
501 i_date_last - i_date );
506 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
509 i_to_send = p_thread->i_group;
511 ssize_t val = send( p_thread->i_handle, p_pk->p_buffer,
515 msg_Warn( p_thread, "send error: %m" );
518 if( i_dropped_packets )
520 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
521 i_dropped_packets = 0;
526 if ( i_sent > i_date + 20000 )
528 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
533 block_FifoPut( p_thread->p_empty_blocks, p_pk );
535 i_date_last = i_date;
540 static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
542 uint32_t rand = (getpid() & 0xffff)
543 | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
550 struct in6_addr addr;
551 memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
552 "\x00\x00\x00\x00", 12);
554 memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
555 return inet_ntop (family, &addr, buf, buflen);
562 addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
563 return inet_ntop (family, &addr, buf, buflen);
567 errno = EAFNOSUPPORT;