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