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.191 2002/05/19 09:37:02 gbazin 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"
83 /*****************************************************************************
84 * Configuration options for the main program. Each module will also separatly
85 * define its own configuration options.
86 * Look into configuration.h if you need to know more about the following
89 *****************************************************************************/
91 #define MODULE_NAME main
92 #include "modules_inner.h" /* for configuration stuff */
95 #define INTF_TEXT N_("interface module")
96 #define INTF_LONGTEXT N_( \
97 "This option allows you to select the interface used by vlc.\nNote that " \
98 "the default behavior is to automatically select the best method " \
101 #define WARNING_TEXT N_("warning level (or use -v, -vv, etc...)")
102 #define WARNING_LONGTEXT N_( \
103 "Increasing the warning level will allow you to see more debug messages " \
104 "and can sometimes help you to troubleshoot a problem.")
106 #define STATS_TEXT N_("output statistics")
107 #define STATS_LONGTEXT N_( \
108 "Enabling the stats mode will flood your log console with various " \
109 "statistics messages.")
111 #define INTF_PATH_TEXT N_("interface default search path")
112 #define INTF_PATH_LONGTEXT N_( \
113 "This option allows you to set the default path that the interface will " \
114 "open when looking for a file.")
116 #define AOUT_TEXT N_("audio output module")
117 #define AOUT_LONGTEXT N_( \
118 "This option allows you to select the audio audio output method used by " \
119 "vlc.\nNote that the default behavior is to automatically select the " \
120 "best method available.")
122 #define NOAUDIO_TEXT N_("disable audio")
123 #define NOAUDIO_LONGTEXT N_( \
124 "This will completely disable the audio output. The audio decoding " \
125 "stage shouldn't even be done, so it can allow you to save some " \
128 #define MONO_TEXT N_("mono audio")
129 #define MONO_LONGTEXT N_("This will force a mono audio output")
131 #define VOLUME_TEXT N_("audio output volume")
132 #define VOLUME_LONGTEXT N_( \
133 "You can set the default audio output volume here, in a range from 0 to " \
136 #define FORMAT_TEXT N_("audio output format")
137 #define FORMAT_LONGTEXT N_( \
138 "You can force the audio output format here.\n" \
139 "0 -> 16 bits signed native endian (default)\n" \
140 "1 -> 8 bits unsigned\n" \
141 "2 -> 16 bits signed little endian\n" \
142 "3 -> 16 bits signed big endian\n" \
143 "4 -> 8 bits signed\n" \
144 "5 -> 16 bits unsigned little endian\n" \
145 "6 -> 16 bits unsigned big endian\n" \
146 "7 -> mpeg2 audio (unsupported)\n" \
147 "8 -> ac3 pass-through")
149 #define RATE_TEXT N_("audio output frequency (Hz)")
150 #define RATE_LONGTEXT N_( \
151 "You can force the audio output frequency here.\nCommon values are " \
152 "48000, 44100, 32000, 22050, 16000, 11025, 8000.")
154 #define DESYNC_TEXT N_("compensate desynchronization of audio (in ms)")
155 #define DESYNC_LONGTEXT N_( \
156 "This option allows you to delay the audio output. This can be handy if " \
157 "you notice a lag between the video and the audio.")
159 #define VOUT_TEXT N_("video output module")
160 #define VOUT_LONGTEXT N_( \
161 "This option allows you to select the video output method used by vlc.\n" \
162 "Note that the default behavior is to automatically select the best " \
165 #define NOVIDEO_TEXT N_("disable video")
166 #define NOVIDEO_LONGTEXT N_( \
167 "This will completely disable the video output. The video decoding " \
168 "stage shouldn't even be done, so it can allow you to save some " \
171 #define DISPLAY_TEXT N_("display identifier")
172 #define DISPLAY_LONGTEXT ""
174 #define WIDTH_TEXT N_("video width")
175 #define WIDTH_LONGTEXT N_( \
176 "You can enforce the video width here.\nNote that by default vlc will " \
177 "adapt to the video characteristics.")
179 #define HEIGHT_TEXT N_("video height")
180 #define HEIGHT_LONGTEXT N_( \
181 "You can enforce the video height here.\nNote that by default vlc will " \
182 "adapt to the video characteristics.")
184 #define ZOOM_TEXT N_("zoom video")
185 #define ZOOM_LONGTEXT N_( \
186 "You can zoom the video by the specified factor.")
188 #define GRAYSCALE_TEXT N_("grayscale video output")
189 #define GRAYSCALE_LONGTEXT N_( \
190 "Using this option, vlc will not decode the color information from the " \
191 "video (this can also allow you to save some processing power).")
193 #define FULLSCREEN_TEXT N_("fullscreen video output")
194 #define FULLSCREEN_LONGTEXT N_( \
195 "If this option is enabled, vlc will always start a video in fullscreen " \
198 #define NOOVERLAY_TEXT N_("disable hardware acceleration for the video output")
199 #define NOOVERLAY_LONGTEXT N_( \
200 "By default vlc will try to take advantage of the overlay capabilities " \
201 "of you graphics card.")
203 #define SPUMARGIN_TEXT N_("force SPU position")
204 #define SPUMARGIN_LONGTEXT ""
206 #define FILTER_TEXT N_("video filter module")
207 #define FILTER_LONGTEXT N_( \
208 "This option allows you to select the video filter module that vlc " \
209 "will use.\nNote that by default no video filter is used.")
211 #define SERVER_PORT_TEXT N_("server port")
212 #define SERVER_PORT_LONGTEXT ""
214 #define NETCHANNEL_TEXT N_("enable network channel mode")
215 #define NETCHANNEL_LONGTEXT ""
217 #define CHAN_SERV_TEXT N_("channel server address")
218 #define CHAN_SERV_LONGTEXT ""
220 #define CHAN_PORT_TEXT N_("channel server port")
221 #define CHAN_PORT_LONGTEXT ""
223 #define IFACE_TEXT N_("network interface")
224 #define IFACE_LONGTEXT ""
226 #define INPUT_PROGRAM_TEXT N_("choose program (SID)")
227 #define INPUT_PROGRAM_LONGTEXT N_( \
228 "Choose the program to select by giving its Service ID.")
230 #define INPUT_AUDIO_TEXT N_("choose audio")
231 #define INPUT_AUDIO_LONGTEXT ""
233 #define INPUT_CHAN_TEXT N_("choose channel")
234 #define INPUT_CHAN_LONGTEXT ""
236 #define INPUT_SUBT_TEXT N_("choose subtitles")
237 #define INPUT_SUBT_LONGTEXT ""
239 #define DVD_DEV_TEXT N_("DVD device")
240 #define DVD_DEV_LONGTEXT N_( \
241 "This option allows you to set the DVD device that vlc will try to use " \
244 #define VCD_DEV_TEXT N_("VCD device")
245 #define VCD_DEV_LONGTEXT N_( \
246 "This option allows you to set the VCD device that vlc will try to use " \
249 #define IPV6_TEXT N_("force IPv6")
250 #define IPV6_LONGTEXT ""
252 #define IPV4_TEXT N_("force IPv4")
253 #define IPV4_LONGTEXT ""
255 #define ADEC_MPEG_TEXT N_("choose MPEG audio decoder")
256 #define ADEC_MPEG_LONGTEXT N_( \
257 "This option allows you to select the MPEG audio decoder you want to use."\
258 "\nBy default vlc will use mpeg_adec.")
260 #define ADEC_AC3_TEXT N_("choose AC3 audio decoder")
261 #define ADEC_AC3_LONGTEXT N_( \
262 "This option allows you to select the AC3 audio decoder you want to use." \
263 "\nBy default vlc will use ac3_adec.")
265 #define NOMMX_TEXT N_("disable CPU's MMX support")
266 #define NOMMX_LONGTEXT N_( \
267 "If your processor supports the MMX instructions set but you don't want " \
268 "vlc to use them, you can use this option.")
270 #define NO3DN_TEXT N_("disable CPU's 3D Now! support")
271 #define NO3DN_LONGTEXT N_( \
272 "If your processor supports the 3D Now! instructions set but you don't " \
273 "want vlc to use them, you can use this option.")
275 #define NOMMXEXT_TEXT N_("disable CPU's MMX EXT support")
276 #define NOMMXEXT_LONGTEXT N_( \
277 "If your processor supports the MMX EXT instructions set but you don't " \
278 "want vlc to use them, you can use this option.")
280 #define NOSSE_TEXT N_("disable CPU's SSE support")
281 #define NOSSE_LONGTEXT N_( \
282 "If your processor supports the SSE instructions set but you don't want " \
283 "vlc to use them, you can use this option.")
285 #define NOALTIVEC_TEXT N_("disable CPU's AltiVec support")
286 #define NOALTIVEC_LONGTEXT N_( \
287 "If your processor supports the AltiVec instructions set but you don't " \
288 "want vlc to use them, you can use this option.")
290 #define PLAYLIST_LAUNCH_TEXT N_("launch playlist on startup")
291 #define PLAYLIST_LAUNCH_LONGTEXT N_( \
292 "If you want vlc to start playing on startup, then enable this option.")
294 #define PLAYLIST_ENQUEUE_TEXT N_("enqueue playlist as default")
295 #define PLAYLIST_ENQUEUE_LONGTEXT N_( \
296 "If you want vlc to add items to the playlist as you open them, then " \
297 "enable this option.")
299 #define PLAYLIST_LOOP_TEXT N_("loop playlist on end")
300 #define PLAYLIST_LOOP_LONGTEXT N_( \
301 "If you want vlc to keep playing the playlist indefinitely then enable " \
304 #define MEMCPY_TEXT N_("memory copy module")
305 #define MEMCPY_LONGTEXT N_( \
306 "You can select wich memory copy module you want to use. By default vlc " \
307 "will select the fastest one supported by your hardware.")
309 #define FAST_PTHREAD_TEXT N_("fast pthread on NT/2K/XP (developpers only)")
310 #define FAST_PTHREAD_LONGTEXT N_( \
311 "On Windows NT/2K/XP we use a slow but correct pthread implementation, " \
312 "you can also use this faster implementation but you might experience " \
316 * Quick usage guide for the configuration options:
318 * MODULE_CONFIG_START
320 * ADD_CATEGORY_HINT( N_(text), longtext )
321 * ADD_SUBCATEGORY_HINT( N_(text), longtext )
322 * ADD_STRING( option_name, value, p_callback, N_(text), N_(longtext) )
323 * ADD_FILE( option_name, psz_value, p_callback, N_(text), N_(longtext) )
324 * ADD_MODULE( option_name, psz_value, i_capability, p_callback,
325 * N_(text), N_(longtext) )
326 * ADD_INTEGER( option_name, i_value, p_callback, N_(text), N_(longtext) )
327 * ADD_BOOL( option_name, p_callback, N_(text), N_(longtext) )
332 /* Interface options */
333 ADD_CATEGORY_HINT( N_("Interface"), NULL)
334 ADD_MODULE_WITH_SHORT ( "intf", 'I', MODULE_CAPABILITY_INTF, NULL, NULL, INTF_TEXT, INTF_LONGTEXT )
335 ADD_INTEGER ( "warning", 0, NULL, WARNING_TEXT, WARNING_LONGTEXT )
336 ADD_BOOL ( "stats", NULL, STATS_TEXT, STATS_LONGTEXT )
337 ADD_STRING ( "search-path", NULL, NULL, INTF_PATH_TEXT, INTF_PATH_LONGTEXT )
340 ADD_CATEGORY_HINT( N_("Audio"), NULL)
341 ADD_MODULE_WITH_SHORT ( "aout", 'A', MODULE_CAPABILITY_AOUT, NULL, NULL, AOUT_TEXT, AOUT_LONGTEXT )
342 ADD_BOOL ( "noaudio", NULL, NOAUDIO_TEXT, NOAUDIO_LONGTEXT )
343 ADD_BOOL ( "mono", NULL, MONO_TEXT, MONO_LONGTEXT )
344 ADD_INTEGER ( "volume", VOLUME_DEFAULT, NULL, VOLUME_TEXT, VOLUME_LONGTEXT )
345 ADD_INTEGER ( "rate", 44100, NULL, RATE_TEXT, RATE_LONGTEXT )
346 ADD_INTEGER ( "desync", 0, NULL, DESYNC_TEXT, DESYNC_LONGTEXT )
347 ADD_INTEGER ( "audio-format", 0, NULL, FORMAT_TEXT,
351 ADD_CATEGORY_HINT( N_("Video"), NULL )
352 ADD_MODULE_WITH_SHORT ( "vout", 'V', MODULE_CAPABILITY_VOUT, NULL, NULL, VOUT_TEXT, VOUT_LONGTEXT )
353 ADD_BOOL ( "novideo", NULL, NOVIDEO_TEXT, NOVIDEO_LONGTEXT )
354 ADD_INTEGER ( "width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT )
355 ADD_INTEGER ( "height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT )
356 ADD_FLOAT ( "zoom", 1, NULL, ZOOM_TEXT, ZOOM_LONGTEXT )
357 ADD_BOOL ( "grayscale", NULL, GRAYSCALE_TEXT, GRAYSCALE_LONGTEXT )
358 ADD_BOOL ( "fullscreen", NULL, FULLSCREEN_TEXT, FULLSCREEN_LONGTEXT )
359 ADD_BOOL ( "nooverlay", NULL, NOOVERLAY_TEXT, NOOVERLAY_LONGTEXT )
360 ADD_INTEGER ( "spumargin", -1, NULL, SPUMARGIN_TEXT, SPUMARGIN_LONGTEXT )
361 ADD_MODULE ( "filter", MODULE_CAPABILITY_VOUT, NULL, NULL, FILTER_TEXT, FILTER_LONGTEXT )
364 ADD_CATEGORY_HINT( N_("Input"), NULL )
365 ADD_INTEGER ( "server-port", 1234, NULL, SERVER_PORT_TEXT, SERVER_PORT_LONGTEXT )
366 ADD_BOOL ( "network-channel", NULL, NETCHANNEL_TEXT, NETCHANNEL_LONGTEXT )
367 ADD_STRING ( "channel-server", "localhost", NULL, CHAN_SERV_TEXT, CHAN_SERV_LONGTEXT )
368 ADD_INTEGER ( "channel-port", 6010, NULL, CHAN_PORT_TEXT, CHAN_PORT_LONGTEXT )
369 ADD_STRING ( "iface", "eth0", NULL, IFACE_TEXT, IFACE_LONGTEXT )
371 ADD_INTEGER ( "program", 0, NULL, INPUT_PROGRAM_TEXT, INPUT_PROGRAM_LONGTEXT )
372 ADD_INTEGER ( "audio-type", -1, NULL, INPUT_AUDIO_TEXT, INPUT_AUDIO_LONGTEXT )
373 ADD_INTEGER ( "audio-channel", -1, NULL, INPUT_CHAN_TEXT, INPUT_CHAN_LONGTEXT )
374 ADD_INTEGER ( "spu-channel", -1, NULL, INPUT_SUBT_TEXT, INPUT_SUBT_LONGTEXT )
376 ADD_STRING ( "dvd", DVD_DEVICE, NULL, DVD_DEV_TEXT, DVD_DEV_LONGTEXT )
377 ADD_STRING ( "vcd", VCD_DEVICE, NULL, VCD_DEV_TEXT, VCD_DEV_LONGTEXT )
379 ADD_BOOL_WITH_SHORT ( "ipv6", '6', NULL, IPV6_TEXT, IPV6_LONGTEXT )
380 ADD_BOOL_WITH_SHORT ( "ipv4", '4', NULL, IPV4_TEXT, IPV4_LONGTEXT )
382 /* Decoder options */
383 ADD_CATEGORY_HINT( N_("Decoders"), NULL )
384 ADD_MODULE ( "mpeg-adec", MODULE_CAPABILITY_DECODER, NULL, NULL, ADEC_MPEG_TEXT, ADEC_MPEG_LONGTEXT )
385 ADD_MODULE ( "ac3-adec", MODULE_CAPABILITY_DECODER, NULL, NULL, ADEC_AC3_TEXT, ADEC_AC3_LONGTEXT )
388 ADD_CATEGORY_HINT( N_("CPU"), NULL )
389 ADD_BOOL ( "nommx", NULL, NOMMX_TEXT, NOMMX_LONGTEXT )
390 ADD_BOOL ( "no3dn", NULL, NO3DN_TEXT, NO3DN_LONGTEXT )
391 ADD_BOOL ( "nommxext", NULL, NOMMXEXT_TEXT, NOMMXEXT_LONGTEXT )
392 ADD_BOOL ( "nosse", NULL, NOSSE_TEXT, NOSSE_LONGTEXT )
393 ADD_BOOL ( "noaltivec", NULL, NOALTIVEC_TEXT, NOALTIVEC_LONGTEXT )
395 /* Playlist options */
396 ADD_CATEGORY_HINT( N_("Playlist"), NULL )
397 ADD_BOOL ( "launch-playlist", NULL, PLAYLIST_LAUNCH_TEXT, PLAYLIST_LAUNCH_LONGTEXT )
398 ADD_BOOL ( "enqueue-playlist", NULL, PLAYLIST_ENQUEUE_TEXT, PLAYLIST_ENQUEUE_LONGTEXT )
399 ADD_BOOL ( "loop-playlist", NULL, PLAYLIST_LOOP_TEXT, PLAYLIST_LOOP_LONGTEXT )
402 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
403 ADD_MODULE ( "memcpy", MODULE_CAPABILITY_MEMCPY, NULL, NULL, MEMCPY_TEXT, MEMCPY_LONGTEXT )
404 ADD_MODULE ( "access", MODULE_CAPABILITY_ACCESS, NULL, NULL, "access module", "This is a legacy entry to let you configure access modules" )
405 ADD_MODULE ( "demux", MODULE_CAPABILITY_DEMUX, NULL, NULL, "demux module", "This is a legacy entry to let you configure demux modules" )
408 ADD_BOOL ( "fast_pthread", NULL, FAST_PTHREAD_TEXT, FAST_PTHREAD_LONGTEXT )
414 SET_DESCRIPTION( N_("main program") )
415 ADD_CAPABILITY( MAIN, 100/*whatever*/ )
418 MODULE_ACTIVATE_START
421 MODULE_DEACTIVATE_START
422 MODULE_DEACTIVATE_STOP
424 /* Hack for help options */
425 static module_t help_module;
426 static module_config_t p_help_config[] =
428 { MODULE_CONFIG_ITEM_BOOL, "help", 'h', N_("print help"),
429 NULL, NULL, 0, 0, NULL, NULL, 0 },
430 { MODULE_CONFIG_ITEM_BOOL, "longhelp", 'H', N_("print detailed help"),
431 NULL, NULL, 0, 0, NULL, NULL, 0 },
432 { MODULE_CONFIG_ITEM_BOOL, "list", 'l', N_("print a list of available "
433 "modules"), NULL, NULL, 0, 0, NULL, NULL, 0 },
434 { MODULE_CONFIG_ITEM_STRING, "module", 'p', N_("print help on module "
435 "<string>"), NULL, NULL, 0, 0, NULL, &help_module.config_lock, 0 },
436 { MODULE_CONFIG_ITEM_BOOL, "version", '\0',
437 N_("print version information"), NULL, NULL, 0, 0, NULL, NULL, 0 },
438 { MODULE_CONFIG_HINT_END, NULL, '\0', NULL, NULL, NULL, 0, 0,
442 /*****************************************************************************
444 *****************************************************************************/
446 /*****************************************************************************
447 * Global variables - these are the only ones, see main.h and modules.h
448 *****************************************************************************/
450 module_bank_t *p_module_bank;
451 input_bank_t *p_input_bank;
452 aout_bank_t *p_aout_bank;
453 vout_bank_t *p_vout_bank;
455 /*****************************************************************************
457 *****************************************************************************/
458 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
459 static void Usage ( const char *psz_module_name );
460 static void ListModules ( void );
461 static void Version ( void );
463 static void InitSignalHandler ( void );
464 static void SimpleSignalHandler ( int i_signal );
465 static void FatalSignalHandler ( int i_signal );
466 static void IllegalSignalHandler ( int i_signal );
467 static u32 CPUCapabilities ( void );
470 static void ShowConsole ( void );
474 static int i_illegal;
475 static char *psz_capability;
477 /*****************************************************************************
478 * main: parse command line, start interface and spawn threads
479 *****************************************************************************
480 * Steps during program execution are:
481 * -configuration parsing and messages interface initialization
482 * -opening of audio output device and some global modules
483 * -execution of interface, which exit on error or on user request
484 * -closing of audio output device and some global modules
485 * On error, the spawned threads are canceled, and the open devices closed.
486 *****************************************************************************/
487 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
489 main_t main_data; /* root of all data - see main.h */
490 module_bank_t module_bank;
491 input_bank_t input_bank;
492 aout_bank_t aout_bank;
493 vout_bank_t vout_bank;
496 struct module_config_s *p_item;
498 p_main = &main_data; /* set up the global variables */
499 p_module_bank = &module_bank;
500 p_input_bank = &input_bank;
501 p_aout_bank = &aout_bank;
502 p_vout_bank = &vout_bank;
504 p_main->i_warning_level = 0;
507 * Support for gettext
509 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
510 # if defined( HAVE_LOCALE_H ) && defined( HAVE_LC_MESSAGES )
511 if( !setlocale( LC_MESSAGES, "" ) )
513 fprintf( stderr, "warning: unsupported locale settings\n" );
516 setlocale( LC_CTYPE, "" );
519 if( !bindtextdomain( PACKAGE, LOCALEDIR ) )
521 fprintf( stderr, "warning: no domain %s in directory %s\n",
522 PACKAGE, LOCALEDIR );
525 textdomain( PACKAGE );
529 * Initialize threads system
534 * Test if our code is likely to run on this CPU
536 p_main->i_cpu_capabilities = CPUCapabilities();
539 * System specific initialization code
541 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
542 system_Init( &i_argc, ppsz_argv, ppsz_env );
544 #elif defined( SYS_LINUX )
546 /* Activate malloc checking routines to detect heap corruptions. */
547 putenv( "MALLOC_CHECK_=2" );
548 putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
553 * Initialize messages interface
557 intf_Msg( COPYRIGHT_MESSAGE "\n" );
560 /* Get the executable name (similar to the basename command) */
563 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
566 if( *p_tmp == '/' ) p_main->psz_arg0 = ++p_tmp;
572 p_main->psz_arg0 = "vlc";
576 * Initialize the module bank and and load the configuration of the main
577 * module. We need to do this at this stage to be able to display a short
578 * help if required by the user. (short help == main module options)
583 /* Hack: insert the help module here */
584 help_module.psz_name = "help";
585 help_module.psz_longname = _( "help module" );
586 help_module.i_config_items =
587 sizeof(p_help_config) / sizeof(module_config_t) - 1;
588 vlc_mutex_init( &help_module.config_lock );
589 help_module.p_config = config_Duplicate( p_help_config );
590 for( p_item = help_module.p_config;
591 p_item->i_type != MODULE_CONFIG_HINT_END;
594 p_item->p_lock = &help_module.config_lock;
596 help_module.next = p_module_bank->first;
597 p_module_bank->first = &help_module;
600 if( config_LoadCmdLine( &i_argc, ppsz_argv, 1 ) )
606 /* Check for short help option */
607 if( config_GetIntVariable( "help" ) )
609 intf_Msg( _("Usage: %s [options] [parameters] [file]...\n"),
617 /* Check for version option */
618 if( config_GetIntVariable( "version" ) )
624 /* Hack: remove the help module here */
625 p_module_bank->first = help_module.next;
629 * Load the builtins and plugins into the module_bank.
630 * We have to do it before config_Load*() because this also gets the
631 * list of configuration options exported by each module and loads their
634 module_LoadBuiltins();
635 module_LoadPlugins();
636 intf_WarnMsg( 2, "module: module bank initialized, found %i modules",
637 p_module_bank->i_count );
639 /* Hack: insert the help module here */
640 help_module.next = p_module_bank->first;
641 p_module_bank->first = &help_module;
644 /* Check for help on modules */
645 if( (p_tmp = config_GetPszVariable( "module" )) )
652 /* Check for long help option */
653 if( config_GetIntVariable( "longhelp" ) )
659 /* Check for module list option */
660 if( config_GetIntVariable( "list" ) )
666 /* Hack: remove the help module here */
667 p_module_bank->first = help_module.next;
672 * Override default configuration with config file settings
674 vlc_mutex_init( &p_main->config_lock );
675 p_main->psz_homedir = config_GetHomeDir();
676 config_LoadConfigFile( NULL );
679 * Override configuration with command line settings
681 if( config_LoadCmdLine( &i_argc, ppsz_argv, 0 ) )
685 /* Pause the console because it's destroyed when we exit */
686 intf_Msg( "The command line options couldn't be loaded, check that "
687 "they are valid.\nPress the RETURN key to continue..." );
696 * System specific configuration
700 /* p_main inititalization. FIXME ? */
701 p_main->i_warning_level = config_GetIntVariable( "warning" );
702 p_main->i_desync = config_GetIntVariable( "desync" ) * (mtime_t)1000;
703 p_main->b_stats = config_GetIntVariable( "stats" );
704 p_main->b_audio = !config_GetIntVariable( "noaudio" );
705 p_main->b_stereo= !config_GetIntVariable( "mono" );
706 p_main->b_video = !config_GetIntVariable( "novideo" );
707 if( config_GetIntVariable( "nommx" ) )
708 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMX;
709 if( config_GetIntVariable( "no3dn" ) )
710 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_3DNOW;
711 if( config_GetIntVariable( "nommxext" ) )
712 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_MMXEXT;
713 if( config_GetIntVariable( "nosse" ) )
714 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_SSE;
715 if( config_GetIntVariable( "noaltivec" ) )
716 p_main->i_cpu_capabilities &= ~CPU_CAPABILITY_ALTIVEC;
719 if( p_main->b_stats )
721 char p_capabilities[200];
722 p_capabilities[0] = '\0';
724 #define PRINT_CAPABILITY( capability, string ) \
725 if( p_main->i_cpu_capabilities & capability ) \
727 strncat( p_capabilities, string " ", \
728 sizeof(p_capabilities) - strlen(p_capabilities) ); \
729 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
732 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
733 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
734 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
735 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
736 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
737 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
738 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
739 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
740 PRINT_CAPABILITY( CPU_CAPABILITY_FPU, "FPU" );
741 intf_StatMsg( "info: CPU has capabilities : %s", p_capabilities );
745 * Initialize playlist and get commandline files
747 p_main->p_playlist = intf_PlaylistCreate();
748 if( !p_main->p_playlist )
750 intf_ErrMsg( "playlist error: playlist initialization failed" );
754 intf_PlaylistInit( p_main->p_playlist );
757 * Get input filenames given as commandline arguments
759 GetFilenames( i_argc, ppsz_argv );
762 * Initialize input, aout and vout banks
769 * Choose the best memcpy module
771 psz_module = config_GetPszVariable( "memcpy" );
772 p_main->p_memcpy_module = module_Need( MODULE_CAPABILITY_MEMCPY,
774 if( psz_module ) free( psz_module );
775 if( p_main->p_memcpy_module == NULL )
777 intf_ErrMsg( "intf error: no suitable memcpy module, "
778 "using libc default" );
779 p_main->pf_memcpy = memcpy;
783 p_main->pf_memcpy = p_main->p_memcpy_module->p_functions
784 ->memcpy.functions.memcpy.pf_memcpy;
788 * Initialize shared resources and libraries
790 if( config_GetIntVariable( "network-channel" ) &&
791 network_ChannelCreate() )
793 /* On error during Channels initialization, switch off channels */
794 intf_ErrMsg( "intf error: channels initialization failed, "
795 "deactivating channels" );
796 config_PutIntVariable( "network-channel", 0 );
800 * Try to run the interface
802 p_main->p_intf = intf_Create();
803 if( p_main->p_intf == NULL )
805 intf_ErrMsg( "intf error: interface initialization failed" );
810 * Set signal handling policy for all threads
815 * This is the main loop
817 p_main->p_intf->pf_run( p_main->p_intf );
820 * Finished, destroy the interface
822 intf_Destroy( p_main->p_intf );
825 * Go back into channel 0 which is the network
827 if( config_GetIntVariable( "network-channel" ) && p_main->p_channel )
829 network_ChannelJoin( COMMON_CHANNEL );
834 * Free input, aout and vout banks
843 intf_PlaylistDestroy( p_main->p_playlist );
846 * Free memcpy module if it was allocated
848 if( p_main->p_memcpy_module != NULL )
850 module_Unneed( p_main->p_memcpy_module );
859 * System specific cleaning code
864 * Terminate messages interface and program
866 intf_WarnMsg( 1, "intf: program terminated" );
870 * Stop threads system
878 /* following functions are local */
880 /*****************************************************************************
881 * GetFilenames: parse command line options which are not flags
882 *****************************************************************************
883 * Parse command line for input files.
884 *****************************************************************************/
885 static int GetFilenames( int i_argc, char *ppsz_argv[] )
889 /* We assume that the remaining parameters are filenames */
890 for( i_opt = optind; i_opt < i_argc; i_opt++ )
892 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
893 ppsz_argv[ i_opt ] );
899 /*****************************************************************************
900 * Usage: print program usage
901 *****************************************************************************
902 * Print a short inline help. Message interface is initialized at this stage.
903 *****************************************************************************/
904 static void Usage( const char *psz_module_name )
906 #define FORMAT_STRING " --%s%s%s%s%s %s"
907 /* option name ---------------' | | | | |
908 * <bra ------------------------' | | | |
909 * option type or "" -------------' | | |
910 * ket> ----------------------------' | |
911 * padding spaces --------------------' |
912 * comment ------------------------------'
914 * The purpose of having bra and ket is that we might i18n them as well.
917 #define PADDING_SPACES 25
919 module_config_t *p_item;
920 char psz_spaces[PADDING_SPACES+LINE_START+1];
921 char psz_format[sizeof(FORMAT_STRING)];
923 memset( psz_spaces, ' ', PADDING_SPACES+LINE_START );
924 psz_spaces[PADDING_SPACES+LINE_START] = '\0';
926 strcpy( psz_format, FORMAT_STRING );
932 /* Enumerate the config of each module */
933 for( p_module = p_module_bank->first ;
935 p_module = p_module->next )
937 if( psz_module_name && strcmp( psz_module_name, p_module->psz_name ) )
940 /* ignore modules without config options */
941 if( !p_module->i_config_items ) continue;
943 /* print module name */
944 intf_Msg( _("%s module options:\n"), p_module->psz_name );
946 for( p_item = p_module->p_config;
947 p_item->i_type != MODULE_CONFIG_HINT_END;
950 char *psz_bra = NULL, *psz_type = NULL, *psz_ket = NULL;
953 if( p_item->i_short )
956 psz_format[3] = p_item->i_short;
966 switch( p_item->i_type )
968 case MODULE_CONFIG_HINT_CATEGORY:
969 intf_Msg( " %s", p_item->psz_text );
972 case MODULE_CONFIG_ITEM_STRING:
973 case MODULE_CONFIG_ITEM_FILE:
974 case MODULE_CONFIG_ITEM_MODULE: /* We could also have "=<" here */
975 psz_bra = " <"; psz_type = _("string"); psz_ket = ">";
977 case MODULE_CONFIG_ITEM_INTEGER:
978 psz_bra = " <"; psz_type = _("integer"); psz_ket = ">";
980 case MODULE_CONFIG_ITEM_FLOAT:
981 psz_bra = " <"; psz_type = _("float"); psz_ket = ">";
983 case MODULE_CONFIG_ITEM_BOOL:
984 psz_bra = ""; psz_type = ""; psz_ket = "";
990 i = PADDING_SPACES - strlen( p_item->psz_name )
991 - strlen( psz_bra ) - strlen( psz_type )
992 - strlen( psz_ket ) - 1;
996 psz_spaces[i] = '\n';
1000 psz_spaces[i] = '\0';
1003 intf_Msg( psz_format, p_item->psz_name, psz_bra, psz_type,
1004 psz_ket, psz_spaces, p_item->psz_text );
1005 psz_spaces[i] = ' ';
1009 /* Yet another nasty hack.
1010 * Maybe we could use MODULE_CONFIG_ITEM_END to display tail messages
1011 * for each module?? */
1012 if( !strcmp( "main", p_module->psz_name ) )
1014 intf_Msg( _("\nPlaylist items:"
1015 "\n *.mpg, *.vob plain MPEG-1/2 files"
1016 "\n [dvd:][device][@raw_device][@[title][,[chapter][,angle]]]"
1018 "\n [vcd:][device][@[title][,[chapter]]"
1020 "\n udpstream:[@[<bind address>][:<bind port>]]"
1021 "\n UDP stream sent by VLS"
1022 "\n vlc:loop loop execution of the "
1024 "\n vlc:pause pause execution of "
1026 "\n vlc:quit quit VLC") );
1033 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1034 intf_Msg( _("\nPress the RETURN key to continue...") );
1039 /*****************************************************************************
1040 * ListModules: list the available modules with their description
1041 *****************************************************************************
1042 * Print a list of all available modules (builtins and plugins) and a short
1043 * description for each one.
1044 *****************************************************************************/
1045 static void ListModules( void )
1048 char psz_spaces[22];
1050 memset( psz_spaces, ' ', 22 );
1057 intf_Msg( _("Usage: %s [options] [parameters] [file]...\n"),
1060 intf_Msg( _("[module] [description]") );
1062 /* Enumerate each module */
1063 for( p_module = p_module_bank->first ;
1065 p_module = p_module->next )
1069 /* Nasty hack, but right now I'm too tired to think about a nice
1071 i = 22 - strlen( p_module->psz_name ) - 1;
1075 intf_Msg( " %s%s %s", p_module->psz_name, psz_spaces,
1076 p_module->psz_longname );
1078 psz_spaces[i] = ' ';
1082 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1083 intf_Msg( _("\nPress the RETURN key to continue...") );
1088 /*****************************************************************************
1089 * Version: print complete program version
1090 *****************************************************************************
1091 * Print complete program version and build number.
1092 *****************************************************************************/
1093 static void Version( void )
1099 intf_Msg( VERSION_MESSAGE );
1101 _("This program comes with NO WARRANTY, to the extent permitted by "
1102 "law.\nYou may redistribute it under the terms of the GNU General "
1103 "Public License;\nsee the file named COPYING for details.\n"
1104 "Written by the VideoLAN team at Ecole Centrale, Paris.") );
1106 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
1107 intf_Msg( _("\nPress the RETURN key to continue...") );
1112 /*****************************************************************************
1113 * InitSignalHandler: system signal handler initialization
1114 *****************************************************************************
1115 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1116 * at least a method to kill the program when all other methods failed, and
1117 * when we don't want to use SIGKILL.
1118 *****************************************************************************/
1119 static void InitSignalHandler( void )
1121 /* Termination signals */
1123 signal( SIGINT, FatalSignalHandler );
1124 signal( SIGHUP, FatalSignalHandler );
1125 signal( SIGQUIT, FatalSignalHandler );
1128 signal( SIGALRM, SimpleSignalHandler );
1129 signal( SIGPIPE, SimpleSignalHandler );
1133 /*****************************************************************************
1134 * SimpleSignalHandler: system signal handler
1135 *****************************************************************************
1136 * This function is called when a non fatal signal is received by the program.
1137 *****************************************************************************/
1138 static void SimpleSignalHandler( int i_signal )
1140 /* Acknowledge the signal received */
1141 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1144 /*****************************************************************************
1145 * FatalSignalHandler: system signal handler
1146 *****************************************************************************
1147 * This function is called when a fatal signal is received by the program.
1148 * It tries to end the program in a clean way.
1149 *****************************************************************************/
1150 static void FatalSignalHandler( int i_signal )
1152 static mtime_t abort_time = 0;
1153 static volatile boolean_t b_die = 0;
1155 /* Once a signal has been trapped, the termination sequence will be
1156 * armed and following signals will be ignored to avoid sending messages
1157 * to an interface having been destroyed */
1161 abort_time = mdate();
1163 /* Acknowledge the signal received */
1164 intf_ErrMsg( "intf error: signal %d received, exiting - do it again "
1165 "if vlc gets stuck", i_signal );
1167 /* Try to terminate everything - this is done by requesting the end
1168 * of the interface thread */
1169 p_main->p_intf->b_die = 1;
1174 /* If user asks again 1 second later, die badly */
1175 if( mdate() > abort_time + 1000000 )
1178 signal( SIGINT, SIG_IGN );
1179 signal( SIGHUP, SIG_IGN );
1180 signal( SIGQUIT, SIG_IGN );
1183 intf_ErrMsg( "intf error: user insisted too much, dying badly" );
1188 /*****************************************************************************
1189 * IllegalSignalHandler: system signal handler
1190 *****************************************************************************
1191 * This function is called when an illegal instruction signal is received by
1192 * the program. We use this function to test OS and CPU capabilities
1193 *****************************************************************************/
1194 static void IllegalSignalHandler( int i_signal )
1196 /* Acknowledge the signal received */
1199 #ifdef HAVE_SIGRELSE
1200 sigrelse( i_signal );
1203 #if defined( __i386__ )
1204 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
1205 "operating system.\n", psz_capability );
1206 fprintf( stderr, " some optimizations will be disabled unless "
1207 "you upgrade your OS\n" );
1208 # if defined( SYS_LINUX )
1209 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );
1216 /*****************************************************************************
1217 * CPUCapabilities: list the processors MMX support and other capabilities
1218 *****************************************************************************
1219 * This function is called to list extensions the CPU may have.
1220 *****************************************************************************/
1221 static u32 CPUCapabilities( void )
1223 volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
1225 #if defined( SYS_DARWIN )
1226 struct host_basic_info hi;
1228 host_name_port_t host;
1231 char *psz_name, *psz_subname;
1233 i_capabilities |= CPU_CAPABILITY_FPU;
1235 /* Should 'never' fail? */
1236 host = mach_host_self();
1238 i_size = sizeof( hi ) / sizeof( int );
1239 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1241 if( ret != KERN_SUCCESS )
1243 fprintf( stderr, "error: couldn't get CPU information\n" );
1244 return( i_capabilities );
1247 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1248 /* FIXME: need better way to detect newer proccessors.
1249 * could do strncmp(a,b,5), but that's real ugly */
1250 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1252 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1255 return( i_capabilities );
1257 #elif defined( __i386__ )
1258 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1259 volatile boolean_t b_amd;
1261 /* Needed for x86 CPU capabilities detection */
1262 # define cpuid( a ) \
1263 asm volatile ( "pushl %%ebx\n\t" \
1265 "movl %%ebx,%1\n\t" \
1274 i_capabilities |= CPU_CAPABILITY_FPU;
1276 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1277 signal( SIGILL, IllegalSignalHandler );
1280 /* test for a 486 CPU */
1281 asm volatile ( "pushl %%ebx\n\t"
1284 "movl %%eax, %%ebx\n\t"
1285 "xorl $0x200000, %%eax\n\t"
1297 if( i_eax == i_ebx )
1299 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1300 signal( SIGILL, NULL );
1302 return( i_capabilities );
1305 i_capabilities |= CPU_CAPABILITY_486;
1307 /* the CPU supports the CPUID instruction - get its level */
1308 cpuid( 0x00000000 );
1312 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1313 signal( SIGILL, NULL );
1315 return( i_capabilities );
1318 /* FIXME: this isn't correct, since some 486s have cpuid */
1319 i_capabilities |= CPU_CAPABILITY_586;
1321 /* borrowed from mpeg2dec */
1322 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1323 && ( i_edx == 0x69746e65 );
1325 /* test for the MMX flag */
1326 cpuid( 0x00000001 );
1328 if( ! (i_edx & 0x00800000) )
1330 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1331 signal( SIGILL, NULL );
1333 return( i_capabilities );
1336 i_capabilities |= CPU_CAPABILITY_MMX;
1338 if( i_edx & 0x02000000 )
1340 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1342 # ifdef CAN_COMPILE_SSE
1343 /* We test if OS supports the SSE instructions */
1344 psz_capability = "SSE";
1346 if( setjmp( env ) == 0 )
1348 /* Test a SSE instruction */
1349 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1352 if( i_illegal == 0 )
1354 i_capabilities |= CPU_CAPABILITY_SSE;
1359 /* test for additional capabilities */
1360 cpuid( 0x80000000 );
1362 if( i_eax < 0x80000001 )
1364 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1365 signal( SIGILL, NULL );
1367 return( i_capabilities );
1370 /* list these additional capabilities */
1371 cpuid( 0x80000001 );
1373 # ifdef CAN_COMPILE_3DNOW
1374 if( i_edx & 0x80000000 )
1376 psz_capability = "3D Now!";
1378 if( setjmp( env ) == 0 )
1380 /* Test a 3D Now! instruction */
1381 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1384 if( i_illegal == 0 )
1386 i_capabilities |= CPU_CAPABILITY_3DNOW;
1391 if( b_amd && ( i_edx & 0x00400000 ) )
1393 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1396 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
1397 signal( SIGILL, NULL );
1399 return( i_capabilities );
1401 #elif defined( __powerpc__ )
1403 i_capabilities |= CPU_CAPABILITY_FPU;
1405 # ifdef CAN_COMPILE_ALTIVEC
1406 signal( SIGILL, IllegalSignalHandler );
1408 psz_capability = "AltiVec";
1410 if( setjmp( env ) == 0 )
1412 asm volatile ("mtspr 256, %0\n\t"
1413 "vand %%v0, %%v0, %%v0"
1418 if( i_illegal == 0 )
1420 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1423 signal( SIGILL, NULL );
1426 return( i_capabilities );
1428 #elif defined( __sparc__ )
1430 i_capabilities |= CPU_CAPABILITY_FPU;
1431 return( i_capabilities );
1434 /* default behaviour */
1435 return( i_capabilities );
1440 /*****************************************************************************
1441 * ShowConsole: On Win32, create an output console for debug messages
1442 *****************************************************************************
1443 * This function is useful only on Win32.
1444 *****************************************************************************/
1446 static void ShowConsole( void )
1449 freopen( "CONOUT$", "w", stdout );
1450 freopen( "CONOUT$", "w", stderr );
1451 freopen( "CONIN$", "r", stdin );