* all: reworked stream output. Now it handles mutliples outputs from
[vlc.git] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.6 2003/03/11 19:02:30 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/input.h>
37 #include <vlc/sout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #ifdef WIN32
44 #   include <winsock2.h>
45 #   include <ws2tcpip.h>
46 #   ifndef IN_MULTICAST
47 #       define IN_MULTICAST(a) IN_CLASSD(a)
48 #   endif
49 #else
50 #   include <sys/socket.h>
51 #endif
52
53 #include "network.h"
54
55 #define DEFAULT_PORT 1234
56 #define LATENCY     100000
57 #define MAX_ERROR    500000
58 /*****************************************************************************
59  * Exported prototypes
60  *****************************************************************************/
61 static int     Open   ( vlc_object_t * );
62 static void    Close  ( vlc_object_t * );
63
64 static int     Write( sout_access_out_t *, sout_buffer_t * );
65 static int     Seek ( sout_access_out_t *, off_t  );
66
67 static void    ThreadWrite( vlc_object_t *p_this );
68
69 /*****************************************************************************
70  * Module descriptor
71  *****************************************************************************/
72 vlc_module_begin();
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 );
78 vlc_module_end();
79
80 typedef struct sout_access_thread_s
81 {
82     VLC_COMMON_MEMBERS
83
84     sout_instance_t *p_sout;
85
86     sout_fifo_t *p_fifo;
87
88     int         i_handle;
89
90 } sout_access_thread_t;
91
92 struct sout_access_out_sys_t
93 {
94     int                 b_rtpts;  // 1 if add rtp/ts header
95     uint16_t            i_sequence_number;
96     uint32_t            i_ssrc;
97
98     unsigned int        i_mtu;
99
100     sout_buffer_t       *p_buffer;
101
102     sout_access_thread_t *p_thread;
103
104 };
105
106 /*****************************************************************************
107  * Open: open the file
108  *****************************************************************************/
109 static int Open( vlc_object_t *p_this )
110 {
111     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
112     sout_access_out_sys_t   *p_sys;
113
114     char                *psz_parser;
115     char                *psz_dst_addr;
116     int                 i_dst_port;
117
118     module_t            *p_network;
119     network_socket_t    socket_desc;
120
121     if( !( p_sys = p_access->p_sys =
122                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
123     {
124         msg_Err( p_access, "Not enough memory" );
125         return( VLC_EGENERIC );
126     }
127
128
129     if( p_access->psz_access != NULL &&
130         !strcmp( p_access->psz_access, "rtp" ) )
131     {
132         msg_Warn( p_access, "becarefull that rtp ouput work only with ts payload(not an error)" );
133         p_sys->b_rtpts = 1;
134     }
135     else
136     {
137         p_sys->b_rtpts = 0;
138     }
139
140     psz_parser = strdup( p_access->psz_name );
141
142     psz_dst_addr = psz_parser;
143     i_dst_port = 0;
144
145     while( *psz_parser && *psz_parser != ':' )
146     {
147         psz_parser++;
148     }
149     if( *psz_parser == ':' )
150     {
151         *psz_parser = '\0';
152         psz_parser++;
153         i_dst_port = atoi( psz_parser );
154     }
155     if( i_dst_port <= 0 )
156     {
157         i_dst_port = DEFAULT_PORT;
158     }
159
160     p_sys->p_thread =
161         vlc_object_create( p_access,
162                            sizeof( sout_access_thread_t ) );
163     if( !p_sys->p_thread )
164     {
165         msg_Err( p_access, "out of memory" );
166         return( VLC_EGENERIC );
167     }
168
169     p_sys->p_thread->p_sout = p_access->p_sout;
170     p_sys->p_thread->b_die  = 0;
171     p_sys->p_thread->b_error= 0;
172     p_sys->p_thread->p_fifo = sout_FifoCreate( p_access->p_sout );
173
174     socket_desc.i_type = NETWORK_UDP;
175     socket_desc.psz_server_addr = psz_dst_addr;
176     socket_desc.i_server_port   = i_dst_port;
177     socket_desc.psz_bind_addr   = "";
178     socket_desc.i_bind_port     = 0;
179     p_sys->p_thread->p_private = (void*)&socket_desc;
180     if( !( p_network = module_Need( p_sys->p_thread,
181                                     "network", "" ) ) )
182     {
183         msg_Err( p_access, "failed to open a connection (udp)" );
184         return( VLC_EGENERIC );
185     }
186     module_Unneed( p_sys->p_thread, p_network );
187
188     p_sys->p_thread->i_handle = socket_desc.i_handle;
189     p_sys->i_mtu     = socket_desc.i_mtu;
190
191     if( vlc_thread_create( p_sys->p_thread, "sout write thread",
192                            ThreadWrite, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
193     {
194         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
195         vlc_object_destroy( p_sys->p_thread );
196         return( VLC_EGENERIC );
197     }
198
199     srand( (uint32_t)mdate());
200     p_sys->p_buffer          = NULL;
201     p_sys->i_sequence_number = rand()&0xffff;
202     p_sys->i_ssrc            = rand()&0xffffffff;
203
204     p_access->pf_write       = Write;
205     p_access->pf_seek        = Seek;
206
207     msg_Info( p_access,
208               "Open: addr:`%s' port:`%d'",
209               psz_dst_addr, i_dst_port );
210
211     free( psz_dst_addr );
212     return VLC_SUCCESS;
213 }
214
215 /*****************************************************************************
216  * Close: close the target
217  *****************************************************************************/
218 static void Close( vlc_object_t * p_this )
219 {
220     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
221     sout_access_out_sys_t   *p_sys = p_access->p_sys;
222     int                 i;
223
224     p_sys->p_thread->b_die = 1;
225     for( i = 0; i < 10; i++ )
226     {
227         sout_buffer_t       *p_dummy;
228
229         p_dummy = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
230         p_dummy->i_dts = 0;
231         p_dummy->i_pts = 0;
232         p_dummy->i_length = 0;
233         sout_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
234     }
235     vlc_thread_join( p_sys->p_thread );
236
237     sout_FifoDestroy( p_access->p_sout, p_sys->p_thread->p_fifo );
238
239     if( p_sys->p_buffer )
240     {
241         sout_BufferDelete( p_access->p_sout, p_sys->p_buffer );
242     }
243
244 #if defined( UNDER_CE )
245     CloseHandle( (HANDLE)p_sys->p_thread->i_handle );
246 #elif defined( WIN32 )
247     closesocket( p_sys->p_thread->i_handle );
248 #else
249     close( p_sys->p_thread->i_handle );
250 #endif
251
252     free( p_sys );
253     msg_Info( p_access, "Close" );
254 }
255
256 /*****************************************************************************
257  * Read: standard read on a file descriptor.
258  *****************************************************************************/
259 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
260 {
261     sout_access_out_sys_t   *p_sys = p_access->p_sys;
262     unsigned int i_write;
263
264     while( p_buffer )
265     {
266         sout_buffer_t *p_next;
267         if( p_buffer->i_size > p_sys->i_mtu )
268         {
269             msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
270             i_write = p_sys->i_mtu;
271         }
272         else
273         {
274             i_write = p_buffer->i_size;
275         }
276
277         /* if we have enough data, enque the buffer */
278         if( p_sys->p_buffer &&
279             p_sys->p_buffer->i_size + i_write > p_sys->i_mtu )
280         {
281             sout_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
282             p_sys->p_buffer = NULL;
283         }
284
285         if( !p_sys->p_buffer )
286         {
287             p_sys->p_buffer = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
288             p_sys->p_buffer->i_dts = p_buffer->i_dts;
289             p_sys->p_buffer->i_size = 0;
290             if( p_sys->b_rtpts )
291             {
292                 mtime_t i_timestamp = p_sys->p_buffer->i_dts * 9 / 100;
293
294                 /* add rtp/ts header */
295                 p_sys->p_buffer->p_buffer[0] = 0x80;
296                 p_sys->p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
297
298                 p_sys->p_buffer->p_buffer[2] =
299                     ( p_sys->i_sequence_number >> 8 )&0xff;
300                 p_sys->p_buffer->p_buffer[3] =
301                     p_sys->i_sequence_number&0xff;
302                 p_sys->i_sequence_number++;
303
304                 p_sys->p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
305                 p_sys->p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
306                 p_sys->p_buffer->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
307                 p_sys->p_buffer->p_buffer[7] = i_timestamp&0xff;
308
309                 p_sys->p_buffer->p_buffer[ 8] =
310                     ( p_sys->i_ssrc >> 24 )&0xff;
311                 p_sys->p_buffer->p_buffer[ 9] =
312                     ( p_sys->i_ssrc >> 16 )&0xff;
313                 p_sys->p_buffer->p_buffer[10] =
314                     ( p_sys->i_ssrc >>  8 )&0xff;
315                 p_sys->p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
316
317                 p_sys->p_buffer->i_size = 12;
318             }
319         }
320
321
322         if( p_buffer->i_size > 0 )
323         {
324             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_size,
325                     p_buffer->p_buffer,
326                     i_write );
327             p_sys->p_buffer->i_size += i_write;
328         }
329         p_next = p_buffer->p_next;
330         sout_BufferDelete( p_access->p_sout, p_buffer );
331         p_buffer = p_next;
332     }
333
334     return( p_sys->p_thread->b_error ? -1 : 0 );
335 }
336
337 /*****************************************************************************
338  * Seek: seek to a specific location in a file
339  *****************************************************************************/
340 static int Seek( sout_access_out_t *p_access, off_t i_pos )
341 {
342
343     msg_Err( p_access, "udp sout access cannot seek" );
344     return( -1 );
345 }
346
347 /*****************************************************************************
348  * ThreadWrite: Write a packet on the network at the good time.
349  *****************************************************************************/
350 static void ThreadWrite( vlc_object_t *p_this )
351 {
352     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
353     sout_instance_t      *p_sout = p_thread->p_sout;
354     sout_buffer_t *p_buffer;
355     mtime_t       i_date;
356
357     while( !p_thread->b_die && p_thread->p_fifo->i_depth < 5 )
358     {
359         msleep( 10000 );
360     }
361     if( p_thread->b_die )
362     {
363         return;
364     }
365     p_buffer = sout_FifoShow( p_thread->p_fifo );
366     i_date = mdate() - p_buffer->i_dts;
367
368     for( ;; )
369     {
370         mtime_t i_wait;
371
372         p_buffer = sout_FifoGet( p_thread->p_fifo );
373
374         if( p_thread->b_die )
375         {
376             return;
377         }
378         i_wait = i_date + p_buffer->i_dts;
379         mwait( i_wait );
380
381         if( i_wait - mdate() > MAX_ERROR ||
382             i_wait - mdate() < -MAX_ERROR )
383         {
384             msg_Warn( p_sout, "resetting clock" );
385             i_date = mdate() - p_buffer->i_dts;
386         }
387         send( p_thread->i_handle,
388               p_buffer->p_buffer,
389               p_buffer->i_size,
390               0 );
391
392         sout_BufferDelete( p_sout, p_buffer );
393     }
394 }
395
396