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