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 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
91 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL,TTL_TEXT, TTL_LONGTEXT,
93 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
95 add_integer( SOUT_CFG_PREFIX "late", 0, NULL, LATE_TEXT, LATE_LONGTEXT,
97 add_bool( SOUT_CFG_PREFIX "raw", 0, NULL, RAW_TEXT, RAW_LONGTEXT,
100 set_capability( "sout access", 100 );
101 add_shortcut( "udp" );
102 add_shortcut( "rtp" ); // Will work only with ts muxer
103 set_callbacks( Open, Close );
106 /*****************************************************************************
107 * Exported prototypes
108 *****************************************************************************/
110 static const char *ppsz_sout_options[] = {
119 static int Write ( sout_access_out_t *, block_t * );
120 static int WriteRaw( sout_access_out_t *, block_t * );
121 static int Seek ( sout_access_out_t *, off_t );
123 static void ThreadWrite( vlc_object_t * );
124 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
126 typedef struct sout_access_thread_t
130 sout_instance_t *p_sout;
132 block_fifo_t *p_fifo;
140 } sout_access_thread_t;
142 struct sout_access_out_sys_t
144 int b_rtpts; // 1 if add rtp/ts header
145 uint16_t i_sequence_number;
152 sout_access_thread_t *p_thread;
154 vlc_bool_t b_mtu_warning;
157 #define DEFAULT_PORT 1234
159 /*****************************************************************************
160 * Open: open the file
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
165 sout_access_out_sys_t *p_sys;
172 network_socket_t socket_desc;
176 sout_CfgParse( p_access, SOUT_CFG_PREFIX,
177 ppsz_sout_options, p_access->p_cfg );
179 if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
181 msg_Err( p_access, "not enough memory" );
184 memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
185 p_access->p_sys = p_sys;
187 if( p_access->psz_access != NULL &&
188 !strcmp( p_access->psz_access, "rtp" ) )
190 msg_Warn( p_access, "be careful that rtp output only works with ts "
191 "payload (not an error)" );
199 psz_parser = strdup( p_access->psz_name );
201 psz_dst_addr = psz_parser;
204 if ( *psz_parser == '[' )
206 while( *psz_parser && *psz_parser != ']' )
211 while( *psz_parser && *psz_parser != ':' )
215 if( *psz_parser == ':' )
219 i_dst_port = atoi( psz_parser );
221 if( i_dst_port <= 0 )
223 i_dst_port = DEFAULT_PORT;
227 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
228 if( !p_sys->p_thread )
230 msg_Err( p_access, "out of memory" );
234 p_sys->p_thread->p_sout = p_access->p_sout;
235 p_sys->p_thread->b_die = 0;
236 p_sys->p_thread->b_error= 0;
237 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
239 socket_desc.i_type = NETWORK_UDP;
240 socket_desc.psz_server_addr = psz_dst_addr;
241 socket_desc.i_server_port = i_dst_port;
242 socket_desc.psz_bind_addr = "";
243 socket_desc.i_bind_port = 0;
245 var_Get( p_access, SOUT_CFG_PREFIX "ttl", &val );
246 socket_desc.i_ttl = val.i_int;
248 p_sys->p_thread->p_private = (void*)&socket_desc;
249 if( !( p_network = module_Need( p_sys->p_thread, "network", NULL, 0 ) ) )
251 msg_Err( p_access, "failed to open a connection (udp)" );
254 module_Unneed( p_sys->p_thread, p_network );
256 p_sys->p_thread->i_handle = socket_desc.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 var_Get( p_access, SOUT_CFG_PREFIX "late", &val );
265 p_sys->p_thread->i_late = (int64_t)val.i_int * 1000;
267 p_sys->i_mtu = socket_desc.i_mtu;
270 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
271 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
273 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
274 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
277 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
278 vlc_object_destroy( p_sys->p_thread );
282 srand( (uint32_t)mdate());
283 p_sys->p_buffer = NULL;
284 p_sys->i_sequence_number = rand()&0xffff;
285 p_sys->i_ssrc = rand()&0xffffffff;
287 var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
288 if( val.b_bool ) p_access->pf_write = WriteRaw;
289 else p_access->pf_write = Write;
291 p_access->pf_seek = Seek;
293 msg_Dbg( p_access, "udp access output opened(%s:%d)",
294 psz_dst_addr, i_dst_port );
296 free( psz_dst_addr );
298 /* update p_sout->i_out_pace_nocontrol */
299 p_access->p_sout->i_out_pace_nocontrol++;
304 /*****************************************************************************
305 * Close: close the target
306 *****************************************************************************/
307 static void Close( vlc_object_t * p_this )
309 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
310 sout_access_out_sys_t *p_sys = p_access->p_sys;
313 p_sys->p_thread->b_die = 1;
314 for( i = 0; i < 10; i++ )
316 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
319 p_dummy->i_length = 0;
320 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
322 vlc_thread_join( p_sys->p_thread );
324 block_FifoRelease( p_sys->p_thread->p_fifo );
326 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
328 net_Close( p_sys->p_thread->i_handle );
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 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
361 p_sys->p_buffer = NULL;
364 while( p_buffer->i_buffer )
366 int i_write = __MIN( p_buffer->i_buffer, p_sys->i_mtu );
370 if( !p_sys->p_buffer )
372 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
373 if( !p_sys->p_buffer ) break;
376 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
377 p_buffer->p_buffer, i_write );
379 p_sys->p_buffer->i_buffer += i_write;
380 p_buffer->p_buffer += i_write;
381 p_buffer->i_buffer -= i_write;
383 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
386 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
387 p_sys->p_buffer = NULL;
391 p_next = p_buffer->p_next;
392 block_Release( p_buffer );
396 return( p_sys->p_thread->b_error ? -1 : 0 );
399 /*****************************************************************************
400 * WriteRaw: write p_buffer without trying to fill mtu
401 *****************************************************************************/
402 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
404 sout_access_out_sys_t *p_sys = p_access->p_sys;
406 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
408 return( p_sys->p_thread->b_error ? -1 : 0 );
411 /*****************************************************************************
412 * Seek: seek to a specific location in a file
413 *****************************************************************************/
414 static int Seek( sout_access_out_t *p_access, off_t i_pos )
416 msg_Err( p_access, "UDP sout access cannot seek" );
420 /*****************************************************************************
421 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
422 *****************************************************************************/
423 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
425 sout_access_out_sys_t *p_sys = p_access->p_sys;
428 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
429 p_buffer->i_dts = i_dts;
430 p_buffer->i_buffer = 0;
434 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
436 /* add rtp/ts header */
437 p_buffer->p_buffer[0] = 0x80;
438 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
440 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
441 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
442 p_sys->i_sequence_number++;
444 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
445 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
446 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
447 p_buffer->p_buffer[7] = i_timestamp&0xff;
449 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
450 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
451 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
452 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
454 p_buffer->i_buffer = 12;
460 /*****************************************************************************
461 * ThreadWrite: Write a packet on the network at the good time.
462 *****************************************************************************/
463 static void ThreadWrite( vlc_object_t *p_this )
465 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
466 mtime_t i_date_last = -1;
467 mtime_t i_to_send = p_thread->i_group;
468 int i_dropped_packets = 0;
470 while( !p_thread->b_die )
473 mtime_t i_date, i_sent;
475 p_pk = block_FifoGet( p_thread->p_fifo );
477 i_date = p_thread->i_caching + p_pk->i_dts;
478 if( i_date_last > 0 )
480 if( i_date - i_date_last > 2000000 )
482 if( !i_dropped_packets )
483 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
484 i_date - i_date_last );
486 block_Release( p_pk );
487 i_date_last = i_date;
491 else if( i_date - i_date_last < 0 )
493 if( !i_dropped_packets )
494 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")"
495 " -> drop", i_date - i_date_last );
497 block_Release( p_pk );
498 i_date_last = i_date;
505 if( p_thread->i_late > 0 && i_sent > i_date + p_thread->i_late )
507 if( !i_dropped_packets )
509 msg_Dbg( p_thread, "late packet to send (" I64Fd ") -> drop",
512 block_Release( p_pk );
513 i_date_last = i_date;
522 i_to_send = p_thread->i_group;
524 send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 );
526 if( i_dropped_packets )
528 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
529 i_dropped_packets = 0;
534 if ( i_sent > i_date + 20000 )
536 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
541 block_Release( p_pk );
542 i_date_last = i_date;