]> git.sesse.net Git - vlc/blob - modules/access/rtp.c
* ALL: the build mechanism now uses automake. See HACKING for more details.
[vlc] / modules / access / rtp.c
1 /*****************************************************************************
2  * rtp.c: RTP access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: rtp.c,v 1.2 2002/09/30 11:05:34 sam Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/input.h>
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
40 #   include <io.h>
41 #endif
42
43 #include "network.h"
44
45 #define RTP_HEADER_LEN 12
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  Open       ( vlc_object_t * );
50 static int  RTPNetworkRead( input_thread_t *, byte_t *, size_t );
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 vlc_module_begin();
55     set_description( _("RTP access module") );
56     set_capability( "access", 0 );
57     add_shortcut( "rtp" );
58     add_shortcut( "rtpstream" );
59     add_shortcut( "rtp4" );
60     add_shortcut( "rtp6" );
61     set_callbacks( Open, __input_FDNetworkClose );
62 vlc_module_end();
63
64 /*****************************************************************************
65  * Open: open the socket
66  *****************************************************************************/
67 static int Open( vlc_object_t *p_this )
68 {
69     input_thread_t *    p_input = (input_thread_t *)p_this;
70     input_socket_t *    p_access_data;
71     module_t *          p_network;
72     char *              psz_network = "";
73     char *              psz_name = strdup(p_input->psz_name);
74     char *              psz_parser = psz_name;
75     char *              psz_server_addr = "";
76     char *              psz_server_port = "";
77     char *              psz_bind_addr = "";
78     char *              psz_bind_port = "";
79     int                 i_bind_port = 0, i_server_port = 0;
80     network_socket_t    socket_desc;
81
82     if( config_GetInt( p_input, "ipv4" ) )
83     {
84         psz_network = "ipv4";
85     }
86     if( config_GetInt( p_input, "ipv6" ) )
87     {
88         psz_network = "ipv6";
89     }
90
91     if( *p_input->psz_access )
92     {
93         /* Find out which shortcut was used */
94         if( !strncmp( p_input->psz_access, "rtp6", 5 ) )
95         {
96             psz_network = "ipv6";
97         }
98         else if( !strncmp( p_input->psz_access, "rtp4", 5 ) )
99         {
100             psz_network = "ipv4";
101         }
102     }
103
104     /* Parse psz_name syntax :
105      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
106
107     if( *psz_parser && *psz_parser != '@' )
108     {
109         /* Found server */
110         psz_server_addr = psz_parser;
111
112         while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
113         {
114             if( *psz_parser == '[' )
115             {
116                 /* IPv6 address */
117                 while( *psz_parser && *psz_parser != ']' )
118                 {
119                     psz_parser++;
120                 }
121             }
122             psz_parser++;
123         }
124
125         if( *psz_parser == ':' )
126         {
127             /* Found server port */
128             *psz_parser = '\0'; /* Terminate server name */
129             psz_parser++;
130             psz_server_port = psz_parser;
131
132             while( *psz_parser && *psz_parser != '@' )
133             {
134                 psz_parser++;
135             }
136         }
137     }
138
139     if( *psz_parser == '@' )
140     {
141         /* Found bind address or bind port */
142         *psz_parser = '\0'; /* Terminate server port or name if necessary */
143         psz_parser++;
144
145         if( *psz_parser && *psz_parser != ':' )
146         {
147             /* Found bind address */
148             psz_bind_addr = psz_parser;
149
150             while( *psz_parser && *psz_parser != ':' )
151             {
152                 if( *psz_parser == '[' )
153                 {
154                     /* IPv6 address */
155                     while( *psz_parser && *psz_parser != ']' )
156                     {
157                         psz_parser++;
158                     }
159                 }
160                 psz_parser++;
161             }
162         }
163
164         if( *psz_parser == ':' )
165         {
166             /* Found bind port */
167             *psz_parser = '\0'; /* Terminate bind address if necessary */
168             psz_parser++;
169
170             psz_bind_port = psz_parser;
171         }
172     }
173
174     /* Convert ports format */
175     if( *psz_server_port )
176     {
177         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
178         if( *psz_parser )
179         {
180             msg_Err( p_input, "cannot parse server port near %s", psz_parser );
181             free(psz_name);
182             return( -1 );
183         }
184     }
185
186     if( *psz_bind_port )
187     {
188         i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
189         if( *psz_parser )
190         {
191             msg_Err( p_input, "cannot parse bind port near %s", psz_parser );
192             free(psz_name);
193             return( -1 );
194         }
195     }
196
197     p_input->pf_read = RTPNetworkRead;
198     p_input->pf_set_program = input_SetProgram;
199     p_input->pf_set_area = NULL;
200     p_input->pf_seek = NULL;
201
202     vlc_mutex_lock( &p_input->stream.stream_lock );
203     p_input->stream.b_pace_control = 0;
204     p_input->stream.b_seekable = 0;
205     p_input->stream.p_selected_area->i_tell = 0;
206     p_input->stream.i_method = INPUT_METHOD_NETWORK;
207     vlc_mutex_unlock( &p_input->stream.stream_lock );
208
209     if( *psz_server_addr || i_server_port )
210     {
211         msg_Err( p_input, "this RTP syntax is deprecated; the server argument will be");
212         msg_Err( p_input, "ignored (%s:%d). If you wanted to enter a multicast address",
213                           psz_server_addr, i_server_port);
214         msg_Err( p_input, "or local port, type : %s:@%s:%d",
215                           *p_input->psz_access ? p_input->psz_access : "udp",
216                           psz_server_addr, i_server_port );
217
218         i_server_port = 0;
219         psz_server_addr = "";
220     }
221  
222     msg_Dbg( p_input, "opening server=%s:%d local=%s:%d",
223              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
224
225     /* Prepare the network_socket_t structure */
226     socket_desc.i_type = NETWORK_UDP;
227     socket_desc.psz_bind_addr = psz_bind_addr;
228     socket_desc.i_bind_port = i_bind_port;
229     socket_desc.psz_server_addr = psz_server_addr;
230     socket_desc.i_server_port = i_server_port;
231
232     /* Find an appropriate network module */
233     p_input->p_private = (void*) &socket_desc;
234     p_network = module_Need( p_input, "network", psz_network );
235     free(psz_name);
236     if( p_network == NULL )
237     {
238         return( -1 );
239     }
240     module_Unneed( p_input, p_network );
241     
242     p_access_data = malloc( sizeof(input_socket_t) );
243     p_input->p_access_data = (access_sys_t *)p_access_data;
244
245     if( p_access_data == NULL )
246     {
247         msg_Err( p_input, "out of memory" );
248         return( -1 );
249     }
250
251     p_access_data->i_handle = socket_desc.i_handle;
252     p_input->i_mtu = socket_desc.i_mtu;
253
254     p_input->psz_demux = "ts";
255     
256     return( 0 );
257 }
258
259 /*****************************************************************************
260  * RTPNetworkRead : Read for the network, and parses the RTP header
261  *****************************************************************************/
262 static int RTPNetworkRead( input_thread_t * p_input, byte_t * p_buffer,
263                            size_t i_len )
264 {
265     int         i_rtp_version;
266     int         i_CSRC_count;
267     int         i_payload_type;
268     int         i;
269     
270     byte_t      p_tmp_buffer[1500];
271     
272     // Get the Raw data from the socket
273     // We first assume that RTP header size is the classic RTP_HEADER_LEN
274     ssize_t i_ret = input_FDNetworkRead(p_input, p_tmp_buffer,
275                                         i_len + RTP_HEADER_LEN);
276     
277     if (!i_ret) return 0;
278              
279     // Parse the header and make some verifications
280     // See RFC 1889 & RFC 2250
281   
282     i_rtp_version  = ( p_tmp_buffer[0] & 0xC0 ) >> 6;
283     i_CSRC_count   = ( p_tmp_buffer[0] & 0x0F );
284     i_payload_type = ( p_tmp_buffer[1] & 0x7F ); 
285   
286     if ( i_rtp_version != 2 ) 
287         msg_Dbg( p_input, "RTP version is %u, should be 2", i_rtp_version );
288   
289     if ( i_payload_type != 33 )
290         msg_Dbg( p_input, "RTP payload type is %u, only 33 (Mpeg2-TS) \
291 is supported", i_payload_type );
292  
293     // If both bytes are wrong, maybe a synchro error occurred...
294     if (( i_rtp_version != 2 ) && ( i_payload_type != 0x33 ))
295     {
296          msg_Dbg( p_input, "Too many RTP errors, trying to re-synchronize" );
297  
298         //Trying to re-synchronize
299         for ( i=0 ; (i<i_len) ||
300                     ((( p_tmp_buffer[0] & 0xC0 ) >> 6 == 2 )
301                      && ( p_tmp_buffer[1] & 0x7F ) >> 6 == 0x33 ) ; i++);
302
303         if (i!=i_len)
304         {
305            input_FDNetworkRead(p_input, p_tmp_buffer,i);
306            return 0;
307         }
308                 
309     }
310
311 /*    
312     // if i_CSRC_count != 0, the header is in fact longer than RTP_HEADER_LEN
313     // so we have to read some extra bytes 
314     // This case is supposed to be very rare (vls does not handle that),
315     //  so in practical this second input_FDNetworkRead is never done...
316     if (i_CSRC_count)
317     {    i_ret += input_FDNetworkRead(p_input,
318                                       p_tmp_buffer + i_len + RTP_HEADER_LEN,
319                                       4 * i_CSRC_count ); 
320     }
321 */
322     
323     // Return the packet without the RTP header
324     i_ret -= ( RTP_HEADER_LEN + 4 * i_CSRC_count );
325
326     p_input->p_vlc->pf_memcpy( p_buffer, 
327                        p_tmp_buffer + RTP_HEADER_LEN + 4 * i_CSRC_count,
328                        i_ret );
329     
330     return (i_ret);
331 }