1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.9 2003/06/19 18:22:05 gbazin Exp $
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>
36 #include <vlc/input.h>
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
50 # include <sys/socket.h>
55 #define DEFAULT_PORT 1234
56 #define LATENCY 100000
57 #define MAX_ERROR 500000
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close ( vlc_object_t * );
64 static int Write( sout_access_out_t *, sout_buffer_t * );
65 static int Seek ( sout_access_out_t *, off_t );
67 static void ThreadWrite( vlc_object_t * );
69 static sout_buffer_t *NewUDPPacket( sout_access_out_t *, mtime_t );
71 /*****************************************************************************
73 *****************************************************************************/
74 #define CACHING_TEXT N_("caching value in ms")
75 #define CACHING_LONGTEXT N_( \
76 "Allows you to modify the default caching value for udp streams. This " \
77 "value should be set in miliseconds units." )
80 set_description( _("UDP stream ouput") );
81 add_category_hint( N_("udp stream output"), NULL , VLC_TRUE );
82 add_integer( "udp-sout-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
83 set_capability( "sout access", 100 );
84 add_shortcut( "udp" );
85 add_shortcut( "rtp" ); // Will work only with ts muxer
86 set_callbacks( Open, Close );
89 typedef struct sout_access_thread_s
93 sout_instance_t *p_sout;
99 } sout_access_thread_t;
101 struct sout_access_out_sys_t
103 int b_rtpts; // 1 if add rtp/ts header
104 uint16_t i_sequence_number;
109 sout_buffer_t *p_buffer;
111 sout_access_thread_t *p_thread;
115 /*****************************************************************************
116 * Open: open the file
117 *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
120 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
121 sout_access_out_sys_t *p_sys;
128 network_socket_t socket_desc;
130 if( !( p_sys = p_access->p_sys =
131 malloc( sizeof( sout_access_out_sys_t ) ) ) )
133 msg_Err( p_access, "Not enough memory" );
134 return( VLC_EGENERIC );
138 if( p_access->psz_access != NULL &&
139 !strcmp( p_access->psz_access, "rtp" ) )
141 msg_Warn( p_access, "becarefull that rtp ouput work only with ts "
142 "payload(not an error)" );
150 psz_parser = strdup( p_access->psz_name );
152 psz_dst_addr = psz_parser;
155 if ( *psz_parser == '[' )
157 while( *psz_parser && *psz_parser != ']' )
162 while( *psz_parser && *psz_parser != ':' )
166 if( *psz_parser == ':' )
170 i_dst_port = atoi( psz_parser );
172 if( i_dst_port <= 0 )
174 i_dst_port = DEFAULT_PORT;
178 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
179 if( !p_sys->p_thread )
181 msg_Err( p_access, "out of memory" );
182 return( VLC_EGENERIC );
185 p_sys->p_thread->p_sout = p_access->p_sout;
186 p_sys->p_thread->b_die = 0;
187 p_sys->p_thread->b_error= 0;
188 p_sys->p_thread->p_fifo = sout_FifoCreate( p_access->p_sout );
190 socket_desc.i_type = NETWORK_UDP;
191 socket_desc.psz_server_addr = psz_dst_addr;
192 socket_desc.i_server_port = i_dst_port;
193 socket_desc.psz_bind_addr = "";
194 socket_desc.i_bind_port = 0;
195 p_sys->p_thread->p_private = (void*)&socket_desc;
196 if( !( p_network = module_Need( p_sys->p_thread,
199 msg_Err( p_access, "failed to open a connection (udp)" );
200 return( VLC_EGENERIC );
202 module_Unneed( p_sys->p_thread, p_network );
204 p_sys->p_thread->i_handle = socket_desc.i_handle;
205 p_sys->i_mtu = socket_desc.i_mtu;
207 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
208 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
210 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
211 vlc_object_destroy( p_sys->p_thread );
212 return( VLC_EGENERIC );
215 srand( (uint32_t)mdate());
216 p_sys->p_buffer = NULL;
217 p_sys->i_sequence_number = rand()&0xffff;
218 p_sys->i_ssrc = rand()&0xffffffff;
220 p_access->pf_write = Write;
221 p_access->pf_seek = Seek;
223 msg_Info( p_access, "Open: addr:`%s' port:`%d'",
224 psz_dst_addr, i_dst_port );
226 free( psz_dst_addr );
230 /*****************************************************************************
231 * Close: close the target
232 *****************************************************************************/
233 static void Close( vlc_object_t * p_this )
235 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
236 sout_access_out_sys_t *p_sys = p_access->p_sys;
239 p_sys->p_thread->b_die = 1;
240 for( i = 0; i < 10; i++ )
242 sout_buffer_t *p_dummy;
244 p_dummy = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
247 p_dummy->i_length = 0;
248 sout_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
250 vlc_thread_join( p_sys->p_thread );
252 sout_FifoDestroy( p_access->p_sout, p_sys->p_thread->p_fifo );
254 if( p_sys->p_buffer )
256 sout_BufferDelete( p_access->p_sout, p_sys->p_buffer );
259 #if defined( UNDER_CE )
260 CloseHandle( (HANDLE)p_sys->p_thread->i_handle );
261 #elif defined( WIN32 )
262 closesocket( p_sys->p_thread->i_handle );
264 close( p_sys->p_thread->i_handle );
268 msg_Info( p_access, "Close" );
271 /*****************************************************************************
272 * Read: standard read on a file descriptor.
273 *****************************************************************************/
274 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
276 sout_access_out_sys_t *p_sys = p_access->p_sys;
277 unsigned int i_write;
281 sout_buffer_t *p_next;
282 if( p_buffer->i_size > p_sys->i_mtu )
284 msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
285 i_write = p_sys->i_mtu;
289 i_write = p_buffer->i_size;
292 /* if we have enough data, enque the buffer */
293 if( p_sys->p_buffer &&
294 p_sys->p_buffer->i_size + i_write > p_sys->i_mtu )
296 sout_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
297 p_sys->p_buffer = NULL;
300 if( !p_sys->p_buffer )
302 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
305 if( p_buffer->i_size > 0 )
307 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_size,
308 p_buffer->p_buffer, i_write );
309 p_sys->p_buffer->i_size += i_write;
311 p_next = p_buffer->p_next;
312 sout_BufferDelete( p_access->p_sout, p_buffer );
316 return( p_sys->p_thread->b_error ? -1 : 0 );
319 /*****************************************************************************
320 * Seek: seek to a specific location in a file
321 *****************************************************************************/
322 static int Seek( sout_access_out_t *p_access, off_t i_pos )
325 msg_Err( p_access, "udp sout access cannot seek" );
329 /*****************************************************************************
330 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
331 *****************************************************************************/
332 static sout_buffer_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
334 sout_access_out_sys_t *p_sys = p_access->p_sys;
335 sout_buffer_t *p_buffer;
337 p_buffer = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
338 p_buffer->i_dts = i_dts;
339 p_buffer->i_size = 0;
343 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
345 /* add rtp/ts header */
346 p_buffer->p_buffer[0] = 0x80;
347 p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
349 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
350 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
351 p_sys->i_sequence_number++;
353 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
354 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
355 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
356 p_buffer->p_buffer[7] = i_timestamp&0xff;
358 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
359 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
360 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
361 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
363 p_buffer->i_size = 12;
369 /*****************************************************************************
370 * ThreadWrite: Write a packet on the network at the good time.
371 *****************************************************************************/
372 static void ThreadWrite( vlc_object_t *p_this )
374 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
375 sout_instance_t *p_sout = p_thread->p_sout;
376 sout_buffer_t *p_buffer;
377 mtime_t i_date, i_pts_delay;
379 /* Get the i_pts_delay value */
380 i_pts_delay = config_GetInt( p_this, "udp-sout-caching" ) * 1000;
382 while( !p_thread->b_die && p_thread->p_fifo->i_depth < 5 )
386 if( p_thread->b_die )
390 p_buffer = sout_FifoShow( p_thread->p_fifo );
391 i_date = mdate() + i_pts_delay - p_buffer->i_dts;
397 p_buffer = sout_FifoGet( p_thread->p_fifo );
399 if( p_thread->b_die ) return;
401 i_wait = i_date + p_buffer->i_dts;
403 if( i_wait - mdate() > MAX_ERROR || i_wait - mdate() < -MAX_ERROR )
405 msg_Warn( p_sout, "resetting clock" );
406 i_date = mdate() + i_pts_delay - p_buffer->i_dts;
413 send( p_thread->i_handle, p_buffer->p_buffer, p_buffer->i_size, 0 );
415 sout_BufferDelete( p_sout, p_buffer );