]> git.sesse.net Git - vlc/blob - modules/access_output/udp.c
* src/stream_output/stream_output.c, include/stream_output.h: new sout_AccessOutRead...
[vlc] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.17 2004/01/23 17:56:14 gbazin Exp $
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/input.h>
37 #include <vlc/sout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #ifdef WIN32
44 #   include <winsock2.h>
45 #   include <ws2tcpip.h>
46 #   ifndef IN_MULTICAST
47 #       define IN_MULTICAST(a) IN_CLASSD(a)
48 #   endif
49 #else
50 #   include <sys/socket.h>
51 #endif
52
53 #include "network.h"
54
55 #define DEFAULT_PORT 1234
56 /*****************************************************************************
57  * Exported prototypes
58  *****************************************************************************/
59 static int     Open   ( vlc_object_t * );
60 static void    Close  ( vlc_object_t * );
61
62 static int     Write   ( sout_access_out_t *, sout_buffer_t * );
63 static int     WriteRaw( sout_access_out_t *, sout_buffer_t * );
64 static int     Seek    ( sout_access_out_t *, off_t  );
65
66 static void    ThreadWrite( vlc_object_t * );
67
68 static sout_buffer_t *NewUDPPacket( sout_access_out_t *, mtime_t );
69
70 /*****************************************************************************
71  * Module descriptor
72  *****************************************************************************/
73 #define CACHING_TEXT N_("caching value in ms")
74 #define CACHING_LONGTEXT N_( \
75     "Allows you to modify the default caching value for udp streams. This " \
76     "value should be set in miliseconds units." )
77
78 vlc_module_begin();
79     set_description( _("UDP stream ouput") );
80     add_category_hint( N_("udp stream output"), NULL , VLC_TRUE );
81     add_integer( "udp-sout-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
82     set_capability( "sout access", 100 );
83     add_shortcut( "udp" );
84     add_shortcut( "rtp" ); // Will work only with ts muxer
85     set_callbacks( Open, Close );
86 vlc_module_end();
87
88 typedef struct sout_access_thread_s
89 {
90     VLC_COMMON_MEMBERS
91
92     sout_instance_t *p_sout;
93
94     sout_fifo_t *p_fifo;
95
96     int         i_handle;
97
98     int64_t     i_caching;
99
100 } sout_access_thread_t;
101
102 struct sout_access_out_sys_t
103 {
104     int                 b_rtpts;  // 1 if add rtp/ts header
105     uint16_t            i_sequence_number;
106     uint32_t            i_ssrc;
107
108     unsigned int        i_mtu;
109
110     sout_buffer_t       *p_buffer;
111
112     sout_access_thread_t *p_thread;
113
114 };
115
116 /*****************************************************************************
117  * Open: open the file
118  *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
120 {
121     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
122     sout_access_out_sys_t   *p_sys;
123
124     char                *psz_parser;
125     char                *psz_dst_addr;
126     int                 i_dst_port;
127
128     module_t            *p_network;
129     network_socket_t    socket_desc;
130
131     vlc_value_t         val;
132     char                *psz_val;
133
134     if( !( p_sys = p_access->p_sys =
135                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
136     {
137         msg_Err( p_access, "Not enough memory" );
138         return VLC_EGENERIC;
139     }
140
141
142     if( p_access->psz_access != NULL &&
143         !strcmp( p_access->psz_access, "rtp" ) )
144     {
145         msg_Warn( p_access, "becarefull that rtp ouput work only with ts "
146                   "payload(not an error)" );
147         p_sys->b_rtpts = 1;
148     }
149     else
150     {
151         p_sys->b_rtpts = 0;
152     }
153
154     psz_parser = strdup( p_access->psz_name );
155
156     psz_dst_addr = psz_parser;
157     i_dst_port = 0;
158
159     if ( *psz_parser == '[' )
160     {
161         while( *psz_parser && *psz_parser != ']' )
162         {
163             psz_parser++;
164         }
165     }
166     while( *psz_parser && *psz_parser != ':' )
167     {
168         psz_parser++;
169     }
170     if( *psz_parser == ':' )
171     {
172         *psz_parser = '\0';
173         psz_parser++;
174         i_dst_port = atoi( psz_parser );
175     }
176     if( i_dst_port <= 0 )
177     {
178         i_dst_port = DEFAULT_PORT;
179     }
180
181     p_sys->p_thread =
182         vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
183     if( !p_sys->p_thread )
184     {
185         msg_Err( p_access, "out of memory" );
186         return VLC_EGENERIC;
187     }
188
189     p_sys->p_thread->p_sout = p_access->p_sout;
190     p_sys->p_thread->b_die  = 0;
191     p_sys->p_thread->b_error= 0;
192     p_sys->p_thread->p_fifo = sout_FifoCreate( p_access->p_sout );
193
194     socket_desc.i_type = NETWORK_UDP;
195     socket_desc.psz_server_addr = psz_dst_addr;
196     socket_desc.i_server_port   = i_dst_port;
197     socket_desc.psz_bind_addr   = "";
198     socket_desc.i_bind_port     = 0;
199     socket_desc.i_ttl           = 0;
200     if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "ttl" ) ) )
201     {
202         socket_desc.i_ttl = atoi( psz_val );
203     }
204     p_sys->p_thread->p_private = (void*)&socket_desc;
205     if( !( p_network = module_Need( p_sys->p_thread, "network", "" ) ) )
206     {
207         msg_Err( p_access, "failed to open a connection (udp)" );
208         return VLC_EGENERIC;
209     }
210     module_Unneed( p_sys->p_thread, p_network );
211
212     p_sys->p_thread->i_handle = socket_desc.i_handle;
213
214     var_Create( p_this, "udp-sout-caching",
215                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
216     var_Get( p_this, "udp-sout-caching", &val );
217     p_sys->p_thread->i_caching = val.i_int * 1000;
218     if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "caching" ) ) )
219     {
220         p_sys->p_thread->i_caching = atoll( psz_val ) * 1000;
221     }
222
223     p_sys->i_mtu = socket_desc.i_mtu;
224
225     if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
226                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
227     {
228         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
229         vlc_object_destroy( p_sys->p_thread );
230         return VLC_EGENERIC;
231     }
232
233     srand( (uint32_t)mdate());
234     p_sys->p_buffer          = NULL;
235     p_sys->i_sequence_number = rand()&0xffff;
236     p_sys->i_ssrc            = rand()&0xffffffff;
237
238     if( sout_cfg_find( p_access->p_cfg, "raw" ) )
239     {
240         p_access->pf_write = WriteRaw;
241     }
242     else
243     {
244         p_access->pf_write = Write;
245     }
246
247     p_access->pf_seek = Seek;
248
249     msg_Info( p_access, "Open: addr:`%s' port:`%d'", psz_dst_addr, i_dst_port);
250
251     free( psz_dst_addr );
252     return VLC_SUCCESS;
253 }
254
255 /*****************************************************************************
256  * Close: close the target
257  *****************************************************************************/
258 static void Close( vlc_object_t * p_this )
259 {
260     sout_access_out_t     *p_access = (sout_access_out_t*)p_this;
261     sout_access_out_sys_t *p_sys = p_access->p_sys;
262     int i;
263
264     p_sys->p_thread->b_die = 1;
265     for( i = 0; i < 10; i++ )
266     {
267         sout_buffer_t *p_dummy;
268
269         p_dummy = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
270         p_dummy->i_dts = 0;
271         p_dummy->i_pts = 0;
272         p_dummy->i_length = 0;
273         sout_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
274     }
275     vlc_thread_join( p_sys->p_thread );
276
277     sout_FifoDestroy( p_access->p_sout, p_sys->p_thread->p_fifo );
278
279     if( p_sys->p_buffer )
280     {
281         sout_BufferDelete( p_access->p_sout, p_sys->p_buffer );
282     }
283
284 #if defined( UNDER_CE )
285     CloseHandle( (HANDLE)p_sys->p_thread->i_handle );
286 #elif defined( WIN32 )
287     closesocket( p_sys->p_thread->i_handle );
288 #else
289     close( p_sys->p_thread->i_handle );
290 #endif
291
292     free( p_sys );
293     msg_Info( p_access, "Close" );
294 }
295
296 /*****************************************************************************
297  * Write: standard write on a file descriptor.
298  *****************************************************************************/
299 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
300 {
301     sout_access_out_sys_t *p_sys = p_access->p_sys;
302     unsigned int i_write;
303
304     while( p_buffer )
305     {
306         sout_buffer_t *p_next;
307         if( p_buffer->i_size > p_sys->i_mtu )
308         {
309             msg_Warn( p_access, "arggggggggggggg packet size > mtu" );
310             i_write = p_sys->i_mtu;
311         }
312         else
313         {
314             i_write = p_buffer->i_size;
315         }
316
317         /* if we have enough data, enque the buffer */
318         if( p_sys->p_buffer &&
319             p_sys->p_buffer->i_size + i_write > p_sys->i_mtu )
320         {
321             sout_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
322             p_sys->p_buffer = NULL;
323         }
324
325         if( !p_sys->p_buffer )
326         {
327             p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
328         }
329
330         if( p_buffer->i_size > 0 )
331         {
332             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_size,
333                     p_buffer->p_buffer, i_write );
334             p_sys->p_buffer->i_size += i_write;
335         }
336         p_next = p_buffer->p_next;
337         sout_BufferDelete( p_access->p_sout, p_buffer );
338         p_buffer = p_next;
339     }
340
341     return( p_sys->p_thread->b_error ? -1 : 0 );
342 }
343
344 /*****************************************************************************
345  * WriteRaw: write p_buffer without trying to fill mtu
346  *****************************************************************************/
347 static int WriteRaw( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
348 {
349     sout_access_out_sys_t   *p_sys = p_access->p_sys;
350
351     sout_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
352
353     return( p_sys->p_thread->b_error ? -1 : 0 );
354 }
355
356 /*****************************************************************************
357  * Seek: seek to a specific location in a file
358  *****************************************************************************/
359 static int Seek( sout_access_out_t *p_access, off_t i_pos )
360 {
361     msg_Err( p_access, "udp sout access cannot seek" );
362     return( -1 );
363 }
364
365 /*****************************************************************************
366  * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
367  *****************************************************************************/
368 static sout_buffer_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
369 {
370     sout_access_out_sys_t *p_sys = p_access->p_sys;
371     sout_buffer_t *p_buffer;
372
373     p_buffer = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
374     p_buffer->i_dts = i_dts;
375     p_buffer->i_size = 0;
376
377     if( p_sys->b_rtpts )
378     {
379         mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
380
381         /* add rtp/ts header */
382         p_buffer->p_buffer[0] = 0x80;
383         p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
384
385         p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
386         p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
387         p_sys->i_sequence_number++;
388
389         p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
390         p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
391         p_buffer->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
392         p_buffer->p_buffer[7] = i_timestamp&0xff;
393
394         p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
395         p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
396         p_buffer->p_buffer[10] = ( p_sys->i_ssrc >>  8 )&0xff;
397         p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
398
399         p_buffer->i_size = 12;
400     }
401
402     return p_buffer;
403 }
404
405 /*****************************************************************************
406  * ThreadWrite: Write a packet on the network at the good time.
407  *****************************************************************************/
408 static void ThreadWrite( vlc_object_t *p_this )
409 {
410     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
411     sout_instance_t      *p_sout = p_thread->p_sout;
412     mtime_t              i_date_last = -1;
413
414     while( ! p_thread->b_die )
415     {
416         sout_buffer_t *p_pk;
417         mtime_t       i_date, i_sent;
418
419         p_pk = sout_FifoGet( p_thread->p_fifo );
420
421         i_date = p_thread->i_caching + p_pk->i_dts;
422         if( i_date_last > 0 )
423         {
424             if( i_date - i_date_last > 2000000 )
425             {
426                 msg_Dbg( p_thread, "mmh, hole > 2s -> drop" );
427
428                 sout_BufferDelete( p_sout, p_pk );
429                 i_date_last = i_date;
430                 continue;
431             }
432             else if( i_date - i_date_last < 0 )
433             {
434                 msg_Dbg( p_thread, "mmh, paquets in the past -> drop" );
435
436                 sout_BufferDelete( p_sout, p_pk );
437                 i_date_last = i_date;
438                 continue;
439             }
440         }
441
442         mwait( i_date );
443         send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_size, 0 );
444         i_sent = mdate();
445         if ( i_sent > i_date + 20000 )
446         {
447             msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
448                      i_sent - i_date );
449         }
450         sout_BufferDelete( p_sout, p_pk );
451         i_date_last = i_date;
452     }
453 }