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