1 /*****************************************************************************
2 * main.c: main vlc source
3 * Includes the main() function for vlc. Parses command line, start interface
5 *****************************************************************************
6 * Copyright (C) 1998, 1999, 2000 VideoLAN
7 * $Id: main.c,v 1.125 2001/11/14 00:01:36 jlj Exp $
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Samuel Hocevar <sam@zoy.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
33 #include <stdio.h> /* sprintf() */
34 #include <setjmp.h> /* longjmp, setjmp */
36 #ifdef HAVE_GETOPT_LONG
38 # include <getopt.h> /* getopt() */
41 # include "GNUgetopt/getopt.h"
45 # include <mach/mach.h> /* Altivec detection */
46 # include <mach/mach_error.h> /* some day the header files||compiler *
47 will define it for us */
48 # include <mach/bootstrap.h>
52 # include <netinet/in.h> /* BSD: struct in_addr */
57 #elif defined( _MSC_VER ) && defined( _WIN32 )
61 #include <errno.h> /* ENOMEM */
62 #include <stdlib.h> /* getenv(), strtol(), */
63 #include <string.h> /* strerror() */
64 #include <fcntl.h> /* open(), O_WRONLY */
65 #include <sys/stat.h> /* S_IREAD */
72 #include "tests.h" /* TestCPU() */
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
79 #include "intf_playlist.h"
80 #include "interface.h"
82 #include "audio_output.h"
85 #include "video_output.h"
88 # include "beos_specific.h"
92 # include "darwin_specific.h"
96 # include "win32_specific.h"
99 #include "netutils.h" /* network_ChannelJoin */
103 /*****************************************************************************
104 * Command line options constants. If something is changed here, be sure that
105 * GetConfiguration and Usage are also changed.
106 *****************************************************************************/
108 /* Long options return values - note that values corresponding to short options
109 * chars, and in general any regular char, should be avoided */
110 #define OPT_NOAUDIO 150
111 #define OPT_STEREO 151
113 #define OPT_SPDIF 153
114 #define OPT_VOLUME 154
116 #define OPT_NOVIDEO 160
117 #define OPT_DISPLAY 161
118 #define OPT_WIDTH 162
119 #define OPT_HEIGHT 163
120 #define OPT_COLOR 164
121 #define OPT_FULLSCREEN 165
122 #define OPT_OVERLAY 166
123 #define OPT_XVADAPTOR 167
126 #define OPT_CHANNELS 170
127 #define OPT_SERVER 171
129 #define OPT_BROADCAST 173
130 #define OPT_CHANNELSERVER 174
132 #define OPT_INPUT 180
133 #define OPT_MOTION 181
136 #define OPT_DOWNMIX 184
137 #define OPT_IMDCT 185
138 #define OPT_DVDCSS_METHOD 186
139 #define OPT_DVDCSS_VERBOSE 187
141 #define OPT_SYNCHRO 190
142 #define OPT_WARNING 191
143 #define OPT_VERSION 192
144 #define OPT_STDOUT 193
145 #define OPT_STATS 194
152 /* Needed for x86 CPU capabilities detection */
154 asm volatile ( "cpuid" \
163 static const struct option longopts[] =
165 /* name, has_arg, flag, val */
167 /* General/common options */
168 { "help", 0, 0, 'h' },
169 { "longhelp", 0, 0, 'H' },
170 { "version", 0, 0, OPT_VERSION },
172 /* Interface options */
173 { "intf", 1, 0, 'I' },
174 { "warning", 1, 0, OPT_WARNING },
175 { "stdout", 1, 0, OPT_STDOUT },
176 { "stats", 0, 0, OPT_STATS },
179 { "noaudio", 0, 0, OPT_NOAUDIO },
180 { "aout", 1, 0, 'A' },
181 { "stereo", 0, 0, OPT_STEREO },
182 { "mono", 0, 0, OPT_MONO },
183 { "spdif", 0, 0, OPT_SPDIF },
184 { "downmix", 1, 0, OPT_DOWNMIX },
185 { "imdct", 1, 0, OPT_IMDCT },
186 { "volume", 1, 0, OPT_VOLUME },
189 { "novideo", 0, 0, OPT_NOVIDEO },
190 { "vout", 1, 0, 'V' },
191 { "display", 1, 0, OPT_DISPLAY },
192 { "width", 1, 0, OPT_WIDTH },
193 { "height", 1, 0, OPT_HEIGHT },
194 { "grayscale", 0, 0, 'g' },
195 { "color", 0, 0, OPT_COLOR },
196 { "motion", 1, 0, OPT_MOTION },
197 { "idct", 1, 0, OPT_IDCT },
198 { "yuv", 1, 0, OPT_YUV },
199 { "fullscreen", 0, 0, OPT_FULLSCREEN },
200 { "overlay", 0, 0, OPT_OVERLAY },
201 { "xvadaptor", 1, 0, OPT_XVADAPTOR },
202 { "smp", 1, 0, OPT_SMP },
205 { "dvdtitle", 1, 0, 't' },
206 { "dvdchapter", 1, 0, 'T' },
207 { "dvdangle", 1, 0, 'u' },
208 { "dvdaudio", 1, 0, 'a' },
209 { "dvdchannel", 1, 0, 'c' },
210 { "dvdsubtitle", 1, 0, 's' },
211 { "dvdcss-method", 1, 0, OPT_DVDCSS_METHOD },
212 { "dvdcss-verbose", 1, 0, OPT_DVDCSS_VERBOSE },
215 { "input", 1, 0, OPT_INPUT },
216 { "server", 1, 0, OPT_SERVER },
217 { "port", 1, 0, OPT_PORT },
218 { "broadcast", 1, 0, OPT_BROADCAST },
219 { "channels", 0, 0, OPT_CHANNELS },
220 { "channelserver", 1, 0, OPT_CHANNELSERVER },
222 /* Synchro options */
223 { "synchro", 1, 0, OPT_SYNCHRO },
228 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
230 /*****************************************************************************
231 * Global variable program_data - these are the only ones, see main.h and
233 *****************************************************************************/
235 module_bank_t *p_module_bank;
236 aout_bank_t *p_aout_bank;
237 vout_bank_t *p_vout_bank;
239 /*****************************************************************************
241 *****************************************************************************/
242 static int GetConfiguration ( int *pi_argc, char *ppsz_argv[],
244 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
245 static void Usage ( int i_fashion );
246 static void Version ( void );
248 static void InitSignalHandler ( void );
249 static void SimpleSignalHandler ( int i_signal );
250 static void FatalSignalHandler ( int i_signal );
251 static void InstructionSignalHandler( int i_signal );
252 static int CPUCapabilities ( void );
254 static int RedirectSTDOUT ( void );
255 static void ShowConsole ( void );
258 static int i_illegal;
260 /*****************************************************************************
261 * main: parse command line, start interface and spawn threads
262 *****************************************************************************
263 * Steps during program execution are:
264 * -configuration parsing and messages interface initialization
265 * -opening of audio output device and some global modules
266 * -execution of interface, which exit on error or on user request
267 * -closing of audio output device and some global modules
268 * On error, the spawned threads are canceled, and the open devices closed.
269 *****************************************************************************/
270 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
272 main_t main_data; /* root of all data - see main.h */
273 module_bank_t module_bank;
274 aout_bank_t aout_bank;
275 vout_bank_t vout_bank;
277 p_main = &main_data; /* set up the global variables */
278 p_module_bank = &module_bank;
279 p_aout_bank = &aout_bank;
280 p_vout_bank = &vout_bank;
283 * Initialize threads system
288 * Test if our code is likely to run on this CPU
290 p_main->i_cpu_capabilities = CPUCapabilities();
293 * System specific initialization code
295 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
296 system_Init( &i_argc, ppsz_argv, ppsz_env );
298 #elif defined( SYS_LINUX )
300 /* Activate malloc checking routines to detect heap corruptions. */
301 main_PutIntVariable( "MALLOC_CHECK_", 2 );
306 * Initialize messages interface
308 p_main->p_msg = intf_MsgCreate();
309 if( !p_main->p_msg ) /* start messages interface */
311 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
316 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
321 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
328 * Redirect the standard output if required by the user, and on Win32 we
329 * also open a console to display the debug messages.
333 if( p_main->b_stats )
335 char p_capabilities[200];
336 p_capabilities[0] = '\0';
338 #define PRINT_CAPABILITY( capability, string ) \
339 if( p_main->i_cpu_capabilities & capability ) \
341 strncat( p_capabilities, string " ", \
342 sizeof(p_capabilities) - strlen(p_capabilities) ); \
343 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
346 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
347 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
348 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
349 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
350 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
351 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
352 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
353 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
354 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
358 * Initialize playlist and get commandline files
360 p_main->p_playlist = intf_PlaylistCreate();
361 if( !p_main->p_playlist )
363 intf_ErrMsg( "playlist error: playlist initialization failed" );
367 intf_PlaylistInit( p_main->p_playlist );
370 * Get input filenames given as commandline arguments
372 GetFilenames( i_argc, ppsz_argv );
375 * Initialize module, aout and vout banks
382 * Initialize shared resources and libraries
384 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
385 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
386 network_ChannelCreate() )
388 /* On error during Channels initialization, switch off channels */
389 intf_Msg( "Channels initialization failed : "
390 "Channel management is deactivated" );
391 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
395 * Try to run the interface
397 p_main->p_intf = intf_Create();
398 if( p_main->p_intf == NULL )
400 intf_ErrMsg( "intf error: interface initialization failed" );
405 * Set signal handling policy for all threads
410 * This is the main loop
412 p_main->p_intf->pf_run( p_main->p_intf );
415 * Finished, destroy the interface
417 intf_Destroy( p_main->p_intf );
420 * Go back into channel 0 which is the network
422 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
423 INPUT_NETWORK_CHANNEL_DEFAULT ) )
425 network_ChannelJoin( COMMON_CHANNEL );
430 * Free module, aout and vout banks
439 intf_PlaylistDestroy( p_main->p_playlist );
442 * System specific cleaning code
444 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
450 * Terminate messages interface and program
452 intf_Msg( "intf: program terminated" );
456 * Stop threads system
463 /*****************************************************************************
464 * main_GetIntVariable: get the int value of an environment variable
465 *****************************************************************************
466 * This function is used to read some default parameters in modules.
467 *****************************************************************************/
468 int main_GetIntVariable( char *psz_name, int i_default )
470 char * psz_env; /* environment value */
471 char * psz_end; /* end of parsing index */
472 long int i_value; /* value */
474 psz_env = getenv( psz_name );
477 i_value = strtol( psz_env, &psz_end, 0 );
478 if( (*psz_env != '\0') && (*psz_end == '\0') )
486 /*****************************************************************************
487 * main_GetPszVariable: get the string value of an environment variable
488 *****************************************************************************
489 * This function is used to read some default parameters in modules.
490 *****************************************************************************/
491 char * main_GetPszVariable( char *psz_name, char *psz_default )
495 psz_env = getenv( psz_name );
500 return( psz_default );
503 /*****************************************************************************
504 * main_PutPszVariable: set the string value of an environment variable
505 *****************************************************************************
506 * This function is used to set some default parameters in modules. The use of
507 * this function will cause some memory leak: since some systems use the pointer
508 * passed to putenv to store the environment string, it can't be freed.
509 *****************************************************************************/
510 void main_PutPszVariable( char *psz_name, char *psz_value )
514 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
515 if( psz_env == NULL )
517 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
522 sprintf( psz_env, "%s=%s", psz_name, psz_value );
523 if( putenv( psz_env ) )
525 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
530 /*****************************************************************************
531 * main_PutIntVariable: set the integer value of an environment variable
532 *****************************************************************************
533 * This function is used to set some default parameters in modules. The use of
534 * this function will cause some memory leak: since some systems use the pointer
535 * passed to putenv to store the environment string, it can't be freed.
536 *****************************************************************************/
537 void main_PutIntVariable( char *psz_name, int i_value )
539 char psz_value[ 256 ]; /* buffer for value */
541 sprintf( psz_value, "%d", i_value );
542 main_PutPszVariable( psz_name, psz_value );
545 /* following functions are local */
547 /*****************************************************************************
548 * GetConfiguration: parse command line
549 *****************************************************************************
550 * Parse command line and configuration file for configuration. If the inline
551 * help is requested, the function Usage() is called and the function returns
552 * -1 (causing main() to exit). The messages interface is initialized at this
553 * stage, but most structures are not allocated, so only environment should
555 *****************************************************************************/
556 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
561 /* Set default configuration and copy arguments */
562 p_main->i_argc = *pi_argc;
563 p_main->ppsz_argv = ppsz_argv;
564 p_main->ppsz_env = ppsz_env;
569 p_main->i_warning_level = 0;
572 p_main->p_channel = NULL;
574 /* Get the executable name (similar to the basename command) */
575 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
580 p_main->psz_arg0 = ++p_tmp;
589 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
590 * is the PSN - process serial number (a unique PID-ish thingie)
591 * still ok for real Darwin & when run from command line */
592 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
593 /* for example -psn_0_9306113 */
595 /* GDMF!... I can't do this or else the MacOSX window server will
596 * not pick up the PSN and not register the app and we crash...
597 * hence the following kludge otherwise we'll get confused w/ argv[1]
598 * being an input file name */
600 ppsz_argv[ 1 ] = NULL;
602 *pi_argc = *pi_argc - 1;
608 /* Parse command line options */
610 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
611 psz_shortopts, longopts, 0 ) ) != EOF )
615 /* General/common options */
616 case 'h': /* -h, --help */
620 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
621 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
622 INTF_STDOUT_DEFAULT ) ) == 0 )
624 /* No stdout redirection has been asked for */
625 intf_MsgImm( "\nPress the RETURN key to continue..." );
631 case 'H': /* -H, --longhelp */
635 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
636 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
637 INTF_STDOUT_DEFAULT ) ) == 0 )
639 /* No stdout redirection has been asked for */
640 intf_MsgImm( "\nPress the RETURN key to continue..." );
646 case OPT_VERSION: /* --version */
650 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
651 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
652 INTF_STDOUT_DEFAULT ) ) == 0 )
654 /* No stdout redirection has been asked for */
655 intf_MsgImm( "\nPress the RETURN key to continue..." );
661 case 'v': /* -v, --verbose */
662 p_main->i_warning_level++;
665 /* Interface warning messages level */
666 case 'I': /* -I, --intf */
667 main_PutPszVariable( INTF_METHOD_VAR, optarg );
669 case OPT_WARNING: /* --warning */
670 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
671 p_main->i_warning_level = atoi(optarg);
674 case OPT_STDOUT: /* --stdout */
675 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
683 case OPT_NOAUDIO: /* --noaudio */
686 case 'A': /* -A, --aout */
687 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
689 case OPT_STEREO: /* --stereo */
690 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
692 case OPT_MONO: /* --mono */
693 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
695 case OPT_SPDIF: /* --spdif */
696 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
698 case OPT_DOWNMIX: /* --downmix */
699 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
701 case OPT_IMDCT: /* --imdct */
702 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
704 case OPT_VOLUME: /* --volume */
705 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
709 case OPT_NOVIDEO: /* --novideo */
712 case 'V': /* -V, --vout */
713 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
715 case OPT_DISPLAY: /* --display */
716 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
718 case OPT_WIDTH: /* --width */
719 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
721 case OPT_HEIGHT: /* --height */
722 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
724 case 'g': /* -g, --grayscale */
725 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
727 case OPT_COLOR: /* --color */
728 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
730 case OPT_FULLSCREEN: /* --fullscreen */
731 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
733 case OPT_OVERLAY: /* --overlay */
734 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
736 case OPT_XVADAPTOR: /* --xvadaptor */
737 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
739 case OPT_MOTION: /* --motion */
740 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
742 case OPT_IDCT: /* --idct */
743 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
745 case OPT_YUV: /* --yuv */
746 main_PutPszVariable( YUV_METHOD_VAR, optarg );
748 case OPT_SMP: /* --smp */
749 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
753 case 't': /* --dvdtitle */
754 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
756 case 'T': /* --dvdchapter */
757 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
759 case 'u': /* --dvdangle */
760 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
762 case 'a': /* --dvdaudio */
763 if ( ! strcmp(optarg, "ac3") )
764 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
765 else if ( ! strcmp(optarg, "lpcm") )
766 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
767 else if ( ! strcmp(optarg, "mpeg") )
768 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
770 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
772 case 'c': /* --dvdchannel */
773 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
775 case 's': /* --dvdsubtitle */
776 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
778 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
779 main_PutPszVariable( "DVDCSS_METHOD", optarg );
781 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
782 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
786 case OPT_INPUT: /* --input */
787 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
789 case OPT_CHANNELS: /* --channels */
790 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
792 case OPT_CHANNELSERVER: /* --channelserver */
793 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
795 case OPT_SERVER: /* --server */
796 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
798 case OPT_PORT: /* --port */
799 main_PutPszVariable( INPUT_PORT_VAR, optarg );
801 case OPT_BROADCAST: /* --broadcast */
802 main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
803 main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
806 /* Synchro options */
808 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
811 /* Internal error: unknown option */
816 intf_ErrMsg( "intf error: unknown option `%s'",
819 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
820 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
821 INTF_STDOUT_DEFAULT ) ) == 0 )
823 /* No stdout redirection has been asked for */
824 intf_MsgImm( "\nPress the RETURN key to continue..." );
833 if( p_main->i_warning_level < 0 )
835 p_main->i_warning_level = 0;
841 /*****************************************************************************
842 * GetFilenames: parse command line options which are not flags
843 *****************************************************************************
844 * Parse command line for input files.
845 *****************************************************************************/
846 static int GetFilenames( int i_argc, char *ppsz_argv[] )
850 /* We assume that the remaining parameters are filenames */
851 for( i_opt = optind; i_opt < i_argc; i_opt++ )
853 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
854 ppsz_argv[ i_opt ] );
860 /*****************************************************************************
861 * Usage: print program usage
862 *****************************************************************************
863 * Print a short inline help. Message interface is initialized at this stage.
864 *****************************************************************************/
865 static void Usage( int i_fashion )
868 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
871 if( i_fashion == USAGE )
873 intf_MsgImm( "Try `%s --help' for more information.",
879 intf_MsgImm( "\nOptions:"
880 "\n -I, --intf <module> \tinterface method"
881 "\n -v, --verbose \tverbose mode (cumulative)"
882 "\n --stdout <filename> \tredirect console stdout"
884 "\n --noaudio \tdisable audio"
885 "\n -A, --aout <module> \taudio output method"
886 "\n --stereo, --mono \tstereo/mono audio"
887 "\n --spdif \tAC3 pass-through mode"
888 "\n --downmix <module> \tAC3 downmix method"
889 "\n --imdct <module> \tAC3 IMDCT method"
890 "\n --volume [0..1024] \tVLC output volume"
892 "\n --novideo \tdisable video"
893 "\n -V, --vout <module> \tvideo output method"
894 "\n --display <display> \tdisplay string"
895 "\n --width <w>, --height <h> \tdisplay dimensions"
896 "\n -g, --grayscale \tgrayscale output"
897 "\n --fullscreen \tfullscreen output"
898 "\n --overlay \taccelerated display"
899 "\n --xvadaptor <adaptor> \tXVideo adaptor"
900 "\n --color \tcolor output"
901 "\n --motion <module> \tmotion compensation method"
902 "\n --idct <module> \tIDCT method"
903 "\n --yuv <module> \tYUV method"
904 "\n --synchro <type> \tforce synchro algorithm"
905 "\n --smp <number of threads> \tuse several processors"
907 "\n -t, --dvdtitle <num> \tchoose DVD title"
908 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
909 "\n -u, --dvdangle <num> \tchoose DVD angle"
910 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
911 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
912 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
913 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
914 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
916 "\n --input \tinput method"
917 "\n --channels \tenable channels"
918 "\n --channelserver <host> \tchannel server address"
919 "\n --server <host> \tvideo server address"
920 "\n --port <port> \tvideo server port"
921 "\n --broadcast \tlisten to a broadcast"
923 "\n -h, --help \tprint help and exit"
924 "\n -H, --longhelp \tprint long help and exit"
925 "\n --version \toutput version information and exit" );
927 if( i_fashion == SHORT_HELP )
930 /* Interface parameters */
931 intf_MsgImm( "\nInterface parameters:"
932 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
933 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
934 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
935 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout" );
937 /* Audio parameters */
938 intf_MsgImm( "\nAudio parameters:"
939 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
940 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
941 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
942 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
943 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
944 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
945 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
946 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
948 /* Video parameters */
949 intf_MsgImm( "\nVideo parameters:"
950 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
951 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
952 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
953 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
954 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
955 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
956 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
957 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
958 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
959 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
960 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
961 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
962 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
963 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
966 intf_MsgImm( "\nDVD parameters:"
967 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
968 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
969 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
970 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
971 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
972 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
973 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
975 /* Input parameters */
976 intf_MsgImm( "\nInput parameters:"
977 "\n " INPUT_SERVER_VAR "=<hostname> \tvideo server"
978 "\n " INPUT_PORT_VAR "=<port> \tvideo server port"
979 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
980 "\n " INPUT_BCAST_ADDR_VAR "=<addr> \tbroadcast mode"
981 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
982 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
986 /*****************************************************************************
987 * Version: print complete program version
988 *****************************************************************************
989 * Print complete program version and build number.
990 *****************************************************************************/
991 static void Version( void )
993 intf_MsgImm( VERSION_MESSAGE
994 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
995 "You may redistribute it under the terms of the GNU General Public License;\n"
996 "see the file named COPYING for details.\n"
997 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1000 /*****************************************************************************
1001 * InitSignalHandler: system signal handler initialization
1002 *****************************************************************************
1003 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1004 * at least a method to kill the program when all other methods failed, and
1005 * when we don't want to use SIGKILL.
1006 *****************************************************************************/
1007 static void InitSignalHandler( void )
1009 /* Termination signals */
1011 signal( SIGINT, FatalSignalHandler );
1012 signal( SIGHUP, FatalSignalHandler );
1013 signal( SIGQUIT, FatalSignalHandler );
1016 signal( SIGALRM, SimpleSignalHandler );
1017 signal( SIGPIPE, SimpleSignalHandler );
1022 /*****************************************************************************
1023 * SimpleSignalHandler: system signal handler
1024 *****************************************************************************
1025 * This function is called when a non fatal signal is received by the program.
1026 *****************************************************************************/
1027 static void SimpleSignalHandler( int i_signal )
1029 /* Acknowledge the signal received */
1030 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1034 /*****************************************************************************
1035 * FatalSignalHandler: system signal handler
1036 *****************************************************************************
1037 * This function is called when a fatal signal is received by the program.
1038 * It tries to end the program in a clean way.
1039 *****************************************************************************/
1040 static void FatalSignalHandler( int i_signal )
1042 /* Once a signal has been trapped, the termination sequence will be
1043 * armed and following signals will be ignored to avoid sending messages
1044 * to an interface having been destroyed */
1046 signal( SIGINT, SIG_IGN );
1047 signal( SIGHUP, SIG_IGN );
1048 signal( SIGQUIT, SIG_IGN );
1051 /* Acknowledge the signal received */
1052 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1054 /* Try to terminate everything - this is done by requesting the end of the
1055 * interface thread */
1056 p_main->p_intf->b_die = 1;
1059 /*****************************************************************************
1060 * InstructionSignalHandler: system signal handler
1061 *****************************************************************************
1062 * This function is called when a illegal instruction signal is received by
1064 * We use this function to test OS and CPU_Capabilities
1065 *****************************************************************************/
1066 static void InstructionSignalHandler( int i_signal )
1068 /* Once a signal has been trapped, the termination sequence will be
1069 * armed and following signals will be ignored to avoid sending messages
1070 * to an interface having been destroyed */
1072 /* Acknowledge the signal received */
1075 #ifdef HAVE_SIGRELSE
1076 sigrelse( i_signal );
1081 /*****************************************************************************
1082 * CPUCapabilities: list the processors MMX support and other capabilities
1083 *****************************************************************************
1084 * This function is called to list extensions the CPU may have.
1085 *****************************************************************************/
1086 static int CPUCapabilities( void )
1088 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1090 #if defined( SYS_BEOS )
1091 i_capabilities |= CPU_CAPABILITY_486
1092 | CPU_CAPABILITY_586
1093 | CPU_CAPABILITY_MMX;
1095 return( i_capabilities );
1097 #elif defined( SYS_DARWIN )
1098 struct host_basic_info hi;
1100 host_name_port_t host;
1103 char *psz_name, *psz_subname;
1105 /* Should 'never' fail? */
1106 host = mach_host_self();
1108 i_size = sizeof( hi ) / sizeof( int );
1109 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1111 if( ret != KERN_SUCCESS )
1113 fprintf( stderr, "error: couldn't get CPU information\n" );
1114 return( i_capabilities );
1117 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1118 /* FIXME: need better way to detect newer proccessors.
1119 * could do strncmp(a,b,5), but that's real ugly */
1120 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1122 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1125 return( i_capabilities );
1127 #elif defined( __i386__ )
1128 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1129 volatile boolean_t b_amd;
1131 signal( SIGILL, InstructionSignalHandler );
1133 /* test for a 486 CPU */
1134 asm volatile ( "pushfl\n\t"
1136 "movl %%eax, %%ebx\n\t"
1137 "xorl $0x200000, %%eax\n\t"
1147 if( i_eax == i_ebx )
1149 signal( SIGILL, NULL );
1150 return( i_capabilities );
1153 i_capabilities |= CPU_CAPABILITY_486;
1155 /* the CPU supports the CPUID instruction - get its level */
1156 cpuid( 0x00000000 );
1160 signal( SIGILL, NULL );
1161 return( i_capabilities );
1164 /* FIXME: this isn't correct, since some 486s have cpuid */
1165 i_capabilities |= CPU_CAPABILITY_586;
1167 /* borrowed from mpeg2dec */
1168 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1169 && ( i_edx == 0x69746e65 );
1171 /* test for the MMX flag */
1172 cpuid( 0x00000001 );
1174 if( ! (i_edx & 0x00800000) )
1176 signal( SIGILL, NULL );
1177 return( i_capabilities );
1180 i_capabilities |= CPU_CAPABILITY_MMX;
1182 if( i_edx & 0x02000000 )
1184 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1187 /* We test if OS support the SSE instructions */
1189 if( setjmp( env ) == 0 )
1191 /* Test a SSE instruction */
1192 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1195 if( i_illegal == 0 )
1197 i_capabilities |= CPU_CAPABILITY_SSE;
1201 fprintf( stderr, "warning: your OS doesn't have support for "
1202 "SSE instructions, "
1203 "some optimizations\nwill be disabled\n" );
1205 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1211 /* test for additional capabilities */
1212 cpuid( 0x80000000 );
1214 if( i_eax < 0x80000001 )
1216 signal( SIGILL, NULL );
1217 return( i_capabilities );
1220 /* list these additional capabilities */
1221 cpuid( 0x80000001 );
1224 if( i_edx & 0x80000000 )
1227 if( setjmp( env ) == 0 )
1229 /* Test a 3D Now! instruction */
1230 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1233 if( i_illegal == 0 )
1235 i_capabilities |= CPU_CAPABILITY_3DNOW;
1240 if( b_amd && ( i_edx & 0x00400000 ) )
1242 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1245 signal( SIGILL, NULL );
1246 return( i_capabilities );
1248 #elif defined( __powerpc__ )
1250 /* Test for Altivec */
1251 signal( SIGILL, InstructionSignalHandler );
1255 if( setjmp( env ) == 0 )
1257 asm volatile ("mtspr 256,%0" : : "r" (-1));
1260 if( i_illegal == 0 )
1262 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1266 signal( SIGILL, NULL );
1267 return( i_capabilities );
1270 /* default behaviour */
1271 return( i_capabilities );
1276 /*****************************************************************************
1277 * RedirectSTDOUT: redirect stdout and stderr to a file
1278 *****************************************************************************
1279 * This function will redirect stdout and stderr to a file if the user has
1281 *****************************************************************************/
1282 static int RedirectSTDOUT( void )
1284 int i_stdout_filedesc;
1285 char *psz_stdout_filename;
1287 psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1288 INTF_STDOUT_DEFAULT );
1289 if( strcmp( "", psz_stdout_filename ) != 0 )
1292 i_stdout_filedesc = open( psz_stdout_filename,
1293 O_CREAT | O_TRUNC | O_RDWR,
1294 S_IREAD | S_IWRITE );
1296 if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1298 intf_ErrMsg( "warning: unable to redirect stdout" );
1301 if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1303 intf_ErrMsg( "warning: unable to redirect stderr" );
1306 close( i_stdout_filedesc );
1310 /* No stdout redirection has been asked so open a console */
1311 if( p_main->i_warning_level )
1321 /*****************************************************************************
1322 * ShowConsole: On Win32, create an output console for debug messages
1323 *****************************************************************************
1324 * This function is usefull only on Win32.
1325 *****************************************************************************/
1326 static void ShowConsole( void )
1330 freopen( "CONOUT$", "w", stdout );
1331 freopen( "CONOUT$", "w", stderr );
1332 freopen( "CONIN$", "r", stdin );