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 *****************************************************************************/
29 #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 RAW_TEXT N_("Raw write")
99 #define RAW_LONGTEXT N_("Packets will be sent " \
100 "directly, without trying to fill the MTU (ie, " \
101 "without trying to make the biggest possible packets " \
102 "in order to improve streaming)." )
103 #define AUTO_MCAST_TEXT N_("Automatic multicast streaming")
104 #define AUTO_MCAST_LONGTEXT N_("Allocates an outbound multicast address " \
108 set_description( _("UDP stream output") );
109 set_shortname( "UDP" );
110 set_category( CAT_SOUT );
111 set_subcategory( SUBCAT_SOUT_ACO );
112 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
113 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
115 add_suppressed_integer( SOUT_CFG_PREFIX "late" );
116 add_bool( SOUT_CFG_PREFIX "raw", 0, NULL, RAW_TEXT, RAW_LONGTEXT,
118 add_bool( SOUT_CFG_PREFIX "auto-mcast", 0, NULL, AUTO_MCAST_TEXT,
119 AUTO_MCAST_LONGTEXT, VLC_TRUE );
121 set_capability( "sout access", 100 );
122 add_shortcut( "udp" );
123 add_shortcut( "rtp" ); // Will work only with ts muxer
124 add_shortcut( "udplite" );
125 set_callbacks( Open, Close );
128 /*****************************************************************************
129 * Exported prototypes
130 *****************************************************************************/
132 static const char *ppsz_sout_options[] = {
140 /* Options handled by the libvlc network core */
141 static const char *ppsz_core_options[] = {
149 static int Write ( sout_access_out_t *, block_t * );
150 static int WriteRaw( sout_access_out_t *, block_t * );
151 static int Seek ( sout_access_out_t *, off_t );
153 static void ThreadWrite( vlc_object_t * );
154 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
155 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
157 typedef struct sout_access_thread_t
161 sout_instance_t *p_sout;
163 block_fifo_t *p_fifo;
170 block_fifo_t *p_empty_blocks;
172 } sout_access_thread_t;
174 struct sout_access_out_sys_t
176 int b_rtpts; // 1 if add rtp/ts header
177 uint16_t i_sequence_number;
184 sout_access_thread_t *p_thread;
186 vlc_bool_t b_mtu_warning;
189 #define DEFAULT_PORT 1234
190 #define RTP_HEADER_LENGTH 12
192 /*****************************************************************************
193 * Open: open the file
194 *****************************************************************************/
195 static int Open( vlc_object_t *p_this )
197 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
198 sout_access_out_sys_t *p_sys;
200 char *psz_dst_addr = NULL;
201 int i_dst_port, proto = IPPROTO_UDP, cscov = 8;
202 const char *protoname = "UDP";
208 config_ChainParse( p_access, SOUT_CFG_PREFIX,
209 ppsz_sout_options, p_access->p_cfg );
210 config_ChainParse( p_access, "",
211 ppsz_core_options, p_access->p_cfg );
213 if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
215 msg_Err( p_access, "not enough memory" );
218 p_access->p_sys = p_sys;
220 if( p_access->psz_access != NULL )
222 if (strcmp (p_access->psz_access, "rtp") == 0)
223 p_sys->b_rtpts = VLC_TRUE;
224 if (strcmp (p_access->psz_access, "udplite") == 0)
226 protoname = "UDP-Lite";
227 proto = IPPROTO_UDPLITE;
228 p_sys->b_rtpts = VLC_TRUE;
232 cscov += RTP_HEADER_LENGTH;
234 i_dst_port = DEFAULT_PORT;
235 if (var_CreateGetBool (p_access, SOUT_CFG_PREFIX"auto-mcast"))
237 char buf[INET6_ADDRSTRLEN];
238 if (MakeRandMulticast (AF_INET, buf, sizeof (buf)) != NULL)
239 psz_dst_addr = strdup (buf);
243 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
245 if (psz_parser[0] == '[')
246 psz_parser = strchr (psz_parser, ']');
248 psz_parser = strchr (psz_parser, ':');
249 if (psz_parser != NULL)
251 *psz_parser++ = '\0';
252 i_dst_port = atoi (psz_parser);
256 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
257 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
258 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
259 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
263 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
264 if( !p_sys->p_thread )
266 msg_Err( p_access, "out of memory" );
270 vlc_object_attach( p_sys->p_thread, p_access );
271 p_sys->p_thread->p_sout = p_access->p_sout;
272 p_sys->p_thread->b_die = 0;
273 p_sys->p_thread->b_error= 0;
274 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
275 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
277 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1, proto );
280 msg_Err( p_access, "failed to create %s socket", protoname );
285 char addr[NI_MAXNUMERICHOST];
288 if (net_GetSockAddress (i_handle, addr, &port) == 0)
290 msg_Dbg (p_access, "source: %s port %d", addr, port);
291 var_SetString (p_access, "src-addr", addr);
292 var_SetInteger (p_access, "src-port", port);
295 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
297 msg_Dbg (p_access, "destination: %s port %d", addr, port);
298 var_SetString (p_access, "dst-addr", addr);
299 var_SetInteger (p_access, "dst-port", port);
302 p_sys->p_thread->i_handle = i_handle;
303 net_StopRecv( i_handle );
305 #ifdef UDPLITE_SEND_CSCOV
306 if (proto == IPPROTO_UDPLITE)
307 setsockopt (i_handle, SOL_UDPLITE, UDPLITE_SEND_CSCOV,
308 &cscov, sizeof (cscov));
311 var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
312 p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
314 var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
315 p_sys->p_thread->i_group = val.i_int;
317 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
319 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
320 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
322 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
323 vlc_object_destroy( p_sys->p_thread );
327 srand( (uint32_t)mdate());
328 p_sys->p_buffer = NULL;
329 p_sys->i_sequence_number = rand()&0xffff;
330 p_sys->i_ssrc = rand()&0xffffffff;
332 var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
333 if( val.b_bool ) p_access->pf_write = WriteRaw;
334 else p_access->pf_write = Write;
336 p_access->pf_seek = Seek;
338 free( psz_dst_addr );
340 /* update p_sout->i_out_pace_nocontrol */
341 p_access->p_sout->i_out_pace_nocontrol++;
346 /*****************************************************************************
347 * Close: close the target
348 *****************************************************************************/
349 static void Close( vlc_object_t * p_this )
351 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
352 sout_access_out_sys_t *p_sys = p_access->p_sys;
355 p_sys->p_thread->b_die = 1;
356 for( i = 0; i < 10; i++ )
358 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
361 p_dummy->i_length = 0;
362 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
363 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
365 vlc_thread_join( p_sys->p_thread );
367 block_FifoRelease( p_sys->p_thread->p_fifo );
368 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
370 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
372 net_Close( p_sys->p_thread->i_handle );
374 vlc_object_detach( p_sys->p_thread );
375 vlc_object_destroy( p_sys->p_thread );
376 /* update p_sout->i_out_pace_nocontrol */
377 p_access->p_sout->i_out_pace_nocontrol--;
379 msg_Dbg( p_access, "UDP access output closed" );
383 /*****************************************************************************
384 * Write: standard write on a file descriptor.
385 *****************************************************************************/
386 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
388 sout_access_out_sys_t *p_sys = p_access->p_sys;
395 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
397 msg_Warn( p_access, "packet size > MTU, you should probably "
398 "increase the MTU" );
399 p_sys->b_mtu_warning = VLC_TRUE;
402 /* Check if there is enough space in the buffer */
403 if( p_sys->p_buffer &&
404 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
406 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < mdate() )
408 msg_Dbg( p_access, "late packet for UDP input (" I64Fd ")",
409 mdate() - p_sys->p_buffer->i_dts
410 - p_sys->p_thread->i_caching );
412 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
413 p_sys->p_buffer = NULL;
416 while( p_buffer->i_buffer )
418 int i_payload_size = p_sys->i_mtu;
420 i_payload_size -= RTP_HEADER_LENGTH;
422 int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
426 if( !p_sys->p_buffer )
428 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
429 if( !p_sys->p_buffer ) break;
432 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
433 p_buffer->p_buffer, i_write );
435 p_sys->p_buffer->i_buffer += i_write;
436 p_buffer->p_buffer += i_write;
437 p_buffer->i_buffer -= i_write;
438 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
440 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
441 msg_Warn( p_access, "putting two PCRs at once" );
442 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
445 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
448 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching
451 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
452 mdate() - p_sys->p_buffer->i_dts
453 - p_sys->p_thread->i_caching );
455 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
456 p_sys->p_buffer = NULL;
460 p_next = p_buffer->p_next;
461 block_Release( p_buffer );
465 return( p_sys->p_thread->b_error ? -1 : 0 );
468 /*****************************************************************************
469 * WriteRaw: write p_buffer without trying to fill mtu
470 *****************************************************************************/
471 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
473 sout_access_out_sys_t *p_sys = p_access->p_sys;
476 while ( p_sys->p_thread->p_empty_blocks->i_depth >= MAX_EMPTY_BLOCKS )
478 p_buf = block_FifoGet(p_sys->p_thread->p_empty_blocks);
479 block_Release( p_buf );
482 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
484 return( p_sys->p_thread->b_error ? -1 : 0 );
487 /*****************************************************************************
488 * Seek: seek to a specific location in a file
489 *****************************************************************************/
490 static int Seek( sout_access_out_t *p_access, off_t i_pos )
492 msg_Err( p_access, "UDP sout access cannot seek" );
496 /*****************************************************************************
497 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
498 *****************************************************************************/
499 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
501 sout_access_out_sys_t *p_sys = p_access->p_sys;
504 while ( p_sys->p_thread->p_empty_blocks->i_depth > MAX_EMPTY_BLOCKS )
506 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
507 block_Release( p_buffer );
510 if( p_sys->p_thread->p_empty_blocks->i_depth == 0 )
512 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
516 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
517 p_buffer->i_flags = 0;
518 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
521 p_buffer->i_dts = i_dts;
522 p_buffer->i_buffer = 0;
526 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
528 /* add rtp/ts header */
529 p_buffer->p_buffer[0] = 0x80;
530 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
532 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
533 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
534 p_sys->i_sequence_number++;
536 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
537 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
538 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
539 p_buffer->p_buffer[7] = i_timestamp&0xff;
541 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
542 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
543 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
544 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
546 p_buffer->i_buffer = RTP_HEADER_LENGTH;
552 /*****************************************************************************
553 * ThreadWrite: Write a packet on the network at the good time.
554 *****************************************************************************/
555 static void ThreadWrite( vlc_object_t *p_this )
557 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
558 mtime_t i_date_last = -1;
559 mtime_t i_to_send = p_thread->i_group;
560 int i_dropped_packets = 0;
561 #if defined(WIN32) || defined(UNDER_CE)
562 char strerror_buf[WINSOCK_STRERROR_SIZE];
563 # define strerror( x ) winsock_strerror( strerror_buf )
566 while( !p_thread->b_die )
569 mtime_t i_date, i_sent;
571 if( (i++ % 1000)==0 ) {
574 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
575 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
576 p_tmp = p_thread->p_fifo->p_first;
577 while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
578 msg_Err( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
579 p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
582 p_pk = block_FifoGet( p_thread->p_fifo );
584 i_date = p_thread->i_caching + p_pk->i_dts;
585 if( i_date_last > 0 )
587 if( i_date - i_date_last > 2000000 )
589 if( !i_dropped_packets )
590 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
591 i_date - i_date_last );
593 block_FifoPut( p_thread->p_empty_blocks, p_pk );
595 i_date_last = i_date;
599 else if( i_date - i_date_last < -1000 )
601 if( !i_dropped_packets )
602 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
603 i_date_last - i_date );
608 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
611 i_to_send = p_thread->i_group;
613 if( send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 )
616 msg_Warn( p_thread, "send error: %s", strerror(errno) );
619 if( i_dropped_packets )
621 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
622 i_dropped_packets = 0;
627 if ( i_sent > i_date + 20000 )
629 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
634 block_FifoPut( p_thread->p_empty_blocks, p_pk );
636 i_date_last = i_date;
641 static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
643 uint32_t rand = (getpid() & 0xffff)
644 | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
651 struct in6_addr addr;
652 memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
653 "\x00\x00\x00\x00", 12);
655 memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
656 return inet_ntop (family, &addr, buf, buflen);
663 addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
664 return inet_ntop (family, &addr, buf, buflen);
667 errno = EAFNOSUPPORT;