1 /*****************************************************************************
2 * udp.c: raw UDP & RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.9 2002/12/30 11:49:32 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Tristan Leteurtre <tooney@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <sys/types.h>
36 #include <vlc/input.h>
40 #elif defined( _MSC_VER ) && defined( _WIN32 )
45 # include <winsock2.h>
46 # include <ws2tcpip.h>
48 # define IN_MULTICAST(a) IN_CLASSD(a)
51 # include <sys/socket.h>
56 #define RTP_HEADER_LEN 12
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close ( vlc_object_t * );
63 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
64 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
65 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
67 /*****************************************************************************
69 *****************************************************************************/
70 #define CACHING_TEXT N_("caching value in ms")
71 #define CACHING_LONGTEXT N_( \
72 "Allows you to modify the default caching value for udp streams. This " \
73 "value should be set in miliseconds units." )
76 set_description( _("raw UDP access module") );
77 add_category_hint( N_("udp"), NULL );
78 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT );
79 set_capability( "access", 0 );
80 add_shortcut( "udp" );
81 add_shortcut( "udpstream" );
82 add_shortcut( "udp4" );
83 add_shortcut( "udp6" );
84 add_shortcut( "rtp" );
85 add_shortcut( "rtp4" );
86 add_shortcut( "rtp6" );
87 set_callbacks( Open, Close );
90 /*****************************************************************************
91 * Open: open the socket
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 input_thread_t * p_input = (input_thread_t *)p_this;
96 input_socket_t * p_access_data;
98 char * psz_network = "";
99 char * psz_name = strdup(p_input->psz_name);
100 char * psz_parser = psz_name;
101 char * psz_server_addr = "";
102 char * psz_server_port = "";
103 char * psz_bind_addr = "";
104 char * psz_bind_port = "";
105 int i_bind_port = 0, i_server_port = 0;
106 network_socket_t socket_desc;
108 if( config_GetInt( p_input, "ipv4" ) )
110 psz_network = "ipv4";
112 if( config_GetInt( p_input, "ipv6" ) )
114 psz_network = "ipv6";
117 if( *p_input->psz_access )
119 /* Find out which shortcut was used */
120 if( !strncmp( p_input->psz_access, "udp6", 5 ) )
122 psz_network = "ipv6";
124 else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
126 psz_network = "ipv4";
130 /* Parse psz_name syntax :
131 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
133 if( *psz_parser && *psz_parser != '@' )
136 psz_server_addr = psz_parser;
138 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
140 if( *psz_parser == '[' )
143 while( *psz_parser && *psz_parser != ']' )
151 if( *psz_parser == ':' )
153 /* Found server port */
154 *psz_parser = '\0'; /* Terminate server name */
156 psz_server_port = psz_parser;
158 while( *psz_parser && *psz_parser != '@' )
165 if( *psz_parser == '@' )
167 /* Found bind address or bind port */
168 *psz_parser = '\0'; /* Terminate server port or name if necessary */
171 if( *psz_parser && *psz_parser != ':' )
173 /* Found bind address */
174 psz_bind_addr = psz_parser;
176 while( *psz_parser && *psz_parser != ':' )
178 if( *psz_parser == '[' )
181 while( *psz_parser && *psz_parser != ']' )
190 if( *psz_parser == ':' )
192 /* Found bind port */
193 *psz_parser = '\0'; /* Terminate bind address if necessary */
196 psz_bind_port = psz_parser;
200 /* Convert ports format */
201 if( *psz_server_port )
203 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
206 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
214 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
217 msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
223 if( i_bind_port == 0 )
225 i_bind_port = config_GetInt( p_this, "server-port" );
228 p_input->pf_read = RTPChoose;
229 p_input->pf_set_program = input_SetProgram;
230 p_input->pf_set_area = NULL;
231 p_input->pf_seek = NULL;
233 vlc_mutex_lock( &p_input->stream.stream_lock );
234 p_input->stream.b_pace_control = 0;
235 p_input->stream.b_seekable = 0;
236 p_input->stream.p_selected_area->i_tell = 0;
237 p_input->stream.i_method = INPUT_METHOD_NETWORK;
238 vlc_mutex_unlock( &p_input->stream.stream_lock );
240 if( *psz_server_addr || i_server_port )
242 msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
243 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
244 psz_server_addr, i_server_port);
245 msg_Err( p_input, "or local port, type : %s:@%s:%d",
246 *p_input->psz_access ? p_input->psz_access : "udp",
247 psz_server_addr, i_server_port );
250 psz_server_addr = "";
253 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
254 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
256 /* Prepare the network_socket_t structure */
257 socket_desc.i_type = NETWORK_UDP;
258 socket_desc.psz_bind_addr = psz_bind_addr;
259 socket_desc.i_bind_port = i_bind_port;
260 socket_desc.psz_server_addr = psz_server_addr;
261 socket_desc.i_server_port = i_server_port;
263 /* Find an appropriate network module */
264 p_input->p_private = (void*) &socket_desc;
265 p_network = module_Need( p_input, "network", psz_network );
267 if( p_network == NULL )
271 module_Unneed( p_input, p_network );
273 p_access_data = malloc( sizeof(input_socket_t) );
274 p_input->p_access_data = (access_sys_t *)p_access_data;
276 if( p_access_data == NULL )
278 msg_Err( p_input, "out of memory" );
282 p_access_data->i_handle = socket_desc.i_handle;
283 p_input->i_mtu = socket_desc.i_mtu;
285 /* Update default_pts to a suitable value for udp access */
286 p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
291 /*****************************************************************************
292 * Close: free unused data structures
293 *****************************************************************************/
294 static void Close( vlc_object_t *p_this )
296 input_thread_t * p_input = (input_thread_t *)p_this;
297 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
299 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
302 CloseHandle( (HANDLE)p_access_data->i_handle );
303 #elif defined( WIN32 )
304 closesocket( p_access_data->i_handle );
306 close( p_access_data->i_handle );
309 free( p_access_data );
312 /*****************************************************************************
313 * Read: read on a file descriptor, checking b_die periodically
314 *****************************************************************************/
315 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
321 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
322 struct timeval timeout;
326 /* Initialize file descriptor set */
328 FD_SET( p_access_data->i_handle, &fds );
330 /* We'll wait 0.5 second if nothing happens */
332 timeout.tv_usec = 500000;
334 /* Find if some data is available */
335 i_ret = select( p_access_data->i_handle + 1, &fds,
336 NULL, NULL, &timeout );
338 if( i_ret == -1 && errno != EINTR )
340 msg_Err( p_input, "network select error (%s)", strerror(errno) );
344 ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
348 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
359 /*****************************************************************************
360 * RTPRead : read from the network, and parse the RTP header
361 *****************************************************************************/
362 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
369 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
371 /* Get the raw data from the socket.
372 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
373 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
375 if ( !i_ret ) return 0;
377 /* Parse the header and make some verifications.
378 * See RFC 1889 & RFC 2250. */
380 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
381 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
382 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
384 if ( i_rtp_version != 2 )
385 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
387 if ( i_payload_type != 33 && i_payload_type != 14
388 && i_payload_type != 32 )
389 msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
391 /* Return the packet without the RTP header. */
392 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
394 if ( (size_t)i_ret > i_len )
396 /* This should NOT happen. */
397 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
401 p_input->p_vlc->pf_memcpy( p_buffer,
402 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
408 /*****************************************************************************
409 * RTPChoose : read from the network, and decide whether it's UDP or RTP
410 *****************************************************************************/
411 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
418 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
420 /* Get the raw data from the socket.
421 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
422 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
424 if ( !i_ret ) return 0;
426 /* Check that it's not TS. */
427 if ( p_tmp_buffer[0] == 0x47 )
429 msg_Dbg( p_input, "detected TS over raw UDP" );
430 p_input->pf_read = Read;
431 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
435 /* Parse the header and make some verifications.
436 * See RFC 1889 & RFC 2250. */
438 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
439 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
440 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
442 if ( i_rtp_version != 2 )
444 msg_Dbg( p_input, "no RTP header detected" );
445 p_input->pf_read = Read;
446 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
450 switch ( i_payload_type )
453 msg_Dbg( p_input, "detected TS over RTP" );
457 msg_Dbg( p_input, "detected MPEG audio over RTP" );
461 msg_Dbg( p_input, "detected MPEG video over RTP" );
465 msg_Dbg( p_input, "no RTP header detected" );
466 p_input->pf_read = Read;
467 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
471 /* Return the packet without the RTP header. */
472 p_input->pf_read = RTPRead;
473 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
475 if ( (size_t)i_ret > i_len )
477 /* This should NOT happen. */
478 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
482 p_input->p_vlc->pf_memcpy( p_buffer,
483 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,