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