1 /*******************************************************************************
2 * main.c: main vlc source
4 *******************************************************************************
5 * Includes the main() function for vlc. Parses command line, start interface
7 *******************************************************************************/
9 /*******************************************************************************
11 *******************************************************************************/
19 #include <sys/soundcard.h> /* audio_output.h */
24 #include "vlc_thread.h"
25 #include "input_vlan.h"
27 #include "interface.h"
28 #include "audio_output.h"
31 /*******************************************************************************
32 * Command line options constants. If something is changed here, be sure that
33 * GetConfiguration and Usage are also changed.
34 *******************************************************************************/
36 /* Long options return values - note that values corresponding to short options
37 * chars, and in general any regular char, should be avoided */
38 #define OPT_NOAUDIO 150
39 #define OPT_STEREO 151
43 #define OPT_NOVIDEO 160
46 #define OPT_XNOSHM 163
47 #define OPT_XNODGA 164
49 #define OPT_NOVLANS 170
50 #define OPT_VLAN_SERVER 171
53 static const struct option longopts[] =
55 /* name, has_arg, flag, val */
57 /* General/common options */
58 { "help", 0, 0, 'h' },
61 { "noaudio", 0, 0, OPT_NOAUDIO },
62 { "stereo", 0, 0, OPT_STEREO },
63 { "mono", 0, 0, OPT_MONO },
66 { "novideo", 0, 0, OPT_NOVIDEO },
68 /* VLAN management options */
69 { "novlans", 0, 0, OPT_NOVLANS },
75 static const char *psz_shortopts = "h";
77 /*******************************************************************************
78 * Global variable program_data - this is the one and only, see main.h
79 *******************************************************************************/
82 /*******************************************************************************
84 *******************************************************************************/
85 static void SetDefaultConfiguration ( void );
86 static int GetConfiguration ( int i_argc, char *ppsz_argv[], char *ppsz_env[] );
87 static void Usage ( void );
89 static void InitSignalHandler ( void );
90 static void SignalHandler ( int i_signal );
92 /*******************************************************************************
93 * main: parse command line, start interface and spawn threads
94 *******************************************************************************
95 * Steps during program execution are:
96 * -configuration parsing and messages interface initialization
97 * -openning of audio output device and some global modules
98 * -execution of interface, which exit on error or on user request
99 * -closing of audio output device and some global modules
100 * On error, the spawned threads are cancelled, and the openned devices closed.
101 *******************************************************************************/
102 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
104 main_t main_data; /* root of all data - see main.h */
105 p_main = &main_data; /* set up the global variable */
108 * Read configuration, initialize messages interface and set up program
110 p_main->p_msg = intf_MsgCreate();
111 if( !p_main->p_msg ) /* start messages interface */
113 fprintf(stderr, "critical error: can't initialize messages interface (%s)\n",
117 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) )/* parse command line */
122 intf_MsgImm( COPYRIGHT_MESSAGE ); /* print welcome message */
125 * Initialize shared resources and libraries
127 if( main_data.b_vlans && input_VlanCreate() )
129 /* On error during vlans initialization, switch of vlans */
130 intf_Msg("Virtual LANs initialization failed : vlans management is desactivated\n");
131 main_data.b_vlans = 0;
135 * Open audio device and start aout thread
137 if( main_data.b_audio )
139 main_data.p_aout = aout_CreateThread( NULL );
140 if( main_data.p_aout == NULL )
142 /* On error during audio initialization, switch of audio */
143 intf_Msg("Audio initialization failed : audio is desactivated\n");
144 main_data.b_audio = 0;
151 main_data.p_intf = intf_Create();
152 if( main_data.p_intf != NULL )
154 InitSignalHandler(); /* prepare signals for interception */
155 intf_Run( main_data.p_intf );
156 intf_Destroy( main_data.p_intf );
162 if( main_data.b_audio )
164 aout_DestroyThread( main_data.p_aout, NULL );
168 * Free shared resources and libraries
170 if( main_data.b_vlans )
176 * Terminate messages interface and program
178 intf_Msg( "Program terminated.\n" );
183 /*******************************************************************************
184 * main_GetIntVariable: get the int value of an environment variable
185 *******************************************************************************
186 * This function is used to read some default parameters in modules.
187 *******************************************************************************/
188 int main_GetIntVariable( char *psz_name, int i_default )
192 psz_env = getenv( psz_name );
195 psz_env = strchr( psz_env, '=' );
198 return( atoi( psz_env + 1) );
205 /*******************************************************************************
206 * main_GetPszVariable: get the string value of an environment variable
207 *******************************************************************************
208 * This function is used to read some default parameters in modules.
209 *******************************************************************************/
210 char * main_GetPszVariable( char *psz_name, char *psz_default )
214 psz_env = getenv( psz_name );
217 psz_env = strchr( psz_env, '=' );
220 return( psz_env + 1 );
224 return( psz_default );
227 /* following functions are local */
229 /*******************************************************************************
230 * SetDefaultConfiguration: set default options
231 *******************************************************************************
232 * This function is called by GetConfiguration before command line is parsed.
233 * It sets all the default values required later by the program. At this stage,
234 * most structure are not yet allocated, so initialization must be done using
236 *******************************************************************************/
237 static void SetDefaultConfiguration( void )
240 * All features are activated by default
247 * Audio output thread configuration
250 // ?? initialization using structures is no more available, use putenv/getenv
256 * Video output thread configuration
258 // p_data->vout_cfg.i_properties = 0;
260 /* VLAN management */
261 /*??? p_data->cfg.b_vlans = 0;
262 p_data->cfg.psz_input_vlan_server = VLAN_DEFAULT_SERVER;
263 p_data->cfg.i_input_vlan_server_port = VLAN_DEFAULT_SERVER_PORT;
267 /*******************************************************************************
268 * GetConfiguration: parse command line
269 *******************************************************************************
270 * Parse command line and configuration file for configuration. If the inline
271 * help is requested, the function Usage() is called and the function returns
272 * -1 (causing main() to exit). The messages interface is initialized at this
273 * stage, but most structures are not allocated, so only environment should
275 *******************************************************************************/
276 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
280 /* Set default configuration and copy arguments */
281 p_main->i_argc = i_argc;
282 p_main->ppsz_argv = ppsz_argv;
283 p_main->ppsz_env = ppsz_env;
284 SetDefaultConfiguration();
286 /* Parse command line options */
288 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
292 /* General/common options */
293 case 'h': /* -h, --help */
299 case OPT_NOAUDIO: /* --noaudio */
302 case OPT_STEREO: /* --stereo */
303 // ?? should be replaced by a putenv
304 //p_main->p_aout->dsp.b_stereo = 1;
306 case OPT_MONO: /* --mono */
307 // ?? should be replaced by a putenv
308 //p_main->p_aout->dsp.b_stereo = 0;
312 case OPT_NOVIDEO: /* --novideo */
316 /* VLAN management options */
317 case OPT_NOVLANS: /* --novlans */
321 /* Internal error: unknown option */
324 intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
330 /* Parse command line parameters - no check is made for these options */
331 for( i_opt = optind; i_opt < i_argc; i_opt++ )
333 putenv( ppsz_argv[ i_opt ] );
338 /*******************************************************************************
339 * Usage: print program usage
340 *******************************************************************************
341 * Print a short inline help. Message interface is initialized at this stage.
342 *******************************************************************************/
343 static void Usage( void )
345 intf_Msg(COPYRIGHT_MESSAGE);
347 intf_Msg("usage: vlc [options...] [parameters]\n" \
348 " parameters can be passed using environment variables\n" \
349 " example: vlan_server=vlan-server.via.ecp.fr:1234\n" \
353 intf_Msg("Options:" \
354 " -h, --help print usage\n" \
355 " --noaudio disable audio\n" \
356 " --stereo enable stereo\n" \
357 " --mono disable stereo\n"
358 " --novideo disable video\n" \
359 " --novlans disable vlans\n" \
362 /* Interface parameters */
363 intf_Msg("Interface parameters:\n" \
364 " " INTF_INIT_SCRIPT_VAR "=<filename> initialization script\n" \
367 /* Audio parameters */
368 intf_Msg("Audio parameters:\n" \
369 " " AOUT_DSP_VAR "=<filename> dsp device path\n" \
370 " " AOUT_STEREO_VAR "={1|0} stereo or mono output\n" \
371 " " AOUT_RATE_VAR "=<rate> output rate\n" \
374 /* Video parameters */
375 intf_Msg("Video parameters:\n" \
378 /* Vlan parameters */
379 intf_Msg("VLANs (Virtual Local Aera Networks) parameters:\n" \
380 " vlan_server=<host>[:<port>] VLANs server address and port\n" \
384 /*******************************************************************************
385 * InitSignalHandler: system signal handler initialization
386 *******************************************************************************
387 * Set the signal handlers. SIGTERM is not intercepted, because we need at
388 * at least a method to kill the program when all other methods failed, and
389 * when we don't want to use SIGKILL.
390 *******************************************************************************/
391 static void InitSignalHandler( void )
393 /* Termination signals */
394 signal( SIGHUP, SignalHandler );
395 signal( SIGINT, SignalHandler );
396 signal( SIGQUIT, SignalHandler );
399 /*******************************************************************************
400 * SignalHandler: system signal handler
401 *******************************************************************************
402 * This function is called when a signal is received by the program. It tries to
403 * end the program in a clean way.
404 *******************************************************************************/
405 static void SignalHandler( int i_signal )
407 /* Once a signal has been trapped, the termination sequence will be armed and
408 * following signals will be ignored to avoid sending messages to an interface
409 * having been destroyed */
410 signal( SIGHUP, SIG_IGN );
411 signal( SIGINT, SIG_IGN );
412 signal( SIGQUIT, SIG_IGN );
414 /* Acknowledge the signal received */
415 intf_ErrMsgImm("intf: signal %d received\n", i_signal );
417 /* Try to terminate everything - this is done by requesting the end of the
418 * interface thread */
419 p_main->p_intf->b_die = 1;