]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Remove trailing whitespace
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP & RTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
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  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
11  *
12  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
27  *****************************************************************************/
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <stdlib.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/input.h>
36
37 #include "network.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44     "Allows you to modify the default caching value for UDP streams. This " \
45     "value should be set in millisecond units." )
46
47 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
48 #define AUTO_MTU_LONGTEXT N_( \
49     "Allows growing the MTU if truncated packets are found" )
50
51 static int  Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
53
54 vlc_module_begin();
55     set_shortname( _("UDP/RTP" ) );
56     set_description( _("UDP/RTP input") );
57     set_category( CAT_INPUT );
58     set_subcategory( SUBCAT_INPUT_ACCESS );
59
60     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
61                  CACHING_LONGTEXT, VLC_TRUE );
62     add_bool( "udp-auto-mtu", 1, NULL,
63               AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
64
65     set_capability( "access2", 0 );
66     add_shortcut( "udp" );
67     add_shortcut( "udpstream" );
68     add_shortcut( "udp4" );
69     add_shortcut( "udp6" );
70     add_shortcut( "rtp" );
71     add_shortcut( "rtp4" );
72     add_shortcut( "rtp6" );
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 #define RTP_HEADER_LEN 12
80
81 static block_t *BlockUDP( access_t * );
82 static block_t *BlockRTP( access_t * );
83 static block_t *BlockChoose( access_t * );
84 static int Control( access_t *, int, va_list );
85
86 struct access_sys_t
87 {
88     int fd;
89
90     int i_mtu;
91     vlc_bool_t b_auto_mtu;
92
93     /* rtp only */
94     int i_sequence_number;
95 };
96
97 /*****************************************************************************
98  * Open: open the socket
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     access_t     *p_access = (access_t*)p_this;
103     access_sys_t *p_sys;
104
105     char *psz_name = strdup( p_access->psz_path );
106     char *psz_parser, *psz_server_addr, *psz_bind_addr = "";
107     int  i_bind_port, i_server_port = 0;
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         vlc_value_t val;
116         /* Find out which shortcut was used */
117         if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
118             !strncmp( p_access->psz_access, "rtp4", 6 ))
119         {
120             val.b_bool = VLC_TRUE;
121             var_Set( p_access, "ipv4", val );
122
123             val.b_bool = VLC_FALSE;
124             var_Set( p_access, "ipv6", val );
125         }
126         else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
127                  !strncmp( p_access->psz_access, "rtp6", 6 ) )
128         {
129             val.b_bool = VLC_TRUE;
130             var_Set( p_access, "ipv6", val );
131
132             val.b_bool = VLC_FALSE;
133             var_Set( p_access, "ipv4", val );
134         }
135     }
136
137     i_bind_port = var_CreateGetInteger( p_access, "server-port" );
138
139     /* Parse psz_name syntax :
140      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
141     psz_parser = strchr( psz_name, '@' );
142     if( psz_parser != NULL )
143     {
144         /* Found bind address and/or bind port */
145         *psz_parser++ = '\0';
146         psz_bind_addr = psz_parser;
147
148         if( *psz_parser == '[' )
149             /* skips bracket'd IPv6 address */
150             psz_parser = strchr( psz_parser, ']' );
151
152         if( psz_parser != NULL )
153         {
154             psz_parser = strchr( psz_parser, ':' );
155             if( psz_parser != NULL )
156             {
157                 *psz_parser++ = '\0';
158                 i_bind_port = atoi( psz_parser );
159             }
160         }
161     }
162
163     psz_server_addr = psz_name;
164     if( *psz_server_addr == '[' )
165         /* skips bracket'd IPv6 address */
166         psz_parser = strchr( psz_name, ']' );
167
168     if( psz_parser != NULL )
169     {
170         psz_parser = strchr( psz_parser, ':' );
171         if( psz_parser != NULL )
172         {
173             *psz_parser++ = '\0';
174             i_server_port = atoi( psz_parser );
175         }
176     }
177
178     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
179              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
180
181     /* Set up p_access */
182     p_access->pf_read = NULL;
183     if( !strcasecmp( p_access->psz_access, "rtp" )
184           || !strcasecmp( p_access->psz_access, "rtp4" )
185           || !strcasecmp( p_access->psz_access, "rtp6" ) )
186     {
187         p_access->pf_block = BlockRTP;
188     }
189     else
190     {
191         p_access->pf_block = BlockChoose;
192     }
193     p_access->pf_control = Control;
194     p_access->pf_seek = NULL;
195     p_access->info.i_update = 0;
196     p_access->info.i_size = 0;
197     p_access->info.i_pos = 0;
198     p_access->info.b_eof = VLC_FALSE;
199     p_access->info.i_title = 0;
200     p_access->info.i_seekpoint = 0;
201
202     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
203     p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
204                                       psz_server_addr, i_server_port );
205     if( p_sys->fd < 0 )
206     {
207         msg_Err( p_access, "cannot open socket" );
208         free( psz_name );
209         free( p_sys );
210         return VLC_EGENERIC;
211     }
212     free( psz_name );
213
214     net_StopSend( p_sys->fd );
215
216     /* FIXME */
217     p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
218     if( p_sys->i_mtu <= 1 )
219         p_sys->i_mtu  = 1500;   /* Avoid problem */
220
221     p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
222
223     /* Update default_pts to a suitable value for udp access */
224     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
225
226     /* Keep track of RTP sequence number */
227     p_sys->i_sequence_number = -1;
228
229     return VLC_SUCCESS;
230 }
231
232 /*****************************************************************************
233  * Close: free unused data structures
234  *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
236 {
237     access_t     *p_access = (access_t*)p_this;
238     access_sys_t *p_sys = p_access->p_sys;
239
240     net_Close( p_sys->fd );
241     free( p_sys );
242 }
243
244 /*****************************************************************************
245  * Control:
246  *****************************************************************************/
247 static int Control( access_t *p_access, int i_query, va_list args )
248 {
249     access_sys_t *p_sys = p_access->p_sys;
250     vlc_bool_t   *pb_bool;
251     int          *pi_int;
252     int64_t      *pi_64;
253
254     switch( i_query )
255     {
256         /* */
257         case ACCESS_CAN_SEEK:
258         case ACCESS_CAN_FASTSEEK:
259         case ACCESS_CAN_PAUSE:
260         case ACCESS_CAN_CONTROL_PACE:
261             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
262             *pb_bool = VLC_FALSE;
263             break;
264         /* */
265         case ACCESS_GET_MTU:
266             pi_int = (int*)va_arg( args, int * );
267             *pi_int = p_sys->i_mtu;
268             break;
269
270         case ACCESS_GET_PTS_DELAY:
271             pi_64 = (int64_t*)va_arg( args, int64_t * );
272             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
273             break;
274
275         /* */
276         case ACCESS_SET_PAUSE_STATE:
277         case ACCESS_GET_TITLE_INFO:
278         case ACCESS_SET_TITLE:
279         case ACCESS_SET_SEEKPOINT:
280         case ACCESS_SET_PRIVATE_ID_STATE:
281             return VLC_EGENERIC;
282
283         default:
284             msg_Warn( p_access, "unimplemented query in control" );
285             return VLC_EGENERIC;
286
287     }
288     return VLC_SUCCESS;
289 }
290
291 /*****************************************************************************
292  * BlockUDP:
293  *****************************************************************************/
294 static block_t *BlockUDP( access_t *p_access )
295 {
296     access_sys_t *p_sys = p_access->p_sys;
297     block_t      *p_block;
298
299     /* Read data */
300     p_block = block_New( p_access, p_sys->i_mtu );
301     p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
302                                   p_block->p_buffer, p_sys->i_mtu,
303                                   VLC_FALSE );
304     if( p_block->i_buffer <= 0 )
305     {
306         block_Release( p_block );
307         return NULL;
308     }
309
310     if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
311         p_sys->i_mtu < 32767 )
312     {
313         /* Increase by 100% */
314         p_sys->i_mtu *= 2;
315         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
316     }
317
318     return p_block;
319 }
320
321 /*****************************************************************************
322  * BlockParseRTP/BlockRTP:
323  *****************************************************************************/
324 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
325 {
326     int     i_rtp_version;
327     int     i_CSRC_count;
328     int     i_payload_type;
329     int     i_skip = 0;
330     int     i_sequence_number = 0;
331
332     if( p_block == NULL )
333         return NULL;
334
335     if( p_block->i_buffer < RTP_HEADER_LEN )
336         goto trash;
337
338     /* Parse the header and make some verifications.
339      * See RFC 1889 & RFC 2250. */
340     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
341     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
342     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
343     i_sequence_number = ( (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3] );
344
345     if ( i_rtp_version != 2 )
346         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
347
348     if( i_payload_type == 14 )
349         i_skip = 4;
350     else if( i_payload_type !=  33 && i_payload_type != 32 )
351         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
352
353     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
354
355     /* A CSRC extension field is 32 bits in size (4 bytes) */
356     if( i_skip >= p_block->i_buffer )
357         goto trash;
358
359     /* Return the packet without the RTP header. */
360     p_block->i_buffer -= i_skip;
361     p_block->p_buffer += i_skip;
362
363 #define RTP_SEQ_NUM_SIZE 65536
364     /* Detect RTP packet loss through tracking sequence numbers.
365      * See RFC 1889. */
366     if( p_access->p_sys->i_sequence_number == -1 )
367         p_access->p_sys->i_sequence_number = i_sequence_number;
368
369     if( ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE) != i_sequence_number )
370     {
371         msg_Warn( p_access, "RTP packet(s) lost, expected sequence number %d got %d",
372             ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE),
373             i_sequence_number );
374         if( i_payload_type == 33 )
375         {
376             /* Mark transport error in the first TS packet in the RTP stream. */
377             p_block->p_buffer[1] |= 0x80;
378         }
379     }
380     p_access->p_sys->i_sequence_number = i_sequence_number;
381 #undef RTP_SEQ_NUM_SIZE
382     return p_block;
383
384 trash:
385     msg_Warn( p_access, "received a too short packet for RTP" );
386     block_Release( p_block );
387     return NULL;
388 }
389
390 static block_t *BlockRTP( access_t *p_access )
391 {
392     block_t *p_block = BlockUDP( p_access );
393
394     if ( p_block != NULL )
395         return BlockParseRTP( p_access, p_block );
396     else
397         return NULL;
398 }
399
400 /*****************************************************************************
401  * BlockChoose: decide between RTP and UDP
402  *****************************************************************************/
403 static block_t *BlockChoose( access_t *p_access )
404 {
405     block_t *p_block;
406     int     i_rtp_version;
407     int     i_CSRC_count;
408     int     i_payload_type;
409
410     if( ( p_block = BlockUDP( p_access ) ) == NULL )
411         return NULL;
412
413     if( p_block->p_buffer[0] == 0x47 )
414     {
415         msg_Dbg( p_access, "detected TS over raw UDP" );
416         p_access->pf_block = BlockUDP;
417         return p_block;
418     }
419
420     if( p_block->i_buffer < RTP_HEADER_LEN )
421         return p_block;
422
423     /* Parse the header and make some verifications.
424      * See RFC 1889 & RFC 2250. */
425
426     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
427     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
428     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
429
430     if( i_rtp_version != 2 )
431     {
432         msg_Dbg( p_access, "no supported RTP header detected" );
433         p_access->pf_block = BlockUDP;
434         return p_block;
435     }
436
437     switch( i_payload_type )
438     {
439         case 33:
440             msg_Dbg( p_access, "detected TS over RTP" );
441             /* Disabled because it is auto-detected. */
442             /* p_access->psz_demux = strdup( "ts" ); */
443             break;
444
445         case 14:
446             msg_Dbg( p_access, "detected MPEG audio over RTP" );
447             p_access->psz_demux = strdup( "mpga" );
448             break;
449
450         case 32:
451             msg_Dbg( p_access, "detected MPEG video over RTP" );
452             p_access->psz_demux = strdup( "mpgv" );
453             break;
454
455         default:
456             msg_Dbg( p_access, "no RTP header detected" );
457             p_access->pf_block = BlockUDP;
458             return p_block;
459     }
460
461     p_access->pf_block = BlockRTP;
462
463     return BlockParseRTP( p_access, p_block );
464 }