1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.3 2003/02/16 14:03:56 fenrir 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>
41 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
46 # include <winsock2.h>
47 # include <ws2tcpip.h>
49 # define IN_MULTICAST(a) IN_CLASSD(a)
52 # include <sys/socket.h>
57 #define DEFAULT_PORT 1234
58 #define LATENCY 100000
59 #define MAX_ERROR 500000
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
66 static int Write( sout_instance_t *, sout_buffer_t * );
67 static int Seek( sout_instance_t *, off_t );
69 static void ThreadWrite( vlc_object_t *p_this );
71 /*****************************************************************************
73 *****************************************************************************/
75 set_description( _("UDP stream ouput") );
76 set_capability( "sout access", 100 );
77 add_shortcut( "udp" );
78 add_shortcut( "rtp" ); // Will work only with ts muxer
79 set_callbacks( Open, Close );
82 typedef struct sout_access_thread_s
86 sout_instance_t *p_sout;
92 } sout_access_thread_t;
94 typedef struct sout_access_data_s
96 int b_rtpts; // 1 if add rtp/ts header
97 uint16_t i_sequence_number;
102 sout_buffer_t *p_buffer;
104 sout_access_thread_t *p_thread;
106 } sout_access_data_t;
108 /*****************************************************************************
109 * Open: open the file
110 *****************************************************************************/
111 static int Open( vlc_object_t *p_this )
113 sout_instance_t *p_sout = (sout_instance_t*)p_this;
114 sout_access_data_t *p_access;
121 network_socket_t socket_desc;
123 if( !( p_access = malloc( sizeof( sout_access_data_t ) ) ) )
125 msg_Err( p_sout, "Not enough memory" );
129 if( p_sout->psz_access != NULL &&
130 !strcmp( p_sout->psz_access, "rtp" ) )
132 if( p_sout->psz_mux != NULL && *p_sout->psz_mux &&
133 strcmp( p_sout->psz_mux, "ts" ) &&
134 strcmp( p_sout->psz_mux, "ts_dvbpsi" ))
136 msg_Err( p_sout, "rtp ouput work only with ts payload" );
140 p_access->b_rtpts = 1;
144 p_access->b_rtpts = 0;
147 psz_parser = strdup( p_sout->psz_name );
149 psz_dst_addr = 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;
167 p_access->p_thread = vlc_object_create( p_sout,
168 sizeof( sout_access_thread_t ) );
169 if( !p_access->p_thread )
171 msg_Err( p_sout, "out of memory" );
175 p_access->p_thread->p_sout = p_sout;
176 p_access->p_thread->b_die = 0;
177 p_access->p_thread->b_error= 0;
178 p_access->p_thread->p_fifo = sout_FifoCreate( p_sout );
180 socket_desc.i_type = NETWORK_UDP;
181 socket_desc.psz_server_addr = psz_dst_addr;
182 socket_desc.i_server_port = i_dst_port;
183 socket_desc.psz_bind_addr = "";
184 socket_desc.i_bind_port = 0;
185 p_access->p_thread->p_private = (void*)&socket_desc;
186 if( !( p_network = module_Need( p_access->p_thread,
189 msg_Err( p_sout, "failed to open a connection (udp)" );
192 module_Unneed( p_access->p_thread, p_network );
194 p_access->p_thread->i_handle = socket_desc.i_handle;
195 p_access->i_mtu = socket_desc.i_mtu;
197 if( vlc_thread_create( p_access->p_thread, "sout write thread",
198 ThreadWrite, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
200 msg_Err( p_sout, "cannot spawn sout access thread" );
201 vlc_object_destroy( p_access->p_thread );
205 p_access->p_buffer = NULL;
206 p_access->i_sequence_number = 12; // FIXME should be random, used by rtp
207 p_access->i_ssrc = 4212; // FIXME " " " " " "
209 p_sout->i_method = SOUT_METHOD_NETWORK;
210 p_sout->p_access_data = (void*)p_access;
211 p_sout->pf_write = Write;
212 p_sout->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_instance_t *p_sout = (sout_instance_t*)p_this;
228 sout_access_data_t *p_access = (sout_access_data_t*)p_sout->p_access_data;
231 p_access->p_thread->b_die = 1;
232 for( i = 0; i < 10; i++ )
234 sout_buffer_t *p_dummy;
236 p_dummy = sout_BufferNew( p_sout, p_access->i_mtu );
239 p_dummy->i_length = 0;
240 sout_FifoPut( p_access->p_thread->p_fifo, p_dummy );
242 vlc_thread_join( p_access->p_thread );
244 sout_FifoDestroy( p_sout, p_access->p_thread->p_fifo );
246 if( p_access->p_buffer )
248 sout_BufferDelete( p_sout, p_access->p_buffer );
251 #if defined( UNDER_CE )
252 CloseHandle( (HANDLE)p_access->p_thread->i_handle );
253 #elif defined( WIN32 )
254 closesocket( p_access->p_thread->i_handle );
256 close( p_access->p_thread->i_handle );
258 msg_Info( p_sout, "Close" );
261 /*****************************************************************************
262 * Read: standard read on a file descriptor.
263 *****************************************************************************/
264 static int Write( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
266 sout_access_data_t *p_access = (sout_access_data_t*)p_sout->p_access_data;
267 unsigned int i_write;
271 sout_buffer_t *p_next;
272 if( p_buffer->i_size > p_access->i_mtu )
274 msg_Warn( p_sout, "arggggggggggggg packet size > mtu" );
275 i_write = p_access->i_mtu;
279 i_write = p_buffer->i_size;
282 /* if we have enough data, enque the buffer */
283 if( p_access->p_buffer &&
284 p_access->p_buffer->i_size + i_write > p_access->i_mtu )
286 sout_FifoPut( p_access->p_thread->p_fifo, p_access->p_buffer );
287 p_access->p_buffer = NULL;
290 if( !p_access->p_buffer )
292 p_access->p_buffer = sout_BufferNew( p_sout, p_access->i_mtu );
293 p_access->p_buffer->i_dts = p_buffer->i_dts;
294 p_access->p_buffer->i_size = 0;
295 if( p_access->b_rtpts )
297 mtime_t i_timestamp = p_access->p_buffer->i_dts * 9 / 100;
299 /* add rtp/ts header */
300 p_access->p_buffer->p_buffer[0] = 0x80;
301 p_access->p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
302 p_access->p_buffer->p_buffer[2] =
303 ( p_access->i_sequence_number >> 8 )&0xff;
304 p_access->p_buffer->p_buffer[3] =
305 p_access->i_sequence_number&0xff;
307 p_access->p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
308 p_access->p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
309 p_access->p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
310 p_access->p_buffer->p_buffer[7] = i_timestamp&0xff;
312 p_access->p_buffer->p_buffer[ 8] =
313 ( p_access->i_ssrc >> 24 )&0xff;
314 p_access->p_buffer->p_buffer[ 9] =
315 ( p_access->i_ssrc >> 16 )&0xff;
316 p_access->p_buffer->p_buffer[10] =
317 ( p_access->i_ssrc >> 8 )&0xff;
318 p_access->p_buffer->p_buffer[11] = p_access->i_ssrc&0xff;
320 p_access->p_buffer->i_size = 12;
325 if( p_buffer->i_size > 0 )
327 memcpy( p_access->p_buffer->p_buffer + p_access->p_buffer->i_size,
330 p_access->p_buffer->i_size += i_write;
332 p_next = p_buffer->p_next;
333 sout_BufferDelete( p_sout, p_buffer );
337 return( p_access->p_thread->b_error ? -1 : 0 );
340 /*****************************************************************************
341 * Seek: seek to a specific location in a file
342 *****************************************************************************/
343 static int Seek( sout_instance_t *p_sout, off_t i_pos )
346 msg_Err( p_sout, "udp sout access cannot seek" );
350 /*****************************************************************************
351 * ThreadWrite: Write a packet on the network at the good time.
352 *****************************************************************************/
353 static void ThreadWrite( vlc_object_t *p_this )
355 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
356 sout_instance_t *p_sout = p_thread->p_sout;
357 sout_buffer_t *p_buffer;
360 while( !p_thread->b_die && p_thread->p_fifo->i_depth < 5 )
364 if( p_thread->b_die )
368 p_buffer = sout_FifoShow( p_thread->p_fifo );
369 i_date = mdate() - p_buffer->i_dts;
375 p_buffer = sout_FifoGet( p_thread->p_fifo );
377 if( p_thread->b_die )
381 i_wait = i_date + p_buffer->i_dts;
384 if( i_wait - mdate() > MAX_ERROR ||
385 i_wait - mdate() < -MAX_ERROR )
387 msg_Warn( p_sout, "resetting clock" );
388 i_date = mdate() - p_buffer->i_dts;
390 send( p_thread->i_handle,
395 sout_BufferDelete( p_sout, p_buffer );