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) >= 4)
140 switch (p_access->psz_access[3])
150 if (strcmp (p_access->psz_access + 3, "lite") == 0)
151 proto = IPPROTO_UDPLITE;
152 if (strncmp (p_access->psz_access, "rtp", 3) == 0)
153 /* Checksum coverage: RTP header is AT LEAST 12 bytes
154 * in addition to UDP header (8 bytes) */
157 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
159 /* Parse psz_name syntax :
160 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
161 psz_parser = strchr( psz_name, '@' );
162 if( psz_parser != NULL )
164 /* Found bind address and/or bind port */
165 *psz_parser++ = '\0';
166 psz_bind_addr = psz_parser;
168 if( *psz_parser == '[' )
169 /* skips bracket'd IPv6 address */
170 psz_parser = strchr( psz_parser, ']' );
172 if( psz_parser != NULL )
174 psz_parser = strchr( psz_parser, ':' );
175 if( psz_parser != NULL )
177 *psz_parser++ = '\0';
178 i_bind_port = atoi( psz_parser );
183 psz_server_addr = psz_name;
184 if( *psz_server_addr == '[' )
185 /* skips bracket'd IPv6 address */
186 psz_parser = strchr( psz_name, ']' );
188 if( psz_parser != NULL )
190 psz_parser = strchr( psz_parser, ':' );
191 if( psz_parser != NULL )
193 *psz_parser++ = '\0';
194 i_server_port = atoi( psz_parser );
198 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
199 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
201 /* Set up p_access */
202 access_InitFields( p_access );
203 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
204 p_access->info.b_prebuffered = VLC_FALSE;
205 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
207 p_sys->fd = net_Open( p_access, psz_bind_addr, i_bind_port,
208 psz_server_addr, i_server_port, fam, SOCK_DGRAM, proto );
210 if( p_sys->fd == -1 )
212 msg_Err( p_access, "cannot open socket" );
217 net_StopSend( p_sys->fd );
219 #ifdef UDPLITE_RECV_CSCOV
220 if (proto == IPPROTO_UDPLITE)
221 setsockopt (p_sys->fd, SOL_UDPLITE, UDPLITE_RECV_CSCOV, &cscov, sizeof (cscov));
225 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
226 if( p_sys->i_mtu <= 1 )
227 p_sys->i_mtu = 1500; /* Avoid problem */
229 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
231 /* Update default_pts to a suitable value for udp access */
232 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
234 /* RTP reordering for out-of-sequence packets */
235 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
236 p_sys->i_last_seqno = 0;
237 p_sys->p_list = NULL;
242 /*****************************************************************************
243 * Close: free unused data structures
244 *****************************************************************************/
245 static void Close( vlc_object_t *p_this )
247 access_t *p_access = (access_t*)p_this;
248 access_sys_t *p_sys = p_access->p_sys;
250 block_ChainRelease( p_sys->p_list );
251 net_Close( p_sys->fd );
255 /*****************************************************************************
257 *****************************************************************************/
258 static int Control( access_t *p_access, int i_query, va_list args )
260 access_sys_t *p_sys = p_access->p_sys;
268 case ACCESS_CAN_SEEK:
269 case ACCESS_CAN_FASTSEEK:
270 case ACCESS_CAN_PAUSE:
271 case ACCESS_CAN_CONTROL_PACE:
272 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
273 *pb_bool = VLC_FALSE;
277 pi_int = (int*)va_arg( args, int * );
278 *pi_int = p_sys->i_mtu;
281 case ACCESS_GET_PTS_DELAY:
282 pi_64 = (int64_t*)va_arg( args, int64_t * );
283 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
287 case ACCESS_SET_PAUSE_STATE:
288 case ACCESS_GET_TITLE_INFO:
289 case ACCESS_SET_TITLE:
290 case ACCESS_SET_SEEKPOINT:
291 case ACCESS_SET_PRIVATE_ID_STATE:
295 msg_Warn( p_access, "unimplemented query in control" );
302 /*****************************************************************************
304 *****************************************************************************/
305 static block_t *BlockUDP( access_t *p_access )
307 access_sys_t *p_sys = p_access->p_sys;
311 p_block = block_New( p_access, p_sys->i_mtu );
312 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
313 p_block->p_buffer, p_sys->i_mtu,
315 if( p_block->i_buffer <= 0 )
317 block_Release( p_block );
321 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
322 p_sys->i_mtu < 32767 )
324 /* Increase by 100% */
326 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
333 * rtp_ChainInsert - insert a p_block in the chain and
334 * look at the sequence numbers.
336 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
338 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
339 block_t *p_prev = NULL;
340 block_t *p = p_sys->p_end;
341 uint16_t i_new = (uint16_t) p_block->i_dts;
346 p_sys->p_list = p_block;
347 p_sys->p_end = p_block;
350 /* walk through the queue from top down since the new packet is in
351 most cases just appended to the end */
355 i_tmp = i_new - (uint16_t) p->i_dts;
357 if( !i_tmp ) /* trash duplicate */
361 { /* insert after this block ( i_new > p->i_dts ) */
362 p_block->p_next = p->p_next;
367 p_prev->p_prev = p_block;
368 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
369 (uint16_t) p->i_dts, i_new );
373 p_sys->p_end = p_block;
377 if( p == p_sys->p_list )
378 { /* we've reached bottom of chain */
379 i_tmp = p_sys->i_last_seqno - i_new;
380 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
382 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
383 i_new, (uint16_t) p->i_dts );
386 p_sys->p_list = p_block;
390 if( !i_tmp ) /* trash duplicate */
393 /* reordering failed - append the packet to the end of queue */
394 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
395 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
396 (uint16_t) p_sys->p_end->i_dts);
397 p_sys->p_end->p_next = p_block;
398 p_block->p_prev = p_sys->p_end;
399 p_sys->p_end = p_block;
405 block_Release( p_block );
409 /*****************************************************************************
410 * BlockParseRTP/BlockRTP:
411 *****************************************************************************/
412 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
418 int i_extension_flag = 0;
419 int i_extension_length = 0;
420 uint16_t i_sequence_number = 0;
422 if( p_block == NULL )
425 if( p_block->i_buffer < RTP_HEADER_LEN )
428 /* Parse the header and make some verifications.
430 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
431 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
432 i_extension_flag = p_block->p_buffer[0] & 0x10;
433 i_payload_type = p_block->p_buffer[1] & 0x7F;
434 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
436 if( i_rtp_version != 2 )
437 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
439 if( i_payload_type == 14 || i_payload_type == 32)
441 else if( i_payload_type != 33 )
442 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
443 if( i_extension_flag )
444 i_extension_length = 4 +
445 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
447 /* Skip header + CSRC extension field n*(32 bits) + extension */
448 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
450 if( i_skip >= p_block->i_buffer )
453 /* Return the packet without the RTP header, remember seqno in i_dts */
454 p_block->i_buffer -= i_skip;
455 p_block->p_buffer += i_skip;
456 p_block->i_pts = mdate();
457 p_block->i_dts = (mtime_t) i_sequence_number;
460 /* Emulate packet loss */
461 if ( (i_sequence_number % 4000) == 0)
463 msg_Warn( p_access, "Emulating packet drop" );
464 block_Release( p_block );
472 msg_Warn( p_access, "received a too short packet for RTP" );
473 block_Release( p_block );
477 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
479 access_sys_t *p_sys = p_access->p_sys;
480 mtime_t i_first = mdate();
482 block_t *p = p_block;
486 mtime_t i_date = mdate();
488 if( p && rtp_ChainInsert( p_access, p ))
491 /* Require at least 2 packets in the buffer */
492 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
495 p = BlockParseRTP( p_access, BlockUDP( p_access ));
496 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
498 msg_Err( p_access, "error in RTP prebuffering!" );
503 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
504 p_access->info.b_prebuffered = VLC_TRUE;
506 p_sys->p_list = p_sys->p_list->p_next;
507 p_sys->i_last_seqno = (uint16_t) p->i_dts;
512 static block_t *BlockRTP( access_t *p_access )
514 access_sys_t *p_sys = p_access->p_sys;
517 while ( !p_sys->p_list ||
518 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
520 p = BlockParseRTP( p_access, BlockUDP( p_access ));
525 rtp_ChainInsert( p_access, p );
529 p_sys->p_list = p_sys->p_list->p_next;
530 p_sys->i_last_seqno++;
531 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
533 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
534 p_sys->i_last_seqno, (uint16_t) p->i_dts );
535 p_sys->i_last_seqno = (uint16_t) p->i_dts;
541 /*****************************************************************************
542 * BlockChoose: decide between RTP and UDP
543 *****************************************************************************/
544 static block_t *BlockChoose( access_t *p_access )
551 if( ( p_block = BlockUDP( p_access ) ) == NULL )
554 if( p_block->p_buffer[0] == 0x47 )
556 msg_Dbg( p_access, "detected TS over raw UDP" );
557 p_access->pf_block = BlockUDP;
558 p_access->info.b_prebuffered = VLC_TRUE;
562 if( p_block->i_buffer < RTP_HEADER_LEN )
565 /* Parse the header and make some verifications.
568 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
569 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
570 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
572 if( i_rtp_version != 2 )
574 msg_Dbg( p_access, "no supported RTP header detected" );
575 p_access->pf_block = BlockUDP;
576 p_access->info.b_prebuffered = VLC_TRUE;
580 switch( i_payload_type )
583 msg_Dbg( p_access, "detected TS over RTP" );
584 p_access->psz_demux = strdup( "ts" );
588 msg_Dbg( p_access, "detected MPEG audio over RTP" );
589 p_access->psz_demux = strdup( "mpga" );
593 msg_Dbg( p_access, "detected MPEG video over RTP" );
594 p_access->psz_demux = strdup( "mpgv" );
598 msg_Dbg( p_access, "no RTP header detected" );
599 p_access->pf_block = BlockUDP;
600 p_access->info.b_prebuffered = VLC_TRUE;
604 if( !BlockParseRTP( p_access, p_block )) return NULL;
606 p_access->pf_block = BlockRTP;
608 return BlockPrebufferRTP( p_access, p_block );