1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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>
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 #define SOUT_CFG_PREFIX "sout-udp-"
63 #define CACHING_TEXT N_("Caching value (ms)")
64 #define CACHING_LONGTEXT N_( \
65 "Allows you to modify the default caching value for UDP streams. This " \
66 "value should be set in millisecond units." )
68 #define TTL_TEXT N_("Time To Live")
69 #define TTL_LONGTEXT N_("Allows you to define the time to live of the " \
72 #define GROUP_TEXT N_("Group packets")
73 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
74 "or by groups. This allows you to give the number " \
75 "of packets that will be sent at a time. It " \
76 "helps reducing the scheduling load on " \
77 "heavily-loaded systems." )
78 #define LATE_TEXT N_("Late delay (ms)" )
79 #define LATE_LONGTEXT N_("Late packets are dropped. This allows you to give " \
80 "the time (in milliseconds) a packet is allowed to be" \
82 #define RAW_TEXT N_("Raw write")
83 #define RAW_LONGTEXT N_("If you enable this option, packets will be sent " \
84 "directly, without trying to fill the MTU (ie, " \
85 "without trying to make the biggest possible packets " \
86 "in order to improve streaming)." )
89 set_description( _("UDP stream output") );
90 set_shortname( N_( "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 "ttl", 0, NULL,TTL_TEXT, TTL_LONGTEXT,
96 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
98 add_integer( SOUT_CFG_PREFIX "late", 0, NULL, LATE_TEXT, LATE_LONGTEXT,
100 add_bool( SOUT_CFG_PREFIX "raw", 0, NULL, RAW_TEXT, RAW_LONGTEXT,
103 set_capability( "sout access", 100 );
104 add_shortcut( "udp" );
105 add_shortcut( "rtp" ); // Will work only with ts muxer
106 set_callbacks( Open, Close );
109 /*****************************************************************************
110 * Exported prototypes
111 *****************************************************************************/
113 static const char *ppsz_sout_options[] = {
122 static int Write ( sout_access_out_t *, block_t * );
123 static int WriteRaw( sout_access_out_t *, block_t * );
124 static int Seek ( sout_access_out_t *, off_t );
126 static void ThreadWrite( vlc_object_t * );
127 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
129 typedef struct sout_access_thread_t
133 sout_instance_t *p_sout;
135 block_fifo_t *p_fifo;
143 } sout_access_thread_t;
145 struct sout_access_out_sys_t
147 int b_rtpts; // 1 if add rtp/ts header
148 uint16_t i_sequence_number;
155 sout_access_thread_t *p_thread;
157 vlc_bool_t b_mtu_warning;
160 #define DEFAULT_PORT 1234
162 /*****************************************************************************
163 * Open: open the file
164 *****************************************************************************/
165 static int Open( vlc_object_t *p_this )
167 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
168 sout_access_out_sys_t *p_sys;
175 network_socket_t socket_desc;
179 sout_CfgParse( p_access, SOUT_CFG_PREFIX,
180 ppsz_sout_options, p_access->p_cfg );
182 if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
184 msg_Err( p_access, "not enough memory" );
187 memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
188 p_access->p_sys = p_sys;
190 if( p_access->psz_access != NULL &&
191 !strcmp( p_access->psz_access, "rtp" ) )
193 msg_Warn( p_access, "be careful that rtp output only works with ts "
194 "payload (not an error)" );
202 psz_parser = strdup( p_access->psz_name );
204 psz_dst_addr = psz_parser;
207 if ( *psz_parser == '[' )
209 while( *psz_parser && *psz_parser != ']' )
214 while( *psz_parser && *psz_parser != ':' )
218 if( *psz_parser == ':' )
222 i_dst_port = atoi( psz_parser );
224 if( i_dst_port <= 0 )
226 i_dst_port = DEFAULT_PORT;
230 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
231 if( !p_sys->p_thread )
233 msg_Err( p_access, "out of memory" );
237 p_sys->p_thread->p_sout = p_access->p_sout;
238 p_sys->p_thread->b_die = 0;
239 p_sys->p_thread->b_error= 0;
240 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
242 socket_desc.i_type = NETWORK_UDP;
243 socket_desc.psz_server_addr = psz_dst_addr;
244 socket_desc.i_server_port = i_dst_port;
245 socket_desc.psz_bind_addr = "";
246 socket_desc.i_bind_port = 0;
248 var_Get( p_access, SOUT_CFG_PREFIX "ttl", &val );
249 socket_desc.i_ttl = val.i_int;
251 p_sys->p_thread->p_private = (void*)&socket_desc;
252 if( !( p_network = module_Need( p_sys->p_thread, "network", NULL, 0 ) ) )
254 msg_Err( p_access, "failed to open a connection (udp)" );
257 module_Unneed( p_sys->p_thread, p_network );
259 p_sys->p_thread->i_handle = socket_desc.i_handle;
261 var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
262 p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
264 var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
265 p_sys->p_thread->i_group = val.i_int;
267 var_Get( p_access, SOUT_CFG_PREFIX "late", &val );
268 p_sys->p_thread->i_late = (int64_t)val.i_int * 1000;
270 p_sys->i_mtu = socket_desc.i_mtu;
273 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
274 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
276 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
277 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
280 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
281 vlc_object_destroy( p_sys->p_thread );
285 srand( (uint32_t)mdate());
286 p_sys->p_buffer = NULL;
287 p_sys->i_sequence_number = rand()&0xffff;
288 p_sys->i_ssrc = rand()&0xffffffff;
290 var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
291 if( val.b_bool ) p_access->pf_write = WriteRaw;
292 else p_access->pf_write = Write;
294 p_access->pf_seek = Seek;
296 msg_Dbg( p_access, "udp access output opened(%s:%d)",
297 psz_dst_addr, i_dst_port );
299 free( psz_dst_addr );
301 /* update p_sout->i_out_pace_nocontrol */
302 p_access->p_sout->i_out_pace_nocontrol++;
307 /*****************************************************************************
308 * Close: close the target
309 *****************************************************************************/
310 static void Close( vlc_object_t * p_this )
312 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
313 sout_access_out_sys_t *p_sys = p_access->p_sys;
316 p_sys->p_thread->b_die = 1;
317 for( i = 0; i < 10; i++ )
319 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
322 p_dummy->i_length = 0;
323 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
325 vlc_thread_join( p_sys->p_thread );
327 block_FifoRelease( p_sys->p_thread->p_fifo );
329 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
331 net_Close( p_sys->p_thread->i_handle );
333 /* update p_sout->i_out_pace_nocontrol */
334 p_access->p_sout->i_out_pace_nocontrol--;
336 msg_Dbg( p_access, "udp access output closed" );
340 /*****************************************************************************
341 * Write: standard write on a file descriptor.
342 *****************************************************************************/
343 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
345 sout_access_out_sys_t *p_sys = p_access->p_sys;
352 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
354 msg_Warn( p_access, "packet size > MTU, you should probably "
355 "increase the MTU" );
356 p_sys->b_mtu_warning = VLC_TRUE;
359 /* Check if there is enough space in the buffer */
360 if( p_sys->p_buffer &&
361 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
363 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
364 p_sys->p_buffer = NULL;
367 while( p_buffer->i_buffer )
369 int i_write = __MIN( p_buffer->i_buffer, p_sys->i_mtu );
373 if( !p_sys->p_buffer )
375 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
376 if( !p_sys->p_buffer ) break;
379 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
380 p_buffer->p_buffer, i_write );
382 p_sys->p_buffer->i_buffer += i_write;
383 p_buffer->p_buffer += i_write;
384 p_buffer->i_buffer -= i_write;
385 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
387 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
388 msg_Warn( p_access, "putting two PCRs at once" );
389 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
392 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
395 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
396 p_sys->p_buffer = NULL;
400 p_next = p_buffer->p_next;
401 block_Release( p_buffer );
405 return( p_sys->p_thread->b_error ? -1 : 0 );
408 /*****************************************************************************
409 * WriteRaw: write p_buffer without trying to fill mtu
410 *****************************************************************************/
411 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
413 sout_access_out_sys_t *p_sys = p_access->p_sys;
415 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
417 return( p_sys->p_thread->b_error ? -1 : 0 );
420 /*****************************************************************************
421 * Seek: seek to a specific location in a file
422 *****************************************************************************/
423 static int Seek( sout_access_out_t *p_access, off_t i_pos )
425 msg_Err( p_access, "UDP sout access cannot seek" );
429 /*****************************************************************************
430 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
431 *****************************************************************************/
432 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
434 sout_access_out_sys_t *p_sys = p_access->p_sys;
437 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
438 p_buffer->i_dts = i_dts;
439 p_buffer->i_buffer = 0;
443 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
445 /* add rtp/ts header */
446 p_buffer->p_buffer[0] = 0x80;
447 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
449 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
450 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
451 p_sys->i_sequence_number++;
453 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
454 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
455 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
456 p_buffer->p_buffer[7] = i_timestamp&0xff;
458 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
459 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
460 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
461 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
463 p_buffer->i_buffer = 12;
469 /*****************************************************************************
470 * ThreadWrite: Write a packet on the network at the good time.
471 *****************************************************************************/
472 static void ThreadWrite( vlc_object_t *p_this )
474 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
475 mtime_t i_date_last = -1;
476 mtime_t i_to_send = p_thread->i_group;
477 int i_dropped_packets = 0;
479 while( !p_thread->b_die )
482 mtime_t i_date, i_sent;
484 p_pk = block_FifoGet( p_thread->p_fifo );
486 i_date = p_thread->i_caching + p_pk->i_dts;
487 if( i_date_last > 0 )
489 if( i_date - i_date_last > 2000000 )
491 if( !i_dropped_packets )
492 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
493 i_date - i_date_last );
495 block_Release( p_pk );
496 i_date_last = i_date;
500 else if( i_date - i_date_last < -15000 )
502 if( !i_dropped_packets )
503 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")"
504 " -> drop", i_date - i_date_last );
506 block_Release( p_pk );
507 i_date_last = i_date;
514 if( p_thread->i_late > 0 && i_sent > i_date + p_thread->i_late )
516 if( !i_dropped_packets )
518 msg_Dbg( p_thread, "late packet to send (" I64Fd ") -> drop",
521 block_Release( p_pk );
522 i_date_last = i_date;
528 if ( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
531 i_to_send = p_thread->i_group;
533 send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 );
535 if( i_dropped_packets )
537 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
538 i_dropped_packets = 0;
543 if ( i_sent > i_date + 20000 )
545 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
550 block_Release( p_pk );
551 i_date_last = i_date;