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