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