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 CACHING_TEXT N_("Caching value (ms)")
62 #define CACHING_LONGTEXT N_( \
63 "Allows you to modify the default caching value for udp streams. This " \
64 "value should be set in millisecond units." )
67 set_description( _("UDP stream ouput") );
68 add_integer( "udp-sout-caching", DEFAULT_PTS_DELAY / 1000, NULL,
69 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
70 set_capability( "sout access", 100 );
71 add_shortcut( "udp" );
72 add_shortcut( "rtp" ); // Will work only with ts muxer
73 set_callbacks( Open, Close );
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Write ( sout_access_out_t *, block_t * );
80 static int WriteRaw( sout_access_out_t *, block_t * );
81 static int Seek ( sout_access_out_t *, off_t );
83 static void ThreadWrite( vlc_object_t * );
85 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
88 typedef struct sout_access_thread_t
92 sout_instance_t *p_sout;
102 } sout_access_thread_t;
104 struct sout_access_out_sys_t
106 int b_rtpts; // 1 if add rtp/ts header
107 uint16_t i_sequence_number;
114 sout_access_thread_t *p_thread;
118 #define DEFAULT_PORT 1234
120 /*****************************************************************************
121 * Open: open the file
122 *****************************************************************************/
123 static int Open( vlc_object_t *p_this )
125 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
126 sout_access_out_sys_t *p_sys;
133 network_socket_t socket_desc;
138 if( !( p_sys = p_access->p_sys =
139 malloc( sizeof( sout_access_out_sys_t ) ) ) )
141 msg_Err( p_access, "not enough memory" );
146 if( p_access->psz_access != NULL &&
147 !strcmp( p_access->psz_access, "rtp" ) )
149 msg_Warn( p_access, "be careful that rtp output only works with ts "
150 "payload (not an error)" );
158 psz_parser = strdup( p_access->psz_name );
160 psz_dst_addr = psz_parser;
163 if ( *psz_parser == '[' )
165 while( *psz_parser && *psz_parser != ']' )
170 while( *psz_parser && *psz_parser != ':' )
174 if( *psz_parser == ':' )
178 i_dst_port = atoi( psz_parser );
180 if( i_dst_port <= 0 )
182 i_dst_port = DEFAULT_PORT;
186 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
187 if( !p_sys->p_thread )
189 msg_Err( p_access, "out of memory" );
193 p_sys->p_thread->p_sout = p_access->p_sout;
194 p_sys->p_thread->b_die = 0;
195 p_sys->p_thread->b_error= 0;
196 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
198 socket_desc.i_type = NETWORK_UDP;
199 socket_desc.psz_server_addr = psz_dst_addr;
200 socket_desc.i_server_port = i_dst_port;
201 socket_desc.psz_bind_addr = "";
202 socket_desc.i_bind_port = 0;
203 socket_desc.i_ttl = 0;
204 if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "ttl" ) ) )
206 socket_desc.i_ttl = atoi( psz_val );
208 p_sys->p_thread->p_private = (void*)&socket_desc;
209 if( !( p_network = module_Need( p_sys->p_thread, "network", NULL, 0 ) ) )
211 msg_Err( p_access, "failed to open a connection (udp)" );
214 module_Unneed( p_sys->p_thread, p_network );
216 p_sys->p_thread->i_handle = socket_desc.i_handle;
218 var_Create( p_this, "udp-sout-caching",
219 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
220 var_Get( p_this, "udp-sout-caching", &val );
221 p_sys->p_thread->i_caching = val.i_int * 1000;
222 if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "caching" ) ) )
224 p_sys->p_thread->i_caching = atoll( psz_val ) * 1000;
227 p_sys->p_thread->i_group = 1;
228 if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "group" ) ) )
230 p_sys->p_thread->i_group = atoi( psz_val );
233 p_sys->p_thread->i_late = 0;
234 if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "late" ) ) )
236 p_sys->p_thread->i_late = atoll( psz_val ) * 1000;
240 p_sys->i_mtu = socket_desc.i_mtu;
243 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
244 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
246 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
247 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
250 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
251 vlc_object_destroy( p_sys->p_thread );
255 srand( (uint32_t)mdate());
256 p_sys->p_buffer = NULL;
257 p_sys->i_sequence_number = rand()&0xffff;
258 p_sys->i_ssrc = rand()&0xffffffff;
260 if( sout_cfg_find( p_access->p_cfg, "raw" ) )
262 p_access->pf_write = WriteRaw;
266 p_access->pf_write = Write;
269 p_access->pf_seek = Seek;
271 msg_Dbg( p_access, "udp access output opened(%s:%d)", psz_dst_addr, i_dst_port );
273 free( psz_dst_addr );
275 /* update p_sout->i_out_pace_nocontrol */
276 p_access->p_sout->i_out_pace_nocontrol++;
281 /*****************************************************************************
282 * Close: close the target
283 *****************************************************************************/
284 static void Close( vlc_object_t * p_this )
286 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
287 sout_access_out_sys_t *p_sys = p_access->p_sys;
290 p_sys->p_thread->b_die = 1;
291 for( i = 0; i < 10; i++ )
293 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
296 p_dummy->i_length = 0;
297 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
299 vlc_thread_join( p_sys->p_thread );
301 block_FifoRelease( p_sys->p_thread->p_fifo );
303 if( p_sys->p_buffer )
305 block_Release( p_sys->p_buffer );
308 net_Close( p_sys->p_thread->i_handle );
310 /* update p_sout->i_out_pace_nocontrol */
311 p_access->p_sout->i_out_pace_nocontrol--;
313 msg_Dbg( p_access, "udp access output closed" );
317 /*****************************************************************************
318 * Write: standard write on a file descriptor.
319 *****************************************************************************/
320 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
322 sout_access_out_sys_t *p_sys = p_access->p_sys;
323 unsigned int i_write;
328 if( p_buffer->i_buffer > p_sys->i_mtu )
330 msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
331 i_write = p_sys->i_mtu;
335 i_write = p_buffer->i_buffer;
338 /* if we have enough data, enque the buffer */
339 if( p_sys->p_buffer &&
340 p_sys->p_buffer->i_buffer + i_write > p_sys->i_mtu )
342 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
343 p_sys->p_buffer = NULL;
346 if( !p_sys->p_buffer )
348 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
351 if( p_buffer->i_buffer > 0 )
353 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
354 p_buffer->p_buffer, i_write );
355 p_sys->p_buffer->i_buffer += i_write;
357 p_next = p_buffer->p_next;
358 block_Release( p_buffer );
362 return( p_sys->p_thread->b_error ? -1 : 0 );
365 /*****************************************************************************
366 * WriteRaw: write p_buffer without trying to fill mtu
367 *****************************************************************************/
368 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
370 sout_access_out_sys_t *p_sys = p_access->p_sys;
372 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
374 return( p_sys->p_thread->b_error ? -1 : 0 );
377 /*****************************************************************************
378 * Seek: seek to a specific location in a file
379 *****************************************************************************/
380 static int Seek( sout_access_out_t *p_access, off_t i_pos )
382 msg_Err( p_access, "UDP sout access cannot seek" );
386 /*****************************************************************************
387 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
388 *****************************************************************************/
389 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
391 sout_access_out_sys_t *p_sys = p_access->p_sys;
394 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
395 p_buffer->i_dts = i_dts;
396 p_buffer->i_buffer = 0;
400 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
402 /* add rtp/ts header */
403 p_buffer->p_buffer[0] = 0x80;
404 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
406 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
407 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
408 p_sys->i_sequence_number++;
410 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
411 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
412 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
413 p_buffer->p_buffer[7] = i_timestamp&0xff;
415 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
416 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
417 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
418 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
420 p_buffer->i_buffer = 12;
426 /*****************************************************************************
427 * ThreadWrite: Write a packet on the network at the good time.
428 *****************************************************************************/
429 static void ThreadWrite( vlc_object_t *p_this )
431 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
432 mtime_t i_date_last = -1;
433 mtime_t i_to_send = p_thread->i_group;
434 int i_dropped_packets = 0;
436 while( !p_thread->b_die )
439 mtime_t i_date, i_sent;
441 p_pk = block_FifoGet( p_thread->p_fifo );
443 i_date = p_thread->i_caching + p_pk->i_dts;
444 if( i_date_last > 0 )
446 if( i_date - i_date_last > 2000000 )
448 if( !i_dropped_packets )
449 msg_Dbg( p_thread, "mmh, hole > 2s -> drop" );
451 block_Release( p_pk );
452 i_date_last = i_date;
456 else if( i_date - i_date_last < 0 )
458 if( !i_dropped_packets )
459 msg_Dbg( p_thread, "mmh, paquets in the past -> drop" );
461 block_Release( p_pk );
462 i_date_last = i_date;
469 if( p_thread->i_late > 0 && i_sent > i_date + p_thread->i_late )
471 if( !i_dropped_packets )
473 msg_Dbg( p_thread, "late packet to send (" I64Fd ") -> drop",
476 block_Release( p_pk );
477 i_date_last = i_date;
486 i_to_send = p_thread->i_group;
488 send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 );
490 if( i_dropped_packets )
492 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
493 i_dropped_packets = 0;
498 if ( i_sent > i_date + 20000 )
500 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
505 block_Release( p_pk );
506 i_date_last = i_date;