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.127 2001/11/28 15:08:06 massiot 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 */
73 #include "tests.h" /* TestCPU() */
76 #include "stream_control.h"
77 #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 { "channels", 0, 0, OPT_CHANNELS },
217 { "channelserver", 1, 0, OPT_CHANNELSERVER },
219 /* Synchro options */
220 { "synchro", 1, 0, OPT_SYNCHRO },
225 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
227 /*****************************************************************************
228 * Global variable program_data - these are the only ones, see main.h and
230 *****************************************************************************/
232 module_bank_t *p_module_bank;
233 aout_bank_t *p_aout_bank;
234 vout_bank_t *p_vout_bank;
236 /*****************************************************************************
238 *****************************************************************************/
239 static int GetConfiguration ( int *pi_argc, char *ppsz_argv[],
241 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
242 static void Usage ( int i_fashion );
243 static void Version ( void );
245 static void InitSignalHandler ( void );
246 static void SimpleSignalHandler ( int i_signal );
247 static void FatalSignalHandler ( int i_signal );
248 static void InstructionSignalHandler( int i_signal );
249 static int CPUCapabilities ( void );
251 static int RedirectSTDOUT ( void );
252 static void ShowConsole ( void );
255 static int i_illegal;
257 /*****************************************************************************
258 * main: parse command line, start interface and spawn threads
259 *****************************************************************************
260 * Steps during program execution are:
261 * -configuration parsing and messages interface initialization
262 * -opening of audio output device and some global modules
263 * -execution of interface, which exit on error or on user request
264 * -closing of audio output device and some global modules
265 * On error, the spawned threads are canceled, and the open devices closed.
266 *****************************************************************************/
267 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
269 main_t main_data; /* root of all data - see main.h */
270 module_bank_t module_bank;
271 aout_bank_t aout_bank;
272 vout_bank_t vout_bank;
274 p_main = &main_data; /* set up the global variables */
275 p_module_bank = &module_bank;
276 p_aout_bank = &aout_bank;
277 p_vout_bank = &vout_bank;
280 * Initialize threads system
285 * Test if our code is likely to run on this CPU
287 p_main->i_cpu_capabilities = CPUCapabilities();
290 * System specific initialization code
292 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
293 system_Init( &i_argc, ppsz_argv, ppsz_env );
295 #elif defined( SYS_LINUX )
297 /* Activate malloc checking routines to detect heap corruptions. */
298 main_PutIntVariable( "MALLOC_CHECK_", 2 );
303 * Initialize messages interface
305 p_main->p_msg = intf_MsgCreate();
306 if( !p_main->p_msg ) /* start messages interface */
308 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
313 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
318 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
325 * Redirect the standard output if required by the user, and on Win32 we
326 * also open a console to display the debug messages.
330 if( p_main->b_stats )
332 char p_capabilities[200];
333 p_capabilities[0] = '\0';
335 #define PRINT_CAPABILITY( capability, string ) \
336 if( p_main->i_cpu_capabilities & capability ) \
338 strncat( p_capabilities, string " ", \
339 sizeof(p_capabilities) - strlen(p_capabilities) ); \
340 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
343 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
344 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
345 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
346 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
347 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
348 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
349 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
350 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
351 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
355 * Initialize playlist and get commandline files
357 p_main->p_playlist = intf_PlaylistCreate();
358 if( !p_main->p_playlist )
360 intf_ErrMsg( "playlist error: playlist initialization failed" );
364 intf_PlaylistInit( p_main->p_playlist );
367 * Get input filenames given as commandline arguments
369 GetFilenames( i_argc, ppsz_argv );
372 * Initialize module, aout and vout banks
379 * Initialize shared resources and libraries
381 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
382 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
383 network_ChannelCreate() )
385 /* On error during Channels initialization, switch off channels */
386 intf_Msg( "Channels initialization failed : "
387 "Channel management is deactivated" );
388 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
392 * Try to run the interface
394 p_main->p_intf = intf_Create();
395 if( p_main->p_intf == NULL )
397 intf_ErrMsg( "intf error: interface initialization failed" );
402 * Set signal handling policy for all threads
407 * This is the main loop
409 p_main->p_intf->pf_run( p_main->p_intf );
412 * Finished, destroy the interface
414 intf_Destroy( p_main->p_intf );
417 * Go back into channel 0 which is the network
419 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
420 INPUT_NETWORK_CHANNEL_DEFAULT ) )
422 network_ChannelJoin( COMMON_CHANNEL );
427 * Free module, aout and vout banks
436 intf_PlaylistDestroy( p_main->p_playlist );
439 * System specific cleaning code
441 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
447 * Terminate messages interface and program
449 intf_Msg( "intf: program terminated" );
453 * Stop threads system
460 /*****************************************************************************
461 * main_GetIntVariable: get the int value of an environment variable
462 *****************************************************************************
463 * This function is used to read some default parameters in modules.
464 *****************************************************************************/
465 int main_GetIntVariable( char *psz_name, int i_default )
467 char * psz_env; /* environment value */
468 char * psz_end; /* end of parsing index */
469 long int i_value; /* value */
471 psz_env = getenv( psz_name );
474 i_value = strtol( psz_env, &psz_end, 0 );
475 if( (*psz_env != '\0') && (*psz_end == '\0') )
483 /*****************************************************************************
484 * main_GetPszVariable: get the string value of an environment variable
485 *****************************************************************************
486 * This function is used to read some default parameters in modules.
487 *****************************************************************************/
488 char * main_GetPszVariable( char *psz_name, char *psz_default )
492 psz_env = getenv( psz_name );
497 return( psz_default );
500 /*****************************************************************************
501 * main_PutPszVariable: set the string value of an environment variable
502 *****************************************************************************
503 * This function is used to set some default parameters in modules. The use of
504 * this function will cause some memory leak: since some systems use the pointer
505 * passed to putenv to store the environment string, it can't be freed.
506 *****************************************************************************/
507 void main_PutPszVariable( char *psz_name, char *psz_value )
511 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
512 if( psz_env == NULL )
514 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
519 sprintf( psz_env, "%s=%s", psz_name, psz_value );
520 if( putenv( psz_env ) )
522 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
527 /*****************************************************************************
528 * main_PutIntVariable: set the integer value of an environment variable
529 *****************************************************************************
530 * This function is used to set some default parameters in modules. The use of
531 * this function will cause some memory leak: since some systems use the pointer
532 * passed to putenv to store the environment string, it can't be freed.
533 *****************************************************************************/
534 void main_PutIntVariable( char *psz_name, int i_value )
536 char psz_value[ 256 ]; /* buffer for value */
538 sprintf( psz_value, "%d", i_value );
539 main_PutPszVariable( psz_name, psz_value );
542 /* following functions are local */
544 /*****************************************************************************
545 * GetConfiguration: parse command line
546 *****************************************************************************
547 * Parse command line and configuration file for configuration. If the inline
548 * help is requested, the function Usage() is called and the function returns
549 * -1 (causing main() to exit). The messages interface is initialized at this
550 * stage, but most structures are not allocated, so only environment should
552 *****************************************************************************/
553 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
558 /* Set default configuration and copy arguments */
559 p_main->i_argc = *pi_argc;
560 p_main->ppsz_argv = ppsz_argv;
561 p_main->ppsz_env = ppsz_env;
566 p_main->i_warning_level = 0;
569 p_main->p_channel = NULL;
571 /* Get the executable name (similar to the basename command) */
572 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
577 p_main->psz_arg0 = ++p_tmp;
586 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
587 * is the PSN - process serial number (a unique PID-ish thingie)
588 * still ok for real Darwin & when run from command line */
589 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
590 /* for example -psn_0_9306113 */
592 /* GDMF!... I can't do this or else the MacOSX window server will
593 * not pick up the PSN and not register the app and we crash...
594 * hence the following kludge otherwise we'll get confused w/ argv[1]
595 * being an input file name */
597 ppsz_argv[ 1 ] = NULL;
599 *pi_argc = *pi_argc - 1;
605 /* Parse command line options */
607 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
608 psz_shortopts, longopts, 0 ) ) != EOF )
612 /* General/common options */
613 case 'h': /* -h, --help */
617 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
618 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
619 INTF_STDOUT_DEFAULT ) ) == 0 )
621 /* No stdout redirection has been asked for */
622 intf_MsgImm( "\nPress the RETURN key to continue..." );
628 case 'H': /* -H, --longhelp */
632 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
633 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
634 INTF_STDOUT_DEFAULT ) ) == 0 )
636 /* No stdout redirection has been asked for */
637 intf_MsgImm( "\nPress the RETURN key to continue..." );
643 case OPT_VERSION: /* --version */
647 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
648 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
649 INTF_STDOUT_DEFAULT ) ) == 0 )
651 /* No stdout redirection has been asked for */
652 intf_MsgImm( "\nPress the RETURN key to continue..." );
658 case 'v': /* -v, --verbose */
659 p_main->i_warning_level++;
662 /* Interface warning messages level */
663 case 'I': /* -I, --intf */
664 main_PutPszVariable( INTF_METHOD_VAR, optarg );
666 case OPT_WARNING: /* --warning */
667 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
668 p_main->i_warning_level = atoi(optarg);
671 case OPT_STDOUT: /* --stdout */
672 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
680 case OPT_NOAUDIO: /* --noaudio */
683 case 'A': /* -A, --aout */
684 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
686 case OPT_STEREO: /* --stereo */
687 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
689 case OPT_MONO: /* --mono */
690 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
692 case OPT_SPDIF: /* --spdif */
693 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
695 case OPT_DOWNMIX: /* --downmix */
696 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
698 case OPT_IMDCT: /* --imdct */
699 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
701 case OPT_VOLUME: /* --volume */
702 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
706 case OPT_NOVIDEO: /* --novideo */
709 case 'V': /* -V, --vout */
710 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
712 case OPT_DISPLAY: /* --display */
713 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
715 case OPT_WIDTH: /* --width */
716 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
718 case OPT_HEIGHT: /* --height */
719 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
721 case 'g': /* -g, --grayscale */
722 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
724 case OPT_COLOR: /* --color */
725 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
727 case OPT_FULLSCREEN: /* --fullscreen */
728 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
730 case OPT_OVERLAY: /* --overlay */
731 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
733 case OPT_XVADAPTOR: /* --xvadaptor */
734 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
736 case OPT_MOTION: /* --motion */
737 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
739 case OPT_IDCT: /* --idct */
740 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
742 case OPT_YUV: /* --yuv */
743 main_PutPszVariable( YUV_METHOD_VAR, optarg );
745 case OPT_SMP: /* --smp */
746 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
750 case 't': /* --dvdtitle */
751 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
753 case 'T': /* --dvdchapter */
754 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
756 case 'u': /* --dvdangle */
757 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
759 case 'a': /* --dvdaudio */
760 if ( ! strcmp(optarg, "ac3") )
761 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
762 else if ( ! strcmp(optarg, "lpcm") )
763 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
764 else if ( ! strcmp(optarg, "mpeg") )
765 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
767 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
769 case 'c': /* --dvdchannel */
770 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
772 case 's': /* --dvdsubtitle */
773 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
775 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
776 main_PutPszVariable( "DVDCSS_METHOD", optarg );
778 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
779 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
783 case OPT_INPUT: /* --input */
784 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
786 case OPT_CHANNELS: /* --channels */
787 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
789 case OPT_CHANNELSERVER: /* --channelserver */
790 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
793 /* Synchro options */
795 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
798 /* Internal error: unknown option */
803 intf_ErrMsg( "intf error: unknown option `%s'",
806 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
807 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
808 INTF_STDOUT_DEFAULT ) ) == 0 )
810 /* No stdout redirection has been asked for */
811 intf_MsgImm( "\nPress the RETURN key to continue..." );
820 if( p_main->i_warning_level < 0 )
822 p_main->i_warning_level = 0;
828 /*****************************************************************************
829 * GetFilenames: parse command line options which are not flags
830 *****************************************************************************
831 * Parse command line for input files.
832 *****************************************************************************/
833 static int GetFilenames( int i_argc, char *ppsz_argv[] )
837 /* We assume that the remaining parameters are filenames */
838 for( i_opt = optind; i_opt < i_argc; i_opt++ )
840 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
841 ppsz_argv[ i_opt ] );
847 /*****************************************************************************
848 * Usage: print program usage
849 *****************************************************************************
850 * Print a short inline help. Message interface is initialized at this stage.
851 *****************************************************************************/
852 static void Usage( int i_fashion )
855 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
858 if( i_fashion == USAGE )
860 intf_MsgImm( "Try `%s --help' for more information.",
866 intf_MsgImm( "\nOptions:"
867 "\n -I, --intf <module> \tinterface method"
868 "\n -v, --verbose \tverbose mode (cumulative)"
869 "\n --stdout <filename> \tredirect console stdout"
871 "\n --noaudio \tdisable audio"
872 "\n -A, --aout <module> \taudio output method"
873 "\n --stereo, --mono \tstereo/mono audio"
874 "\n --spdif \tAC3 pass-through mode"
875 "\n --downmix <module> \tAC3 downmix method"
876 "\n --imdct <module> \tAC3 IMDCT method"
877 "\n --volume [0..1024] \tVLC output volume"
879 "\n --novideo \tdisable video"
880 "\n -V, --vout <module> \tvideo output method"
881 "\n --display <display> \tdisplay string"
882 "\n --width <w>, --height <h> \tdisplay dimensions"
883 "\n -g, --grayscale \tgrayscale output"
884 "\n --fullscreen \tfullscreen output"
885 "\n --overlay \taccelerated display"
886 "\n --xvadaptor <adaptor> \tXVideo adaptor"
887 "\n --color \tcolor output"
888 "\n --motion <module> \tmotion compensation method"
889 "\n --idct <module> \tIDCT method"
890 "\n --yuv <module> \tYUV method"
891 "\n --synchro <type> \tforce synchro algorithm"
892 "\n --smp <number of threads> \tuse several processors"
894 "\n -t, --dvdtitle <num> \tchoose DVD title"
895 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
896 "\n -u, --dvdangle <num> \tchoose DVD angle"
897 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
898 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
899 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
900 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
901 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
903 "\n --input \tinput method"
904 "\n --channels \tenable channels"
905 "\n --channelserver <host> \tchannel server address"
907 "\n -h, --help \tprint help and exit"
908 "\n -H, --longhelp \tprint long help and exit"
909 "\n --version \toutput version information and exit" );
911 if( i_fashion == SHORT_HELP )
914 /* Interface parameters */
915 intf_MsgImm( "\nInterface parameters:"
916 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
917 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
918 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
919 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout" );
921 /* Audio parameters */
922 intf_MsgImm( "\nAudio parameters:"
923 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
924 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
925 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
926 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
927 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
928 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
929 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
930 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
932 /* Video parameters */
933 intf_MsgImm( "\nVideo parameters:"
934 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
935 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
936 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
937 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
938 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
939 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
940 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
941 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
942 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
943 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
944 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
945 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
946 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
947 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
950 intf_MsgImm( "\nDVD parameters:"
951 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
952 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
953 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
954 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
955 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
956 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
957 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
959 /* Input parameters */
960 intf_MsgImm( "\nInput parameters:"
961 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
962 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
963 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
967 /*****************************************************************************
968 * Version: print complete program version
969 *****************************************************************************
970 * Print complete program version and build number.
971 *****************************************************************************/
972 static void Version( void )
974 intf_MsgImm( VERSION_MESSAGE
975 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
976 "You may redistribute it under the terms of the GNU General Public License;\n"
977 "see the file named COPYING for details.\n"
978 "Written by the VideoLAN team at Ecole Centrale, Paris." );
981 /*****************************************************************************
982 * InitSignalHandler: system signal handler initialization
983 *****************************************************************************
984 * Set the signal handlers. SIGTERM is not intercepted, because we need at
985 * at least a method to kill the program when all other methods failed, and
986 * when we don't want to use SIGKILL.
987 *****************************************************************************/
988 static void InitSignalHandler( void )
990 /* Termination signals */
992 signal( SIGINT, FatalSignalHandler );
993 signal( SIGHUP, FatalSignalHandler );
994 signal( SIGQUIT, FatalSignalHandler );
997 signal( SIGALRM, SimpleSignalHandler );
998 signal( SIGPIPE, SimpleSignalHandler );
1003 /*****************************************************************************
1004 * SimpleSignalHandler: system signal handler
1005 *****************************************************************************
1006 * This function is called when a non fatal signal is received by the program.
1007 *****************************************************************************/
1008 static void SimpleSignalHandler( int i_signal )
1010 /* Acknowledge the signal received */
1011 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1015 /*****************************************************************************
1016 * FatalSignalHandler: system signal handler
1017 *****************************************************************************
1018 * This function is called when a fatal signal is received by the program.
1019 * It tries to end the program in a clean way.
1020 *****************************************************************************/
1021 static void FatalSignalHandler( int i_signal )
1023 /* Once a signal has been trapped, the termination sequence will be
1024 * armed and following signals will be ignored to avoid sending messages
1025 * to an interface having been destroyed */
1027 signal( SIGINT, SIG_IGN );
1028 signal( SIGHUP, SIG_IGN );
1029 signal( SIGQUIT, SIG_IGN );
1032 /* Acknowledge the signal received */
1033 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1035 /* Try to terminate everything - this is done by requesting the end of the
1036 * interface thread */
1037 p_main->p_intf->b_die = 1;
1040 /*****************************************************************************
1041 * InstructionSignalHandler: system signal handler
1042 *****************************************************************************
1043 * This function is called when a illegal instruction signal is received by
1045 * We use this function to test OS and CPU_Capabilities
1046 *****************************************************************************/
1047 static void InstructionSignalHandler( int i_signal )
1049 /* Once a signal has been trapped, the termination sequence will be
1050 * armed and following signals will be ignored to avoid sending messages
1051 * to an interface having been destroyed */
1053 /* Acknowledge the signal received */
1056 #ifdef HAVE_SIGRELSE
1057 sigrelse( i_signal );
1062 /*****************************************************************************
1063 * CPUCapabilities: list the processors MMX support and other capabilities
1064 *****************************************************************************
1065 * This function is called to list extensions the CPU may have.
1066 *****************************************************************************/
1067 static int CPUCapabilities( void )
1069 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1071 #if defined( SYS_BEOS )
1072 i_capabilities |= CPU_CAPABILITY_486
1073 | CPU_CAPABILITY_586
1074 | CPU_CAPABILITY_MMX;
1076 return( i_capabilities );
1078 #elif defined( SYS_DARWIN )
1079 struct host_basic_info hi;
1081 host_name_port_t host;
1084 char *psz_name, *psz_subname;
1086 /* Should 'never' fail? */
1087 host = mach_host_self();
1089 i_size = sizeof( hi ) / sizeof( int );
1090 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1092 if( ret != KERN_SUCCESS )
1094 fprintf( stderr, "error: couldn't get CPU information\n" );
1095 return( i_capabilities );
1098 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1099 /* FIXME: need better way to detect newer proccessors.
1100 * could do strncmp(a,b,5), but that's real ugly */
1101 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1103 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1106 return( i_capabilities );
1108 #elif defined( __i386__ )
1109 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1110 volatile boolean_t b_amd;
1112 signal( SIGILL, InstructionSignalHandler );
1114 /* test for a 486 CPU */
1115 asm volatile ( "pushfl\n\t"
1117 "movl %%eax, %%ebx\n\t"
1118 "xorl $0x200000, %%eax\n\t"
1128 if( i_eax == i_ebx )
1130 signal( SIGILL, NULL );
1131 return( i_capabilities );
1134 i_capabilities |= CPU_CAPABILITY_486;
1136 /* the CPU supports the CPUID instruction - get its level */
1137 cpuid( 0x00000000 );
1141 signal( SIGILL, NULL );
1142 return( i_capabilities );
1145 /* FIXME: this isn't correct, since some 486s have cpuid */
1146 i_capabilities |= CPU_CAPABILITY_586;
1148 /* borrowed from mpeg2dec */
1149 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1150 && ( i_edx == 0x69746e65 );
1152 /* test for the MMX flag */
1153 cpuid( 0x00000001 );
1155 if( ! (i_edx & 0x00800000) )
1157 signal( SIGILL, NULL );
1158 return( i_capabilities );
1161 i_capabilities |= CPU_CAPABILITY_MMX;
1163 if( i_edx & 0x02000000 )
1165 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1168 /* We test if OS support the SSE instructions */
1170 if( setjmp( env ) == 0 )
1172 /* Test a SSE instruction */
1173 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1176 if( i_illegal == 0 )
1178 i_capabilities |= CPU_CAPABILITY_SSE;
1182 fprintf( stderr, "warning: your OS doesn't have support for "
1183 "SSE instructions, "
1184 "some optimizations\nwill be disabled\n" );
1186 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1192 /* test for additional capabilities */
1193 cpuid( 0x80000000 );
1195 if( i_eax < 0x80000001 )
1197 signal( SIGILL, NULL );
1198 return( i_capabilities );
1201 /* list these additional capabilities */
1202 cpuid( 0x80000001 );
1205 if( i_edx & 0x80000000 )
1208 if( setjmp( env ) == 0 )
1210 /* Test a 3D Now! instruction */
1211 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1214 if( i_illegal == 0 )
1216 i_capabilities |= CPU_CAPABILITY_3DNOW;
1221 if( b_amd && ( i_edx & 0x00400000 ) )
1223 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1226 signal( SIGILL, NULL );
1227 return( i_capabilities );
1229 #elif defined( __powerpc__ )
1231 /* Test for Altivec */
1232 signal( SIGILL, InstructionSignalHandler );
1236 if( setjmp( env ) == 0 )
1238 asm volatile ("mtspr 256,%0" : : "r" (-1));
1241 if( i_illegal == 0 )
1243 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1247 signal( SIGILL, NULL );
1248 return( i_capabilities );
1251 /* default behaviour */
1252 return( i_capabilities );
1257 /*****************************************************************************
1258 * RedirectSTDOUT: redirect stdout and stderr to a file
1259 *****************************************************************************
1260 * This function will redirect stdout and stderr to a file if the user has
1262 *****************************************************************************/
1263 static int RedirectSTDOUT( void )
1265 int i_stdout_filedesc;
1266 char *psz_stdout_filename;
1268 psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1269 INTF_STDOUT_DEFAULT );
1270 if( strcmp( "", psz_stdout_filename ) != 0 )
1273 i_stdout_filedesc = open( psz_stdout_filename,
1274 O_CREAT | O_TRUNC | O_RDWR,
1275 S_IREAD | S_IWRITE );
1277 if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1279 intf_ErrMsg( "warning: unable to redirect stdout" );
1282 if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1284 intf_ErrMsg( "warning: unable to redirect stderr" );
1287 close( i_stdout_filedesc );
1291 /* No stdout redirection has been asked so open a console */
1292 if( p_main->i_warning_level )
1302 /*****************************************************************************
1303 * ShowConsole: On Win32, create an output console for debug messages
1304 *****************************************************************************
1305 * This function is usefull only on Win32.
1306 *****************************************************************************/
1307 static void ShowConsole( void )
1311 freopen( "CONOUT$", "w", stdout );
1312 freopen( "CONOUT$", "w", stderr );
1313 freopen( "CONIN$", "r", stdin );