]> git.sesse.net Git - vlc/blob - modules/access/udp.c
don't perform prebuffering for UDP, since it causes refclock fluctuation
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP & RTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 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 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
52 #define RTP_LATE_LONGTEXT N_( \
53     "Allows you to modify the RTP reordering behaviour. " \
54     "RTP input will wait for late packets upto " \
55     "the specified timeout in milisecond units." )
56
57 static int  Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
59
60 vlc_module_begin();
61     set_shortname( _("UDP/RTP" ) );
62     set_description( _("UDP/RTP input") );
63     set_category( CAT_INPUT );
64     set_subcategory( SUBCAT_INPUT_ACCESS );
65
66     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
67                  CACHING_LONGTEXT, VLC_TRUE );
68     add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
69
70     add_bool( "udp-auto-mtu", 1, NULL,
71               AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
72
73     set_capability( "access2", 0 );
74     add_shortcut( "udp" );
75     add_shortcut( "udpstream" );
76     add_shortcut( "udp4" );
77     add_shortcut( "udp6" );
78     add_shortcut( "rtp" );
79     add_shortcut( "rtp4" );
80     add_shortcut( "rtp6" );
81     set_callbacks( Open, Close );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 #define RTP_HEADER_LEN 12
88
89 static block_t *BlockUDP( access_t * );
90 static block_t *BlockRTP( access_t * );
91 static block_t *BlockChoose( access_t * );
92 static int Control( access_t *, int, va_list );
93
94 struct access_sys_t
95 {
96     int fd;
97
98     int i_mtu;
99     vlc_bool_t b_auto_mtu;
100
101     /* reorder rtp packets when out-of-sequence */
102     mtime_t i_rtp_late;
103     uint16_t i_last_seqno;
104     block_t *p_list;
105     block_t *p_end;
106 };
107
108 /*****************************************************************************
109  * Open: open the socket
110  *****************************************************************************/
111 static int Open( vlc_object_t *p_this )
112 {
113     access_t     *p_access = (access_t*)p_this;
114     access_sys_t *p_sys;
115
116     char *psz_name = strdup( p_access->psz_path );
117     char *psz_parser, *psz_server_addr, *psz_bind_addr = "";
118     int  i_bind_port, i_server_port = 0;
119
120     /* First set ipv4/ipv6 */
121     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
122     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
123
124     if( *p_access->psz_access )
125     {
126         vlc_value_t val;
127         /* Find out which shortcut was used */
128         if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
129             !strncmp( p_access->psz_access, "rtp4", 6 ))
130         {
131             val.b_bool = VLC_TRUE;
132             var_Set( p_access, "ipv4", val );
133
134             val.b_bool = VLC_FALSE;
135             var_Set( p_access, "ipv6", val );
136         }
137         else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
138                  !strncmp( p_access->psz_access, "rtp6", 6 ) )
139         {
140             val.b_bool = VLC_TRUE;
141             var_Set( p_access, "ipv6", val );
142
143             val.b_bool = VLC_FALSE;
144             var_Set( p_access, "ipv4", val );
145         }
146     }
147
148     i_bind_port = var_CreateGetInteger( p_access, "server-port" );
149
150     /* Parse psz_name syntax :
151      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
152     psz_parser = strchr( psz_name, '@' );
153     if( psz_parser != NULL )
154     {
155         /* Found bind address and/or bind port */
156         *psz_parser++ = '\0';
157         psz_bind_addr = psz_parser;
158
159         if( *psz_parser == '[' )
160             /* skips bracket'd IPv6 address */
161             psz_parser = strchr( psz_parser, ']' );
162
163         if( psz_parser != NULL )
164         {
165             psz_parser = strchr( psz_parser, ':' );
166             if( psz_parser != NULL )
167             {
168                 *psz_parser++ = '\0';
169                 i_bind_port = atoi( psz_parser );
170             }
171         }
172     }
173
174     psz_server_addr = psz_name;
175     if( *psz_server_addr == '[' )
176         /* skips bracket'd IPv6 address */
177         psz_parser = strchr( psz_name, ']' );
178
179     if( psz_parser != NULL )
180     {
181         psz_parser = strchr( psz_parser, ':' );
182         if( psz_parser != NULL )
183         {
184             *psz_parser++ = '\0';
185             i_server_port = atoi( psz_parser );
186         }
187     }
188
189     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
190              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
191
192     /* Set up p_access */
193     p_access->pf_read = NULL;
194     if( !strcasecmp( p_access->psz_access, "rtp" )
195           || !strcasecmp( p_access->psz_access, "rtp4" )
196           || !strcasecmp( p_access->psz_access, "rtp6" ) )
197     {
198         p_access->pf_block = BlockRTP;
199     }
200     else
201     {
202         p_access->pf_block = BlockChoose;
203     }
204     p_access->pf_control = Control;
205     p_access->pf_seek = NULL;
206     p_access->info.i_update = 0;
207     p_access->info.i_size = 0;
208     p_access->info.i_pos = 0;
209     p_access->info.b_eof = VLC_FALSE;
210     p_access->info.b_prebuffered = VLC_FALSE;
211     p_access->info.i_title = 0;
212     p_access->info.i_seekpoint = 0;
213
214     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
215     p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
216                                       psz_server_addr, i_server_port );
217     if( p_sys->fd < 0 )
218     {
219         msg_Err( p_access, "cannot open socket" );
220         free( psz_name );
221         free( p_sys );
222         return VLC_EGENERIC;
223     }
224     free( psz_name );
225
226     net_StopSend( p_sys->fd );
227
228     /* FIXME */
229     p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
230     if( p_sys->i_mtu <= 1 )
231         p_sys->i_mtu  = 1500;   /* Avoid problem */
232
233     p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
234
235     /* Update default_pts to a suitable value for udp access */
236     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
237
238
239     /* RTP reordering for out-of-sequence packets */
240     p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
241     p_sys->i_last_seqno = 0;
242     p_sys->p_list = NULL;
243     p_sys->p_end = NULL;
244     return VLC_SUCCESS;
245 }
246
247 /*****************************************************************************
248  * Close: free unused data structures
249  *****************************************************************************/
250 static void Close( vlc_object_t *p_this )
251 {
252     access_t     *p_access = (access_t*)p_this;
253     access_sys_t *p_sys = p_access->p_sys;
254
255     block_ChainRelease( p_sys->p_list );
256     net_Close( p_sys->fd );
257     free( p_sys );
258 }
259
260 /*****************************************************************************
261  * Control:
262  *****************************************************************************/
263 static int Control( access_t *p_access, int i_query, va_list args )
264 {
265     access_sys_t *p_sys = p_access->p_sys;
266     vlc_bool_t   *pb_bool;
267     int          *pi_int;
268     int64_t      *pi_64;
269
270     switch( i_query )
271     {
272         /* */
273         case ACCESS_CAN_SEEK:
274         case ACCESS_CAN_FASTSEEK:
275         case ACCESS_CAN_PAUSE:
276         case ACCESS_CAN_CONTROL_PACE:
277             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
278             *pb_bool = VLC_FALSE;
279             break;
280         /* */
281         case ACCESS_GET_MTU:
282             pi_int = (int*)va_arg( args, int * );
283             *pi_int = p_sys->i_mtu;
284             break;
285
286         case ACCESS_GET_PTS_DELAY:
287             pi_64 = (int64_t*)va_arg( args, int64_t * );
288             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
289             break;
290
291         /* */
292         case ACCESS_SET_PAUSE_STATE:
293         case ACCESS_GET_TITLE_INFO:
294         case ACCESS_SET_TITLE:
295         case ACCESS_SET_SEEKPOINT:
296         case ACCESS_SET_PRIVATE_ID_STATE:
297             return VLC_EGENERIC;
298
299         default:
300             msg_Warn( p_access, "unimplemented query in control" );
301             return VLC_EGENERIC;
302
303     }
304     return VLC_SUCCESS;
305 }
306
307 /*****************************************************************************
308  * BlockUDP:
309  *****************************************************************************/
310 static block_t *BlockUDP( access_t *p_access )
311 {
312     access_sys_t *p_sys = p_access->p_sys;
313     block_t      *p_block;
314
315     /* Read data */
316     p_block = block_New( p_access, p_sys->i_mtu );
317     p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
318                                   p_block->p_buffer, p_sys->i_mtu,
319                                   VLC_FALSE );
320     if( p_block->i_buffer <= 0 )
321     {
322         block_Release( p_block );
323         return NULL;
324     }
325
326     if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
327         p_sys->i_mtu < 32767 )
328     {
329         /* Increase by 100% */
330         p_sys->i_mtu *= 2;
331         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
332     }
333
334     return p_block;
335 }
336
337 /*
338  * rtp_ChainInsert - insert a p_block in the chain and
339  * look at the sequence numbers.
340  */
341 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
342 {
343     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
344     block_t *p_prev = NULL;
345     block_t *p = p_sys->p_end;
346     uint16_t i_new = (uint16_t) p_block->i_dts;
347     uint16_t i_tmp = 0;
348
349     if( !p_sys->p_list )
350     {
351         p_sys->p_list = p_block;
352         p_sys->p_end = p_block;
353         return VLC_TRUE;
354     }
355     /* walk through the queue from top down since the new packet is in 
356     most cases just appended to the end */
357
358     for( ;; )
359     {
360         i_tmp = i_new - (uint16_t) p->i_dts;
361
362         if( !i_tmp )   /* trash duplicate */
363             break; 
364
365         if ( i_tmp < 32768 )
366         {   /* insert after this block ( i_new > p->i_dts ) */
367             p_block->p_next = p->p_next;
368             p->p_next = p_block;
369             p_block->p_prev = p;
370             if (p_prev)
371             {
372                 p_prev->p_prev = p_block;
373                 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d", 
374                     (uint16_t) p->i_dts, i_new );
375             }
376             else 
377             {
378                 p_sys->p_end = p_block;
379             }
380             return VLC_TRUE;
381         }
382         if( p == p_sys->p_list )
383         {   /* we've reached bottom of chain */
384             i_tmp = p_sys->i_last_seqno - i_new;
385             if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
386             {
387                 msg_Dbg(p_access, "RTP reordering: prepend %d before %d", 
388                         i_new, (uint16_t) p->i_dts );
389                 p_block->p_next = p;
390                 p->p_prev = p_block;
391                 p_sys->p_list = p_block;
392                 return VLC_TRUE;
393             }
394
395             if( !i_tmp )   /* trash duplicate */
396                 break;    
397
398             /* reordering failed - append the packet to the end of queue */
399             msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
400                 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts, 
401                 (uint16_t) p_sys->p_end->i_dts);
402             p_sys->p_end->p_next = p_block;
403             p_block->p_prev = p_sys->p_end;
404             p_sys->p_end = p_block;
405             return VLC_TRUE;
406         }
407         p_prev = p;
408         p = p->p_prev;
409     }
410     block_Release( p_block );
411     return VLC_FALSE;
412 }
413
414 /*****************************************************************************
415  * BlockParseRTP/BlockRTP:
416  *****************************************************************************/
417 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
418 {
419     int      i_rtp_version;
420     int      i_CSRC_count;
421     int      i_payload_type;
422     int      i_skip = 0;
423     int      i_extension_flag = 0;
424     int      i_extension_length = 0;
425     uint16_t i_sequence_number = 0;
426
427     if( p_block == NULL )
428         return NULL;
429
430     if( p_block->i_buffer < RTP_HEADER_LEN )
431         goto trash;
432
433     /* Parse the header and make some verifications.
434      * See RFC 3550. */
435     i_rtp_version     = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
436     i_CSRC_count      = p_block->p_buffer[0] & 0x0F;
437     i_extension_flag  = p_block->p_buffer[0] & 0x10;
438     i_payload_type    = p_block->p_buffer[1] & 0x7F;
439     i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
440
441     if( i_rtp_version != 2 )
442         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
443
444     if( i_payload_type == 14 || i_payload_type == 32)
445         i_skip = 4;
446     else if( i_payload_type !=  33 )
447         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
448     if( i_extension_flag )
449         i_extension_length = 4 +
450             4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
451
452     /* Skip header + CSRC extension field n*(32 bits) + extension */
453     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
454
455     if( i_skip >= p_block->i_buffer )
456         goto trash;
457
458     /* Return the packet without the RTP header, remember seqno in i_dts */
459     p_block->i_buffer -= i_skip;
460     p_block->p_buffer += i_skip;
461     p_block->i_pts = mdate();
462     p_block->i_dts = (mtime_t) i_sequence_number;
463
464 #if 0
465     /* Emulate packet loss */
466     if ( (i_sequence_number % 4000) == 0)
467     {
468         msg_Warn( p_access, "Emulating packet drop" );
469         block_Release( p_block );
470         return NULL;
471     }
472 #endif
473
474     return p_block;
475
476
477 trash:
478     msg_Warn( p_access, "received a too short packet for RTP" );
479     block_Release( p_block );
480     return NULL;
481 }
482
483 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
484 {
485     access_sys_t *p_sys = p_access->p_sys;
486     mtime_t   i_first = mdate();
487     int       i_count = 0;
488     block_t   *p = p_block;
489
490     for( ;; )
491     {
492         mtime_t i_date = mdate();
493
494         if( p && rtp_ChainInsert( p_access, p ))
495             i_count++;
496
497         /* Require at least 2 packets in the buffer */
498         if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
499             break;
500
501         p = BlockParseRTP( p_access, BlockUDP( p_access ));
502         if( !p && (i_date - i_first) > p_sys->i_rtp_late ) 
503         {
504             msg_Err( p_access, "Error in RTP prebuffering!" );
505             break;
506         }
507     }
508
509     msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
510     p_access->info.b_prebuffered = VLC_TRUE;
511     p = p_sys->p_list;
512     p_sys->p_list = p_sys->p_list->p_next;
513     p_sys->i_last_seqno = (uint16_t) p->i_dts;
514     p->p_next = NULL;
515     return p;
516 }
517
518 static block_t *BlockRTP( access_t *p_access )
519 {
520     access_sys_t *p_sys = p_access->p_sys;
521     block_t *p;
522
523     while ( !p_sys->p_list || 
524              ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
525     {
526         p = BlockParseRTP( p_access, BlockUDP( p_access ));
527
528         if ( !p ) 
529             return NULL;
530
531         if ( !p_access->info.b_prebuffered )
532             return BlockPrebufferRTP( p_access, p );
533  
534         rtp_ChainInsert( p_access, p );
535     }
536
537     p = p_sys->p_list;
538     p_sys->p_list = p_sys->p_list->p_next;
539     p_sys->i_last_seqno++;
540     if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
541     {
542         msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
543                  p_sys->i_last_seqno, (uint16_t) p->i_dts );
544         p_sys->i_last_seqno = (uint16_t) p->i_dts;
545     }
546     p->p_next = NULL;
547     return p;
548 }
549
550 /*****************************************************************************
551  * BlockChoose: decide between RTP and UDP
552  *****************************************************************************/
553 static block_t *BlockChoose( access_t *p_access )
554 {
555     block_t *p_block;
556     int     i_rtp_version;
557     int     i_CSRC_count;
558     int     i_payload_type;
559
560     if( ( p_block = BlockUDP( p_access ) ) == NULL )
561         return NULL;
562
563     if( p_block->p_buffer[0] == 0x47 )
564     {
565         msg_Dbg( p_access, "detected TS over raw UDP" );
566         p_access->pf_block = BlockUDP;
567         p_access->info.b_prebuffered = VLC_TRUE;
568         return p_block;
569     }
570
571     if( p_block->i_buffer < RTP_HEADER_LEN )
572         return p_block;
573
574     /* Parse the header and make some verifications.
575      * See RFC 3550. */
576
577     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
578     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
579     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
580
581     if( i_rtp_version != 2 )
582     {
583         msg_Dbg( p_access, "no supported RTP header detected" );
584         p_access->pf_block = BlockUDP;
585         p_access->info.b_prebuffered = VLC_TRUE;
586         return p_block;
587     }
588
589     switch( i_payload_type )
590     {
591         case 33:
592             msg_Dbg( p_access, "detected TS over RTP" );
593             p_access->psz_demux = strdup( "ts" );
594             break;
595
596         case 14:
597             msg_Dbg( p_access, "detected MPEG audio over RTP" );
598             p_access->psz_demux = strdup( "mpga" );
599             break;
600
601         case 32:
602             msg_Dbg( p_access, "detected MPEG video over RTP" );
603             p_access->psz_demux = strdup( "mpgv" );
604             break;
605
606         default:
607             msg_Dbg( p_access, "no RTP header detected" );
608             p_access->pf_block = BlockUDP;
609             p_access->info.b_prebuffered = VLC_TRUE;
610             return p_block;
611     }
612
613     if( !BlockParseRTP( p_access, p_block )) return NULL;
614
615     p_access->pf_block = BlockRTP;
616
617     return BlockPrebufferRTP( p_access, p_block );
618 }