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 *****************************************************************************/
37 #include <vlc_access.h>
38 #include <vlc_network.h>
40 /* Big pile of stuff missing form glibc 2.5 */
41 #if defined (HAVE_NETINET_UDPLITE_H)
42 # include <netinet/udplite.h>
43 #elif defined (__linux__)
44 # define UDPLITE_SEND_CSCOV 10
45 # define UDPLITE_RECV_CSCOV 11
48 #ifndef SOCK_DCCP /* provisional API */
55 # define IPPROTO_DCCP 33 /* IANA */
58 #ifndef IPPROTO_UDPLITE
59 # define IPPROTO_UDPLITE 136 /* from IANA */
62 # define SOL_UDPLITE IPPROTO_UDPLITE
66 /*****************************************************************************
68 *****************************************************************************/
69 #define CACHING_TEXT N_("Caching value in ms")
70 #define CACHING_LONGTEXT N_( \
71 "Caching value for UDP streams. This " \
72 "value should be set in milliseconds." )
74 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
75 #define AUTO_MTU_LONGTEXT N_( \
76 "Automatically detect the line's MTU. This will increase the size if" \
77 " truncated packets are found" )
79 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
80 #define RTP_LATE_LONGTEXT N_( \
81 "VLC reorders RTP packets. The input will wait for late packets at most "\
82 "the time specified here (in milliseconds)." )
84 static int Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
88 set_shortname( _("UDP/RTP" ) );
89 set_description( _("UDP/RTP input") );
90 set_category( CAT_INPUT );
91 set_subcategory( SUBCAT_INPUT_ACCESS );
93 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
94 CACHING_LONGTEXT, VLC_TRUE );
95 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
97 add_bool( "udp-auto-mtu", 1, NULL,
98 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
100 set_capability( "access2", 0 );
101 add_shortcut( "udp" );
102 add_shortcut( "udpstream" );
103 add_shortcut( "udp4" );
104 add_shortcut( "udp6" );
105 add_shortcut( "rtp" );
106 add_shortcut( "rtp4" );
107 add_shortcut( "rtp6" );
108 add_shortcut( "udplite" );
109 add_shortcut( "rtptcp" );
110 add_shortcut( "dccp" );
112 set_callbacks( Open, Close );
115 /*****************************************************************************
117 *****************************************************************************/
118 #define RTP_HEADER_LEN 12
120 static block_t *BlockUDP( access_t * );
121 static block_t *BlockTCP( access_t * );
122 static block_t *BlockRTP( access_t * );
123 static block_t *BlockChoose( access_t * );
124 static int Control( access_t *, int, va_list );
131 vlc_bool_t b_auto_mtu;
132 vlc_bool_t b_framed_rtp;
134 /* reorder rtp packets when out-of-sequence */
135 uint16_t i_last_seqno;
139 block_t *p_partial_frame; /* Partial Framed RTP packet */
142 /*****************************************************************************
143 * Open: open the socket
144 *****************************************************************************/
145 static int Open( vlc_object_t *p_this )
147 access_t *p_access = (access_t*)p_this;
150 char *psz_name = strdup( p_access->psz_path );
152 const char *psz_server_addr, *psz_bind_addr = "";
153 int i_bind_port, i_server_port = 0;
154 int fam = AF_UNSPEC, proto = IPPROTO_UDP;
156 if (strlen (p_access->psz_access) >= 3)
158 switch (p_access->psz_access[3])
168 if (strcmp (p_access->psz_access + 3, "lite") == 0)
169 proto = IPPROTO_UDPLITE;
171 if (strcmp (p_access->psz_access + 3, "tcp") == 0)
174 if (strcmp (p_access->psz_access, "dccp") == 0)
175 proto = IPPROTO_DCCP;
178 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
180 /* Parse psz_name syntax :
181 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
182 psz_parser = strchr( psz_name, '@' );
183 if( psz_parser != NULL )
185 /* Found bind address and/or bind port */
186 *psz_parser++ = '\0';
187 psz_bind_addr = psz_parser;
189 if( *psz_parser == '[' )
190 /* skips bracket'd IPv6 address */
191 psz_parser = strchr( psz_parser, ']' );
193 if( psz_parser != NULL )
195 psz_parser = strchr( psz_parser, ':' );
196 if( psz_parser != NULL )
198 *psz_parser++ = '\0';
199 i_bind_port = atoi( psz_parser );
204 psz_server_addr = psz_name;
205 if( *psz_server_addr == '[' )
206 /* skips bracket'd IPv6 address */
207 psz_parser = strchr( psz_name, ']' );
209 if( psz_parser != NULL )
211 psz_parser = strchr( psz_parser, ':' );
212 if( psz_parser != NULL )
214 *psz_parser++ = '\0';
215 i_server_port = atoi( psz_parser );
219 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
220 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
222 /* Set up p_access */
223 access_InitFields( p_access );
224 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
225 p_access->info.b_prebuffered = VLC_FALSE;
226 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
231 case IPPROTO_UDPLITE:
232 p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
233 psz_server_addr, i_server_port, fam,
238 p_sys = net_ConnectTCP( p_access, psz_server_addr, i_server_port );
239 p_sys->b_framed_rtp = VLC_TRUE;
244 p_sys->fd = net_Connect( p_access, psz_server_addr, i_server_port,
245 SOCK_DCCP, IPPROTO_DCCP );
248 msg_Err( p_access, "DCCP support not compiled-in!" );
253 if( p_sys->fd == -1 )
255 msg_Err( p_access, "cannot open socket" );
260 net_StopSend( p_sys->fd );
262 #ifdef UDPLITE_RECV_CSCOV
263 if (proto == IPPROTO_UDPLITE)
264 /* UDP header: 8 bytes + RTP header: 12 bytes (or more) */
265 setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
266 &(int){ 20 }, sizeof (int));
269 if (p_sys->b_framed_rtp)
271 /* We don't do autodetection and prebuffering in case of framing */
272 p_access->pf_block = BlockRTP;
273 p_sys->i_mtu = 65535;
278 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
279 if( p_sys->i_mtu <= 1 )
280 p_sys->i_mtu = 1500; /* Avoid problem */
282 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
285 /* Update default_pts to a suitable value for udp access */
286 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
288 /* RTP reordering for out-of-sequence packets */
289 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
290 p_sys->i_last_seqno = 0;
291 p_sys->p_list = NULL;
296 /*****************************************************************************
297 * Close: free unused data structures
298 *****************************************************************************/
299 static void Close( vlc_object_t *p_this )
301 access_t *p_access = (access_t*)p_this;
302 access_sys_t *p_sys = p_access->p_sys;
304 block_ChainRelease( p_sys->p_list );
305 net_Close( p_sys->fd );
309 /*****************************************************************************
311 *****************************************************************************/
312 static int Control( access_t *p_access, int i_query, va_list args )
314 access_sys_t *p_sys = p_access->p_sys;
322 case ACCESS_CAN_SEEK:
323 case ACCESS_CAN_FASTSEEK:
324 case ACCESS_CAN_PAUSE:
325 case ACCESS_CAN_CONTROL_PACE:
326 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
327 *pb_bool = VLC_FALSE;
331 pi_int = (int*)va_arg( args, int * );
332 *pi_int = p_sys->i_mtu;
335 case ACCESS_GET_PTS_DELAY:
336 pi_64 = (int64_t*)va_arg( args, int64_t * );
337 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
341 case ACCESS_SET_PAUSE_STATE:
342 case ACCESS_GET_TITLE_INFO:
343 case ACCESS_SET_TITLE:
344 case ACCESS_SET_SEEKPOINT:
345 case ACCESS_SET_PRIVATE_ID_STATE:
349 msg_Warn( p_access, "unimplemented query in control" );
356 /*****************************************************************************
358 *****************************************************************************/
359 static block_t *BlockUDP( access_t *p_access )
361 access_sys_t *p_sys = p_access->p_sys;
365 p_block = block_New( p_access, p_sys->i_mtu );
366 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
367 p_block->p_buffer, p_sys->i_mtu,
369 if( p_block->i_buffer < 0 )
371 block_Release( p_block );
375 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
376 p_sys->i_mtu < 32767 )
378 /* Increase by 100% */
380 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
386 /*****************************************************************************
387 * BlockTCP: Framed RTP/AVP packet reception for COMEDIA
388 * Still an I-D (draft-ietf-avt-rtp-framing-contrans-06) - subject to change.
389 *****************************************************************************/
390 static block_t *BlockTCP( access_t *p_access )
392 access_sys_t *p_sys = p_access->p_sys;
393 block_t *p_block = p_sys->p_partial_frame;
395 if( p_access->info.b_eof )
398 if( p_block == NULL )
400 /* MTU should always be 65535 in this case */
401 p_sys->p_partial_frame = p_block = block_New( p_access, 65537 );
406 /* Read RTP framing */
407 if (p_block->i_buffer < 2)
409 /* FIXME: not very efficient */
410 int i_read = net_Read( p_access, p_sys->fd, NULL,
411 p_block->p_buffer + p_block->i_buffer,
412 2 - p_block->i_buffer, VLC_FALSE );
416 p_block->i_buffer += i_read;
417 if (p_block->i_buffer < 2)
421 uint16_t framelen = GetWLE( p_block->p_buffer );
425 int i_read = net_Read( p_access, p_sys->fd, NULL,
426 p_block->p_buffer + p_block->i_buffer,
427 2 + framelen - p_block->i_buffer, VLC_FALSE );
431 p_block->i_buffer += i_read;
434 if( p_block->i_buffer < (2 + framelen) )
435 return NULL; // incomplete frame
437 /* Hide framing from RTP layer */
438 p_block->p_buffer += 2;
439 p_block->i_buffer -= 2;
440 p_sys->p_partial_frame = NULL;
444 p_access->info.b_eof = VLC_TRUE;
445 block_Release( p_block );
446 p_sys->p_partial_frame = NULL;
452 * rtp_ChainInsert - insert a p_block in the chain and
453 * look at the sequence numbers.
455 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
457 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
458 block_t *p_prev = NULL;
459 block_t *p = p_sys->p_end;
460 uint16_t i_new = (uint16_t) p_block->i_dts;
465 p_sys->p_list = p_block;
466 p_sys->p_end = p_block;
469 /* walk through the queue from top down since the new packet is in
470 most cases just appended to the end */
474 i_tmp = i_new - (uint16_t) p->i_dts;
476 if( !i_tmp ) /* trash duplicate */
480 { /* insert after this block ( i_new > p->i_dts ) */
481 p_block->p_next = p->p_next;
486 p_prev->p_prev = p_block;
487 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
488 (uint16_t) p->i_dts, i_new );
492 p_sys->p_end = p_block;
496 if( p == p_sys->p_list )
497 { /* we've reached bottom of chain */
498 i_tmp = p_sys->i_last_seqno - i_new;
499 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
501 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
502 i_new, (uint16_t) p->i_dts );
505 p_sys->p_list = p_block;
509 if( !i_tmp ) /* trash duplicate */
512 /* reordering failed - append the packet to the end of queue */
513 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
514 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
515 (uint16_t) p_sys->p_end->i_dts);
516 p_sys->p_end->p_next = p_block;
517 p_block->p_prev = p_sys->p_end;
518 p_sys->p_end = p_block;
524 block_Release( p_block );
528 /*****************************************************************************
529 * BlockParseRTP/BlockRTP:
530 *****************************************************************************/
531 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
537 int i_extension_flag = 0;
538 int i_extension_length = 0;
539 uint16_t i_sequence_number = 0;
541 if( p_block == NULL )
544 if( p_block->i_buffer < RTP_HEADER_LEN )
547 /* Parse the header and make some verifications.
549 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
550 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
551 i_extension_flag = p_block->p_buffer[0] & 0x10;
552 i_payload_type = p_block->p_buffer[1] & 0x7F;
553 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
555 if( i_rtp_version != 2 )
556 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
558 if( i_payload_type == 14 || i_payload_type == 32)
560 else if( i_payload_type != 33 )
561 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
562 if( i_extension_flag )
564 if( p_block->i_buffer < 16 )
566 i_extension_length = 4 +
567 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
570 /* Skip header + CSRC extension field n*(32 bits) + extension */
571 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
573 if( i_skip >= p_block->i_buffer )
576 /* Return the packet without the RTP header, remember seqno in i_dts */
577 p_block->i_buffer -= i_skip;
578 p_block->p_buffer += i_skip;
579 p_block->i_pts = mdate();
580 p_block->i_dts = (mtime_t) i_sequence_number;
583 /* Emulate packet loss */
584 if ( (i_sequence_number % 4000) == 0)
586 msg_Warn( p_access, "Emulating packet drop" );
587 block_Release( p_block );
595 msg_Warn( p_access, "received a too short packet for RTP" );
596 block_Release( p_block );
600 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
602 access_sys_t *p_sys = p_access->p_sys;
603 mtime_t i_first = mdate();
605 block_t *p = p_block;
609 mtime_t i_date = mdate();
611 if( p && rtp_ChainInsert( p_access, p ))
614 /* Require at least 2 packets in the buffer */
615 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
618 p = BlockParseRTP( p_access, BlockUDP( p_access ) );
619 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
621 msg_Err( p_access, "error in RTP prebuffering!" );
626 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
627 p_access->info.b_prebuffered = VLC_TRUE;
629 p_sys->p_list = p_sys->p_list->p_next;
630 p_sys->i_last_seqno = (uint16_t) p->i_dts;
635 static block_t *BlockRTP( access_t *p_access )
637 access_sys_t *p_sys = p_access->p_sys;
640 while ( !p_sys->p_list ||
641 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
643 p = BlockParseRTP( p_access,
644 p_sys->b_framed_rtp ? BlockTCP( p_access )
645 : BlockUDP( p_access ) );
649 rtp_ChainInsert( p_access, p );
653 p_sys->p_list = p_sys->p_list->p_next;
654 p_sys->i_last_seqno++;
655 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
657 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
658 p_sys->i_last_seqno, (uint16_t) p->i_dts );
659 p_sys->i_last_seqno = (uint16_t) p->i_dts;
665 /*****************************************************************************
666 * BlockChoose: decide between RTP and UDP
667 *****************************************************************************/
668 static block_t *BlockChoose( access_t *p_access )
675 if( ( p_block = BlockUDP( p_access ) ) == NULL )
678 if( p_block->p_buffer[0] == 0x47 )
680 msg_Dbg( p_access, "detected TS over raw UDP" );
681 p_access->pf_block = BlockUDP;
682 p_access->info.b_prebuffered = VLC_TRUE;
686 if( p_block->i_buffer < RTP_HEADER_LEN )
689 /* Parse the header and make some verifications.
692 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
693 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
694 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
696 if( i_rtp_version != 2 )
698 msg_Dbg( p_access, "no supported RTP header detected" );
699 p_access->pf_block = BlockUDP;
700 p_access->info.b_prebuffered = VLC_TRUE;
704 switch( i_payload_type )
707 msg_Dbg( p_access, "detected TS over RTP" );
708 p_access->psz_demux = strdup( "ts" );
712 msg_Dbg( p_access, "detected MPEG audio over RTP" );
713 p_access->psz_demux = strdup( "mpga" );
717 msg_Dbg( p_access, "detected MPEG video over RTP" );
718 p_access->psz_demux = strdup( "mpgv" );
722 msg_Dbg( p_access, "no RTP header detected" );
723 p_access->pf_block = BlockUDP;
724 p_access->info.b_prebuffered = VLC_TRUE;
728 if( !BlockParseRTP( p_access, p_block )) return NULL;
730 p_access->pf_block = BlockRTP;
732 return BlockPrebufferRTP( p_access, p_block );