1 /*****************************************************************************
2 * udp.c: raw UDP & RTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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@wxs.nl>
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 "Allows you to modify the default caching value for UDP streams. This " \
45 "value should be set in millisecond units." )
47 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
48 #define AUTO_MTU_LONGTEXT N_( \
49 "Allows growing the MTU if truncated packets are found" )
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_shortname( _("UDP/RTP" ) );
56 set_description( _("UDP/RTP input") );
57 set_category( CAT_INPUT );
58 set_subcategory( SUBCAT_INPUT_ACCESS );
60 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
61 CACHING_LONGTEXT, VLC_TRUE );
62 add_bool( "udp-auto-mtu", 1, NULL,
63 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
65 set_capability( "access2", 0 );
66 add_shortcut( "udp" );
67 add_shortcut( "udpstream" );
68 add_shortcut( "udp4" );
69 add_shortcut( "udp6" );
70 add_shortcut( "rtp" );
71 add_shortcut( "rtp4" );
72 add_shortcut( "rtp6" );
73 set_callbacks( Open, Close );
76 /*****************************************************************************
78 *****************************************************************************/
79 #define RTP_HEADER_LEN 12
81 static block_t *BlockUDP( access_t * );
82 static block_t *BlockRTP( access_t * );
83 static block_t *BlockChoose( access_t * );
84 static int Control( access_t *, int, va_list );
91 vlc_bool_t b_auto_mtu;
94 int i_sequence_number;
97 /*****************************************************************************
98 * Open: open the socket
99 *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
102 access_t *p_access = (access_t*)p_this;
105 char *psz_name = strdup( p_access->psz_path );
106 char *psz_parser, *psz_server_addr, *psz_bind_addr = "";
107 int i_bind_port, i_server_port = 0;
109 /* First set ipv4/ipv6 */
110 var_Create( p_access, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111 var_Create( p_access, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
113 if( *p_access->psz_access )
116 /* Find out which shortcut was used */
117 if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
118 !strncmp( p_access->psz_access, "rtp4", 6 ))
120 val.b_bool = VLC_TRUE;
121 var_Set( p_access, "ipv4", val );
123 val.b_bool = VLC_FALSE;
124 var_Set( p_access, "ipv6", val );
126 else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
127 !strncmp( p_access->psz_access, "rtp6", 6 ) )
129 val.b_bool = VLC_TRUE;
130 var_Set( p_access, "ipv6", val );
132 val.b_bool = VLC_FALSE;
133 var_Set( p_access, "ipv4", val );
137 i_bind_port = var_CreateGetInteger( p_access, "server-port" );
139 /* Parse psz_name syntax :
140 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
141 psz_parser = strchr( psz_name, '@' );
142 if( psz_parser != NULL )
144 /* Found bind address and/or bind port */
145 *psz_parser++ = '\0';
146 psz_bind_addr = psz_parser;
148 if( *psz_parser == '[' )
149 /* skips bracket'd IPv6 address */
150 psz_parser = strchr( psz_parser, ']' );
152 if( psz_parser != NULL )
154 psz_parser = strchr( psz_parser, ':' );
155 if( psz_parser != NULL )
157 *psz_parser++ = '\0';
158 i_bind_port = atoi( psz_parser );
163 psz_server_addr = psz_name;
164 if( *psz_server_addr == '[' )
165 /* skips bracket'd IPv6 address */
166 psz_parser = strchr( psz_name, ']' );
168 if( psz_parser != NULL )
170 psz_parser = strchr( psz_parser, ':' );
171 if( psz_parser != NULL )
173 *psz_parser++ = '\0';
174 i_server_port = atoi( psz_parser );
178 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
179 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
181 /* Set up p_access */
182 p_access->pf_read = NULL;
183 if( !strcasecmp( p_access->psz_access, "rtp" )
184 || !strcasecmp( p_access->psz_access, "rtp4" )
185 || !strcasecmp( p_access->psz_access, "rtp6" ) )
187 p_access->pf_block = BlockRTP;
191 p_access->pf_block = BlockChoose;
193 p_access->pf_control = Control;
194 p_access->pf_seek = NULL;
195 p_access->info.i_update = 0;
196 p_access->info.i_size = 0;
197 p_access->info.i_pos = 0;
198 p_access->info.b_eof = VLC_FALSE;
199 p_access->info.i_title = 0;
200 p_access->info.i_seekpoint = 0;
202 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
203 p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
204 psz_server_addr, i_server_port );
207 msg_Err( p_access, "cannot open socket" );
214 net_StopSend( p_sys->fd );
217 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
218 if( p_sys->i_mtu <= 1 )
219 p_sys->i_mtu = 1500; /* Avoid problem */
221 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
223 /* Update default_pts to a suitable value for udp access */
224 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
226 /* Keep track of RTP sequence number */
227 p_sys->i_sequence_number = -1;
232 /*****************************************************************************
233 * Close: free unused data structures
234 *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
237 access_t *p_access = (access_t*)p_this;
238 access_sys_t *p_sys = p_access->p_sys;
240 net_Close( p_sys->fd );
244 /*****************************************************************************
246 *****************************************************************************/
247 static int Control( access_t *p_access, int i_query, va_list args )
249 access_sys_t *p_sys = p_access->p_sys;
257 case ACCESS_CAN_SEEK:
258 case ACCESS_CAN_FASTSEEK:
259 case ACCESS_CAN_PAUSE:
260 case ACCESS_CAN_CONTROL_PACE:
261 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
262 *pb_bool = VLC_FALSE;
266 pi_int = (int*)va_arg( args, int * );
267 *pi_int = p_sys->i_mtu;
270 case ACCESS_GET_PTS_DELAY:
271 pi_64 = (int64_t*)va_arg( args, int64_t * );
272 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
276 case ACCESS_SET_PAUSE_STATE:
277 case ACCESS_GET_TITLE_INFO:
278 case ACCESS_SET_TITLE:
279 case ACCESS_SET_SEEKPOINT:
280 case ACCESS_SET_PRIVATE_ID_STATE:
284 msg_Warn( p_access, "unimplemented query in control" );
291 /*****************************************************************************
293 *****************************************************************************/
294 static block_t *BlockUDP( access_t *p_access )
296 access_sys_t *p_sys = p_access->p_sys;
300 p_block = block_New( p_access, p_sys->i_mtu );
301 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
302 p_block->p_buffer, p_sys->i_mtu,
304 if( p_block->i_buffer <= 0 )
306 block_Release( p_block );
310 if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
311 p_sys->i_mtu < 32767 )
313 /* Increase by 100% */
315 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
321 /*****************************************************************************
322 * BlockParseRTP/BlockRTP:
323 *****************************************************************************/
324 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
330 int i_sequence_number = 0;
331 int i_extention_flag;
332 int i_extention_length = 0;
333 int i_sequence_expected;
335 if( p_block == NULL )
338 if( p_block->i_buffer < RTP_HEADER_LEN )
341 /* Parse the header and make some verifications.
342 * See RFC 1889 & RFC 2250. */
343 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
344 i_CSRC_count = p_block->p_buffer[0] & 0x0F;
345 i_extention_flag = p_block->p_buffer[0] & 0x10;
346 i_payload_type = p_block->p_buffer[1] & 0x7F;
347 i_sequence_number = (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3];
349 if ( i_rtp_version != 2 )
350 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
352 if( i_payload_type == 14 )
354 else if( i_payload_type != 33 && i_payload_type != 32 )
355 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
356 if( i_extention_flag )
357 i_extention_length = 4 +
358 4 * ( (p_block->p_buffer[14] << 8) + p_block->p_buffer[15] );
360 /* Skip header + CSRC extension field n*(32 bits) + extention */
361 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count + i_extention_length;
363 if( i_skip >= p_block->i_buffer )
366 /* Return the packet without the RTP header. */
367 p_block->i_buffer -= i_skip;
368 p_block->p_buffer += i_skip;
370 #define RTP_SEQ_NUM_SIZE 65536
371 #define RTP_SEQ_MAX_NO_DELTA 50
372 /* Detect RTP packet loss through tracking sequence numbers.
374 if( p_access->p_sys->i_sequence_number == -1 )
375 p_access->p_sys->i_sequence_number =
376 (i_sequence_number - 1 + RTP_SEQ_NUM_SIZE ) % RTP_SEQ_NUM_SIZE;
378 i_sequence_expected = (p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE;
380 if( i_sequence_expected != i_sequence_number )
382 if( ((p_access->p_sys->i_sequence_number - i_sequence_number + RTP_SEQ_NUM_SIZE) % RTP_SEQ_NUM_SIZE) < RTP_SEQ_MAX_NO_DELTA )
384 msg_Warn( p_access, "Trashing reordered/duplicate RTP packet, expected sequence number %d got %d",
385 i_sequence_expected, i_sequence_number );
386 block_Release( p_block );
391 "RTP packet(s) lost, expected sequence number %d got %d",
392 i_sequence_expected, i_sequence_number );
393 /* Mark transport error in the first TS packet in the RTP stream. */
394 if( i_payload_type == 33 && p_block->p_buffer[0] == 0x47 )
395 p_block->p_buffer[1] |= 0x80;
397 p_access->p_sys->i_sequence_number = i_sequence_number;
398 #undef RTP_SEQ_MAX_NO_DELTA
399 #undef RTP_SEQ_NUM_SIZE
403 msg_Warn( p_access, "received a too short packet for RTP" );
404 block_Release( p_block );
408 static block_t *BlockRTP( access_t *p_access )
410 block_t *p_block = BlockUDP( p_access );
412 if ( p_block != NULL )
413 return BlockParseRTP( p_access, p_block );
418 /*****************************************************************************
419 * BlockChoose: decide between RTP and UDP
420 *****************************************************************************/
421 static block_t *BlockChoose( access_t *p_access )
428 if( ( p_block = BlockUDP( p_access ) ) == NULL )
431 if( p_block->p_buffer[0] == 0x47 )
433 msg_Dbg( p_access, "detected TS over raw UDP" );
434 p_access->pf_block = BlockUDP;
438 if( p_block->i_buffer < RTP_HEADER_LEN )
441 /* Parse the header and make some verifications.
442 * See RFC 1889 & RFC 2250. */
444 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
445 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
446 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
448 if( i_rtp_version != 2 )
450 msg_Dbg( p_access, "no supported RTP header detected" );
451 p_access->pf_block = BlockUDP;
455 switch( i_payload_type )
458 msg_Dbg( p_access, "detected TS over RTP" );
459 p_access->psz_demux = strdup( "ts" );
463 msg_Dbg( p_access, "detected MPEG audio over RTP" );
464 p_access->psz_demux = strdup( "mpga" );
468 msg_Dbg( p_access, "detected MPEG video over RTP" );
469 p_access->psz_demux = strdup( "mpgv" );
473 msg_Dbg( p_access, "no RTP header detected" );
474 p_access->pf_block = BlockUDP;
478 p_access->pf_block = BlockRTP;
480 return BlockParseRTP( p_access, p_block );