]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Track RTP sequence numbers and mark the first MPEG2-TS packet with a transport error...
[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
164     psz_server_addr = psz_name;
165     if( *psz_server_addr == '[' )
166         /* skips bracket'd IPv6 address */
167         psz_parser = strchr( psz_name, ']' );
168
169     if( psz_parser != NULL )
170     {
171         psz_parser = strchr( psz_parser, ':' );
172         if( psz_parser != NULL )
173         {
174             *psz_parser++ = '\0';
175             i_server_port = atoi( psz_parser );
176         }
177     }
178
179     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
180              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
181
182     /* Set up p_access */
183     p_access->pf_read = NULL;
184     if( !strcasecmp( p_access->psz_access, "rtp" )
185           || !strcasecmp( p_access->psz_access, "rtp4" )
186           || !strcasecmp( p_access->psz_access, "rtp6" ) )
187     {
188         p_access->pf_block = BlockRTP;
189     }
190     else
191     {
192         p_access->pf_block = BlockChoose;
193     }
194     p_access->pf_control = Control;
195     p_access->pf_seek = NULL;
196     p_access->info.i_update = 0;
197     p_access->info.i_size = 0;
198     p_access->info.i_pos = 0;
199     p_access->info.b_eof = VLC_FALSE;
200     p_access->info.i_title = 0;
201     p_access->info.i_seekpoint = 0;
202
203     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
204     p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
205                                       psz_server_addr, i_server_port );
206     if( p_sys->fd < 0 )
207     {
208         msg_Err( p_access, "cannot open socket" );
209         free( psz_name );
210         free( p_sys );
211         return VLC_EGENERIC;
212     }
213     free( psz_name );
214
215     net_StopSend( p_sys->fd );
216
217     /* FIXME */
218     p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
219     if( p_sys->i_mtu <= 1 )
220         p_sys->i_mtu  = 1500;   /* Avoid problem */
221
222     p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
223
224     /* Update default_pts to a suitable value for udp access */
225     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
226
227     /* Keep track of RTP sequence number */
228     p_sys->i_sequence_number = -1;
229     
230     return VLC_SUCCESS;
231 }
232
233 /*****************************************************************************
234  * Close: free unused data structures
235  *****************************************************************************/
236 static void Close( vlc_object_t *p_this )
237 {
238     access_t     *p_access = (access_t*)p_this;
239     access_sys_t *p_sys = p_access->p_sys;
240
241     net_Close( p_sys->fd );
242     free( p_sys );
243 }
244
245 /*****************************************************************************
246  * Control:
247  *****************************************************************************/
248 static int Control( access_t *p_access, int i_query, va_list args )
249 {
250     access_sys_t *p_sys = p_access->p_sys;
251     vlc_bool_t   *pb_bool;
252     int          *pi_int;
253     int64_t      *pi_64;
254
255     switch( i_query )
256     {
257         /* */
258         case ACCESS_CAN_SEEK:
259         case ACCESS_CAN_FASTSEEK:
260         case ACCESS_CAN_PAUSE:
261         case ACCESS_CAN_CONTROL_PACE:
262             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
263             *pb_bool = VLC_FALSE;
264             break;
265         /* */
266         case ACCESS_GET_MTU:
267             pi_int = (int*)va_arg( args, int * );
268             *pi_int = p_sys->i_mtu;
269             break;
270
271         case ACCESS_GET_PTS_DELAY:
272             pi_64 = (int64_t*)va_arg( args, int64_t * );
273             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
274             break;
275
276         /* */
277         case ACCESS_SET_PAUSE_STATE:
278         case ACCESS_GET_TITLE_INFO:
279         case ACCESS_SET_TITLE:
280         case ACCESS_SET_SEEKPOINT:
281         case ACCESS_SET_PRIVATE_ID_STATE:
282             return VLC_EGENERIC;
283
284         default:
285             msg_Warn( p_access, "unimplemented query in control" );
286             return VLC_EGENERIC;
287
288     }
289     return VLC_SUCCESS;
290 }
291
292 /*****************************************************************************
293  * BlockUDP:
294  *****************************************************************************/
295 static block_t *BlockUDP( access_t *p_access )
296 {
297     access_sys_t *p_sys = p_access->p_sys;
298     block_t      *p_block;
299
300     /* Read data */
301     p_block = block_New( p_access, p_sys->i_mtu );
302     p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
303                                   p_block->p_buffer, p_sys->i_mtu,
304                                   VLC_FALSE );
305     if( p_block->i_buffer <= 0 )
306     {
307         block_Release( p_block );
308         return NULL;
309     }
310
311     if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
312         p_sys->i_mtu < 32767 )
313     {
314         /* Increase by 100% */
315         p_sys->i_mtu *= 2;
316         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
317     }
318
319     return p_block;
320 }
321
322 /*****************************************************************************
323  * BlockParseRTP/BlockRTP:
324  *****************************************************************************/
325 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
326 {
327     int     i_rtp_version;
328     int     i_CSRC_count;
329     int     i_payload_type;
330     int     i_skip = 0;
331     int     i_sequence_number = 0;
332  
333     if( p_block == NULL )
334         return NULL;
335         
336     if( p_block->i_buffer < RTP_HEADER_LEN )
337         goto trash;
338
339     /* Parse the header and make some verifications.
340      * See RFC 1889 & RFC 2250. */
341     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
342     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
343     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
344     i_sequence_number = ( (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3] );
345
346     if ( i_rtp_version != 2 )
347         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
348
349     if( i_payload_type == 14 )
350         i_skip = 4;
351     else if( i_payload_type !=  33 && i_payload_type != 32 )
352         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
353
354     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
355
356     /* A CSRC extension field is 32 bits in size (4 bytes) */
357     if( i_skip >= p_block->i_buffer )
358         goto trash;
359
360     /* Return the packet without the RTP header. */
361     p_block->i_buffer -= i_skip;
362     p_block->p_buffer += i_skip;
363     
364 #define RTP_SEQ_NUM_SIZE 65536
365     /* Detect RTP packet loss through tracking sequence numbers.
366      * See RFC 1889. */
367     if( p_access->p_sys->i_sequence_number == -1 )
368         p_access->p_sys->i_sequence_number = i_sequence_number;
369     
370     if( ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE) != i_sequence_number )
371     {
372         msg_Warn( p_access, "RTP packet(s) lost, expected sequence number %d got %d",
373             ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE),
374             i_sequence_number );
375         if( i_payload_type == 33 )
376         {
377             /* Mark transport error in the first TS packet in the RTP stream. */
378             p_block->p_buffer[1] |= 0x80;
379         }
380     }
381     p_access->p_sys->i_sequence_number = i_sequence_number;
382 #undef RTP_SEQ_NUM_SIZE
383     return p_block;
384
385 trash:
386     msg_Warn( p_access, "received a too short packet for RTP" );
387     block_Release( p_block );
388     return NULL;
389 }
390
391 static block_t *BlockRTP( access_t *p_access )
392 {
393     block_t *p_block = BlockUDP( p_access );
394
395     if ( p_block != NULL )
396         return BlockParseRTP( p_access, p_block );
397     else
398         return NULL;
399 }
400
401 /*****************************************************************************
402  * BlockChoose: decide between RTP and UDP
403  *****************************************************************************/
404 static block_t *BlockChoose( access_t *p_access )
405 {
406     block_t *p_block;
407     int     i_rtp_version;
408     int     i_CSRC_count;
409     int     i_payload_type;
410
411     if( ( p_block = BlockUDP( p_access ) ) == NULL )
412         return NULL;
413
414     if( p_block->p_buffer[0] == 0x47 )
415     {
416         msg_Dbg( p_access, "detected TS over raw UDP" );
417         p_access->pf_block = BlockUDP;
418         return p_block;
419     }
420
421     if( p_block->i_buffer < RTP_HEADER_LEN )
422         return p_block;
423
424     /* Parse the header and make some verifications.
425      * See RFC 1889 & RFC 2250. */
426
427     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
428     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
429     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
430
431     if( i_rtp_version != 2 )
432     {
433         msg_Dbg( p_access, "no supported RTP header detected" );
434         p_access->pf_block = BlockUDP;
435         return p_block;
436     }
437
438     switch( i_payload_type )
439     {
440         case 33:
441             msg_Dbg( p_access, "detected TS over RTP" );
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 }