]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Increase MTU too lowest legal MTU when it is really too small
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP & RTP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * Copyright (C) 2007 Remi Denis-Courmont
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Tristan Leteurtre <tooney@via.ecp.fr>
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12  *          Remi Denis-Courmont
13  *
14  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #include <vlc/vlc.h>
36 #include <vlc_access.h>
37 #include <vlc_network.h>
38
39 /* Big pile of stuff missing form glibc 2.5 */
40 #if defined (HAVE_NETINET_UDPLITE_H)
41 # include <netinet/udplite.h>
42 #elif defined (__linux__)
43 # define UDPLITE_SEND_CSCOV     10
44 # define UDPLITE_RECV_CSCOV     11
45 #endif
46
47 #ifndef SOCK_DCCP /* provisional API */
48 # ifdef __linux__
49 #  define SOCK_DCCP 6
50 # endif
51 #endif
52
53 #ifndef IPPROTO_DCCP
54 # define IPPROTO_DCCP 33 /* IANA */
55 #endif
56
57 #ifndef IPPROTO_UDPLITE
58 # define IPPROTO_UDPLITE 136 /* from IANA */
59 #endif
60 #ifndef SOL_UDPLITE
61 # define SOL_UDPLITE IPPROTO_UDPLITE
62 #endif
63
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 #define CACHING_TEXT N_("Caching value in ms")
69 #define CACHING_LONGTEXT N_( \
70     "Caching value for UDP streams. This " \
71     "value should be set in milliseconds." )
72
73 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
74 #define AUTO_MTU_LONGTEXT N_( \
75     "Automatically detect the line's MTU. This will increase the size if" \
76     " truncated packets are found" )
77
78 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
79 #define RTP_LATE_LONGTEXT N_( \
80     "VLC reorders RTP packets. The input will wait for late packets at most "\
81     "the time specified here (in milliseconds)." )
82
83 static int  Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
85
86 vlc_module_begin();
87     set_shortname( _("UDP/RTP" ) );
88     set_description( _("UDP/RTP input") );
89     set_category( CAT_INPUT );
90     set_subcategory( SUBCAT_INPUT_ACCESS );
91
92     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
93                  CACHING_LONGTEXT, VLC_TRUE );
94     add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
95
96     add_bool( "udp-auto-mtu", 1, NULL,
97               AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
98
99     set_capability( "access2", 0 );
100     add_shortcut( "udp" );
101     add_shortcut( "udpstream" );
102     add_shortcut( "udp4" );
103     add_shortcut( "udp6" );
104     add_shortcut( "rtp" );
105     add_shortcut( "rtp4" );
106     add_shortcut( "rtp6" );
107     add_shortcut( "udplite" );
108     add_shortcut( "rtptcp" );
109     add_shortcut( "dccp" );
110
111     set_callbacks( Open, Close );
112 vlc_module_end();
113
114 /*****************************************************************************
115  * Local prototypes
116  *****************************************************************************/
117 #define RTP_HEADER_LEN 12
118
119 static block_t *BlockUDP( access_t * );
120 static block_t *BlockTCP( access_t * );
121 static block_t *BlockRTP( access_t * );
122 static block_t *BlockChoose( access_t * );
123 static int Control( access_t *, int, va_list );
124
125 struct access_sys_t
126 {
127     int fd;
128
129     int i_mtu;
130     vlc_bool_t b_auto_mtu;
131     vlc_bool_t b_framed_rtp;
132
133     /* reorder rtp packets when out-of-sequence */
134     uint16_t i_last_seqno;
135     mtime_t i_rtp_late;
136     block_t *p_list;
137     block_t *p_end;
138     block_t *p_partial_frame; /* Partial Framed RTP packet */
139 };
140
141 /*****************************************************************************
142  * Open: open the socket
143  *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
145 {
146     access_t     *p_access = (access_t*)p_this;
147     access_sys_t *p_sys;
148
149     char *psz_name = strdup( p_access->psz_path );
150     char *psz_parser;
151     const char *psz_server_addr, *psz_bind_addr = "";
152     int  i_bind_port, i_server_port = 0;
153     int fam = AF_UNSPEC, proto = IPPROTO_UDP;
154
155     if (strlen (p_access->psz_access) >= 3)
156     {
157         switch (p_access->psz_access[3])
158         {
159             case '4':
160                 fam = AF_INET;
161                 break;
162
163             case '6':
164                 fam = AF_INET6;
165                 break;
166         }
167         if (strcmp (p_access->psz_access + 3, "lite") == 0)
168             proto = IPPROTO_UDPLITE;
169         else
170         if (strcmp (p_access->psz_access + 3, "tcp") == 0)
171             proto = IPPROTO_TCP;
172         else
173         if (strcmp (p_access->psz_access, "dccp") == 0)
174             proto = IPPROTO_DCCP;
175     }
176
177     i_bind_port = var_CreateGetInteger( p_access, "server-port" );
178
179     /* Parse psz_name syntax :
180      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
181     psz_parser = strchr( psz_name, '@' );
182     if( psz_parser != NULL )
183     {
184         /* Found bind address and/or bind port */
185         *psz_parser++ = '\0';
186         psz_bind_addr = psz_parser;
187
188         if( psz_bind_addr[0] == '[' )
189             /* skips bracket'd IPv6 address */
190             psz_parser = strchr( psz_parser, ']' );
191
192         if( psz_parser != NULL )
193         {
194             psz_parser = strchr( psz_parser, ':' );
195             if( psz_parser != NULL )
196             {
197                 *psz_parser++ = '\0';
198                 i_bind_port = atoi( psz_parser );
199             }
200         }
201     }
202
203     psz_server_addr = psz_name;
204     psz_parser = ( psz_server_addr[0] == '[' )
205         ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
206         : psz_name;
207
208     if( psz_parser != NULL )
209     {
210         psz_parser = strchr( psz_parser, ':' );
211         if( psz_parser != NULL )
212         {
213             *psz_parser++ = '\0';
214             i_server_port = atoi( psz_parser );
215         }
216     }
217
218     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
219              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
220
221     /* Set up p_access */
222     access_InitFields( p_access );
223     ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
224     p_access->info.b_prebuffered = VLC_FALSE;
225     MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
226     memset (p_sys, 0, sizeof (*p_sys));
227
228     switch (proto)
229     {
230         case IPPROTO_UDP:
231         case IPPROTO_UDPLITE:
232             p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
233                                        psz_server_addr, i_server_port, fam,
234                                        proto );
235             break;
236
237         case IPPROTO_TCP:
238             p_sys->fd = net_ConnectTCP( p_access, psz_server_addr, i_server_port );
239             p_sys->b_framed_rtp = VLC_TRUE;
240             break;
241
242         case IPPROTO_DCCP:
243 #ifdef SOCK_DCCP
244             p_sys->fd = net_Connect( p_access, psz_server_addr, i_server_port,
245                                      SOCK_DCCP, IPPROTO_DCCP );
246 #else
247             p_sys->fd = -1;
248             msg_Err( p_access, "DCCP support not compiled-in!" );
249 #endif
250             break;
251     }
252     free (psz_name);
253     if( p_sys->fd == -1 )
254     {
255         msg_Err( p_access, "cannot open socket" );
256         free( p_sys );
257         return VLC_EGENERIC;
258     }
259
260     shutdown( p_sys->fd, SHUT_WR );
261
262 #ifdef UDPLITE_RECV_CSCOV
263     if (proto == IPPROTO_UDPLITE)
264         /* UDP header: 8 bytes + RTP header: 12 bytes (or more) */
265         setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
266                     &(int){ 20 }, sizeof (int));
267 #endif
268
269     if (p_sys->b_framed_rtp)
270     {
271         /* We don't do autodetection and prebuffering in case of framing */
272         p_access->pf_block = BlockRTP;
273         p_sys->i_mtu = 65535;
274     }
275     else
276     {
277         /* FIXME */
278         p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
279         if( p_sys->i_mtu <= 1 )
280             p_sys->i_mtu  = 576 - 20 - 8;   /* Avoid problem */
281
282         p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
283     }
284
285     /* Update default_pts to a suitable value for udp access */
286     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
287
288     /* RTP reordering for out-of-sequence packets */
289     p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
290     p_sys->i_last_seqno = 0;
291     p_sys->p_list = NULL;
292     p_sys->p_end = NULL;
293     return VLC_SUCCESS;
294 }
295
296 /*****************************************************************************
297  * Close: free unused data structures
298  *****************************************************************************/
299 static void Close( vlc_object_t *p_this )
300 {
301     access_t     *p_access = (access_t*)p_this;
302     access_sys_t *p_sys = p_access->p_sys;
303
304     block_ChainRelease( p_sys->p_list );
305     net_Close( p_sys->fd );
306     free( p_sys );
307 }
308
309 /*****************************************************************************
310  * Control:
311  *****************************************************************************/
312 static int Control( access_t *p_access, int i_query, va_list args )
313 {
314     access_sys_t *p_sys = p_access->p_sys;
315     vlc_bool_t   *pb_bool;
316     int          *pi_int;
317     int64_t      *pi_64;
318
319     switch( i_query )
320     {
321         /* */
322         case ACCESS_CAN_SEEK:
323         case ACCESS_CAN_FASTSEEK:
324         case ACCESS_CAN_PAUSE:
325         case ACCESS_CAN_CONTROL_PACE:
326             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
327             *pb_bool = VLC_FALSE;
328             break;
329         /* */
330         case ACCESS_GET_MTU:
331             pi_int = (int*)va_arg( args, int * );
332             *pi_int = p_sys->i_mtu;
333             break;
334
335         case ACCESS_GET_PTS_DELAY:
336             pi_64 = (int64_t*)va_arg( args, int64_t * );
337             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
338             break;
339
340         /* */
341         case ACCESS_SET_PAUSE_STATE:
342         case ACCESS_GET_TITLE_INFO:
343         case ACCESS_SET_TITLE:
344         case ACCESS_SET_SEEKPOINT:
345         case ACCESS_SET_PRIVATE_ID_STATE:
346             return VLC_EGENERIC;
347
348         default:
349             msg_Warn( p_access, "unimplemented query in control" );
350             return VLC_EGENERIC;
351
352     }
353     return VLC_SUCCESS;
354 }
355
356 /*****************************************************************************
357  * BlockUDP:
358  *****************************************************************************/
359 static block_t *BlockUDP( access_t *p_access )
360 {
361     access_sys_t *p_sys = p_access->p_sys;
362     block_t      *p_block;
363
364     /* Read data */
365     p_block = block_New( p_access, p_sys->i_mtu );
366     p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
367                                   p_block->p_buffer, p_sys->i_mtu,
368                                   VLC_FALSE );
369     if( p_block->i_buffer < 0 )
370     {
371         block_Release( p_block );
372         return NULL;
373     }
374
375     if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
376         p_sys->i_mtu < 32767 )
377     {
378         /* Increase by 100% */
379         p_sys->i_mtu *= 2;
380         msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
381     }
382
383     return p_block;
384 }
385
386 /*****************************************************************************
387  * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
388  *****************************************************************************/
389 static block_t *BlockTCP( access_t *p_access )
390 {
391     access_sys_t *p_sys = p_access->p_sys;
392     block_t      *p_block = p_sys->p_partial_frame;
393
394     if( p_access->info.b_eof )
395         return NULL;
396
397     if( p_block == NULL )
398     {
399         /* MTU should always be 65535 in this case */
400         p_sys->p_partial_frame = p_block = block_New( p_access, 65537 );
401         if (p_block == NULL)
402             return NULL;
403     }
404
405     /* Read RTP framing */
406     if (p_block->i_buffer < 2)
407     {
408         /* FIXME: not very efficient */
409         int i_read = net_Read( p_access, p_sys->fd, NULL,
410                                p_block->p_buffer + p_block->i_buffer,
411                                2 - p_block->i_buffer, VLC_FALSE );
412         if( i_read <= 0 )
413             goto error;
414
415         p_block->i_buffer += i_read;
416         if (p_block->i_buffer < 2)
417             return NULL;
418     }
419
420     uint16_t framelen = GetWLE( p_block->p_buffer );
421     /* Read RTP frame */
422     if( framelen > 0 )
423     {
424         int i_read = net_Read( p_access, p_sys->fd, NULL,
425                                p_block->p_buffer + p_block->i_buffer,
426                                2 + framelen - p_block->i_buffer, VLC_FALSE );
427         if( i_read <= 0 )
428             goto error;
429
430         p_block->i_buffer += i_read;
431     }
432
433     if( p_block->i_buffer < (2 + framelen) )
434         return NULL; // incomplete frame
435
436     /* Hide framing from RTP layer */
437     p_block->p_buffer += 2;
438     p_block->i_buffer -= 2;
439     p_sys->p_partial_frame = NULL;
440     return p_block;
441
442 error:
443     p_access->info.b_eof = VLC_TRUE;
444     block_Release( p_block );
445     p_sys->p_partial_frame = NULL;
446     return NULL;
447 }
448
449
450 /*
451  * rtp_ChainInsert - insert a p_block in the chain and
452  * look at the sequence numbers.
453  */
454 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
455 {
456     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
457     block_t *p_prev = NULL;
458     block_t *p = p_sys->p_end;
459     uint16_t i_new = (uint16_t) p_block->i_dts;
460     uint16_t i_tmp = 0;
461
462     if( !p_sys->p_list )
463     {
464         p_sys->p_list = p_block;
465         p_sys->p_end = p_block;
466         return VLC_TRUE;
467     }
468     /* walk through the queue from top down since the new packet is in 
469     most cases just appended to the end */
470
471     for( ;; )
472     {
473         i_tmp = i_new - (uint16_t) p->i_dts;
474
475         if( !i_tmp )   /* trash duplicate */
476             break;
477
478         if ( i_tmp < 32768 )
479         {   /* insert after this block ( i_new > p->i_dts ) */
480             p_block->p_next = p->p_next;
481             p->p_next = p_block;
482             p_block->p_prev = p;
483             if (p_prev)
484             {
485                 p_prev->p_prev = p_block;
486                 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
487                         (uint16_t) p->i_dts, i_new );
488             }
489             else
490             {
491                 p_sys->p_end = p_block;
492             }
493             return VLC_TRUE;
494         }
495         if( p == p_sys->p_list )
496         {   /* we've reached bottom of chain */
497             i_tmp = p_sys->i_last_seqno - i_new;
498             if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
499             {
500                 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
501                         i_new, (uint16_t) p->i_dts );
502                 p_block->p_next = p;
503                 p->p_prev = p_block;
504                 p_sys->p_list = p_block;
505                 return VLC_TRUE;
506             }
507
508             if( !i_tmp )   /* trash duplicate */
509                 break;
510
511             /* reordering failed - append the packet to the end of queue */
512             msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
513                     "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
514                 (uint16_t) p_sys->p_end->i_dts);
515             p_sys->p_end->p_next = p_block;
516             p_block->p_prev = p_sys->p_end;
517             p_sys->p_end = p_block;
518             return VLC_TRUE;
519         }
520         p_prev = p;
521         p = p->p_prev;
522     }
523     block_Release( p_block );
524     return VLC_FALSE;
525 }
526
527 /*****************************************************************************
528  * BlockParseRTP/BlockRTP:
529  *****************************************************************************/
530 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
531 {
532     int      i_payload_type;
533     size_t   i_skip = RTP_HEADER_LEN;
534
535     if( p_block == NULL )
536         return NULL;
537
538     if( p_block->i_buffer < RTP_HEADER_LEN )
539     {
540         msg_Dbg( p_access, "short RTP packet received" );
541         goto trash;
542     }
543
544     /* Parse the header and make some verifications.
545      * See RFC 3550. */
546     // Version number:
547     if( ( p_block->p_buffer[0] >> 6 ) != 2)
548     {
549         msg_Dbg( p_access, "RTP version is %u instead of 2",
550                  p_block->p_buffer[0] >> 6 );
551         goto trash;
552     }
553     // Padding bit:
554     uint8_t pad = (p_block->p_buffer[0] & 0x20)
555                     ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
556     // CSRC count:
557     i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
558     // Extension header:
559     if (p_block->p_buffer[0] & 0x10) /* Extension header */
560     {
561         i_skip += 4;
562         if ((size_t)p_block->i_buffer < i_skip)
563             goto trash;
564
565         i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
566     }
567
568     i_payload_type    = p_block->p_buffer[1] & 0x7F;
569
570     /* Remember sequence number in i_dts */
571     p_block->i_pts = mdate();
572     p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 );
573
574     /* FIXME: use rtpmap */
575     switch( i_payload_type )
576     {
577         case 14: // MPA: MPEG Audio (RFC2250, §3.4)
578             i_skip += 4; // 32 bits RTP/MPA header
579             break;
580
581         case 32: // MPV: MPEG Video (RFC2250, §3.5)
582             i_skip += 4; // 32 bits RTP/MPV header
583             if( (size_t)p_block->i_buffer < i_skip )
584                 goto trash;
585             if( p_block->p_buffer[i_skip - 3] & 0x4 )
586             {
587                 /* MPEG2 Video extension header */
588                 /* TODO: shouldn't we skip this too ? */
589             }
590             break;
591
592         case 33: // MP2: MPEG TS (RFC2250, §2)
593             /* plain TS over RTP */
594             break;
595
596         default:
597             msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
598             goto trash;
599     }
600
601     if( (size_t)p_block->i_buffer < (i_skip + pad) )
602         goto trash;
603
604     /* Remove the RTP header */
605     p_block->i_buffer -= i_skip;
606     p_block->p_buffer += i_skip;
607
608     /* This is the place for deciphering and authentication */
609
610     /* Remove padding (at the end) */
611     p_block->i_buffer -= pad;
612
613 #if 0
614     /* Emulate packet loss */
615     if ( (i_sequence_number % 4000) == 0)
616     {
617         msg_Warn( p_access, "Emulating packet drop" );
618         block_Release( p_block );
619         return NULL;
620     }
621 #endif
622
623     return p_block;
624
625 trash:
626     block_Release( p_block );
627     return NULL;
628 }
629
630 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
631 {
632     access_sys_t *p_sys = p_access->p_sys;
633     mtime_t   i_first = mdate();
634     int       i_count = 0;
635     block_t   *p = p_block;
636
637     for( ;; )
638     {
639         mtime_t i_date = mdate();
640
641         if( p && rtp_ChainInsert( p_access, p ))
642             i_count++;
643
644         /* Require at least 2 packets in the buffer */
645         if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
646             break;
647
648         p = BlockParseRTP( p_access, BlockUDP( p_access ) );
649         if( !p && (i_date - i_first) > p_sys->i_rtp_late ) 
650         {
651             msg_Err( p_access, "error in RTP prebuffering!" );
652             break;
653         }
654     }
655
656     msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
657     p_access->info.b_prebuffered = VLC_TRUE;
658     p = p_sys->p_list;
659     p_sys->p_list = p_sys->p_list->p_next;
660     p_sys->i_last_seqno = (uint16_t) p->i_dts;
661     p->p_next = NULL;
662     return p;
663 }
664
665 static block_t *BlockRTP( access_t *p_access )
666 {
667     access_sys_t *p_sys = p_access->p_sys;
668     block_t *p;
669
670     while ( !p_sys->p_list ||
671              ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
672     {
673         p = BlockParseRTP( p_access,
674                            p_sys->b_framed_rtp ? BlockTCP( p_access )
675                                                : BlockUDP( p_access ) );
676         if ( !p )
677             return NULL;
678
679         rtp_ChainInsert( p_access, p );
680     }
681
682     p = p_sys->p_list;
683     p_sys->p_list = p_sys->p_list->p_next;
684     p_sys->i_last_seqno++;
685     if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
686     {
687         msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
688                  p_sys->i_last_seqno, (uint16_t) p->i_dts );
689         p_sys->i_last_seqno = (uint16_t) p->i_dts;
690     }
691     p->p_next = NULL;
692     return p;
693 }
694
695 /*****************************************************************************
696  * BlockChoose: decide between RTP and UDP
697  *****************************************************************************/
698 static block_t *BlockChoose( access_t *p_access )
699 {
700     block_t *p_block;
701     int     i_rtp_version;
702     int     i_payload_type;
703
704     if( ( p_block = BlockUDP( p_access ) ) == NULL )
705         return NULL;
706
707     if( p_block->p_buffer[0] == 0x47 )
708     {
709         msg_Dbg( p_access, "detected TS over raw UDP" );
710         p_access->pf_block = BlockUDP;
711         p_access->info.b_prebuffered = VLC_TRUE;
712         return p_block;
713     }
714
715     if( p_block->i_buffer < RTP_HEADER_LEN )
716         return p_block;
717
718     /* Parse the header and make some verifications.
719      * See RFC 3550. */
720
721     i_rtp_version  = p_block->p_buffer[0] >> 6;
722     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
723
724     if( i_rtp_version != 2 )
725     {
726         msg_Dbg( p_access, "no supported RTP header detected" );
727         p_access->pf_block = BlockUDP;
728         p_access->info.b_prebuffered = VLC_TRUE;
729         return p_block;
730     }
731
732     switch( i_payload_type )
733     {
734         case 33:
735             msg_Dbg( p_access, "detected MPEG2 TS over RTP" );
736             p_access->psz_demux = strdup( "ts" );
737             break;
738
739         case 14:
740             msg_Dbg( p_access, "detected MPEG Audio over RTP" );
741             p_access->psz_demux = strdup( "mpga" );
742             break;
743
744         case 32:
745             msg_Dbg( p_access, "detected MPEG Video over RTP" );
746             p_access->psz_demux = strdup( "mpgv" );
747             break;
748
749         default:
750             msg_Dbg( p_access, "no RTP header detected" );
751             p_access->pf_block = BlockUDP;
752             p_access->info.b_prebuffered = VLC_TRUE;
753             return p_block;
754     }
755
756     if( !BlockParseRTP( p_access, p_block )) return NULL;
757
758     p_access->pf_block = BlockRTP;
759
760     return BlockPrebufferRTP( p_access, p_block );
761 }