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
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>
14 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
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.
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.
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 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
36 #include <vlc_access.h>
37 #include <vlc_network.h>
39 #ifndef SOCK_DCCP /* provisional API */
46 # define IPPROTO_DCCP 33 /* IANA */
49 #ifndef IPPROTO_UDPLITE
50 # define IPPROTO_UDPLITE 136 /* from IANA */
53 # define SOL_UDPLITE IPPROTO_UDPLITE
58 /*****************************************************************************
60 *****************************************************************************/
61 #define CACHING_TEXT N_("Caching value in ms")
62 #define CACHING_LONGTEXT N_( \
63 "Caching value for UDP streams. This " \
64 "value should be set in milliseconds." )
66 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
67 #define RTP_LATE_LONGTEXT N_( \
68 "VLC reorders RTP packets. The input will wait for late packets at most "\
69 "the time specified here (in milliseconds)." )
71 static int Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
75 set_shortname( _("UDP/RTP" ) );
76 set_description( _("UDP/RTP input") );
77 set_category( CAT_INPUT );
78 set_subcategory( SUBCAT_INPUT_ACCESS );
80 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
81 CACHING_LONGTEXT, VLC_TRUE );
82 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
83 add_obsolete_bool( "udp-auto-mtu" );
85 set_capability( "access2", 0 );
86 add_shortcut( "udp" );
87 add_shortcut( "udpstream" );
88 add_shortcut( "udp4" );
89 add_shortcut( "udp6" );
90 add_shortcut( "rtp" );
91 add_shortcut( "rtp4" );
92 add_shortcut( "rtp6" );
93 add_shortcut( "udplite" );
94 add_shortcut( "rtptcp" );
95 add_shortcut( "dccp" );
97 set_callbacks( Open, Close );
100 /*****************************************************************************
102 *****************************************************************************/
103 #define RTP_HEADER_LEN 12
105 static block_t *BlockUDP( access_t * );
106 static block_t *BlockTCP( access_t * );
107 static block_t *BlockRTP( access_t * );
108 static block_t *BlockChoose( access_t * );
109 static int Control( access_t *, int, va_list );
115 vlc_bool_t b_framed_rtp;
117 /* reorder rtp packets when out-of-sequence */
118 uint16_t i_last_seqno;
122 block_t *p_partial_frame; /* Partial Framed RTP packet */
125 /*****************************************************************************
126 * Open: open the socket
127 *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
130 access_t *p_access = (access_t*)p_this;
133 char *psz_name = strdup( p_access->psz_path );
135 const char *psz_server_addr, *psz_bind_addr = "";
136 int i_bind_port, i_server_port = 0;
137 int fam = AF_UNSPEC, proto = IPPROTO_UDP;
139 if (strlen (p_access->psz_access) >= 3)
141 switch (p_access->psz_access[3])
151 if (strcmp (p_access->psz_access + 3, "lite") == 0)
152 proto = IPPROTO_UDPLITE;
154 if (strcmp (p_access->psz_access + 3, "tcp") == 0)
157 if (strcmp (p_access->psz_access, "dccp") == 0)
158 proto = IPPROTO_DCCP;
161 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
163 /* Parse psz_name syntax :
164 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
165 psz_parser = strchr( psz_name, '@' );
166 if( psz_parser != NULL )
168 /* Found bind address and/or bind port */
169 *psz_parser++ = '\0';
170 psz_bind_addr = psz_parser;
172 if( psz_bind_addr[0] == '[' )
173 /* skips bracket'd IPv6 address */
174 psz_parser = strchr( psz_parser, ']' );
176 if( psz_parser != NULL )
178 psz_parser = strchr( psz_parser, ':' );
179 if( psz_parser != NULL )
181 *psz_parser++ = '\0';
182 i_bind_port = atoi( psz_parser );
187 psz_server_addr = psz_name;
188 psz_parser = ( psz_server_addr[0] == '[' )
189 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
192 if( psz_parser != NULL )
194 psz_parser = strchr( psz_parser, ':' );
195 if( psz_parser != NULL )
197 *psz_parser++ = '\0';
198 i_server_port = atoi( psz_parser );
202 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
203 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
205 /* Set up p_access */
206 access_InitFields( p_access );
207 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
208 p_access->info.b_prebuffered = VLC_FALSE;
209 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
210 memset (p_sys, 0, sizeof (*p_sys));
215 case IPPROTO_UDPLITE:
216 p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
217 psz_server_addr, i_server_port, fam,
222 p_sys->fd = net_ConnectTCP( p_access, psz_server_addr, i_server_port );
223 p_sys->b_framed_rtp = VLC_TRUE;
228 p_sys->fd = net_Connect( p_access, psz_server_addr, i_server_port,
229 SOCK_DCCP, IPPROTO_DCCP );
232 msg_Err( p_access, "DCCP support not compiled-in!" );
237 if( p_sys->fd == -1 )
239 msg_Err( p_access, "cannot open socket" );
244 shutdown( p_sys->fd, SHUT_WR );
246 net_SetCSCov (p_sys->fd, -1, 12);
248 if (p_sys->b_framed_rtp)
250 /* We don't do autodetection and prebuffering in case of framing */
251 p_access->pf_block = BlockRTP;
254 /* Update default_pts to a suitable value for udp access */
255 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
257 /* RTP reordering for out-of-sequence packets */
258 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
259 p_sys->i_last_seqno = 0;
260 p_sys->p_list = NULL;
265 /*****************************************************************************
266 * Close: free unused data structures
267 *****************************************************************************/
268 static void Close( vlc_object_t *p_this )
270 access_t *p_access = (access_t*)p_this;
271 access_sys_t *p_sys = p_access->p_sys;
273 block_ChainRelease( p_sys->p_list );
274 net_Close( p_sys->fd );
278 /*****************************************************************************
280 *****************************************************************************/
281 static int Control( access_t *p_access, int i_query, va_list args )
290 case ACCESS_CAN_SEEK:
291 case ACCESS_CAN_FASTSEEK:
292 case ACCESS_CAN_PAUSE:
293 case ACCESS_CAN_CONTROL_PACE:
294 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
295 *pb_bool = VLC_FALSE;
299 pi_int = (int*)va_arg( args, int * );
303 case ACCESS_GET_PTS_DELAY:
304 pi_64 = (int64_t*)va_arg( args, int64_t * );
305 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
309 case ACCESS_SET_PAUSE_STATE:
310 case ACCESS_GET_TITLE_INFO:
311 case ACCESS_SET_TITLE:
312 case ACCESS_SET_SEEKPOINT:
313 case ACCESS_SET_PRIVATE_ID_STATE:
317 msg_Warn( p_access, "unimplemented query in control" );
324 /*****************************************************************************
326 *****************************************************************************/
327 static block_t *BlockUDP( access_t *p_access )
329 access_sys_t *p_sys = p_access->p_sys;
333 p_block = block_New( p_access, MTU );
334 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
335 p_block->p_buffer, MTU, VLC_FALSE );
336 if( p_block->i_buffer < 0 )
338 block_Release( p_block );
342 return block_Realloc( p_block, 0, p_block->i_buffer );
345 /*****************************************************************************
346 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
347 *****************************************************************************/
348 static block_t *BlockTCP( access_t *p_access )
350 access_sys_t *p_sys = p_access->p_sys;
351 block_t *p_block = p_sys->p_partial_frame;
353 if( p_access->info.b_eof )
356 if( p_block == NULL )
358 /* MTU should always be 65535 in this case */
359 p_sys->p_partial_frame = p_block = block_New( p_access, 2 + MTU );
364 /* Read RTP framing */
365 if (p_block->i_buffer < 2)
367 /* FIXME: not very efficient */
368 int i_read = net_Read( p_access, p_sys->fd, NULL,
369 p_block->p_buffer + p_block->i_buffer,
370 2 - p_block->i_buffer, VLC_FALSE );
374 p_block->i_buffer += i_read;
375 if (p_block->i_buffer < 2)
379 uint16_t framelen = GetWLE( p_block->p_buffer );
383 int i_read = net_Read( p_access, p_sys->fd, NULL,
384 p_block->p_buffer + p_block->i_buffer,
385 2 + framelen - p_block->i_buffer, VLC_FALSE );
389 p_block->i_buffer += i_read;
392 if( p_block->i_buffer < (2 + framelen) )
393 return NULL; // incomplete frame
395 /* Hide framing from RTP layer */
396 p_block->p_buffer += 2;
397 p_block->i_buffer -= 2;
398 p_sys->p_partial_frame = NULL;
402 p_access->info.b_eof = VLC_TRUE;
403 block_Release( p_block );
404 p_sys->p_partial_frame = NULL;
410 * rtp_ChainInsert - insert a p_block in the chain and
411 * look at the sequence numbers.
413 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
415 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
416 block_t *p_prev = NULL;
417 block_t *p = p_sys->p_end;
418 uint16_t i_new = (uint16_t) p_block->i_dts;
423 p_sys->p_list = p_block;
424 p_sys->p_end = p_block;
427 /* walk through the queue from top down since the new packet is in
428 most cases just appended to the end */
432 i_tmp = i_new - (uint16_t) p->i_dts;
434 if( !i_tmp ) /* trash duplicate */
438 { /* insert after this block ( i_new > p->i_dts ) */
439 p_block->p_next = p->p_next;
444 p_prev->p_prev = p_block;
445 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
446 (uint16_t) p->i_dts, i_new );
450 p_sys->p_end = p_block;
454 if( p == p_sys->p_list )
455 { /* we've reached bottom of chain */
456 i_tmp = p_sys->i_last_seqno - i_new;
457 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
459 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
460 i_new, (uint16_t) p->i_dts );
463 p_sys->p_list = p_block;
467 if( !i_tmp ) /* trash duplicate */
470 /* reordering failed - append the packet to the end of queue */
471 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
472 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
473 (uint16_t) p_sys->p_end->i_dts);
474 p_sys->p_end->p_next = p_block;
475 p_block->p_prev = p_sys->p_end;
476 p_sys->p_end = p_block;
482 block_Release( p_block );
486 /*****************************************************************************
487 * BlockParseRTP/BlockRTP:
488 *****************************************************************************/
489 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
492 size_t i_skip = RTP_HEADER_LEN;
494 if( p_block == NULL )
497 if( p_block->i_buffer < RTP_HEADER_LEN )
499 msg_Dbg( p_access, "short RTP packet received" );
503 /* Parse the header and make some verifications.
506 if( ( p_block->p_buffer[0] >> 6 ) != 2)
508 msg_Dbg( p_access, "RTP version is %u instead of 2",
509 p_block->p_buffer[0] >> 6 );
513 uint8_t pad = (p_block->p_buffer[0] & 0x20)
514 ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
516 i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
518 if (p_block->p_buffer[0] & 0x10) /* Extension header */
521 if ((size_t)p_block->i_buffer < i_skip)
524 i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
527 i_payload_type = p_block->p_buffer[1] & 0x7F;
529 /* Remember sequence number in i_dts */
530 p_block->i_pts = mdate();
531 p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 );
533 /* FIXME: use rtpmap */
534 switch( i_payload_type )
536 case 14: // MPA: MPEG Audio (RFC2250, §3.4)
537 i_skip += 4; // 32 bits RTP/MPA header
540 case 32: // MPV: MPEG Video (RFC2250, §3.5)
541 i_skip += 4; // 32 bits RTP/MPV header
542 if( (size_t)p_block->i_buffer < i_skip )
544 if( p_block->p_buffer[i_skip - 3] & 0x4 )
546 /* MPEG2 Video extension header */
547 /* TODO: shouldn't we skip this too ? */
551 case 33: // MP2: MPEG TS (RFC2250, §2)
552 /* plain TS over RTP */
556 msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
560 if( (size_t)p_block->i_buffer < (i_skip + pad) )
563 /* Remove the RTP header */
564 p_block->i_buffer -= i_skip;
565 p_block->p_buffer += i_skip;
567 /* This is the place for deciphering and authentication */
569 /* Remove padding (at the end) */
570 p_block->i_buffer -= pad;
573 /* Emulate packet loss */
574 if ( (i_sequence_number % 4000) == 0)
576 msg_Warn( p_access, "Emulating packet drop" );
577 block_Release( p_block );
585 block_Release( p_block );
589 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
591 access_sys_t *p_sys = p_access->p_sys;
592 mtime_t i_first = mdate();
594 block_t *p = p_block;
598 mtime_t i_date = mdate();
600 if( p && rtp_ChainInsert( p_access, p ))
603 /* Require at least 2 packets in the buffer */
604 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
607 p = BlockParseRTP( p_access, BlockUDP( p_access ) );
608 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
610 msg_Err( p_access, "error in RTP prebuffering!" );
615 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
616 p_access->info.b_prebuffered = VLC_TRUE;
618 p_sys->p_list = p_sys->p_list->p_next;
619 p_sys->i_last_seqno = (uint16_t) p->i_dts;
624 static block_t *BlockRTP( access_t *p_access )
626 access_sys_t *p_sys = p_access->p_sys;
629 while ( !p_sys->p_list ||
630 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
632 p = BlockParseRTP( p_access,
633 p_sys->b_framed_rtp ? BlockTCP( p_access )
634 : BlockUDP( p_access ) );
638 rtp_ChainInsert( p_access, p );
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 )
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;
654 /*****************************************************************************
655 * BlockChoose: decide between RTP and UDP
656 *****************************************************************************/
657 static block_t *BlockChoose( access_t *p_access )
663 if( ( p_block = BlockUDP( p_access ) ) == NULL )
666 if( p_block->p_buffer[0] == 0x47 )
668 msg_Dbg( p_access, "detected TS over raw UDP" );
669 p_access->pf_block = BlockUDP;
670 p_access->info.b_prebuffered = VLC_TRUE;
674 if( p_block->i_buffer < RTP_HEADER_LEN )
677 /* Parse the header and make some verifications.
680 i_rtp_version = p_block->p_buffer[0] >> 6;
681 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
683 if( i_rtp_version != 2 )
685 msg_Dbg( p_access, "no supported RTP header detected" );
686 p_access->pf_block = BlockUDP;
687 p_access->info.b_prebuffered = VLC_TRUE;
691 switch( i_payload_type )
694 msg_Dbg( p_access, "detected MPEG2 TS over RTP" );
695 p_access->psz_demux = strdup( "ts" );
699 msg_Dbg( p_access, "detected MPEG Audio over RTP" );
700 p_access->psz_demux = strdup( "mpga" );
704 msg_Dbg( p_access, "detected MPEG Video over RTP" );
705 p_access->psz_demux = strdup( "mpgv" );
709 msg_Dbg( p_access, "no RTP header detected" );
710 p_access->pf_block = BlockUDP;
711 p_access->info.b_prebuffered = VLC_TRUE;
715 if( !BlockParseRTP( p_access, p_block )) return NULL;
717 p_access->pf_block = BlockRTP;
719 return BlockPrebufferRTP( p_access, p_block );