]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Replace vlc_bool_t by bool, VLC_TRUE by true and VLC_FALSE by false.
[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, true );
83     add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, 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     bool 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 = 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 = true;
231             break;
232
233         case IPPROTO_DCCP:
234 #ifdef SOCK_DCCP
235             var_Create( p_access, "dccp-service", VLC_VAR_STRING );
236             var_SetString( p_access, "dccp-service", "RTPV" );
237             p_sys->fd = net_Connect( p_access, psz_server_addr, i_server_port,
238                                      SOCK_DCCP, IPPROTO_DCCP );
239 #else
240             p_sys->fd = -1;
241             msg_Err( p_access, "DCCP support not compiled-in!" );
242 #endif
243             p_sys->b_comedia = true;
244             break;
245     }
246     free (psz_name);
247     if( p_sys->fd == -1 )
248     {
249         msg_Err( p_access, "cannot open socket" );
250         free( p_sys );
251         return VLC_EGENERIC;
252     }
253
254     shutdown( p_sys->fd, SHUT_WR );
255     net_SetCSCov (p_sys->fd, -1, 12);
256
257     /* Update default_pts to a suitable value for udp access */
258     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
259
260     /* RTP reordering for out-of-sequence packets */
261     p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
262     p_sys->i_last_seqno = 0;
263     p_sys->p_list = NULL;
264     p_sys->p_end = NULL;
265     return VLC_SUCCESS;
266 }
267
268 /*****************************************************************************
269  * Close: free unused data structures
270  *****************************************************************************/
271 static void Close( vlc_object_t *p_this )
272 {
273     access_t     *p_access = (access_t*)p_this;
274     access_sys_t *p_sys = p_access->p_sys;
275
276     block_ChainRelease( p_sys->p_list );
277     net_Close( p_sys->fd );
278     free( p_sys );
279 }
280
281 /*****************************************************************************
282  * Control:
283  *****************************************************************************/
284 static int Control( access_t *p_access, int i_query, va_list args )
285 {
286     bool   *pb_bool;
287     int          *pi_int;
288     int64_t      *pi_64;
289
290     switch( i_query )
291     {
292         /* */
293         case ACCESS_CAN_SEEK:
294         case ACCESS_CAN_FASTSEEK:
295         case ACCESS_CAN_PAUSE:
296         case ACCESS_CAN_CONTROL_PACE:
297             pb_bool = (bool*)va_arg( args, bool* );
298             *pb_bool = false;
299             break;
300         /* */
301         case ACCESS_GET_MTU:
302             pi_int = (int*)va_arg( args, int * );
303             *pi_int = MTU;
304             break;
305
306         case ACCESS_GET_PTS_DELAY:
307             pi_64 = (int64_t*)va_arg( args, int64_t * );
308             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
309             break;
310
311         /* */
312         case ACCESS_SET_PAUSE_STATE:
313         case ACCESS_GET_TITLE_INFO:
314         case ACCESS_SET_TITLE:
315         case ACCESS_SET_SEEKPOINT:
316         case ACCESS_SET_PRIVATE_ID_STATE:
317         case ACCESS_GET_CONTENT_TYPE:
318             return VLC_EGENERIC;
319
320         default:
321             msg_Warn( p_access, "unimplemented query in control" );
322             return VLC_EGENERIC;
323
324     }
325     return VLC_SUCCESS;
326 }
327
328 /*****************************************************************************
329  * BlockUDP:
330  *****************************************************************************/
331 static block_t *BlockUDP( access_t *p_access )
332 {
333     access_sys_t *p_sys = p_access->p_sys;
334     block_t      *p_block;
335     ssize_t len;
336
337     if( p_access->info.b_eof )
338         return NULL;
339
340     /* Read data */
341     p_block = block_New( p_access, MTU );
342     len = net_Read( p_access, p_sys->fd, NULL,
343                     p_block->p_buffer, MTU, false );
344     if( ( len < 0 )
345      || ( p_sys->b_comedia && ( len == 0 ) ) )
346     {
347         if( p_sys->b_comedia )
348         {
349             p_access->info.b_eof = true;
350             msg_Dbg( p_access, "connection-oriented media hangup" );
351         }
352         block_Release( p_block );
353         return NULL;
354     }
355
356     return block_Realloc( p_block, 0, p_block->i_buffer = len );
357 }
358
359 /*****************************************************************************
360  * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
361  *****************************************************************************/
362 static block_t *BlockTCP( access_t *p_access )
363 {
364     access_sys_t *p_sys = p_access->p_sys;
365     block_t      *p_block = p_sys->p_partial_frame;
366
367     if( p_access->info.b_eof )
368         return NULL;
369
370     if( p_block == NULL )
371     {
372         /* MTU should always be 65535 in this case */
373         p_sys->p_partial_frame = p_block = block_New( p_access, 2 + MTU );
374         if (p_block == NULL)
375             return NULL;
376     }
377
378     /* Read RTP framing */
379     if (p_block->i_buffer < 2)
380     {
381         int i_read = net_Read( p_access, p_sys->fd, NULL,
382                                p_block->p_buffer + p_block->i_buffer,
383                                2 - p_block->i_buffer, false );
384         if( i_read <= 0 )
385             goto error;
386
387         p_block->i_buffer += i_read;
388         if (p_block->i_buffer < 2)
389             return NULL;
390     }
391
392     uint16_t framelen = GetWLE( p_block->p_buffer );
393     /* Read RTP frame */
394     if( framelen > 0 )
395     {
396         int i_read = net_Read( p_access, p_sys->fd, NULL,
397                                p_block->p_buffer + p_block->i_buffer,
398                                2 + framelen - p_block->i_buffer, false );
399         if( i_read <= 0 )
400             goto error;
401
402         p_block->i_buffer += i_read;
403     }
404
405     if( p_block->i_buffer < (2u + framelen) )
406         return NULL; // incomplete frame
407
408     /* Hide framing from RTP layer */
409     p_block->p_buffer += 2;
410     p_block->i_buffer -= 2;
411     p_sys->p_partial_frame = NULL;
412     return p_block;
413
414 error:
415     p_access->info.b_eof = true;
416     block_Release( p_block );
417     p_sys->p_partial_frame = NULL;
418     return NULL;
419 }
420
421
422 /*
423  * rtp_ChainInsert - insert a p_block in the chain and
424  * look at the sequence numbers.
425  */
426 static inline bool rtp_ChainInsert( access_t *p_access, block_t *p_block )
427 {
428     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
429     block_t *p_prev = NULL;
430     block_t *p = p_sys->p_end;
431     uint16_t i_new = (uint16_t) p_block->i_dts;
432     uint16_t i_tmp = 0;
433
434     if( !p_sys->p_list )
435     {
436         p_sys->p_list = p_block;
437         p_sys->p_end = p_block;
438         return true;
439     }
440     /* walk through the queue from top down since the new packet is in
441     most cases just appended to the end */
442
443     for( ;; )
444     {
445         i_tmp = i_new - (uint16_t) p->i_dts;
446
447         if( !i_tmp )   /* trash duplicate */
448             break;
449
450         if ( i_tmp < 32768 )
451         {   /* insert after this block ( i_new > p->i_dts ) */
452             p_block->p_next = p->p_next;
453             p->p_next = p_block;
454             p_block->p_prev = p;
455             if (p_prev)
456             {
457                 p_prev->p_prev = p_block;
458                 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
459                         (uint16_t) p->i_dts, i_new );
460             }
461             else
462             {
463                 p_sys->p_end = p_block;
464             }
465             return true;
466         }
467         if( p == p_sys->p_list )
468         {   /* we've reached bottom of chain */
469             i_tmp = p_sys->i_last_seqno - i_new;
470             if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
471             {
472                 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
473                         i_new, (uint16_t) p->i_dts );
474                 p_block->p_next = p;
475                 p->p_prev = p_block;
476                 p_sys->p_list = p_block;
477                 return true;
478             }
479
480             if( !i_tmp )   /* trash duplicate */
481                 break;
482
483             /* reordering failed - append the packet to the end of queue */
484             msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
485                     "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
486                 (uint16_t) p_sys->p_end->i_dts);
487             p_sys->p_end->p_next = p_block;
488             p_block->p_prev = p_sys->p_end;
489             p_sys->p_end = p_block;
490             return true;
491         }
492         p_prev = p;
493         p = p->p_prev;
494     }
495     block_Release( p_block );
496     return false;
497 }
498
499 /*****************************************************************************
500  * BlockParseRTP: decapsulate the RTP packet and return it
501  *****************************************************************************/
502 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
503 {
504     int      i_payload_type;
505     size_t   i_skip = RTP_HEADER_LEN;
506
507     if( p_block == NULL )
508         return NULL;
509
510     if( p_block->i_buffer < RTP_HEADER_LEN )
511     {
512         msg_Dbg( p_access, "short RTP packet received" );
513         goto trash;
514     }
515
516     /* Parse the header and make some verifications.
517      * See RFC 3550. */
518     // Version number:
519     if( ( p_block->p_buffer[0] >> 6 ) != 2)
520     {
521         msg_Dbg( p_access, "RTP version is %u instead of 2",
522                  p_block->p_buffer[0] >> 6 );
523         goto trash;
524     }
525     // Padding bit:
526     uint8_t pad = (p_block->p_buffer[0] & 0x20)
527                     ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
528     // CSRC count:
529     i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
530     // Extension header:
531     if (p_block->p_buffer[0] & 0x10) /* Extension header */
532     {
533         i_skip += 4;
534         if ((size_t)p_block->i_buffer < i_skip)
535             goto trash;
536
537         i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
538     }
539
540     i_payload_type    = p_block->p_buffer[1] & 0x7F;
541
542     /* Remember sequence number in i_dts */
543     p_block->i_pts = mdate();
544     p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 );
545
546     /* FIXME: use rtpmap */
547     const char *psz_demux = NULL;
548
549     switch( i_payload_type )
550     {
551         case 14: // MPA: MPEG Audio (RFC2250, §3.4)
552             i_skip += 4; // 32 bits RTP/MPA header
553             psz_demux = "mpga";
554             break;
555
556         case 32: // MPV: MPEG Video (RFC2250, §3.5)
557             i_skip += 4; // 32 bits RTP/MPV header
558             if( (size_t)p_block->i_buffer < i_skip )
559                 goto trash;
560             if( p_block->p_buffer[i_skip - 3] & 0x4 )
561             {
562                 /* MPEG2 Video extension header */
563                 /* TODO: shouldn't we skip this too ? */
564             }
565             psz_demux = "mpgv";
566             break;
567
568         case 33: // MP2: MPEG TS (RFC2250, §2)
569             /* plain TS over RTP */
570             psz_demux = "ts";
571             break;
572
573         case 72: /* muxed SR */
574         case 73: /* muxed RR */
575         case 74: /* muxed SDES */
576         case 75: /* muxed BYE */
577         case 76: /* muxed APP */
578             goto trash; /* ooh! ignoring RTCP is evil! */
579
580         default:
581             msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
582             goto trash;
583     }
584
585     if( (size_t)p_block->i_buffer < (i_skip + pad) )
586         goto trash;
587
588     /* Remove the RTP header */
589     p_block->i_buffer -= i_skip;
590     p_block->p_buffer += i_skip;
591
592     /* This is the place for deciphering and authentication */
593
594     /* Remove padding (at the end) */
595     p_block->i_buffer -= pad;
596
597 #if 0
598     /* Emulate packet loss */
599     if ( (i_sequence_number % 4000) == 0)
600     {
601         msg_Warn( p_access, "Emulating packet drop" );
602         block_Release( p_block );
603         return NULL;
604     }
605 #endif
606
607     if( !p_access->psz_demux || !*p_access->psz_demux )
608     {
609         free( p_access->psz_demux );
610         p_access->psz_demux = strdup( psz_demux );
611     }
612
613     return p_block;
614
615 trash:
616     block_Release( p_block );
617     return NULL;
618 }
619
620 /*****************************************************************************
621  * BlockRTP: receives an RTP packet, parses it, queues it queue,
622  * then dequeues the oldest packet and returns it to input/demux.
623  ****************************************************************************/
624 static block_t *BlockRTP( access_t *p_access )
625 {
626     access_sys_t *p_sys = p_access->p_sys;
627     block_t *p;
628
629     while ( !p_sys->p_list ||
630              ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
631     {
632         p = BlockParseRTP( p_access,
633                            p_sys->b_framed_rtp ? BlockTCP( p_access )
634                                                : BlockUDP( p_access ) );
635         if ( !p )
636             return NULL;
637
638         rtp_ChainInsert( p_access, p );
639     }
640
641     p = p_sys->p_list;
642     p_sys->p_list = p_sys->p_list->p_next;
643     p_sys->i_last_seqno++;
644     if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
645     {
646         msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
647                  p_sys->i_last_seqno, (uint16_t) p->i_dts );
648         p_sys->i_last_seqno = (uint16_t) p->i_dts;
649     }
650     p->p_next = NULL;
651     return p;
652 }
653
654 /*****************************************************************************
655  * BlockPrebufferRTP: waits until we have at least two RTP datagrams,
656  * so that we can synchronize the RTP sequence number.
657  * This is only useful for non-reliable transport protocols.
658  ****************************************************************************/
659 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
660 {
661     access_sys_t *p_sys = p_access->p_sys;
662     mtime_t   i_first = mdate();
663     int       i_count = 0;
664     block_t   *p = p_block;
665
666     if( BlockParseRTP( p_access, p_block ) == NULL )
667         return NULL;
668
669     for( ;; )
670     {
671         mtime_t i_date = mdate();
672
673         if( p && rtp_ChainInsert( p_access, p ))
674             i_count++;
675
676         /* Require at least 2 packets in the buffer */
677         if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
678             break;
679
680         p = BlockParseRTP( p_access, BlockUDP( p_access ) );
681         if( !p && (i_date - i_first) > p_sys->i_rtp_late )
682         {
683             msg_Err( p_access, "error in RTP prebuffering!" );
684             return NULL;
685         }
686     }
687
688     msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
689     p_access->info.b_prebuffered = true;
690     p = p_sys->p_list;
691     p_sys->p_list = p_sys->p_list->p_next;
692     p_sys->i_last_seqno = (uint16_t) p->i_dts;
693     p->p_next = NULL;
694     return p;
695 }
696
697 static block_t *BlockStartRTP( access_t *p_access )
698 {
699     p_access->pf_block = BlockRTP;
700     return BlockPrebufferRTP( p_access, BlockUDP( p_access ) );
701 }
702
703
704 /*****************************************************************************
705  * BlockChoose: decide between RTP and UDP
706  *****************************************************************************/
707 static block_t *BlockChoose( access_t *p_access )
708 {
709     block_t *p_block;
710     int     i_rtp_version;
711     int     i_payload_type;
712
713     if( ( p_block = BlockUDP( p_access ) ) == NULL )
714         return NULL;
715
716     if( p_block->p_buffer[0] == 0x47 )
717     {
718         msg_Dbg( p_access, "detected TS over raw UDP" );
719         p_access->pf_block = BlockUDP;
720         p_access->info.b_prebuffered = true;
721         return p_block;
722     }
723
724     if( p_block->i_buffer < RTP_HEADER_LEN )
725         return p_block;
726
727     /* Parse the header and make some verifications.
728      * See RFC 3550. */
729
730     i_rtp_version  = p_block->p_buffer[0] >> 6;
731     i_payload_type = ( p_block->p_buffer[1] & 0x7F );
732
733     if( i_rtp_version != 2 )
734     {
735         msg_Dbg( p_access, "no supported RTP header detected" );
736         p_access->pf_block = BlockUDP;
737         p_access->info.b_prebuffered = true;
738         return p_block;
739     }
740
741     switch( i_payload_type )
742     {
743         case 33:
744             msg_Dbg( p_access, "detected MPEG2 TS over RTP" );
745             p_access->psz_demux = strdup( "ts" );
746             break;
747
748         case 14:
749             msg_Dbg( p_access, "detected MPEG Audio over RTP" );
750             p_access->psz_demux = strdup( "mpga" );
751             break;
752
753         case 32:
754             msg_Dbg( p_access, "detected MPEG Video over RTP" );
755             p_access->psz_demux = strdup( "mpgv" );
756             break;
757
758         default:
759             msg_Dbg( p_access, "no RTP header detected" );
760             p_access->pf_block = BlockUDP;
761             p_access->info.b_prebuffered = true;
762             return p_block;
763     }
764
765     p_access->pf_block = BlockRTP;
766     return BlockPrebufferRTP( p_access, p_block );
767 }