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