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 "Default 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 );
117 char *psz_parser, *psz_server_addr, *psz_bind_addr = "";
118 int i_bind_port, i_server_port = 0;
120 /* First set ipv4/ipv6 */
121 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
122 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
124 if( *p_access->psz_access )
127 /* Find out which shortcut was used */
128 if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
129 !strncmp( p_access->psz_access, "rtp4", 6 ))
131 val.b_bool = VLC_TRUE;
132 var_Set( p_access, "ipv4", val );
134 val.b_bool = VLC_FALSE;
135 var_Set( p_access, "ipv6", val );
137 else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
138 !strncmp( p_access->psz_access, "rtp6", 6 ) )
140 val.b_bool = VLC_TRUE;
141 var_Set( p_access, "ipv6", val );
143 val.b_bool = VLC_FALSE;
144 var_Set( p_access, "ipv4", val );
148 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
150 /* Parse psz_name syntax :
151 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
152 psz_parser = strchr( psz_name, '@' );
153 if( psz_parser != NULL )
155 /* Found bind address and/or bind port */
156 *psz_parser++ = '\0';
157 psz_bind_addr = psz_parser;
159 if( *psz_parser == '[' )
160 /* skips bracket'd IPv6 address */
161 psz_parser = strchr( psz_parser, ']' );
163 if( psz_parser != NULL )
165 psz_parser = strchr( psz_parser, ':' );
166 if( psz_parser != NULL )
168 *psz_parser++ = '\0';
169 i_bind_port = atoi( psz_parser );
174 psz_server_addr = psz_name;
175 if( *psz_server_addr == '[' )
176 /* skips bracket'd IPv6 address */
177 psz_parser = strchr( psz_name, ']' );
179 if( psz_parser != NULL )
181 psz_parser = strchr( psz_parser, ':' );
182 if( psz_parser != NULL )
184 *psz_parser++ = '\0';
185 i_server_port = atoi( psz_parser );
189 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
190 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
192 /* Set up p_access */
193 p_access->pf_read = NULL;
194 p_access->pf_block = BlockChoose;
195 p_access->pf_control = Control;
196 p_access->pf_seek = NULL;
197 p_access->info.i_update = 0;
198 p_access->info.i_size = 0;
199 p_access->info.i_pos = 0;
200 p_access->info.b_eof = VLC_FALSE;
201 p_access->info.b_prebuffered = VLC_FALSE;
202 p_access->info.i_title = 0;
203 p_access->info.i_seekpoint = 0;
205 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
206 p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
207 psz_server_addr, i_server_port );
210 msg_Err( p_access, "cannot open socket" );
217 net_StopSend( p_sys->fd );
220 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
221 if( p_sys->i_mtu <= 1 )
222 p_sys->i_mtu = 1500; /* Avoid problem */
224 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
226 /* Update default_pts to a suitable value for udp access */
227 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
230 /* RTP reordering for out-of-sequence packets */
231 p_sys->i_rtp_late = var_CreateGetInteger( p_access, "rtp-late" ) * 1000;
232 p_sys->i_last_seqno = 0;
233 p_sys->p_list = NULL;
238 /*****************************************************************************
239 * Close: free unused data structures
240 *****************************************************************************/
241 static void Close( vlc_object_t *p_this )
243 access_t *p_access = (access_t*)p_this;
244 access_sys_t *p_sys = p_access->p_sys;
246 block_ChainRelease( p_sys->p_list );
247 net_Close( p_sys->fd );
251 /*****************************************************************************
253 *****************************************************************************/
254 static int Control( access_t *p_access, int i_query, va_list args )
256 access_sys_t *p_sys = p_access->p_sys;
264 case ACCESS_CAN_SEEK:
265 case ACCESS_CAN_FASTSEEK:
266 case ACCESS_CAN_PAUSE:
267 case ACCESS_CAN_CONTROL_PACE:
268 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
269 *pb_bool = VLC_FALSE;
273 pi_int = (int*)va_arg( args, int * );
274 *pi_int = p_sys->i_mtu;
277 case ACCESS_GET_PTS_DELAY:
278 pi_64 = (int64_t*)va_arg( args, int64_t * );
279 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
283 case ACCESS_SET_PAUSE_STATE:
284 case ACCESS_GET_TITLE_INFO:
285 case ACCESS_SET_TITLE:
286 case ACCESS_SET_SEEKPOINT:
287 case ACCESS_SET_PRIVATE_ID_STATE:
291 msg_Warn( p_access, "unimplemented query in control" );
298 /*****************************************************************************
300 *****************************************************************************/
301 static block_t *BlockUDP( access_t *p_access )
303 access_sys_t *p_sys = p_access->p_sys;
307 p_block = block_New( p_access, p_sys->i_mtu );
308 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
309 p_block->p_buffer, p_sys->i_mtu,
311 if( p_block->i_buffer <= 0 )
313 block_Release( p_block );
317 if( (p_block->i_buffer >= p_sys->i_mtu) && p_sys->b_auto_mtu &&
318 p_sys->i_mtu < 32767 )
320 /* Increase by 100% */
322 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
329 * rtp_ChainInsert - insert a p_block in the chain and
330 * look at the sequence numbers.
332 static inline vlc_bool_t rtp_ChainInsert( access_t *p_access, block_t *p_block )
334 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
335 block_t *p_prev = NULL;
336 block_t *p = p_sys->p_end;
337 uint16_t i_new = (uint16_t) p_block->i_dts;
342 p_sys->p_list = p_block;
343 p_sys->p_end = p_block;
346 /* walk through the queue from top down since the new packet is in
347 most cases just appended to the end */
351 i_tmp = i_new - (uint16_t) p->i_dts;
353 if( !i_tmp ) /* trash duplicate */
357 { /* insert after this block ( i_new > p->i_dts ) */
358 p_block->p_next = p->p_next;
363 p_prev->p_prev = p_block;
364 msg_Dbg(p_access, "RTP reordering: insert after %d, new %d",
365 (uint16_t) p->i_dts, i_new );
369 p_sys->p_end = p_block;
373 if( p == p_sys->p_list )
374 { /* we've reached bottom of chain */
375 i_tmp = p_sys->i_last_seqno - i_new;
376 if( !p_access->info.b_prebuffered || (i_tmp > 32767) )
378 msg_Dbg(p_access, "RTP reordering: prepend %d before %d",
379 i_new, (uint16_t) p->i_dts );
382 p_sys->p_list = p_block;
386 if( !i_tmp ) /* trash duplicate */
389 /* reordering failed - append the packet to the end of queue */
390 msg_Dbg(p_access, "RTP: sequence changed (or buffer too small) "
391 "new: %d, buffer %d...%d", i_new, (uint16_t) p->i_dts,
392 (uint16_t) p_sys->p_end->i_dts);
393 p_sys->p_end->p_next = p_block;
394 p_block->p_prev = p_sys->p_end;
395 p_sys->p_end = p_block;
401 block_Release( p_block );
405 /*****************************************************************************
406 * BlockParseRTP/BlockRTP:
407 *****************************************************************************/
408 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
414 int i_extension_flag = 0;
415 int i_extension_length = 0;
416 uint16_t i_sequence_number = 0;
418 if( p_block == NULL )
421 if( p_block->i_buffer < RTP_HEADER_LEN )
424 /* Parse the header and make some verifications.
426 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
427 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
428 i_extension_flag = p_block->p_buffer[0] & 0x10;
429 i_payload_type = p_block->p_buffer[1] & 0x7F;
430 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
432 if( i_rtp_version != 2 )
433 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
435 if( i_payload_type == 14 || i_payload_type == 32)
437 else if( i_payload_type != 33 )
438 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
439 if( i_extension_flag )
440 i_extension_length = 4 +
441 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
443 /* Skip header + CSRC extension field n*(32 bits) + extension */
444 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extension_length;
446 if( i_skip >= p_block->i_buffer )
449 /* Return the packet without the RTP header, remember seqno in i_dts */
450 p_block->i_buffer -= i_skip;
451 p_block->p_buffer += i_skip;
452 p_block->i_pts = mdate();
453 p_block->i_dts = (mtime_t) i_sequence_number;
456 /* Emulate packet loss */
457 if ( (i_sequence_number % 4000) == 0)
459 msg_Warn( p_access, "Emulating packet drop" );
460 block_Release( p_block );
469 msg_Warn( p_access, "received a too short packet for RTP" );
470 block_Release( p_block );
474 static block_t *BlockPrebufferRTP( access_t *p_access, block_t *p_block )
476 access_sys_t *p_sys = p_access->p_sys;
477 mtime_t i_first = mdate();
479 block_t *p = p_block;
483 mtime_t i_date = mdate();
485 if( p && rtp_ChainInsert( p_access, p ))
488 /* Require at least 2 packets in the buffer */
489 if( i_count > 2 && (i_date - i_first) > p_sys->i_rtp_late )
492 p = BlockParseRTP( p_access, BlockUDP( p_access ));
493 if( !p && (i_date - i_first) > p_sys->i_rtp_late )
495 msg_Err( p_access, "Error in RTP prebuffering!" );
500 msg_Dbg( p_access, "RTP: prebuffered %d packets", i_count - 1 );
501 p_access->info.b_prebuffered = VLC_TRUE;
503 p_sys->p_list = p_sys->p_list->p_next;
504 p_sys->i_last_seqno = (uint16_t) p->i_dts;
509 static block_t *BlockRTP( access_t *p_access )
511 access_sys_t *p_sys = p_access->p_sys;
514 while ( !p_sys->p_list ||
515 ( mdate() - p_sys->p_list->i_pts ) < p_sys->i_rtp_late )
517 p = BlockParseRTP( p_access, BlockUDP( p_access ));
522 rtp_ChainInsert( p_access, p );
526 p_sys->p_list = p_sys->p_list->p_next;
527 p_sys->i_last_seqno++;
528 if( p_sys->i_last_seqno != (uint16_t) p->i_dts )
530 msg_Dbg( p_access, "RTP: packet(s) lost, expected %d, got %d",
531 p_sys->i_last_seqno, (uint16_t) p->i_dts );
532 p_sys->i_last_seqno = (uint16_t) p->i_dts;
538 /*****************************************************************************
539 * BlockChoose: decide between RTP and UDP
540 *****************************************************************************/
541 static block_t *BlockChoose( access_t *p_access )
548 if( ( p_block = BlockUDP( p_access ) ) == NULL )
551 if( p_block->p_buffer[0] == 0x47 )
553 msg_Dbg( p_access, "detected TS over raw UDP" );
554 p_access->pf_block = BlockUDP;
555 p_access->info.b_prebuffered = VLC_TRUE;
559 if( p_block->i_buffer < RTP_HEADER_LEN )
562 /* Parse the header and make some verifications.
565 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
566 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
567 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
569 if( i_rtp_version != 2 )
571 msg_Dbg( p_access, "no supported RTP header detected" );
572 p_access->pf_block = BlockUDP;
573 p_access->info.b_prebuffered = VLC_TRUE;
577 switch( i_payload_type )
580 msg_Dbg( p_access, "detected TS over RTP" );
581 p_access->psz_demux = strdup( "ts" );
585 msg_Dbg( p_access, "detected MPEG audio over RTP" );
586 p_access->psz_demux = strdup( "mpga" );
590 msg_Dbg( p_access, "detected MPEG video over RTP" );
591 p_access->psz_demux = strdup( "mpgv" );
595 msg_Dbg( p_access, "no RTP header detected" );
596 p_access->pf_block = BlockUDP;
597 p_access->info.b_prebuffered = VLC_TRUE;
601 if( !BlockParseRTP( p_access, p_block )) return NULL;
603 p_access->pf_block = BlockRTP;
605 return BlockPrebufferRTP( p_access, p_block );