1 /*****************************************************************************
2 * udp.c: raw UDP & RTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Tristan Leteurtre <tooney@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
10 * Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
29 /*****************************************************************************
31 *****************************************************************************/
35 #include <vlc_access.h>
36 #include <vlc_network.h>
38 #if defined (HAVE_NETINET_UDPLITE_H)
39 # include <netinet/udplite.h>
40 #elif defined (__linux__)
41 # define UDPLITE_SEND_CSCOV 10
42 # define UDPLITE_RECV_CSCOV 11
45 #ifndef IPPROTO_UDPLITE
46 # define IPPROTO_UDPLITE 136 /* from IANA */
49 # define SOL_UDPLITE IPPROTO_UDPLITE
53 /*****************************************************************************
55 *****************************************************************************/
56 #define CACHING_TEXT N_("Caching value in ms")
57 #define CACHING_LONGTEXT N_( \
58 "Caching value for UDP streams. This " \
59 "value should be set in milliseconds." )
61 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
62 #define AUTO_MTU_LONGTEXT N_( \
63 "Automatically detect the line's MTU. This will increase the size if" \
64 " truncated packets are found" )
66 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
67 #define RTP_LATE_LONGTEXT N_( \
68 "VLC reorders RTP packets. The input will wait for late packets at most "\
69 "the time specified here (in milliseconds)." )
71 static int Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
75 set_shortname( _("UDP/RTP" ) );
76 set_description( _("UDP/RTP input") );
77 set_category( CAT_INPUT );
78 set_subcategory( SUBCAT_INPUT_ACCESS );
80 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
81 CACHING_LONGTEXT, VLC_TRUE );
82 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
84 add_bool( "udp-auto-mtu", 1, NULL,
85 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
87 set_capability( "access2", 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( "rtplite" );
98 set_callbacks( Open, Close );
101 /*****************************************************************************
103 *****************************************************************************/
104 #define RTP_HEADER_LEN 12
106 static block_t *BlockUDP( access_t * );
107 static block_t *BlockRTP( access_t * );
108 static block_t *BlockChoose( access_t * );
109 static int Control( access_t *, int, va_list );
116 vlc_bool_t b_auto_mtu;
118 /* reorder rtp packets when out-of-sequence */
120 uint16_t i_last_seqno;
125 /*****************************************************************************
126 * Open: open the socket
127 *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
130 access_t *p_access = (access_t*)p_this;
133 char *psz_name = strdup( p_access->psz_path );
135 const char *psz_server_addr, *psz_bind_addr = "";
136 int i_bind_port, i_server_port = 0;
137 int fam = AF_UNSPEC, proto = IPPROTO_UDP, cscov = 8;
139 if (strlen (p_access->psz_access) >= 3)
141 switch (p_access->psz_access[3])
151 if (strcmp (p_access->psz_access + 3, "lite") == 0)
152 proto = IPPROTO_UDPLITE;
154 if (strncmp (p_access->psz_access, "rtp", 3) == 0)
155 /* Checksum coverage: RTP header is AT LEAST 12 bytes
156 * in addition to UDP header (8 bytes) */
159 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
161 /* Parse psz_name syntax :
162 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
163 psz_parser = strchr( psz_name, '@' );
164 if( psz_parser != NULL )
166 /* Found bind address and/or bind port */
167 *psz_parser++ = '\0';
168 psz_bind_addr = psz_parser;
170 if( *psz_parser == '[' )
171 /* skips bracket'd IPv6 address */
172 psz_parser = strchr( psz_parser, ']' );
174 if( psz_parser != NULL )
176 psz_parser = strchr( psz_parser, ':' );
177 if( psz_parser != NULL )
179 *psz_parser++ = '\0';
180 i_bind_port = atoi( psz_parser );
185 psz_server_addr = psz_name;
186 if( *psz_server_addr == '[' )
187 /* skips bracket'd IPv6 address */
188 psz_parser = strchr( psz_name, ']' );
190 if( psz_parser != NULL )
192 psz_parser = strchr( psz_parser, ':' );
193 if( psz_parser != NULL )
195 *psz_parser++ = '\0';
196 i_server_port = atoi( psz_parser );
200 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
201 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
203 /* Set up p_access */
204 access_InitFields( p_access );
205 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
206 p_access->info.b_prebuffered = VLC_FALSE;
207 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
209 p_sys->fd = net_Open( p_access, psz_bind_addr, i_bind_port,
210 psz_server_addr, i_server_port, fam, SOCK_DGRAM, proto );
212 if( p_sys->fd == -1 )
214 msg_Err( p_access, "cannot open socket" );
219 net_StopSend( p_sys->fd );
221 #ifdef UDPLITE_RECV_CSCOV
222 if (proto == IPPROTO_UDPLITE)
223 setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV, &cscov, sizeof (cscov));
227 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
228 if( p_sys->i_mtu <= 1 )
229 p_sys->i_mtu = 1500; /* Avoid problem */
231 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
233 /* Update default_pts to a suitable value for udp access */
234 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
236 /* RTP reordering for out-of-sequence packets */
237 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
238 p_sys->i_last_seqno = 0;
239 p_sys->p_list = NULL;
244 /*****************************************************************************
245 * Close: free unused data structures
246 *****************************************************************************/
247 static void Close( vlc_object_t *p_this )
249 access_t *p_access = (access_t*)p_this;
250 access_sys_t *p_sys = p_access->p_sys;
252 block_ChainRelease( p_sys->p_list );
253 net_Close( p_sys->fd );
257 /*****************************************************************************
259 *****************************************************************************/
260 static int Control( access_t *p_access, int i_query, va_list args )
262 access_sys_t *p_sys = p_access->p_sys;
270 case ACCESS_CAN_SEEK:
271 case ACCESS_CAN_FASTSEEK:
272 case ACCESS_CAN_PAUSE:
273 case ACCESS_CAN_CONTROL_PACE:
274 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
275 *pb_bool = VLC_FALSE;
279 pi_int = (int*)va_arg( args, int * );
280 *pi_int = p_sys->i_mtu;
283 case ACCESS_GET_PTS_DELAY:
284 pi_64 = (int64_t*)va_arg( args, int64_t * );
285 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
289 case ACCESS_SET_PAUSE_STATE:
290 case ACCESS_GET_TITLE_INFO:
291 case ACCESS_SET_TITLE:
292 case ACCESS_SET_SEEKPOINT:
293 case ACCESS_SET_PRIVATE_ID_STATE:
297 msg_Warn( p_access, "unimplemented query in control" );
304 /*****************************************************************************
306 *****************************************************************************/
307 static block_t *BlockUDP( access_t *p_access )
309 access_sys_t *p_sys = p_access->p_sys;
313 p_block = block_New( p_access, p_sys->i_mtu );
314 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
315 p_block->p_buffer, p_sys->i_mtu,
317 if( p_block->i_buffer <= 0 )
319 block_Release( p_block );
323 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
324 p_sys->i_mtu < 32767 )
326 /* Increase by 100% */
328 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
335 * rtp_ChainInsert - insert a p_block in the chain and
336 * look at the sequence numbers.
338 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
340 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
341 block_t *p_prev = NULL;
342 block_t *p = p_sys->p_end;
343 uint16_t i_new = (uint16_t) p_block->i_dts;
348 p_sys->p_list = p_block;
349 p_sys->p_end = p_block;
352 /* walk through the queue from top down since the new packet is in
353 most cases just appended to the end */
357 i_tmp = i_new - (uint16_t) p->i_dts;
359 if( !i_tmp ) /* trash duplicate */
363 { /* insert after this block ( i_new > p->i_dts ) */
364 p_block->p_next = p->p_next;
369 p_prev->p_prev = p_block;
370 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
371 (uint16_t) p->i_dts, i_new );
375 p_sys->p_end = p_block;
379 if( p == p_sys->p_list )
380 { /* we've reached bottom of chain */
381 i_tmp = p_sys->i_last_seqno - i_new;
382 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
384 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
385 i_new, (uint16_t) p->i_dts );
388 p_sys->p_list = p_block;
392 if( !i_tmp ) /* trash duplicate */
395 /* reordering failed - append the packet to the end of queue */
396 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
397 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
398 (uint16_t) p_sys->p_end->i_dts);
399 p_sys->p_end->p_next = p_block;
400 p_block->p_prev = p_sys->p_end;
401 p_sys->p_end = p_block;
407 block_Release( p_block );
411 /*****************************************************************************
412 * BlockParseRTP/BlockRTP:
413 *****************************************************************************/
414 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
420 int i_extension_flag = 0;
421 int i_extension_length = 0;
422 uint16_t i_sequence_number = 0;
424 if( p_block == NULL )
427 if( p_block->i_buffer < RTP_HEADER_LEN )
430 /* Parse the header and make some verifications.
432 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
433 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
434 i_extension_flag = p_block->p_buffer[0] & 0x10;
435 i_payload_type = p_block->p_buffer[1] & 0x7F;
436 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
438 if( i_rtp_version != 2 )
439 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
441 if( i_payload_type == 14 || i_payload_type == 32)
443 else if( i_payload_type != 33 )
444 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
445 if( i_extension_flag )
446 i_extension_length = 4 +
447 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
449 /* Skip header + CSRC extension field n*(32 bits) + extension */
450 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
452 if( i_skip >= p_block->i_buffer )
455 /* Return the packet without the RTP header, remember seqno in i_dts */
456 p_block->i_buffer -= i_skip;
457 p_block->p_buffer += i_skip;
458 p_block->i_pts = mdate();
459 p_block->i_dts = (mtime_t) i_sequence_number;
462 /* Emulate packet loss */
463 if ( (i_sequence_number % 4000) == 0)
465 msg_Warn( p_access, "Emulating packet drop" );
466 block_Release( p_block );
474 msg_Warn( p_access, "received a too short packet for RTP" );
475 block_Release( p_block );
479 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
481 access_sys_t *p_sys = p_access->p_sys;
482 mtime_t i_first = mdate();
484 block_t *p = p_block;
488 mtime_t i_date = mdate();
490 if( p && rtp_ChainInsert( p_access, p ))
493 /* Require at least 2 packets in the buffer */
494 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
497 p = BlockParseRTP( p_access, BlockUDP( p_access ));
498 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
500 msg_Err( p_access, "error in RTP prebuffering!" );
505 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
506 p_access->info.b_prebuffered = VLC_TRUE;
508 p_sys->p_list = p_sys->p_list->p_next;
509 p_sys->i_last_seqno = (uint16_t) p->i_dts;
514 static block_t *BlockRTP( access_t *p_access )
516 access_sys_t *p_sys = p_access->p_sys;
519 while ( !p_sys->p_list ||
520 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
522 p = BlockParseRTP( p_access, BlockUDP( p_access ));
527 rtp_ChainInsert( p_access, p );
531 p_sys->p_list = p_sys->p_list->p_next;
532 p_sys->i_last_seqno++;
533 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
535 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
536 p_sys->i_last_seqno, (uint16_t) p->i_dts );
537 p_sys->i_last_seqno = (uint16_t) p->i_dts;
543 /*****************************************************************************
544 * BlockChoose: decide between RTP and UDP
545 *****************************************************************************/
546 static block_t *BlockChoose( access_t *p_access )
553 if( ( p_block = BlockUDP( p_access ) ) == NULL )
556 if( p_block->p_buffer[0] == 0x47 )
558 msg_Dbg( p_access, "detected TS over raw UDP" );
559 p_access->pf_block = BlockUDP;
560 p_access->info.b_prebuffered = VLC_TRUE;
564 if( p_block->i_buffer < RTP_HEADER_LEN )
567 /* Parse the header and make some verifications.
570 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
571 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
572 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
574 if( i_rtp_version != 2 )
576 msg_Dbg( p_access, "no supported RTP header detected" );
577 p_access->pf_block = BlockUDP;
578 p_access->info.b_prebuffered = VLC_TRUE;
582 switch( i_payload_type )
585 msg_Dbg( p_access, "detected TS over RTP" );
586 p_access->psz_demux = strdup( "ts" );
590 msg_Dbg( p_access, "detected MPEG audio over RTP" );
591 p_access->psz_demux = strdup( "mpga" );
595 msg_Dbg( p_access, "detected MPEG video over RTP" );
596 p_access->psz_demux = strdup( "mpgv" );
600 msg_Dbg( p_access, "no RTP header detected" );
601 p_access->pf_block = BlockUDP;
602 p_access->info.b_prebuffered = VLC_TRUE;
606 if( !BlockParseRTP( p_access, p_block )) return NULL;
608 p_access->pf_block = BlockRTP;
610 return BlockPrebufferRTP( p_access, p_block );