]> git.sesse.net Git - vlc/blob - modules/access/udp.c
* access/*: use var_* helpers.
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP & RTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
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 millisecond units." )
45
46 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
47 #define AUTO_MTU_LONGTEXT N_( \
48     "Allows growing the MTU if truncated packets are found" )
49
50 static int  Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
52
53 vlc_module_begin();
54     set_description( _("UDP/RTP input") );
55
56     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
57                  CACHING_LONGTEXT, VLC_TRUE );
58     add_bool( "udp-auto-mtu", 1, NULL,
59               AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
60
61     set_capability( "access2", 0 );
62     add_shortcut( "udp" );
63     add_shortcut( "udpstream" );
64     add_shortcut( "udp4" );
65     add_shortcut( "udp6" );
66     add_shortcut( "rtp" );
67     add_shortcut( "rtp4" );
68     add_shortcut( "rtp6" );
69     set_callbacks( Open, Close );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 #define RTP_HEADER_LEN 12
76
77 static block_t *BlockUDP( access_t * );
78 static block_t *BlockRTP( access_t * );
79 static block_t *BlockChoose( access_t * );
80 static int Control( access_t *, int, va_list );
81
82 struct access_sys_t
83 {
84     int fd;
85
86     int i_mtu;
87     vlc_bool_t b_auto_mtu;
88 };
89
90 /*****************************************************************************
91  * Open: open the socket
92  *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
94 {
95     access_t     *p_access = (access_t*)p_this;
96     access_sys_t *p_sys;
97
98     char *psz_name = strdup( p_access->psz_path );
99     char *psz_parser = psz_name;
100     char *psz_server_addr = "";
101     char *psz_server_port = "";
102     char *psz_bind_addr = "";
103     char *psz_bind_port = "";
104     int  i_bind_port = 0;
105     int  i_server_port = 0;
106
107
108     /* First set ipv4/ipv6 */
109     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
110     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111
112     if( *p_access->psz_access )
113     {
114         vlc_value_t val;
115         /* Find out which shortcut was used */
116         if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
117             !strncmp( p_access->psz_access, "rtp4", 6 ))
118         {
119             val.b_bool = VLC_TRUE;
120             var_Set( p_access, "ipv4", val );
121
122             val.b_bool = VLC_FALSE;
123             var_Set( p_access, "ipv6", val );
124         }
125         else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
126                  !strncmp( p_access->psz_access, "rtp6", 6 ) )
127         {
128             val.b_bool = VLC_TRUE;
129             var_Set( p_access, "ipv6", val );
130
131             val.b_bool = VLC_FALSE;
132             var_Set( p_access, "ipv4", val );
133         }
134     }
135
136     /* Parse psz_name syntax :
137      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
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_server_port = psz_parser;
161
162             while( *psz_parser && *psz_parser != '@' )
163             {
164                 psz_parser++;
165             }
166         }
167     }
168
169     if( *psz_parser == '@' )
170     {
171         /* Found bind address or bind port */
172         *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
173
174         if( *psz_parser && *psz_parser != ':' )
175         {
176             /* Found bind address */
177             psz_bind_addr = psz_parser;
178
179             while( *psz_parser && *psz_parser != ':' )
180             {
181                 if( *psz_parser == '[' )
182                 {
183                     /* IPv6 address */
184                     while( *psz_parser && *psz_parser != ']' )
185                     {
186                         psz_parser++;
187                     }
188                 }
189                 psz_parser++;
190             }
191         }
192
193         if( *psz_parser == ':' )
194         {
195             /* Found bind port */
196             *psz_parser++ = '\0'; /* Terminate bind address if necessary */
197             psz_bind_port = psz_parser;
198         }
199     }
200
201     i_server_port = strtol( psz_server_port, NULL, 10 );
202     if( ( i_bind_port   = strtol( psz_bind_port,   NULL, 10 ) ) == 0 )
203     {
204         i_bind_port = var_CreateGetInteger( p_access, "server-port" );
205     }
206
207     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
208              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
209
210     /* Set up p_access */
211     p_access->pf_read = NULL;
212     p_access->pf_block = BlockChoose;
213     p_access->pf_control = Control;
214     p_access->pf_seek = NULL;
215     p_access->info.i_update = 0;
216     p_access->info.i_size = 0;
217     p_access->info.i_pos = 0;
218     p_access->info.b_eof = VLC_FALSE;
219     p_access->info.i_title = 0;
220     p_access->info.i_seekpoint = 0;
221
222     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
223     p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
224                                       psz_server_addr, i_server_port );
225     if( p_sys->fd < 0 )
226     {
227         msg_Err( p_access, "cannot open socket" );
228         free( psz_name );
229         free( p_sys );
230         return VLC_EGENERIC;
231     }
232     free( psz_name );
233
234     /* FIXME */
235     p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
236     if( p_sys->i_mtu <= 1 )
237         p_sys->i_mtu  = 1500;   /* Avoid problem */
238
239     p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
240
241     /* Update default_pts to a suitable value for udp access */
242     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
243
244     return VLC_SUCCESS;
245 }
246
247 /*****************************************************************************
248  * Close: free unused data structures
249  *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
251 {
252     access_t     *p_access = (access_t*)p_this;
253     access_sys_t *p_sys = p_access->p_sys;
254
255     net_Close( p_sys->fd );
256     free( p_sys );
257 }
258
259 /*****************************************************************************
260  * Control:
261  *****************************************************************************/
262 static int Control( access_t *p_access, int i_query, va_list args )
263 {
264     access_sys_t *p_sys = p_access->p_sys;
265     vlc_bool_t   *pb_bool;
266     int          *pi_int;
267     int64_t      *pi_64;
268     vlc_value_t  val;
269
270     switch( i_query )
271     {
272         /* */
273         case ACCESS_CAN_SEEK:
274         case ACCESS_CAN_FASTSEEK:
275         case ACCESS_CAN_PAUSE:
276         case ACCESS_CAN_CONTROL_PACE:
277             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
278             *pb_bool = VLC_FALSE;
279             break;
280         /* */
281         case ACCESS_GET_MTU:
282             pi_int = (int*)va_arg( args, int * );
283             *pi_int = p_sys->i_mtu;
284             break;
285
286         case ACCESS_GET_PTS_DELAY:
287             pi_64 = (int64_t*)va_arg( args, int64_t * );
288             var_Get( p_access, "udp-caching", &val );
289             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
290             break;
291
292         /* */
293         case ACCESS_SET_PAUSE_STATE:
294         case ACCESS_GET_TITLE_INFO:
295         case ACCESS_SET_TITLE:
296         case ACCESS_SET_SEEKPOINT:
297             return VLC_EGENERIC;
298
299         default:
300             msg_Err( p_access, "unimplemented query in control" );
301             return VLC_EGENERIC;
302
303     }
304     return VLC_SUCCESS;
305 }
306
307 /*****************************************************************************
308  * BlockUDP:
309  *****************************************************************************/
310 static block_t *BlockUDP( access_t *p_access )
311 {
312     access_sys_t *p_sys = p_access->p_sys;
313     block_t      *p_block;
314
315     /* Read data */
316     p_block = block_New( p_access, p_sys->i_mtu );
317     p_block->i_buffer = net_Read( p_access, p_sys->fd, p_block->p_buffer, p_sys->i_mtu, VLC_FALSE );
318     if( p_block->i_buffer <= 0 )
319     {
320         block_Release( p_block );
321         return NULL;
322     }
323
324     if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu )
325     {
326         /* Increase by 10% */
327         p_sys->i_mtu += ( p_sys->i_mtu + 9 ) / 10;
328         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
329     }
330
331     return p_block;
332 }
333
334 /*****************************************************************************
335  * BlockParseRTP/BlockRTP:
336  *****************************************************************************/
337 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
338 {
339     int     i_rtp_version;
340     int     i_CSRC_count;
341     int     i_payload_type;
342     int     i_skip = 0;
343
344     if( p_block->i_buffer < RTP_HEADER_LEN )
345         goto trash;
346
347     /* Parse the header and make some verifications.
348      * See RFC 1889 & RFC 2250. */
349     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
350     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
351     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
352
353     if ( i_rtp_version != 2 )
354         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
355
356     if( i_payload_type == 14 )
357         i_skip = 4;
358     else if( i_payload_type !=  33 && i_payload_type != 32 )
359         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
360
361     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
362
363     /* A CSRC extension field is 32 bits in size (4 bytes) */
364     if( i_skip >= p_block->i_buffer )
365         goto trash;
366
367     /* Return the packet without the RTP header. */
368     p_block->i_buffer -= i_skip;
369     p_block->p_buffer += i_skip;
370
371     return p_block;
372
373 trash:
374     msg_Warn( p_access, "received a too short packet for RTP" );
375     block_Release( p_block );
376     return NULL;
377 }
378
379 static block_t *BlockRTP( access_t *p_access )
380 {
381     return BlockParseRTP( p_access, BlockUDP( p_access ) );
382 }
383
384 /*****************************************************************************
385  * BlockChoose: decide between RTP and UDP
386  *****************************************************************************/
387 static block_t *BlockChoose( access_t *p_access )
388 {
389     block_t *p_block;
390     int     i_rtp_version;
391     int     i_CSRC_count;
392     int     i_payload_type;
393
394     if( ( p_block = BlockUDP( p_access ) ) == NULL )
395         return NULL;
396
397     if( p_block->p_buffer[0] == 0x47 )
398     {
399         msg_Dbg( p_access, "detected TS over raw UDP" );
400         p_access->pf_block = BlockUDP;
401         return p_block;
402     }
403
404     if( p_block->i_buffer < RTP_HEADER_LEN )
405         return p_block;
406
407     /* Parse the header and make some verifications.
408      * See RFC 1889 & RFC 2250. */
409
410     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
411     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
412     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
413
414     if( i_rtp_version != 2 )
415     {
416         msg_Dbg( p_access, "no supported RTP header detected" );
417         p_access->pf_block = BlockUDP;
418         return p_block;
419     }
420
421     switch( i_payload_type )
422     {
423         case 33:
424             msg_Dbg( p_access, "detected TS over RTP" );
425             p_access->psz_demux = strdup( "ts2" );
426             break;
427
428         case 14:
429             msg_Dbg( p_access, "detected MPEG audio over RTP" );
430             p_access->psz_demux = strdup( "mp3" );
431             break;
432
433         case 32:
434             msg_Dbg( p_access, "detected MPEG video over RTP" );
435             p_access->psz_demux = strdup( "es" );
436             break;
437
438         default:
439             msg_Dbg( p_access, "no RTP header detected" );
440             p_access->pf_block = BlockUDP;
441             return p_block;
442     }
443
444     p_access->pf_block = BlockRTP;
445
446     return BlockParseRTP( p_access, p_block );
447 }