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" );
98 add_shortcut( "rtplite" );
100 set_callbacks( Open, Close );
103 /*****************************************************************************
105 *****************************************************************************/
106 #define RTP_HEADER_LEN 12
108 static block_t *BlockUDP( 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 );
118 vlc_bool_t b_auto_mtu;
120 /* reorder rtp packets when out-of-sequence */
122 uint16_t i_last_seqno;
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, cscov = 8;
141 if (strlen (p_access->psz_access) >= 3)
143 switch (p_access->psz_access[3])
153 if (strcmp (p_access->psz_access + 3, "lite") == 0)
154 proto = IPPROTO_UDPLITE;
156 if (strncmp (p_access->psz_access, "rtp", 3) == 0)
157 /* Checksum coverage: RTP header is AT LEAST 12 bytes
158 * in addition to UDP header (8 bytes) */
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_parser == '[' )
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 if( *psz_server_addr == '[' )
189 /* skips bracket'd IPv6 address */
190 psz_parser = strchr( psz_name, ']' );
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;
211 p_sys->fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
212 psz_server_addr, i_server_port, fam, proto );
214 if( p_sys->fd == -1 )
216 msg_Err( p_access, "cannot open socket" );
221 net_StopSend( p_sys->fd );
223 #ifdef UDPLITE_RECV_CSCOV
224 if (proto == IPPROTO_UDPLITE)
225 setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV, &cscov, sizeof (cscov));
229 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
230 if( p_sys->i_mtu <= 1 )
231 p_sys->i_mtu = 1500; /* Avoid problem */
233 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
235 /* Update default_pts to a suitable value for udp access */
236 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
238 /* RTP reordering for out-of-sequence packets */
239 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
240 p_sys->i_last_seqno = 0;
241 p_sys->p_list = NULL;
246 /*****************************************************************************
247 * Close: free unused data structures
248 *****************************************************************************/
249 static void Close( vlc_object_t *p_this )
251 access_t *p_access = (access_t*)p_this;
252 access_sys_t *p_sys = p_access->p_sys;
254 block_ChainRelease( p_sys->p_list );
255 net_Close( p_sys->fd );
259 /*****************************************************************************
261 *****************************************************************************/
262 static int Control( access_t *p_access, int i_query, va_list args )
264 access_sys_t *p_sys = p_access->p_sys;
272 case ACCESS_CAN_SEEK:
273 case ACCESS_CAN_FASTSEEK:
274 case ACCESS_CAN_PAUSE:
275 case ACCESS_CAN_CONTROL_PACE:
276 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
277 *pb_bool = VLC_FALSE;
281 pi_int = (int*)va_arg( args, int * );
282 *pi_int = p_sys->i_mtu;
285 case ACCESS_GET_PTS_DELAY:
286 pi_64 = (int64_t*)va_arg( args, int64_t * );
287 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
291 case ACCESS_SET_PAUSE_STATE:
292 case ACCESS_GET_TITLE_INFO:
293 case ACCESS_SET_TITLE:
294 case ACCESS_SET_SEEKPOINT:
295 case ACCESS_SET_PRIVATE_ID_STATE:
299 msg_Warn( p_access, "unimplemented query in control" );
306 /*****************************************************************************
308 *****************************************************************************/
309 static block_t *BlockUDP( access_t *p_access )
311 access_sys_t *p_sys = p_access->p_sys;
315 p_block = block_New( p_access, p_sys->i_mtu );
316 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
317 p_block->p_buffer, p_sys->i_mtu,
319 if( p_block->i_buffer <= 0 )
321 block_Release( p_block );
325 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
326 p_sys->i_mtu < 32767 )
328 /* Increase by 100% */
330 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
337 * rtp_ChainInsert - insert a p_block in the chain and
338 * look at the sequence numbers.
340 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
342 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
343 block_t *p_prev = NULL;
344 block_t *p = p_sys->p_end;
345 uint16_t i_new = (uint16_t) p_block->i_dts;
350 p_sys->p_list = p_block;
351 p_sys->p_end = p_block;
354 /* walk through the queue from top down since the new packet is in
355 most cases just appended to the end */
359 i_tmp = i_new - (uint16_t) p->i_dts;
361 if( !i_tmp ) /* trash duplicate */
365 { /* insert after this block ( i_new > p->i_dts ) */
366 p_block->p_next = p->p_next;
371 p_prev->p_prev = p_block;
372 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
373 (uint16_t) p->i_dts, i_new );
377 p_sys->p_end = p_block;
381 if( p == p_sys->p_list )
382 { /* we've reached bottom of chain */
383 i_tmp = p_sys->i_last_seqno - i_new;
384 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
386 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
387 i_new, (uint16_t) p->i_dts );
390 p_sys->p_list = p_block;
394 if( !i_tmp ) /* trash duplicate */
397 /* reordering failed - append the packet to the end of queue */
398 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
399 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
400 (uint16_t) p_sys->p_end->i_dts);
401 p_sys->p_end->p_next = p_block;
402 p_block->p_prev = p_sys->p_end;
403 p_sys->p_end = p_block;
409 block_Release( p_block );
413 /*****************************************************************************
414 * BlockParseRTP/BlockRTP:
415 *****************************************************************************/
416 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
422 int i_extension_flag = 0;
423 int i_extension_length = 0;
424 uint16_t i_sequence_number = 0;
426 if( p_block == NULL )
429 if( p_block->i_buffer < RTP_HEADER_LEN )
432 /* Parse the header and make some verifications.
434 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
435 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
436 i_extension_flag = p_block->p_buffer[0] & 0x10;
437 i_payload_type = p_block->p_buffer[1] & 0x7F;
438 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
440 if( i_rtp_version != 2 )
441 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
443 if( i_payload_type == 14 || i_payload_type == 32)
445 else if( i_payload_type != 33 )
446 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
447 if( i_extension_flag )
448 i_extension_length = 4 +
449 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
451 /* Skip header + CSRC extension field n*(32 bits) + extension */
452 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
454 if( i_skip >= p_block->i_buffer )
457 /* Return the packet without the RTP header, remember seqno in i_dts */
458 p_block->i_buffer -= i_skip;
459 p_block->p_buffer += i_skip;
460 p_block->i_pts = mdate();
461 p_block->i_dts = (mtime_t) i_sequence_number;
464 /* Emulate packet loss */
465 if ( (i_sequence_number % 4000) == 0)
467 msg_Warn( p_access, "Emulating packet drop" );
468 block_Release( p_block );
476 msg_Warn( p_access, "received a too short packet for RTP" );
477 block_Release( p_block );
481 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
483 access_sys_t *p_sys = p_access->p_sys;
484 mtime_t i_first = mdate();
486 block_t *p = p_block;
490 mtime_t i_date = mdate();
492 if( p && rtp_ChainInsert( p_access, p ))
495 /* Require at least 2 packets in the buffer */
496 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
499 p = BlockParseRTP( p_access, BlockUDP( p_access ));
500 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
502 msg_Err( p_access, "error in RTP prebuffering!" );
507 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
508 p_access->info.b_prebuffered = VLC_TRUE;
510 p_sys->p_list = p_sys->p_list->p_next;
511 p_sys->i_last_seqno = (uint16_t) p->i_dts;
516 static block_t *BlockRTP( access_t *p_access )
518 access_sys_t *p_sys = p_access->p_sys;
521 while ( !p_sys->p_list ||
522 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
524 p = BlockParseRTP( p_access, BlockUDP( p_access ));
529 rtp_ChainInsert( p_access, p );
533 p_sys->p_list = p_sys->p_list->p_next;
534 p_sys->i_last_seqno++;
535 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
537 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
538 p_sys->i_last_seqno, (uint16_t) p->i_dts );
539 p_sys->i_last_seqno = (uint16_t) p->i_dts;
545 /*****************************************************************************
546 * BlockChoose: decide between RTP and UDP
547 *****************************************************************************/
548 static block_t *BlockChoose( access_t *p_access )
555 if( ( p_block = BlockUDP( p_access ) ) == NULL )
558 if( p_block->p_buffer[0] == 0x47 )
560 msg_Dbg( p_access, "detected TS over raw UDP" );
561 p_access->pf_block = BlockUDP;
562 p_access->info.b_prebuffered = VLC_TRUE;
566 if( p_block->i_buffer < RTP_HEADER_LEN )
569 /* Parse the header and make some verifications.
572 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
573 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
574 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
576 if( i_rtp_version != 2 )
578 msg_Dbg( p_access, "no supported RTP header detected" );
579 p_access->pf_block = BlockUDP;
580 p_access->info.b_prebuffered = VLC_TRUE;
584 switch( i_payload_type )
587 msg_Dbg( p_access, "detected TS over RTP" );
588 p_access->psz_demux = strdup( "ts" );
592 msg_Dbg( p_access, "detected MPEG audio over RTP" );
593 p_access->psz_demux = strdup( "mpga" );
597 msg_Dbg( p_access, "detected MPEG video over RTP" );
598 p_access->psz_demux = strdup( "mpgv" );
602 msg_Dbg( p_access, "no RTP header detected" );
603 p_access->pf_block = BlockUDP;
604 p_access->info.b_prebuffered = VLC_TRUE;
608 if( !BlockParseRTP( p_access, p_block )) return NULL;
610 p_access->pf_block = BlockRTP;
612 return BlockPrebufferRTP( p_access, p_block );