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 *****************************************************************************/
40 #include <vlc_plugin.h>
41 #include <vlc_access.h>
42 #include <vlc_network.h>
44 #ifndef SOCK_DCCP /* provisional API */
51 # define IPPROTO_DCCP 33 /* IANA */
54 #ifndef IPPROTO_UDPLITE
55 # define IPPROTO_UDPLITE 136 /* from IANA */
60 /*****************************************************************************
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." )
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)." )
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
77 set_shortname( N_("UDP/RTP" ) );
78 set_description( N_("UDP/RTP input") );
79 set_category( CAT_INPUT );
80 set_subcategory( SUBCAT_INPUT_ACCESS );
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" );
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" );
99 set_callbacks( Open, Close );
102 /*****************************************************************************
104 *****************************************************************************/
105 #define RTP_HEADER_LEN 12
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 );
117 bool b_framed_rtp, b_comedia;
119 /* reorder rtp packets when out-of-sequence */
120 uint16_t i_last_seqno;
124 block_t *p_partial_frame; /* Partial Framed RTP packet */
127 /*****************************************************************************
128 * Open: open the socket
129 *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
132 access_t *p_access = (access_t*)p_this;
135 char *psz_name = strdup( p_access->psz_path );
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;
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));
148 if (strlen (p_access->psz_access) > 0)
150 switch (p_access->psz_access[strlen (p_access->psz_access) - 1])
161 if (strcmp (p_access->psz_access, "udplite") == 0)
162 proto = IPPROTO_UDPLITE;
164 if (strncmp (p_access->psz_access, "udp", 3 ) == 0 )
165 p_access->pf_block = BlockChoose;
167 if (strcmp (p_access->psz_access, "rtptcp") == 0)
170 if (strcmp (p_access->psz_access, "dccp") == 0)
171 proto = IPPROTO_DCCP;
174 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
176 /* Parse psz_name syntax :
177 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
178 psz_parser = strchr( psz_name, '@' );
179 if( psz_parser != NULL )
181 /* Found bind address and/or bind port */
182 *psz_parser++ = '\0';
183 psz_bind_addr = psz_parser;
185 if( psz_bind_addr[0] == '[' )
186 /* skips bracket'd IPv6 address */
187 psz_parser = strchr( psz_parser, ']' );
189 if( psz_parser != NULL )
191 psz_parser = strchr( psz_parser, ':' );
192 if( psz_parser != NULL )
194 *psz_parser++ = '\0';
195 i_bind_port = atoi( psz_parser );
200 psz_server_addr = psz_name;
201 psz_parser = ( psz_server_addr[0] == '[' )
202 ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
205 if( psz_parser != NULL )
207 psz_parser = strchr( psz_parser, ':' );
208 if( psz_parser != NULL )
210 *psz_parser++ = '\0';
211 i_server_port = atoi( psz_parser );
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 );
218 /* Hmm, the net_* connection functions may need to be unified... */
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,
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;
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 );
242 msg_Err( p_access, "DCCP support not compiled-in!" );
244 p_sys->b_comedia = true;
248 if( p_sys->fd == -1 )
250 msg_Err( p_access, "cannot open socket" );
255 shutdown( p_sys->fd, SHUT_WR );
256 net_SetCSCov (p_sys->fd, -1, 12);
258 /* Update default_pts to a suitable value for udp access */
259 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
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;
269 /*****************************************************************************
270 * Close: free unused data structures
271 *****************************************************************************/
272 static void Close( vlc_object_t *p_this )
274 access_t *p_access = (access_t*)p_this;
275 access_sys_t *p_sys = p_access->p_sys;
277 block_ChainRelease( p_sys->p_list );
278 net_Close( p_sys->fd );
282 /*****************************************************************************
284 *****************************************************************************/
285 static int Control( access_t *p_access, int i_query, va_list args )
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* );
303 pi_int = (int*)va_arg( args, int * );
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;
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:
322 msg_Warn( p_access, "unimplemented query in control" );
329 /*****************************************************************************
331 *****************************************************************************/
332 static block_t *BlockUDP( access_t *p_access )
334 access_sys_t *p_sys = p_access->p_sys;
338 if( p_access->info.b_eof )
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 );
346 || ( p_sys->b_comedia && ( len == 0 ) ) )
348 if( p_sys->b_comedia )
350 p_access->info.b_eof = true;
351 msg_Dbg( p_access, "connection-oriented media hangup" );
353 block_Release( p_block );
357 return block_Realloc( p_block, 0, p_block->i_buffer = len );
360 /*****************************************************************************
361 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA (see RFC4571)
362 *****************************************************************************/
363 static block_t *BlockTCP( access_t *p_access )
365 access_sys_t *p_sys = p_access->p_sys;
366 block_t *p_block = p_sys->p_partial_frame;
368 if( p_access->info.b_eof )
371 if( p_block == NULL )
373 /* MTU should always be 65535 in this case */
374 p_sys->p_partial_frame = p_block = block_New( p_access, 2 + MTU );
379 /* Read RTP framing */
380 if (p_block->i_buffer < 2)
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 );
388 p_block->i_buffer += i_read;
389 if (p_block->i_buffer < 2)
393 uint16_t framelen = GetWLE( p_block->p_buffer );
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 );
403 p_block->i_buffer += i_read;
406 if( p_block->i_buffer < (2u + framelen) )
407 return NULL; // incomplete frame
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;
416 p_access->info.b_eof = true;
417 block_Release( p_block );
418 p_sys->p_partial_frame = NULL;
424 * rtp_ChainInsert - insert a p_block in the chain and
425 * look at the sequence numbers.
427 static inline bool rtp_ChainInsert( access_t *p_access, block_t *p_block )
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;
437 p_sys->p_list = p_block;
438 p_sys->p_end = p_block;
441 /* walk through the queue from top down since the new packet is in
442 most cases just appended to the end */
446 i_tmp = i_new - (uint16_t) p->i_dts;
448 if( !i_tmp ) /* trash duplicate */
452 { /* insert after this block ( i_new > p->i_dts ) */
453 p_block->p_next = p->p_next;
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 );
464 p_sys->p_end = p_block;
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) )
473 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
474 i_new, (uint16_t) p->i_dts );
477 p_sys->p_list = p_block;
481 if( !i_tmp ) /* trash duplicate */
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;
496 block_Release( p_block );
500 /*****************************************************************************
501 * BlockParseRTP: decapsulate the RTP packet and return it
502 *****************************************************************************/
503 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
506 size_t i_skip = RTP_HEADER_LEN;
508 if( p_block == NULL )
511 if( p_block->i_buffer < RTP_HEADER_LEN )
513 msg_Dbg( p_access, "short RTP packet received" );
517 /* Parse the header and make some verifications.
520 if( ( p_block->p_buffer[0] >> 6 ) != 2)
522 msg_Dbg( p_access, "RTP version is %u instead of 2",
523 p_block->p_buffer[0] >> 6 );
527 uint8_t pad = (p_block->p_buffer[0] & 0x20)
528 ? p_block->p_buffer[p_block->i_buffer - 1] : 0;
530 i_skip += (p_block->p_buffer[0] & 0x0F) * 4;
532 if (p_block->p_buffer[0] & 0x10) /* Extension header */
535 if ((size_t)p_block->i_buffer < i_skip)
538 i_skip += 4 * GetWBE( p_block->p_buffer + i_skip - 2 );
541 i_payload_type = p_block->p_buffer[1] & 0x7F;
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 );
547 /* FIXME: use rtpmap */
548 const char *psz_demux = NULL;
550 switch( i_payload_type )
552 case 14: // MPA: MPEG Audio (RFC2250, §3.4)
553 i_skip += 4; // 32 bits RTP/MPA header
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 )
561 if( p_block->p_buffer[i_skip - 3] & 0x4 )
563 /* MPEG2 Video extension header */
564 /* TODO: shouldn't we skip this too ? */
569 case 33: // MP2: MPEG TS (RFC2250, §2)
570 /* plain TS over RTP */
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! */
582 msg_Dbg( p_access, "unsupported RTP payload type: %u", i_payload_type );
586 if( (size_t)p_block->i_buffer < (i_skip + pad) )
589 /* Remove the RTP header */
590 p_block->i_buffer -= i_skip;
591 p_block->p_buffer += i_skip;
593 /* This is the place for deciphering and authentication */
595 /* Remove padding (at the end) */
596 p_block->i_buffer -= pad;
599 /* Emulate packet loss */
600 if ( (i_sequence_number % 4000) == 0)
602 msg_Warn( p_access, "Emulating packet drop" );
603 block_Release( p_block );
608 if( !p_access->psz_demux || !*p_access->psz_demux )
610 free( p_access->psz_demux );
611 p_access->psz_demux = strdup( psz_demux );
617 block_Release( p_block );
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 )
627 access_sys_t *p_sys = p_access->p_sys;
630 while ( !p_sys->p_list ||
631 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
633 p = BlockParseRTP( p_access,
634 p_sys->b_framed_rtp ? BlockTCP( p_access )
635 : BlockUDP( p_access ) );
639 rtp_ChainInsert( p_access, p );
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 )
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;
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 )
662 access_sys_t *p_sys = p_access->p_sys;
663 mtime_t i_first = mdate();
665 block_t *p = p_block;
667 if( BlockParseRTP( p_access, p_block ) == NULL )
672 mtime_t i_date = mdate();
674 if( p && rtp_ChainInsert( p_access, p ))
677 /* Require at least 2 packets in the buffer */
678 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
681 p = BlockParseRTP( p_access, BlockUDP( p_access ) );
682 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
684 msg_Err( p_access, "error in RTP prebuffering!" );
689 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
690 p_access->info.b_prebuffered = true;
692 p_sys->p_list = p_sys->p_list->p_next;
693 p_sys->i_last_seqno = (uint16_t) p->i_dts;
698 static block_t *BlockStartRTP( access_t *p_access )
700 p_access->pf_block = BlockRTP;
701 return BlockPrebufferRTP( p_access, BlockUDP( p_access ) );
705 /*****************************************************************************
706 * BlockChoose: decide between RTP and UDP
707 *****************************************************************************/
708 static block_t *BlockChoose( access_t *p_access )
714 if( ( p_block = BlockUDP( p_access ) ) == NULL )
717 if( p_block->p_buffer[0] == 0x47 )
719 msg_Dbg( p_access, "detected TS over raw UDP" );
720 p_access->pf_block = BlockUDP;
721 p_access->info.b_prebuffered = true;
725 if( p_block->i_buffer < RTP_HEADER_LEN )
728 /* Parse the header and make some verifications.
731 i_rtp_version = p_block->p_buffer[0] >> 6;
732 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
734 if( i_rtp_version != 2 )
736 msg_Dbg( p_access, "no supported RTP header detected" );
737 p_access->pf_block = BlockUDP;
738 p_access->info.b_prebuffered = true;
742 switch( i_payload_type )
745 msg_Dbg( p_access, "detected MPEG2 TS over RTP" );
746 free( p_access->psz_demux );
747 p_access->psz_demux = strdup( "ts" );
751 msg_Dbg( p_access, "detected MPEG Audio over RTP" );
752 free( p_access->psz_demux );
753 p_access->psz_demux = strdup( "mpga" );
757 msg_Dbg( p_access, "detected MPEG Video over RTP" );
758 free( p_access->psz_demux );
759 p_access->psz_demux = strdup( "mpgv" );
763 msg_Dbg( p_access, "no RTP header detected" );
764 p_access->pf_block = BlockUDP;
765 p_access->info.b_prebuffered = true;
769 p_access->pf_block = BlockRTP;
770 return BlockPrebufferRTP( p_access, p_block );