]> git.sesse.net Git - vlc/blob - modules/access_output/udp.c
* src/video_output/video_text.c: removed legacy code that has been rotting for ages.
[vlc] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.12 2003/08/01 19:38:48 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 * );
68
69 static sout_buffer_t *NewUDPPacket( sout_access_out_t *, mtime_t );
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 #define CACHING_TEXT N_("caching value in ms")
75 #define CACHING_LONGTEXT N_( \
76     "Allows you to modify the default caching value for udp streams. This " \
77     "value should be set in miliseconds units." )
78
79 vlc_module_begin();
80     set_description( _("UDP stream ouput") );
81     add_category_hint( N_("udp stream output"), NULL , VLC_TRUE );
82     add_integer( "udp-sout-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
83     set_capability( "sout access", 100 );
84     add_shortcut( "udp" );
85     add_shortcut( "rtp" ); // Will work only with ts muxer
86     set_callbacks( Open, Close );
87 vlc_module_end();
88
89 typedef struct sout_access_thread_s
90 {
91     VLC_COMMON_MEMBERS
92
93     sout_instance_t *p_sout;
94
95     sout_fifo_t *p_fifo;
96
97     int         i_handle;
98
99 } sout_access_thread_t;
100
101 struct sout_access_out_sys_t
102 {
103     int                 b_rtpts;  // 1 if add rtp/ts header
104     uint16_t            i_sequence_number;
105     uint32_t            i_ssrc;
106
107     unsigned int        i_mtu;
108
109     sout_buffer_t       *p_buffer;
110
111     sout_access_thread_t *p_thread;
112
113 };
114
115 /*****************************************************************************
116  * Open: open the file
117  *****************************************************************************/
118 static int Open( vlc_object_t *p_this )
119 {
120     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
121     sout_access_out_sys_t   *p_sys;
122
123     char                *psz_parser;
124     char                *psz_dst_addr;
125     int                 i_dst_port;
126
127     module_t            *p_network;
128     network_socket_t    socket_desc;
129
130     char                *val;
131
132     if( !( p_sys = p_access->p_sys =
133                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
134     {
135         msg_Err( p_access, "Not enough memory" );
136         return( VLC_EGENERIC );
137     }
138
139
140     if( p_access->psz_access != NULL &&
141         !strcmp( p_access->psz_access, "rtp" ) )
142     {
143         msg_Warn( p_access, "becarefull that rtp ouput work only with ts "
144                   "payload(not an error)" );
145         p_sys->b_rtpts = 1;
146     }
147     else
148     {
149         p_sys->b_rtpts = 0;
150     }
151
152     psz_parser = strdup( p_access->psz_name );
153
154     psz_dst_addr = psz_parser;
155     i_dst_port = 0;
156
157     if ( *psz_parser == '[' )
158     {
159         while( *psz_parser && *psz_parser != ']' )
160         {
161             psz_parser++;
162         }
163     }
164     while( *psz_parser && *psz_parser != ':' )
165     {
166         psz_parser++;
167     }
168     if( *psz_parser == ':' )
169     {
170         *psz_parser = '\0';
171         psz_parser++;
172         i_dst_port = atoi( psz_parser );
173     }
174     if( i_dst_port <= 0 )
175     {
176         i_dst_port = DEFAULT_PORT;
177     }
178
179     p_sys->p_thread =
180         vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
181     if( !p_sys->p_thread )
182     {
183         msg_Err( p_access, "out of memory" );
184         return( VLC_EGENERIC );
185     }
186
187     p_sys->p_thread->p_sout = p_access->p_sout;
188     p_sys->p_thread->b_die  = 0;
189     p_sys->p_thread->b_error= 0;
190     p_sys->p_thread->p_fifo = sout_FifoCreate( p_access->p_sout );
191
192     socket_desc.i_type = NETWORK_UDP;
193     socket_desc.psz_server_addr = psz_dst_addr;
194     socket_desc.i_server_port   = i_dst_port;
195     socket_desc.psz_bind_addr   = "";
196     socket_desc.i_bind_port     = 0;
197     socket_desc.i_ttl           = 0;
198     if( ( val = sout_cfg_find_value( p_access->p_cfg, "ttl" ) ) )
199     {
200         socket_desc.i_ttl = atoi( val );
201     }
202     p_sys->p_thread->p_private = (void*)&socket_desc;
203     if( !( p_network = module_Need( p_sys->p_thread,
204                                     "network", "" ) ) )
205     {
206         msg_Err( p_access, "failed to open a connection (udp)" );
207         return( VLC_EGENERIC );
208     }
209     module_Unneed( p_sys->p_thread, p_network );
210
211     p_sys->p_thread->i_handle = socket_desc.i_handle;
212     p_sys->i_mtu     = socket_desc.i_mtu;
213
214     if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
215                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
216     {
217         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
218         vlc_object_destroy( p_sys->p_thread );
219         return( VLC_EGENERIC );
220     }
221
222     srand( (uint32_t)mdate());
223     p_sys->p_buffer          = NULL;
224     p_sys->i_sequence_number = rand()&0xffff;
225     p_sys->i_ssrc            = rand()&0xffffffff;
226
227     p_access->pf_write       = Write;
228     p_access->pf_seek        = Seek;
229
230     msg_Info( p_access, "Open: addr:`%s' port:`%d'",
231               psz_dst_addr, i_dst_port );
232
233     free( psz_dst_addr );
234     return VLC_SUCCESS;
235 }
236
237 /*****************************************************************************
238  * Close: close the target
239  *****************************************************************************/
240 static void Close( vlc_object_t * p_this )
241 {
242     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
243     sout_access_out_sys_t   *p_sys = p_access->p_sys;
244     int                 i;
245
246     p_sys->p_thread->b_die = 1;
247     for( i = 0; i < 10; i++ )
248     {
249         sout_buffer_t       *p_dummy;
250
251         p_dummy = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
252         p_dummy->i_dts = 0;
253         p_dummy->i_pts = 0;
254         p_dummy->i_length = 0;
255         sout_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
256     }
257     vlc_thread_join( p_sys->p_thread );
258
259     sout_FifoDestroy( p_access->p_sout, p_sys->p_thread->p_fifo );
260
261     if( p_sys->p_buffer )
262     {
263         sout_BufferDelete( p_access->p_sout, p_sys->p_buffer );
264     }
265
266 #if defined( UNDER_CE )
267     CloseHandle( (HANDLE)p_sys->p_thread->i_handle );
268 #elif defined( WIN32 )
269     closesocket( p_sys->p_thread->i_handle );
270 #else
271     close( p_sys->p_thread->i_handle );
272 #endif
273
274     free( p_sys );
275     msg_Info( p_access, "Close" );
276 }
277
278 /*****************************************************************************
279  * Read: standard read on a file descriptor.
280  *****************************************************************************/
281 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
282 {
283     sout_access_out_sys_t   *p_sys = p_access->p_sys;
284     unsigned int i_write;
285
286     while( p_buffer )
287     {
288         sout_buffer_t *p_next;
289         if( p_buffer->i_size > p_sys->i_mtu )
290         {
291             msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
292             i_write = p_sys->i_mtu;
293         }
294         else
295         {
296             i_write = p_buffer->i_size;
297         }
298
299         /* if we have enough data, enque the buffer */
300         if( p_sys->p_buffer &&
301             p_sys->p_buffer->i_size + i_write > p_sys->i_mtu )
302         {
303             sout_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
304             p_sys->p_buffer = NULL;
305         }
306
307         if( !p_sys->p_buffer )
308         {
309             p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
310         }
311
312         if( p_buffer->i_size > 0 )
313         {
314             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_size,
315                     p_buffer->p_buffer, i_write );
316             p_sys->p_buffer->i_size += i_write;
317         }
318         p_next = p_buffer->p_next;
319         sout_BufferDelete( p_access->p_sout, p_buffer );
320         p_buffer = p_next;
321     }
322
323     return( p_sys->p_thread->b_error ? -1 : 0 );
324 }
325
326 /*****************************************************************************
327  * Seek: seek to a specific location in a file
328  *****************************************************************************/
329 static int Seek( sout_access_out_t *p_access, off_t i_pos )
330 {
331
332     msg_Err( p_access, "udp sout access cannot seek" );
333     return( -1 );
334 }
335
336 /*****************************************************************************
337  * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
338  *****************************************************************************/
339 static sout_buffer_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
340 {
341     sout_access_out_sys_t *p_sys = p_access->p_sys;
342     sout_buffer_t *p_buffer;
343
344     p_buffer = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
345     p_buffer->i_dts = i_dts;
346     p_buffer->i_size = 0;
347
348     if( p_sys->b_rtpts )
349     {
350         mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
351
352         /* add rtp/ts header */
353         p_buffer->p_buffer[0] = 0x80;
354         p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
355
356         p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
357         p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
358         p_sys->i_sequence_number++;
359
360         p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
361         p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
362         p_buffer->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
363         p_buffer->p_buffer[7] = i_timestamp&0xff;
364
365         p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
366         p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
367         p_buffer->p_buffer[10] = ( p_sys->i_ssrc >>  8 )&0xff;
368         p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
369
370         p_buffer->i_size = 12;
371     }
372
373     return p_buffer;
374 }
375
376 /*****************************************************************************
377  * ThreadWrite: Write a packet on the network at the good time.
378  *****************************************************************************/
379 static void ThreadWrite( vlc_object_t *p_this )
380 {
381     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
382     sout_instance_t      *p_sout = p_thread->p_sout;
383     mtime_t              i_pts_delay;
384     mtime_t              i_date_last = -1;
385
386     /* Get the i_pts_delay value */
387     i_pts_delay = config_GetInt( p_this, "udp-sout-caching" ) * 1000;
388
389     while( ! p_thread->b_die )
390     {
391         sout_buffer_t *p_pk;
392         mtime_t       i_date;
393
394         p_pk = sout_FifoGet( p_thread->p_fifo );
395
396         i_date = i_pts_delay + p_pk->i_dts;
397         if( i_date_last > 0 )
398         {
399             if( i_date - i_date_last > 2000000 )
400             {
401                 msg_Dbg( p_thread, "mmh, hole > 2s -> drop" );
402
403                 sout_BufferDelete( p_sout, p_pk );
404                 i_date_last = i_date;
405                 continue;
406             }
407             else if( i_date - i_date_last < 0 )
408             {
409                 msg_Dbg( p_thread, "mmh, paquets in the past -> drop" );
410
411                 sout_BufferDelete( p_sout, p_pk );
412                 i_date_last = i_date;
413                 continue;
414             }
415         }
416
417
418         mwait( i_date );
419         send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_size, 0 );
420         sout_BufferDelete( p_sout, p_pk );
421         i_date_last = i_date;
422     }
423 }