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 *****************************************************************************/
30 #include <sys/types.h>
37 #include <vlc_block.h>
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # include <sys/socket.h>
50 #include <vlc_network.h>
52 #define MAX_EMPTY_BLOCKS 200
54 #if defined(WIN32) || defined(UNDER_CE)
55 # define WINSOCK_STRERROR_SIZE 20
56 static const char *winsock_strerror( char *buf )
58 snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",
60 buf[WINSOCK_STRERROR_SIZE - 1] = '\0';
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close( vlc_object_t * );
71 #define SOUT_CFG_PREFIX "sout-udp-"
73 #define CACHING_TEXT N_("Caching value (ms)")
74 #define CACHING_LONGTEXT N_( \
75 "Default caching value for outbound UDP streams. This " \
76 "value should be set in milliseconds." )
78 #define GROUP_TEXT N_("Group packets")
79 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
80 "or by groups. You can choose the number " \
81 "of packets that will be sent at a time. It " \
82 "helps reducing the scheduling load on " \
83 "heavily-loaded systems." )
84 #define AUTO_MCAST_TEXT N_("Automatic multicast streaming")
85 #define AUTO_MCAST_LONGTEXT N_("Allocates an outbound multicast address " \
89 set_description( _("UDP stream output") );
90 set_shortname( "UDP" );
91 set_category( CAT_SOUT );
92 set_subcategory( SUBCAT_SOUT_ACO );
93 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
94 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
96 add_obsolete_integer( SOUT_CFG_PREFIX "late" );
97 add_obsolete_bool( SOUT_CFG_PREFIX "raw" );
98 add_bool( SOUT_CFG_PREFIX "auto-mcast", VLC_FALSE, NULL, AUTO_MCAST_TEXT,
99 AUTO_MCAST_LONGTEXT, VLC_TRUE );
101 set_capability( "sout access", 100 );
102 add_shortcut( "udp" );
103 set_callbacks( Open, Close );
106 /*****************************************************************************
107 * Exported prototypes
108 *****************************************************************************/
110 static const char *const ppsz_sout_options[] = {
117 /* Options handled by the libvlc network core */
118 static const char *const ppsz_core_options[] = {
126 static int Write ( sout_access_out_t *, block_t * );
127 static int Seek ( sout_access_out_t *, off_t );
129 static void ThreadWrite( vlc_object_t * );
130 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
131 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
133 typedef struct sout_access_thread_t
137 sout_instance_t *p_sout;
139 block_fifo_t *p_fifo;
146 block_fifo_t *p_empty_blocks;
147 } sout_access_thread_t;
149 struct sout_access_out_sys_t
152 vlc_bool_t b_mtu_warning;
156 sout_access_thread_t *p_thread;
160 #define DEFAULT_PORT 1234
161 #define RTP_HEADER_LENGTH 12
163 /*****************************************************************************
164 * Open: open the file
165 *****************************************************************************/
166 static int Open( vlc_object_t *p_this )
168 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
169 sout_access_out_sys_t *p_sys;
171 char *psz_dst_addr = NULL;
176 config_ChainParse( p_access, SOUT_CFG_PREFIX,
177 ppsz_sout_options, p_access->p_cfg );
178 config_ChainParse( p_access, "",
179 ppsz_core_options, p_access->p_cfg );
181 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
182 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
183 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
184 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
189 if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
191 msg_Err( p_access, "not enough memory" );
194 p_access->p_sys = p_sys;
196 i_dst_port = DEFAULT_PORT;
197 if (var_GetBool (p_access, SOUT_CFG_PREFIX"auto-mcast"))
199 char buf[INET6_ADDRSTRLEN];
200 if (MakeRandMulticast (AF_INET, buf, sizeof (buf)) != NULL)
201 psz_dst_addr = strdup (buf);
205 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
207 if (psz_parser[0] == '[')
208 psz_parser = strchr (psz_parser, ']');
210 psz_parser = strchr (psz_parser ?: psz_dst_addr, ':');
211 if (psz_parser != NULL)
213 *psz_parser++ = '\0';
214 i_dst_port = atoi (psz_parser);
219 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
220 if( !p_sys->p_thread )
222 msg_Err( p_access, "out of memory" );
228 vlc_object_attach( p_sys->p_thread, p_access );
229 p_sys->p_thread->p_sout = p_access->p_sout;
230 p_sys->p_thread->b_die = 0;
231 p_sys->p_thread->b_error= 0;
232 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
233 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
235 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1,
241 msg_Err( p_access, "failed to create raw UDP socket" );
242 vlc_object_destroy (p_sys->p_thread);
248 char addr[NI_MAXNUMERICHOST];
251 if (net_GetSockAddress (i_handle, addr, &port) == 0)
253 msg_Dbg (p_access, "source: %s port %d", addr, port);
254 var_SetString (p_access, "src-addr", addr);
255 var_SetInteger (p_access, "src-port", port);
258 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
260 msg_Dbg (p_access, "destination: %s port %d", addr, port);
261 var_SetString (p_access, "dst-addr", addr);
262 var_SetInteger (p_access, "dst-port", port);
265 p_sys->p_thread->i_handle = i_handle;
266 shutdown( i_handle, SHUT_RD );
268 p_sys->p_thread->i_caching =
269 (int64_t)1000 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
270 p_sys->p_thread->i_group =
271 var_GetInteger( p_access, SOUT_CFG_PREFIX "group" );
273 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
274 p_sys->p_buffer = NULL;
276 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
277 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
279 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
280 net_Close (i_handle);
281 vlc_object_destroy( p_sys->p_thread );
286 p_access->pf_write = Write;
287 p_access->pf_seek = Seek;
289 /* update p_sout->i_out_pace_nocontrol */
290 p_access->p_sout->i_out_pace_nocontrol++;
295 /*****************************************************************************
296 * Close: close the target
297 *****************************************************************************/
298 static void Close( vlc_object_t * p_this )
300 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
301 sout_access_out_sys_t *p_sys = p_access->p_sys;
304 vlc_object_kill( p_sys->p_thread );
305 block_FifoWake( p_sys->p_thread->p_fifo );
307 for( i = 0; i < 10; i++ )
309 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
312 p_dummy->i_length = 0;
313 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
314 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
316 vlc_thread_join( p_sys->p_thread );
318 block_FifoRelease( p_sys->p_thread->p_fifo );
319 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
321 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
323 net_Close( p_sys->p_thread->i_handle );
325 vlc_object_detach( p_sys->p_thread );
326 vlc_object_destroy( p_sys->p_thread );
327 /* update p_sout->i_out_pace_nocontrol */
328 p_access->p_sout->i_out_pace_nocontrol--;
330 msg_Dbg( p_access, "UDP access output closed" );
334 /*****************************************************************************
335 * Write: standard write on a file descriptor.
336 *****************************************************************************/
337 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
339 sout_access_out_sys_t *p_sys = p_access->p_sys;
346 mtime_t now = mdate();
348 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
350 msg_Warn( p_access, "packet size > MTU, you should probably "
351 "increase the MTU" );
352 p_sys->b_mtu_warning = VLC_TRUE;
355 /* Check if there is enough space in the buffer */
356 if( p_sys->p_buffer &&
357 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
359 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
361 msg_Dbg( p_access, "late packet for UDP input (" I64Fd ")",
362 now - p_sys->p_buffer->i_dts
363 - p_sys->p_thread->i_caching );
365 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
366 p_sys->p_buffer = NULL;
369 i_len += p_buffer->i_buffer;
370 while( p_buffer->i_buffer )
372 int i_payload_size = p_sys->i_mtu;
374 int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
378 if( !p_sys->p_buffer )
380 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
381 if( !p_sys->p_buffer ) break;
384 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
385 p_buffer->p_buffer, i_write );
387 p_sys->p_buffer->i_buffer += i_write;
388 p_buffer->p_buffer += i_write;
389 p_buffer->i_buffer -= i_write;
390 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
392 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
393 msg_Warn( p_access, "putting two PCRs at once" );
394 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
397 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
400 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
402 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
403 mdate() - p_sys->p_buffer->i_dts
404 - p_sys->p_thread->i_caching );
406 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
407 p_sys->p_buffer = NULL;
411 p_next = p_buffer->p_next;
412 block_Release( p_buffer );
416 return( p_sys->p_thread->b_error ? -1 : i_len );
419 /*****************************************************************************
420 * Seek: seek to a specific location in a file
421 *****************************************************************************/
422 static int Seek( sout_access_out_t *p_access, off_t i_pos )
424 msg_Err( p_access, "UDP sout access cannot seek" );
428 /*****************************************************************************
429 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
430 *****************************************************************************/
431 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
433 sout_access_out_sys_t *p_sys = p_access->p_sys;
436 while ( block_FifoCount( p_sys->p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
438 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
439 block_Release( p_buffer );
442 if( block_FifoCount( p_sys->p_thread->p_empty_blocks ) == 0 )
444 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
448 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
449 p_buffer->i_flags = 0;
450 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
453 p_buffer->i_dts = i_dts;
454 p_buffer->i_buffer = 0;
459 /*****************************************************************************
460 * ThreadWrite: Write a packet on the network at the good time.
461 *****************************************************************************/
462 static void ThreadWrite( vlc_object_t *p_this )
464 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
465 mtime_t i_date_last = -1;
466 mtime_t i_to_send = p_thread->i_group;
467 int i_dropped_packets = 0;
468 #if defined(WIN32) || defined(UNDER_CE)
469 char strerror_buf[WINSOCK_STRERROR_SIZE];
470 # define strerror( x ) winsock_strerror( strerror_buf )
473 while( !p_thread->b_die )
476 mtime_t i_date, i_sent;
478 if( (i++ % 1000)==0 ) {
481 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
482 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
483 p_tmp = p_thread->p_fifo->p_first;
484 while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
485 msg_Dbg( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
486 p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
489 p_pk = block_FifoGet( p_thread->p_fifo );
491 continue; /* forced wake-up */
493 i_date = p_thread->i_caching + p_pk->i_dts;
494 if( i_date_last > 0 )
496 if( i_date - i_date_last > 2000000 )
498 if( !i_dropped_packets )
499 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
500 i_date - i_date_last );
502 block_FifoPut( p_thread->p_empty_blocks, p_pk );
504 i_date_last = i_date;
508 else if( i_date - i_date_last < -1000 )
510 if( !i_dropped_packets )
511 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
512 i_date_last - i_date );
517 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
520 i_to_send = p_thread->i_group;
522 ssize_t val = send( p_thread->i_handle, p_pk->p_buffer,
526 msg_Warn( p_thread, "send error: %s", strerror(errno) );
529 if( i_dropped_packets )
531 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
532 i_dropped_packets = 0;
537 if ( i_sent > i_date + 20000 )
539 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
544 block_FifoPut( p_thread->p_empty_blocks, p_pk );
546 i_date_last = i_date;
551 static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
553 uint32_t rand = (getpid() & 0xffff)
554 | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
561 struct in6_addr addr;
562 memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
563 "\x00\x00\x00\x00", 12);
565 memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
566 return inet_ntop (family, &addr, buf, buflen);
573 addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
574 return inet_ntop (family, &addr, buf, buflen);
578 errno = EAFNOSUPPORT;