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 #if defined (HAVE_NETINET_UDPLITE_H)
41 # include <netinet/udplite.h>
42 #elif defined (__linux__)
43 # define UDPLITE_SEND_CSCOV 10
44 # define UDPLITE_RECV_CSCOV 11
47 #ifndef IPPROTO_UDPLITE
48 # define IPPROTO_UDPLITE 136 /* from IANA */
51 # define SOL_UDPLITE IPPROTO_UDPLITE
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 AUTO_MTU_TEXT N_("Autodetection of MTU")
64 #define AUTO_MTU_LONGTEXT N_( \
65 "Automatically detect the line's MTU. This will increase the size if" \
66 " truncated packets are found" )
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( _("UDP/RTP" ) );
78 set_description( _("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, VLC_TRUE );
84 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
86 add_bool( "udp-auto-mtu", 1, NULL,
87 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
89 set_capability( "access2", 0 );
90 add_shortcut( "udp" );
91 add_shortcut( "udpstream" );
92 add_shortcut( "udp4" );
93 add_shortcut( "udp6" );
94 add_shortcut( "rtp" );
95 add_shortcut( "rtp4" );
96 add_shortcut( "rtp6" );
97 add_shortcut( "udplite" );
99 set_callbacks( Open, Close );
102 /*****************************************************************************
104 *****************************************************************************/
105 #define RTP_HEADER_LEN 12
107 static block_t *BlockUDP( 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 );
117 vlc_bool_t b_auto_mtu;
119 /* reorder rtp packets when out-of-sequence */
121 uint16_t i_last_seqno;
126 /*****************************************************************************
127 * Open: open the socket
128 *****************************************************************************/
129 static int Open( vlc_object_t *p_this )
131 access_t *p_access = (access_t*)p_this;
134 char *psz_name = strdup( p_access->psz_path );
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;
140 if (strlen (p_access->psz_access) >= 3)
142 switch (p_access->psz_access[3])
152 if (strcmp (p_access->psz_access + 3, "lite") == 0)
153 proto = IPPROTO_UDPLITE;
156 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
158 /* Parse psz_name syntax :
159 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
160 psz_parser = strchr( psz_name, '@' );
161 if( psz_parser != NULL )
163 /* Found bind address and/or bind port */
164 *psz_parser++ = '\0';
165 psz_bind_addr = psz_parser;
167 if( *psz_parser == '[' )
168 /* skips bracket'd IPv6 address */
169 psz_parser = strchr( psz_parser, ']' );
171 if( psz_parser != NULL )
173 psz_parser = strchr( psz_parser, ':' );
174 if( psz_parser != NULL )
176 *psz_parser++ = '\0';
177 i_bind_port = atoi( psz_parser );
182 psz_server_addr = psz_name;
183 if( *psz_server_addr == '[' )
184 /* skips bracket'd IPv6 address */
185 psz_parser = strchr( psz_name, ']' );
187 if( psz_parser != NULL )
189 psz_parser = strchr( psz_parser, ':' );
190 if( psz_parser != NULL )
192 *psz_parser++ = '\0';
193 i_server_port = atoi( psz_parser );
197 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
198 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
200 /* Set up p_access */
201 access_InitFields( p_access );
202 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
203 p_access->info.b_prebuffered = VLC_FALSE;
204 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
206 p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
207 psz_server_addr, i_server_port, fam, proto );
209 if( p_sys->fd == -1 )
211 msg_Err( p_access, "cannot open socket" );
216 net_StopSend( p_sys->fd );
218 #ifdef UDPLITE_RECV_CSCOV
219 if (proto == IPPROTO_UDPLITE)
220 /* UDP header: 8 bytes + RTP header: 12 bytes (or more) */
221 setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV,
222 &(int){ 20 }, sizeof (int));
226 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
227 if( p_sys->i_mtu <= 1 )
228 p_sys->i_mtu = 1500; /* Avoid problem */
230 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
232 /* Update default_pts to a suitable value for udp access */
233 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
235 /* RTP reordering for out-of-sequence packets */
236 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
237 p_sys->i_last_seqno = 0;
238 p_sys->p_list = NULL;
243 /*****************************************************************************
244 * Close: free unused data structures
245 *****************************************************************************/
246 static void Close( vlc_object_t *p_this )
248 access_t *p_access = (access_t*)p_this;
249 access_sys_t *p_sys = p_access->p_sys;
251 block_ChainRelease( p_sys->p_list );
252 net_Close( p_sys->fd );
256 /*****************************************************************************
258 *****************************************************************************/
259 static int Control( access_t *p_access, int i_query, va_list args )
261 access_sys_t *p_sys = p_access->p_sys;
269 case ACCESS_CAN_SEEK:
270 case ACCESS_CAN_FASTSEEK:
271 case ACCESS_CAN_PAUSE:
272 case ACCESS_CAN_CONTROL_PACE:
273 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
274 *pb_bool = VLC_FALSE;
278 pi_int = (int*)va_arg( args, int * );
279 *pi_int = p_sys->i_mtu;
282 case ACCESS_GET_PTS_DELAY:
283 pi_64 = (int64_t*)va_arg( args, int64_t * );
284 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
288 case ACCESS_SET_PAUSE_STATE:
289 case ACCESS_GET_TITLE_INFO:
290 case ACCESS_SET_TITLE:
291 case ACCESS_SET_SEEKPOINT:
292 case ACCESS_SET_PRIVATE_ID_STATE:
296 msg_Warn( p_access, "unimplemented query in control" );
303 /*****************************************************************************
305 *****************************************************************************/
306 static block_t *BlockUDP( access_t *p_access )
308 access_sys_t *p_sys = p_access->p_sys;
312 p_block = block_New( p_access, p_sys->i_mtu );
313 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
314 p_block->p_buffer, p_sys->i_mtu,
316 if( p_block->i_buffer <= 0 )
318 block_Release( p_block );
322 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
323 p_sys->i_mtu < 32767 )
325 /* Increase by 100% */
327 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
334 * rtp_ChainInsert - insert a p_block in the chain and
335 * look at the sequence numbers.
337 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
339 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
340 block_t *p_prev = NULL;
341 block_t *p = p_sys->p_end;
342 uint16_t i_new = (uint16_t) p_block->i_dts;
347 p_sys->p_list = p_block;
348 p_sys->p_end = p_block;
351 /* walk through the queue from top down since the new packet is in
352 most cases just appended to the end */
356 i_tmp = i_new - (uint16_t) p->i_dts;
358 if( !i_tmp ) /* trash duplicate */
362 { /* insert after this block ( i_new > p->i_dts ) */
363 p_block->p_next = p->p_next;
368 p_prev->p_prev = p_block;
369 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
370 (uint16_t) p->i_dts, i_new );
374 p_sys->p_end = p_block;
378 if( p == p_sys->p_list )
379 { /* we've reached bottom of chain */
380 i_tmp = p_sys->i_last_seqno - i_new;
381 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
383 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
384 i_new, (uint16_t) p->i_dts );
387 p_sys->p_list = p_block;
391 if( !i_tmp ) /* trash duplicate */
394 /* reordering failed - append the packet to the end of queue */
395 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
396 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
397 (uint16_t) p_sys->p_end->i_dts);
398 p_sys->p_end->p_next = p_block;
399 p_block->p_prev = p_sys->p_end;
400 p_sys->p_end = p_block;
406 block_Release( p_block );
410 /*****************************************************************************
411 * BlockParseRTP/BlockRTP:
412 *****************************************************************************/
413 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
419 int i_extension_flag = 0;
420 int i_extension_length = 0;
421 uint16_t i_sequence_number = 0;
423 if( p_block == NULL )
426 if( p_block->i_buffer < RTP_HEADER_LEN )
429 /* Parse the header and make some verifications.
431 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
432 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
433 i_extension_flag = p_block->p_buffer[0] & 0x10;
434 i_payload_type = p_block->p_buffer[1] & 0x7F;
435 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
437 if( i_rtp_version != 2 )
438 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
440 if( i_payload_type == 14 || i_payload_type == 32)
442 else if( i_payload_type != 33 )
443 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
444 if( i_extension_flag )
446 if( p_block->i_buffer < 16 )
448 i_extension_length = 4 +
449 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
452 /* Skip header + CSRC extension field n*(32 bits) + extension */
453 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
455 if( i_skip >= p_block->i_buffer )
458 /* Return the packet without the RTP header, remember seqno in i_dts */
459 p_block->i_buffer -= i_skip;
460 p_block->p_buffer += i_skip;
461 p_block->i_pts = mdate();
462 p_block->i_dts = (mtime_t) i_sequence_number;
465 /* Emulate packet loss */
466 if ( (i_sequence_number % 4000) == 0)
468 msg_Warn( p_access, "Emulating packet drop" );
469 block_Release( p_block );
477 msg_Warn( p_access, "received a too short packet for RTP" );
478 block_Release( p_block );
482 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
484 access_sys_t *p_sys = p_access->p_sys;
485 mtime_t i_first = mdate();
487 block_t *p = p_block;
491 mtime_t i_date = mdate();
493 if( p && rtp_ChainInsert( p_access, p ))
496 /* Require at least 2 packets in the buffer */
497 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
500 p = BlockParseRTP( p_access, BlockUDP( p_access ));
501 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
503 msg_Err( p_access, "error in RTP prebuffering!" );
508 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
509 p_access->info.b_prebuffered = VLC_TRUE;
511 p_sys->p_list = p_sys->p_list->p_next;
512 p_sys->i_last_seqno = (uint16_t) p->i_dts;
517 static block_t *BlockRTP( access_t *p_access )
519 access_sys_t *p_sys = p_access->p_sys;
522 while ( !p_sys->p_list ||
523 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
525 p = BlockParseRTP( p_access, BlockUDP( p_access ));
530 rtp_ChainInsert( p_access, p );
534 p_sys->p_list = p_sys->p_list->p_next;
535 p_sys->i_last_seqno++;
536 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
538 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
539 p_sys->i_last_seqno, (uint16_t) p->i_dts );
540 p_sys->i_last_seqno = (uint16_t) p->i_dts;
546 /*****************************************************************************
547 * BlockChoose: decide between RTP and UDP
548 *****************************************************************************/
549 static block_t *BlockChoose( access_t *p_access )
556 if( ( p_block = BlockUDP( p_access ) ) == NULL )
559 if( p_block->p_buffer[0] == 0x47 )
561 msg_Dbg( p_access, "detected TS over raw UDP" );
562 p_access->pf_block = BlockUDP;
563 p_access->info.b_prebuffered = VLC_TRUE;
567 if( p_block->i_buffer < RTP_HEADER_LEN )
570 /* Parse the header and make some verifications.
573 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
574 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
575 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
577 if( i_rtp_version != 2 )
579 msg_Dbg( p_access, "no supported RTP header detected" );
580 p_access->pf_block = BlockUDP;
581 p_access->info.b_prebuffered = VLC_TRUE;
585 switch( i_payload_type )
588 msg_Dbg( p_access, "detected TS over RTP" );
589 p_access->psz_demux = strdup( "ts" );
593 msg_Dbg( p_access, "detected MPEG audio over RTP" );
594 p_access->psz_demux = strdup( "mpga" );
598 msg_Dbg( p_access, "detected MPEG video over RTP" );
599 p_access->psz_demux = strdup( "mpgv" );
603 msg_Dbg( p_access, "no RTP header detected" );
604 p_access->pf_block = BlockUDP;
605 p_access->info.b_prebuffered = VLC_TRUE;
609 if( !BlockParseRTP( p_access, p_block )) return NULL;
611 p_access->pf_block = BlockRTP;
613 return BlockPrebufferRTP( p_access, p_block );