]> git.sesse.net Git - vlc/blob - modules/access/udp.c
glibc 2.1 port for linupy (linux on a yopy pda)
[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.12 2003/02/07 23:36:55 marcari 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             msg_Err( p_input, "recv failed (%s)", strerror(errno) );
355         }
356
357         return i_recv;
358     }
359
360     return 0;
361
362 #endif
363 }
364
365 /*****************************************************************************
366  * RTPRead : read from the network, and parse the RTP header
367  *****************************************************************************/
368 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
369                         size_t i_len )
370 {
371     int         i_rtp_version;
372     int         i_CSRC_count;
373     int         i_payload_type;
374
375     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
376
377     /* Get the raw data from the socket.
378      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
379     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
380
381     if ( !i_ret ) return 0;
382
383     /* Parse the header and make some verifications.
384      * See RFC 1889 & RFC 2250. */
385
386     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
387     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
388     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
389
390     if ( i_rtp_version != 2 )
391         msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
392
393     if ( i_payload_type != 33 && i_payload_type != 14
394           && i_payload_type != 32 )
395         msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
396
397     /* Return the packet without the RTP header. */
398     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
399
400     if ( (size_t)i_ret > i_len )
401     {
402         /* This should NOT happen. */
403         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
404         i_ret = i_len;
405     }
406
407     p_input->p_vlc->pf_memcpy( p_buffer,
408                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
409                        i_ret );
410
411     return i_ret;
412 }
413
414 /*****************************************************************************
415  * RTPChoose : read from the network, and decide whether it's UDP or RTP
416  *****************************************************************************/
417 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
418                           size_t i_len )
419 {
420     int         i_rtp_version;
421     int         i_CSRC_count;
422     int         i_payload_type;
423
424     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
425
426     /* Get the raw data from the socket.
427      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
428     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
429
430     if ( !i_ret ) return 0;
431     
432     /* Check that it's not TS. */
433     if ( p_tmp_buffer[0] == 0x47 )
434     {
435         msg_Dbg( p_input, "detected TS over raw UDP" );
436         p_input->pf_read = Read;
437         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
438         return i_ret;
439     }
440
441     /* Parse the header and make some verifications.
442      * See RFC 1889 & RFC 2250. */
443
444     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
445     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
446     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
447
448     if ( i_rtp_version != 2 )
449     {
450         msg_Dbg( p_input, "no RTP header detected" );
451         p_input->pf_read = Read;
452         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
453         return i_ret;
454     }
455
456     switch ( i_payload_type )
457     {
458     case 33:
459         msg_Dbg( p_input, "detected TS over RTP" );
460         break;
461
462     case 14:
463         msg_Dbg( p_input, "detected MPEG audio over RTP" );
464         break;
465
466     case 32:
467         msg_Dbg( p_input, "detected MPEG video over RTP" );
468         break;
469
470     default:
471         msg_Dbg( p_input, "no RTP header detected" );
472         p_input->pf_read = Read;
473         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
474         return i_ret;
475     }
476
477     /* Return the packet without the RTP header. */
478     p_input->pf_read = RTPRead;
479     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
480
481     if ( (size_t)i_ret > i_len )
482     {
483         /* This should NOT happen. */
484         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
485         i_ret = i_len;
486     }
487
488     p_input->p_vlc->pf_memcpy( p_buffer,
489                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
490                        i_ret );
491
492     return i_ret;
493 }