]> git.sesse.net Git - vlc/blob - modules/access_output/udp.c
Avoid ?: GCC-ism
[vlc] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <assert.h>
40
41 #include <vlc_sout.h>
42 #include <vlc_block.h>
43
44 #ifdef HAVE_UNISTD_H
45 #   include <unistd.h>
46 #endif
47
48 #ifdef WIN32
49 #   include <winsock2.h>
50 #   include <ws2tcpip.h>
51 #else
52 #   include <sys/socket.h>
53 #endif
54
55 #include <vlc_network.h>
56
57 #define MAX_EMPTY_BLOCKS 200
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 static int  Open ( vlc_object_t * );
63 static void Close( vlc_object_t * );
64
65 #define SOUT_CFG_PREFIX "sout-udp-"
66
67 #define CACHING_TEXT N_("Caching value (ms)")
68 #define CACHING_LONGTEXT N_( \
69     "Default caching value for outbound UDP streams. This " \
70     "value should be set in milliseconds." )
71
72 #define GROUP_TEXT N_("Group packets")
73 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
74                           "or by groups. You can choose the number " \
75                           "of packets that will be sent at a time. It " \
76                           "helps reducing the scheduling load on " \
77                           "heavily-loaded systems." )
78
79 vlc_module_begin ()
80     set_description( N_("UDP stream output") )
81     set_shortname( "UDP" )
82     set_category( CAT_SOUT )
83     set_subcategory( SUBCAT_SOUT_ACO )
84     add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
85     add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
86                                  true )
87     add_obsolete_integer( SOUT_CFG_PREFIX "late" )
88     add_obsolete_bool( SOUT_CFG_PREFIX "raw" )
89
90     set_capability( "sout access", 0 )
91     add_shortcut( "udp" )
92     set_callbacks( Open, Close )
93 vlc_module_end ()
94
95 /*****************************************************************************
96  * Exported prototypes
97  *****************************************************************************/
98
99 static const char *const ppsz_sout_options[] = {
100     "caching",
101     "group",
102     NULL
103 };
104
105 /* Options handled by the libvlc network core */
106 static const char *const ppsz_core_options[] = {
107     "dscp",
108     "ttl",
109     "miface",
110     "miface-addr",
111     NULL
112 };
113
114 static ssize_t Write   ( sout_access_out_t *, block_t * );
115 static int  Seek    ( sout_access_out_t *, off_t  );
116 static int Control( sout_access_out_t *, int, va_list );
117
118 static void* ThreadWrite( void * );
119 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
120
121 struct sout_access_out_sys_t
122 {
123     mtime_t       i_caching;
124     int           i_handle;
125     bool          b_mtu_warning;
126     size_t        i_mtu;
127
128     block_fifo_t *p_fifo;
129     block_fifo_t *p_empty_blocks;
130     block_t      *p_buffer;
131
132     vlc_thread_t  thread;
133 };
134
135 #define DEFAULT_PORT 1234
136
137 /*****************************************************************************
138  * Open: open the file
139  *****************************************************************************/
140 static int Open( vlc_object_t *p_this )
141 {
142     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
143     sout_access_out_sys_t   *p_sys;
144
145     char                *psz_dst_addr = NULL;
146     int                 i_dst_port;
147
148     int                 i_handle;
149
150     config_ChainParse( p_access, SOUT_CFG_PREFIX,
151                        ppsz_sout_options, p_access->p_cfg );
152     config_ChainParse( p_access, "",
153                        ppsz_core_options, p_access->p_cfg );
154
155     if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
156      || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
157      || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
158      || var_Create (p_access, "src-addr", VLC_VAR_STRING))
159     {
160         return VLC_ENOMEM;
161     }
162
163     if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
164         return VLC_ENOMEM;
165     p_access->p_sys = p_sys;
166
167     i_dst_port = DEFAULT_PORT;
168     char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
169     if( !psz_dst_addr )
170     {
171         free( p_sys );
172         return VLC_ENOMEM;
173     }
174
175     if (psz_parser[0] == '[')
176         psz_parser = strchr (psz_parser, ']');
177
178     psz_parser = strchr (psz_parser ? psz_parser : psz_dst_addr, ':');
179     if (psz_parser != NULL)
180     {
181         *psz_parser++ = '\0';
182         i_dst_port = atoi (psz_parser);
183     }
184
185     i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1,
186                                  IPPROTO_UDP );
187     free (psz_dst_addr);
188
189     if( i_handle == -1 )
190     {
191          msg_Err( p_access, "failed to create raw UDP socket" );
192          free (p_sys);
193          return VLC_EGENERIC;
194     }
195     else
196     {
197         char addr[NI_MAXNUMERICHOST];
198         int port;
199
200         if (net_GetSockAddress (i_handle, addr, &port) == 0)
201         {
202             msg_Dbg (p_access, "source: %s port %d", addr, port);
203             var_SetString (p_access, "src-addr", addr);
204             var_SetInteger (p_access, "src-port", port);
205         }
206
207         if (net_GetPeerAddress (i_handle, addr, &port) == 0)
208         {
209             msg_Dbg (p_access, "destination: %s port %d", addr, port);
210             var_SetString (p_access, "dst-addr", addr);
211             var_SetInteger (p_access, "dst-port", port);
212         }
213     }
214     shutdown( i_handle, SHUT_RD );
215
216     p_sys->i_caching = UINT64_C(1000)
217                      * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
218     p_sys->i_handle = i_handle;
219     p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
220     p_sys->b_mtu_warning = false;
221     p_sys->p_fifo = block_FifoNew();
222     p_sys->p_empty_blocks = block_FifoNew();
223     p_sys->p_buffer = NULL;
224
225     if( vlc_clone( &p_sys->thread, ThreadWrite, p_access,
226                            VLC_THREAD_PRIORITY_HIGHEST ) )
227     {
228         msg_Err( p_access, "cannot spawn sout access thread" );
229         block_FifoRelease( p_sys->p_fifo );
230         block_FifoRelease( p_sys->p_empty_blocks );
231         net_Close (i_handle);
232         free (p_sys);
233         return VLC_EGENERIC;
234     }
235
236     p_access->pf_write = Write;
237     p_access->pf_seek = Seek;
238     p_access->pf_control = Control;
239
240     return VLC_SUCCESS;
241 }
242
243 /*****************************************************************************
244  * Close: close the target
245  *****************************************************************************/
246 static void Close( vlc_object_t * p_this )
247 {
248     sout_access_out_t     *p_access = (sout_access_out_t*)p_this;
249     sout_access_out_sys_t *p_sys = p_access->p_sys;
250
251     vlc_cancel( p_sys->thread );
252     vlc_join( p_sys->thread, NULL );
253     block_FifoRelease( p_sys->p_fifo );
254     block_FifoRelease( p_sys->p_empty_blocks );
255
256     if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
257
258     net_Close( p_sys->i_handle );
259     free( p_sys );
260 }
261
262 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
263 {
264     (void)p_access;
265
266     switch( i_query )
267     {
268         case ACCESS_OUT_CONTROLS_PACE:
269             *va_arg( args, bool * ) = false;
270             break;
271
272         default:
273             return VLC_EGENERIC;
274     }
275     return VLC_SUCCESS;
276 }
277
278 /*****************************************************************************
279  * Write: standard write on a file descriptor.
280  *****************************************************************************/
281 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
282 {
283     sout_access_out_sys_t *p_sys = p_access->p_sys;
284     int i_len = 0;
285
286     while( p_buffer )
287     {
288         block_t *p_next;
289         int i_packets = 0;
290         mtime_t now = mdate();
291
292         if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
293         {
294             msg_Warn( p_access, "packet size > MTU, you should probably "
295                       "increase the MTU" );
296             p_sys->b_mtu_warning = true;
297         }
298
299         /* Check if there is enough space in the buffer */
300         if( p_sys->p_buffer &&
301             p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
302         {
303             if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
304             {
305                 msg_Dbg( p_access, "late packet for UDP input (%"PRId64 ")",
306                          now - p_sys->p_buffer->i_dts
307                           - p_sys->i_caching );
308             }
309             block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
310             p_sys->p_buffer = NULL;
311         }
312
313         i_len += p_buffer->i_buffer;
314         while( p_buffer->i_buffer )
315         {
316             size_t i_payload_size = p_sys->i_mtu;
317             size_t i_write = __MIN( p_buffer->i_buffer, i_payload_size );
318
319             i_packets++;
320
321             if( !p_sys->p_buffer )
322             {
323                 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
324                 if( !p_sys->p_buffer ) break;
325             }
326
327             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
328                     p_buffer->p_buffer, i_write );
329
330             p_sys->p_buffer->i_buffer += i_write;
331             p_buffer->p_buffer += i_write;
332             p_buffer->i_buffer -= i_write;
333             if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
334             {
335                 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
336                     msg_Warn( p_access, "putting two PCRs at once" );
337                 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
338             }
339
340             if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
341             {
342                 /* Flush */
343                 if( p_sys->p_buffer->i_dts + p_sys->i_caching < now )
344                 {
345                     msg_Dbg( p_access, "late packet for udp input (%"PRId64 ")",
346                              mdate() - p_sys->p_buffer->i_dts
347                               - p_sys->i_caching );
348                 }
349                 block_FifoPut( p_sys->p_fifo, p_sys->p_buffer );
350                 p_sys->p_buffer = NULL;
351             }
352         }
353
354         p_next = p_buffer->p_next;
355         block_Release( p_buffer );
356         p_buffer = p_next;
357     }
358
359     return i_len;
360 }
361
362 /*****************************************************************************
363  * Seek: seek to a specific location in a file
364  *****************************************************************************/
365 static int Seek( sout_access_out_t *p_access, off_t i_pos )
366 {
367     (void) i_pos;
368     msg_Err( p_access, "UDP sout access cannot seek" );
369     return -1;
370 }
371
372 /*****************************************************************************
373  * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
374  *****************************************************************************/
375 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
376 {
377     sout_access_out_sys_t *p_sys = p_access->p_sys;
378     block_t *p_buffer;
379
380     while ( block_FifoCount( p_sys->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
381     {
382         p_buffer = block_FifoGet( p_sys->p_empty_blocks );
383         block_Release( p_buffer );
384     }
385
386     if( block_FifoCount( p_sys->p_empty_blocks ) == 0 )
387     {
388         p_buffer = block_Alloc( p_sys->i_mtu );
389     }
390     else
391     {
392         p_buffer = block_FifoGet(p_sys->p_empty_blocks );
393         p_buffer->i_flags = 0;
394         p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
395     }
396
397     p_buffer->i_dts = i_dts;
398     p_buffer->i_buffer = 0;
399
400     return p_buffer;
401 }
402
403 /*****************************************************************************
404  * ThreadWrite: Write a packet on the network at the good time.
405  *****************************************************************************/
406 static void* ThreadWrite( void *data )
407 {
408     sout_access_out_t *p_access = data;
409     sout_access_out_sys_t *p_sys = p_access->p_sys;
410     mtime_t i_date_last = -1;
411     const unsigned i_group = var_GetInteger( p_access,
412                                              SOUT_CFG_PREFIX "group" );
413     mtime_t i_to_send = i_group;
414     unsigned i_dropped_packets = 0;
415
416     for (;;)
417     {
418         block_t *p_pk = block_FifoGet( p_sys->p_fifo );
419         mtime_t       i_date, i_sent;
420
421         i_date = p_sys->i_caching + p_pk->i_dts;
422         if( i_date_last > 0 )
423         {
424             if( i_date - i_date_last > 2000000 )
425             {
426                 if( !i_dropped_packets )
427                     msg_Dbg( p_access, "mmh, hole (%"PRId64" > 2s) -> drop",
428                              i_date - i_date_last );
429
430                 block_FifoPut( p_sys->p_empty_blocks, p_pk );
431
432                 i_date_last = i_date;
433                 i_dropped_packets++;
434                 continue;
435             }
436             else if( i_date - i_date_last < -1000 )
437             {
438                 if( !i_dropped_packets )
439                     msg_Dbg( p_access, "mmh, packets in the past (%"PRId64")",
440                              i_date_last - i_date );
441             }
442         }
443
444         block_cleanup_push( p_pk );
445         i_to_send--;
446         if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
447         {
448             mwait( i_date );
449             i_to_send = i_group;
450         }
451         if ( send( p_sys->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 ) == -1 )
452             msg_Warn( p_access, "send error: %m" );
453         vlc_cleanup_pop();
454
455         if( i_dropped_packets )
456         {
457             msg_Dbg( p_access, "dropped %i packets", i_dropped_packets );
458             i_dropped_packets = 0;
459         }
460
461 #if 1
462         i_sent = mdate();
463         if ( i_sent > i_date + 20000 )
464         {
465             msg_Dbg( p_access, "packet has been sent too late (%"PRId64 ")",
466                      i_sent - i_date );
467         }
468 #endif
469
470         block_FifoPut( p_sys->p_empty_blocks, p_pk );
471
472         i_date_last = i_date;
473     }
474     return NULL;
475 }