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 */
55 /*****************************************************************************
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." )
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)." )
68 static int Open ( vlc_object_t * );
69 static void Close( vlc_object_t * );
72 set_shortname( _("UDP/RTP" ) );
73 set_description( _("UDP/RTP input") );
74 set_category( CAT_INPUT );
75 set_subcategory( SUBCAT_INPUT_ACCESS );
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" );
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" );
94 set_callbacks( Open, Close );
97 /*****************************************************************************
99 *****************************************************************************/
100 #define RTP_HEADER_LEN 12
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 );
112 vlc_bool_t b_framed_rtp;
114 /* reorder rtp packets when out-of-sequence */
115 uint16_t i_last_seqno;
119 block_t *p_partial_frame; /* Partial Framed RTP packet */
122 /*****************************************************************************
123 * Open: open the socket
124 *****************************************************************************/
125 static int Open( vlc_object_t *p_this )
127 access_t *p_access = (access_t*)p_this;
130 char *psz_name = strdup( p_access->psz_path );
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;
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));
143 if (strlen (p_access->psz_access) > 0)
145 switch (p_access->psz_access[strlen (p_access->psz_access) - 1])
156 if (strcmp (p_access->psz_access, "udplite") == 0)
157 proto = IPPROTO_UDPLITE;
159 if (strncmp (p_access->psz_access, "udp", 3 ) == 0 )
160 p_access->pf_block = BlockChoose;
162 if (strcmp (p_access->psz_access, "rtptcp") == 0)
165 if (strcmp (p_access->psz_access, "dccp") == 0)
166 proto = IPPROTO_DCCP;
169 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
171 /* Parse psz_name syntax :
172 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
173 psz_parser = strchr( psz_name, '@' );
174 if( psz_parser != NULL )
176 /* Found bind address and/or bind port */
177 *psz_parser++ = '\0';
178 psz_bind_addr = psz_parser;
180 if( psz_bind_addr[0] == '[' )
181 /* skips bracket'd IPv6 address */
182 psz_parser = strchr( psz_parser, ']' );
184 if( psz_parser != NULL )
186 psz_parser = strchr( psz_parser, ':' );
187 if( psz_parser != NULL )
189 *psz_parser++ = '\0';
190 i_bind_port = atoi( psz_parser );
195 psz_server_addr = psz_name;
196 psz_parser = ( psz_server_addr[0] == '[' )
197 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
200 if( psz_parser != NULL )
202 psz_parser = strchr( psz_parser, ':' );
203 if( psz_parser != NULL )
205 *psz_parser++ = '\0';
206 i_server_port = atoi( psz_parser );
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 );
213 /* Hmm, the net_* connection functions may need to be unified... */
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,
224 p_sys->fd = net_ConnectTCP( p_access, psz_server_addr, i_server_port );
225 p_access->pf_block = BlockRTP;
226 p_sys->b_framed_rtp = VLC_TRUE;
231 p_sys->fd = net_Connect( p_access, psz_server_addr, i_server_port,
232 SOCK_DCCP, IPPROTO_DCCP );
235 msg_Err( p_access, "DCCP support not compiled-in!" );
240 if( p_sys->fd == -1 )
242 msg_Err( p_access, "cannot open socket" );
247 shutdown( p_sys->fd, SHUT_WR );
248 net_SetCSCov (p_sys->fd, -1, 12);
250 /* Update default_pts to a suitable value for udp access */
251 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
253 /* RTP reordering for out-of-sequence packets */
254 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
255 p_sys->i_last_seqno = 0;
256 p_sys->p_list = NULL;
261 /*****************************************************************************
262 * Close: free unused data structures
263 *****************************************************************************/
264 static void Close( vlc_object_t *p_this )
266 access_t *p_access = (access_t*)p_this;
267 access_sys_t *p_sys = p_access->p_sys;
269 block_ChainRelease( p_sys->p_list );
270 net_Close( p_sys->fd );
274 /*****************************************************************************
276 *****************************************************************************/
277 static int Control( access_t *p_access, int i_query, va_list args )
286 case ACCESS_CAN_SEEK:
287 case ACCESS_CAN_FASTSEEK:
288 case ACCESS_CAN_PAUSE:
289 case ACCESS_CAN_CONTROL_PACE:
290 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
291 *pb_bool = VLC_FALSE;
295 pi_int = (int*)va_arg( args, int * );
299 case ACCESS_GET_PTS_DELAY:
300 pi_64 = (int64_t*)va_arg( args, int64_t * );
301 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
305 case ACCESS_SET_PAUSE_STATE:
306 case ACCESS_GET_TITLE_INFO:
307 case ACCESS_SET_TITLE:
308 case ACCESS_SET_SEEKPOINT:
309 case ACCESS_SET_PRIVATE_ID_STATE:
313 msg_Warn( p_access, "unimplemented query in control" );
320 /*****************************************************************************
322 *****************************************************************************/
323 static block_t *BlockUDP( access_t *p_access )
325 access_sys_t *p_sys = p_access->p_sys;
329 p_block = block_New( p_access, MTU );
330 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
331 p_block->p_buffer, MTU, VLC_FALSE );
332 if( p_block->i_buffer < 0 )
334 block_Release( p_block );
338 return block_Realloc( p_block, 0, p_block->i_buffer );
341 /*****************************************************************************
342 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
343 *****************************************************************************/
344 static block_t *BlockTCP( access_t *p_access )
346 access_sys_t *p_sys = p_access->p_sys;
347 block_t *p_block = p_sys->p_partial_frame;
349 if( p_access->info.b_eof )
352 if( p_block == NULL )
354 /* MTU should always be 65535 in this case */
355 p_sys->p_partial_frame = p_block = block_New( p_access, 2 + MTU );
360 /* Read RTP framing */
361 if (p_block->i_buffer < 2)
363 int i_read = net_Read( p_access, p_sys->fd, NULL,
364 p_block->p_buffer + p_block->i_buffer,
365 2 - p_block->i_buffer, VLC_FALSE );
369 p_block->i_buffer += i_read;
370 if (p_block->i_buffer < 2)
374 uint16_t framelen = GetWLE( p_block->p_buffer );
378 int i_read = net_Read( p_access, p_sys->fd, NULL,
379 p_block->p_buffer + p_block->i_buffer,
380 2 + framelen - p_block->i_buffer, VLC_FALSE );
384 p_block->i_buffer += i_read;
387 if( p_block->i_buffer < (2 + framelen) )
388 return NULL; // incomplete frame
390 /* Hide framing from RTP layer */
391 p_block->p_buffer += 2;
392 p_block->i_buffer -= 2;
393 p_sys->p_partial_frame = NULL;
397 p_access->info.b_eof = VLC_TRUE;
398 block_Release( p_block );
399 p_sys->p_partial_frame = NULL;
405 * rtp_ChainInsert - insert a p_block in the chain and
406 * look at the sequence numbers.
408 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
410 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
411 block_t *p_prev = NULL;
412 block_t *p = p_sys->p_end;
413 uint16_t i_new = (uint16_t) p_block->i_dts;
418 p_sys->p_list = p_block;
419 p_sys->p_end = p_block;
422 /* walk through the queue from top down since the new packet is in
423 most cases just appended to the end */
427 i_tmp = i_new - (uint16_t) p->i_dts;
429 if( !i_tmp ) /* trash duplicate */
433 { /* insert after this block ( i_new > p->i_dts ) */
434 p_block->p_next = p->p_next;
439 p_prev->p_prev = p_block;
440 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
441 (uint16_t) p->i_dts, i_new );
445 p_sys->p_end = p_block;
449 if( p == p_sys->p_list )
450 { /* we've reached bottom of chain */
451 i_tmp = p_sys->i_last_seqno - i_new;
452 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
454 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
455 i_new, (uint16_t) p->i_dts );
458 p_sys->p_list = p_block;
462 if( !i_tmp ) /* trash duplicate */
465 /* reordering failed - append the packet to the end of queue */
466 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
467 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
468 (uint16_t) p_sys->p_end->i_dts);
469 p_sys->p_end->p_next = p_block;
470 p_block->p_prev = p_sys->p_end;
471 p_sys->p_end = p_block;
477 block_Release( p_block );
481 /*****************************************************************************
482 * BlockParseRTP: decapsulate the RTP packet and return it
483 *****************************************************************************/
484 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
487 size_t i_skip = RTP_HEADER_LEN;
489 if( p_block == NULL )
492 if( p_block->i_buffer < RTP_HEADER_LEN )
494 msg_Dbg( p_access, "short RTP packet received" );
498 /* Parse the header and make some verifications.
501 if( ( p_block->p_buffer[0] >> 6 ) != 2)
503 msg_Dbg( p_access, "RTP version is %u instead of 2",
504 p_block->p_buffer[0] >> 6 );
508 uint8_t pad = (p_block->p_buffer[0] & 0x20)
509 ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
511 i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
513 if (p_block->p_buffer[0] & 0x10) /* Extension header */
516 if ((size_t)p_block->i_buffer < i_skip)
519 i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
522 i_payload_type = p_block->p_buffer[1] & 0x7F;
524 /* Remember sequence number in i_dts */
525 p_block->i_pts = mdate();
526 p_block->i_dts = (mtime_t) GetWBE( p_block->p_buffer + 2 );
528 /* FIXME: use rtpmap */
529 const char *psz_demux = NULL;
531 switch( i_payload_type )
533 case 14: // MPA: MPEG Audio (RFC2250, §3.4)
534 i_skip += 4; // 32 bits RTP/MPA header
538 case 32: // MPV: MPEG Video (RFC2250, §3.5)
539 i_skip += 4; // 32 bits RTP/MPV header
540 if( (size_t)p_block->i_buffer < i_skip )
542 if( p_block->p_buffer[i_skip - 3] & 0x4 )
544 /* MPEG2 Video extension header */
545 /* TODO: shouldn't we skip this too ? */
550 case 33: // MP2: MPEG TS (RFC2250, §2)
551 /* plain TS over RTP */
555 case 72: /* muxed SR */
556 case 73: /* muxed RR */
557 case 74: /* muxed SDES */
558 case 75: /* muxed BYE */
559 case 76: /* muxed APP */
560 goto trash; /* ooh! ignoring RTCP is evil! */
563 msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
567 if( (size_t)p_block->i_buffer < (i_skip + pad) )
570 /* Remove the RTP header */
571 p_block->i_buffer -= i_skip;
572 p_block->p_buffer += i_skip;
574 /* This is the place for deciphering and authentication */
576 /* Remove padding (at the end) */
577 p_block->i_buffer -= pad;
580 /* Emulate packet loss */
581 if ( (i_sequence_number % 4000) == 0)
583 msg_Warn( p_access, "Emulating packet drop" );
584 block_Release( p_block );
589 if( !p_access->psz_demux || !*p_access->psz_demux )
591 free( p_access->psz_demux );
592 p_access->psz_demux = strdup( psz_demux );
598 block_Release( p_block );
602 /*****************************************************************************
603 * BlockRTP: receives an RTP packet, parses it, queues it queue,
604 * then dequeues the oldest packet and returns it to input/demux.
605 ****************************************************************************/
606 static block_t *BlockRTP( access_t *p_access )
608 access_sys_t *p_sys = p_access->p_sys;
611 while ( !p_sys->p_list ||
612 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
614 p = BlockParseRTP( p_access,
615 p_sys->b_framed_rtp ? BlockTCP( p_access )
616 : BlockUDP( p_access ) );
620 rtp_ChainInsert( p_access, p );
624 p_sys->p_list = p_sys->p_list->p_next;
625 p_sys->i_last_seqno++;
626 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
628 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
629 p_sys->i_last_seqno, (uint16_t) p->i_dts );
630 p_sys->i_last_seqno = (uint16_t) p->i_dts;
636 /*****************************************************************************
637 * BlockPrebufferRTP: waits until we have at least two RTP datagrams,
638 * so that we can synchronize the RTP sequence number.
639 * This is only useful for non-reliable transport protocols.
640 ****************************************************************************/
641 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
643 access_sys_t *p_sys = p_access->p_sys;
644 mtime_t i_first = mdate();
646 block_t *p = p_block;
648 if( BlockParseRTP( p_access, p_block ) == NULL )
653 mtime_t i_date = mdate();
655 if( p && rtp_ChainInsert( p_access, p ))
658 /* Require at least 2 packets in the buffer */
659 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
662 p = BlockParseRTP( p_access, BlockUDP( p_access ) );
663 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
665 msg_Err( p_access, "error in RTP prebuffering!" );
670 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
671 p_access->info.b_prebuffered = VLC_TRUE;
673 p_sys->p_list = p_sys->p_list->p_next;
674 p_sys->i_last_seqno = (uint16_t) p->i_dts;
679 static block_t *BlockStartRTP( access_t *p_access )
681 p_access->pf_block = BlockRTP;
682 return BlockPrebufferRTP( p_access, BlockUDP( p_access ) );
686 /*****************************************************************************
687 * BlockChoose: decide between RTP and UDP
688 *****************************************************************************/
689 static block_t *BlockChoose( access_t *p_access )
695 if( ( p_block = BlockUDP( p_access ) ) == NULL )
698 if( p_block->p_buffer[0] == 0x47 )
700 msg_Dbg( p_access, "detected TS over raw UDP" );
701 p_access->pf_block = BlockUDP;
702 p_access->info.b_prebuffered = VLC_TRUE;
706 if( p_block->i_buffer < RTP_HEADER_LEN )
709 /* Parse the header and make some verifications.
712 i_rtp_version = p_block->p_buffer[0] >> 6;
713 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
715 if( i_rtp_version != 2 )
717 msg_Dbg( p_access, "no supported RTP header detected" );
718 p_access->pf_block = BlockUDP;
719 p_access->info.b_prebuffered = VLC_TRUE;
723 switch( i_payload_type )
726 msg_Dbg( p_access, "detected MPEG2 TS over RTP" );
727 p_access->psz_demux = strdup( "ts" );
731 msg_Dbg( p_access, "detected MPEG Audio over RTP" );
732 p_access->psz_demux = strdup( "mpga" );
736 msg_Dbg( p_access, "detected MPEG Video over RTP" );
737 p_access->psz_demux = strdup( "mpgv" );
741 msg_Dbg( p_access, "no RTP header detected" );
742 p_access->pf_block = BlockUDP;
743 p_access->info.b_prebuffered = VLC_TRUE;
747 p_access->pf_block = BlockRTP;
748 return BlockPrebufferRTP( p_access, p_block );