]> git.sesse.net Git - vlc/blob - modules/access/udp.c
udp: support for RTP"header extension. Patch by Zohar Lorberbaum .
[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     int     i_extention_flag;
332     int     i_extention_length = 0;
333
334     if( p_block == NULL )
335         return NULL;
336
337     if( p_block->i_buffer < RTP_HEADER_LEN )
338         goto trash;
339
340     /* Parse the header and make some verifications.
341      * See RFC 1889 & RFC 2250. */
342     i_rtp_version     = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
343     i_CSRC_count      = p_block->p_buffer[0] & 0x0F;
344     i_extention_flag  = p_block->p_buffer[0] & 0x10;
345     i_payload_type    = p_block->p_buffer[1] & 0x7F;
346     i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
347
348     if ( i_rtp_version != 2 )
349         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
350
351     if( i_payload_type == 14 )
352         i_skip = 4;
353     else if( i_payload_type !=  33 && i_payload_type != 32 )
354         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
355     if( i_extention_flag )
356         i_extention_length = 4 +
357             4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
358
359     /* Skip header + CSRC extension field n*(32 bits) + extention */
360     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extention_length;
361
362     if( i_skip >= p_block->i_buffer )
363         goto trash;
364
365     /* Return the packet without the RTP header. */
366     p_block->i_buffer -= i_skip;
367     p_block->p_buffer += i_skip;
368
369 #define RTP_SEQ_NUM_SIZE 65536
370     /* Detect RTP packet loss through tracking sequence numbers.
371      * See RFC 1889. */
372     if( p_access->p_sys->i_sequence_number == -1 )
373         p_access->p_sys->i_sequence_number = i_sequence_number;
374
375     if( ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE) != i_sequence_number )
376     {
377         msg_Warn( p_access, "RTP packet(s) lost, expected sequence number %d got %d",
378             ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE),
379             i_sequence_number );
380         if( i_payload_type == 33 )
381         {
382             /* Mark transport error in the first TS packet in the RTP stream. */
383             p_block->p_buffer[1] |= 0x80;
384         }
385     }
386     p_access->p_sys->i_sequence_number = i_sequence_number;
387 #undef RTP_SEQ_NUM_SIZE
388     return p_block;
389
390 trash:
391     msg_Warn( p_access, "received a too short packet for RTP" );
392     block_Release( p_block );
393     return NULL;
394 }
395
396 static block_t *BlockRTP( access_t *p_access )
397 {
398     block_t *p_block = BlockUDP( p_access );
399
400     if ( p_block != NULL )
401         return BlockParseRTP( p_access, p_block );
402     else
403         return NULL;
404 }
405
406 /*****************************************************************************
407  * BlockChoose: decide between RTP and UDP
408  *****************************************************************************/
409 static block_t *BlockChoose( access_t *p_access )
410 {
411     block_t *p_block;
412     int     i_rtp_version;
413     int     i_CSRC_count;
414     int     i_payload_type;
415
416     if( ( p_block = BlockUDP( p_access ) ) == NULL )
417         return NULL;
418
419     if( p_block->p_buffer[0] == 0x47 )
420     {
421         msg_Dbg( p_access, "detected TS over raw UDP" );
422         p_access->pf_block = BlockUDP;
423         return p_block;
424     }
425
426     if( p_block->i_buffer < RTP_HEADER_LEN )
427         return p_block;
428
429     /* Parse the header and make some verifications.
430      * See RFC 1889 & RFC 2250. */
431
432     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
433     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
434     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
435
436     if( i_rtp_version != 2 )
437     {
438         msg_Dbg( p_access, "no supported RTP header detected" );
439         p_access->pf_block = BlockUDP;
440         return p_block;
441     }
442
443     switch( i_payload_type )
444     {
445         case 33:
446             msg_Dbg( p_access, "detected TS over RTP" );
447             /* Disabled because it is auto-detected. */
448             /* p_access->psz_demux = strdup( "ts" ); */
449             break;
450
451         case 14:
452             msg_Dbg( p_access, "detected MPEG audio over RTP" );
453             p_access->psz_demux = strdup( "mpga" );
454             break;
455
456         case 32:
457             msg_Dbg( p_access, "detected MPEG video over RTP" );
458             p_access->psz_demux = strdup( "mpgv" );
459             break;
460
461         default:
462             msg_Dbg( p_access, "no RTP header detected" );
463             p_access->pf_block = BlockUDP;
464             return p_block;
465     }
466
467     p_access->pf_block = BlockRTP;
468
469     return BlockParseRTP( p_access, p_block );
470 }