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