]> git.sesse.net Git - vlc/blob - plugins/access/udp.c
* UDP access plug-in can now receive MPTS (Multiple Program TS) and switch
[vlc] / plugins / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP access plug-in
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: udp.c,v 1.6 2002/03/27 22:15:40 massiot 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 <videolan/vlc.h>
35
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
39 #   include <io.h>
40 #endif
41
42 #include "stream_control.h"
43 #include "input_ext-intf.h"
44 #include "input_ext-dec.h"
45 #include "input_ext-plugins.h"
46
47 #include "network.h"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static void input_getfunctions( function_list_t * );
53 static int  UDPOpen       ( struct input_thread_s * );
54 static int  UDPSetProgram ( struct input_thread_s * , pgrm_descriptor_t * );  
55
56 /*****************************************************************************
57  * Build configuration tree.
58  *****************************************************************************/
59 MODULE_CONFIG_START
60 MODULE_CONFIG_STOP
61  
62 MODULE_INIT_START
63     SET_DESCRIPTION( "Raw UDP access plug-in" )
64     ADD_CAPABILITY( ACCESS, 0 )
65     ADD_SHORTCUT( "udp" )
66     ADD_SHORTCUT( "udpstream" )
67     ADD_SHORTCUT( "udp4" )
68     ADD_SHORTCUT( "udp6" )
69 MODULE_INIT_STOP
70  
71 MODULE_ACTIVATE_START
72     input_getfunctions( &p_module->p_functions->access );
73 MODULE_ACTIVATE_STOP
74  
75 MODULE_DEACTIVATE_START
76 MODULE_DEACTIVATE_STOP
77
78 /*****************************************************************************
79  * Functions exported as capabilities. They are declared as static so that
80  * we don't pollute the namespace too much.
81  *****************************************************************************/
82 static void input_getfunctions( function_list_t * p_function_list )
83 {
84 #define input p_function_list->functions.access
85     input.pf_open             = UDPOpen;
86     input.pf_read             = input_FDNetworkRead;
87     input.pf_close            = input_FDClose;
88     input.pf_set_program      = UDPSetProgram;
89     input.pf_set_area         = NULL;
90     input.pf_seek             = NULL;
91 #undef input
92 }
93
94 /*****************************************************************************
95  * UDPOpen: open the socket
96  *****************************************************************************/
97 static int UDPOpen( input_thread_t * p_input )
98 {
99     input_socket_t *    p_access_data;
100     struct module_s *   p_network;
101     char *              psz_network = "";
102     char *              psz_name = strdup(p_input->psz_name);
103     char *              psz_parser = psz_name;
104     char *              psz_server_addr = "";
105     char *              psz_server_port = "";
106     char *              psz_bind_addr = "";
107     char *              psz_bind_port = "";
108     int                 i_bind_port = 0, i_server_port = 0;
109     network_socket_t    socket_desc;
110
111     if( config_GetIntVariable( "ipv4" ) )
112     {
113         psz_network = "ipv4";
114     }
115     if( config_GetIntVariable( "ipv6" ) )
116     {
117         psz_network = "ipv6";
118     }
119
120     if( *p_input->psz_access )
121     {
122         /* Find out which shortcut was used */
123         if( !strncmp( p_input->psz_access, "udp6", 5 ) )
124         {
125             psz_network = "ipv6";
126         }
127         else if( !strncmp( p_input->psz_access, "udp4", 5 ) )
128         {
129             psz_network = "ipv4";
130         }
131     }
132
133     /* Parse psz_name syntax :
134      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
135
136     if( *psz_parser && *psz_parser != '@' )
137     {
138         /* Found server */
139         psz_server_addr = psz_parser;
140
141         while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
142         {
143             if( *psz_parser == '[' )
144             {
145                 /* IPv6 address */
146                 while( *psz_parser && *psz_parser != ']' )
147                 {
148                     psz_parser++;
149                 }
150             }
151             psz_parser++;
152         }
153
154         if( *psz_parser == ':' )
155         {
156             /* Found server port */
157             *psz_parser = '\0'; /* Terminate server name */
158             psz_parser++;
159             psz_server_port = psz_parser;
160
161             while( *psz_parser && *psz_parser != '@' )
162             {
163                 psz_parser++;
164             }
165         }
166     }
167
168     if( *psz_parser == '@' )
169     {
170         /* Found bind address or bind port */
171         *psz_parser = '\0'; /* Terminate server port or name if necessary */
172         psz_parser++;
173
174         if( *psz_parser && *psz_parser != ':' )
175         {
176             /* Found bind address */
177             psz_bind_addr = psz_parser;
178
179             while( *psz_parser && *psz_parser != ':' )
180             {
181                 if( *psz_parser == '[' )
182                 {
183                     /* IPv6 address */
184                     while( *psz_parser && *psz_parser != ']' )
185                     {
186                         psz_parser++;
187                     }
188                 }
189                 psz_parser++;
190             }
191         }
192
193         if( *psz_parser == ':' )
194         {
195             /* Found bind port */
196             *psz_parser = '\0'; /* Terminate bind address if necessary */
197             psz_parser++;
198
199             psz_bind_port = psz_parser;
200         }
201     }
202
203     /* Convert ports format */
204     if( *psz_server_port )
205     {
206         i_server_port = strtol( psz_server_port, &psz_parser, 10 );
207         if( *psz_parser )
208         {
209             intf_ErrMsg( "input error: cannot parse server port near %s",
210                          psz_parser );
211             free(psz_name);
212             return( -1 );
213         }
214     }
215
216     if( *psz_bind_port )
217     {
218         i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
219         if( *psz_parser )
220         {
221             intf_ErrMsg( "input error: cannot parse bind port near %s",
222                          psz_parser );
223             free(psz_name);
224             return( -1 );
225         }
226     }
227
228     vlc_mutex_lock( &p_input->stream.stream_lock );
229     p_input->stream.b_pace_control = 0;
230     p_input->stream.b_seekable = 0;
231     p_input->stream.p_selected_area->i_tell = 0;
232     p_input->stream.i_method = INPUT_METHOD_NETWORK;
233     vlc_mutex_unlock( &p_input->stream.stream_lock );
234  
235     intf_WarnMsg( 2, "input: opening server=%s:%d local=%s:%d",
236                   psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
237
238     /* Prepare the network_socket_t structure */
239     socket_desc.i_type = NETWORK_UDP;
240     socket_desc.psz_bind_addr = psz_bind_addr;
241     socket_desc.i_bind_port = i_bind_port;
242     socket_desc.psz_server_addr = psz_server_addr;
243     socket_desc.i_server_port = i_server_port;
244
245     /* Find an appropriate network module */
246     p_network = module_Need( MODULE_CAPABILITY_NETWORK, psz_network,
247                              &socket_desc );
248     free(psz_name);
249     if( p_network == NULL )
250     {
251         return( -1 );
252     }
253     module_Unneed( p_network );
254     
255     p_access_data = p_input->p_access_data = malloc( sizeof(input_socket_t) );
256     if( p_access_data == NULL )
257     {
258         intf_ErrMsg( "input error: Out of memory" );
259         return( -1 );
260     }
261
262     p_access_data->i_handle = socket_desc.i_handle;
263     p_input->i_mtu = socket_desc.i_mtu;
264
265     return( 0 );
266 }
267
268 /*****************************************************************************
269  * UDPSetProgram: Selects another program
270  *****************************************************************************/
271 int UDPSetProgram( input_thread_t    * p_input,
272                    pgrm_descriptor_t * p_new_prg )
273 {
274     int                 i_es_index;
275
276     if ( p_input->stream.p_selected_program )
277     {
278         for ( i_es_index = 1 ; /* 0 should be the PMT */
279                 i_es_index < p_input->stream.p_selected_program->
280                     i_es_number ;
281                 i_es_index ++ )
282         {
283 #define p_es p_input->stream.p_selected_program->pp_es[i_es_index]
284             if ( p_es->p_decoder_fifo )
285             {
286                 input_UnselectES( p_input , p_es );
287                 p_es->p_pes = NULL; /* FIXME */
288             }
289 #undef p_es
290         }
291     }
292
293     for (i_es_index = 1 ; i_es_index < p_new_prg->i_es_number ; i_es_index ++ )
294     {
295 #define p_es p_new_prg->pp_es[i_es_index]
296         switch( p_es->i_cat )
297         {
298             case MPEG1_VIDEO_ES:
299             case MPEG2_VIDEO_ES:
300                 if ( p_main->b_video )
301                 {
302                     input_SelectES( p_input , p_es );
303                 }
304                 break;
305             case MPEG1_AUDIO_ES:
306             case MPEG2_AUDIO_ES:
307                 if ( p_main->b_audio )
308                 {
309                     input_SelectES( p_input , p_es );
310                 }
311                 break;
312             default:
313                 input_SelectES( p_input , p_es );
314                 break;
315 #undef p_es
316         }
317     }
318
319     p_input->stream.p_selected_program = p_new_prg;
320
321     return( 0 );
322 }