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