* ALL: Finally fixed the MSVC project files.
[vlc.git] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.5 2003/03/03 14:21:08 gbazin 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     if( p_access->psz_access != NULL &&
129         !strcmp( p_access->psz_access, "rtp" ) )
130     {
131          if( p_access->p_sout->psz_mux != NULL && 
132              *p_access->p_sout->psz_mux &&
133              strcmp( p_access->p_sout->psz_mux, "ts" ) &&
134              strcmp( p_access->p_sout->psz_mux, "ts_dvbpsi" ) )
135         {
136             msg_Err( p_access, "rtp ouput work only with ts payload" );
137             free( p_sys );
138             return( VLC_EGENERIC );
139         }
140         p_sys->b_rtpts = 1;
141     }
142     else
143     {
144         p_sys->b_rtpts = 0;
145     }
146
147     psz_parser = strdup( p_access->psz_name );
148
149     psz_dst_addr = psz_parser;
150     i_dst_port = 0;
151
152     while( *psz_parser && *psz_parser != ':' )
153     {
154         psz_parser++;
155     }
156     if( *psz_parser == ':' )
157     {
158         *psz_parser = '\0';
159         psz_parser++;
160         i_dst_port = atoi( psz_parser );
161     }
162     if( i_dst_port <= 0 )
163     {
164         i_dst_port = DEFAULT_PORT;
165     }
166
167     p_sys->p_thread =
168         vlc_object_create( p_access,
169                            sizeof( sout_access_thread_t ) );
170     if( !p_sys->p_thread )
171     {
172         msg_Err( p_access, "out of memory" );
173         return( VLC_EGENERIC );
174     }
175
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 );
180
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,
188                                     "network", "" ) ) )
189     {
190         msg_Err( p_access, "failed to open a connection (udp)" );
191         return( VLC_EGENERIC );
192     }
193     module_Unneed( p_sys->p_thread, p_network );
194
195     p_sys->p_thread->i_handle = socket_desc.i_handle;
196     p_sys->i_mtu     = socket_desc.i_mtu;
197
198     if( vlc_thread_create( p_sys->p_thread, "sout write thread",
199                            ThreadWrite, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
200     {
201         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
202         vlc_object_destroy( p_sys->p_thread );
203         return( VLC_EGENERIC );
204     }
205
206     p_sys->p_buffer          = NULL;
207     p_sys->i_sequence_number = 12;   // FIXME should be random, used by rtp
208     p_sys->i_ssrc            = 4212; // FIXME   "    "    "       "  "   "
209
210     p_access->pf_write       = Write;
211     p_access->pf_seek        = Seek;
212
213     msg_Info( p_access,
214               "Open: addr:`%s' port:`%d'",
215               psz_dst_addr, i_dst_port );
216
217     free( psz_dst_addr );
218     return VLC_SUCCESS;
219 }
220
221 /*****************************************************************************
222  * Close: close the target
223  *****************************************************************************/
224 static void Close( vlc_object_t * p_this )
225 {
226     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
227     sout_access_out_sys_t   *p_sys = p_access->p_sys;
228     int                 i;
229
230     p_sys->p_thread->b_die = 1;
231     for( i = 0; i < 10; i++ )
232     {
233         sout_buffer_t       *p_dummy;
234
235         p_dummy = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
236         p_dummy->i_dts = 0;
237         p_dummy->i_pts = 0;
238         p_dummy->i_length = 0;
239         sout_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
240     }
241     vlc_thread_join( p_sys->p_thread );
242
243     sout_FifoDestroy( p_access->p_sout, p_sys->p_thread->p_fifo );
244
245     if( p_sys->p_buffer )
246     {
247         sout_BufferDelete( p_access->p_sout, p_sys->p_buffer );
248     }
249
250 #if defined( UNDER_CE )
251     CloseHandle( (HANDLE)p_sys->p_thread->i_handle );
252 #elif defined( WIN32 )
253     closesocket( p_sys->p_thread->i_handle );
254 #else
255     close( p_sys->p_thread->i_handle );
256 #endif
257
258     free( p_sys );
259     msg_Info( p_access, "Close" );
260 }
261
262 /*****************************************************************************
263  * Read: standard read on a file descriptor.
264  *****************************************************************************/
265 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
266 {
267     sout_access_out_sys_t   *p_sys = p_access->p_sys;
268     unsigned int i_write;
269
270     while( p_buffer )
271     {
272         sout_buffer_t *p_next;
273         if( p_buffer->i_size > p_sys->i_mtu )
274         {
275             msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
276             i_write = p_sys->i_mtu;
277         }
278         else
279         {
280             i_write = p_buffer->i_size;
281         }
282
283         /* if we have enough data, enque the buffer */
284         if( p_sys->p_buffer &&
285             p_sys->p_buffer->i_size + i_write > p_sys->i_mtu )
286         {
287             sout_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
288             p_sys->p_buffer = NULL;
289         }
290
291         if( !p_sys->p_buffer )
292         {
293             p_sys->p_buffer = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
294             p_sys->p_buffer->i_dts = p_buffer->i_dts;
295             p_sys->p_buffer->i_size = 0;
296             if( p_sys->b_rtpts )
297             {
298                 mtime_t i_timestamp = p_sys->p_buffer->i_dts * 9 / 100;
299
300                 /* add rtp/ts header */
301                 p_sys->p_buffer->p_buffer[0] = 0x80;
302                 p_sys->p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
303                 p_sys->p_buffer->p_buffer[2] =
304                     ( p_sys->i_sequence_number >> 8 )&0xff;
305                 p_sys->p_buffer->p_buffer[3] =
306                     p_sys->i_sequence_number&0xff;
307
308                 p_sys->p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
309                 p_sys->p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
310                 p_sys->p_buffer->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
311                 p_sys->p_buffer->p_buffer[7] = i_timestamp&0xff;
312
313                 p_sys->p_buffer->p_buffer[ 8] =
314                     ( p_sys->i_ssrc >> 24 )&0xff;
315                 p_sys->p_buffer->p_buffer[ 9] =
316                     ( p_sys->i_ssrc >> 16 )&0xff;
317                 p_sys->p_buffer->p_buffer[10] =
318                     ( p_sys->i_ssrc >>  8 )&0xff;
319                 p_sys->p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
320
321                 p_sys->p_buffer->i_size = 12;
322             }
323         }
324
325
326         if( p_buffer->i_size > 0 )
327         {
328             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_size,
329                     p_buffer->p_buffer,
330                     i_write );
331             p_sys->p_buffer->i_size += i_write;
332         }
333         p_next = p_buffer->p_next;
334         sout_BufferDelete( p_access->p_sout, p_buffer );
335         p_buffer = p_next;
336     }
337
338     return( p_sys->p_thread->b_error ? -1 : 0 );
339 }
340
341 /*****************************************************************************
342  * Seek: seek to a specific location in a file
343  *****************************************************************************/
344 static int Seek( sout_access_out_t *p_access, off_t i_pos )
345 {
346
347     msg_Err( p_access, "udp sout access cannot seek" );
348     return( -1 );
349 }
350
351 /*****************************************************************************
352  * ThreadWrite: Write a packet on the network at the good time.
353  *****************************************************************************/
354 static void ThreadWrite( vlc_object_t *p_this )
355 {
356     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
357     sout_instance_t      *p_sout = p_thread->p_sout;
358     sout_buffer_t *p_buffer;
359     mtime_t       i_date;
360
361     while( !p_thread->b_die && p_thread->p_fifo->i_depth < 5 )
362     {
363         msleep( 10000 );
364     }
365     if( p_thread->b_die )
366     {
367         return;
368     }
369     p_buffer = sout_FifoShow( p_thread->p_fifo );
370     i_date = mdate() - p_buffer->i_dts;
371
372     for( ;; )
373     {
374         mtime_t i_wait;
375
376         p_buffer = sout_FifoGet( p_thread->p_fifo );
377
378         if( p_thread->b_die )
379         {
380             return;
381         }
382         i_wait = i_date + p_buffer->i_dts;
383         mwait( i_wait );
384
385         if( i_wait - mdate() > MAX_ERROR ||
386             i_wait - mdate() < -MAX_ERROR )
387         {
388             msg_Warn( p_sout, "resetting clock" );
389             i_date = mdate() - p_buffer->i_dts;
390         }
391         send( p_thread->i_handle,
392               p_buffer->p_buffer,
393               p_buffer->i_size,
394               0 );
395
396         sout_BufferDelete( p_sout, p_buffer );
397     }
398 }
399
400