* Less checks on muxer type.
[vlc.git] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.3 2003/02/16 14:03:56 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_instance_t *, sout_buffer_t * );
67 static int     Seek( sout_instance_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 typedef struct sout_access_data_s
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 } sout_access_data_t;
107
108 /*****************************************************************************
109  * Open: open the file
110  *****************************************************************************/
111 static int Open( vlc_object_t *p_this )
112 {
113     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
114     sout_access_data_t  *p_access;
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_access = malloc( sizeof( sout_access_data_t ) ) ) )
124     {
125         msg_Err( p_sout, "Not enough memory" );
126         return( -1 );
127     }
128
129     if( p_sout->psz_access != NULL &&
130         !strcmp( p_sout->psz_access, "rtp" ) )
131     {
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" ))
135         {
136             msg_Err( p_sout, "rtp ouput work only with ts payload" );
137             free( p_access );
138             return( -1 );
139         }
140         p_access->b_rtpts = 1;
141     }
142     else
143     {
144         p_access->b_rtpts = 0;
145     }
146
147     psz_parser = strdup( p_sout->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_access->p_thread = vlc_object_create( p_sout,
168                                             sizeof( sout_access_thread_t ) );
169     if( !p_access->p_thread )
170     {
171         msg_Err( p_sout, "out of memory" );
172         return( -1 );
173     }
174
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 );
179
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, 
187                                     "network", "" ) ) )
188     {
189         msg_Err( p_sout, "failed to open a connection (udp)" );
190         return( -1 );
191     }
192     module_Unneed( p_access->p_thread, p_network );
193
194     p_access->p_thread->i_handle = socket_desc.i_handle;
195     p_access->i_mtu     = socket_desc.i_mtu;
196
197     if( vlc_thread_create( p_access->p_thread, "sout write thread",
198                            ThreadWrite, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
199     {
200         msg_Err( p_sout, "cannot spawn sout access thread" );
201         vlc_object_destroy( p_access->p_thread );
202         return( -1 );
203     }
204
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   "    "    "       "  "   "
208
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;
213
214     msg_Info( p_sout,
215               "Open: addr:`%s' port:`%d'",
216               psz_dst_addr, i_dst_port );
217
218     free( psz_dst_addr );
219     return VLC_SUCCESS;
220 }
221
222 /*****************************************************************************
223  * Close: close the target
224  *****************************************************************************/
225 static void Close( vlc_object_t * p_this )
226 {
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;
229     int                 i;
230
231     p_access->p_thread->b_die = 1;
232     for( i = 0; i < 10; i++ )
233     {
234         sout_buffer_t       *p_dummy;
235
236         p_dummy = sout_BufferNew( p_sout, p_access->i_mtu );
237         p_dummy->i_dts = 0;
238         p_dummy->i_pts = 0;
239         p_dummy->i_length = 0;
240         sout_FifoPut( p_access->p_thread->p_fifo, p_dummy );
241     }
242     vlc_thread_join( p_access->p_thread );
243
244     sout_FifoDestroy( p_sout, p_access->p_thread->p_fifo );
245
246     if( p_access->p_buffer )
247     {
248         sout_BufferDelete( p_sout, p_access->p_buffer );
249     }
250
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 );
255 #else
256     close( p_access->p_thread->i_handle );
257 #endif
258     msg_Info( p_sout, "Close" );
259 }
260
261 /*****************************************************************************
262  * Read: standard read on a file descriptor.
263  *****************************************************************************/
264 static int Write( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
265 {
266     sout_access_data_t  *p_access = (sout_access_data_t*)p_sout->p_access_data;
267     unsigned int i_write;
268
269     while( p_buffer )
270     {
271         sout_buffer_t *p_next;
272         if( p_buffer->i_size > p_access->i_mtu )
273         {
274             msg_Warn( p_sout, "arggggggggggggg packet size > mtu" );
275             i_write = p_access->i_mtu;
276         }
277         else
278         {
279             i_write = p_buffer->i_size;
280         }
281
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 )
285         {
286             sout_FifoPut( p_access->p_thread->p_fifo, p_access->p_buffer );
287             p_access->p_buffer = NULL;
288         }
289
290         if( !p_access->p_buffer )
291         {
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 )
296             {
297                 mtime_t i_timestamp = p_access->p_buffer->i_dts * 9 / 100;
298
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;
306
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;
311
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;
319
320                 p_access->p_buffer->i_size = 12;
321             }
322         }
323
324
325         if( p_buffer->i_size > 0 )
326         {
327             memcpy( p_access->p_buffer->p_buffer + p_access->p_buffer->i_size,
328                     p_buffer->p_buffer,
329                     i_write );
330             p_access->p_buffer->i_size += i_write;
331         }
332         p_next = p_buffer->p_next;
333         sout_BufferDelete( p_sout, p_buffer );
334         p_buffer = p_next;
335     }
336
337     return( p_access->p_thread->b_error ? -1 : 0 );
338 }
339
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 )
344 {
345
346     msg_Err( p_sout, "udp sout access cannot seek" );
347     return( -1 );
348 }
349
350 /*****************************************************************************
351  * ThreadWrite: Write a packet on the network at the good time.
352  *****************************************************************************/
353 static void ThreadWrite( vlc_object_t *p_this )
354 {
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;
358     mtime_t       i_date;
359
360     while( !p_thread->b_die && p_thread->p_fifo->i_depth < 5 )
361     {
362         msleep( 10000 );
363     }
364     if( p_thread->b_die )
365     {
366         return;
367     }
368     p_buffer = sout_FifoShow( p_thread->p_fifo );
369     i_date = mdate() - p_buffer->i_dts;
370
371     for( ;; )
372     {
373         mtime_t i_wait;
374
375         p_buffer = sout_FifoGet( p_thread->p_fifo );
376
377         if( p_thread->b_die )
378         {
379             return;
380         }
381         i_wait = i_date + p_buffer->i_dts;
382         mwait( i_wait );
383
384         if( i_wait - mdate() > MAX_ERROR ||
385             i_wait - mdate() < -MAX_ERROR )
386         {
387             msg_Warn( p_sout, "resetting clock" );
388             i_date = mdate() - p_buffer->i_dts;
389         }
390         send( p_thread->i_handle,
391               p_buffer->p_buffer,
392               p_buffer->i_size,
393               0 );
394
395         sout_BufferDelete( p_sout, p_buffer );
396     }
397 }
398
399