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 * );
125 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;
158 #define DEFAULT_PORT 1234
160 /*****************************************************************************
161 * Open: open the file
162 *****************************************************************************/
163 static int Open( vlc_object_t *p_this )
165 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
166 sout_access_out_sys_t *p_sys;
173 network_socket_t socket_desc;
177 sout_ParseCfg( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
179 if( !( p_sys = p_access->p_sys =
180 malloc( sizeof( sout_access_out_sys_t ) ) ) )
182 msg_Err( p_access, "not enough memory" );
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 );
290 p_access->pf_write = WriteRaw;
294 p_access->pf_write = Write;
297 p_access->pf_seek = Seek;
299 msg_Dbg( p_access, "udp access output opened(%s:%d)", psz_dst_addr, i_dst_port );
301 free( psz_dst_addr );
303 /* update p_sout->i_out_pace_nocontrol */
304 p_access->p_sout->i_out_pace_nocontrol++;
309 /*****************************************************************************
310 * Close: close the target
311 *****************************************************************************/
312 static void Close( vlc_object_t * p_this )
314 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
315 sout_access_out_sys_t *p_sys = p_access->p_sys;
318 p_sys->p_thread->b_die = 1;
319 for( i = 0; i < 10; i++ )
321 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
324 p_dummy->i_length = 0;
325 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
327 vlc_thread_join( p_sys->p_thread );
329 block_FifoRelease( p_sys->p_thread->p_fifo );
331 if( p_sys->p_buffer )
333 block_Release( p_sys->p_buffer );
336 net_Close( p_sys->p_thread->i_handle );
338 /* update p_sout->i_out_pace_nocontrol */
339 p_access->p_sout->i_out_pace_nocontrol--;
341 msg_Dbg( p_access, "udp access output closed" );
345 /*****************************************************************************
346 * Write: standard write on a file descriptor.
347 *****************************************************************************/
348 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
350 sout_access_out_sys_t *p_sys = p_access->p_sys;
351 unsigned int i_write;
356 if( p_buffer->i_buffer > p_sys->i_mtu )
358 msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
359 i_write = p_sys->i_mtu;
363 i_write = p_buffer->i_buffer;
366 /* if we have enough data, enque the buffer */
367 if( p_sys->p_buffer &&
368 p_sys->p_buffer->i_buffer + i_write > p_sys->i_mtu )
370 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
371 p_sys->p_buffer = NULL;
374 if( !p_sys->p_buffer )
376 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
379 if( p_buffer->i_buffer > 0 )
381 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
382 p_buffer->p_buffer, i_write );
383 p_sys->p_buffer->i_buffer += i_write;
385 p_next = p_buffer->p_next;
386 block_Release( p_buffer );
390 return( p_sys->p_thread->b_error ? -1 : 0 );
393 /*****************************************************************************
394 * WriteRaw: write p_buffer without trying to fill mtu
395 *****************************************************************************/
396 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
398 sout_access_out_sys_t *p_sys = p_access->p_sys;
400 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
402 return( p_sys->p_thread->b_error ? -1 : 0 );
405 /*****************************************************************************
406 * Seek: seek to a specific location in a file
407 *****************************************************************************/
408 static int Seek( sout_access_out_t *p_access, off_t i_pos )
410 msg_Err( p_access, "UDP sout access cannot seek" );
414 /*****************************************************************************
415 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
416 *****************************************************************************/
417 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
419 sout_access_out_sys_t *p_sys = p_access->p_sys;
422 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
423 p_buffer->i_dts = i_dts;
424 p_buffer->i_buffer = 0;
428 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
430 /* add rtp/ts header */
431 p_buffer->p_buffer[0] = 0x80;
432 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
434 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
435 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
436 p_sys->i_sequence_number++;
438 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
439 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
440 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
441 p_buffer->p_buffer[7] = i_timestamp&0xff;
443 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
444 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
445 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
446 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
448 p_buffer->i_buffer = 12;
454 /*****************************************************************************
455 * ThreadWrite: Write a packet on the network at the good time.
456 *****************************************************************************/
457 static void ThreadWrite( vlc_object_t *p_this )
459 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
460 mtime_t i_date_last = -1;
461 mtime_t i_to_send = p_thread->i_group;
462 int i_dropped_packets = 0;
464 while( !p_thread->b_die )
467 mtime_t i_date, i_sent;
469 p_pk = block_FifoGet( p_thread->p_fifo );
471 i_date = p_thread->i_caching + p_pk->i_dts;
472 if( i_date_last > 0 )
474 if( i_date - i_date_last > 2000000 )
476 if( !i_dropped_packets )
477 msg_Dbg( p_thread, "mmh, hole > 2s -> drop" );
479 block_Release( p_pk );
480 i_date_last = i_date;
484 else if( i_date - i_date_last < 0 )
486 if( !i_dropped_packets )
487 msg_Dbg( p_thread, "mmh, paquets in the past -> drop" );
489 block_Release( p_pk );
490 i_date_last = i_date;
497 if( p_thread->i_late > 0 && i_sent > i_date + p_thread->i_late )
499 if( !i_dropped_packets )
501 msg_Dbg( p_thread, "late packet to send (" I64Fd ") -> drop",
504 block_Release( p_pk );
505 i_date_last = i_date;
514 i_to_send = p_thread->i_group;
516 send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 );
518 if( i_dropped_packets )
520 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
521 i_dropped_packets = 0;
526 if ( i_sent > i_date + 20000 )
528 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
533 block_Release( p_pk );
534 i_date_last = i_date;