]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Patch courtesy of gibalou :
[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.13 2003/02/12 13:42:43 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/time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <vlc/vlc.h>
37 #include <vlc/input.h>
38
39 #ifdef HAVE_SYS_TIME_H
40 #    include <sys/time.h>
41 #endif
42
43 #ifdef HAVE_UNISTD_H
44 #   include <unistd.h>
45 #elif defined( _MSC_VER ) && defined( _WIN32 )
46 #   include <io.h>
47 #endif
48
49 #ifdef WIN32
50 #   include <winsock2.h>
51 #   include <ws2tcpip.h>
52 #   ifndef IN_MULTICAST
53 #       define IN_MULTICAST(a) IN_CLASSD(a)
54 #   endif
55 #else
56 #   include <sys/socket.h>
57 #endif
58
59 #include "network.h"
60
61 #define RTP_HEADER_LEN 12
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int  Open       ( vlc_object_t * );
67 static void Close      ( vlc_object_t * );
68 static ssize_t Read    ( input_thread_t *, byte_t *, size_t );
69 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
70 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
71
72 /*****************************************************************************
73  * Module descriptor
74  *****************************************************************************/
75 #define CACHING_TEXT N_("caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77     "Allows you to modify the default caching value for udp streams. This " \
78     "value should be set in miliseconds units." )
79
80 vlc_module_begin();
81     set_description( _("raw UDP access module") );
82     add_category_hint( N_("udp"), NULL );
83     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
84     set_capability( "access", 0 );
85     add_shortcut( "udp" );
86     add_shortcut( "udpstream" );
87     add_shortcut( "udp4" );
88     add_shortcut( "udp6" );
89     add_shortcut( "rtp" );
90     add_shortcut( "rtp4" );
91     add_shortcut( "rtp6" );
92     set_callbacks( Open, Close );
93 vlc_module_end();
94
95 /*****************************************************************************
96  * Open: open the socket
97  *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
99 {
100     input_thread_t *    p_input = (input_thread_t *)p_this;
101     input_socket_t *    p_access_data;
102     module_t *          p_network;
103     char *              psz_network = "";
104     char *              psz_name = strdup(p_input->psz_name);
105     char *              psz_parser = psz_name;
106     char *              psz_server_addr = "";
107     char *              psz_server_port = "";
108     char *              psz_bind_addr = "";
109     char *              psz_bind_port = "";
110     int                 i_bind_port = 0, i_server_port = 0;
111     network_socket_t    socket_desc;
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.b_connected = 0;
242     p_input->stream.p_selected_area->i_tell = 0;
243     p_input->stream.i_method = INPUT_METHOD_NETWORK;
244     vlc_mutex_unlock( &p_input->stream.stream_lock );
245
246     if( *psz_server_addr || i_server_port )
247     {
248         msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
249         msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
250                           psz_server_addr, i_server_port);
251         msg_Err( p_input, "or local port, type : %s:@%s:%d",
252                           *p_input->psz_access ? p_input->psz_access : "udp",
253                           psz_server_addr, i_server_port );
254
255         i_server_port = 0;
256         psz_server_addr = "";
257     }
258  
259     msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
260              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
261
262     /* Prepare the network_socket_t structure */
263     socket_desc.i_type = NETWORK_UDP;
264     socket_desc.psz_bind_addr = psz_bind_addr;
265     socket_desc.i_bind_port = i_bind_port;
266     socket_desc.psz_server_addr = psz_server_addr;
267     socket_desc.i_server_port = i_server_port;
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     p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
293
294     return( 0 );
295 }
296
297 /*****************************************************************************
298  * Close: free unused data structures
299  *****************************************************************************/
300 static void Close( vlc_object_t *p_this )
301 {
302     input_thread_t *  p_input = (input_thread_t *)p_this;
303     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
304
305     msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
306
307 #ifdef UNDER_CE
308     CloseHandle( (HANDLE)p_access_data->i_handle );
309 #elif defined( WIN32 )
310     closesocket( p_access_data->i_handle );
311 #else
312     close( p_access_data->i_handle );
313 #endif
314
315     free( p_access_data );
316 }
317
318 /*****************************************************************************
319  * Read: read on a file descriptor, checking b_die periodically
320  *****************************************************************************/
321 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
322 {
323 #ifdef UNDER_CE
324     return -1;
325
326 #else
327     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
328     struct timeval  timeout;
329     fd_set          fds;
330     int             i_ret;
331
332     /* Initialize file descriptor set */
333     FD_ZERO( &fds );
334     FD_SET( p_access_data->i_handle, &fds );
335
336     /* We'll wait 0.5 second if nothing happens */
337     timeout.tv_sec = 0;
338     timeout.tv_usec = 500000;
339
340     /* Find if some data is available */
341     i_ret = select( p_access_data->i_handle + 1, &fds,
342                     NULL, NULL, &timeout );
343
344     if( i_ret == -1 && errno != EINTR )
345     {
346         msg_Err( p_input, "network select error (%s)", strerror(errno) );
347     }
348     else if( i_ret > 0 )
349     {
350         ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
351
352         if( i_recv < 0 )
353         {
354 #ifdef WIN32
355             /* On win32 recv() will fail if the datagram doesn't fit inside
356              * the passed buffer, even though the buffer will be filled with
357              * the first part of the datagram. */
358             if( WSAGetLastError() == WSAEMSGSIZE )
359             {
360                 msg_Err( p_input, "recv() failed. "
361                                   "Increase the mtu size (--mtu option)" );
362                 i_recv = i_len;
363             }
364             else
365 #endif
366             msg_Err( p_input, "recv failed (%s)", strerror(errno) );
367         }
368
369         return i_recv;
370     }
371
372     return 0;
373
374 #endif
375 }
376
377 /*****************************************************************************
378  * RTPRead : read from the network, and parse the RTP header
379  *****************************************************************************/
380 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
381                         size_t i_len )
382 {
383     int         i_rtp_version;
384     int         i_CSRC_count;
385     int         i_payload_type;
386
387     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
388
389     /* Get the raw data from the socket.
390      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
391     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
392
393     if ( !i_ret ) return 0;
394
395     /* Parse the header and make some verifications.
396      * See RFC 1889 & RFC 2250. */
397
398     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
399     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
400     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
401
402     if ( i_rtp_version != 2 )
403         msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
404
405     if ( i_payload_type != 33 && i_payload_type != 14
406           && i_payload_type != 32 )
407         msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
408
409     /* Return the packet without the RTP header. */
410     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
411
412     if ( (size_t)i_ret > i_len )
413     {
414         /* This should NOT happen. */
415         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
416         i_ret = i_len;
417     }
418
419     p_input->p_vlc->pf_memcpy( p_buffer,
420                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
421                        i_ret );
422
423     return i_ret;
424 }
425
426 /*****************************************************************************
427  * RTPChoose : read from the network, and decide whether it's UDP or RTP
428  *****************************************************************************/
429 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
430                           size_t i_len )
431 {
432     int         i_rtp_version;
433     int         i_CSRC_count;
434     int         i_payload_type;
435
436     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
437
438     /* Get the raw data from the socket.
439      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
440     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
441
442     if ( !i_ret ) return 0;
443     
444     /* Check that it's not TS. */
445     if ( p_tmp_buffer[0] == 0x47 )
446     {
447         msg_Dbg( p_input, "detected TS over raw UDP" );
448         p_input->pf_read = Read;
449         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
450         return i_ret;
451     }
452
453     /* Parse the header and make some verifications.
454      * See RFC 1889 & RFC 2250. */
455
456     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
457     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
458     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
459
460     if ( i_rtp_version != 2 )
461     {
462         msg_Dbg( p_input, "no RTP header detected" );
463         p_input->pf_read = Read;
464         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
465         return i_ret;
466     }
467
468     switch ( i_payload_type )
469     {
470     case 33:
471         msg_Dbg( p_input, "detected TS over RTP" );
472         break;
473
474     case 14:
475         msg_Dbg( p_input, "detected MPEG audio over RTP" );
476         break;
477
478     case 32:
479         msg_Dbg( p_input, "detected MPEG video over RTP" );
480         break;
481
482     default:
483         msg_Dbg( p_input, "no RTP header detected" );
484         p_input->pf_read = Read;
485         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
486         return i_ret;
487     }
488
489     /* Return the packet without the RTP header. */
490     p_input->pf_read = RTPRead;
491     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
492
493     if ( (size_t)i_ret > i_len )
494     {
495         /* This should NOT happen. */
496         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
497         i_ret = i_len;
498     }
499
500     p_input->p_vlc->pf_memcpy( p_buffer,
501                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
502                        i_ret );
503
504     return i_ret;
505 }