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 );
164 psz_server_addr = psz_name;
165 if( *psz_server_addr == '[' )
166 /* skips bracket'd IPv6 address */
167 psz_parser = strchr( psz_name, ']' );
169 if( psz_parser != NULL )
171 psz_parser = strchr( psz_parser, ':' );
172 if( psz_parser != NULL )
174 *psz_parser++ = '\0';
175 i_server_port = atoi( psz_parser );
179 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
180 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
182 /* Set up p_access */
183 p_access->pf_read = NULL;
184 if( !strcasecmp( p_access->psz_access, "rtp" )
185 || !strcasecmp( p_access->psz_access, "rtp4" )
186 || !strcasecmp( p_access->psz_access, "rtp6" ) )
188 p_access->pf_block = BlockRTP;
192 p_access->pf_block = BlockChoose;
194 p_access->pf_control = Control;
195 p_access->pf_seek = NULL;
196 p_access->info.i_update = 0;
197 p_access->info.i_size = 0;
198 p_access->info.i_pos = 0;
199 p_access->info.b_eof = VLC_FALSE;
200 p_access->info.i_title = 0;
201 p_access->info.i_seekpoint = 0;
203 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
204 p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
205 psz_server_addr, i_server_port );
208 msg_Err( p_access, "cannot open socket" );
215 net_StopSend( p_sys->fd );
218 p_sys->i_mtu = var_CreateGetInteger( p_access, "mtu" );
219 if( p_sys->i_mtu <= 1 )
220 p_sys->i_mtu = 1500; /* Avoid problem */
222 p_sys->b_auto_mtu = var_CreateGetBool( p_access, "udp-auto-mtu" );;
224 /* Update default_pts to a suitable value for udp access */
225 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
227 /* Keep track of RTP sequence number */
228 p_sys->i_sequence_number = -1;
233 /*****************************************************************************
234 * Close: free unused data structures
235 *****************************************************************************/
236 static void Close( vlc_object_t *p_this )
238 access_t *p_access = (access_t*)p_this;
239 access_sys_t *p_sys = p_access->p_sys;
241 net_Close( p_sys->fd );
245 /*****************************************************************************
247 *****************************************************************************/
248 static int Control( access_t *p_access, int i_query, va_list args )
250 access_sys_t *p_sys = p_access->p_sys;
258 case ACCESS_CAN_SEEK:
259 case ACCESS_CAN_FASTSEEK:
260 case ACCESS_CAN_PAUSE:
261 case ACCESS_CAN_CONTROL_PACE:
262 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
263 *pb_bool = VLC_FALSE;
267 pi_int = (int*)va_arg( args, int * );
268 *pi_int = p_sys->i_mtu;
271 case ACCESS_GET_PTS_DELAY:
272 pi_64 = (int64_t*)va_arg( args, int64_t * );
273 *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
277 case ACCESS_SET_PAUSE_STATE:
278 case ACCESS_GET_TITLE_INFO:
279 case ACCESS_SET_TITLE:
280 case ACCESS_SET_SEEKPOINT:
281 case ACCESS_SET_PRIVATE_ID_STATE:
285 msg_Warn( p_access, "unimplemented query in control" );
292 /*****************************************************************************
294 *****************************************************************************/
295 static block_t *BlockUDP( access_t *p_access )
297 access_sys_t *p_sys = p_access->p_sys;
301 p_block = block_New( p_access, p_sys->i_mtu );
302 p_block->i_buffer = net_Read( p_access, p_sys->fd, NULL,
303 p_block->p_buffer, p_sys->i_mtu,
305 if( p_block->i_buffer <= 0 )
307 block_Release( p_block );
311 if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu &&
312 p_sys->i_mtu < 32767 )
314 /* Increase by 100% */
316 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
322 /*****************************************************************************
323 * BlockParseRTP/BlockRTP:
324 *****************************************************************************/
325 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
331 int i_sequence_number = 0;
333 if( p_block == NULL )
336 if( p_block->i_buffer < RTP_HEADER_LEN )
339 /* Parse the header and make some verifications.
340 * See RFC 1889 & RFC 2250. */
341 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
342 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
343 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
344 i_sequence_number = ( (p_block->p_buffer[2] << 8 ) + p_block->p_buffer[3] );
346 if ( i_rtp_version != 2 )
347 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
349 if( i_payload_type == 14 )
351 else if( i_payload_type != 33 && i_payload_type != 32 )
352 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
354 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
356 /* A CSRC extension field is 32 bits in size (4 bytes) */
357 if( i_skip >= p_block->i_buffer )
360 /* Return the packet without the RTP header. */
361 p_block->i_buffer -= i_skip;
362 p_block->p_buffer += i_skip;
364 #define RTP_SEQ_NUM_SIZE 65536
365 /* Detect RTP packet loss through tracking sequence numbers.
367 if( p_access->p_sys->i_sequence_number == -1 )
368 p_access->p_sys->i_sequence_number = i_sequence_number;
370 if( ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE) != i_sequence_number )
372 msg_Warn( p_access, "RTP packet(s) lost, expected sequence number %d got %d",
373 ((p_access->p_sys->i_sequence_number + 1) % RTP_SEQ_NUM_SIZE),
375 if( i_payload_type == 33 )
377 /* Mark transport error in the first TS packet in the RTP stream. */
378 p_block->p_buffer[1] |= 0x80;
381 p_access->p_sys->i_sequence_number = i_sequence_number;
382 #undef RTP_SEQ_NUM_SIZE
386 msg_Warn( p_access, "received a too short packet for RTP" );
387 block_Release( p_block );
391 static block_t *BlockRTP( access_t *p_access )
393 block_t *p_block = BlockUDP( p_access );
395 if ( p_block != NULL )
396 return BlockParseRTP( p_access, p_block );
401 /*****************************************************************************
402 * BlockChoose: decide between RTP and UDP
403 *****************************************************************************/
404 static block_t *BlockChoose( access_t *p_access )
411 if( ( p_block = BlockUDP( p_access ) ) == NULL )
414 if( p_block->p_buffer[0] == 0x47 )
416 msg_Dbg( p_access, "detected TS over raw UDP" );
417 p_access->pf_block = BlockUDP;
421 if( p_block->i_buffer < RTP_HEADER_LEN )
424 /* Parse the header and make some verifications.
425 * See RFC 1889 & RFC 2250. */
427 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
428 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
429 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
431 if( i_rtp_version != 2 )
433 msg_Dbg( p_access, "no supported RTP header detected" );
434 p_access->pf_block = BlockUDP;
438 switch( i_payload_type )
441 msg_Dbg( p_access, "detected TS over RTP" );
442 /* Disabled because it is auto-detected. */
443 /* p_access->psz_demux = strdup( "ts" ); */
447 msg_Dbg( p_access, "detected MPEG audio over RTP" );
448 p_access->psz_demux = strdup( "mpga" );
452 msg_Dbg( p_access, "detected MPEG video over RTP" );
453 p_access->psz_demux = strdup( "mpgv" );
457 msg_Dbg( p_access, "no RTP header detected" );
458 p_access->pf_block = BlockUDP;
462 p_access->pf_block = BlockRTP;
464 return BlockParseRTP( p_access, p_block );