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_category( CAT_SOUT );
91 set_subcategory( SUBCAT_SOUT_ACO );
92 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
93 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL,TTL_TEXT, TTL_LONGTEXT,
95 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
97 add_integer( SOUT_CFG_PREFIX "late", 0, NULL, LATE_TEXT, LATE_LONGTEXT,
99 add_bool( SOUT_CFG_PREFIX "raw", 0, NULL, RAW_TEXT, RAW_LONGTEXT,
102 set_capability( "sout access", 100 );
103 add_shortcut( "udp" );
104 add_shortcut( "rtp" ); // Will work only with ts muxer
105 set_callbacks( Open, Close );
108 /*****************************************************************************
109 * Exported prototypes
110 *****************************************************************************/
112 static const char *ppsz_sout_options[] = {
121 static int Write ( sout_access_out_t *, block_t * );
122 static int WriteRaw( sout_access_out_t *, block_t * );
123 static int Seek ( sout_access_out_t *, off_t );
125 static void ThreadWrite( vlc_object_t * );
126 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
128 typedef struct sout_access_thread_t
132 sout_instance_t *p_sout;
134 block_fifo_t *p_fifo;
142 } sout_access_thread_t;
144 struct sout_access_out_sys_t
146 int b_rtpts; // 1 if add rtp/ts header
147 uint16_t i_sequence_number;
154 sout_access_thread_t *p_thread;
156 vlc_bool_t b_mtu_warning;
159 #define DEFAULT_PORT 1234
161 /*****************************************************************************
162 * Open: open the file
163 *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
166 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
167 sout_access_out_sys_t *p_sys;
174 network_socket_t socket_desc;
178 sout_CfgParse( p_access, SOUT_CFG_PREFIX,
179 ppsz_sout_options, p_access->p_cfg );
181 if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
183 msg_Err( p_access, "not enough memory" );
186 memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
187 p_access->p_sys = p_sys;
189 if( p_access->psz_access != NULL &&
190 !strcmp( p_access->psz_access, "rtp" ) )
192 msg_Warn( p_access, "be careful that rtp output only works with ts "
193 "payload (not an error)" );
201 psz_parser = strdup( p_access->psz_name );
203 psz_dst_addr = psz_parser;
206 if ( *psz_parser == '[' )
208 while( *psz_parser && *psz_parser != ']' )
213 while( *psz_parser && *psz_parser != ':' )
217 if( *psz_parser == ':' )
221 i_dst_port = atoi( psz_parser );
223 if( i_dst_port <= 0 )
225 i_dst_port = DEFAULT_PORT;
229 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
230 if( !p_sys->p_thread )
232 msg_Err( p_access, "out of memory" );
236 p_sys->p_thread->p_sout = p_access->p_sout;
237 p_sys->p_thread->b_die = 0;
238 p_sys->p_thread->b_error= 0;
239 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
241 socket_desc.i_type = NETWORK_UDP;
242 socket_desc.psz_server_addr = psz_dst_addr;
243 socket_desc.i_server_port = i_dst_port;
244 socket_desc.psz_bind_addr = "";
245 socket_desc.i_bind_port = 0;
247 var_Get( p_access, SOUT_CFG_PREFIX "ttl", &val );
248 socket_desc.i_ttl = val.i_int;
250 p_sys->p_thread->p_private = (void*)&socket_desc;
251 if( !( p_network = module_Need( p_sys->p_thread, "network", NULL, 0 ) ) )
253 msg_Err( p_access, "failed to open a connection (udp)" );
256 module_Unneed( p_sys->p_thread, p_network );
258 p_sys->p_thread->i_handle = socket_desc.i_handle;
260 var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
261 p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
263 var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
264 p_sys->p_thread->i_group = val.i_int;
266 var_Get( p_access, SOUT_CFG_PREFIX "late", &val );
267 p_sys->p_thread->i_late = (int64_t)val.i_int * 1000;
269 p_sys->i_mtu = socket_desc.i_mtu;
272 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
273 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
275 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
276 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
279 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
280 vlc_object_destroy( p_sys->p_thread );
284 srand( (uint32_t)mdate());
285 p_sys->p_buffer = NULL;
286 p_sys->i_sequence_number = rand()&0xffff;
287 p_sys->i_ssrc = rand()&0xffffffff;
289 var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
290 if( val.b_bool ) p_access->pf_write = WriteRaw;
291 else p_access->pf_write = Write;
293 p_access->pf_seek = Seek;
295 msg_Dbg( p_access, "udp access output opened(%s:%d)",
296 psz_dst_addr, i_dst_port );
298 free( psz_dst_addr );
300 /* update p_sout->i_out_pace_nocontrol */
301 p_access->p_sout->i_out_pace_nocontrol++;
306 /*****************************************************************************
307 * Close: close the target
308 *****************************************************************************/
309 static void Close( vlc_object_t * p_this )
311 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
312 sout_access_out_sys_t *p_sys = p_access->p_sys;
315 p_sys->p_thread->b_die = 1;
316 for( i = 0; i < 10; i++ )
318 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
321 p_dummy->i_length = 0;
322 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
324 vlc_thread_join( p_sys->p_thread );
326 block_FifoRelease( p_sys->p_thread->p_fifo );
328 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
330 net_Close( p_sys->p_thread->i_handle );
332 /* update p_sout->i_out_pace_nocontrol */
333 p_access->p_sout->i_out_pace_nocontrol--;
335 msg_Dbg( p_access, "udp access output closed" );
339 /*****************************************************************************
340 * Write: standard write on a file descriptor.
341 *****************************************************************************/
342 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
344 sout_access_out_sys_t *p_sys = p_access->p_sys;
351 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
353 msg_Warn( p_access, "packet size > MTU, you should probably "
354 "increase the MTU" );
355 p_sys->b_mtu_warning = VLC_TRUE;
358 /* Check if there is enough space in the buffer */
359 if( p_sys->p_buffer &&
360 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
362 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
363 p_sys->p_buffer = NULL;
366 while( p_buffer->i_buffer )
368 int i_write = __MIN( p_buffer->i_buffer, p_sys->i_mtu );
372 if( !p_sys->p_buffer )
374 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
375 if( !p_sys->p_buffer ) break;
378 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
379 p_buffer->p_buffer, i_write );
381 p_sys->p_buffer->i_buffer += i_write;
382 p_buffer->p_buffer += i_write;
383 p_buffer->i_buffer -= i_write;
385 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
388 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
389 p_sys->p_buffer = NULL;
393 p_next = p_buffer->p_next;
394 block_Release( p_buffer );
398 return( p_sys->p_thread->b_error ? -1 : 0 );
401 /*****************************************************************************
402 * WriteRaw: write p_buffer without trying to fill mtu
403 *****************************************************************************/
404 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
406 sout_access_out_sys_t *p_sys = p_access->p_sys;
408 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
410 return( p_sys->p_thread->b_error ? -1 : 0 );
413 /*****************************************************************************
414 * Seek: seek to a specific location in a file
415 *****************************************************************************/
416 static int Seek( sout_access_out_t *p_access, off_t i_pos )
418 msg_Err( p_access, "UDP sout access cannot seek" );
422 /*****************************************************************************
423 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
424 *****************************************************************************/
425 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
427 sout_access_out_sys_t *p_sys = p_access->p_sys;
430 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
431 p_buffer->i_dts = i_dts;
432 p_buffer->i_buffer = 0;
436 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
438 /* add rtp/ts header */
439 p_buffer->p_buffer[0] = 0x80;
440 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
442 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
443 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
444 p_sys->i_sequence_number++;
446 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
447 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
448 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
449 p_buffer->p_buffer[7] = i_timestamp&0xff;
451 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
452 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
453 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
454 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
456 p_buffer->i_buffer = 12;
462 /*****************************************************************************
463 * ThreadWrite: Write a packet on the network at the good time.
464 *****************************************************************************/
465 static void ThreadWrite( vlc_object_t *p_this )
467 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
468 mtime_t i_date_last = -1;
469 mtime_t i_to_send = p_thread->i_group;
470 int i_dropped_packets = 0;
472 while( !p_thread->b_die )
475 mtime_t i_date, i_sent;
477 p_pk = block_FifoGet( p_thread->p_fifo );
479 i_date = p_thread->i_caching + p_pk->i_dts;
480 if( i_date_last > 0 )
482 if( i_date - i_date_last > 2000000 )
484 if( !i_dropped_packets )
485 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
486 i_date - i_date_last );
488 block_Release( p_pk );
489 i_date_last = i_date;
493 else if( i_date - i_date_last < 0 )
495 if( !i_dropped_packets )
496 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")"
497 " -> drop", i_date - i_date_last );
499 block_Release( p_pk );
500 i_date_last = i_date;
507 if( p_thread->i_late > 0 && i_sent > i_date + p_thread->i_late )
509 if( !i_dropped_packets )
511 msg_Dbg( p_thread, "late packet to send (" I64Fd ") -> drop",
514 block_Release( p_pk );
515 i_date_last = i_date;
524 i_to_send = p_thread->i_group;
526 send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 );
528 if( i_dropped_packets )
530 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
531 i_dropped_packets = 0;
536 if ( i_sent > i_date + 20000 )
538 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
543 block_Release( p_pk );
544 i_date_last = i_date;