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