]> git.sesse.net Git - vlc/blob - modules/access_output/udp.c
Improvements to preferences
[vlc] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/sout.h>
37
38 #ifdef HAVE_UNISTD_H
39 #   include <unistd.h>
40 #endif
41
42 #ifdef WIN32
43 #   include <winsock2.h>
44 #   include <ws2tcpip.h>
45 #   ifndef IN_MULTICAST
46 #       define IN_MULTICAST(a) IN_CLASSD(a)
47 #   endif
48 #else
49 #   include <sys/socket.h>
50 #endif
51
52 #include "network.h"
53
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 #define SOUT_CFG_PREFIX "sout-udp-"
62
63 #define CACHING_TEXT N_("Caching value (ms)")
64 #define CACHING_LONGTEXT N_( \
65     "Allows you to modify the default caching value for UDP streams. This " \
66     "value should be set in millisecond units." )
67
68 #define TTL_TEXT N_("Time To Live")
69 #define TTL_LONGTEXT N_("Allows you to define the time to live of the " \
70                         "outgoing stream.")
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. This allows you to give 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 #define LATE_TEXT N_("Late delay (ms)" )
79 #define LATE_LONGTEXT N_("Late packets are dropped. This allows you to give " \
80                        "the time (in milliseconds) a packet is allowed to be" \
81                        " late.")
82 #define RAW_TEXT N_("Raw write")
83 #define RAW_LONGTEXT N_("If you enable this option, packets will be sent " \
84                        "directly, without trying to fill the MTU (ie, " \
85                        "without trying to make the biggest possible packets " \
86                        "in order to improve streaming)." )
87
88 vlc_module_begin();
89     set_description( _("UDP stream output") );
90     set_category( CAT_SOUT );
91     set_subcategory( SUBCAT_SOUT_ACO );
92     add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
93     add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL,TTL_TEXT, TTL_LONGTEXT,
94                                  VLC_TRUE );
95     add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
96                                  VLC_TRUE );
97     add_integer( SOUT_CFG_PREFIX "late", 0, NULL, LATE_TEXT, LATE_LONGTEXT,
98                                  VLC_TRUE );
99     add_bool( SOUT_CFG_PREFIX "raw",  0, NULL, RAW_TEXT, RAW_LONGTEXT,
100                                  VLC_TRUE );
101
102     set_capability( "sout access", 100 );
103     add_shortcut( "udp" );
104     add_shortcut( "rtp" ); // Will work only with ts muxer
105     set_callbacks( Open, Close );
106 vlc_module_end();
107
108 /*****************************************************************************
109  * Exported prototypes
110  *****************************************************************************/
111
112 static const char *ppsz_sout_options[] = {
113     "caching",
114     "ttl",
115     "group",
116     "late",
117     "raw",
118     NULL
119 };
120
121 static int  Write   ( sout_access_out_t *, block_t * );
122 static int  WriteRaw( sout_access_out_t *, block_t * );
123 static int  Seek    ( sout_access_out_t *, off_t  );
124
125 static void ThreadWrite( vlc_object_t * );
126 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
127
128 typedef struct sout_access_thread_t
129 {
130     VLC_COMMON_MEMBERS
131
132     sout_instance_t *p_sout;
133
134     block_fifo_t *p_fifo;
135
136     int         i_handle;
137
138     int64_t     i_caching;
139     int64_t     i_late;
140     int         i_group;
141
142 } sout_access_thread_t;
143
144 struct sout_access_out_sys_t
145 {
146     int                 b_rtpts;  // 1 if add rtp/ts header
147     uint16_t            i_sequence_number;
148     uint32_t            i_ssrc;
149
150     int                 i_mtu;
151
152     block_t             *p_buffer;
153
154     sout_access_thread_t *p_thread;
155
156     vlc_bool_t          b_mtu_warning;
157 };
158
159 #define DEFAULT_PORT 1234
160
161 /*****************************************************************************
162  * Open: open the file
163  *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
165 {
166     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
167     sout_access_out_sys_t   *p_sys;
168
169     char                *psz_parser;
170     char                *psz_dst_addr;
171     int                 i_dst_port;
172
173     module_t            *p_network;
174     network_socket_t    socket_desc;
175
176     vlc_value_t         val;
177
178     sout_CfgParse( p_access, SOUT_CFG_PREFIX,
179                    ppsz_sout_options, p_access->p_cfg );
180
181     if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
182     {
183         msg_Err( p_access, "not enough memory" );
184         return VLC_EGENERIC;
185     }
186     memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
187     p_access->p_sys = p_sys;
188
189     if( p_access->psz_access != NULL &&
190         !strcmp( p_access->psz_access, "rtp" ) )
191     {
192         msg_Warn( p_access, "be careful that rtp output only works with ts "
193                   "payload (not an error)" );
194         p_sys->b_rtpts = 1;
195     }
196     else
197     {
198         p_sys->b_rtpts = 0;
199     }
200
201     psz_parser = strdup( p_access->psz_name );
202
203     psz_dst_addr = psz_parser;
204     i_dst_port = 0;
205
206     if ( *psz_parser == '[' )
207     {
208         while( *psz_parser && *psz_parser != ']' )
209         {
210             psz_parser++;
211         }
212     }
213     while( *psz_parser && *psz_parser != ':' )
214     {
215         psz_parser++;
216     }
217     if( *psz_parser == ':' )
218     {
219         *psz_parser = '\0';
220         psz_parser++;
221         i_dst_port = atoi( psz_parser );
222     }
223     if( i_dst_port <= 0 )
224     {
225         i_dst_port = DEFAULT_PORT;
226     }
227
228     p_sys->p_thread =
229         vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
230     if( !p_sys->p_thread )
231     {
232         msg_Err( p_access, "out of memory" );
233         return VLC_EGENERIC;
234     }
235
236     p_sys->p_thread->p_sout = p_access->p_sout;
237     p_sys->p_thread->b_die  = 0;
238     p_sys->p_thread->b_error= 0;
239     p_sys->p_thread->p_fifo = block_FifoNew( p_access );
240
241     socket_desc.i_type = NETWORK_UDP;
242     socket_desc.psz_server_addr = psz_dst_addr;
243     socket_desc.i_server_port   = i_dst_port;
244     socket_desc.psz_bind_addr   = "";
245     socket_desc.i_bind_port     = 0;
246
247     var_Get( p_access, SOUT_CFG_PREFIX "ttl", &val );
248     socket_desc.i_ttl = val.i_int;
249
250     p_sys->p_thread->p_private = (void*)&socket_desc;
251     if( !( p_network = module_Need( p_sys->p_thread, "network", NULL, 0 ) ) )
252     {
253         msg_Err( p_access, "failed to open a connection (udp)" );
254         return VLC_EGENERIC;
255     }
256     module_Unneed( p_sys->p_thread, p_network );
257
258     p_sys->p_thread->i_handle = socket_desc.i_handle;
259
260     var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
261     p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
262
263     var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
264     p_sys->p_thread->i_group = val.i_int;
265
266     var_Get( p_access, SOUT_CFG_PREFIX "late", &val );
267     p_sys->p_thread->i_late = (int64_t)val.i_int * 1000;
268
269     p_sys->i_mtu = socket_desc.i_mtu;
270
271 #ifdef WIN32
272     if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
273                            VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
274 #else
275     if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
276                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
277 #endif
278     {
279         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
280         vlc_object_destroy( p_sys->p_thread );
281         return VLC_EGENERIC;
282     }
283
284     srand( (uint32_t)mdate());
285     p_sys->p_buffer          = NULL;
286     p_sys->i_sequence_number = rand()&0xffff;
287     p_sys->i_ssrc            = rand()&0xffffffff;
288
289     var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
290     if( val.b_bool )  p_access->pf_write = WriteRaw;
291     else p_access->pf_write = Write;
292
293     p_access->pf_seek = Seek;
294
295     msg_Dbg( p_access, "udp access output opened(%s:%d)",
296              psz_dst_addr, i_dst_port );
297
298     free( psz_dst_addr );
299
300     /* update p_sout->i_out_pace_nocontrol */
301     p_access->p_sout->i_out_pace_nocontrol++;
302
303     return VLC_SUCCESS;
304 }
305
306 /*****************************************************************************
307  * Close: close the target
308  *****************************************************************************/
309 static void Close( vlc_object_t * p_this )
310 {
311     sout_access_out_t     *p_access = (sout_access_out_t*)p_this;
312     sout_access_out_sys_t *p_sys = p_access->p_sys;
313     int i;
314
315     p_sys->p_thread->b_die = 1;
316     for( i = 0; i < 10; i++ )
317     {
318         block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
319         p_dummy->i_dts = 0;
320         p_dummy->i_pts = 0;
321         p_dummy->i_length = 0;
322         block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
323     }
324     vlc_thread_join( p_sys->p_thread );
325
326     block_FifoRelease( p_sys->p_thread->p_fifo );
327
328     if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
329
330     net_Close( p_sys->p_thread->i_handle );
331
332     /* update p_sout->i_out_pace_nocontrol */
333     p_access->p_sout->i_out_pace_nocontrol--;
334
335     msg_Dbg( p_access, "udp access output closed" );
336     free( p_sys );
337 }
338
339 /*****************************************************************************
340  * Write: standard write on a file descriptor.
341  *****************************************************************************/
342 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
343 {
344     sout_access_out_sys_t *p_sys = p_access->p_sys;
345
346     while( p_buffer )
347     {
348         block_t *p_next;
349         int i_packets = 0;
350
351         if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
352         {
353             msg_Warn( p_access, "packet size > MTU, you should probably "
354                       "increase the MTU" );
355             p_sys->b_mtu_warning = VLC_TRUE;
356         }
357
358         /* Check if there is enough space in the buffer */
359         if( p_sys->p_buffer &&
360             p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
361         {
362             block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
363             p_sys->p_buffer = NULL;
364         }
365
366         while( p_buffer->i_buffer )
367         {
368             int i_write = __MIN( p_buffer->i_buffer, p_sys->i_mtu );
369
370             i_packets++;
371
372             if( !p_sys->p_buffer )
373             {
374                 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
375                 if( !p_sys->p_buffer ) break;
376             }
377
378             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
379                     p_buffer->p_buffer, i_write );
380
381             p_sys->p_buffer->i_buffer += i_write;
382             p_buffer->p_buffer += i_write;
383             p_buffer->i_buffer -= i_write;
384
385             if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
386             {
387                 /* Flush */
388                 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
389                 p_sys->p_buffer = NULL;
390             }
391         }
392
393         p_next = p_buffer->p_next;
394         block_Release( p_buffer );
395         p_buffer = p_next;
396     }
397
398     return( p_sys->p_thread->b_error ? -1 : 0 );
399 }
400
401 /*****************************************************************************
402  * WriteRaw: write p_buffer without trying to fill mtu
403  *****************************************************************************/
404 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
405 {
406     sout_access_out_sys_t   *p_sys = p_access->p_sys;
407
408     block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
409
410     return( p_sys->p_thread->b_error ? -1 : 0 );
411 }
412
413 /*****************************************************************************
414  * Seek: seek to a specific location in a file
415  *****************************************************************************/
416 static int Seek( sout_access_out_t *p_access, off_t i_pos )
417 {
418     msg_Err( p_access, "UDP sout access cannot seek" );
419     return -1;
420 }
421
422 /*****************************************************************************
423  * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
424  *****************************************************************************/
425 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
426 {
427     sout_access_out_sys_t *p_sys = p_access->p_sys;
428     block_t *p_buffer;
429
430     p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
431     p_buffer->i_dts = i_dts;
432     p_buffer->i_buffer = 0;
433
434     if( p_sys->b_rtpts )
435     {
436         mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
437
438         /* add rtp/ts header */
439         p_buffer->p_buffer[0] = 0x80;
440         p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
441
442         p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
443         p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
444         p_sys->i_sequence_number++;
445
446         p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
447         p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
448         p_buffer->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
449         p_buffer->p_buffer[7] = i_timestamp&0xff;
450
451         p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
452         p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
453         p_buffer->p_buffer[10] = ( p_sys->i_ssrc >>  8 )&0xff;
454         p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
455
456         p_buffer->i_buffer = 12;
457     }
458
459     return p_buffer;
460 }
461
462 /*****************************************************************************
463  * ThreadWrite: Write a packet on the network at the good time.
464  *****************************************************************************/
465 static void ThreadWrite( vlc_object_t *p_this )
466 {
467     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
468     mtime_t              i_date_last = -1;
469     mtime_t              i_to_send = p_thread->i_group;
470     int                  i_dropped_packets = 0;
471
472     while( !p_thread->b_die )
473     {
474         block_t *p_pk;
475         mtime_t       i_date, i_sent;
476
477         p_pk = block_FifoGet( p_thread->p_fifo );
478
479         i_date = p_thread->i_caching + p_pk->i_dts;
480         if( i_date_last > 0 )
481         {
482             if( i_date - i_date_last > 2000000 )
483             {
484                 if( !i_dropped_packets )
485                     msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
486                              i_date - i_date_last );
487
488                 block_Release( p_pk  );
489                 i_date_last = i_date;
490                 i_dropped_packets++;
491                 continue;
492             }
493             else if( i_date - i_date_last < 0 )
494             {
495                 if( !i_dropped_packets )
496                     msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")"
497                              " -> drop", i_date - i_date_last );
498
499                 block_Release( p_pk  );
500                 i_date_last = i_date;
501                 i_dropped_packets++;
502                 continue;
503             }
504         }
505
506         i_sent = mdate();
507         if( p_thread->i_late > 0 && i_sent > i_date + p_thread->i_late )
508         {
509             if( !i_dropped_packets )
510             {
511                 msg_Dbg( p_thread, "late packet to send (" I64Fd ") -> drop",
512                          i_sent - i_date );
513             }
514             block_Release( p_pk  );
515             i_date_last = i_date;
516             i_dropped_packets++;
517             continue;
518         }
519
520         i_to_send--;
521         if ( !i_to_send )
522         {
523             mwait( i_date );
524             i_to_send = p_thread->i_group;
525         }
526         send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 );
527
528         if( i_dropped_packets )
529         {
530             msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
531             i_dropped_packets = 0;
532         }
533
534 #if 0
535         i_sent = mdate();
536         if ( i_sent > i_date + 20000 )
537         {
538             msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
539                      i_sent - i_date );
540         }
541 #endif
542
543         block_Release( p_pk  );
544         i_date_last = i_date;
545     }
546 }