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/input.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( \
44 "Caching value for UDP streams. This " \
45 "value should be set in milliseconds." )
47 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
48 #define AUTO_MTU_LONGTEXT N_( \
49 "Automatically detect the line's MTU. This will increase the size if" \
50 " truncated packets are found" )
52 #define RTP_LATE_TEXT N_("RTP reordering timeout in ms")
53 #define RTP_LATE_LONGTEXT N_( \
54 "VLC reorders RTP packets. The input will wait for late packets at most "\
55 "the time specified here (in milliseconds)." )
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
61 set_shortname( _("UDP/RTP" ) );
62 set_description( _("UDP/RTP input") );
63 set_category( CAT_INPUT );
64 set_subcategory( SUBCAT_INPUT_ACCESS );
66 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
67 CACHING_LONGTEXT, VLC_TRUE );
68 add_integer( "rtp-late", 100, NULL, RTP_LATE_TEXT, RTP_LATE_LONGTEXT, VLC_TRUE );
70 add_bool( "udp-auto-mtu", 1, NULL,
71 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
73 set_capability( "access2", 0 );
74 add_shortcut( "udp" );
75 add_shortcut( "udpstream" );
76 add_shortcut( "udp4" );
77 add_shortcut( "udp6" );
78 add_shortcut( "rtp" );
79 add_shortcut( "rtp4" );
80 add_shortcut( "rtp6" );
81 set_callbacks( Open, Close );
84 /*****************************************************************************
86 *****************************************************************************/
87 #define RTP_HEADER_LEN 12
89 static block_t *BlockUDP( access_t * );
90 static block_t *BlockRTP( access_t * );
91 static block_t *BlockChoose( access_t * );
92 static int Control( access_t *, int, va_list );
99 vlc_bool_t b_auto_mtu;
101 /* reorder rtp packets when out-of-sequence */
103 uint16_t i_last_seqno;
108 /*****************************************************************************
109 * Open: open the socket
110 *****************************************************************************/
111 static int Open( vlc_object_t *p_this )
113 access_t *p_access = (access_t*)p_this;
116 char *psz_name = strdup( p_access->psz_path );
118 const char *psz_server_addr, *psz_bind_addr = "";
119 int i_bind_port, i_server_port = 0;
121 /* First set ipv4/ipv6 */
122 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
123 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
125 if( *p_access->psz_access )
128 /* Find out which shortcut was used */
129 if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
130 !strncmp( p_access->psz_access, "rtp4", 6 ))
132 val.b_bool = VLC_TRUE;
133 var_Set( p_access, "ipv4", val );
135 val.b_bool = VLC_FALSE;
136 var_Set( p_access, "ipv6", val );
138 else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
139 !strncmp( p_access->psz_access, "rtp6", 6 ) )
141 val.b_bool = VLC_TRUE;
142 var_Set( p_access, "ipv6", val );
144 val.b_bool = VLC_FALSE;
145 var_Set( p_access, "ipv4", val );
149 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
151 /* Parse psz_name syntax :
152 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
153 psz_parser = strchr( psz_name, '@' );
154 if( psz_parser != NULL )
156 /* Found bind address and/or bind port */
157 *psz_parser++ = '\0';
158 psz_bind_addr = psz_parser;
160 if( *psz_parser == '[' )
161 /* skips bracket'd IPv6 address */
162 psz_parser = strchr( psz_parser, ']' );
164 if( psz_parser != NULL )
166 psz_parser = strchr( psz_parser, ':' );
167 if( psz_parser != NULL )
169 *psz_parser++ = '\0';
170 i_bind_port = atoi( psz_parser );
175 psz_server_addr = psz_name;
176 if( *psz_server_addr == '[' )
177 /* skips bracket'd IPv6 address */
178 psz_parser = strchr( psz_name, ']' );
180 if( psz_parser != NULL )
182 psz_parser = strchr( psz_parser, ':' );
183 if( psz_parser != NULL )
185 *psz_parser++ = '\0';
186 i_server_port = atoi( psz_parser );
190 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
191 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
193 /* Set up p_access */
194 access_InitFields( p_access );
195 ACCESS_SET_CALLBACKS( NULL, BlockChoose, Control, NULL );
196 p_access->info.b_prebuffered = VLC_FALSE;
197 MALLOC_ERR( p_access->p_sys, access_sys_t ); p_sys = p_access->p_sys;
199 p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
200 psz_server_addr, i_server_port );
203 msg_Err( p_access, "cannot open socket" );
210 net_StopSend( p_sys->fd );
213 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
214 if( p_sys->i_mtu <= 1 )
215 p_sys->i_mtu = 1500; /* Avoid problem */
217 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
219 /* Update default_pts to a suitable value for udp access */
220 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
222 /* RTP reordering for out-of-sequence packets */
223 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
224 p_sys->i_last_seqno = 0;
225 p_sys->p_list = NULL;
230 /*****************************************************************************
231 * Close: free unused data structures
232 *****************************************************************************/
233 static void Close( vlc_object_t *p_this )
235 access_t *p_access = (access_t*)p_this;
236 access_sys_t *p_sys = p_access->p_sys;
238 block_ChainRelease( p_sys->p_list );
239 net_Close( p_sys->fd );
243 /*****************************************************************************
245 *****************************************************************************/
246 static int Control( access_t *p_access, int i_query, va_list args )
248 access_sys_t *p_sys = p_access->p_sys;
256 case ACCESS_CAN_SEEK:
257 case ACCESS_CAN_FASTSEEK:
258 case ACCESS_CAN_PAUSE:
259 case ACCESS_CAN_CONTROL_PACE:
260 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
261 *pb_bool = VLC_FALSE;
265 pi_int = (int*)va_arg( args, int * );
266 *pi_int = p_sys->i_mtu;
269 case ACCESS_GET_PTS_DELAY:
270 pi_64 = (int64_t*)va_arg( args, int64_t * );
271 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
275 case ACCESS_SET_PAUSE_STATE:
276 case ACCESS_GET_TITLE_INFO:
277 case ACCESS_SET_TITLE:
278 case ACCESS_SET_SEEKPOINT:
279 case ACCESS_SET_PRIVATE_ID_STATE:
283 msg_Warn( p_access, "unimplemented query in control" );
290 /*****************************************************************************
292 *****************************************************************************/
293 static block_t *BlockUDP( access_t *p_access )
295 access_sys_t *p_sys = p_access->p_sys;
299 p_block = block_New( p_access, p_sys->i_mtu );
300 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
301 p_block->p_buffer, p_sys->i_mtu,
303 if( p_block->i_buffer <= 0 )
305 block_Release( p_block );
309 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
310 p_sys->i_mtu < 32767 )
312 /* Increase by 100% */
314 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
321 * rtp_ChainInsert - insert a p_block in the chain and
322 * look at the sequence numbers.
324 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
326 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
327 block_t *p_prev = NULL;
328 block_t *p = p_sys->p_end;
329 uint16_t i_new = (uint16_t) p_block->i_dts;
334 p_sys->p_list = p_block;
335 p_sys->p_end = p_block;
338 /* walk through the queue from top down since the new packet is in
339 most cases just appended to the end */
343 i_tmp = i_new - (uint16_t) p->i_dts;
345 if( !i_tmp ) /* trash duplicate */
349 { /* insert after this block ( i_new > p->i_dts ) */
350 p_block->p_next = p->p_next;
355 p_prev->p_prev = p_block;
356 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
357 (uint16_t) p->i_dts, i_new );
361 p_sys->p_end = p_block;
365 if( p == p_sys->p_list )
366 { /* we've reached bottom of chain */
367 i_tmp = p_sys->i_last_seqno - i_new;
368 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
370 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
371 i_new, (uint16_t) p->i_dts );
374 p_sys->p_list = p_block;
378 if( !i_tmp ) /* trash duplicate */
381 /* reordering failed - append the packet to the end of queue */
382 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
383 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
384 (uint16_t) p_sys->p_end->i_dts);
385 p_sys->p_end->p_next = p_block;
386 p_block->p_prev = p_sys->p_end;
387 p_sys->p_end = p_block;
393 block_Release( p_block );
397 /*****************************************************************************
398 * BlockParseRTP/BlockRTP:
399 *****************************************************************************/
400 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
406 int i_extension_flag = 0;
407 int i_extension_length = 0;
408 uint16_t i_sequence_number = 0;
410 if( p_block == NULL )
413 if( p_block->i_buffer < RTP_HEADER_LEN )
416 /* Parse the header and make some verifications.
418 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
419 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
420 i_extension_flag = p_block->p_buffer[0] & 0x10;
421 i_payload_type = p_block->p_buffer[1] & 0x7F;
422 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
424 if( i_rtp_version != 2 )
425 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
427 if( i_payload_type == 14 || i_payload_type == 32)
429 else if( i_payload_type != 33 )
430 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
431 if( i_extension_flag )
432 i_extension_length = 4 +
433 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
435 /* Skip header + CSRC extension field n*(32 bits) + extension */
436 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
438 if( i_skip >= p_block->i_buffer )
441 /* Return the packet without the RTP header, remember seqno in i_dts */
442 p_block->i_buffer -= i_skip;
443 p_block->p_buffer += i_skip;
444 p_block->i_pts = mdate();
445 p_block->i_dts = (mtime_t) i_sequence_number;
448 /* Emulate packet loss */
449 if ( (i_sequence_number % 4000) == 0)
451 msg_Warn( p_access, "Emulating packet drop" );
452 block_Release( p_block );
460 msg_Warn( p_access, "received a too short packet for RTP" );
461 block_Release( p_block );
465 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
467 access_sys_t *p_sys = p_access->p_sys;
468 mtime_t i_first = mdate();
470 block_t *p = p_block;
474 mtime_t i_date = mdate();
476 if( p && rtp_ChainInsert( p_access, p ))
479 /* Require at least 2 packets in the buffer */
480 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
483 p = BlockParseRTP( p_access, BlockUDP( p_access ));
484 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
486 msg_Err( p_access, "error in RTP prebuffering!" );
491 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
492 p_access->info.b_prebuffered = VLC_TRUE;
494 p_sys->p_list = p_sys->p_list->p_next;
495 p_sys->i_last_seqno = (uint16_t) p->i_dts;
500 static block_t *BlockRTP( access_t *p_access )
502 access_sys_t *p_sys = p_access->p_sys;
505 while ( !p_sys->p_list ||
506 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
508 p = BlockParseRTP( p_access, BlockUDP( p_access ));
513 rtp_ChainInsert( p_access, p );
517 p_sys->p_list = p_sys->p_list->p_next;
518 p_sys->i_last_seqno++;
519 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
521 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
522 p_sys->i_last_seqno, (uint16_t) p->i_dts );
523 p_sys->i_last_seqno = (uint16_t) p->i_dts;
529 /*****************************************************************************
530 * BlockChoose: decide between RTP and UDP
531 *****************************************************************************/
532 static block_t *BlockChoose( access_t *p_access )
539 if( ( p_block = BlockUDP( p_access ) ) == NULL )
542 if( p_block->p_buffer[0] == 0x47 )
544 msg_Dbg( p_access, "detected TS over raw UDP" );
545 p_access->pf_block = BlockUDP;
546 p_access->info.b_prebuffered = VLC_TRUE;
550 if( p_block->i_buffer < RTP_HEADER_LEN )
553 /* Parse the header and make some verifications.
556 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
557 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
558 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
560 if( i_rtp_version != 2 )
562 msg_Dbg( p_access, "no supported RTP header detected" );
563 p_access->pf_block = BlockUDP;
564 p_access->info.b_prebuffered = VLC_TRUE;
568 switch( i_payload_type )
571 msg_Dbg( p_access, "detected TS over RTP" );
572 p_access->psz_demux = strdup( "ts" );
576 msg_Dbg( p_access, "detected MPEG audio over RTP" );
577 p_access->psz_demux = strdup( "mpga" );
581 msg_Dbg( p_access, "detected MPEG video over RTP" );
582 p_access->psz_demux = strdup( "mpgv" );
586 msg_Dbg( p_access, "no RTP header detected" );
587 p_access->pf_block = BlockUDP;
588 p_access->info.b_prebuffered = VLC_TRUE;
592 if( !BlockParseRTP( p_access, p_block )) return NULL;
594 p_access->pf_block = BlockRTP;
596 return BlockPrebufferRTP( p_access, p_block );