]> git.sesse.net Git - vlc/blob - modules/access/udp.c
* modules/access/*: strings review + coding style fixes.
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP & RTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id: udp.c,v 1.28 2004/01/25 17:31:22 gbazin Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Tristan Leteurtre <tooney@via.ecp.fr>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <stdlib.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc/input.h>
35
36 #include "network.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43     "Allows you to modify the default caching value for udp streams. This " \
44     "value should be set in miliseconds units." )
45
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 vlc_module_begin();
50     set_description( _("UDP/RTP input") );
51
52     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
53                  CACHING_LONGTEXT, VLC_TRUE );
54
55     set_capability( "access", 0 );
56     add_shortcut( "udp" );
57     add_shortcut( "udpstream" );
58     add_shortcut( "udp4" );
59     add_shortcut( "udp6" );
60     add_shortcut( "rtp" );
61     add_shortcut( "rtp4" );
62     add_shortcut( "rtp6" );
63     set_callbacks( Open, Close );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Local prototypes
68  *****************************************************************************/
69 #define RTP_HEADER_LEN 12
70
71 static ssize_t Read    ( input_thread_t *, byte_t *, size_t );
72 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
73 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
74
75 struct access_sys_t
76 {
77     int fd;
78 };
79
80 /*****************************************************************************
81  * Open: open the socket
82  *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
84 {
85     input_thread_t     *p_input = (input_thread_t *)p_this;
86     access_sys_t       *p_sys;
87
88     char *              psz_name = strdup(p_input->psz_name);
89     char *              psz_parser = psz_name;
90     char *              psz_server_addr = "";
91     char *              psz_server_port = "";
92     char *              psz_bind_addr = "";
93     char *              psz_bind_port = "";
94     int                 i_bind_port = 0, i_server_port = 0;
95     vlc_value_t         val;
96
97
98     /* First set ipv4/ipv6 */
99     var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
100     var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
101
102     if( *p_input->psz_access )
103     {
104         /* Find out which shortcut was used */
105         if( !strncmp( p_input->psz_access, "udp4", 6 ) ||
106             !strncmp( p_input->psz_access, "rtp4", 6 ))
107         {
108             val.b_bool = VLC_TRUE;
109             var_Set( p_input, "ipv4", val );
110
111             val.b_bool = VLC_FALSE;
112             var_Set( p_input, "ipv6", val );
113         }
114         else if( !strncmp( p_input->psz_access, "udp6", 6 ) ||
115                  !strncmp( p_input->psz_access, "rtp6", 6 ) )
116         {
117             val.b_bool = VLC_TRUE;
118             var_Set( p_input, "ipv6", val );
119
120             val.b_bool = VLC_FALSE;
121             var_Set( p_input, "ipv4", val );
122         }
123     }
124
125     /* Parse psz_name syntax :
126      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
127     if( *psz_parser && *psz_parser != '@' )
128     {
129         /* Found server */
130         psz_server_addr = psz_parser;
131
132         while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
133         {
134             if( *psz_parser == '[' )
135             {
136                 /* IPv6 address */
137                 while( *psz_parser && *psz_parser != ']' )
138                 {
139                     psz_parser++;
140                 }
141             }
142             psz_parser++;
143         }
144
145         if( *psz_parser == ':' )
146         {
147             /* Found server port */
148             *psz_parser++ = '\0'; /* Terminate server name */
149             psz_server_port = psz_parser;
150
151             while( *psz_parser && *psz_parser != '@' )
152             {
153                 psz_parser++;
154             }
155         }
156     }
157
158     if( *psz_parser == '@' )
159     {
160         /* Found bind address or bind port */
161         *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
162
163         if( *psz_parser && *psz_parser != ':' )
164         {
165             /* Found bind address */
166             psz_bind_addr = psz_parser;
167
168             while( *psz_parser && *psz_parser != ':' )
169             {
170                 if( *psz_parser == '[' )
171                 {
172                     /* IPv6 address */
173                     while( *psz_parser && *psz_parser != ']' )
174                     {
175                         psz_parser++;
176                     }
177                 }
178                 psz_parser++;
179             }
180         }
181
182         if( *psz_parser == ':' )
183         {
184             /* Found bind port */
185             *psz_parser++ = '\0'; /* Terminate bind address if necessary */
186             psz_bind_port = psz_parser;
187         }
188     }
189
190     i_server_port = strtol( psz_server_port, NULL, 10 );
191     if( ( i_bind_port   = strtol( psz_bind_port,   NULL, 10 ) ) == 0 )
192     {
193         i_bind_port = config_GetInt( p_this, "server-port" );
194     }
195
196     if( *psz_server_addr || i_server_port )
197     {
198         msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
199         msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
200                           psz_server_addr, i_server_port);
201         msg_Err( p_input, "or local port, type : %s:@%s:%d",
202                           *p_input->psz_access ? p_input->psz_access : "udp",
203                           psz_server_addr, i_server_port );
204
205         i_server_port = 0;
206         psz_server_addr = "";
207     }
208
209     msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
210              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
211
212     p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
213     if( p_sys == NULL )
214     {
215         msg_Err( p_input, "out of memory" );
216         return VLC_EGENERIC;
217     }
218
219     p_sys->fd = net_OpenUDP( p_input, psz_bind_addr, i_bind_port,
220                                       psz_server_addr, i_server_port );
221     if( p_sys->fd < 0 )
222     {
223         msg_Err( p_input, "cannot open socket" );
224         free( psz_name );
225         free( p_sys );
226         return VLC_EGENERIC;
227     }
228     free( psz_name );
229
230     /* FIXME */
231     p_input->i_mtu = config_GetInt( p_this, "mtu" );
232
233     /* fill p_input fields */
234     p_input->pf_read = RTPChoose;
235     p_input->pf_set_program = input_SetProgram;
236     p_input->pf_set_area = NULL;
237     p_input->pf_seek = NULL;
238
239     vlc_mutex_lock( &p_input->stream.stream_lock );
240     p_input->stream.b_pace_control = VLC_FALSE;
241     p_input->stream.b_seekable = VLC_FALSE;
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     /* Update default_pts to a suitable value for udp access */
247     var_Create( p_input, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
248     var_Get( p_input, "udp-caching", &val );
249     p_input->i_pts_delay = val.i_int * 1000;
250
251     return VLC_SUCCESS;
252 }
253
254 /*****************************************************************************
255  * Close: free unused data structures
256  *****************************************************************************/
257 static void Close( vlc_object_t *p_this )
258 {
259     input_thread_t *p_input = (input_thread_t *)p_this;
260     access_sys_t   *p_sys = p_input->p_access_data;
261
262     msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
263
264     net_Close( p_sys->fd );
265
266     free( p_sys );
267 }
268
269 /*****************************************************************************
270  * Read: read on a file descriptor, checking b_die periodically
271  *****************************************************************************/
272 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
273 {
274     access_sys_t   *p_sys = p_input->p_access_data;
275
276     return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
277 }
278
279 /*****************************************************************************
280  * RTPRead : read from the network, and parse the RTP header
281  *****************************************************************************/
282 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
283                         size_t i_len )
284 {
285     int         i_rtp_version;
286     int         i_CSRC_count;
287     int         i_payload_type;
288     int         i_skip = 0;
289
290     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
291
292     /* Get the raw data from the socket.
293      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
294     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
295
296     if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
297
298     /* Parse the header and make some verifications.
299      * See RFC 1889 & RFC 2250. */
300
301     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
302     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
303     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
304
305     if ( i_rtp_version != 2 )
306         msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
307
308     if( i_payload_type == 14 )
309     {
310         i_skip = 4;
311     }
312     else if( i_payload_type !=  33 && i_payload_type != 32 )
313     {
314         msg_Dbg( p_input, "unsupported RTP payload type (%u)",
315                  i_payload_type );
316     }
317     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
318
319     /* A CSRC extension field is 32 bits in size (4 bytes) */
320     if ( i_ret < i_skip )
321     {
322         /* Packet is not big enough to hold the complete RTP_HEADER with
323          * CSRC extensions.
324          */
325         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
326         return 0;
327     }
328
329     /* Return the packet without the RTP header. */
330     i_ret -= i_skip;
331
332     if ( (size_t)i_ret > i_len )
333     {
334         /* This should NOT happen. */
335         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
336         i_ret = i_len;
337     }
338
339     p_input->p_vlc->pf_memcpy( p_buffer, &p_tmp_buffer[i_skip], i_ret );
340
341     return i_ret;
342 }
343
344 /*****************************************************************************
345  * RTPChoose : read from the network, and decide whether it's UDP or RTP
346  *****************************************************************************/
347 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
348                           size_t i_len )
349 {
350     int         i_rtp_version;
351     int         i_CSRC_count;
352     int         i_payload_type;
353
354     byte_t *    p_tmp_buffer = alloca( p_input->i_mtu );
355
356     /* Get the raw data from the socket.
357      * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
358     ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
359
360     if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
361
362     /* Check that it's not TS. */
363     if ( p_tmp_buffer[0] == 0x47 )
364     {
365         msg_Dbg( p_input, "detected TS over raw UDP" );
366         p_input->pf_read = Read;
367         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
368         return i_ret;
369     }
370
371     /* Parse the header and make some verifications.
372      * See RFC 1889 & RFC 2250. */
373
374     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
375     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
376     i_payload_type = ( p_tmp_buffer[1] & 0x7F );
377
378     if ( i_rtp_version != 2 )
379     {
380         msg_Dbg( p_input, "no supported RTP header detected" );
381         p_input->pf_read = Read;
382         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
383         return i_ret;
384     }
385
386     switch ( i_payload_type )
387     {
388     case 33:
389         msg_Dbg( p_input, "detected TS over RTP" );
390         break;
391
392     case 14:
393         msg_Dbg( p_input, "detected MPEG audio over RTP" );
394         if( !p_input->psz_demux || *p_input->psz_demux == '\0' )
395         {
396             p_input->psz_demux = "mp3";
397         }
398         break;
399
400     case 32:
401         msg_Dbg( p_input, "detected MPEG video over RTP" );
402         break;
403
404     default:
405         msg_Dbg( p_input, "no RTP header detected" );
406         p_input->pf_read = Read;
407         p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
408         return i_ret;
409     }
410
411     p_input->pf_read = RTPRead;
412
413     /* A CSRC extension field is 32 bits in size (4 bytes) */
414     if( i_ret < RTP_HEADER_LEN + 4*i_CSRC_count )
415     {
416         /* Packet is not big enough to hold the complete RTP_HEADER with
417          * CSRC extensions.
418          */
419         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
420         return 0;
421     }
422
423     /* Return the packet without the RTP header. */
424     i_ret -= RTP_HEADER_LEN + 4*i_CSRC_count;
425
426     if ( (size_t)i_ret > i_len )
427     {
428         /* This should NOT happen. */
429         msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
430         i_ret = i_len;
431     }
432
433     p_input->p_vlc->pf_memcpy( p_buffer,
434                                &p_tmp_buffer[RTP_HEADER_LEN + 4*i_CSRC_count],
435                                i_ret );
436
437     return i_ret;
438 }