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 #if defined (HAVE_NETINET_UDPLITE_H)
53 # include <netinet/udplite.h>
54 #elif defined (__linux__)
55 # define UDPLITE_SEND_CSCOV 10
56 # define UDPLITE_RECV_CSCOV 11
59 #ifndef IPPROTO_UDPLITE
60 # define IPPROTO_UDPLITE 136 /* from IANA */
63 # define SOL_UDPLITE IPPROTO_UDPLITE
66 #define MAX_EMPTY_BLOCKS 200
68 #if defined(WIN32) || defined(UNDER_CE)
69 # define WINSOCK_STRERROR_SIZE 20
70 static const char *winsock_strerror( char *buf )
72 snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",
74 buf[WINSOCK_STRERROR_SIZE - 1] = '\0';
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Open ( vlc_object_t * );
83 static void Close( vlc_object_t * );
85 #define SOUT_CFG_PREFIX "sout-udp-"
87 #define CACHING_TEXT N_("Caching value (ms)")
88 #define CACHING_LONGTEXT N_( \
89 "Default caching value for outbound UDP streams. This " \
90 "value should be set in milliseconds." )
92 #define GROUP_TEXT N_("Group packets")
93 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
94 "or by groups. You can choose the number " \
95 "of packets that will be sent at a time. It " \
96 "helps reducing the scheduling load on " \
97 "heavily-loaded systems." )
98 #define AUTO_MCAST_TEXT N_("Automatic multicast streaming")
99 #define AUTO_MCAST_LONGTEXT N_("Allocates an outbound multicast address " \
101 #define UDPLITE_TEXT N_("UDP-Lite")
102 #define UDPLITE_LONGTEXT N_("Use UDP-Lite/IP instead of normal UDP/IP")
103 #define CSCOV_TEXT N_("Checksum coverage")
104 #define CSCOV_LONGTEXT N_("Payload bytes covered by layer-4 checksum")
107 set_description( _("UDP stream output") );
108 set_shortname( "UDP" );
109 set_category( CAT_SOUT );
110 set_subcategory( SUBCAT_SOUT_ACO );
111 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
112 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
114 add_obsolete_integer( SOUT_CFG_PREFIX "late" );
115 add_obsolete_bool( SOUT_CFG_PREFIX "raw" );
116 add_bool( SOUT_CFG_PREFIX "auto-mcast", VLC_FALSE, NULL, AUTO_MCAST_TEXT,
117 AUTO_MCAST_LONGTEXT, VLC_TRUE );
118 add_bool( SOUT_CFG_PREFIX "udplite", VLC_FALSE, NULL, UDPLITE_TEXT, UDPLITE_LONGTEXT, VLC_TRUE );
119 add_integer( SOUT_CFG_PREFIX "cscov", 12, NULL, CSCOV_TEXT, CSCOV_LONGTEXT, VLC_TRUE );
121 set_capability( "sout access", 100 );
122 add_shortcut( "udp" );
123 set_callbacks( Open, Close );
126 /*****************************************************************************
127 * Exported prototypes
128 *****************************************************************************/
130 static const char *const ppsz_sout_options[] = {
139 /* Options handled by the libvlc network core */
140 static const char *const ppsz_core_options[] = {
148 static int Write ( sout_access_out_t *, block_t * );
149 static int Seek ( sout_access_out_t *, off_t );
151 static void ThreadWrite( vlc_object_t * );
152 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
153 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
155 typedef struct sout_access_thread_t
159 sout_instance_t *p_sout;
161 block_fifo_t *p_fifo;
168 block_fifo_t *p_empty_blocks;
169 } sout_access_thread_t;
171 struct sout_access_out_sys_t
174 vlc_bool_t b_mtu_warning;
178 sout_access_thread_t *p_thread;
182 #define DEFAULT_PORT 1234
183 #define RTP_HEADER_LENGTH 12
185 /*****************************************************************************
186 * Open: open the file
187 *****************************************************************************/
188 static int Open( vlc_object_t *p_this )
190 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
191 sout_access_out_sys_t *p_sys;
193 char *psz_dst_addr = NULL;
194 int i_dst_port, proto = IPPROTO_UDP;
195 const char *protoname = "UDP";
199 config_ChainParse( p_access, SOUT_CFG_PREFIX,
200 ppsz_sout_options, p_access->p_cfg );
201 config_ChainParse( p_access, "",
202 ppsz_core_options, p_access->p_cfg );
204 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
205 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
206 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
207 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
212 if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
214 msg_Err( p_access, "not enough memory" );
217 p_access->p_sys = p_sys;
219 if (var_GetBool (p_access, SOUT_CFG_PREFIX"lite"))
221 protoname = "UDP-Lite";
222 proto = IPPROTO_UDPLITE;
225 i_dst_port = DEFAULT_PORT;
226 if (var_GetBool (p_access, SOUT_CFG_PREFIX"auto-mcast"))
228 char buf[INET6_ADDRSTRLEN];
229 if (MakeRandMulticast (AF_INET, buf, sizeof (buf)) != NULL)
230 psz_dst_addr = strdup (buf);
234 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
236 if (psz_parser[0] == '[')
237 psz_parser = strchr (psz_parser, ']');
239 psz_parser = strchr (psz_parser ?: psz_dst_addr, ':');
240 if (psz_parser != NULL)
242 *psz_parser++ = '\0';
243 i_dst_port = atoi (psz_parser);
248 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
249 if( !p_sys->p_thread )
251 msg_Err( p_access, "out of memory" );
257 vlc_object_attach( p_sys->p_thread, p_access );
258 p_sys->p_thread->p_sout = p_access->p_sout;
259 p_sys->p_thread->b_die = 0;
260 p_sys->p_thread->b_error= 0;
261 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
262 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
264 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1, proto );
269 msg_Err( p_access, "failed to create %s socket", protoname );
270 vlc_object_destroy (p_sys->p_thread);
276 char addr[NI_MAXNUMERICHOST];
279 if (net_GetSockAddress (i_handle, addr, &port) == 0)
281 msg_Dbg (p_access, "source: %s port %d", addr, port);
282 var_SetString (p_access, "src-addr", addr);
283 var_SetInteger (p_access, "src-port", port);
286 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
288 msg_Dbg (p_access, "destination: %s port %d", addr, port);
289 var_SetString (p_access, "dst-addr", addr);
290 var_SetInteger (p_access, "dst-port", port);
293 p_sys->p_thread->i_handle = i_handle;
294 shutdown( i_handle, SHUT_RD );
296 int cscov = var_GetInteger (p_access, SOUT_CFG_PREFIX"cscov");
301 #ifdef UDPLITE_SEND_CSCOV
302 case IPPROTO_UDPLITE:
304 setsockopt (i_handle, SOL_UDPLITE, UDPLITE_SEND_CSCOV,
305 &(int){ cscov }, sizeof (cscov));
308 #ifdef DCCP_SOCKOPT_RECV_CSCOV
309 /* FIXME: ^^is this the right name ? */
310 /* FIXME: is this inherited by accept() ? */
312 cscov = ((cscov + 3) >> 2) + 1;
314 break; /* out of DCCP cscov range */
315 setsockopt (i_handle, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
316 &(int){ cscov }, sizeof (cscov));
322 p_sys->p_thread->i_caching =
323 (int64_t)1000 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
324 p_sys->p_thread->i_group =
325 var_GetInteger( p_access, SOUT_CFG_PREFIX "group" );
327 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
328 p_sys->p_buffer = NULL;
330 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
331 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
333 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
334 net_Close (i_handle);
335 vlc_object_destroy( p_sys->p_thread );
340 p_access->pf_write = Write;
341 p_access->pf_seek = Seek;
343 /* update p_sout->i_out_pace_nocontrol */
344 p_access->p_sout->i_out_pace_nocontrol++;
349 /*****************************************************************************
350 * Close: close the target
351 *****************************************************************************/
352 static void Close( vlc_object_t * p_this )
354 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
355 sout_access_out_sys_t *p_sys = p_access->p_sys;
358 vlc_object_kill( p_sys->p_thread );
359 for( i = 0; i < 10; i++ )
361 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
364 p_dummy->i_length = 0;
365 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
366 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
368 vlc_thread_join( p_sys->p_thread );
370 block_FifoRelease( p_sys->p_thread->p_fifo );
371 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
373 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
375 net_Close( p_sys->p_thread->i_handle );
377 vlc_object_detach( p_sys->p_thread );
378 vlc_object_destroy( p_sys->p_thread );
379 /* update p_sout->i_out_pace_nocontrol */
380 p_access->p_sout->i_out_pace_nocontrol--;
382 msg_Dbg( p_access, "UDP access output closed" );
386 /*****************************************************************************
387 * Write: standard write on a file descriptor.
388 *****************************************************************************/
389 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
391 sout_access_out_sys_t *p_sys = p_access->p_sys;
398 mtime_t now = mdate();
400 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
402 msg_Warn( p_access, "packet size > MTU, you should probably "
403 "increase the MTU" );
404 p_sys->b_mtu_warning = VLC_TRUE;
407 /* Check if there is enough space in the buffer */
408 if( p_sys->p_buffer &&
409 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
411 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
413 msg_Dbg( p_access, "late packet for UDP input (" I64Fd ")",
414 now - p_sys->p_buffer->i_dts
415 - p_sys->p_thread->i_caching );
417 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
418 p_sys->p_buffer = NULL;
421 i_len += p_buffer->i_buffer;
422 while( p_buffer->i_buffer )
424 int i_payload_size = p_sys->i_mtu;
426 int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
430 if( !p_sys->p_buffer )
432 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
433 if( !p_sys->p_buffer ) break;
436 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
437 p_buffer->p_buffer, i_write );
439 p_sys->p_buffer->i_buffer += i_write;
440 p_buffer->p_buffer += i_write;
441 p_buffer->i_buffer -= i_write;
442 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
444 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
445 msg_Warn( p_access, "putting two PCRs at once" );
446 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
449 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
452 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
454 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
455 mdate() - p_sys->p_buffer->i_dts
456 - p_sys->p_thread->i_caching );
458 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
459 p_sys->p_buffer = NULL;
463 p_next = p_buffer->p_next;
464 block_Release( p_buffer );
468 return( p_sys->p_thread->b_error ? -1 : i_len );
471 /*****************************************************************************
472 * Seek: seek to a specific location in a file
473 *****************************************************************************/
474 static int Seek( sout_access_out_t *p_access, off_t i_pos )
476 msg_Err( p_access, "UDP sout access cannot seek" );
480 /*****************************************************************************
481 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
482 *****************************************************************************/
483 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
485 sout_access_out_sys_t *p_sys = p_access->p_sys;
488 while ( p_sys->p_thread->p_empty_blocks->i_depth > MAX_EMPTY_BLOCKS )
490 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
491 block_Release( p_buffer );
494 if( p_sys->p_thread->p_empty_blocks->i_depth == 0 )
496 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
500 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
501 p_buffer->i_flags = 0;
502 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
505 p_buffer->i_dts = i_dts;
506 p_buffer->i_buffer = 0;
511 /*****************************************************************************
512 * ThreadWrite: Write a packet on the network at the good time.
513 *****************************************************************************/
514 static void ThreadWrite( vlc_object_t *p_this )
516 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
517 mtime_t i_date_last = -1;
518 mtime_t i_to_send = p_thread->i_group;
519 int i_dropped_packets = 0;
520 #if defined(WIN32) || defined(UNDER_CE)
521 char strerror_buf[WINSOCK_STRERROR_SIZE];
522 # define strerror( x ) winsock_strerror( strerror_buf )
525 while( !p_thread->b_die )
528 mtime_t i_date, i_sent;
530 if( (i++ % 1000)==0 ) {
533 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
534 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
535 p_tmp = p_thread->p_fifo->p_first;
536 while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
537 msg_Dbg( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
538 p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
541 p_pk = block_FifoGet( p_thread->p_fifo );
543 i_date = p_thread->i_caching + p_pk->i_dts;
544 if( i_date_last > 0 )
546 if( i_date - i_date_last > 2000000 )
548 if( !i_dropped_packets )
549 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
550 i_date - i_date_last );
552 block_FifoPut( p_thread->p_empty_blocks, p_pk );
554 i_date_last = i_date;
558 else if( i_date - i_date_last < -1000 )
560 if( !i_dropped_packets )
561 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
562 i_date_last - i_date );
567 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
570 i_to_send = p_thread->i_group;
572 ssize_t val = send( p_thread->i_handle, p_pk->p_buffer,
576 msg_Warn( p_thread, "send error: %s", strerror(errno) );
579 if( i_dropped_packets )
581 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
582 i_dropped_packets = 0;
587 if ( i_sent > i_date + 20000 )
589 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
594 block_FifoPut( p_thread->p_empty_blocks, p_pk );
596 i_date_last = i_date;
601 static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
603 uint32_t rand = (getpid() & 0xffff)
604 | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
611 struct in6_addr addr;
612 memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
613 "\x00\x00\x00\x00", 12);
615 memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
616 return inet_ntop (family, &addr, buf, buflen);
623 addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
624 return inet_ntop (family, &addr, buf, buflen);
628 errno = EAFNOSUPPORT;