]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Several RTP reordering fixes (Thanks to Marian Durkovic for finding them)
[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_("Reorder timeout in ms for late RTP packets")
52 #define RTP_LATE_LONGTEXT N_( \
53     "Allows you to modify the RTP packets reorder and late behaviour. " \
54     "If enabled (value>0) then out-of-order packets will be held for the " \
55     "specified timeout in ms. " \
56     "The default behaviour is not to reorder." )
57
58 static int  Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
60
61 vlc_module_begin();
62     set_shortname( _("UDP/RTP" ) );
63     set_description( _("UDP/RTP input") );
64     set_category( CAT_INPUT );
65     set_subcategory( SUBCAT_INPUT_ACCESS );
66
67     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
68                  CACHING_LONGTEXT, VLC_TRUE );
69     add_bool( "udp-auto-mtu", 1, NULL,
70               AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
71
72     add_integer( "rtp-late", 0, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
73
74     set_capability( "access2", 0 );
75     add_shortcut( "udp" );
76     add_shortcut( "udpstream" );
77     add_shortcut( "udp4" );
78     add_shortcut( "udp6" );
79     add_shortcut( "rtp" );
80     add_shortcut( "rtp4" );
81     add_shortcut( "rtp6" );
82     set_callbacks( Open, Close );
83 vlc_module_end();
84
85 /*****************************************************************************
86  * Local prototypes
87  *****************************************************************************/
88 #define RTP_HEADER_LEN 12
89 #define RTP_SEQ_NUM_SIZE 65536
90
91 static block_t *BlockUDP( access_t * );
92 static block_t *BlockRTP( access_t * );
93 static block_t *BlockChoose( access_t * );
94 static int Control( access_t *, int, va_list );
95
96 struct access_sys_t
97 {
98     int fd;
99
100     int i_mtu;
101     vlc_bool_t b_auto_mtu;
102
103     /* rtp only */
104     uint16_t i_sequence_number;
105     vlc_bool_t b_first_seqno;
106
107     /* reorder rtp packets when out-of-bounds 
108      * the packets hold queue is one level deep
109      */
110     uint32_t i_rtp_late; /* number of ms an RTP packet may be too late*/
111     uint32_t i_last_pcr; /* last known good PCR */
112     block_t *p_list;     /* list of packets to rearrange */
113     block_t *p_end;      /* last packet in p_list */
114     block_t *p_next;     /* p_next ?? */
115 };
116
117 /*****************************************************************************
118  * Open: open the socket
119  *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
121 {
122     access_t     *p_access = (access_t*)p_this;
123     access_sys_t *p_sys;
124
125     char *psz_name = strdup( p_access->psz_path );
126     char *psz_parser, *psz_server_addr, *psz_bind_addr = "";
127     int  i_bind_port, i_server_port = 0;
128
129     /* First set ipv4/ipv6 */
130     var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
131     var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
132
133     if( *p_access->psz_access )
134     {
135         vlc_value_t val;
136         /* Find out which shortcut was used */
137         if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
138             !strncmp( p_access->psz_access, "rtp4", 6 ))
139         {
140             val.b_bool = VLC_TRUE;
141             var_Set( p_access, "ipv4", val );
142
143             val.b_bool = VLC_FALSE;
144             var_Set( p_access, "ipv6", val );
145         }
146         else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
147                  !strncmp( p_access->psz_access, "rtp6", 6 ) )
148         {
149             val.b_bool = VLC_TRUE;
150             var_Set( p_access, "ipv6", val );
151
152             val.b_bool = VLC_FALSE;
153             var_Set( p_access, "ipv4", val );
154         }
155     }
156
157     i_bind_port = var_CreateGetInteger( p_access, "server-port" );
158
159     /* Parse psz_name syntax :
160      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
161     psz_parser = strchr( psz_name, '@' );
162     if( psz_parser != NULL )
163     {
164         /* Found bind address and/or bind port */
165         *psz_parser++ = '\0';
166         psz_bind_addr = psz_parser;
167
168         if( *psz_parser == '[' )
169             /* skips bracket'd IPv6 address */
170             psz_parser = strchr( psz_parser, ']' );
171
172         if( psz_parser != NULL )
173         {
174             psz_parser = strchr( psz_parser, ':' );
175             if( psz_parser != NULL )
176             {
177                 *psz_parser++ = '\0';
178                 i_bind_port = atoi( psz_parser );
179             }
180         }
181     }
182
183     psz_server_addr = psz_name;
184     if( *psz_server_addr == '[' )
185         /* skips bracket'd IPv6 address */
186         psz_parser = strchr( psz_name, ']' );
187
188     if( psz_parser != NULL )
189     {
190         psz_parser = strchr( psz_parser, ':' );
191         if( psz_parser != NULL )
192         {
193             *psz_parser++ = '\0';
194             i_server_port = atoi( psz_parser );
195         }
196     }
197
198     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
199              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
200
201     /* Set up p_access */
202     p_access->pf_read = NULL;
203     if( !strcasecmp( p_access->psz_access, "rtp" )
204           || !strcasecmp( p_access->psz_access, "rtp4" )
205           || !strcasecmp( p_access->psz_access, "rtp6" ) )
206     {
207         p_access->pf_block = BlockRTP;
208     }
209     else
210     {
211         p_access->pf_block = BlockChoose;
212     }
213     p_access->pf_control = Control;
214     p_access->pf_seek = NULL;
215     p_access->info.i_update = 0;
216     p_access->info.i_size = 0;
217     p_access->info.i_pos = 0;
218     p_access->info.b_eof = VLC_FALSE;
219     p_access->info.i_title = 0;
220     p_access->info.i_seekpoint = 0;
221
222     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
223     p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
224                                       psz_server_addr, i_server_port );
225     if( p_sys->fd < 0 )
226     {
227         msg_Err( p_access, "cannot open socket" );
228         free( psz_name );
229         free( p_sys );
230         return VLC_EGENERIC;
231     }
232     free( psz_name );
233
234     net_StopSend( p_sys->fd );
235
236     /* FIXME */
237     p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
238     if( p_sys->i_mtu <= 1 )
239         p_sys->i_mtu  = 1500;   /* Avoid problem */
240
241     p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
242
243     /* Update default_pts to a suitable value for udp access */
244     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
245
246     /* Keep track of RTP sequence number */
247     p_sys->i_sequence_number = 0;
248     p_sys->b_first_seqno = VLC_TRUE;
249
250     /* RTP reordering out-of-bound packets */
251     p_sys->i_last_pcr = 0;
252     p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" );
253     p_sys->p_list = NULL;
254     p_sys->p_end = NULL;
255     p_sys->p_next = NULL;
256
257     return VLC_SUCCESS;
258 }
259
260 /*****************************************************************************
261  * Close: free unused data structures
262  *****************************************************************************/
263 static void Close( vlc_object_t *p_this )
264 {
265     access_t     *p_access = (access_t*)p_this;
266     access_sys_t *p_sys = p_access->p_sys;
267
268     block_ChainRelease( p_sys->p_list );
269     net_Close( p_sys->fd );
270     free( p_sys );
271 }
272
273 /*****************************************************************************
274  * Control:
275  *****************************************************************************/
276 static int Control( access_t *p_access, int i_query, va_list args )
277 {
278     access_sys_t *p_sys = p_access->p_sys;
279     vlc_bool_t   *pb_bool;
280     int          *pi_int;
281     int64_t      *pi_64;
282
283     switch( i_query )
284     {
285         /* */
286         case ACCESS_CAN_SEEK:
287         case ACCESS_CAN_FASTSEEK:
288         case ACCESS_CAN_PAUSE:
289         case ACCESS_CAN_CONTROL_PACE:
290             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
291             *pb_bool = VLC_FALSE;
292             break;
293         /* */
294         case ACCESS_GET_MTU:
295             pi_int = (int*)va_arg( args, int * );
296             *pi_int = p_sys->i_mtu;
297             break;
298
299         case ACCESS_GET_PTS_DELAY:
300             pi_64 = (int64_t*)va_arg( args, int64_t * );
301             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
302             break;
303
304         /* */
305         case ACCESS_SET_PAUSE_STATE:
306         case ACCESS_GET_TITLE_INFO:
307         case ACCESS_SET_TITLE:
308         case ACCESS_SET_SEEKPOINT:
309         case ACCESS_SET_PRIVATE_ID_STATE:
310             return VLC_EGENERIC;
311
312         default:
313             msg_Warn( p_access, "unimplemented query in control" );
314             return VLC_EGENERIC;
315
316     }
317     return VLC_SUCCESS;
318 }
319
320 /*****************************************************************************
321  * BlockUDP:
322  *****************************************************************************/
323 static block_t *BlockUDP( access_t *p_access )
324 {
325     access_sys_t *p_sys = p_access->p_sys;
326     block_t      *p_block;
327
328     /* Read data */
329     p_block = block_New( p_access, p_sys->i_mtu );
330     p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
331                                   p_block->p_buffer, p_sys->i_mtu,
332                                   VLC_FALSE );
333     if( p_block->i_buffer <= 0 )
334     {
335         block_Release( p_block );
336         return NULL;
337     }
338
339     if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
340         p_sys->i_mtu < 32767 )
341     {
342         /* Increase by 100% */
343         p_sys->i_mtu *= 2;
344         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
345     }
346
347     return p_block;
348 }
349
350 /*
351  * rtp_ChainInsert - insert a p_block in the chain and
352  * look at the sequence numbers.
353  */
354 static inline void rtp_ChainInsert( access_t *p_access, block_t **pp_list, block_t **pp_end, block_t *p_block )
355 {
356     block_t *p_tmp = NULL;
357     block_t *p = NULL;
358     uint16_t i_new = 0;
359     uint16_t i_cur = 0;
360     uint16_t i_expected = 0;
361     uint32_t i_pcr_new = 0;
362
363     if( *pp_list == NULL )
364     {
365         *pp_list = p_block;
366         *pp_end  = p_block;
367         return;
368     }
369     /* Appending packets at the end of the chain is the normal case */
370     i_pcr_new = ( (p_block->p_buffer[4] << 24) +
371                   (p_block->p_buffer[5] << 16) +
372                   (p_block->p_buffer[6] << 8) +
373                    p_block->p_buffer[7] );
374     i_new = ( (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3] );
375
376     p = *pp_end;
377     i_cur = ( (p->p_buffer[2] << 8 ) + p->p_buffer[3] );
378     i_expected = ((i_cur+1) % RTP_SEQ_NUM_SIZE);
379     if( (i_new - i_expected) >= 0 ) /* Append at the end? */
380     {
381         msg_Dbg( p_access, ">> append %p(%u)==%p(%u)\n", p_block, i_cur, p, i_new );
382         p->p_next = *pp_end = p_block;
383         return;
384     }
385     /* Add to the front fo the chain? */
386     p = *pp_list;
387     i_new = ( (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3] );
388     i_cur = ( (p->p_buffer[2] << 8 ) + p->p_buffer[3] );
389     if( (i_cur - i_new) > 0 )
390     {
391         msg_Dbg( p_access, ">> prepend %p(%u)==%p(%u)\n", p_block, i_cur, p, i_new );
392         p_block->p_next = p;
393         *pp_list = p_block;
394         return;
395     }
396     /* The packet can't be added to the front or the end of the chain,
397      * thus walk the chain from the start.
398      */
399     while( p )
400     {
401         i_cur = (p->p_buffer[2] << 8 ) + p->p_buffer[3];
402         i_expected = (i_cur+1) % RTP_SEQ_NUM_SIZE;
403
404         msg_Dbg( p_access,  "i_cur: %u, i_new: %u", i_cur, i_new);
405         if( i_cur == i_new )
406         {
407             uint32_t i_pcr_cur = ( (p->p_buffer[4] << 24) +
408                                    (p->p_buffer[5] << 16) +
409                                    (p->p_buffer[6] << 8) +
410                                     p->p_buffer[7] );
411             /* This packet might be a duplicate, so check PCR's */
412             if( i_pcr_cur >= i_pcr_new )
413             {
414                 /* packet way too late drop it. */
415                 block_Release( p_block );
416                 return;
417             }
418             /* Add it to list later on
419              * else if( i_pcr_cur < i_pcr_new ) */
420             break;
421         }
422         else if( (i_expected - i_new) >= 0 ) /* insert in chain */
423         {
424             p_tmp = p->p_next;
425             msg_Dbg( p_access, ">> insert between %p(%u)==%p(%u)", p, i_cur, p_tmp, i_new );
426             p->p_next = p_block;
427             p_block->p_next = p_tmp;
428             return;
429         }
430         if( !p->p_next ) break;
431         p = p->p_next;
432     }
433 }
434
435 /*
436  * rtp_ChainSend - look which packets are ready for sending.
437  */
438 static inline block_t *rtp_ChainSend( access_t *p_access, block_t **pp_list, uint16_t i_seq )
439 {
440     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
441     uint16_t i_cur = 0;
442
443     if( *pp_list )
444     {
445         /* Parse RTP header */
446         int i_skip = 0;
447         int i_extension_bit = 0;
448         int i_extension_length = 0;
449         int i_CSRC_count = 0;
450         int i_payload_type = 0;
451         uint32_t i_pcr_prev = 0;
452         uint16_t i_seq_prev = 0;
453         /* Data pointers */
454         block_t *p_prev = NULL;
455         block_t *p_send = *pp_list;
456         block_t *p = *pp_list;
457
458         while( p )
459         {
460             i_cur = ( (p->p_buffer[2] << 8 ) + p->p_buffer[3] );
461             msg_Dbg( p_access, "rtp_ChainSend: i_cur %u, i_seq %u", i_cur, i_seq );
462             if( (i_cur - i_seq) == 0 )
463             {
464                 i_seq++; /* sent all packets that are received in order */
465
466                 /* Remember PCR and sequence number of packet
467                  * for next iteration */
468                 i_pcr_prev = ( (p->p_buffer[4] << 24) +
469                                (p->p_buffer[5] << 16) +
470                                (p->p_buffer[6] << 8) +
471                                 p->p_buffer[7] );
472                 i_seq_prev = ( (p->p_buffer[2] << 8 ) +
473                                 p->p_buffer[3] );
474
475                 /* Parse headerfields we need */
476                 i_CSRC_count = p->p_buffer[0] & 0x0F;
477                 i_payload_type = (p->p_buffer[1] & 0x7F);
478                 i_extension_bit  = ( p->p_buffer[0] & 0x10 ) >> 4;
479                 if ( i_extension_bit == 1)
480                     i_extension_length = ( (p->p_buffer[14] << 8 ) +
481                                             p->p_buffer[15] );
482
483                 /* Skip header + CSRC extension field n*(32 bits) + extention */
484                 i_skip = RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
485                 if( i_payload_type == 14 ) i_skip += 4;
486
487                 /* Return the packet without the RTP header. */
488                 p->i_buffer -= i_skip;
489                 p->p_buffer += i_skip;
490             }
491             else if( (i_cur - i_seq) > 0)
492             {
493                 if( p_prev )
494                 {
495                     *pp_list = p;
496                     p_prev->p_next = NULL;
497                     p_sys->i_last_pcr = i_pcr_prev;
498                     p_sys->i_sequence_number = i_seq_prev;
499                     return p_send;
500                 }
501                 /* FiXME: or should we return NULL here? */
502                 return NULL;
503             }
504             p_prev = p;
505             if (!p->p_next) break;
506             p = p->p_next;
507         }
508         /* We have walked through the complete chain and all packets are
509          * in sequence - so send the whole chain
510          */
511         i_payload_type = (p->p_buffer[1] & 0x7F);
512         i_CSRC_count = p->p_buffer[0] & 0x0F;
513         i_extension_bit  = ( p->p_buffer[0] & 0x10 ) >> 4;
514         if( i_extension_bit == 1)
515             i_extension_length = ( (p->p_buffer[14] << 8 ) + p->p_buffer[15] );
516
517         /* Skip header + CSRC extension field n*(32 bits) + extention */
518         i_skip = RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
519         if( i_payload_type == 14 ) i_skip += 4;
520
521         /* Update the list pointers */
522         *pp_list = NULL;
523         p_sys->p_next = NULL;
524         p_sys->p_end = NULL;
525         p_sys->i_sequence_number = ( (p->p_buffer[2] << 8 ) +
526                                       p->p_buffer[3] );
527         p_sys->i_last_pcr = ( (p->p_buffer[4] << 24) +
528                               (p->p_buffer[5] << 16) +
529                               (p->p_buffer[6] << 8) +
530                                p->p_buffer[7] );
531         /* Return the packet without the RTP header. */
532         p->i_buffer -= i_skip;
533         p->p_buffer += i_skip;
534         return p_send;
535     }
536     return NULL;
537 }
538
539 /*****************************************************************************
540  * BlockParseRTP/BlockRTP:
541  *****************************************************************************/
542 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
543 {
544     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
545     int      i_rtp_version;
546     int      i_CSRC_count;
547     int      i_payload_type;
548     int      i_skip = 0;
549     uint16_t i_sequence_number = 0;
550     uint16_t i_sequence_expected = 0;
551     int      i_extension_bit = 0;
552     int      i_extension_length = 0;
553     uint32_t i_pcr = 0;
554
555     if( p_block == NULL )
556         return NULL;
557
558     if( p_block->i_buffer < RTP_HEADER_LEN )
559     {
560         msg_Warn( p_access, "received a too short packet for RTP" );
561         block_Release( p_block );
562         return NULL;
563     }
564     /* Parse the header and make some verifications.
565      * See RFC 3550. */
566     i_rtp_version     = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
567     i_CSRC_count      = p_block->p_buffer[0] & 0x0F;
568     i_payload_type    = p_block->p_buffer[1] & 0x7F;
569     i_sequence_number = (p_block->p_buffer[2] << 8) +
570                          p_block->p_buffer[3];
571     i_pcr = ( (p_block->p_buffer[4] << 24) +
572               (p_block->p_buffer[5] << 16) +
573               (p_block->p_buffer[6] << 8) +
574                p_block->p_buffer[7] );
575     i_extension_bit  = ( p_block->p_buffer[0] & 0x10 ) >> 4;
576
577     if( i_rtp_version != 2 )
578         msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
579
580     if( i_payload_type == 14 )
581         i_skip = 4;
582     else if( i_payload_type !=  33 && i_payload_type != 32 )
583         msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
584
585     if( i_extension_bit == 1)
586         i_extension_length = 4 +
587             4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
588
589     /* Skip header + CSRC extension field n*(32 bits) + extention */
590     i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
591     if( i_skip >= p_block->i_buffer )
592     {
593         msg_Warn( p_access, "received a too short packet for RTP" );
594         block_Release( p_block );
595         return NULL;
596     }
597
598     /* Detect RTP packet loss through tracking sequence numbers,
599      * and take RTP PCR into account.
600      * See RFC 3550.
601      */
602     if( p_sys->b_first_seqno )
603     {
604         p_sys->i_sequence_number = i_sequence_number-1;
605         p_sys->i_last_pcr = i_pcr;
606         p_sys->b_first_seqno = VLC_FALSE;
607     }
608 #if 0
609     /* Emulate packet loss */
610     if ( (i_sequence_number % 4000) == 0)
611     {
612         msg_Warn( p_access, "Emulating packet drop" );
613         block_Release( p_block );
614         return NULL;
615     }
616 #endif
617     i_sequence_expected = ((p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE);
618     if( (i_sequence_expected - i_sequence_number) != 0 )
619     {
620         /* Handle out of order packets */
621         if( p_sys->i_rtp_late > 0 )
622         {
623             if( (i_sequence_number - i_sequence_expected) > 0 )
624             {
625                 msg_Warn( p_access,
626                     "RTP packet out of order (too early) expected %u, current %u",
627                     i_sequence_expected, i_sequence_number );
628                 if( (i_pcr - p_sys->i_last_pcr) > (p_sys->i_rtp_late*90) )
629                 {
630                     block_t *p_start = p_sys->p_list;
631                     uint16_t i_start = (p_start->p_buffer[2] << 8) +
632                                         p_start->p_buffer[3];
633                     /* Gap too big, we have been holding this data for too long,
634                      * send what we have.
635                      */
636                     msg_Warn( p_access,
637                         "Gap too big resyncing: delta %u, held for %d ms",
638                         (i_pcr - p_sys->i_last_pcr), p_sys->i_rtp_late );
639                     rtp_ChainInsert( p_access, &p_sys->p_list, &p_sys->p_end, p_block );
640                     return rtp_ChainSend( p_access, &p_sys->p_list, i_start );
641                 }
642                 /* hold packets that arrive too early. */
643                 rtp_ChainInsert( p_access, &p_sys->p_list, &p_sys->p_end, p_block );
644                 return rtp_ChainSend( p_access, &p_sys->p_list, i_sequence_expected );
645             }
646             else if( /* ((i_sequence_expected - i_sequence_number ) > 0) && */
647                      (i_pcr <= p_sys->i_last_pcr) )
648             {
649                 msg_Warn( p_access,
650                     "RTP packet out of order (duplicate or too late) expected %u, current %u .. trashing it",
651                     i_sequence_expected, i_sequence_number );
652                 block_Release( p_block );
653                 p_sys->i_sequence_number = i_sequence_number;
654                 p_sys->i_last_pcr = i_pcr;
655                 return NULL;
656             }
657
658             if( p_sys->p_list )
659             {
660                 block_t *p = NULL;
661                 block_t **p_send = &p_sys->p_list;
662
663                 msg_Warn( p_access,
664                     "RTP packet (unexpected condition) expected %u, current %u",
665                     i_sequence_expected, i_sequence_number );
666
667                 /* Append block to the end of chain and send whole chain */
668                 block_ChainLastAppend( &p_send, p_block );
669                 p_sys->p_list = p_sys->p_end = NULL;
670                 p_sys->i_sequence_number = i_sequence_number;
671                 p_sys->i_last_pcr = i_pcr;
672
673                 /* Return the packet without the RTP header. */
674                 p = *p_send;
675                 while( p )
676                 {
677                     p->i_buffer -= i_skip;
678                     p->p_buffer += i_skip;
679                     if( !p->p_next ) break;
680                     p = p->p_next;
681                 }
682                 return *p_send;
683             }
684             /* This code should never be reached !! */
685             msg_Err( p_access,
686                 "Bug in algorithme: (unexpected condition) expected %u (pcr=%u), current %u (pcr=%u)",
687                 i_sequence_expected, i_sequence_number, p_sys->i_last_pcr, i_pcr );
688         }
689         msg_Warn( p_access,
690                   "RTP packet(s) lost, expected sequence number %d got %d",
691                   i_sequence_expected, i_sequence_number );
692
693         /* Mark transport error in the first TS packet in the RTP stream. */
694         if( (i_payload_type == 33) && (p_block->p_buffer[0] == 0x47) )
695             p_block->p_buffer[1] |= 0x80;
696     }
697     else if( (p_sys->i_rtp_late > 0) && p_sys->p_list )
698     {
699         if( i_pcr <= p_sys->i_last_pcr )
700         {
701             msg_Warn( p_access,
702                 "RTP packet out of order (duplicate) expected %u, current %u .. trashing it",
703                 i_sequence_expected, i_sequence_number );
704             block_Release( p_block );
705             p_sys->i_sequence_number = i_sequence_number;
706             p_sys->i_last_pcr = i_pcr;
707             return NULL;
708         }
709         rtp_ChainInsert( p_access, &p_sys->p_list, &p_sys->p_end, p_block );
710         return rtp_ChainSend( p_access, &p_sys->p_list, i_sequence_expected );
711     }
712
713     /* This is the normal case when no packet reordering is effective */
714     p_sys->i_sequence_number = i_sequence_number;
715     p_sys->i_last_pcr = i_pcr;
716
717     /* Return the packet without the RTP header. */
718     p_block->i_buffer -= i_skip;
719     p_block->p_buffer += i_skip;
720     return p_block;
721 }
722
723 static block_t *BlockRTP( access_t *p_access )
724 {
725     block_t *p_block = BlockUDP( p_access );
726
727     if ( p_block != NULL )
728         return BlockParseRTP( p_access, p_block );
729     else
730         return NULL;
731 }
732
733 /*****************************************************************************
734  * BlockChoose: decide between RTP and UDP
735  *****************************************************************************/
736 static block_t *BlockChoose( access_t *p_access )
737 {
738     block_t *p_block;
739     int     i_rtp_version;
740     int     i_CSRC_count;
741     int     i_payload_type;
742
743     if( ( p_block = BlockUDP( p_access ) ) == NULL )
744         return NULL;
745
746     if( p_block->p_buffer[0] == 0x47 )
747     {
748         msg_Dbg( p_access, "detected TS over raw UDP" );
749         p_access->pf_block = BlockUDP;
750         return p_block;
751     }
752
753     if( p_block->i_buffer < RTP_HEADER_LEN )
754         return p_block;
755
756     /* Parse the header and make some verifications.
757      * See RFC 3550. */
758
759     i_rtp_version  = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
760     i_CSRC_count   = ( p_block->p_buffer[0] & 0x0F );
761     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
762
763     if( i_rtp_version != 2 )
764     {
765         msg_Dbg( p_access, "no supported RTP header detected" );
766         p_access->pf_block = BlockUDP;
767         return p_block;
768     }
769
770     switch( i_payload_type )
771     {
772         case 33:
773             msg_Dbg( p_access, "detected TS over RTP" );
774             p_access->psz_demux = strdup( "ts" );
775             break;
776
777         case 14:
778             msg_Dbg( p_access, "detected MPEG audio over RTP" );
779             p_access->psz_demux = strdup( "mpga" );
780             break;
781
782         case 32:
783             msg_Dbg( p_access, "detected MPEG video over RTP" );
784             p_access->psz_demux = strdup( "mpgv" );
785             break;
786
787         default:
788             msg_Dbg( p_access, "no RTP header detected" );
789             p_access->pf_block = BlockUDP;
790             return p_block;
791     }
792
793     p_access->pf_block = BlockRTP;
794
795     return BlockParseRTP( p_access, p_block );
796 }