1 /*****************************************************************************
2 * main.c: main vlc source
3 * Includes the main() function for vlc. Parses command line, start interface
5 *****************************************************************************
6 * Copyright (C) 1998-2001 VideoLAN
7 * $Id: main.c,v 1.179 2002/04/17 11:43:31 sam Exp $
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
11 * Gildas Bazin <gbazin@netcourrier.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
28 /*****************************************************************************
30 *****************************************************************************/
31 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
32 #include <stdio.h> /* sprintf() */
33 #include <setjmp.h> /* longjmp, setjmp */
35 #include <videolan/vlc.h>
37 #ifdef HAVE_GETOPT_LONG
39 # include <getopt.h> /* getopt() */
42 # include "GNUgetopt/getopt.h"
46 # include <mach/mach.h> /* Altivec detection */
47 # include <mach/mach_error.h> /* some day the header files||compiler *
48 will define it for us */
49 # include <mach/bootstrap.h>
53 # include <netinet/in.h> /* BSD: struct in_addr */
58 #elif defined( _MSC_VER ) && defined( _WIN32 )
66 #include <errno.h> /* ENOMEM */
67 #include <stdlib.h> /* getenv(), strtol(), */
68 #include <string.h> /* strerror() */
70 #include "netutils.h" /* network_ChannelJoin */
72 #include "stream_control.h"
73 #include "input_ext-intf.h"
75 #include "intf_playlist.h"
76 #include "interface.h"
78 #include "audio_output.h"
81 #include "video_output.h"
85 /*****************************************************************************
86 * Configuration options for the main program. Each plugin will also separatly
87 * define its own configuration options.
88 * Look into configuration.h if you need to know more about the following
91 *****************************************************************************/
93 #define MODULE_NAME main
94 #include "modules_inner.h" /* for configuration stuff */
97 #define INTF_TEXT "interface method"
98 #define INTF_LONGTEXT "This option allows you to select the interface used by"\
99 " vlc.\nNote that the default behaviour is to" \
100 " automatically select the best method available"
102 #define WARNING_TEXT "warning level (or use -v, -vv, etc...)"
103 #define WARNING_LONGTEXT "Increasing the warning level will allow you to see" \
104 " more debug messages and can sometimes help you to" \
105 " troubleshoot a problem"
107 #define STATS_TEXT "output statistics"
108 #define STATS_LONGTEXT "Enabling the stats mode will flood your log console" \
109 " with various statistics messages"
111 #define INTF_PATH_TEXT "interface default search path"
112 #define INTF_PATH_LONGTEXT "This option allows you to set the default path" \
113 " that the interface will open when looking for a" \
116 #define AOUT_TEXT "audio output method"
117 #define AOUT_LONGTEXT "This option allows you to select the audio" \
118 " audio output method used by vlc.\nNote that the" \
119 " default behaviour is to automatically select the best"\
122 #define NOAUDIO_TEXT "disable audio"
123 #define NOAUDIO_LONGTEXT "This will completely disable the audio output. The" \
124 " audio decoding stage shouldn't even be done, so it"\
125 " can allow you to save some processing power"
127 #define MONO_TEXT "mono audio"
128 #define MONO_LONGTEXT "This will force a mono audio output"
130 #define VOLUME_TEXT "audio output volume"
131 #define VOLUME_LONGTEXT "You can set the default audio output volume here," \
132 " in a range from 0 to 1024"
134 #define FORMAT_TEXT "audio output format"
135 #define FORMAT_LONGTEXT "You can force the audio output format here.\n" \
136 "0 -> 16 bits signed native endian (default)\n" \
137 "1 -> 8 bits unsigned\n" \
138 "2 -> 16 bits signed little endian\n" \
139 "3 -> 16 bits signed big endian\n" \
140 "4 -> 8 bits signed\n" \
141 "5 -> 16 bits unsigned little endian\n" \
142 "6 -> 16 bits unsigned big endian\n" \
143 "7 -> mpeg2 audio (unsupported)\n" \
144 "8 -> ac3 pass-through"
146 #define RATE_TEXT "audio output frequency (Hz)"
147 #define RATE_LONGTEXT "You can force the audio output frequency here.\n" \
148 "Common values are 48000, 44100, 32000, 22050," \
149 " 16000, 11025, 8000"
151 #define DESYNC_TEXT "Compensate desynchronization of audio (in ms)"
152 #define DESYNC_LONGTEXT "This option allows you to delay the audio output." \
153 " This can be handy if you notice a lag between the" \
154 " video and the audio"
156 #define VOUT_TEXT "video output method"
157 #define VOUT_LONGTEXT "This option allows you to select the video output" \
158 " method used by vlc.\nNote that the default behaviour" \
159 " is to automatically select the best method available"
161 #define NOVIDEO_TEXT "disable video"
162 #define NOVIDEO_LONGTEXT "This will completely disable the video output. The" \
163 " video decoding stage shouldn't even be done, so" \
164 " it can allow you to save some processing power"
166 #define DISPLAY_TEXT "display identifier"
167 #define DISPLAY_LONGTEXT NULL
169 #define WIDTH_TEXT "video width"
170 #define WIDTH_LONGTEXT "You can enforce the video width here.\nNote" \
171 " that by default vlc will adapt to the video" \
174 #define HEIGHT_TEXT "video height"
175 #define HEIGHT_LONGTEXT "You can enforce the video height here.\nNote" \
176 " that by default vlc will adapt to the video " \
179 #define GRAYSCALE_TEXT "grayscale video output"
180 #define GRAYSCALE_LONGTEXT "Using this option, vlc will not decode the color "\
181 "information from the video (this can also allow " \
182 "you to save some processing power)"
184 #define FULLSCREEN_TEXT "fullscreen video output"
185 #define FULLSCREEN_LONGTEXT "If this option is enabled, vlc will always " \
186 "start a video in fullscreen mode"
188 #define NOOVERLAY_TEXT "disable hardware acceleration for the video output"
189 #define NOOVERLAY_LONGTEXT "By default vlc will try to take advantage of the "\
190 "overlay capabilities of you graphics card.\n"
192 #define SPUMARGIN_TEXT "force SPU position"
193 #define SPUMARGIN_LONGTEXT NULL
195 #define FILTER_TEXT "video filter module"
196 #define FILTER_LONGTEXT NULL
198 #define SERVER_PORT_TEXT "server port"
199 #define SERVER_PORT_LONGTEXT NULL
201 #define NETCHANNEL_TEXT "enable network channel mode"
202 #define NETCHANNEL_LONGTEXT NULL
204 #define CHAN_SERV_TEXT "channel server address"
205 #define CHAN_SERV_LONGTEXT NULL
207 #define CHAN_PORT_TEXT "channel server port"
208 #define CHAN_PORT_LONGTEXT NULL
210 #define IFACE_TEXT "network interface"
211 #define IFACE_LONGTEXT NULL
213 #define INPUT_PROGRAM_TEXT "choose program (SID)"
214 #define INPUT_PROGRAM_LONGTEXT "choose the program to select by giving its"\
217 #define INPUT_AUDIO_TEXT "choose audio"
218 #define INPUT_AUDIO_LONGTEXT NULL
220 #define INPUT_CHAN_TEXT "choose channel"
221 #define INPUT_CHAN_LONGTEXT NULL
223 #define INPUT_SUBT_TEXT "choose subtitles"
224 #define INPUT_SUBT_LONGTEXT NULL
226 #define DVD_DEV_TEXT "DVD device"
227 #define DVD_DEV_LONGTEXT NULL
229 #define VCD_DEV_TEXT "VCD device"
230 #define VCD_DEV_LONGTEXT NULL
232 #define SAT_FREQ_TEXT "Satellite transponder frequency"
233 #define SAT_FREQ_LONGTEXT NULL
235 #define SAT_POL_TEXT "Satellite transponder polarization"
236 #define SAT_POL_LONGTEXT NULL
238 #define SAT_FEC_TEXT "Satellite transponder FEC"
239 #define SAT_FEC_LONGTEXT NULL
241 #define SAT_SRATE_TEXT "Satellite transponder symbol rate"
242 #define SAT_SRATE_LONGTEXT NULL
244 #define SAT_DISEQC_TEXT "Use diseqc with antenna"
245 #define SAT_DISEQC_LONGTEXT NULL
247 #define SAT_LNB_LOF1_TEXT "Antenna lnb_lof1 (kHz)"
248 #define SAT_LNB_LOF1_LONGTEXT NULL
250 #define SAT_LNB_LOF2_TEXT "Antenna lnb_lof2 (kHz)"
251 #define SAT_LNB_LOF2_LONGTEXT NULL
253 #define SAT_LNB_SLOF_TEXT "Antenna lnb_slof (kHz)"
254 #define SAT_LNB_SLOF_LONGTEXT NULL
256 #define IPV6_TEXT "force IPv6"
257 #define IPV6_LONGTEXT NULL
259 #define IPV4_TEXT "force IPv4"
260 #define IPV4_LONGTEXT NULL
262 #define ADEC_MPEG_TEXT "choose MPEG audio decoder"
263 #define ADEC_MPEG_LONGTEXT NULL
265 #define ADEC_AC3_TEXT "choose AC3 audio decoder"
266 #define ADEC_AC3_LONGTEXT NULL
268 #define VDEC_SMP_TEXT "use additional processors"
269 #define VDEC_SMP_LONGTEXT NULL
271 #define VPAR_SYNCHRO_TEXT "force synchro algorithm {I|I+|IP|IP+|IPB}"
272 #define VPAR_SYNCHRO_LONGTEXT NULL
274 #define NOMMX_TEXT "disable CPU's MMX support"
275 #define NOMMX_LONGTEXT NULL
277 #define NO3DN_TEXT "disable CPU's 3D Now! support"
278 #define NO3DN_LONGTEXT NULL
280 #define NOMMXEXT_TEXT "disable CPU's MMX EXT support"
281 #define NOMMXEXT_LONGTEXT NULL
283 #define NOSSE_TEXT "disable CPU's SSE support"
284 #define NOSSE_LONGTEXT NULL
286 #define NOALTIVEC_TEXT "disable CPU's AltiVec support"
287 #define NOALTIVEC_LONGTEXT NULL
289 #define PLAYLIST_LAUNCH_TEXT "launch playlist on startup"
290 #define PLAYLIST_LAUNCH_LONGTEXT NULL
292 #define PLAYLIST_ENQUEUE_TEXT "enqueue playlist as default"
293 #define PLAYLIST_ENQUEUE_LONGTEXT NULL
295 #define PLAYLIST_LOOP_TEXT "loop playlist on end"
296 #define PLAYLIST_LOOP_LONGTEXT NULL
298 #define MEMCPY_TEXT "memory copy method"
299 #define MEMCPY_LONGTEXT NULL
301 #define FAST_PTHREAD_TEXT "fast pthread on NT/2K/XP (developpers only)"
302 #define FAST_PTHREAD_LONGTEXT "On Windows NT/2K/XP we use a slow but correct "\
303 "pthread implementation, you can also use this "\
304 "faster implementation but you might "\
305 "experience problems with it"
310 ADD_CATEGORY_HINT( text, longtext )
311 ADD_SUBCATEGORY_HINT( text, longtext )
312 ADD_STRING( option_name, value, p_callback, text, longtext )
313 ADD_FILE( option_name, psz_value, p_callback, text, longtext )
314 ADD_PLUGIN( option_name, psz_value, i_capability, p_callback, text, longtext )
315 ADD_INTEGER( option_name, i_value, p_callback, text, longtext )
316 ADD_BOOL( option_name, p_callback, text, longtext )
321 /* Interface options */
322 ADD_CATEGORY_HINT( "Interface", NULL)
323 ADD_PLUGIN ( "intf", MODULE_CAPABILITY_INTF, NULL, NULL, INTF_TEXT, INTF_LONGTEXT )
324 ADD_INTEGER ( "warning", 0, NULL, WARNING_TEXT, WARNING_LONGTEXT )
325 ADD_BOOL ( "stats", NULL, STATS_TEXT, STATS_LONGTEXT )
326 ADD_STRING ( "search_path", NULL, NULL, INTF_PATH_TEXT, INTF_PATH_LONGTEXT )
329 ADD_CATEGORY_HINT( "Audio", NULL)
330 ADD_PLUGIN ( "aout", MODULE_CAPABILITY_AOUT, NULL, NULL, AOUT_TEXT, AOUT_LONGTEXT )
331 ADD_BOOL ( "noaudio", NULL, NOAUDIO_TEXT, NOAUDIO_LONGTEXT )
332 ADD_BOOL ( "mono", NULL, MONO_TEXT, MONO_LONGTEXT )
333 ADD_INTEGER ( "volume", VOLUME_DEFAULT, NULL, VOLUME_TEXT, VOLUME_LONGTEXT )
334 ADD_INTEGER ( "rate", 44100, NULL, RATE_TEXT, RATE_LONGTEXT )
335 ADD_INTEGER ( "desync", 0, NULL, DESYNC_TEXT, DESYNC_LONGTEXT )
336 ADD_INTEGER ( "aout_format", 0, NULL, FORMAT_TEXT,
340 ADD_CATEGORY_HINT( "Video", NULL )
341 ADD_PLUGIN ( "vout", MODULE_CAPABILITY_VOUT, NULL, NULL, VOUT_TEXT, VOUT_LONGTEXT )
342 ADD_BOOL ( "novideo", NULL, NOVIDEO_TEXT, NOVIDEO_LONGTEXT )
343 ADD_INTEGER ( "width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT )
344 ADD_INTEGER ( "height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT )
345 ADD_BOOL ( "grayscale", NULL, GRAYSCALE_TEXT, GRAYSCALE_LONGTEXT )
346 ADD_BOOL ( "fullscreen", NULL, FULLSCREEN_TEXT, FULLSCREEN_LONGTEXT )
347 ADD_BOOL ( "nooverlay", NULL, NOOVERLAY_TEXT, NOOVERLAY_LONGTEXT )
348 ADD_INTEGER ( "spumargin", -1, NULL, SPUMARGIN_TEXT, SPUMARGIN_LONGTEXT )
349 ADD_PLUGIN ( "filter", MODULE_CAPABILITY_VOUT, NULL, NULL, FILTER_TEXT, FILTER_LONGTEXT )
352 ADD_CATEGORY_HINT( "Input", NULL )
353 ADD_INTEGER ( "server_port", 1234, NULL, SERVER_PORT_TEXT, SERVER_PORT_LONGTEXT )
354 ADD_BOOL ( "network_channel", NULL, NETCHANNEL_TEXT, NETCHANNEL_LONGTEXT )
355 ADD_STRING ( "channel_server", "localhost", NULL, CHAN_SERV_TEXT, CHAN_SERV_LONGTEXT )
356 ADD_INTEGER ( "channel_port", 6010, NULL, CHAN_PORT_TEXT, CHAN_PORT_LONGTEXT )
357 ADD_STRING ( "iface", "eth0", NULL, IFACE_TEXT, IFACE_LONGTEXT )
359 ADD_INTEGER ( "input_program", 0, NULL, INPUT_PROGRAM_TEXT,
360 INPUT_PROGRAM_LONGTEXT )
361 ADD_INTEGER ( "input_audio", -1, NULL, INPUT_AUDIO_TEXT, INPUT_AUDIO_LONGTEXT )
362 ADD_INTEGER ( "input_channel", -1, NULL, INPUT_CHAN_TEXT, INPUT_CHAN_LONGTEXT )
363 ADD_INTEGER ( "input_subtitle", -1, NULL, INPUT_SUBT_TEXT, INPUT_SUBT_LONGTEXT )
365 ADD_STRING ( "dvd_device", "/dev/dvd", NULL, DVD_DEV_TEXT, DVD_DEV_LONGTEXT )
366 ADD_STRING ( "vcd_device", "/dev/cdrom", NULL, VCD_DEV_TEXT, VCD_DEV_LONGTEXT )
367 #ifdef HAVE_SATELLITE
368 ADD_INTEGER ( "sat_frequency", 11954, NULL, SAT_FREQ_TEXT, SAT_FREQ_LONGTEXT )
369 ADD_INTEGER ( "sat_polarization", 0, NULL, SAT_POL_TEXT, SAT_POL_LONGTEXT )
370 ADD_INTEGER ( "sat_fec", 3, NULL, SAT_FEC_TEXT, SAT_FEC_LONGTEXT )
371 ADD_INTEGER ( "sat_symbol_rate", 27500, NULL, SAT_SRATE_TEXT,
373 ADD_BOOL ( "sat_diseqc", 0, SAT_DISEQC_TEXT, SAT_DISEQC_LONGTEXT )
374 ADD_INTEGER ( "sat_lnb_lof1", 10000, NULL, SAT_LNB_LOF1_TEXT,
375 SAT_LNB_LOF1_LONGTEXT )
376 ADD_INTEGER ( "sat_lnb_lof2", 10000, NULL, SAT_LNB_LOF2_TEXT,
377 SAT_LNB_LOF2_LONGTEXT )
378 ADD_INTEGER ( "sat_lnb_slof", 11700, NULL, SAT_LNB_SLOF_TEXT,
379 SAT_LNB_SLOF_LONGTEXT )
382 ADD_BOOL ( "ipv6", NULL, IPV6_TEXT, IPV6_LONGTEXT )
383 ADD_BOOL ( "ipv4", NULL, IPV4_TEXT, IPV4_LONGTEXT )
385 /* Decoder options */
386 ADD_CATEGORY_HINT( "Decoders", NULL )
387 ADD_PLUGIN ( "mpeg_adec", MODULE_CAPABILITY_DECODER, NULL, NULL, ADEC_MPEG_TEXT, ADEC_MPEG_LONGTEXT )
388 ADD_PLUGIN ( "ac3_adec", MODULE_CAPABILITY_DECODER, NULL, NULL, ADEC_AC3_TEXT, ADEC_AC3_LONGTEXT )
389 ADD_INTEGER ( "vdec_smp", 0, NULL, VDEC_SMP_TEXT, VDEC_SMP_LONGTEXT )
390 ADD_STRING ( "vpar_synchro", NULL, NULL, VPAR_SYNCHRO_TEXT, VPAR_SYNCHRO_LONGTEXT )
393 ADD_CATEGORY_HINT( "CPU", NULL )
394 ADD_BOOL ( "nommx", NULL, NOMMX_TEXT, NOMMX_LONGTEXT )
395 ADD_BOOL ( "no3dn", NULL, NO3DN_TEXT, NO3DN_LONGTEXT )
396 ADD_BOOL ( "nommxext", NULL, NOMMXEXT_TEXT, NOMMXEXT_LONGTEXT )
397 ADD_BOOL ( "nosse", NULL, NOSSE_TEXT, NOSSE_LONGTEXT )
398 ADD_BOOL ( "noaltivec", NULL, NOALTIVEC_TEXT, NOALTIVEC_LONGTEXT )
400 /* Playlist options */
401 ADD_CATEGORY_HINT( "Playlist", NULL )
402 ADD_BOOL ( "playlist_launch", NULL, PLAYLIST_LAUNCH_TEXT, PLAYLIST_LAUNCH_LONGTEXT )
403 ADD_BOOL ( "playlist_enqueue", NULL, PLAYLIST_ENQUEUE_TEXT, PLAYLIST_ENQUEUE_LONGTEXT )
404 ADD_BOOL ( "playlist_loop", NULL, PLAYLIST_LOOP_TEXT, PLAYLIST_LOOP_LONGTEXT )
407 ADD_CATEGORY_HINT( "Miscellaneous", NULL )
408 ADD_PLUGIN ( "memcpy", MODULE_CAPABILITY_MEMCPY, NULL, NULL, MEMCPY_TEXT, MEMCPY_LONGTEXT )
411 ADD_BOOL ( "fast_pthread", NULL, FAST_PTHREAD_TEXT, FAST_PTHREAD_LONGTEXT )
417 SET_DESCRIPTION( "Main program" )
418 ADD_CAPABILITY( MAIN, 100/*whatever*/ )
421 MODULE_ACTIVATE_START
424 MODULE_DEACTIVATE_START
425 MODULE_DEACTIVATE_STOP
427 /* Hack for help options */
428 static module_t help_module;
429 static module_config_t p_help_config[] = {
430 { MODULE_CONFIG_ITEM_BOOL, "help", "print help (or use -h)",
431 NULL, NULL, 0, NULL, NULL, 0 },
432 { MODULE_CONFIG_ITEM_BOOL, "longhelp", "print detailed help (or use -H)",
433 NULL, NULL, 0, NULL, NULL, 0 },
434 { MODULE_CONFIG_ITEM_BOOL, "list", "print a list of available plugins "
435 "(or use -l)", NULL, NULL, 0, NULL, NULL, 0 },
436 { MODULE_CONFIG_ITEM_STRING, "plugin", "print help on plugin (or use -p)",
437 NULL, NULL, 0, NULL, &help_module.config_lock, 0 },
438 { MODULE_CONFIG_ITEM_BOOL, "version", "print version information",
439 NULL, NULL, 0, NULL, NULL, 0 },
440 { MODULE_CONFIG_HINT_END, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 } };
443 /*****************************************************************************
445 *****************************************************************************/
447 /*****************************************************************************
448 * Global variables - these are the only ones, see main.h and modules.h
449 *****************************************************************************/
451 p_main_sys_t p_main_sys;
452 module_bank_t *p_module_bank;
453 input_bank_t *p_input_bank;
454 aout_bank_t *p_aout_bank;
455 vout_bank_t *p_vout_bank;
457 /*****************************************************************************
459 *****************************************************************************/
460 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
461 static void Usage ( const char *psz_module_name );
462 static void ListModules ( void );
463 static void Version ( void );
465 static void InitSignalHandler ( void );
466 static void SimpleSignalHandler ( int i_signal );
467 static void FatalSignalHandler ( int i_signal );
468 static void IllegalSignalHandler ( int i_signal );
469 static u32 CPUCapabilities ( void );
472 static void ShowConsole ( void );
476 static int i_illegal;
477 static char *psz_capability;
479 /*****************************************************************************
480 * main: parse command line, start interface and spawn threads
481 *****************************************************************************
482 * Steps during program execution are:
483 * -configuration parsing and messages interface initialization
484 * -opening of audio output device and some global modules
485 * -execution of interface, which exit on error or on user request
486 * -closing of audio output device and some global modules
487 * On error, the spawned threads are canceled, and the open devices closed.
488 *****************************************************************************/
489 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
491 main_t main_data; /* root of all data - see main.h */
492 module_bank_t module_bank;
493 input_bank_t input_bank;
494 aout_bank_t aout_bank;
495 vout_bank_t vout_bank;
499 p_main = &main_data; /* set up the global variables */
500 p_module_bank = &module_bank;
501 p_input_bank = &input_bank;
502 p_aout_bank = &aout_bank;
503 p_vout_bank = &vout_bank;
505 p_main->i_warning_level = 0;
507 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
511 #if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
512 if( !setlocale( LC_MESSAGES, "" ) )
514 fprintf( stderr, "warning: unsupported locale.\n" );
518 if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
520 fprintf( stderr, "warning: no domain %s in directory %s\n",
521 PACKAGE, LOCALEDIR );
524 textdomain( PACKAGE );
528 * Initialize threads system
533 * Test if our code is likely to run on this CPU
535 p_main->i_cpu_capabilities = CPUCapabilities();
538 * System specific initialization code
540 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
541 system_Init( &i_argc, ppsz_argv, ppsz_env );
543 #elif defined( SYS_LINUX )
545 /* Activate malloc checking routines to detect heap corruptions. */
546 putenv( "MALLOC_CHECK_=2" );
547 putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
552 * Initialize messages interface
556 intf_Msg( COPYRIGHT_MESSAGE "\n" );
559 /* Get the executable name (similar to the basename command) */
560 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
563 if( *p_tmp == '/' ) p_main->psz_arg0 = ++p_tmp;
568 * Initialize the module bank and and load the configuration of the main
569 * module. We need to do this at this stage to be able to display a short
570 * help if required by the user. (short help == main module options)
575 /* Hack: insert the help module here */
576 help_module.psz_name = "help";
577 help_module.i_config_lines = sizeof(p_help_config) /
578 sizeof(module_config_t);
579 help_module.i_config_items = help_module.i_config_lines - 1;
580 vlc_mutex_init( &help_module.config_lock );
581 help_module.p_config = p_help_config;
582 help_module.next = p_module_bank->first;
583 p_module_bank->first = &help_module;
586 if( config_LoadCmdLine( &i_argc, ppsz_argv, 1 ) )
592 /* Check for short help option */
593 if( config_GetIntVariable( "help" ) )
595 intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
603 /* Check for version option */
604 if( config_GetIntVariable( "version" ) )
610 /* Hack: remove the help module here */
611 p_module_bank->first = help_module.next;
615 * Load the builtins and plugins into the module_bank.
616 * We have to do it before config_Load*() because this also gets the
617 * list of configuration options exported by each plugin and loads their
620 module_LoadBuiltins();
621 module_LoadPlugins();
622 intf_WarnMsg( 2, "module: module bank initialized, found %i modules",
623 p_module_bank->i_count );
625 /* Hack: insert the help module here */
626 help_module.next = p_module_bank->first;
627 p_module_bank->first = &help_module;
630 /* Check for help on plugins */
631 if( (p_tmp = config_GetPszVariable( "plugin" )) )
638 /* Check for long help option */
639 if( config_GetIntVariable( "longhelp" ) )
645 /* Check for plugin list option */
646 if( config_GetIntVariable( "list" ) )
652 /* Hack: remove the help module here */
653 p_module_bank->first = help_module.next;
658 * Override default configuration with config file settings
660 vlc_mutex_init( &p_main->config_lock );
661 p_main->psz_homedir = config_GetHomeDir();
662 config_LoadConfigFile( NULL );
665 * Override configuration with command line settings
667 if( config_LoadCmdLine( &i_argc, ppsz_argv, 0 ) )
671 /* Pause the console because it's destroyed when we exit */
672 intf_Msg( "The command line options couldn't be loaded, check that "
673 "they are valid.\nPress the RETURN key to continue..." );
682 * System specific configuration
688 /* p_main inititalization. FIXME ? */
689 p_main->i_desync = (mtime_t)config_GetIntVariable( "desync" )
691 p_main->b_stats = config_GetIntVariable( "stats" );
692 p_main->b_audio = !config_GetIntVariable( "noaudio" );
693 p_main->b_stereo= !config_GetIntVariable( "mono" );
694 p_main->b_video = !config_GetIntVariable( "novideo" );
695 if( config_GetIntVariable( "nommx" ) )
696 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
697 if( config_GetIntVariable( "no3dn" ) )
698 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
699 if( config_GetIntVariable( "nommxext" ) )
700 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
701 if( config_GetIntVariable( "nosse" ) )
702 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
703 if( config_GetIntVariable( "noaltivec" ) )
704 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
707 if( p_main->b_stats )
709 char p_capabilities[200];
710 p_capabilities[0] = '\0';
712 #define PRINT_CAPABILITY( capability, string ) \
713 if( p_main->i_cpu_capabilities & capability ) \
715 strncat( p_capabilities, string " ", \
716 sizeof(p_capabilities) - strlen(p_capabilities) ); \
717 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
720 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
721 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
722 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
723 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
724 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
725 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
726 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
727 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
728 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
729 intf_StatMsg( "info: CPU has capabilities : %s", p_capabilities );
733 * Initialize playlist and get commandline files
735 p_main->p_playlist = intf_PlaylistCreate();
736 if( !p_main->p_playlist )
738 intf_ErrMsg( "playlist error: playlist initialization failed" );
742 intf_PlaylistInit( p_main->p_playlist );
745 * Get input filenames given as commandline arguments
747 GetFilenames( i_argc, ppsz_argv );
750 * Initialize input, aout and vout banks
757 * Choose the best memcpy module
759 psz_plugin = config_GetPszVariable( "memcpy" );
760 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY,
762 if( psz_plugin ) free( psz_plugin );
763 if( p_main->p_memcpy_module == NULL )
765 intf_ErrMsg( "intf error: no suitable memcpy module, "
766 "using libc default" );
767 p_main->pf_memcpy = memcpy;
771 p_main->pf_memcpy = p_main->p_memcpy_module->p_functions
772 ->memcpy.functions.memcpy.pf_memcpy;
776 * Initialize shared resources and libraries
778 if( config_GetIntVariable( "network_channel" ) &&
779 network_ChannelCreate() )
781 /* On error during Channels initialization, switch off channels */
782 intf_ErrMsg( "intf error: channels initialization failed, "
783 "deactivating channels" );
784 config_PutIntVariable( "network_channel", 0 );
788 * Try to run the interface
790 p_main->p_intf = intf_Create();
791 if( p_main->p_intf == NULL )
793 intf_ErrMsg( "intf error: interface initialization failed" );
798 * Set signal handling policy for all threads
803 * This is the main loop
805 p_main->p_intf->pf_run( p_main->p_intf );
808 * Finished, destroy the interface
810 intf_Destroy( p_main->p_intf );
813 * Go back into channel 0 which is the network
815 if( config_GetIntVariable( "network_channel" ) && p_main->p_channel )
817 network_ChannelJoin( COMMON_CHANNEL );
822 * Free input, aout and vout banks
831 intf_PlaylistDestroy( p_main->p_playlist );
834 * Free memcpy module if it was allocated
836 if( p_main->p_memcpy_module != NULL )
838 module_Unneed( p_main->p_memcpy_module );
847 * System specific cleaning code
849 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
855 * Terminate messages interface and program
857 intf_WarnMsg( 1, "intf: program terminated" );
861 * Stop threads system
869 /* following functions are local */
871 /*****************************************************************************
872 * GetFilenames: parse command line options which are not flags
873 *****************************************************************************
874 * Parse command line for input files.
875 *****************************************************************************/
876 static int GetFilenames( int i_argc, char *ppsz_argv[] )
880 /* We assume that the remaining parameters are filenames */
881 for( i_opt = optind; i_opt < i_argc; i_opt++ )
883 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
884 ppsz_argv[ i_opt ] );
890 /*****************************************************************************
891 * Usage: print program usage
892 *****************************************************************************
893 * Print a short inline help. Message interface is initialized at this stage.
894 *****************************************************************************/
895 static void Usage( const char *psz_module_name )
901 memset( psz_spaces, 32, 30 );
907 /* Enumerate the config of each module */
908 for( p_module = p_module_bank->first ;
910 p_module = p_module->next )
913 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
916 /* ignore plugins without config options */
917 if( !p_module->i_config_items ) continue;
919 /* print module name */
920 intf_Msg( "%s options:\n", p_module->psz_name );
922 for( i = 0; i < p_module->i_config_lines; i++ )
926 switch( p_module->p_config[i].i_type )
928 case MODULE_CONFIG_HINT_CATEGORY:
929 intf_Msg( " %s", p_module->p_config[i].psz_text );
932 case MODULE_CONFIG_ITEM_STRING:
933 case MODULE_CONFIG_ITEM_FILE:
934 case MODULE_CONFIG_ITEM_PLUGIN:
935 /* Nasty hack, but right now I'm too tired to think about
937 j = 25 - strlen( p_module->p_config[i].psz_name )
938 - strlen(" <string>") - 1;
939 if( j < 0 ) j = 0; psz_spaces[j] = 0;
941 intf_Msg( " --%s <string>%s %s",
942 p_module->p_config[i].psz_name, psz_spaces,
943 p_module->p_config[i].psz_text );
946 case MODULE_CONFIG_ITEM_INTEGER:
947 /* Nasty hack, but right now I'm too tired to think about
949 j = 25 - strlen( p_module->p_config[i].psz_name )
950 - strlen(" <integer>") - 1;
951 if( j < 0 ) j = 0; psz_spaces[j] = 0;
953 intf_Msg( " --%s <integer>%s %s",
954 p_module->p_config[i].psz_name, psz_spaces,
955 p_module->p_config[i].psz_text );
958 case MODULE_CONFIG_ITEM_BOOL:
959 /* Nasty hack, but right now I'm too tired to think about
961 j = 25 - strlen( p_module->p_config[i].psz_name ) - 1;
962 if( j < 0 ) j = 0; psz_spaces[j] = 0;
964 intf_Msg( " --%s%s %s",
965 p_module->p_config[i].psz_name, psz_spaces,
966 p_module->p_config[i].psz_text );
972 /* Yet another nasty hack.
973 * Maybe we could use MODULE_CONFIG_ITEM_END to display tail messages
974 * for each module?? */
975 if( !strcmp( "main", p_module->psz_name ) )
976 intf_Msg( "\nPlaylist items:"
977 "\n *.mpg, *.vob \tPlain MPEG-1/2 files"
978 "\n [dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"
980 "\n [vcd:][device][@[title][,[chapter]]"
982 "\n udpstream:[@[<bind address>][:<bind port>]]"
983 "\n \tUDP stream sent by VLS"
984 "\n vlc:loop \tLoop execution of the "
986 "\n vlc:pause \tPause execution of "
988 "\n vlc:quit \tQuit VLC" );
994 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
995 intf_Msg( "\nPress the RETURN key to continue..." );
1000 /*****************************************************************************
1001 * ListModules: list the available modules with their description
1002 *****************************************************************************
1003 * Print a list of all available modules (builtins and plugins) and a short
1004 * description for each one.
1005 *****************************************************************************/
1006 static void ListModules( void )
1009 char psz_spaces[20];
1011 memset( psz_spaces, 32, 20 );
1018 intf_Msg( "Usage: %s [options] [parameters] [file]...\n",
1021 intf_Msg( "[plugin] [description]" );
1023 /* Enumerate each module */
1024 for( p_module = p_module_bank->first ;
1026 p_module = p_module->next )
1030 /* Nasty hack, but right now I'm too tired to think about a nice
1032 i = 20 - strlen( p_module->psz_name ) - 1;
1036 intf_Msg( " %s%s %s", p_module->psz_name, psz_spaces,
1037 p_module->psz_longname );
1043 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1044 intf_Msg( "\nPress the RETURN key to continue..." );
1049 /*****************************************************************************
1050 * Version: print complete program version
1051 *****************************************************************************
1052 * Print complete program version and build number.
1053 *****************************************************************************/
1054 static void Version( void )
1060 intf_Msg( VERSION_MESSAGE );
1062 "This program comes with NO WARRANTY, to the extent permitted by "
1063 "law.\nYou may redistribute it under the terms of the GNU General "
1064 "Public License;\nsee the file named COPYING for details.\n"
1065 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1067 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1068 intf_Msg( "\nPress the RETURN key to continue..." );
1073 /*****************************************************************************
1074 * InitSignalHandler: system signal handler initialization
1075 *****************************************************************************
1076 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1077 * at least a method to kill the program when all other methods failed, and
1078 * when we don't want to use SIGKILL.
1079 *****************************************************************************/
1080 static void InitSignalHandler( void )
1082 /* Termination signals */
1084 signal( SIGINT, FatalSignalHandler );
1085 signal( SIGHUP, FatalSignalHandler );
1086 signal( SIGQUIT, FatalSignalHandler );
1089 signal( SIGALRM, SimpleSignalHandler );
1090 signal( SIGPIPE, SimpleSignalHandler );
1094 /*****************************************************************************
1095 * SimpleSignalHandler: system signal handler
1096 *****************************************************************************
1097 * This function is called when a non fatal signal is received by the program.
1098 *****************************************************************************/
1099 static void SimpleSignalHandler( int i_signal )
1101 /* Acknowledge the signal received */
1102 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1105 /*****************************************************************************
1106 * FatalSignalHandler: system signal handler
1107 *****************************************************************************
1108 * This function is called when a fatal signal is received by the program.
1109 * It tries to end the program in a clean way.
1110 *****************************************************************************/
1111 static void FatalSignalHandler( int i_signal )
1113 /* Once a signal has been trapped, the termination sequence will be
1114 * armed and following signals will be ignored to avoid sending messages
1115 * to an interface having been destroyed */
1117 signal( SIGINT, SIG_IGN );
1118 signal( SIGHUP, SIG_IGN );
1119 signal( SIGQUIT, SIG_IGN );
1122 /* Acknowledge the signal received */
1123 intf_ErrMsg( "intf error: signal %d received, exiting", i_signal );
1125 /* Try to terminate everything - this is done by requesting the end of the
1126 * interface thread */
1127 p_main->p_intf->b_die = 1;
1130 /*****************************************************************************
1131 * IllegalSignalHandler: system signal handler
1132 *****************************************************************************
1133 * This function is called when an illegal instruction signal is received by
1134 * the program. We use this function to test OS and CPU capabilities
1135 *****************************************************************************/
1136 static void IllegalSignalHandler( int i_signal )
1138 /* Acknowledge the signal received */
1141 #ifdef HAVE_SIGRELSE
1142 sigrelse( i_signal );
1145 #if defined( __i386__ )
1146 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
1147 "operating system.\n", psz_capability );
1148 fprintf( stderr, " some optimizations will be disabled unless "
1149 "you upgrade your OS\n" );
1150 # if defined( SYS_LINUX )
1151 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );
1158 /*****************************************************************************
1159 * CPUCapabilities: list the processors MMX support and other capabilities
1160 *****************************************************************************
1161 * This function is called to list extensions the CPU may have.
1162 *****************************************************************************/
1163 static u32 CPUCapabilities( void )
1165 volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
1167 #if defined( SYS_DARWIN )
1168 struct host_basic_info hi;
1170 host_name_port_t host;
1173 char *psz_name, *psz_subname;
1175 i_capabilities |= CPU_CAPABILITY_FPU;
1177 /* Should 'never' fail? */
1178 host = mach_host_self();
1180 i_size = sizeof( hi ) / sizeof( int );
1181 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1183 if( ret != KERN_SUCCESS )
1185 fprintf( stderr, "error: couldn't get CPU information\n" );
1186 return( i_capabilities );
1189 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1190 /* FIXME: need better way to detect newer proccessors.
1191 * could do strncmp(a,b,5), but that's real ugly */
1192 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1194 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1197 return( i_capabilities );
1199 #elif defined( __i386__ )
1200 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1201 volatile boolean_t b_amd;
1203 /* Needed for x86 CPU capabilities detection */
1204 # define cpuid( a ) \
1205 asm volatile ( "pushl %%ebx\n\t" \
1207 "movl %%ebx,%1\n\t" \
1216 i_capabilities |= CPU_CAPABILITY_FPU;
1218 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1219 signal( SIGILL, IllegalSignalHandler );
1222 /* test for a 486 CPU */
1223 asm volatile ( "pushl %%ebx\n\t"
1226 "movl %%eax, %%ebx\n\t"
1227 "xorl $0x200000, %%eax\n\t"
1239 if( i_eax == i_ebx )
1241 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1242 signal( SIGILL, NULL );
1244 return( i_capabilities );
1247 i_capabilities |= CPU_CAPABILITY_486;
1249 /* the CPU supports the CPUID instruction - get its level */
1250 cpuid( 0x00000000 );
1254 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1255 signal( SIGILL, NULL );
1257 return( i_capabilities );
1260 /* FIXME: this isn't correct, since some 486s have cpuid */
1261 i_capabilities |= CPU_CAPABILITY_586;
1263 /* borrowed from mpeg2dec */
1264 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1265 && ( i_edx == 0x69746e65 );
1267 /* test for the MMX flag */
1268 cpuid( 0x00000001 );
1270 if( ! (i_edx & 0x00800000) )
1272 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1273 signal( SIGILL, NULL );
1275 return( i_capabilities );
1278 i_capabilities |= CPU_CAPABILITY_MMX;
1280 if( i_edx & 0x02000000 )
1282 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1284 # ifdef CAN_COMPILE_SSE
1285 /* We test if OS support the SSE instructions */
1286 psz_capability = "SSE";
1288 if( setjmp( env ) == 0 )
1290 /* Test a SSE instruction */
1291 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1294 if( i_illegal == 0 )
1296 i_capabilities |= CPU_CAPABILITY_SSE;
1301 /* test for additional capabilities */
1302 cpuid( 0x80000000 );
1304 if( i_eax < 0x80000001 )
1306 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1307 signal( SIGILL, NULL );
1309 return( i_capabilities );
1312 /* list these additional capabilities */
1313 cpuid( 0x80000001 );
1315 # ifdef CAN_COMPILE_3DNOW
1316 if( i_edx & 0x80000000 )
1318 psz_capability = "3D Now!";
1320 if( setjmp( env ) == 0 )
1322 /* Test a 3D Now! instruction */
1323 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1326 if( i_illegal == 0 )
1328 i_capabilities |= CPU_CAPABILITY_3DNOW;
1333 if( b_amd && ( i_edx & 0x00400000 ) )
1335 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1338 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1339 signal( SIGILL, NULL );
1341 return( i_capabilities );
1343 #elif defined( __powerpc__ )
1345 i_capabilities |= CPU_CAPABILITY_FPU;
1347 # ifdef CAN_COMPILE_ALTIVEC
1348 signal( SIGILL, IllegalSignalHandler );
1350 psz_capability = "AltiVec";
1352 if( setjmp( env ) == 0 )
1354 asm volatile ("mtspr 256, %0\n\t"
1355 "vand %%v0, %%v0, %%v0"
1360 if( i_illegal == 0 )
1362 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1365 signal( SIGILL, NULL );
1368 return( i_capabilities );
1370 #elif defined( __sparc__ )
1372 i_capabilities |= CPU_CAPABILITY_FPU;
1373 return( i_capabilities );
1376 /* default behaviour */
1377 return( i_capabilities );
1382 /*****************************************************************************
1383 * ShowConsole: On Win32, create an output console for debug messages
1384 *****************************************************************************
1385 * This function is usefull only on Win32.
1386 *****************************************************************************/
1388 static void ShowConsole( void )
1391 freopen( "CONOUT$", "w", stdout );
1392 freopen( "CONOUT$", "w", stderr );
1393 freopen( "CONIN$", "r", stdin );