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 "Default caching value for outbound UDP streams. This " \
78 "value should be set in milliseconds." )
80 #define GROUP_TEXT N_("Group packets")
81 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
82 "or by groups. You can choose the number " \
83 "of packets that will be sent at a time. It " \
84 "helps reducing the scheduling load on " \
85 "heavily-loaded systems." )
86 #define RAW_TEXT N_("Raw write")
87 #define RAW_LONGTEXT N_("Packets will be sent " \
88 "directly, without trying to fill the MTU (ie, " \
89 "without trying to make the biggest possible packets " \
90 "in order to improve streaming)." )
93 set_description( _("UDP stream output") );
94 set_shortname( "UDP" );
95 set_category( CAT_SOUT );
96 set_subcategory( SUBCAT_SOUT_ACO );
97 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
98 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
100 add_suppressed_integer( SOUT_CFG_PREFIX "late" );
101 add_bool( SOUT_CFG_PREFIX "raw", 0, NULL, RAW_TEXT, RAW_LONGTEXT,
104 set_capability( "sout access", 100 );
105 add_shortcut( "udp" );
106 add_shortcut( "rtp" ); // Will work only with ts muxer
107 set_callbacks( Open, Close );
110 /*****************************************************************************
111 * Exported prototypes
112 *****************************************************************************/
114 static const char *ppsz_sout_options[] = {
121 /* Options handled by the libvlc network core */
122 static const char *ppsz_core_options[] = {
130 static int Write ( sout_access_out_t *, block_t * );
131 static int WriteRaw( sout_access_out_t *, block_t * );
132 static int Seek ( sout_access_out_t *, off_t );
134 static void ThreadWrite( vlc_object_t * );
135 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
137 typedef struct sout_access_thread_t
141 sout_instance_t *p_sout;
143 block_fifo_t *p_fifo;
150 block_fifo_t *p_empty_blocks;
152 } sout_access_thread_t;
154 struct sout_access_out_sys_t
156 int b_rtpts; // 1 if add rtp/ts header
157 uint16_t i_sequence_number;
164 sout_access_thread_t *p_thread;
166 vlc_bool_t b_mtu_warning;
169 #define DEFAULT_PORT 1234
171 /*****************************************************************************
172 * Open: open the file
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
177 sout_access_out_sys_t *p_sys;
187 config_ChainParse( p_access, SOUT_CFG_PREFIX,
188 ppsz_sout_options, p_access->p_cfg );
189 config_ChainParse( p_access, "",
190 ppsz_core_options, p_access->p_cfg );
192 if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
194 msg_Err( p_access, "not enough memory" );
197 memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
198 p_access->p_sys = p_sys;
200 if( p_access->psz_access != NULL &&
201 !strcmp( p_access->psz_access, "rtp" ) )
210 psz_parser = strdup( p_access->psz_name );
212 psz_dst_addr = psz_parser;
215 if ( *psz_parser == '[' )
217 while( *psz_parser && *psz_parser != ']' )
222 while( *psz_parser && *psz_parser != ':' )
226 if( *psz_parser == ':' )
230 i_dst_port = atoi( psz_parser );
232 if( i_dst_port <= 0 )
234 i_dst_port = DEFAULT_PORT;
238 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
239 if( !p_sys->p_thread )
241 msg_Err( p_access, "out of memory" );
245 vlc_object_attach( p_sys->p_thread, p_access );
246 p_sys->p_thread->p_sout = p_access->p_sout;
247 p_sys->p_thread->b_die = 0;
248 p_sys->p_thread->b_error= 0;
249 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
250 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
252 i_handle = net_ConnectUDP( p_this, psz_dst_addr, i_dst_port, -1 );
255 msg_Err( p_access, "failed to create UDP socket" );
259 p_sys->p_thread->i_handle = i_handle;
260 net_StopRecv( i_handle );
262 var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
263 p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
265 var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
266 p_sys->p_thread->i_group = val.i_int;
268 p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
270 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
271 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
273 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
274 vlc_object_destroy( p_sys->p_thread );
278 srand( (uint32_t)mdate());
279 p_sys->p_buffer = NULL;
280 p_sys->i_sequence_number = rand()&0xffff;
281 p_sys->i_ssrc = rand()&0xffffffff;
283 var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
284 if( val.b_bool ) p_access->pf_write = WriteRaw;
285 else p_access->pf_write = Write;
287 p_access->pf_seek = Seek;
289 msg_Dbg( p_access, "udp access output opened(%s:%d)",
290 psz_dst_addr, i_dst_port );
292 free( psz_dst_addr );
294 /* update p_sout->i_out_pace_nocontrol */
295 p_access->p_sout->i_out_pace_nocontrol++;
300 /*****************************************************************************
301 * Close: close the target
302 *****************************************************************************/
303 static void Close( vlc_object_t * p_this )
305 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
306 sout_access_out_sys_t *p_sys = p_access->p_sys;
309 p_sys->p_thread->b_die = 1;
310 for( i = 0; i < 10; i++ )
312 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
315 p_dummy->i_length = 0;
316 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
317 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
319 vlc_thread_join( p_sys->p_thread );
321 block_FifoRelease( p_sys->p_thread->p_fifo );
322 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
324 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
326 net_Close( p_sys->p_thread->i_handle );
328 vlc_object_detach( p_sys->p_thread );
329 vlc_object_destroy( p_sys->p_thread );
330 /* update p_sout->i_out_pace_nocontrol */
331 p_access->p_sout->i_out_pace_nocontrol--;
333 msg_Dbg( p_access, "udp access output closed" );
337 /*****************************************************************************
338 * Write: standard write on a file descriptor.
339 *****************************************************************************/
340 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
342 sout_access_out_sys_t *p_sys = p_access->p_sys;
349 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
351 msg_Warn( p_access, "packet size > MTU, you should probably "
352 "increase the MTU" );
353 p_sys->b_mtu_warning = VLC_TRUE;
356 /* Check if there is enough space in the buffer */
357 if( p_sys->p_buffer &&
358 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
360 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < mdate() )
362 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
363 mdate() - p_sys->p_buffer->i_dts
364 - p_sys->p_thread->i_caching );
366 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
367 p_sys->p_buffer = NULL;
370 while( p_buffer->i_buffer )
372 int i_write = __MIN( p_buffer->i_buffer, p_sys->i_mtu );
376 if( !p_sys->p_buffer )
378 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
379 if( !p_sys->p_buffer ) break;
382 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
383 p_buffer->p_buffer, i_write );
385 p_sys->p_buffer->i_buffer += i_write;
386 p_buffer->p_buffer += i_write;
387 p_buffer->i_buffer -= i_write;
388 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
390 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
391 msg_Warn( p_access, "putting two PCRs at once" );
392 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
395 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
398 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching
401 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
402 mdate() - p_sys->p_buffer->i_dts
403 - p_sys->p_thread->i_caching );
405 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
406 p_sys->p_buffer = NULL;
410 p_next = p_buffer->p_next;
411 block_Release( p_buffer );
415 return( p_sys->p_thread->b_error ? -1 : 0 );
418 /*****************************************************************************
419 * WriteRaw: write p_buffer without trying to fill mtu
420 *****************************************************************************/
421 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
423 sout_access_out_sys_t *p_sys = p_access->p_sys;
426 while ( p_sys->p_thread->p_empty_blocks->i_depth >= MAX_EMPTY_BLOCKS )
428 p_buf = block_FifoGet(p_sys->p_thread->p_empty_blocks);
429 block_Release( p_buf );
432 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
434 return( p_sys->p_thread->b_error ? -1 : 0 );
437 /*****************************************************************************
438 * Seek: seek to a specific location in a file
439 *****************************************************************************/
440 static int Seek( sout_access_out_t *p_access, off_t i_pos )
442 msg_Err( p_access, "UDP sout access cannot seek" );
446 /*****************************************************************************
447 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
448 *****************************************************************************/
449 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
451 sout_access_out_sys_t *p_sys = p_access->p_sys;
454 while ( p_sys->p_thread->p_empty_blocks->i_depth > MAX_EMPTY_BLOCKS )
456 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
457 block_Release( p_buffer );
460 if( p_sys->p_thread->p_empty_blocks->i_depth == 0 )
462 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
466 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
467 p_buffer->i_flags = 0;
468 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
471 p_buffer->i_dts = i_dts;
472 p_buffer->i_buffer = 0;
476 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
478 /* add rtp/ts header */
479 p_buffer->p_buffer[0] = 0x80;
480 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
482 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
483 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
484 p_sys->i_sequence_number++;
486 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
487 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
488 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
489 p_buffer->p_buffer[7] = i_timestamp&0xff;
491 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
492 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
493 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
494 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
496 p_buffer->i_buffer = 12;
502 /*****************************************************************************
503 * ThreadWrite: Write a packet on the network at the good time.
504 *****************************************************************************/
505 static void ThreadWrite( vlc_object_t *p_this )
507 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
508 mtime_t i_date_last = -1;
509 mtime_t i_to_send = p_thread->i_group;
510 int i_dropped_packets = 0;
511 #if defined(WIN32) || defined(UNDER_CE)
512 char strerror_buf[WINSOCK_STRERROR_SIZE];
513 # define strerror( x ) winsock_strerror( strerror_buf )
516 while( !p_thread->b_die )
519 mtime_t i_date, i_sent;
521 if( (i++ % 1000)==0 ) {
524 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
525 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
526 p_tmp = p_thread->p_fifo->p_first;
527 while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
528 msg_Err( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
529 p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
532 p_pk = block_FifoGet( p_thread->p_fifo );
534 i_date = p_thread->i_caching + p_pk->i_dts;
535 if( i_date_last > 0 )
537 if( i_date - i_date_last > 2000000 )
539 if( !i_dropped_packets )
540 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
541 i_date - i_date_last );
543 block_FifoPut( p_thread->p_empty_blocks, p_pk );
545 i_date_last = i_date;
549 else if( i_date - i_date_last < -1000 )
551 if( !i_dropped_packets )
552 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
553 i_date_last - i_date );
558 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
561 i_to_send = p_thread->i_group;
563 if( send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 )
566 msg_Warn( p_thread, "send error: %s", strerror(errno) );
569 if( i_dropped_packets )
571 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
572 i_dropped_packets = 0;
577 if ( i_sent > i_date + 20000 )
579 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
584 block_FifoPut( p_thread->p_empty_blocks, p_pk );
586 i_date_last = i_date;