1 /*****************************************************************************
2 * udp.c: raw UDP & RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.27 2004/01/21 10:22:31 fenrir Exp $
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 miliseconds units." )
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_description( _("UDP/RTP input") );
51 add_category_hint( N_("UDP"), NULL , VLC_TRUE );
52 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
53 set_capability( "access", 0 );
54 add_shortcut( "udp" );
55 add_shortcut( "udpstream" );
56 add_shortcut( "udp4" );
57 add_shortcut( "udp6" );
58 add_shortcut( "rtp" );
59 add_shortcut( "rtp4" );
60 add_shortcut( "rtp6" );
61 set_callbacks( Open, Close );
64 /*****************************************************************************
66 *****************************************************************************/
67 #define RTP_HEADER_LEN 12
69 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
70 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
71 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
78 /*****************************************************************************
79 * Open: open the socket
80 *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
83 input_thread_t *p_input = (input_thread_t *)p_this;
86 char * psz_name = strdup(p_input->psz_name);
87 char * psz_parser = psz_name;
88 char * psz_server_addr = "";
89 char * psz_server_port = "";
90 char * psz_bind_addr = "";
91 char * psz_bind_port = "";
92 int i_bind_port = 0, i_server_port = 0;
96 /* First set ipv4/ipv6 */
97 var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
98 var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
100 if( *p_input->psz_access )
102 /* Find out which shortcut was used */
103 if( !strncmp( p_input->psz_access, "udp4", 6 ) || !strncmp( p_input->psz_access, "rtp4", 6 ))
105 val.b_bool = VLC_TRUE;
106 var_Set( p_input, "ipv4", val );
108 val.b_bool = VLC_FALSE;
109 var_Set( p_input, "ipv6", val );
111 else if( !strncmp( p_input->psz_access, "udp6", 6 ) || !strncmp( p_input->psz_access, "rtp6", 6 ) )
113 val.b_bool = VLC_TRUE;
114 var_Set( p_input, "ipv6", val );
116 val.b_bool = VLC_FALSE;
117 var_Set( p_input, "ipv4", val );
121 /* Parse psz_name syntax :
122 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
123 if( *psz_parser && *psz_parser != '@' )
126 psz_server_addr = psz_parser;
128 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
130 if( *psz_parser == '[' )
133 while( *psz_parser && *psz_parser != ']' )
141 if( *psz_parser == ':' )
143 /* Found server port */
144 *psz_parser++ = '\0'; /* Terminate server name */
145 psz_server_port = psz_parser;
147 while( *psz_parser && *psz_parser != '@' )
154 if( *psz_parser == '@' )
156 /* Found bind address or bind port */
157 *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
159 if( *psz_parser && *psz_parser != ':' )
161 /* Found bind address */
162 psz_bind_addr = psz_parser;
164 while( *psz_parser && *psz_parser != ':' )
166 if( *psz_parser == '[' )
169 while( *psz_parser && *psz_parser != ']' )
178 if( *psz_parser == ':' )
180 /* Found bind port */
181 *psz_parser++ = '\0'; /* Terminate bind address if necessary */
182 psz_bind_port = psz_parser;
186 i_server_port = strtol( psz_server_port, NULL, 10 );
187 if( ( i_bind_port = strtol( psz_bind_port, NULL, 10 ) ) == 0 )
189 i_bind_port = config_GetInt( p_this, "server-port" );
192 if( *psz_server_addr || i_server_port )
194 msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
195 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
196 psz_server_addr, i_server_port);
197 msg_Err( p_input, "or local port, type : %s:@%s:%d",
198 *p_input->psz_access ? p_input->psz_access : "udp",
199 psz_server_addr, i_server_port );
202 psz_server_addr = "";
205 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
206 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
208 p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
211 msg_Err( p_input, "out of memory" );
215 p_sys->fd = net_OpenUDP( p_input, psz_bind_addr, i_bind_port,
216 psz_server_addr, i_server_port );
219 msg_Err( p_input, "cannot open socket" );
227 p_input->i_mtu = config_GetInt( p_this, "mtu" );
229 /* fill p_input fields */
230 p_input->pf_read = RTPChoose;
231 p_input->pf_set_program = input_SetProgram;
232 p_input->pf_set_area = NULL;
233 p_input->pf_seek = NULL;
235 vlc_mutex_lock( &p_input->stream.stream_lock );
236 p_input->stream.b_pace_control = VLC_FALSE;
237 p_input->stream.b_seekable = VLC_FALSE;
238 p_input->stream.p_selected_area->i_tell = 0;
239 p_input->stream.i_method = INPUT_METHOD_NETWORK;
240 vlc_mutex_unlock( &p_input->stream.stream_lock );
242 /* Update default_pts to a suitable value for udp access */
243 var_Create( p_input, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
244 var_Get( p_input, "udp-caching", &val );
245 p_input->i_pts_delay = val.i_int * 1000;
250 /*****************************************************************************
251 * Close: free unused data structures
252 *****************************************************************************/
253 static void Close( vlc_object_t *p_this )
255 input_thread_t *p_input = (input_thread_t *)p_this;
256 access_sys_t *p_sys = p_input->p_access_data;
258 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
260 net_Close( p_sys->fd );
265 /*****************************************************************************
266 * Read: read on a file descriptor, checking b_die periodically
267 *****************************************************************************/
268 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
270 access_sys_t *p_sys = p_input->p_access_data;
272 return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
275 /*****************************************************************************
276 * RTPRead : read from the network, and parse the RTP header
277 *****************************************************************************/
278 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
286 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
288 /* Get the raw data from the socket.
289 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
290 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
292 if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
294 /* Parse the header and make some verifications.
295 * See RFC 1889 & RFC 2250. */
297 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
298 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
299 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
301 if ( i_rtp_version != 2 )
302 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
304 if( i_payload_type == 14 )
308 else if( i_payload_type != 33 && i_payload_type != 32 )
310 msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
312 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
314 /* A CSRC extension field is 32 bits in size (4 bytes) */
315 if ( i_ret < i_skip )
317 /* Packet is not big enough to hold the complete RTP_HEADER with
320 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
324 /* Return the packet without the RTP header. */
327 if ( (size_t)i_ret > i_len )
329 /* This should NOT happen. */
330 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
334 p_input->p_vlc->pf_memcpy( p_buffer, &p_tmp_buffer[i_skip], i_ret );
339 /*****************************************************************************
340 * RTPChoose : read from the network, and decide whether it's UDP or RTP
341 *****************************************************************************/
342 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
349 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
351 /* Get the raw data from the socket.
352 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
353 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
355 if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
357 /* Check that it's not TS. */
358 if ( p_tmp_buffer[0] == 0x47 )
360 msg_Dbg( p_input, "detected TS over raw UDP" );
361 p_input->pf_read = Read;
362 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
366 /* Parse the header and make some verifications.
367 * See RFC 1889 & RFC 2250. */
369 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
370 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
371 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
373 if ( i_rtp_version != 2 )
375 msg_Dbg( p_input, "no supported RTP header detected" );
376 p_input->pf_read = Read;
377 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
381 switch ( i_payload_type )
384 msg_Dbg( p_input, "detected TS over RTP" );
388 msg_Dbg( p_input, "detected MPEG audio over RTP" );
389 if( !p_input->psz_demux || *p_input->psz_demux == '\0' )
391 p_input->psz_demux = "mp3";
396 msg_Dbg( p_input, "detected MPEG video over RTP" );
400 msg_Dbg( p_input, "no RTP header detected" );
401 p_input->pf_read = Read;
402 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
406 p_input->pf_read = RTPRead;
408 /* A CSRC extension field is 32 bits in size (4 bytes) */
409 if( i_ret < RTP_HEADER_LEN + 4*i_CSRC_count )
411 /* Packet is not big enough to hold the complete RTP_HEADER with
414 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
418 /* Return the packet without the RTP header. */
419 i_ret -= RTP_HEADER_LEN + 4*i_CSRC_count;
421 if ( (size_t)i_ret > i_len )
423 /* This should NOT happen. */
424 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
428 p_input->p_vlc->pf_memcpy( p_buffer, &p_tmp_buffer[RTP_HEADER_LEN + 4*i_CSRC_count], i_ret );