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