1 /*****************************************************************************
2 * rtp.c: RTP access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: rtp.c,v 1.6 2002/11/13 20:23:21 fenrir Exp $
7 * Authors: Tristan Leteurtre <tooney@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <sys/types.h>
35 #include <vlc/input.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
44 # include <winsock2.h>
45 # include <ws2tcpip.h>
47 # define IN_MULTICAST(a) IN_CLASSD(a)
50 # include <sys/socket.h>
59 #define RTP_HEADER_LEN 12
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close ( vlc_object_t * );
66 static ssize_t RTPNetworkRead( input_thread_t *, byte_t *, size_t );
67 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
69 /*****************************************************************************
71 *****************************************************************************/
73 set_description( _("RTP access module") );
74 set_capability( "access", 0 );
75 add_shortcut( "rtp" );
76 add_shortcut( "rtpstream" );
77 add_shortcut( "rtp4" );
78 add_shortcut( "rtp6" );
79 set_callbacks( Open, Close );
82 /*****************************************************************************
83 * Open: open the socket
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 input_thread_t * p_input = (input_thread_t *)p_this;
88 input_socket_t * p_access_data;
90 char * psz_network = "";
91 char * psz_name = strdup(p_input->psz_name);
92 char * psz_parser = psz_name;
93 char * psz_server_addr = "";
94 char * psz_server_port = "";
95 char * psz_bind_addr = "";
96 char * psz_bind_port = "";
97 int i_bind_port = 0, i_server_port = 0;
98 network_socket_t socket_desc;
100 if( config_GetInt( p_input, "ipv4" ) )
102 psz_network = "ipv4";
104 if( config_GetInt( p_input, "ipv6" ) )
106 psz_network = "ipv6";
109 if( *p_input->psz_access )
111 /* Find out which shortcut was used */
112 if( !strncmp( p_input->psz_access, "rtp6", 5 ) )
114 psz_network = "ipv6";
116 else if( !strncmp( p_input->psz_access, "rtp4", 5 ) )
118 psz_network = "ipv4";
122 /* Parse psz_name syntax :
123 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
125 if( *psz_parser && *psz_parser != '@' )
128 psz_server_addr = psz_parser;
130 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
132 if( *psz_parser == '[' )
135 while( *psz_parser && *psz_parser != ']' )
143 if( *psz_parser == ':' )
145 /* Found server port */
146 *psz_parser = '\0'; /* Terminate server name */
148 psz_server_port = psz_parser;
150 while( *psz_parser && *psz_parser != '@' )
157 if( *psz_parser == '@' )
159 /* Found bind address or bind port */
160 *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 */
188 psz_bind_port = psz_parser;
192 /* Convert ports format */
193 if( *psz_server_port )
195 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
198 msg_Err( p_input, "cannot parse server port near %s", psz_parser );
206 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
209 msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
215 p_input->pf_read = RTPNetworkRead;
216 p_input->pf_set_program = input_SetProgram;
217 p_input->pf_set_area = NULL;
218 p_input->pf_seek = NULL;
220 vlc_mutex_lock( &p_input->stream.stream_lock );
221 p_input->stream.b_pace_control = 0;
222 p_input->stream.b_seekable = 0;
223 p_input->stream.p_selected_area->i_tell = 0;
224 p_input->stream.i_method = INPUT_METHOD_NETWORK;
225 vlc_mutex_unlock( &p_input->stream.stream_lock );
227 if( *psz_server_addr || i_server_port )
229 msg_Err( p_input, "this RTP syntax is deprecated; the server argument will be");
230 msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
231 psz_server_addr, i_server_port);
232 msg_Err( p_input, "or local port, type : %s:@%s:%d",
233 *p_input->psz_access ? p_input->psz_access : "udp",
234 psz_server_addr, i_server_port );
237 psz_server_addr = "";
240 msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
241 psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
243 /* Prepare the network_socket_t structure */
244 socket_desc.i_type = NETWORK_UDP;
245 socket_desc.psz_bind_addr = psz_bind_addr;
246 socket_desc.i_bind_port = i_bind_port;
247 socket_desc.psz_server_addr = psz_server_addr;
248 socket_desc.i_server_port = i_server_port;
250 /* Find an appropriate network module */
251 p_input->p_private = (void*) &socket_desc;
252 p_network = module_Need( p_input, "network", psz_network );
254 if( p_network == NULL )
258 module_Unneed( p_input, p_network );
260 p_access_data = malloc( sizeof(input_socket_t) );
261 p_input->p_access_data = (access_sys_t *)p_access_data;
263 if( p_access_data == NULL )
265 msg_Err( p_input, "out of memory" );
269 p_access_data->i_handle = socket_desc.i_handle;
270 p_input->i_mtu = socket_desc.i_mtu;
271 if( !p_input->psz_demux || !*p_input->psz_demux )
273 p_input->psz_demux = "ts";
279 /*****************************************************************************
280 * Close: free unused data structures
281 *****************************************************************************/
282 static void Close( vlc_object_t *p_this )
284 input_thread_t * p_input = (input_thread_t *)p_this;
285 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
287 msg_Info( p_input, "closing RTP target `%s'", p_input->psz_source );
290 CloseHandle( (HANDLE)p_access_data->i_handle );
291 #elif defined( WIN32 )
292 closesocket( p_access_data->i_handle );
294 close( p_access_data->i_handle );
297 free( p_access_data );
300 /*****************************************************************************
301 * RTPNetworkRead : Read for the network, and parses the RTP header
302 *****************************************************************************/
303 static ssize_t RTPNetworkRead( input_thread_t * p_input, byte_t * p_buffer,
310 byte_t * p_tmp_buffer = alloca( p_input->i_mtu );
312 /* Get the raw data from the socket.
313 * We first assume that RTP header size is the classic RTP_HEADER_LEN. */
314 ssize_t i_ret = Read( p_input, p_tmp_buffer, p_input->i_mtu );
316 if (!i_ret) return 0;
318 /* Parse the header and make some verifications.
319 * See RFC 1889 & RFC 2250. */
321 i_rtp_version = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
322 i_CSRC_count = ( p_tmp_buffer[0] & 0x0F );
323 i_payload_type = ( p_tmp_buffer[1] & 0x7F );
325 if ( i_rtp_version != 2 )
326 msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
328 if ( i_payload_type != 33 )
329 msg_Dbg( p_input, "RTP payload type is %u, only 33 (Mpeg2-TS) " \
330 "is supported", i_payload_type );
332 /* Return the packet without the RTP header. */
333 i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
337 /* This should NOT happen. */
338 msg_Warn( p_input, "RTP input trashing %d bytes", i_ret - i_len );
342 p_input->p_vlc->pf_memcpy( p_buffer,
343 p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
349 /*****************************************************************************
350 * Read: read on a file descriptor, checking b_die periodically
351 *****************************************************************************/
352 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
358 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
359 struct timeval timeout;
363 /* Initialize file descriptor set */
365 FD_SET( p_access_data->i_handle, &fds );
367 /* We'll wait 0.5 second if nothing happens */
369 timeout.tv_usec = 500000;
371 /* Find if some data is available */
372 i_ret = select( p_access_data->i_handle + 1, &fds,
373 NULL, NULL, &timeout );
375 if( i_ret == -1 && errno != EINTR )
377 msg_Err( p_input, "network select error (%s)", strerror(errno) );
381 ssize_t i_recv = recv( p_access_data->i_handle, p_buffer, i_len, 0 );
385 vlc_mutex_lock( &p_input->stream.stream_lock );
386 p_input->stream.p_selected_area->i_tell += i_recv;
387 vlc_mutex_unlock( &p_input->stream.stream_lock );
392 msg_Err( p_input, "recv failed (%s)", strerror(errno) );