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 miliseconds units." )
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_description( _("UDP/RTP input") );
52 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
53 CACHING_LONGTEXT, VLC_TRUE );
55 set_capability( "access", 0 );
56 add_shortcut( "udp" );
57 add_shortcut( "udpstream" );
58 add_shortcut( "udp4" );
59 add_shortcut( "udp6" );
60 add_shortcut( "rtp" );
61 add_shortcut( "rtp4" );
62 add_shortcut( "rtp6" );
63 set_callbacks( Open, Close );
66 /*****************************************************************************
68 *****************************************************************************/
69 #define RTP_HEADER_LEN 12
71 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
72 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
73 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
80 /*****************************************************************************
81 * Open: open the socket
82 *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
85 input_thread_t *p_input = (input_thread_t *)p_this;
88 char * psz_name = strdup(p_input->psz_name);
89 char * psz_parser = psz_name;
90 char * psz_server_addr = "";
91 char * psz_server_port = "";
92 char * psz_bind_addr = "";
93 char * psz_bind_port = "";
94 int i_bind_port = 0, i_server_port = 0;
98 /* First set ipv4/ipv6 */
99 var_Create( p_input, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
100 var_Create( p_input, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
102 if( *p_input->psz_access )
104 /* Find out which shortcut was used */
105 if( !strncmp( p_input->psz_access, "udp4", 6 ) ||
106 !strncmp( p_input->psz_access, "rtp4", 6 ))
108 val.b_bool = VLC_TRUE;
109 var_Set( p_input, "ipv4", val );
111 val.b_bool = VLC_FALSE;
112 var_Set( p_input, "ipv6", val );
114 else if( !strncmp( p_input->psz_access, "udp6", 6 ) ||
115 !strncmp( p_input->psz_access, "rtp6", 6 ) )
117 val.b_bool = VLC_TRUE;
118 var_Set( p_input, "ipv6", val );
120 val.b_bool = VLC_FALSE;
121 var_Set( p_input, "ipv4", val );
125 /* Parse psz_name syntax :
126 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
127 if( *psz_parser && *psz_parser != '@' )
130 psz_server_addr = psz_parser;
132 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
134 if( *psz_parser == '[' )
137 while( *psz_parser && *psz_parser != ']' )
145 if( *psz_parser == ':' )
147 /* Found server port */
148 *psz_parser++ = '\0'; /* Terminate server name */
149 psz_server_port = psz_parser;
151 while( *psz_parser && *psz_parser != '@' )
158 if( *psz_parser == '@' )
160 /* Found bind address or bind port */
161 *psz_parser++ = '\0'; /* Terminate server port or name if necessary */
163 if( *psz_parser && *psz_parser != ':' )
165 /* Found bind address */
166 psz_bind_addr = psz_parser;
168 while( *psz_parser && *psz_parser != ':' )
170 if( *psz_parser == '[' )
173 while( *psz_parser && *psz_parser != ']' )
182 if( *psz_parser == ':' )
184 /* Found bind port */
185 *psz_parser++ = '\0'; /* Terminate bind address if necessary */
186 psz_bind_port = psz_parser;
190 i_server_port = strtol( psz_server_port, NULL, 10 );
191 if( ( i_bind_port = strtol( psz_bind_port, NULL, 10 ) ) == 0 )
193 i_bind_port = config_GetInt( p_this, "server-port" );
196 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
197 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
199 p_sys = p_input->p_access_data = malloc( sizeof( access_sys_t ) );
202 msg_Err( p_input, "out of memory" );
206 p_sys->fd = net_OpenUDP( p_input, psz_bind_addr, i_bind_port,
207 psz_server_addr, i_server_port );
210 msg_Err( p_input, "cannot open socket" );
218 var_Create( p_input, "mtu", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
219 var_Get( p_input, "mtu", &val);
220 p_input->i_mtu = val.i_int;
222 /* fill p_input fields */
223 p_input->pf_read = RTPChoose;
224 p_input->pf_set_program = input_SetProgram;
225 p_input->pf_set_area = NULL;
226 p_input->pf_seek = NULL;
228 vlc_mutex_lock( &p_input->stream.stream_lock );
229 p_input->stream.b_pace_control = VLC_FALSE;
230 p_input->stream.b_seekable = VLC_FALSE;
231 p_input->stream.p_selected_area->i_tell = 0;
232 p_input->stream.i_method = INPUT_METHOD_NETWORK;
233 vlc_mutex_unlock( &p_input->stream.stream_lock );
235 /* Update default_pts to a suitable value for udp access */
236 var_Create( p_input, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
237 var_Get( p_input, "udp-caching", &val );
238 p_input->i_pts_delay = val.i_int * 1000;
243 /*****************************************************************************
244 * Close: free unused data structures
245 *****************************************************************************/
246 static void Close( vlc_object_t *p_this )
248 input_thread_t *p_input = (input_thread_t *)p_this;
249 access_sys_t *p_sys = p_input->p_access_data;
251 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
253 net_Close( p_sys->fd );
258 /*****************************************************************************
259 * Read: read on a file descriptor, checking b_die periodically
260 *****************************************************************************/
261 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
263 access_sys_t *p_sys = p_input->p_access_data;
265 return net_Read( p_input, p_sys->fd, p_buffer, i_len, VLC_FALSE );
268 /*****************************************************************************
269 * RTPRead : read from the network, and parse the RTP header
270 *****************************************************************************/
271 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
279 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
281 /* Get the raw data from the socket.
282 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
283 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
285 if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
287 /* Parse the header and make some verifications.
288 * See RFC 1889 & RFC 2250. */
290 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
291 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
292 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
294 if ( i_rtp_version != 2 )
295 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
297 if( i_payload_type == 14 )
301 else if( i_payload_type != 33 && i_payload_type != 32 )
303 msg_Dbg( p_input, "unsupported RTP payload type (%u)",
306 i_skip += RTP_HEADER_LEN + 4*i_CSRC_count;
308 /* A CSRC extension field is 32 bits in size (4 bytes) */
309 if ( i_ret < i_skip )
311 /* Packet is not big enough to hold the complete RTP_HEADER with
314 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
318 /* Return the packet without the RTP header. */
321 if ( (size_t)i_ret > i_len )
323 /* This should NOT happen. */
324 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
328 p_input->p_vlc->pf_memcpy( p_buffer, &p_tmp_buffer[i_skip], i_ret );
333 /*****************************************************************************
334 * RTPChoose : read from the network, and decide whether it's UDP or RTP
335 *****************************************************************************/
336 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
343 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
345 /* Get the raw data from the socket.
346 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
347 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
349 if ( i_ret <= 0 ) return 0; /* i_ret is at least 1 */
351 /* Check that it's not TS. */
352 if ( p_tmp_buffer[0] == 0x47 )
354 msg_Dbg( p_input, "detected TS over raw UDP" );
355 p_input->pf_read = Read;
356 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
360 /* Parse the header and make some verifications.
361 * See RFC 1889 & RFC 2250. */
363 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
364 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
365 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
367 if ( i_rtp_version != 2 )
369 msg_Dbg( p_input, "no supported RTP header detected" );
370 p_input->pf_read = Read;
371 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
375 switch ( i_payload_type )
378 msg_Dbg( p_input, "detected TS over RTP" );
382 msg_Dbg( p_input, "detected MPEG audio over RTP" );
383 if( !p_input->psz_demux || *p_input->psz_demux == '\0' )
385 p_input->psz_demux = "mp3";
390 msg_Dbg( p_input, "detected MPEG video over RTP" );
394 msg_Dbg( p_input, "no RTP header detected" );
395 p_input->pf_read = Read;
396 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
400 p_input->pf_read = RTPRead;
402 /* A CSRC extension field is 32 bits in size (4 bytes) */
403 if( i_ret < RTP_HEADER_LEN + 4*i_CSRC_count )
405 /* Packet is not big enough to hold the complete RTP_HEADER with
408 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
412 /* Return the packet without the RTP header. */
413 i_ret -= RTP_HEADER_LEN + 4*i_CSRC_count;
415 if ( (size_t)i_ret > i_len )
417 /* This should NOT happen. */
418 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
422 p_input->p_vlc->pf_memcpy( p_buffer,
423 &p_tmp_buffer[RTP_HEADER_LEN + 4*i_CSRC_count],