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