]> git.sesse.net Git - vlc/blob - modules/access_output/udp.c
527e5be35d1664b36081158cb8555469ee02fd9e
[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", 100 );
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
117 static void ThreadWrite( vlc_object_t * );
118 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
119 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
120
121 typedef struct sout_access_thread_t
122 {
123     VLC_COMMON_MEMBERS
124
125     sout_instance_t *p_sout;
126
127     block_fifo_t *p_fifo;
128
129     int         i_handle;
130
131     int64_t     i_caching;
132     int         i_group;
133
134     block_fifo_t *p_empty_blocks;
135 } sout_access_thread_t;
136
137 struct sout_access_out_sys_t
138 {
139     int                 i_mtu;
140     bool          b_mtu_warning;
141
142     block_t             *p_buffer;
143
144     sout_access_thread_t *p_thread;
145
146 };
147
148 #define DEFAULT_PORT 1234
149
150 /*****************************************************************************
151  * Open: open the file
152  *****************************************************************************/
153 static int Open( vlc_object_t *p_this )
154 {
155     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
156     sout_access_out_sys_t   *p_sys;
157
158     char                *psz_dst_addr = NULL;
159     int                 i_dst_port;
160
161     int                 i_handle;
162
163     config_ChainParse( p_access, SOUT_CFG_PREFIX,
164                        ppsz_sout_options, p_access->p_cfg );
165     config_ChainParse( p_access, "",
166                        ppsz_core_options, p_access->p_cfg );
167
168     if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
169      || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
170      || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
171      || var_Create (p_access, "src-addr", VLC_VAR_STRING))
172     {
173         return VLC_ENOMEM;
174     }
175
176     if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
177         return VLC_ENOMEM;
178     p_access->p_sys = p_sys;
179
180     i_dst_port = DEFAULT_PORT;
181     char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
182     if( !psz_dst_addr )
183     {
184         free( p_sys );
185         return VLC_ENOMEM;
186     }
187
188     if (psz_parser[0] == '[')
189         psz_parser = strchr (psz_parser, ']');
190
191     psz_parser = strchr (psz_parser ?: psz_dst_addr, ':');
192     if (psz_parser != NULL)
193     {
194         *psz_parser++ = '\0';
195         i_dst_port = atoi (psz_parser);
196     }
197
198     p_sys->p_thread =
199         vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
200     if( !p_sys->p_thread )
201     {
202         free (p_sys);
203         free (psz_dst_addr);
204         return VLC_ENOMEM;
205     }
206
207     vlc_object_attach( p_sys->p_thread, p_access );
208     p_sys->p_thread->p_sout = p_access->p_sout;
209     p_sys->p_thread->b_die  = 0;
210     p_sys->p_thread->b_error= 0;
211     p_sys->p_thread->p_fifo = block_FifoNew();
212     p_sys->p_thread->p_empty_blocks = block_FifoNew();
213
214     i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1,
215                                  IPPROTO_UDP );
216     free (psz_dst_addr);
217
218     if( i_handle == -1 )
219     {
220          msg_Err( p_access, "failed to create raw UDP socket" );
221          vlc_object_release (p_sys->p_thread);
222          free (p_sys);
223          return VLC_EGENERIC;
224     }
225     else
226     {
227         char addr[NI_MAXNUMERICHOST];
228         int port;
229
230         if (net_GetSockAddress (i_handle, addr, &port) == 0)
231         {
232             msg_Dbg (p_access, "source: %s port %d", addr, port);
233             var_SetString (p_access, "src-addr", addr);
234             var_SetInteger (p_access, "src-port", port);
235         }
236
237         if (net_GetPeerAddress (i_handle, addr, &port) == 0)
238         {
239             msg_Dbg (p_access, "destination: %s port %d", addr, port);
240             var_SetString (p_access, "dst-addr", addr);
241             var_SetInteger (p_access, "dst-port", port);
242         }
243     }
244     p_sys->p_thread->i_handle = i_handle;
245     shutdown( i_handle, SHUT_RD );
246
247     p_sys->p_thread->i_caching =
248         (int64_t)1000 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
249     p_sys->p_thread->i_group =
250         var_GetInteger( p_access, SOUT_CFG_PREFIX "group" );
251
252     p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
253     p_sys->p_buffer = NULL;
254
255     if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
256                            VLC_THREAD_PRIORITY_HIGHEST, false ) )
257     {
258         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
259         net_Close (i_handle);
260         vlc_object_release( p_sys->p_thread );
261         free (p_sys);
262         return VLC_EGENERIC;
263     }
264
265     p_access->pf_write = Write;
266     p_access->pf_seek = Seek;
267
268     /* update p_sout->i_out_pace_nocontrol */
269     p_access->p_sout->i_out_pace_nocontrol++;
270
271     return VLC_SUCCESS;
272 }
273
274 /*****************************************************************************
275  * Close: close the target
276  *****************************************************************************/
277 static void Close( vlc_object_t * p_this )
278 {
279     sout_access_out_t     *p_access = (sout_access_out_t*)p_this;
280     sout_access_out_sys_t *p_sys = p_access->p_sys;
281     int i;
282
283     vlc_object_kill( p_sys->p_thread );
284     block_FifoWake( p_sys->p_thread->p_fifo );
285
286     for( i = 0; i < 10; i++ )
287     {
288         block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
289         p_dummy->i_dts = 0;
290         p_dummy->i_pts = 0;
291         p_dummy->i_length = 0;
292         memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
293         block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
294     }
295     vlc_thread_join( p_sys->p_thread );
296
297     block_FifoRelease( p_sys->p_thread->p_fifo );
298     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
299
300     if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
301
302     net_Close( p_sys->p_thread->i_handle );
303
304     vlc_object_detach( p_sys->p_thread );
305     vlc_object_release( p_sys->p_thread );
306     /* update p_sout->i_out_pace_nocontrol */
307     p_access->p_sout->i_out_pace_nocontrol--;
308
309     msg_Dbg( p_access, "UDP access output closed" );
310     free( p_sys );
311 }
312
313 /*****************************************************************************
314  * Write: standard write on a file descriptor.
315  *****************************************************************************/
316 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
317 {
318     sout_access_out_sys_t *p_sys = p_access->p_sys;
319     int i_len = 0;
320
321     while( p_buffer )
322     {
323         block_t *p_next;
324         int i_packets = 0;
325         mtime_t now = mdate();
326
327         if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
328         {
329             msg_Warn( p_access, "packet size > MTU, you should probably "
330                       "increase the MTU" );
331             p_sys->b_mtu_warning = true;
332         }
333
334         /* Check if there is enough space in the buffer */
335         if( p_sys->p_buffer &&
336             p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
337         {
338             if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
339             {
340                 msg_Dbg( p_access, "late packet for UDP input (%"PRId64 ")",
341                          now - p_sys->p_buffer->i_dts
342                           - p_sys->p_thread->i_caching );
343             }
344             block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
345             p_sys->p_buffer = NULL;
346         }
347
348         i_len += p_buffer->i_buffer;
349         while( p_buffer->i_buffer )
350         {
351             int i_payload_size = p_sys->i_mtu;
352
353             int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
354
355             i_packets++;
356
357             if( !p_sys->p_buffer )
358             {
359                 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
360                 if( !p_sys->p_buffer ) break;
361             }
362
363             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
364                     p_buffer->p_buffer, i_write );
365
366             p_sys->p_buffer->i_buffer += i_write;
367             p_buffer->p_buffer += i_write;
368             p_buffer->i_buffer -= i_write;
369             if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
370             {
371                 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
372                     msg_Warn( p_access, "putting two PCRs at once" );
373                 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
374             }
375
376             if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
377             {
378                 /* Flush */
379                 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
380                 {
381                     msg_Dbg( p_access, "late packet for udp input (%"PRId64 ")",
382                              mdate() - p_sys->p_buffer->i_dts
383                               - p_sys->p_thread->i_caching );
384                 }
385                 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
386                 p_sys->p_buffer = NULL;
387             }
388         }
389
390         p_next = p_buffer->p_next;
391         block_Release( p_buffer );
392         p_buffer = p_next;
393     }
394
395     return( p_sys->p_thread->b_error ? -1 : i_len );
396 }
397
398 /*****************************************************************************
399  * Seek: seek to a specific location in a file
400  *****************************************************************************/
401 static int Seek( sout_access_out_t *p_access, off_t i_pos )
402 {
403     msg_Err( p_access, "UDP sout access cannot seek" );
404     return -1;
405 }
406
407 /*****************************************************************************
408  * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
409  *****************************************************************************/
410 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
411 {
412     sout_access_out_sys_t *p_sys = p_access->p_sys;
413     block_t *p_buffer;
414
415     while ( block_FifoCount( p_sys->p_thread->p_empty_blocks ) > MAX_EMPTY_BLOCKS )
416     {
417         p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
418         block_Release( p_buffer );
419     }
420
421     if( block_FifoCount( p_sys->p_thread->p_empty_blocks ) == 0 )
422     {
423         p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
424     }
425     else
426     {
427         p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
428         p_buffer->i_flags = 0;
429         p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
430     }
431
432     p_buffer->i_dts = i_dts;
433     p_buffer->i_buffer = 0;
434
435     return p_buffer;
436 }
437
438 /*****************************************************************************
439  * ThreadWrite: Write a packet on the network at the good time.
440  *****************************************************************************/
441 static void ThreadWrite( vlc_object_t *p_this )
442 {
443     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
444     mtime_t              i_date_last = -1;
445     mtime_t              i_to_send = p_thread->i_group;
446     int                  i_dropped_packets = 0;
447
448     while( !p_thread->b_die )
449     {
450         block_t *p_pk;
451         mtime_t       i_date, i_sent;
452 #if 0
453         if( (i++ % 1000)==0 ) {
454           int i = 0;
455           int j = 0;
456           block_t *p_tmp = p_thread->p_empty_blocks->p_first;
457           while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
458           p_tmp = p_thread->p_fifo->p_first;
459           while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
460           msg_Dbg( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
461                    p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
462         }
463 #endif
464         p_pk = block_FifoGet( p_thread->p_fifo );
465         if( p_pk == NULL )
466             continue; /* forced wake-up */
467
468         i_date = p_thread->i_caching + p_pk->i_dts;
469         if( i_date_last > 0 )
470         {
471             if( i_date - i_date_last > 2000000 )
472             {
473                 if( !i_dropped_packets )
474                     msg_Dbg( p_thread, "mmh, hole (%"PRId64" > 2s) -> drop",
475                              i_date - i_date_last );
476
477                 block_FifoPut( p_thread->p_empty_blocks, p_pk );
478
479                 i_date_last = i_date;
480                 i_dropped_packets++;
481                 continue;
482             }
483             else if( i_date - i_date_last < -1000 )
484             {
485                 if( !i_dropped_packets )
486                     msg_Dbg( p_thread, "mmh, packets in the past (%"PRId64")",
487                              i_date_last - i_date );
488             }
489         }
490
491         i_to_send--;
492         if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
493         {
494             mwait( i_date );
495             i_to_send = p_thread->i_group;
496         }
497         ssize_t val = send( p_thread->i_handle, p_pk->p_buffer,
498                             p_pk->i_buffer, 0 );
499         if (val == -1)
500         {
501             msg_Warn( p_thread, "send error: %m" );
502         }
503
504         if( i_dropped_packets )
505         {
506             msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
507             i_dropped_packets = 0;
508         }
509
510 #if 1
511         i_sent = mdate();
512         if ( i_sent > i_date + 20000 )
513         {
514             msg_Dbg( p_thread, "packet has been sent too late (%"PRId64 ")",
515                      i_sent - i_date );
516         }
517 #endif
518
519         block_FifoPut( p_thread->p_empty_blocks, p_pk );
520
521         i_date_last = i_date;
522     }
523 }