1 /*****************************************************************************
2 * udp.c: raw UDP & RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: udp.c,v 1.21 2003/07/31 23:44:49 fenrir 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( _("UDP/RTP input") );
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;
264 socket_desc.i_ttl = 0;
266 /* Find an appropriate network module */
267 p_input->p_private = (void*) &socket_desc;
268 p_network = module_Need( p_input, "network", psz_network );
270 if( p_network == NULL )
274 module_Unneed( p_input, p_network );
276 p_access_data = malloc( sizeof(input_socket_t) );
277 p_input->p_access_data = (access_sys_t *)p_access_data;
279 if( p_access_data == NULL )
281 msg_Err( p_input, "out of memory" );
285 p_access_data->i_handle = socket_desc.i_handle;
286 p_input->i_mtu = socket_desc.i_mtu;
288 /* Update default_pts to a suitable value for udp access */
289 p_input->i_pts_delay = config_GetInt( p_input, "udp-caching" ) * 1000;
294 /*****************************************************************************
295 * Close: free unused data structures
296 *****************************************************************************/
297 static void Close( vlc_object_t *p_this )
299 input_thread_t * p_input = (input_thread_t *)p_this;
300 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
302 msg_Info( p_input, "closing UDP target `%s'", p_input->psz_source );
305 CloseHandle( (HANDLE)p_access_data->i_handle );
306 #elif defined( WIN32 )
307 closesocket( p_access_data->i_handle );
309 close( p_access_data->i_handle );
312 free( p_access_data );
315 /*****************************************************************************
316 * Read: read on a file descriptor, checking b_die periodically
317 *****************************************************************************/
318 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
324 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
325 struct timeval timeout;
330 /* Initialize file descriptor set */
332 FD_SET( p_access_data->i_handle, &fds );
334 /* We'll wait 0.5 second if nothing happens */
336 timeout.tv_usec = 500000;
338 /* Find if some data is available */
339 while( (i_ret = select( p_access_data->i_handle + 1, &fds,
340 NULL, NULL, &timeout )) == 0
341 || (i_ret < 0 && errno == EINTR) )
344 FD_SET( p_access_data->i_handle, &fds );
346 timeout.tv_usec = 500000;
348 if( p_input->b_die || p_input->b_error )
356 msg_Err( p_input, "network select error (%s)", strerror(errno) );
360 i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
365 /* On win32 recv() will fail if the datagram doesn't fit inside
366 * the passed buffer, even though the buffer will be filled with
367 * the first part of the datagram. */
368 if( WSAGetLastError() == WSAEMSGSIZE )
370 msg_Err( p_input, "recv() failed. "
371 "Increase the mtu size (--mtu option)" );
376 msg_Err( p_input, "recv failed (%s)", strerror(errno) );
384 /*****************************************************************************
385 * RTPRead : read from the network, and parse the RTP header
386 *****************************************************************************/
387 static ssize_t RTPRead( input_thread_t * p_input, byte_t * p_buffer,
394 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
396 /* Get the raw data from the socket.
397 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
398 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
400 if ( i_ret < 0 ) return 0;
402 /* Parse the header and make some verifications.
403 * See RFC 1889 & RFC 2250. */
405 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
406 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
407 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
409 if ( i_rtp_version != 2 )
410 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
412 if ( i_payload_type != 33 && i_payload_type != 14
413 && i_payload_type != 32 )
414 msg_Dbg( p_input, "unsupported RTP payload type (%u)", i_payload_type );
416 /* Return the packet without the RTP header. */
417 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
419 if ( (size_t)i_ret > i_len )
421 /* This should NOT happen. */
422 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
426 p_input->p_vlc->pf_memcpy( p_buffer,
427 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
433 /*****************************************************************************
434 * RTPChoose : read from the network, and decide whether it's UDP or RTP
435 *****************************************************************************/
436 static ssize_t RTPChoose( input_thread_t * p_input, byte_t * p_buffer,
443 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
445 /* Get the raw data from the socket.
446 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
447 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
449 if ( i_ret < 0 ) return 0;
451 /* Check that it's not TS. */
452 if ( p_tmp_buffer[0] == 0x47 )
454 msg_Dbg( p_input, "detected TS over raw UDP" );
455 p_input->pf_read = Read;
456 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
460 /* Parse the header and make some verifications.
461 * See RFC 1889 & RFC 2250. */
463 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
464 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
465 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
467 if ( i_rtp_version != 2 )
469 msg_Dbg( p_input, "no RTP header detected" );
470 p_input->pf_read = Read;
471 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
475 switch ( i_payload_type )
478 msg_Dbg( p_input, "detected TS over RTP" );
482 msg_Dbg( p_input, "detected MPEG audio over RTP" );
486 msg_Dbg( p_input, "detected MPEG video over RTP" );
490 msg_Dbg( p_input, "no RTP header detected" );
491 p_input->pf_read = Read;
492 p_input->p_vlc->pf_memcpy( p_buffer, p_tmp_buffer, i_ret );
496 /* Return the packet without the RTP header. */
497 p_input->pf_read = RTPRead;
498 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
500 if ( (size_t)i_ret > i_len )
502 /* This should NOT happen. */
503 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
507 p_input->p_vlc->pf_memcpy( p_buffer,
508 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,