1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.8 2003/05/24 11:53:11 sam 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 *p_this );
69 /*****************************************************************************
71 *****************************************************************************/
73 set_description( _("UDP stream ouput") );
74 set_capability( "sout access", 100 );
75 add_shortcut( "udp" );
76 add_shortcut( "rtp" ); // Will work only with ts muxer
77 set_callbacks( Open, Close );
80 typedef struct sout_access_thread_s
84 sout_instance_t *p_sout;
90 } sout_access_thread_t;
92 struct sout_access_out_sys_t
94 int b_rtpts; // 1 if add rtp/ts header
95 uint16_t i_sequence_number;
100 sout_buffer_t *p_buffer;
102 sout_access_thread_t *p_thread;
106 /*****************************************************************************
107 * Open: open the file
108 *****************************************************************************/
109 static int Open( vlc_object_t *p_this )
111 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
112 sout_access_out_sys_t *p_sys;
119 network_socket_t socket_desc;
121 if( !( p_sys = p_access->p_sys =
122 malloc( sizeof( sout_access_out_sys_t ) ) ) )
124 msg_Err( p_access, "Not enough memory" );
125 return( VLC_EGENERIC );
129 if( p_access->psz_access != NULL &&
130 !strcmp( p_access->psz_access, "rtp" ) )
132 msg_Warn( p_access, "becarefull that rtp ouput work only with ts payload(not an error)" );
140 psz_parser = strdup( p_access->psz_name );
142 psz_dst_addr = psz_parser;
145 if ( *psz_parser == '[' )
147 while( *psz_parser && *psz_parser != ']' )
152 while( *psz_parser && *psz_parser != ':' )
156 if( *psz_parser == ':' )
160 i_dst_port = atoi( psz_parser );
162 if( i_dst_port <= 0 )
164 i_dst_port = DEFAULT_PORT;
168 vlc_object_create( p_access,
169 sizeof( sout_access_thread_t ) );
170 if( !p_sys->p_thread )
172 msg_Err( p_access, "out of memory" );
173 return( VLC_EGENERIC );
176 p_sys->p_thread->p_sout = p_access->p_sout;
177 p_sys->p_thread->b_die = 0;
178 p_sys->p_thread->b_error= 0;
179 p_sys->p_thread->p_fifo = sout_FifoCreate( p_access->p_sout );
181 socket_desc.i_type = NETWORK_UDP;
182 socket_desc.psz_server_addr = psz_dst_addr;
183 socket_desc.i_server_port = i_dst_port;
184 socket_desc.psz_bind_addr = "";
185 socket_desc.i_bind_port = 0;
186 p_sys->p_thread->p_private = (void*)&socket_desc;
187 if( !( p_network = module_Need( p_sys->p_thread,
190 msg_Err( p_access, "failed to open a connection (udp)" );
191 return( VLC_EGENERIC );
193 module_Unneed( p_sys->p_thread, p_network );
195 p_sys->p_thread->i_handle = socket_desc.i_handle;
196 p_sys->i_mtu = socket_desc.i_mtu;
198 if( vlc_thread_create( p_sys->p_thread, "sout write thread",
199 ThreadWrite, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
201 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
202 vlc_object_destroy( p_sys->p_thread );
203 return( VLC_EGENERIC );
206 srand( (uint32_t)mdate());
207 p_sys->p_buffer = NULL;
208 p_sys->i_sequence_number = rand()&0xffff;
209 p_sys->i_ssrc = rand()&0xffffffff;
211 p_access->pf_write = Write;
212 p_access->pf_seek = Seek;
215 "Open: addr:`%s' port:`%d'",
216 psz_dst_addr, i_dst_port );
218 free( psz_dst_addr );
222 /*****************************************************************************
223 * Close: close the target
224 *****************************************************************************/
225 static void Close( vlc_object_t * p_this )
227 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
228 sout_access_out_sys_t *p_sys = p_access->p_sys;
231 p_sys->p_thread->b_die = 1;
232 for( i = 0; i < 10; i++ )
234 sout_buffer_t *p_dummy;
236 p_dummy = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
239 p_dummy->i_length = 0;
240 sout_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
242 vlc_thread_join( p_sys->p_thread );
244 sout_FifoDestroy( p_access->p_sout, p_sys->p_thread->p_fifo );
246 if( p_sys->p_buffer )
248 sout_BufferDelete( p_access->p_sout, p_sys->p_buffer );
251 #if defined( UNDER_CE )
252 CloseHandle( (HANDLE)p_sys->p_thread->i_handle );
253 #elif defined( WIN32 )
254 closesocket( p_sys->p_thread->i_handle );
256 close( p_sys->p_thread->i_handle );
260 msg_Info( p_access, "Close" );
263 /*****************************************************************************
264 * Read: standard read on a file descriptor.
265 *****************************************************************************/
266 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
268 sout_access_out_sys_t *p_sys = p_access->p_sys;
269 unsigned int i_write;
273 sout_buffer_t *p_next;
274 if( p_buffer->i_size > p_sys->i_mtu )
276 msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
277 i_write = p_sys->i_mtu;
281 i_write = p_buffer->i_size;
284 /* if we have enough data, enque the buffer */
285 if( p_sys->p_buffer &&
286 p_sys->p_buffer->i_size + i_write > p_sys->i_mtu )
288 sout_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
289 p_sys->p_buffer = NULL;
292 if( !p_sys->p_buffer )
294 p_sys->p_buffer = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
295 p_sys->p_buffer->i_dts = p_buffer->i_dts;
296 p_sys->p_buffer->i_size = 0;
299 mtime_t i_timestamp = p_sys->p_buffer->i_dts * 9 / 100;
301 /* add rtp/ts header */
302 p_sys->p_buffer->p_buffer[0] = 0x80;
303 p_sys->p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
305 p_sys->p_buffer->p_buffer[2] =
306 ( p_sys->i_sequence_number >> 8 )&0xff;
307 p_sys->p_buffer->p_buffer[3] =
308 p_sys->i_sequence_number&0xff;
309 p_sys->i_sequence_number++;
311 p_sys->p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
312 p_sys->p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
313 p_sys->p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
314 p_sys->p_buffer->p_buffer[7] = i_timestamp&0xff;
316 p_sys->p_buffer->p_buffer[ 8] =
317 ( p_sys->i_ssrc >> 24 )&0xff;
318 p_sys->p_buffer->p_buffer[ 9] =
319 ( p_sys->i_ssrc >> 16 )&0xff;
320 p_sys->p_buffer->p_buffer[10] =
321 ( p_sys->i_ssrc >> 8 )&0xff;
322 p_sys->p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
324 p_sys->p_buffer->i_size = 12;
329 if( p_buffer->i_size > 0 )
331 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_size,
334 p_sys->p_buffer->i_size += i_write;
336 p_next = p_buffer->p_next;
337 sout_BufferDelete( p_access->p_sout, p_buffer );
341 return( p_sys->p_thread->b_error ? -1 : 0 );
344 /*****************************************************************************
345 * Seek: seek to a specific location in a file
346 *****************************************************************************/
347 static int Seek( sout_access_out_t *p_access, off_t i_pos )
350 msg_Err( p_access, "udp sout access cannot seek" );
354 /*****************************************************************************
355 * ThreadWrite: Write a packet on the network at the good time.
356 *****************************************************************************/
357 static void ThreadWrite( vlc_object_t *p_this )
359 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
360 sout_instance_t *p_sout = p_thread->p_sout;
361 sout_buffer_t *p_buffer;
364 while( !p_thread->b_die && p_thread->p_fifo->i_depth < 5 )
368 if( p_thread->b_die )
372 p_buffer = sout_FifoShow( p_thread->p_fifo );
373 i_date = mdate() - p_buffer->i_dts;
379 p_buffer = sout_FifoGet( p_thread->p_fifo );
381 if( p_thread->b_die )
385 i_wait = i_date + p_buffer->i_dts;
387 if( i_wait - mdate() > MAX_ERROR ||
388 i_wait - mdate() < -MAX_ERROR )
390 msg_Warn( p_sout, "resetting clock" );
391 i_date = mdate() - p_buffer->i_dts;
398 send( p_thread->i_handle,
403 sout_BufferDelete( p_sout, p_buffer );