]> git.sesse.net Git - vlc/blob - modules/access/udp.c
* src/misc/darwin_specific.m: Partial attempt at fixing a memory leak,
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP & RTP access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.10 2002/12/31 01:54:35 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Tristan Leteurtre <tooney@via.ecp.fr>
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
38 #ifdef HAVE_UNISTD_H
39 #   include <unistd.h>
40 #elif defined( _MSC_VER ) && defined( _WIN32 )
41 #   include <io.h>
42 #endif
43
44 #ifdef WIN32
45 #   include <winsock2.h>
46 #   include <ws2tcpip.h>
47 #   ifndef IN_MULTICAST
48 #       define IN_MULTICAST(a) IN_CLASSD(a)
49 #   endif
50 #else
51 #   include <sys/socket.h>
52 #endif
53
54 #include "network.h"
55
56 #define RTP_HEADER_LEN 12
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int  Open       ( vlc_object_t * );
62 static void Close      ( vlc_object_t * );
63 static ssize_t Read    ( input_thread_t *, byte_t *, size_t );
64 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
65 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
66
67 /*****************************************************************************
68  * Module descriptor
69  *****************************************************************************/
70 #define CACHING_TEXT N_("caching value in ms")
71 #define CACHING_LONGTEXT N_( \
72     "Allows you to modify the default caching value for udp streams. This " \
73     "value should be set in miliseconds units." )
74
75 vlc_module_begin();
76     set_description( _("raw UDP access module") );
77     add_category_hint( N_("udp"), NULL );
78     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
79     set_capability( "access", 0 );
80     add_shortcut( "udp" );
81     add_shortcut( "udpstream" );
82     add_shortcut( "udp4" );
83     add_shortcut( "udp6" );
84     add_shortcut( "rtp" );
85     add_shortcut( "rtp4" );
86     add_shortcut( "rtp6" );
87     set_callbacks( Open, Close );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * Open: open the socket
92  *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
94 {
95     input_thread_t *    p_input = (input_thread_t *)p_this;
96     input_socket_t *    p_access_data;
97     module_t *          p_network;
98     char *              psz_network = "";
99     char *              psz_name = strdup(p_input->psz_name);
100     char *              psz_parser = psz_name;
101     char *              psz_server_addr = "";
102     char *              psz_server_port = "";
103     char *              psz_bind_addr = "";
104     char *              psz_bind_port = "";
105     int                 i_bind_port = 0, i_server_port = 0;
106     network_socket_t    socket_desc;
107
108     if( config_GetInt( p_input, "ipv4" ) )
109     {
110         psz_network = "ipv4";
111     }
112     if( config_GetInt( p_input, "ipv6" ) )
113     {
114         psz_network = "ipv6";
115     }
116
117     if( *p_input->psz_access )
118     {
119         /* Find out which shortcut was used */
120         if( !strncmp( p_input->psz_access, "udp6", 5 ) )
121         {
122             psz_network = "ipv6";
123         }
124         else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
125         {
126             psz_network = "ipv4";
127         }
128     }
129
130     /* Parse psz_name syntax :
131      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
132
133     if( *psz_parser && *psz_parser != '@' )
134     {
135         /* Found server */
136         psz_server_addr = psz_parser;
137
138         while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
139         {
140             if( *psz_parser == '[' )
141             {
142                 /* IPv6 address */
143                 while( *psz_parser && *psz_parser != ']' )
144                 {
145                     psz_parser++;
146                 }
147             }
148             psz_parser++;
149         }
150
151         if( *psz_parser == ':' )
152         {
153             /* Found server port */
154             *psz_parser = '\0'; /* Terminate server name */
155             psz_parser++;
156             psz_server_port = psz_parser;
157
158             while( *psz_parser && *psz_parser != '@' )
159             {
160                 psz_parser++;
161             }
162         }
163     }
164
165     if( *psz_parser == '@' )
166     {
167         /* Found bind address or bind port */
168         *psz_parser = '\0'; /* Terminate server port or name if necessary */
169         psz_parser++;
170
171         if( *psz_parser && *psz_parser != ':' )
172         {
173             /* Found bind address */
174             psz_bind_addr = psz_parser;
175
176             while( *psz_parser && *psz_parser != ':' )
177             {
178                 if( *psz_parser == '[' )
179                 {
180                     /* IPv6 address */
181                     while( *psz_parser && *psz_parser != ']' )
182                     {
183                         psz_parser++;
184                     }
185                 }
186                 psz_parser++;
187             }
188         }
189
190         if( *psz_parser == ':' )
191         {
192             /* Found bind port */
193             *psz_parser = '\0'; /* Terminate bind address if necessary */
194             psz_parser++;
195
196             psz_bind_port = psz_parser;
197         }
198     }
199
200     /* Convert ports format */
201     if( *psz_server_port )
202     {
203         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
204         if( *psz_parser )
205         {
206             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
207             free(psz_name);
208             return( -1 );
209         }
210     }
211
212     if( *psz_bind_port )
213     {
214         i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
215         if( *psz_parser )
216         {
217             msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
218             free(psz_name);
219             return( -1 );
220         }
221     }
222
223     if( i_bind_port == 0 )
224     {
225         i_bind_port = config_GetInt( p_this, "server-port" );
226     }
227
228     p_input->pf_read = RTPChoose;
229     p_input->pf_set_program = input_SetProgram;
230     p_input->pf_set_area = NULL;
231     p_input->pf_seek = NULL;
232
233     vlc_mutex_lock( &p_input->stream.stream_lock );
234     p_input->stream.b_pace_control = 0;
235     p_input->stream.b_seekable = 0;
236     p_input->stream.b_connected = 0;
237     p_input->stream.p_selected_area->i_tell = 0;
238     p_input->stream.i_method = INPUT_METHOD_NETWORK;
239     vlc_mutex_unlock( &p_input->stream.stream_lock );
240
241     if( *psz_server_addr || i_server_port )
242     {
243         msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
244         msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
245                           psz_server_addr, i_server_port);
246         msg_Err( p_input, "or local port, type : %s:@%s:%d",
247                           *p_input->psz_access ? p_input->psz_access : "udp",
248                           psz_server_addr, i_server_port );
249
250         i_server_port = 0;
251         psz_server_addr = "";
252     }
253  
254     msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
255              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
256
257     /* Prepare the network_socket_t structure */
258     socket_desc.i_type = NETWORK_UDP;
259     socket_desc.psz_bind_addr = psz_bind_addr;
260     socket_desc.i_bind_port = i_bind_port;
261     socket_desc.psz_server_addr = psz_server_addr;
262     socket_desc.i_server_port = i_server_port;
263
264     /* Find an appropriate network module */
265     p_input->p_private = (void*) &socket_desc;
266     p_network = module_Need( p_input, "network", psz_network );
267     free(psz_name);
268     if( p_network == NULL )
269     {
270         return( -1 );
271     }
272     module_Unneed( p_input, p_network );
273     
274     p_access_data = malloc( sizeof(input_socket_t) );
275     p_input->p_access_data = (access_sys_t *)p_access_data;
276
277     if( p_access_data == NULL )
278     {
279         msg_Err( p_input, "out of memory" );
280         return( -1 );
281     }
282
283     p_access_data->i_handle = socket_desc.i_handle;
284     p_input->i_mtu = socket_desc.i_mtu;
285
286     /* Update default_pts to a suitable value for udp access */
287     p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
288
289     return( 0 );
290 }
291
292 /*****************************************************************************
293  * Close: free unused data structures
294  *****************************************************************************/
295 static void Close( vlc_object_t *p_this )
296 {
297     input_thread_t *  p_input = (input_thread_t *)p_this;
298     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
299
300     msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
301
302 #ifdef UNDER_CE
303     CloseHandle( (HANDLE)p_access_data->i_handle );
304 #elif defined( WIN32 )
305     closesocket( p_access_data->i_handle );
306 #else
307     close( p_access_data->i_handle );
308 #endif
309
310     free( p_access_data );
311 }
312
313 /*****************************************************************************
314  * Read: read on a file descriptor, checking b_die periodically
315  *****************************************************************************/
316 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
317 {
318 #ifdef UNDER_CE
319     return -1;
320
321 #else
322     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
323     struct timeval  timeout;
324     fd_set          fds;
325     int             i_ret;
326
327     /* Initialize file descriptor set */
328     FD_ZERO( &fds );
329     FD_SET( p_access_data->i_handle, &fds );
330
331     /* We'll wait 0.5 second if nothing happens */
332     timeout.tv_sec = 0;
333     timeout.tv_usec = 500000;
334
335     /* Find if some data is available */
336     i_ret = select( p_access_data->i_handle + 1, &fds,
337                     NULL, NULL, &timeout );
338
339     if( i_ret == -1 && errno != EINTR )
340     {
341         msg_Err( p_input, "network select error (%s)", strerror(errno) );
342     }
343     else if( i_ret > 0 )
344     {
345         ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
346
347         if( i_recv < 0 )
348         {
349             msg_Err( p_input, "recv failed (%s)", strerror(errno) );
350         }
351
352         return i_recv;
353     }
354
355     return 0;
356
357 #endif
358 }
359
360 /*****************************************************************************
361  * RTPRead : read from the network, and parse the RTP header
362  *****************************************************************************/
363 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
364                         size_t i_len )
365 {
366     int         i_rtp_version;
367     int         i_CSRC_count;
368     int         i_payload_type;
369
370     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
371
372     /* Get the raw data from the socket.
373      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
374     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
375
376     if ( !i_ret ) return 0;
377
378     /* Parse the header and make some verifications.
379      * See RFC 1889 & RFC 2250. */
380
381     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
382     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
383     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
384
385     if ( i_rtp_version != 2 )
386         msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
387
388     if ( i_payload_type != 33 && i_payload_type != 14
389           && i_payload_type != 32 )
390         msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
391
392     /* Return the packet without the RTP header. */
393     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
394
395     if ( (size_t)i_ret > i_len )
396     {
397         /* This should NOT happen. */
398         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
399         i_ret = i_len;
400     }
401
402     p_input->p_vlc->pf_memcpy( p_buffer,
403                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
404                        i_ret );
405
406     return i_ret;
407 }
408
409 /*****************************************************************************
410  * RTPChoose : read from the network, and decide whether it's UDP or RTP
411  *****************************************************************************/
412 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
413                           size_t i_len )
414 {
415     int         i_rtp_version;
416     int         i_CSRC_count;
417     int         i_payload_type;
418
419     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
420
421     /* Get the raw data from the socket.
422      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
423     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
424
425     if ( !i_ret ) return 0;
426     
427     /* Check that it's not TS. */
428     if ( p_tmp_buffer[0] == 0x47 )
429     {
430         msg_Dbg( p_input, "detected TS over raw UDP" );
431         p_input->pf_read = Read;
432         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
433         return i_ret;
434     }
435
436     /* Parse the header and make some verifications.
437      * See RFC 1889 & RFC 2250. */
438
439     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
440     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
441     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
442
443     if ( i_rtp_version != 2 )
444     {
445         msg_Dbg( p_input, "no RTP header detected" );
446         p_input->pf_read = Read;
447         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
448         return i_ret;
449     }
450
451     switch ( i_payload_type )
452     {
453     case 33:
454         msg_Dbg( p_input, "detected TS over RTP" );
455         break;
456
457     case 14:
458         msg_Dbg( p_input, "detected MPEG audio over RTP" );
459         break;
460
461     case 32:
462         msg_Dbg( p_input, "detected MPEG video over RTP" );
463         break;
464
465     default:
466         msg_Dbg( p_input, "no RTP header detected" );
467         p_input->pf_read = Read;
468         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
469         return i_ret;
470     }
471
472     /* Return the packet without the RTP header. */
473     p_input->pf_read = RTPRead;
474     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
475
476     if ( (size_t)i_ret > i_len )
477     {
478         /* This should NOT happen. */
479         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
480         i_ret = i_len;
481     }
482
483     p_input->p_vlc->pf_memcpy( p_buffer,
484                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
485                        i_ret );
486
487     return i_ret;
488 }