]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Added RTP shortcuts to udp.c for backwards compatibility.
[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.9 2002/12/30 11:49:32 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.p_selected_area->i_tell = 0;
237     p_input->stream.i_method = INPUT_METHOD_NETWORK;
238     vlc_mutex_unlock( &p_input->stream.stream_lock );
239
240     if( *psz_server_addr || i_server_port )
241     {
242         msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
243         msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
244                           psz_server_addr, i_server_port);
245         msg_Err( p_input, "or local port, type : %s:@%s:%d",
246                           *p_input->psz_access ? p_input->psz_access : "udp",
247                           psz_server_addr, i_server_port );
248
249         i_server_port = 0;
250         psz_server_addr = "";
251     }
252  
253     msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
254              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
255
256     /* Prepare the network_socket_t structure */
257     socket_desc.i_type = NETWORK_UDP;
258     socket_desc.psz_bind_addr = psz_bind_addr;
259     socket_desc.i_bind_port = i_bind_port;
260     socket_desc.psz_server_addr = psz_server_addr;
261     socket_desc.i_server_port = i_server_port;
262
263     /* Find an appropriate network module */
264     p_input->p_private = (void*) &socket_desc;
265     p_network = module_Need( p_input, "network", psz_network );
266     free(psz_name);
267     if( p_network == NULL )
268     {
269         return( -1 );
270     }
271     module_Unneed( p_input, p_network );
272     
273     p_access_data = malloc( sizeof(input_socket_t) );
274     p_input->p_access_data = (access_sys_t *)p_access_data;
275
276     if( p_access_data == NULL )
277     {
278         msg_Err( p_input, "out of memory" );
279         return( -1 );
280     }
281
282     p_access_data->i_handle = socket_desc.i_handle;
283     p_input->i_mtu = socket_desc.i_mtu;
284
285     /* Update default_pts to a suitable value for udp access */
286     p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
287
288     return( 0 );
289 }
290
291 /*****************************************************************************
292  * Close: free unused data structures
293  *****************************************************************************/
294 static void Close( vlc_object_t *p_this )
295 {
296     input_thread_t *  p_input = (input_thread_t *)p_this;
297     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
298
299     msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
300
301 #ifdef UNDER_CE
302     CloseHandle( (HANDLE)p_access_data->i_handle );
303 #elif defined( WIN32 )
304     closesocket( p_access_data->i_handle );
305 #else
306     close( p_access_data->i_handle );
307 #endif
308
309     free( p_access_data );
310 }
311
312 /*****************************************************************************
313  * Read: read on a file descriptor, checking b_die periodically
314  *****************************************************************************/
315 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
316 {
317 #ifdef UNDER_CE
318     return -1;
319
320 #else
321     input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
322     struct timeval  timeout;
323     fd_set          fds;
324     int             i_ret;
325
326     /* Initialize file descriptor set */
327     FD_ZERO( &fds );
328     FD_SET( p_access_data->i_handle, &fds );
329
330     /* We'll wait 0.5 second if nothing happens */
331     timeout.tv_sec = 0;
332     timeout.tv_usec = 500000;
333
334     /* Find if some data is available */
335     i_ret = select( p_access_data->i_handle + 1, &fds,
336                     NULL, NULL, &timeout );
337
338     if( i_ret == -1 && errno != EINTR )
339     {
340         msg_Err( p_input, "network select error (%s)", strerror(errno) );
341     }
342     else if( i_ret > 0 )
343     {
344         ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
345
346         if( i_recv < 0 )
347         {
348             msg_Err( p_input, "recv failed (%s)", strerror(errno) );
349         }
350
351         return i_recv;
352     }
353
354     return 0;
355
356 #endif
357 }
358
359 /*****************************************************************************
360  * RTPRead : read from the network, and parse the RTP header
361  *****************************************************************************/
362 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
363                         size_t i_len )
364 {
365     int         i_rtp_version;
366     int         i_CSRC_count;
367     int         i_payload_type;
368
369     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
370
371     /* Get the raw data from the socket.
372      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
373     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
374
375     if ( !i_ret ) return 0;
376
377     /* Parse the header and make some verifications.
378      * See RFC 1889 & RFC 2250. */
379
380     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
381     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
382     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
383
384     if ( i_rtp_version != 2 )
385         msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
386
387     if ( i_payload_type != 33 && i_payload_type != 14
388           && i_payload_type != 32 )
389         msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
390
391     /* Return the packet without the RTP header. */
392     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
393
394     if ( (size_t)i_ret > i_len )
395     {
396         /* This should NOT happen. */
397         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
398         i_ret = i_len;
399     }
400
401     p_input->p_vlc->pf_memcpy( p_buffer,
402                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
403                        i_ret );
404
405     return i_ret;
406 }
407
408 /*****************************************************************************
409  * RTPChoose : read from the network, and decide whether it's UDP or RTP
410  *****************************************************************************/
411 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
412                           size_t i_len )
413 {
414     int         i_rtp_version;
415     int         i_CSRC_count;
416     int         i_payload_type;
417
418     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
419
420     /* Get the raw data from the socket.
421      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
422     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
423
424     if ( !i_ret ) return 0;
425     
426     /* Check that it's not TS. */
427     if ( p_tmp_buffer[0] == 0x47 )
428     {
429         msg_Dbg( p_input, "detected TS over raw UDP" );
430         p_input->pf_read = Read;
431         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
432         return i_ret;
433     }
434
435     /* Parse the header and make some verifications.
436      * See RFC 1889 & RFC 2250. */
437
438     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
439     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
440     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
441
442     if ( i_rtp_version != 2 )
443     {
444         msg_Dbg( p_input, "no RTP header detected" );
445         p_input->pf_read = Read;
446         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
447         return i_ret;
448     }
449
450     switch ( i_payload_type )
451     {
452     case 33:
453         msg_Dbg( p_input, "detected TS over RTP" );
454         break;
455
456     case 14:
457         msg_Dbg( p_input, "detected MPEG audio over RTP" );
458         break;
459
460     case 32:
461         msg_Dbg( p_input, "detected MPEG video over RTP" );
462         break;
463
464     default:
465         msg_Dbg( p_input, "no RTP header detected" );
466         p_input->pf_read = Read;
467         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
468         return i_ret;
469     }
470
471     /* Return the packet without the RTP header. */
472     p_input->pf_read = RTPRead;
473     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
474
475     if ( (size_t)i_ret > i_len )
476     {
477         /* This should NOT happen. */
478         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
479         i_ret = i_len;
480     }
481
482     p_input->p_vlc->pf_memcpy( p_buffer,
483                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
484                        i_ret );
485
486     return i_ret;
487 }