1 /*****************************************************************************
2 * udp.c: raw UDP & RTP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Tristan Leteurtre <tooney@via.ecp.fr>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman@wxs.nl>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
34 #include <vlc/input.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43 "Allows you to modify the default caching value for UDP streams. This " \
44 "value should be set in millisecond units." )
46 #define AUTO_MTU_TEXT N_("Autodetection of MTU")
47 #define AUTO_MTU_LONGTEXT N_( \
48 "Allows growing the MTU if truncated packets are found" )
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_description( _("UDP/RTP input") );
56 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
57 CACHING_LONGTEXT, VLC_TRUE );
58 add_bool( "udp-auto-mtu", 0, NULL,
59 AUTO_MTU_TEXT, AUTO_MTU_LONGTEXT, VLC_TRUE );
61 set_capability( "access2", 0 );
62 add_shortcut( "udp" );
63 add_shortcut( "udpstream" );
64 add_shortcut( "udp4" );
65 add_shortcut( "udp6" );
66 add_shortcut( "rtp" );
67 add_shortcut( "rtp4" );
68 add_shortcut( "rtp6" );
69 set_callbacks( Open, Close );
72 /*****************************************************************************
74 *****************************************************************************/
75 #define RTP_HEADER_LEN 12
77 static block_t *BlockUDP( access_t * );
78 static block_t *BlockRTP( access_t * );
79 static block_t *BlockChoose( access_t * );
80 static int Control( access_t *, int, va_list );
87 vlc_bool_t b_auto_mtu;
90 /*****************************************************************************
91 * Open: open the socket
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 access_t *p_access = (access_t*)p_this;
98 char *psz_name = strdup( p_access->psz_path );
99 char *psz_parser = psz_name;
100 char *psz_server_addr = "";
101 char *psz_server_port = "";
102 char *psz_bind_addr = "";
103 char *psz_bind_port = "";
105 int 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 )
115 /* Find out which shortcut was used */
116 if( !strncmp( p_access->psz_access, "udp4", 6 ) ||
117 !strncmp( p_access->psz_access, "rtp4", 6 ))
119 val.b_bool = VLC_TRUE;
120 var_Set( p_access, "ipv4", val );
122 val.b_bool = VLC_FALSE;
123 var_Set( p_access, "ipv6", val );
125 else if( !strncmp( p_access->psz_access, "udp6", 6 ) ||
126 !strncmp( p_access->psz_access, "rtp6", 6 ) )
128 val.b_bool = VLC_TRUE;
129 var_Set( p_access, "ipv6", val );
131 val.b_bool = VLC_FALSE;
132 var_Set( p_access, "ipv4", val );
136 /* Parse psz_name syntax :
137 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
138 if( *psz_parser && *psz_parser != '@' )
141 psz_server_addr = psz_parser;
143 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
145 if( *psz_parser == '[' )
148 while( *psz_parser && *psz_parser != ']' )
156 if( *psz_parser == ':' )
158 /* Found server port */
159 *psz_parser++ = '\0'; /* Terminate server name */
160 psz_server_port = psz_parser;
162 while( *psz_parser && *psz_parser != '@' )
169 if( *psz_parser == '@' )
171 /* Found bind address or bind port */
172 *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
174 if( *psz_parser && *psz_parser != ':' )
176 /* Found bind address */
177 psz_bind_addr = psz_parser;
179 while( *psz_parser && *psz_parser != ':' )
181 if( *psz_parser == '[' )
184 while( *psz_parser && *psz_parser != ']' )
193 if( *psz_parser == ':' )
195 /* Found bind port */
196 *psz_parser++ = '\0'; /* Terminate bind address if necessary */
197 psz_bind_port = psz_parser;
201 i_server_port = strtol( psz_server_port, NULL, 10 );
202 if( ( i_bind_port = strtol( psz_bind_port, NULL, 10 ) ) == 0 )
204 var_Create( p_access, "server-port", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
205 var_Get( p_access, "server-port", &val );
206 i_bind_port = val.i_int;
209 msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
210 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
212 /* Set up p_access */
213 p_access->pf_read = NULL;
214 p_access->pf_block = BlockChoose;
215 p_access->pf_control = Control;
216 p_access->pf_seek = NULL;
217 p_access->info.i_update = 0;
218 p_access->info.i_size = 0;
219 p_access->info.i_pos = 0;
220 p_access->info.b_eof = VLC_FALSE;
221 p_access->info.i_title = 0;
222 p_access->info.i_seekpoint = 0;
224 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
225 p_sys->fd = net_OpenUDP( p_access, psz_bind_addr, i_bind_port,
226 psz_server_addr, i_server_port );
229 msg_Err( p_access, "cannot open socket" );
237 var_Create( p_access, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
238 var_Get( p_access, "mtu", &val);
239 p_sys->i_mtu = val.i_int > 0 ? val.i_int : 1500; /* avoid problem */
241 var_Create( p_access, "udp-auto-mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
242 var_Get( p_access, "udp-auto-mtu", &val);
243 p_sys->b_auto_mtu = val.b_bool;
245 /* Update default_pts to a suitable value for udp access */
246 var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
251 /*****************************************************************************
252 * Close: free unused data structures
253 *****************************************************************************/
254 static void Close( vlc_object_t *p_this )
256 access_t *p_access = (access_t*)p_this;
257 access_sys_t *p_sys = p_access->p_sys;
259 net_Close( p_sys->fd );
263 /*****************************************************************************
265 *****************************************************************************/
266 static int Control( access_t *p_access, int i_query, va_list args )
268 access_sys_t *p_sys = p_access->p_sys;
277 case ACCESS_CAN_SEEK:
278 case ACCESS_CAN_FASTSEEK:
279 case ACCESS_CAN_PAUSE:
280 case ACCESS_CAN_CONTROL_PACE:
281 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
282 *pb_bool = VLC_FALSE;
286 pi_int = (int*)va_arg( args, int * );
287 *pi_int = p_sys->i_mtu;
290 case ACCESS_GET_PTS_DELAY:
291 pi_64 = (int64_t*)va_arg( args, int64_t * );
292 var_Get( p_access, "udp-caching", &val );
293 *pi_64 = val.i_int * 1000;
297 case ACCESS_SET_PAUSE_STATE:
298 case ACCESS_GET_TITLE_INFO:
299 case ACCESS_SET_TITLE:
300 case ACCESS_SET_SEEKPOINT:
304 msg_Err( p_access, "unimplemented query in control" );
311 /*****************************************************************************
313 *****************************************************************************/
314 static block_t *BlockUDP( access_t *p_access )
316 access_sys_t *p_sys = p_access->p_sys;
320 p_block = block_New( p_access, p_sys->i_mtu );
321 p_block->i_buffer = net_Read( p_access, p_sys->fd, p_block->p_buffer, p_sys->i_mtu, VLC_FALSE );
322 if( p_block->i_buffer <= 0 )
324 block_Release( p_block );
328 if( p_block->i_buffer >= p_sys->i_mtu && p_sys->b_auto_mtu )
330 /* Increase by 10% */
331 p_sys->i_mtu += ( p_sys->i_mtu + 9 ) / 10;
332 msg_Dbg( p_access, "increasing MTU to %d", p_sys->i_mtu );
338 /*****************************************************************************
339 * BlockParseRTP/BlockRTP:
340 *****************************************************************************/
341 static block_t *BlockParseRTP( access_t *p_access, block_t *p_block )
348 if( p_block->i_buffer < RTP_HEADER_LEN )
351 /* Parse the header and make some verifications.
352 * See RFC 1889 & RFC 2250. */
353 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
354 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
355 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
357 if ( i_rtp_version != 2 )
358 msg_Dbg( p_access, "RTP version is %u, should be 2", i_rtp_version );
360 if( i_payload_type == 14 )
362 else if( i_payload_type != 33 && i_payload_type != 32 )
363 msg_Dbg( p_access, "unsupported RTP payload type (%u)", i_payload_type );
365 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
367 /* A CSRC extension field is 32 bits in size (4 bytes) */
368 if( i_skip >= p_block->i_buffer )
371 /* Return the packet without the RTP header. */
372 p_block->i_buffer -= i_skip;
373 p_block->p_buffer += i_skip;
378 msg_Warn( p_access, "received a too short packet for RTP" );
379 block_Release( p_block );
383 static block_t *BlockRTP( access_t *p_access )
385 return BlockParseRTP( p_access, BlockUDP( p_access ) );
388 /*****************************************************************************
389 * BlockChoose: decide between RTP and UDP
390 *****************************************************************************/
391 static block_t *BlockChoose( access_t *p_access )
398 if( ( p_block = BlockUDP( p_access ) ) == NULL )
401 if( p_block->p_buffer[0] == 0x47 )
403 msg_Dbg( p_access, "detected TS over raw UDP" );
404 p_access->pf_block = BlockUDP;
408 if( p_block->i_buffer < RTP_HEADER_LEN )
411 /* Parse the header and make some verifications.
412 * See RFC 1889 & RFC 2250. */
414 i_rtp_version = ( p_block->p_buffer[0] & 0xC0 ) >> 6;
415 i_CSRC_count = ( p_block->p_buffer[0] & 0x0F );
416 i_payload_type = ( p_block->p_buffer[1] & 0x7F );
418 if( i_rtp_version != 2 )
420 msg_Dbg( p_access, "no supported RTP header detected" );
421 p_access->pf_block = BlockUDP;
425 switch( i_payload_type )
428 msg_Dbg( p_access, "detected TS over RTP" );
429 p_access->psz_demux = strdup( "ts2" );
433 msg_Dbg( p_access, "detected MPEG audio over RTP" );
434 p_access->psz_demux = strdup( "mp3" );
438 msg_Dbg( p_access, "detected MPEG video over RTP" );
439 p_access->psz_demux = strdup( "es" );
443 msg_Dbg( p_access, "no RTP header detected" );
444 p_access->pf_block = BlockUDP;
448 p_access->pf_block = BlockRTP;
450 return BlockParseRTP( p_access, p_block );