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