1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2005 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>
43 # include <winsock2.h>
44 # include <ws2tcpip.h>
46 # define IN_MULTICAST(a) IN_CLASSD(a)
49 # include <sys/socket.h>
54 #define MAX_EMPTY_BLOCKS 200
56 #if defined(WIN32) || defined(UNDER_CE)
57 # define WINSOCK_STRERROR_SIZE 20
58 static const char *winsock_strerror( char *buf )
60 snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",
62 buf[WINSOCK_STRERROR_SIZE - 1] = '\0';
67 /*****************************************************************************
69 *****************************************************************************/
70 static int Open ( vlc_object_t * );
71 static void Close( vlc_object_t * );
73 #define SOUT_CFG_PREFIX "sout-udp-"
75 #define CACHING_TEXT N_("Caching value (ms)")
76 #define CACHING_LONGTEXT N_( \
77 "Allows you to modify the default caching value for UDP streams. This " \
78 "value should be set in millisecond units." )
80 #define TTL_TEXT N_("Time-To-Live (TTL)")
81 #define TTL_LONGTEXT N_("Allows you to define the Time-To-Live of the " \
84 #define GROUP_TEXT N_("Group packets")
85 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
86 "or by groups. This allows you to give the number " \
87 "of packets that will be sent at a time. It " \
88 "helps reducing the scheduling load on " \
89 "heavily-loaded systems." )
90 #define RAW_TEXT N_("Raw write")
91 #define RAW_LONGTEXT N_("If you enable this option, packets will be sent " \
92 "directly, without trying to fill the MTU (ie, " \
93 "without trying to make the biggest possible packets " \
94 "in order to improve streaming)." )
97 set_description( _("UDP stream output") );
98 set_shortname( N_( "UDP" ) );
99 set_category( CAT_SOUT );
100 set_subcategory( SUBCAT_SOUT_ACO );
101 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
102 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL,TTL_TEXT, TTL_LONGTEXT,
104 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
106 add_suppressed_integer( SOUT_CFG_PREFIX "late" );
107 add_bool( SOUT_CFG_PREFIX "raw", 0, NULL, RAW_TEXT, RAW_LONGTEXT,
110 set_capability( "sout access", 100 );
111 add_shortcut( "udp" );
112 add_shortcut( "rtp" ); // Will work only with ts muxer
113 set_callbacks( Open, Close );
116 /*****************************************************************************
117 * Exported prototypes
118 *****************************************************************************/
120 static const char *ppsz_sout_options[] = {
128 static int Write ( sout_access_out_t *, block_t * );
129 static int WriteRaw( sout_access_out_t *, block_t * );
130 static int Seek ( sout_access_out_t *, off_t );
132 static void ThreadWrite( vlc_object_t * );
133 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
135 typedef struct sout_access_thread_t
139 sout_instance_t *p_sout;
141 block_fifo_t *p_fifo;
148 block_fifo_t *p_empty_blocks;
150 } sout_access_thread_t;
152 struct sout_access_out_sys_t
154 int b_rtpts; // 1 if add rtp/ts header
155 uint16_t i_sequence_number;
162 sout_access_thread_t *p_thread;
164 vlc_bool_t b_mtu_warning;
167 #define DEFAULT_PORT 1234
169 /*****************************************************************************
170 * Open: open the file
171 *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
174 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
175 sout_access_out_sys_t *p_sys;
185 sout_CfgParse( p_access, SOUT_CFG_PREFIX,
186 ppsz_sout_options, p_access->p_cfg );
188 if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
190 msg_Err( p_access, "not enough memory" );
193 memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
194 p_access->p_sys = p_sys;
196 if( p_access->psz_access != NULL &&
197 !strcmp( p_access->psz_access, "rtp" ) )
206 psz_parser = strdup( p_access->psz_name );
208 psz_dst_addr = psz_parser;
211 if ( *psz_parser == '[' )
213 while( *psz_parser && *psz_parser != ']' )
218 while( *psz_parser && *psz_parser != ':' )
222 if( *psz_parser == ':' )
226 i_dst_port = atoi( psz_parser );
228 if( i_dst_port <= 0 )
230 i_dst_port = DEFAULT_PORT;
234 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
235 if( !p_sys->p_thread )
237 msg_Err( p_access, "out of memory" );
241 p_sys->p_thread->p_sout = p_access->p_sout;
242 p_sys->p_thread->b_die = 0;
243 p_sys->p_thread->b_error= 0;
244 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
245 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
247 var_Get( p_access, SOUT_CFG_PREFIX "ttl", &val );
248 i_handle = net_ConnectUDP( p_this, psz_dst_addr, i_dst_port, val.i_int );
251 msg_Err( p_access, "failed to open a connection (udp)" );
255 p_sys->p_thread->i_handle = i_handle;
256 net_StopRecv( i_handle );
258 var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
259 p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
261 var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
262 p_sys->p_thread->i_group = val.i_int;
264 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
266 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
267 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
269 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
270 vlc_object_destroy( p_sys->p_thread );
274 srand( (uint32_t)mdate());
275 p_sys->p_buffer = NULL;
276 p_sys->i_sequence_number = rand()&0xffff;
277 p_sys->i_ssrc = rand()&0xffffffff;
279 var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
280 if( val.b_bool ) p_access->pf_write = WriteRaw;
281 else p_access->pf_write = Write;
283 p_access->pf_seek = Seek;
285 msg_Dbg( p_access, "udp access output opened(%s:%d)",
286 psz_dst_addr, i_dst_port );
288 free( psz_dst_addr );
290 /* update p_sout->i_out_pace_nocontrol */
291 p_access->p_sout->i_out_pace_nocontrol++;
296 /*****************************************************************************
297 * Close: close the target
298 *****************************************************************************/
299 static void Close( vlc_object_t * p_this )
301 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
302 sout_access_out_sys_t *p_sys = p_access->p_sys;
305 p_sys->p_thread->b_die = 1;
306 for( i = 0; i < 10; i++ )
308 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
311 p_dummy->i_length = 0;
312 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
313 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
315 vlc_thread_join( p_sys->p_thread );
317 block_FifoRelease( p_sys->p_thread->p_fifo );
318 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
320 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
322 net_Close( p_sys->p_thread->i_handle );
324 vlc_object_destroy( p_sys->p_thread );
325 /* update p_sout->i_out_pace_nocontrol */
326 p_access->p_sout->i_out_pace_nocontrol--;
328 msg_Dbg( p_access, "udp access output closed" );
332 /*****************************************************************************
333 * Write: standard write on a file descriptor.
334 *****************************************************************************/
335 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
337 sout_access_out_sys_t *p_sys = p_access->p_sys;
344 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
346 msg_Warn( p_access, "packet size > MTU, you should probably "
347 "increase the MTU" );
348 p_sys->b_mtu_warning = VLC_TRUE;
351 /* Check if there is enough space in the buffer */
352 if( p_sys->p_buffer &&
353 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
355 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < mdate() )
357 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
358 mdate() - p_sys->p_buffer->i_dts
359 - p_sys->p_thread->i_caching );
361 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
362 p_sys->p_buffer = NULL;
365 while( p_buffer->i_buffer )
367 int i_write = __MIN( p_buffer->i_buffer, p_sys->i_mtu );
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
396 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
397 mdate() - p_sys->p_buffer->i_dts
398 - p_sys->p_thread->i_caching );
400 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
401 p_sys->p_buffer = NULL;
405 p_next = p_buffer->p_next;
406 block_Release( p_buffer );
410 return( p_sys->p_thread->b_error ? -1 : 0 );
413 /*****************************************************************************
414 * WriteRaw: write p_buffer without trying to fill mtu
415 *****************************************************************************/
416 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
418 sout_access_out_sys_t *p_sys = p_access->p_sys;
421 while ( p_sys->p_thread->p_empty_blocks->i_depth >= MAX_EMPTY_BLOCKS )
423 p_buf = block_FifoGet(p_sys->p_thread->p_empty_blocks);
424 block_Release( p_buf );
427 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
429 return( p_sys->p_thread->b_error ? -1 : 0 );
432 /*****************************************************************************
433 * Seek: seek to a specific location in a file
434 *****************************************************************************/
435 static int Seek( sout_access_out_t *p_access, off_t i_pos )
437 msg_Err( p_access, "UDP sout access cannot seek" );
441 /*****************************************************************************
442 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
443 *****************************************************************************/
444 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
446 sout_access_out_sys_t *p_sys = p_access->p_sys;
449 while ( p_sys->p_thread->p_empty_blocks->i_depth > MAX_EMPTY_BLOCKS )
451 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
452 block_Release( p_buffer );
455 if( p_sys->p_thread->p_empty_blocks->i_depth == 0 )
457 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
461 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
462 p_buffer->i_flags = 0;
463 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
466 p_buffer->i_dts = i_dts;
467 p_buffer->i_buffer = 0;
471 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
473 /* add rtp/ts header */
474 p_buffer->p_buffer[0] = 0x80;
475 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
477 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
478 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
479 p_sys->i_sequence_number++;
481 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
482 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
483 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
484 p_buffer->p_buffer[7] = i_timestamp&0xff;
486 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
487 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
488 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
489 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
491 p_buffer->i_buffer = 12;
497 /*****************************************************************************
498 * ThreadWrite: Write a packet on the network at the good time.
499 *****************************************************************************/
500 static void ThreadWrite( vlc_object_t *p_this )
502 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
503 mtime_t i_date_last = -1;
504 mtime_t i_to_send = p_thread->i_group;
505 int i_dropped_packets = 0;
506 #if defined(WIN32) || defined(UNDER_CE)
507 char strerror_buf[WINSOCK_STRERROR_SIZE];
508 # define strerror( x ) winsock_strerror( strerror_buf )
512 while( !p_thread->b_die )
515 mtime_t i_date, i_sent;
517 if( (i++ % 1000)==0 ) {
519 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
520 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
521 msg_Err( p_thread, "fifo depth: %d, empty blocks: %d/%d",
522 p_thread->p_fifo->i_depth, p_thread->p_empty_blocks->i_depth,i );
525 p_pk = block_FifoGet( p_thread->p_fifo );
527 i_date = p_thread->i_caching + p_pk->i_dts;
528 if( i_date_last > 0 )
530 if( i_date - i_date_last > 2000000 )
532 if( !i_dropped_packets )
533 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
534 i_date - i_date_last );
536 block_FifoPut( p_thread->p_empty_blocks, p_pk );
538 i_date_last = i_date;
542 else if( i_date - i_date_last < -1000 )
544 if( !i_dropped_packets )
545 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
546 i_date_last - i_date );
551 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
554 i_to_send = p_thread->i_group;
556 if( send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 )
559 msg_Warn( p_thread, "send error: %s", strerror(errno) );
562 if( i_dropped_packets )
564 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
565 i_dropped_packets = 0;
570 if ( i_sent > i_date + 20000 )
572 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
577 block_FifoPut( p_thread->p_empty_blocks, p_pk );
579 i_date_last = i_date;