1 /*****************************************************************************
2 * udp.c: raw UDP & RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.17 2003/03/24 19:12:16 gbazin 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>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
47 # include <winsock2.h>
48 # include <ws2tcpip.h>
50 # define IN_MULTICAST(a) IN_CLASSD(a)
53 # include <sys/socket.h>
58 #define RTP_HEADER_LEN 12
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
65 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
66 static ssize_t RTPRead ( input_thread_t *, byte_t *, size_t );
67 static ssize_t RTPChoose( input_thread_t *, byte_t *, size_t );
69 /*****************************************************************************
71 *****************************************************************************/
72 #define CACHING_TEXT N_("caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74 "Allows you to modify the default caching value for udp streams. This " \
75 "value should be set in miliseconds units." )
78 set_description( _("raw UDP access module") );
79 add_category_hint( N_("udp"), NULL , VLC_TRUE );
80 add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
81 set_capability( "access", 0 );
82 add_shortcut( "udp" );
83 add_shortcut( "udpstream" );
84 add_shortcut( "udp4" );
85 add_shortcut( "udp6" );
86 add_shortcut( "rtp" );
87 add_shortcut( "rtp4" );
88 add_shortcut( "rtp6" );
89 set_callbacks( Open, Close );
92 /*****************************************************************************
93 * Open: open the socket
94 *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
97 input_thread_t * p_input = (input_thread_t *)p_this;
98 input_socket_t * p_access_data;
100 char * psz_network = "";
101 char * psz_name = strdup(p_input->psz_name);
102 char * psz_parser = psz_name;
103 char * psz_server_addr = "";
104 char * psz_server_port = "";
105 char * psz_bind_addr = "";
106 char * psz_bind_port = "";
107 int i_bind_port = 0, i_server_port = 0;
108 network_socket_t socket_desc;
110 if( config_GetInt( p_input, "ipv4" ) )
112 psz_network = "ipv4";
114 if( config_GetInt( p_input, "ipv6" ) )
116 psz_network = "ipv6";
119 if( *p_input->psz_access )
121 /* Find out which shortcut was used */
122 if( !strncmp( p_input->psz_access, "udp6", 5 ) )
124 psz_network = "ipv6";
126 else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
128 psz_network = "ipv4";
132 /* Parse psz_name syntax :
133 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
135 if( *psz_parser && *psz_parser != '@' )
138 psz_server_addr = psz_parser;
140 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
142 if( *psz_parser == '[' )
145 while( *psz_parser && *psz_parser != ']' )
153 if( *psz_parser == ':' )
155 /* Found server port */
156 *psz_parser = '\0'; /* Terminate server name */
158 psz_server_port = psz_parser;
160 while( *psz_parser && *psz_parser != '@' )
167 if( *psz_parser == '@' )
169 /* Found bind address or bind port */
170 *psz_parser = '\0'; /* Terminate server port or name if necessary */
173 if( *psz_parser && *psz_parser != ':' )
175 /* Found bind address */
176 psz_bind_addr = psz_parser;
178 while( *psz_parser && *psz_parser != ':' )
180 if( *psz_parser == '[' )
183 while( *psz_parser && *psz_parser != ']' )
192 if( *psz_parser == ':' )
194 /* Found bind port */
195 *psz_parser = '\0'; /* Terminate bind address if necessary */
198 psz_bind_port = psz_parser;
202 /* Convert ports format */
203 if( *psz_server_port )
205 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
208 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
216 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
219 msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
225 if( i_bind_port == 0 )
227 i_bind_port = config_GetInt( p_this, "server-port" );
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 = 0;
237 p_input->stream.b_seekable = 0;
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 if( *psz_server_addr || i_server_port )
244 msg_Err( p_input, "this UDP syntax is deprecated; the server argument will be");
245 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
246 psz_server_addr, i_server_port);
247 msg_Err( p_input, "or local port, type : %s:@%s:%d",
248 *p_input->psz_access ? p_input->psz_access : "udp",
249 psz_server_addr, i_server_port );
252 psz_server_addr = "";
255 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
256 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
258 /* Prepare the network_socket_t structure */
259 socket_desc.i_type = NETWORK_UDP;
260 socket_desc.psz_bind_addr = psz_bind_addr;
261 socket_desc.i_bind_port = i_bind_port;
262 socket_desc.psz_server_addr = psz_server_addr;
263 socket_desc.i_server_port = i_server_port;
265 /* Find an appropriate network module */
266 p_input->p_private = (void*) &socket_desc;
267 p_network = module_Need( p_input, "network", psz_network );
269 if( p_network == NULL )
273 module_Unneed( p_input, p_network );
275 p_access_data = malloc( sizeof(input_socket_t) );
276 p_input->p_access_data = (access_sys_t *)p_access_data;
278 if( p_access_data == NULL )
280 msg_Err( p_input, "out of memory" );
284 p_access_data->i_handle = socket_desc.i_handle;
285 p_input->i_mtu = socket_desc.i_mtu;
287 /* Update default_pts to a suitable value for udp access */
288 p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
293 /*****************************************************************************
294 * Close: free unused data structures
295 *****************************************************************************/
296 static void Close( vlc_object_t *p_this )
298 input_thread_t * p_input = (input_thread_t *)p_this;
299 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
301 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
304 CloseHandle( (HANDLE)p_access_data->i_handle );
305 #elif defined( WIN32 )
306 closesocket( p_access_data->i_handle );
308 close( p_access_data->i_handle );
311 free( p_access_data );
314 /*****************************************************************************
315 * Read: read on a file descriptor, checking b_die periodically
316 *****************************************************************************/
317 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
323 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
324 struct timeval timeout;
329 /* Initialize file descriptor set */
331 FD_SET( p_access_data->i_handle, &fds );
333 /* We'll wait 0.5 second if nothing happens */
335 timeout.tv_usec = 500000;
337 /* Find if some data is available */
338 while( (i_ret = select( p_access_data->i_handle + 1, &fds,
339 NULL, NULL, &timeout )) == 0
340 || (i_ret < 0 && errno == EINTR) )
343 timeout.tv_usec = 500000;
345 if( p_input->b_die || p_input->b_error )
353 msg_Err( p_input, "network select error (%s)", strerror(errno) );
357 i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
362 /* On win32 recv() will fail if the datagram doesn't fit inside
363 * the passed buffer, even though the buffer will be filled with
364 * the first part of the datagram. */
365 if( WSAGetLastError() == WSAEMSGSIZE )
367 msg_Err( p_input, "recv() failed. "
368 "Increase the mtu size (--mtu option)" );
373 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
381 /*****************************************************************************
382 * RTPRead : read from the network, and parse the RTP header
383 *****************************************************************************/
384 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
391 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
393 /* Get the raw data from the socket.
394 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
395 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
397 if ( !i_ret ) return 0;
399 /* Parse the header and make some verifications.
400 * See RFC 1889 & RFC 2250. */
402 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
403 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
404 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
406 if ( i_rtp_version != 2 )
407 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
409 if ( i_payload_type != 33 && i_payload_type != 14
410 && i_payload_type != 32 )
411 msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
413 /* Return the packet without the RTP header. */
414 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
416 if ( (size_t)i_ret > i_len )
418 /* This should NOT happen. */
419 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
423 p_input->p_vlc->pf_memcpy( p_buffer,
424 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
430 /*****************************************************************************
431 * RTPChoose : read from the network, and decide whether it's UDP or RTP
432 *****************************************************************************/
433 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
440 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
442 /* Get the raw data from the socket.
443 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
444 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
446 if ( !i_ret ) return 0;
448 /* Check that it's not TS. */
449 if ( p_tmp_buffer[0] == 0x47 )
451 msg_Dbg( p_input, "detected TS over raw UDP" );
452 p_input->pf_read = Read;
453 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
457 /* Parse the header and make some verifications.
458 * See RFC 1889 & RFC 2250. */
460 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
461 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
462 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
464 if ( i_rtp_version != 2 )
466 msg_Dbg( p_input, "no RTP header detected" );
467 p_input->pf_read = Read;
468 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
472 switch ( i_payload_type )
475 msg_Dbg( p_input, "detected TS over RTP" );
479 msg_Dbg( p_input, "detected MPEG audio over RTP" );
483 msg_Dbg( p_input, "detected MPEG video over RTP" );
487 msg_Dbg( p_input, "no RTP header detected" );
488 p_input->pf_read = Read;
489 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
493 /* Return the packet without the RTP header. */
494 p_input->pf_read = RTPRead;
495 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
497 if ( (size_t)i_ret > i_len )
499 /* This should NOT happen. */
500 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
504 p_input->p_vlc->pf_memcpy( p_buffer,
505 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,