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