]> git.sesse.net Git - vlc/blob - modules/access/udp.c
* udp: converted to access2 (using pf_block, so for now it may hurt a
[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", 0, 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     vlc_value_t val;
107
108
109     /* First set ipv4/ipv6 */
110     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
112
113     if( *p_access->psz_access )
114     {
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         var_Create( p_access, "server-port", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
205         var_Get( p_access, "server-port", &val  );
206         i_bind_port = val.i_int;
207     }
208
209     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
210              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
211
212     /* Set up p_access */
213     p_access->pf_read = NULL;
214     p_access->pf_block = BlockChoose;
215     p_access->pf_control = Control;
216     p_access->pf_seek = NULL;
217     p_access->info.i_update = 0;
218     p_access->info.i_size = 0;
219     p_access->info.i_pos = 0;
220     p_access->info.b_eof = VLC_FALSE;
221     p_access->info.i_title = 0;
222     p_access->info.i_seekpoint = 0;
223
224     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
225     p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
226                                       psz_server_addr, i_server_port );
227     if( p_sys->fd < 0 )
228     {
229         msg_Err( p_access, "cannot open socket" );
230         free( psz_name );
231         free( p_sys );
232         return VLC_EGENERIC;
233     }
234     free( psz_name );
235
236     /* FIXME */
237     var_Create( p_access, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
238     var_Get( p_access, "mtu", &val);
239     p_sys->i_mtu = val.i_int > 0 ? val.i_int : 1500;    /* avoid problem */
240
241     var_Create( p_access, "udp-auto-mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
242     var_Get( p_access, "udp-auto-mtu", &val);
243     p_sys->b_auto_mtu = val.b_bool;
244
245     /* Update default_pts to a suitable value for udp access */
246     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
247
248     return VLC_SUCCESS;
249 }
250
251 /*****************************************************************************
252  * Close: free unused data structures
253  *****************************************************************************/
254 static void Close( vlc_object_t *p_this )
255 {
256     access_t     *p_access = (access_t*)p_this;
257     access_sys_t *p_sys = p_access->p_sys;
258
259     net_Close( p_sys->fd );
260     free( p_sys );
261 }
262
263 /*****************************************************************************
264  * Control:
265  *****************************************************************************/
266 static int Control( access_t *p_access, int i_query, va_list args )
267 {
268     access_sys_t *p_sys = p_access->p_sys;
269     vlc_bool_t   *pb_bool;
270     int          *pi_int;
271     int64_t      *pi_64;
272     vlc_value_t  val;
273
274     switch( i_query )
275     {
276         /* */
277         case ACCESS_CAN_SEEK:
278         case ACCESS_CAN_FASTSEEK:
279         case ACCESS_CAN_PAUSE:
280         case ACCESS_CAN_CONTROL_PACE:
281             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
282             *pb_bool = VLC_FALSE;
283             break;
284         /* */
285         case ACCESS_GET_MTU:
286             pi_int = (int*)va_arg( args, int * );
287             *pi_int = p_sys->i_mtu;
288             break;
289
290         case ACCESS_GET_PTS_DELAY:
291             pi_64 = (int64_t*)va_arg( args, int64_t * );
292             var_Get( p_access, "udp-caching", &val );
293             *pi_64 = val.i_int * 1000;
294             break;
295
296         /* */
297         case ACCESS_SET_PAUSE_STATE:
298         case ACCESS_GET_TITLE_INFO:
299         case ACCESS_SET_TITLE:
300         case ACCESS_SET_SEEKPOINT:
301             return VLC_EGENERIC;
302
303         default:
304             msg_Err( p_access, "unimplemented query in control" );
305             return VLC_EGENERIC;
306
307     }
308     return VLC_SUCCESS;
309 }
310
311 /*****************************************************************************
312  * BlockUDP:
313  *****************************************************************************/
314 static block_t *BlockUDP( access_t *p_access )
315 {
316     access_sys_t *p_sys = p_access->p_sys;
317     block_t      *p_block;
318
319     /* Read data */
320     p_block = block_New( p_access, p_sys->i_mtu );
321     p_block->i_buffer = net_Read( p_access, p_sys->fd, p_block->p_buffer, p_sys->i_mtu, 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     {
330         /* Increase by 10% */
331         p_sys->i_mtu += ( p_sys->i_mtu + 9 ) / 10;
332         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
333     }
334
335     return p_block;
336 }
337
338 /*****************************************************************************
339  * BlockParseRTP/BlockRTP:
340  *****************************************************************************/
341 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
342 {
343     int     i_rtp_version;
344     int     i_CSRC_count;
345     int     i_payload_type;
346     int     i_skip = 0;
347
348     if( p_block->i_buffer < RTP_HEADER_LEN )
349         goto trash;
350
351     /* Parse the header and make some verifications.
352      * See RFC 1889 & RFC 2250. */
353     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
354     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
355     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
356
357     if ( i_rtp_version != 2 )
358         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
359
360     if( i_payload_type == 14 )
361         i_skip = 4;
362     else if( i_payload_type !=  33 && i_payload_type != 32 )
363         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
364
365     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
366
367     /* A CSRC extension field is 32 bits in size (4 bytes) */
368     if( i_skip >= p_block->i_buffer )
369         goto trash;
370
371     /* Return the packet without the RTP header. */
372     p_block->i_buffer -= i_skip;
373     p_block->p_buffer += i_skip;
374
375     return p_block;
376
377 trash:
378     msg_Warn( p_access, "received a too short packet for RTP" );
379     block_Release( p_block );
380     return NULL;
381 }
382
383 static block_t *BlockRTP( access_t *p_access )
384 {
385     return BlockParseRTP( p_access, BlockUDP( p_access ) );
386 }
387
388 /*****************************************************************************
389  * BlockChoose: decide between RTP and UDP
390  *****************************************************************************/
391 static block_t *BlockChoose( access_t *p_access )
392 {
393     block_t *p_block;
394     int     i_rtp_version;
395     int     i_CSRC_count;
396     int     i_payload_type;
397
398     if( ( p_block = BlockUDP( p_access ) ) == NULL )
399         return NULL;
400
401     if( p_block->p_buffer[0] == 0x47 )
402     {
403         msg_Dbg( p_access, "detected TS over raw UDP" );
404         p_access->pf_block = BlockUDP;
405         return p_block;
406     }
407
408     if( p_block->i_buffer < RTP_HEADER_LEN )
409         return p_block;
410
411     /* Parse the header and make some verifications.
412      * See RFC 1889 & RFC 2250. */
413
414     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
415     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
416     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
417
418     if( i_rtp_version != 2 )
419     {
420         msg_Dbg( p_access, "no supported RTP header detected" );
421         p_access->pf_block = BlockUDP;
422         return p_block;
423     }
424
425     switch( i_payload_type )
426     {
427         case 33:
428             msg_Dbg( p_access, "detected TS over RTP" );
429             p_access->psz_demux = strdup( "ts2" );
430             break;
431
432         case 14:
433             msg_Dbg( p_access, "detected MPEG audio over RTP" );
434             p_access->psz_demux = strdup( "mp3" );
435             break;
436
437         case 32:
438             msg_Dbg( p_access, "detected MPEG video over RTP" );
439             p_access->psz_demux = strdup( "es" );
440             break;
441
442         default:
443             msg_Dbg( p_access, "no RTP header detected" );
444             p_access->pf_block = BlockUDP;
445             return p_block;
446     }
447
448     p_access->pf_block = BlockRTP;
449
450     return BlockParseRTP( p_access, p_block );
451 }