]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Reviewed file:
[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.24 2003/10/23 14:30:26 jpsaman Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Tristan Leteurtre <tooney@via.ecp.fr>
9  *
10  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <fcntl.h>
36
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #ifdef HAVE_SYS_TIME_H
41 #    include <sys/time.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #   include <unistd.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( _("UDP/RTP input") );
81     add_category_hint( N_("udp"), NULL , VLC_TRUE );
82     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
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     vlc_value_t         val;
112
113     if( config_GetInt( p_input, "ipv4" ) )
114     {
115         psz_network = "ipv4";
116     }
117     if( config_GetInt( p_input, "ipv6" ) )
118     {
119         psz_network = "ipv6";
120     }
121
122     if( *p_input->psz_access )
123     {
124         /* Find out which shortcut was used */
125         if( !strncmp( p_input->psz_access, "udp6", 5 ) )
126         {
127             psz_network = "ipv6";
128         }
129         else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
130         {
131             psz_network = "ipv4";
132         }
133     }
134
135     /* Parse psz_name syntax :
136      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
137
138     if( *psz_parser && *psz_parser != '@' )
139     {
140         /* Found server */
141         psz_server_addr = psz_parser;
142
143         while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
144         {
145             if( *psz_parser == '[' )
146             {
147                 /* IPv6 address */
148                 while( *psz_parser && *psz_parser != ']' )
149                 {
150                     psz_parser++;
151                 }
152             }
153             psz_parser++;
154         }
155
156         if( *psz_parser == ':' )
157         {
158             /* Found server port */
159             *psz_parser = '\0'; /* Terminate server name */
160             psz_parser++;
161             psz_server_port = psz_parser;
162
163             while( *psz_parser && *psz_parser != '@' )
164             {
165                 psz_parser++;
166             }
167         }
168     }
169
170     if( *psz_parser == '@' )
171     {
172         /* Found bind address or bind port */
173         *psz_parser = '\0'; /* Terminate server port or name if necessary */
174         psz_parser++;
175
176         if( *psz_parser && *psz_parser != ':' )
177         {
178             /* Found bind address */
179             psz_bind_addr = psz_parser;
180
181             while( *psz_parser && *psz_parser != ':' )
182             {
183                 if( *psz_parser == '[' )
184                 {
185                     /* IPv6 address */
186                     while( *psz_parser && *psz_parser != ']' )
187                     {
188                         psz_parser++;
189                     }
190                 }
191                 psz_parser++;
192             }
193         }
194
195         if( *psz_parser == ':' )
196         {
197             /* Found bind port */
198             *psz_parser = '\0'; /* Terminate bind address if necessary */
199             psz_parser++;
200
201             psz_bind_port = psz_parser;
202         }
203     }
204
205     /* Convert ports format */
206     if( *psz_server_port )
207     {
208         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
209         if( *psz_parser )
210         {
211             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
212             free(psz_name);
213             return( -1 );
214         }
215     }
216
217     if( *psz_bind_port )
218     {
219         i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
220         if( *psz_parser )
221         {
222             msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
223             free(psz_name);
224             return( -1 );
225         }
226     }
227
228     if( i_bind_port == 0 )
229     {
230         i_bind_port = config_GetInt( p_this, "server-port" );
231     }
232
233     p_input->pf_read = RTPChoose;
234     p_input->pf_set_program = input_SetProgram;
235     p_input->pf_set_area = NULL;
236     p_input->pf_seek = NULL;
237
238     vlc_mutex_lock( &p_input->stream.stream_lock );
239     p_input->stream.b_pace_control = 0;
240     p_input->stream.b_seekable = 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     socket_desc.i_ttl           = 0;
268
269     /* Find an appropriate network module */
270     p_input->p_private = (void*) &socket_desc;
271     p_network = module_Need( p_input, "network", psz_network );
272     free(psz_name);
273     if( p_network == NULL )
274     {
275         return( -1 );
276     }
277     module_Unneed( p_input, p_network );
278
279     p_access_data = malloc( sizeof(input_socket_t) );
280     p_input->p_access_data = (access_sys_t *)p_access_data;
281
282     if( p_access_data == NULL )
283     {
284         msg_Err( p_input, "out of memory" );
285         return( -1 );
286     }
287
288     p_access_data->i_handle = socket_desc.i_handle;
289     p_input->i_mtu = socket_desc.i_mtu;
290
291     /* Update default_pts to a suitable value for udp access */
292     var_Create( p_input, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
293     var_Get( p_input, "udp-caching", &val );
294     p_input->i_pts_delay = val.i_int * 1000;
295
296     return( 0 );
297 }
298
299 /*****************************************************************************
300  * Close: free unused data structures
301  *****************************************************************************/
302 static void Close( vlc_object_t *p_this )
303 {
304     input_thread_t *  p_input = (input_thread_t *)p_this;
305     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
306
307     msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
308
309 #ifdef UNDER_CE
310     CloseHandle( (HANDLE)p_access_data->i_handle );
311 #elif defined( WIN32 )
312     closesocket( p_access_data->i_handle );
313 #else
314     close( p_access_data->i_handle );
315 #endif
316
317     free( p_access_data );
318 }
319
320 /*****************************************************************************
321  * Read: read on a file descriptor, checking b_die periodically
322  *****************************************************************************/
323 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
324 {
325 #ifdef UNDER_CE
326     return -1;
327
328 #else
329     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
330     struct timeval  timeout;
331     fd_set          fds;
332     ssize_t         i_recv;
333     int             i_ret;
334
335     /* Initialize file descriptor set */
336     FD_ZERO( &fds );
337     FD_SET( p_access_data->i_handle, &fds );
338
339     /* We'll wait 0.5 second if nothing happens */
340     timeout.tv_sec = 0;
341     timeout.tv_usec = 500000;
342
343     /* Find if some data is available */
344     while( ((i_ret = select( p_access_data->i_handle + 1, &fds,
345                             NULL, NULL, &timeout )) == 0)
346            || ((i_ret < 0) && (errno == EINTR)) )
347     {
348         FD_ZERO( &fds );
349         FD_SET( p_access_data->i_handle, &fds );
350         timeout.tv_sec = 0;
351         timeout.tv_usec = 500000;
352
353         if( p_input->b_die || p_input->b_error )
354         {
355             return 0;
356         }
357     }
358
359     if( i_ret < 0 )
360     {
361         msg_Err( p_input, "network select error (%s)", strerror(errno) );
362         return -1;
363     }
364
365     i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
366
367     if( i_recv < 0 )
368     {
369 #ifdef WIN32
370         /* On win32 recv() will fail if the datagram doesn't fit inside
371          * the passed buffer, even though the buffer will be filled with
372          * the first part of the datagram. */
373         if( WSAGetLastError() == WSAEMSGSIZE )
374         {
375             msg_Err( p_input, "recv() failed. "
376                      "Increase the mtu size (--mtu option)" );
377             i_recv = i_len;
378         }
379         else
380 #endif
381             msg_Err( p_input, "recv failed (%s)", strerror(errno) );
382     }
383
384     return i_recv;
385
386 #endif
387 }
388
389 /*****************************************************************************
390  * RTPRead : read from the network, and parse the RTP header
391  *****************************************************************************/
392 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
393                         size_t i_len )
394 {
395     int         i_rtp_version;
396     int         i_CSRC_count;
397     int         i_payload_type;
398
399     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
400
401     /* Get the raw data from the socket.
402      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
403     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
404
405     if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
406
407     /* Parse the header and make some verifications.
408      * See RFC 1889 & RFC 2250. */
409
410     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
411     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
412     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
413
414     if ( i_rtp_version != 2 )
415         msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
416
417     if ( i_payload_type != 33 && i_payload_type != 14
418           && i_payload_type != 32 )
419         msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
420
421     /* A CSRC extension field is 32 bits in size (4 bytes) */
422     if ( i_ret < (RTP_HEADER_LEN + 4*i_CSRC_count) )
423     {
424         /* Packet is not big enough to hold the complete RTP_HEADER with
425          * CSRC extensions.
426          */
427         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
428         return 0;
429     }
430
431     /* Return the packet without the RTP header. */
432     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
433
434     if ( (size_t)i_ret > i_len )
435     {
436         /* This should NOT happen. */
437         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
438         i_ret = i_len;
439     }
440
441     p_input->p_vlc->pf_memcpy( p_buffer,
442                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
443                        i_ret );
444
445     return i_ret;
446 }
447
448 /*****************************************************************************
449  * RTPChoose : read from the network, and decide whether it's UDP or RTP
450  *****************************************************************************/
451 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
452                           size_t i_len )
453 {
454     int         i_rtp_version;
455     int         i_CSRC_count;
456     int         i_payload_type;
457
458     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
459
460     /* Get the raw data from the socket.
461      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
462     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
463
464     if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
465     
466     /* Check that it's not TS. */
467     if ( p_tmp_buffer[0] == 0x47 )
468     {
469         msg_Dbg( p_input, "detected TS over raw UDP" );
470         p_input->pf_read = Read;
471         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
472         return i_ret;
473     }
474
475     /* Parse the header and make some verifications.
476      * See RFC 1889 & RFC 2250. */
477
478     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
479     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
480     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
481
482     if ( i_rtp_version != 2 )
483     {
484         msg_Dbg( p_input, "no supported RTP header detected" );
485         p_input->pf_read = Read;
486         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
487         return i_ret;
488     }
489
490     switch ( i_payload_type )
491     {
492     case 33:
493         msg_Dbg( p_input, "detected TS over RTP" );
494         break;
495
496     case 14:
497         msg_Dbg( p_input, "detected MPEG audio over RTP" );
498         break;
499
500     case 32:
501         msg_Dbg( p_input, "detected MPEG video over RTP" );
502         break;
503
504     default:
505         msg_Dbg( p_input, "no RTP header detected" );
506         p_input->pf_read = Read;
507         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
508         return i_ret;
509     }
510
511     p_input->pf_read = RTPRead;
512
513     /* A CSRC extension field is 32 bits in size (4 bytes) */
514     if ( i_ret < (RTP_HEADER_LEN + 4*i_CSRC_count) )
515     {
516         /* Packet is not big enough to hold the complete RTP_HEADER with
517          * CSRC extensions.
518          */
519         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
520         return 0;
521     }
522
523     /* Return the packet without the RTP header. */
524     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
525
526     if ( (size_t)i_ret > i_len )
527     {
528         /* This should NOT happen. */
529         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
530         i_ret = i_len;
531     }
532
533     p_input->p_vlc->pf_memcpy( p_buffer,
534                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
535                        i_ret );
536
537     return i_ret;
538 }