1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <sys/types.h>
41 #include <vlc_block.h>
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # include <sys/socket.h>
50 #include <vlc_network.h>
52 #define MAX_EMPTY_BLOCKS 200
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t
* );
58 static void Close( vlc_object_t
* );
60 #define SOUT_CFG_PREFIX "sout-udp-"
62 #define CACHING_TEXT N_("Caching value (ms)")
63 #define CACHING_LONGTEXT N_( \
64 "Default caching value for outbound UDP streams. This " \
65 "value should be set in milliseconds." )
67 #define GROUP_TEXT N_("Group packets")
68 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
69 "or by groups. You can choose the number " \
70 "of packets that will be sent at a time. It " \
71 "helps reducing the scheduling load on " \
72 "heavily-loaded systems." )
75 set_description( N_("UDP stream output") )
76 set_shortname( "UDP" )
77 set_category( CAT_SOUT
)
78 set_subcategory( SUBCAT_SOUT_ACO
)
79 add_integer( SOUT_CFG_PREFIX
"caching", DEFAULT_PTS_DELAY
/ 1000, CACHING_TEXT
, CACHING_LONGTEXT
, true )
80 add_integer( SOUT_CFG_PREFIX
"group", 1, GROUP_TEXT
, GROUP_LONGTEXT
,
83 set_capability( "sout access", 0 )
85 set_callbacks( Open
, Close
)
88 /*****************************************************************************
90 *****************************************************************************/
92 static const char *const ppsz_sout_options
[] = {
98 /* Options handled by the libvlc network core */
99 static const char *const ppsz_core_options
[] = {
106 static ssize_t
Write ( sout_access_out_t
*, block_t
* );
107 static int Control( sout_access_out_t
*, int, va_list );
109 static void* ThreadWrite( void * );
110 static block_t
*NewUDPPacket( sout_access_out_t
*, mtime_t
);
112 struct sout_access_out_sys_t
119 block_fifo_t
*p_fifo
;
120 block_fifo_t
*p_empty_blocks
;
126 #define DEFAULT_PORT 1234
128 /*****************************************************************************
129 * Open: open the file
130 *****************************************************************************/
131 static int Open( vlc_object_t
*p_this
)
133 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
134 sout_access_out_sys_t
*p_sys
;
136 char *psz_dst_addr
= NULL
;
141 config_ChainParse( p_access
, SOUT_CFG_PREFIX
,
142 ppsz_sout_options
, p_access
->p_cfg
);
143 config_ChainParse( p_access
, "",
144 ppsz_core_options
, p_access
->p_cfg
);
146 if (var_Create (p_access
, "dst-port", VLC_VAR_INTEGER
)
147 || var_Create (p_access
, "src-port", VLC_VAR_INTEGER
)
148 || var_Create (p_access
, "dst-addr", VLC_VAR_STRING
)
149 || var_Create (p_access
, "src-addr", VLC_VAR_STRING
))
154 if( !( p_sys
= malloc ( sizeof( *p_sys
) ) ) )
156 p_access
->p_sys
= p_sys
;
158 i_dst_port
= DEFAULT_PORT
;
159 char *psz_parser
= psz_dst_addr
= strdup( p_access
->psz_path
);
166 if (psz_parser
[0] == '[')
167 psz_parser
= strchr (psz_parser
, ']');
169 psz_parser
= strchr (psz_parser
? psz_parser
: psz_dst_addr
, ':');
170 if (psz_parser
!= NULL
)
172 *psz_parser
++ = '\0';
173 i_dst_port
= atoi (psz_parser
);
176 i_handle
= net_ConnectDgram( p_this
, psz_dst_addr
, i_dst_port
, -1,
182 msg_Err( p_access
, "failed to create raw UDP socket" );
188 char addr
[NI_MAXNUMERICHOST
];
191 if (net_GetSockAddress (i_handle
, addr
, &port
) == 0)
193 msg_Dbg (p_access
, "source: %s port %d", addr
, port
);
194 var_SetString (p_access
, "src-addr", addr
);
195 var_SetInteger (p_access
, "src-port", port
);
198 if (net_GetPeerAddress (i_handle
, addr
, &port
) == 0)
200 msg_Dbg (p_access
, "destination: %s port %d", addr
, port
);
201 var_SetString (p_access
, "dst-addr", addr
);
202 var_SetInteger (p_access
, "dst-port", port
);
205 shutdown( i_handle
, SHUT_RD
);
207 p_sys
->i_caching
= UINT64_C(1000)
208 * var_GetInteger( p_access
, SOUT_CFG_PREFIX
"caching");
209 p_sys
->i_handle
= i_handle
;
210 p_sys
->i_mtu
= var_CreateGetInteger( p_this
, "mtu" );
211 p_sys
->b_mtu_warning
= false;
212 p_sys
->p_fifo
= block_FifoNew();
213 p_sys
->p_empty_blocks
= block_FifoNew();
214 p_sys
->p_buffer
= NULL
;
216 if( vlc_clone( &p_sys
->thread
, ThreadWrite
, p_access
,
217 VLC_THREAD_PRIORITY_HIGHEST
) )
219 msg_Err( p_access
, "cannot spawn sout access thread" );
220 block_FifoRelease( p_sys
->p_fifo
);
221 block_FifoRelease( p_sys
->p_empty_blocks
);
222 net_Close (i_handle
);
227 p_access
->pf_write
= Write
;
228 p_access
->pf_control
= Control
;
233 /*****************************************************************************
234 * Close: close the target
235 *****************************************************************************/
236 static void Close( vlc_object_t
* p_this
)
238 sout_access_out_t
*p_access
= (sout_access_out_t
*)p_this
;
239 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
241 vlc_cancel( p_sys
->thread
);
242 vlc_join( p_sys
->thread
, NULL
);
243 block_FifoRelease( p_sys
->p_fifo
);
244 block_FifoRelease( p_sys
->p_empty_blocks
);
246 if( p_sys
->p_buffer
) block_Release( p_sys
->p_buffer
);
248 net_Close( p_sys
->i_handle
);
252 static int Control( sout_access_out_t
*p_access
, int i_query
, va_list args
)
258 case ACCESS_OUT_CONTROLS_PACE
:
259 *va_arg( args
, bool * ) = false;
268 /*****************************************************************************
269 * Write: standard write on a file descriptor.
270 *****************************************************************************/
271 static ssize_t
Write( sout_access_out_t
*p_access
, block_t
*p_buffer
)
273 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
280 mtime_t now
= mdate();
282 if( !p_sys
->b_mtu_warning
&& p_buffer
->i_buffer
> p_sys
->i_mtu
)
284 msg_Warn( p_access
, "packet size > MTU, you should probably "
285 "increase the MTU" );
286 p_sys
->b_mtu_warning
= true;
289 /* Check if there is enough space in the buffer */
290 if( p_sys
->p_buffer
&&
291 p_sys
->p_buffer
->i_buffer
+ p_buffer
->i_buffer
> p_sys
->i_mtu
)
293 if( p_sys
->p_buffer
->i_dts
+ p_sys
->i_caching
< now
)
295 msg_Dbg( p_access
, "late packet for UDP input (%"PRId64
")",
296 now
- p_sys
->p_buffer
->i_dts
297 - p_sys
->i_caching
);
299 block_FifoPut( p_sys
->p_fifo
, p_sys
->p_buffer
);
300 p_sys
->p_buffer
= NULL
;
303 i_len
+= p_buffer
->i_buffer
;
304 while( p_buffer
->i_buffer
)
306 size_t i_payload_size
= p_sys
->i_mtu
;
307 size_t i_write
= __MIN( p_buffer
->i_buffer
, i_payload_size
);
311 if( !p_sys
->p_buffer
)
313 p_sys
->p_buffer
= NewUDPPacket( p_access
, p_buffer
->i_dts
);
314 if( !p_sys
->p_buffer
) break;
317 memcpy( p_sys
->p_buffer
->p_buffer
+ p_sys
->p_buffer
->i_buffer
,
318 p_buffer
->p_buffer
, i_write
);
320 p_sys
->p_buffer
->i_buffer
+= i_write
;
321 p_buffer
->p_buffer
+= i_write
;
322 p_buffer
->i_buffer
-= i_write
;
323 if ( p_buffer
->i_flags
& BLOCK_FLAG_CLOCK
)
325 if ( p_sys
->p_buffer
->i_flags
& BLOCK_FLAG_CLOCK
)
326 msg_Warn( p_access
, "putting two PCRs at once" );
327 p_sys
->p_buffer
->i_flags
|= BLOCK_FLAG_CLOCK
;
330 if( p_sys
->p_buffer
->i_buffer
== p_sys
->i_mtu
|| i_packets
> 1 )
333 if( p_sys
->p_buffer
->i_dts
+ p_sys
->i_caching
< now
)
335 msg_Dbg( p_access
, "late packet for udp input (%"PRId64
")",
336 mdate() - p_sys
->p_buffer
->i_dts
337 - p_sys
->i_caching
);
339 block_FifoPut( p_sys
->p_fifo
, p_sys
->p_buffer
);
340 p_sys
->p_buffer
= NULL
;
344 p_next
= p_buffer
->p_next
;
345 block_Release( p_buffer
);
352 /*****************************************************************************
353 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
354 *****************************************************************************/
355 static block_t
*NewUDPPacket( sout_access_out_t
*p_access
, mtime_t i_dts
)
357 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
360 while ( block_FifoCount( p_sys
->p_empty_blocks
) > MAX_EMPTY_BLOCKS
)
362 p_buffer
= block_FifoGet( p_sys
->p_empty_blocks
);
363 block_Release( p_buffer
);
366 if( block_FifoCount( p_sys
->p_empty_blocks
) == 0 )
368 p_buffer
= block_Alloc( p_sys
->i_mtu
);
372 p_buffer
= block_FifoGet(p_sys
->p_empty_blocks
);
373 p_buffer
->i_flags
= 0;
374 p_buffer
= block_Realloc( p_buffer
, 0, p_sys
->i_mtu
);
377 p_buffer
->i_dts
= i_dts
;
378 p_buffer
->i_buffer
= 0;
383 /*****************************************************************************
384 * ThreadWrite: Write a packet on the network at the good time.
385 *****************************************************************************/
386 static void* ThreadWrite( void *data
)
388 sout_access_out_t
*p_access
= data
;
389 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
390 mtime_t i_date_last
= -1;
391 const unsigned i_group
= var_GetInteger( p_access
,
392 SOUT_CFG_PREFIX
"group" );
393 mtime_t i_to_send
= i_group
;
394 unsigned i_dropped_packets
= 0;
398 block_t
*p_pk
= block_FifoGet( p_sys
->p_fifo
);
399 mtime_t i_date
, i_sent
;
401 i_date
= p_sys
->i_caching
+ p_pk
->i_dts
;
402 if( i_date_last
> 0 )
404 if( i_date
- i_date_last
> 2000000 )
406 if( !i_dropped_packets
)
407 msg_Dbg( p_access
, "mmh, hole (%"PRId64
" > 2s) -> drop",
408 i_date
- i_date_last
);
410 block_FifoPut( p_sys
->p_empty_blocks
, p_pk
);
412 i_date_last
= i_date
;
416 else if( i_date
- i_date_last
< -1000 )
418 if( !i_dropped_packets
)
419 msg_Dbg( p_access
, "mmh, packets in the past (%"PRId64
")",
420 i_date_last
- i_date
);
424 block_cleanup_push( p_pk
);
426 if( !i_to_send
|| (p_pk
->i_flags
& BLOCK_FLAG_CLOCK
) )
431 if ( send( p_sys
->i_handle
, p_pk
->p_buffer
, p_pk
->i_buffer
, 0 ) == -1 )
432 msg_Warn( p_access
, "send error: %s", vlc_strerror_c(errno
) );
435 if( i_dropped_packets
)
437 msg_Dbg( p_access
, "dropped %i packets", i_dropped_packets
);
438 i_dropped_packets
= 0;
443 if ( i_sent
> i_date
+ 20000 )
445 msg_Dbg( p_access
, "packet has been sent too late (%"PRId64
")",
450 block_FifoPut( p_sys
->p_empty_blocks
, p_pk
);
452 i_date_last
= i_date
;