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.124 2001/11/12 22:42:56 sam 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( WIN32 )
299 _fmode = _O_BINARY; /* sets the default file-translation mode on Win32 */
301 #elif defined( SYS_LINUX )
303 /* Activate malloc checking routines to detect heap corruptions. */
304 main_PutIntVariable( "MALLOC_CHECK_", 2 );
309 * Initialize messages interface
311 p_main->p_msg = intf_MsgCreate();
312 if( !p_main->p_msg ) /* start messages interface */
314 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
319 intf_MsgImm( COPYRIGHT_MESSAGE "\n" );
324 if( GetConfiguration( &i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
331 * Redirect the standard output if required by the user, and on Win32 we
332 * also open a console to display the debug messages.
336 if( p_main->b_stats )
338 char p_capabilities[200];
339 p_capabilities[0] = '\0';
341 #define PRINT_CAPABILITY( capability, string ) \
342 if( p_main->i_cpu_capabilities & capability ) \
344 strncat( p_capabilities, string " ", \
345 sizeof(p_capabilities) - strlen(p_capabilities) ); \
346 p_capabilities[sizeof(p_capabilities) - 1] = '\0'; \
349 PRINT_CAPABILITY( CPU_CAPABILITY_486, "486" );
350 PRINT_CAPABILITY( CPU_CAPABILITY_586, "586" );
351 PRINT_CAPABILITY( CPU_CAPABILITY_PPRO, "Pentium Pro" );
352 PRINT_CAPABILITY( CPU_CAPABILITY_MMX, "MMX" );
353 PRINT_CAPABILITY( CPU_CAPABILITY_3DNOW, "3DNow!" );
354 PRINT_CAPABILITY( CPU_CAPABILITY_MMXEXT, "MMXEXT" );
355 PRINT_CAPABILITY( CPU_CAPABILITY_SSE, "SSE" );
356 PRINT_CAPABILITY( CPU_CAPABILITY_ALTIVEC, "Altivec" );
357 intf_StatMsg("info: CPU has capabilities %s", p_capabilities );
361 * Initialize playlist and get commandline files
363 p_main->p_playlist = intf_PlaylistCreate();
364 if( !p_main->p_playlist )
366 intf_ErrMsg( "playlist error: playlist initialization failed" );
370 intf_PlaylistInit( p_main->p_playlist );
373 * Get input filenames given as commandline arguments
375 GetFilenames( i_argc, ppsz_argv );
378 * Initialize module, aout and vout banks
385 * Initialize shared resources and libraries
387 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
388 INPUT_NETWORK_CHANNEL_DEFAULT ) &&
389 network_ChannelCreate() )
391 /* On error during Channels initialization, switch off channels */
392 intf_Msg( "Channels initialization failed : "
393 "Channel management is deactivated" );
394 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 0 );
398 * Try to run the interface
400 p_main->p_intf = intf_Create();
401 if( p_main->p_intf == NULL )
403 intf_ErrMsg( "intf error: interface initialization failed" );
408 * Set signal handling policy for all threads
413 * This is the main loop
415 p_main->p_intf->pf_run( p_main->p_intf );
418 * Finished, destroy the interface
420 intf_Destroy( p_main->p_intf );
423 * Go back into channel 0 which is the network
425 if( main_GetIntVariable( INPUT_NETWORK_CHANNEL_VAR,
426 INPUT_NETWORK_CHANNEL_DEFAULT ) )
428 network_ChannelJoin( COMMON_CHANNEL );
433 * Free module, aout and vout banks
442 intf_PlaylistDestroy( p_main->p_playlist );
445 * System specific cleaning code
447 #if defined( SYS_BEOS ) || defined( SYS_DARWIN ) || defined( WIN32 )
453 * Terminate messages interface and program
455 intf_Msg( "intf: program terminated" );
459 * Stop threads system
466 /*****************************************************************************
467 * main_GetIntVariable: get the int value of an environment variable
468 *****************************************************************************
469 * This function is used to read some default parameters in modules.
470 *****************************************************************************/
471 int main_GetIntVariable( char *psz_name, int i_default )
473 char * psz_env; /* environment value */
474 char * psz_end; /* end of parsing index */
475 long int i_value; /* value */
477 psz_env = getenv( psz_name );
480 i_value = strtol( psz_env, &psz_end, 0 );
481 if( (*psz_env != '\0') && (*psz_end == '\0') )
489 /*****************************************************************************
490 * main_GetPszVariable: get the string value of an environment variable
491 *****************************************************************************
492 * This function is used to read some default parameters in modules.
493 *****************************************************************************/
494 char * main_GetPszVariable( char *psz_name, char *psz_default )
498 psz_env = getenv( psz_name );
503 return( psz_default );
506 /*****************************************************************************
507 * main_PutPszVariable: set the string value of an environment variable
508 *****************************************************************************
509 * This function is used to set some default parameters in modules. The use of
510 * this function will cause some memory leak: since some systems use the pointer
511 * passed to putenv to store the environment string, it can't be freed.
512 *****************************************************************************/
513 void main_PutPszVariable( char *psz_name, char *psz_value )
517 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
518 if( psz_env == NULL )
520 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
525 sprintf( psz_env, "%s=%s", psz_name, psz_value );
526 if( putenv( psz_env ) )
528 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
533 /*****************************************************************************
534 * main_PutIntVariable: set the integer value of an environment variable
535 *****************************************************************************
536 * This function is used to set some default parameters in modules. The use of
537 * this function will cause some memory leak: since some systems use the pointer
538 * passed to putenv to store the environment string, it can't be freed.
539 *****************************************************************************/
540 void main_PutIntVariable( char *psz_name, int i_value )
542 char psz_value[ 256 ]; /* buffer for value */
544 sprintf( psz_value, "%d", i_value );
545 main_PutPszVariable( psz_name, psz_value );
548 /* following functions are local */
550 /*****************************************************************************
551 * GetConfiguration: parse command line
552 *****************************************************************************
553 * Parse command line and configuration file for configuration. If the inline
554 * help is requested, the function Usage() is called and the function returns
555 * -1 (causing main() to exit). The messages interface is initialized at this
556 * stage, but most structures are not allocated, so only environment should
558 *****************************************************************************/
559 static int GetConfiguration( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
564 /* Set default configuration and copy arguments */
565 p_main->i_argc = *pi_argc;
566 p_main->ppsz_argv = ppsz_argv;
567 p_main->ppsz_env = ppsz_env;
572 p_main->i_warning_level = 0;
575 p_main->p_channel = NULL;
577 /* Get the executable name (similar to the basename command) */
578 p_main->psz_arg0 = p_tmp = ppsz_argv[ 0 ];
583 p_main->psz_arg0 = ++p_tmp;
592 /* When vlc.app is run by double clicking in Mac OS X, the 2nd arg
593 * is the PSN - process serial number (a unique PID-ish thingie)
594 * still ok for real Darwin & when run from command line */
595 if ( (*pi_argc > 1) && (strncmp( ppsz_argv[ 1 ] , "-psn" , 4 ) == 0) )
596 /* for example -psn_0_9306113 */
598 /* GDMF!... I can't do this or else the MacOSX window server will
599 * not pick up the PSN and not register the app and we crash...
600 * hence the following kludge otherwise we'll get confused w/ argv[1]
601 * being an input file name */
603 ppsz_argv[ 1 ] = NULL;
605 *pi_argc = *pi_argc - 1;
611 /* Parse command line options */
613 while( ( i_cmd = getopt_long( *pi_argc, ppsz_argv,
614 psz_shortopts, longopts, 0 ) ) != EOF )
618 /* General/common options */
619 case 'h': /* -h, --help */
623 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
624 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
625 INTF_STDOUT_DEFAULT ) ) == 0 )
627 /* No stdout redirection has been asked for */
628 intf_MsgImm( "\nPress the RETURN key to continue..." );
634 case 'H': /* -H, --longhelp */
638 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
639 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
640 INTF_STDOUT_DEFAULT ) ) == 0 )
642 /* No stdout redirection has been asked for */
643 intf_MsgImm( "\nPress the RETURN key to continue..." );
649 case OPT_VERSION: /* --version */
653 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
654 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
655 INTF_STDOUT_DEFAULT ) ) == 0 )
657 /* No stdout redirection has been asked for */
658 intf_MsgImm( "\nPress the RETURN key to continue..." );
664 case 'v': /* -v, --verbose */
665 p_main->i_warning_level++;
668 /* Interface warning messages level */
669 case 'I': /* -I, --intf */
670 main_PutPszVariable( INTF_METHOD_VAR, optarg );
672 case OPT_WARNING: /* --warning */
673 intf_ErrMsg( "intf error: `--warning' is deprecated, use `-v'" );
674 p_main->i_warning_level = atoi(optarg);
677 case OPT_STDOUT: /* --stdout */
678 main_PutPszVariable( INTF_STDOUT_VAR, optarg );
686 case OPT_NOAUDIO: /* --noaudio */
689 case 'A': /* -A, --aout */
690 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
692 case OPT_STEREO: /* --stereo */
693 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
695 case OPT_MONO: /* --mono */
696 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
698 case OPT_SPDIF: /* --spdif */
699 main_PutIntVariable( AOUT_SPDIF_VAR, 1 );
701 case OPT_DOWNMIX: /* --downmix */
702 main_PutPszVariable( DOWNMIX_METHOD_VAR, optarg );
704 case OPT_IMDCT: /* --imdct */
705 main_PutPszVariable( IMDCT_METHOD_VAR, optarg );
707 case OPT_VOLUME: /* --volume */
708 main_PutIntVariable( AOUT_VOLUME_VAR, atoi(optarg) );
712 case OPT_NOVIDEO: /* --novideo */
715 case 'V': /* -V, --vout */
716 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
718 case OPT_DISPLAY: /* --display */
719 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
721 case OPT_WIDTH: /* --width */
722 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
724 case OPT_HEIGHT: /* --height */
725 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
727 case 'g': /* -g, --grayscale */
728 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
730 case OPT_COLOR: /* --color */
731 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
733 case OPT_FULLSCREEN: /* --fullscreen */
734 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
736 case OPT_OVERLAY: /* --overlay */
737 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
739 case OPT_XVADAPTOR: /* --xvadaptor */
740 main_PutIntVariable( VOUT_XVADAPTOR_VAR, atoi(optarg) );
742 case OPT_MOTION: /* --motion */
743 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
745 case OPT_IDCT: /* --idct */
746 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
748 case OPT_YUV: /* --yuv */
749 main_PutPszVariable( YUV_METHOD_VAR, optarg );
751 case OPT_SMP: /* --smp */
752 main_PutIntVariable( VDEC_SMP_VAR, atoi(optarg) );
756 case 't': /* --dvdtitle */
757 main_PutIntVariable( INPUT_TITLE_VAR, atoi(optarg) );
759 case 'T': /* --dvdchapter */
760 main_PutIntVariable( INPUT_CHAPTER_VAR, atoi(optarg) );
762 case 'u': /* --dvdangle */
763 main_PutIntVariable( INPUT_ANGLE_VAR, atoi(optarg) );
765 case 'a': /* --dvdaudio */
766 if ( ! strcmp(optarg, "ac3") )
767 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_AC3 );
768 else if ( ! strcmp(optarg, "lpcm") )
769 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_LPCM );
770 else if ( ! strcmp(optarg, "mpeg") )
771 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_MPEG );
773 main_PutIntVariable( INPUT_AUDIO_VAR, REQUESTED_NOAUDIO );
775 case 'c': /* --dvdchannel */
776 main_PutIntVariable( INPUT_CHANNEL_VAR, atoi(optarg) );
778 case 's': /* --dvdsubtitle */
779 main_PutIntVariable( INPUT_SUBTITLE_VAR, atoi(optarg) );
781 case OPT_DVDCSS_METHOD: /* --dvdcss-method */
782 main_PutPszVariable( "DVDCSS_METHOD", optarg );
784 case OPT_DVDCSS_VERBOSE: /* --dvdcss-verbose */
785 main_PutPszVariable( "DVDCSS_VERBOSE", optarg );
789 case OPT_INPUT: /* --input */
790 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
792 case OPT_CHANNELS: /* --channels */
793 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
795 case OPT_CHANNELSERVER: /* --channelserver */
796 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
798 case OPT_SERVER: /* --server */
799 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
801 case OPT_PORT: /* --port */
802 main_PutPszVariable( INPUT_PORT_VAR, optarg );
804 case OPT_BROADCAST: /* --broadcast */
805 main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
806 main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
809 /* Synchro options */
811 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
814 /* Internal error: unknown option */
819 intf_ErrMsg( "intf error: unknown option `%s'",
822 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
823 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
824 INTF_STDOUT_DEFAULT ) ) == 0 )
826 /* No stdout redirection has been asked for */
827 intf_MsgImm( "\nPress the RETURN key to continue..." );
836 if( p_main->i_warning_level < 0 )
838 p_main->i_warning_level = 0;
844 /*****************************************************************************
845 * GetFilenames: parse command line options which are not flags
846 *****************************************************************************
847 * Parse command line for input files.
848 *****************************************************************************/
849 static int GetFilenames( int i_argc, char *ppsz_argv[] )
853 /* We assume that the remaining parameters are filenames */
854 for( i_opt = optind; i_opt < i_argc; i_opt++ )
856 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
857 ppsz_argv[ i_opt ] );
863 /*****************************************************************************
864 * Usage: print program usage
865 *****************************************************************************
866 * Print a short inline help. Message interface is initialized at this stage.
867 *****************************************************************************/
868 static void Usage( int i_fashion )
871 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
874 if( i_fashion == USAGE )
876 intf_MsgImm( "Try `%s --help' for more information.",
882 intf_MsgImm( "\nOptions:"
883 "\n -I, --intf <module> \tinterface method"
884 "\n -v, --verbose \tverbose mode (cumulative)"
885 "\n --stdout <filename> \tredirect console stdout"
887 "\n --noaudio \tdisable audio"
888 "\n -A, --aout <module> \taudio output method"
889 "\n --stereo, --mono \tstereo/mono audio"
890 "\n --spdif \tAC3 pass-through mode"
891 "\n --downmix <module> \tAC3 downmix method"
892 "\n --imdct <module> \tAC3 IMDCT method"
893 "\n --volume [0..1024] \tVLC output volume"
895 "\n --novideo \tdisable video"
896 "\n -V, --vout <module> \tvideo output method"
897 "\n --display <display> \tdisplay string"
898 "\n --width <w>, --height <h> \tdisplay dimensions"
899 "\n -g, --grayscale \tgrayscale output"
900 "\n --fullscreen \tfullscreen output"
901 "\n --overlay \taccelerated display"
902 "\n --xvadaptor <adaptor> \tXVideo adaptor"
903 "\n --color \tcolor output"
904 "\n --motion <module> \tmotion compensation method"
905 "\n --idct <module> \tIDCT method"
906 "\n --yuv <module> \tYUV method"
907 "\n --synchro <type> \tforce synchro algorithm"
908 "\n --smp <number of threads> \tuse several processors"
910 "\n -t, --dvdtitle <num> \tchoose DVD title"
911 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
912 "\n -u, --dvdangle <num> \tchoose DVD angle"
913 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
914 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
915 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
916 "\n --dvdcss-method <method> \tselect dvdcss decryption method"
917 "\n --dvdcss-verbose <level> \tselect dvdcss verbose level"
919 "\n --input \tinput method"
920 "\n --channels \tenable channels"
921 "\n --channelserver <host> \tchannel server address"
922 "\n --server <host> \tvideo server address"
923 "\n --port <port> \tvideo server port"
924 "\n --broadcast \tlisten to a broadcast"
926 "\n -h, --help \tprint help and exit"
927 "\n -H, --longhelp \tprint long help and exit"
928 "\n --version \toutput version information and exit" );
930 if( i_fashion == SHORT_HELP )
933 /* Interface parameters */
934 intf_MsgImm( "\nInterface parameters:"
935 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
936 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
937 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
938 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout" );
940 /* Audio parameters */
941 intf_MsgImm( "\nAudio parameters:"
942 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
943 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
944 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
945 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
946 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
947 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
948 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
949 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
951 /* Video parameters */
952 intf_MsgImm( "\nVideo parameters:"
953 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
954 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
955 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
956 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
957 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
958 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
959 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
960 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
961 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
962 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
963 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
964 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
965 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
966 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
969 intf_MsgImm( "\nDVD parameters:"
970 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
971 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
972 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
973 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
974 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
975 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
976 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
978 /* Input parameters */
979 intf_MsgImm( "\nInput parameters:"
980 "\n " INPUT_SERVER_VAR "=<hostname> \tvideo server"
981 "\n " INPUT_PORT_VAR "=<port> \tvideo server port"
982 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
983 "\n " INPUT_BCAST_ADDR_VAR "=<addr> \tbroadcast mode"
984 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
985 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
989 /*****************************************************************************
990 * Version: print complete program version
991 *****************************************************************************
992 * Print complete program version and build number.
993 *****************************************************************************/
994 static void Version( void )
996 intf_MsgImm( VERSION_MESSAGE
997 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
998 "You may redistribute it under the terms of the GNU General Public License;\n"
999 "see the file named COPYING for details.\n"
1000 "Written by the VideoLAN team at Ecole Centrale, Paris." );
1003 /*****************************************************************************
1004 * InitSignalHandler: system signal handler initialization
1005 *****************************************************************************
1006 * Set the signal handlers. SIGTERM is not intercepted, because we need at
1007 * at least a method to kill the program when all other methods failed, and
1008 * when we don't want to use SIGKILL.
1009 *****************************************************************************/
1010 static void InitSignalHandler( void )
1012 /* Termination signals */
1014 signal( SIGINT, FatalSignalHandler );
1015 signal( SIGHUP, FatalSignalHandler );
1016 signal( SIGQUIT, FatalSignalHandler );
1019 signal( SIGALRM, SimpleSignalHandler );
1020 signal( SIGPIPE, SimpleSignalHandler );
1025 /*****************************************************************************
1026 * SimpleSignalHandler: system signal handler
1027 *****************************************************************************
1028 * This function is called when a non fatal signal is received by the program.
1029 *****************************************************************************/
1030 static void SimpleSignalHandler( int i_signal )
1032 /* Acknowledge the signal received */
1033 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1037 /*****************************************************************************
1038 * FatalSignalHandler: system signal handler
1039 *****************************************************************************
1040 * This function is called when a fatal signal is received by the program.
1041 * It tries to end the program in a clean way.
1042 *****************************************************************************/
1043 static void FatalSignalHandler( int i_signal )
1045 /* Once a signal has been trapped, the termination sequence will be
1046 * armed and following signals will be ignored to avoid sending messages
1047 * to an interface having been destroyed */
1049 signal( SIGINT, SIG_IGN );
1050 signal( SIGHUP, SIG_IGN );
1051 signal( SIGQUIT, SIG_IGN );
1054 /* Acknowledge the signal received */
1055 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1057 /* Try to terminate everything - this is done by requesting the end of the
1058 * interface thread */
1059 p_main->p_intf->b_die = 1;
1062 /*****************************************************************************
1063 * InstructionSignalHandler: system signal handler
1064 *****************************************************************************
1065 * This function is called when a illegal instruction signal is received by
1067 * We use this function to test OS and CPU_Capabilities
1068 *****************************************************************************/
1069 static void InstructionSignalHandler( int i_signal )
1071 /* Once a signal has been trapped, the termination sequence will be
1072 * armed and following signals will be ignored to avoid sending messages
1073 * to an interface having been destroyed */
1075 /* Acknowledge the signal received */
1078 #ifdef HAVE_SIGRELSE
1079 sigrelse( i_signal );
1084 /*****************************************************************************
1085 * CPUCapabilities: list the processors MMX support and other capabilities
1086 *****************************************************************************
1087 * This function is called to list extensions the CPU may have.
1088 *****************************************************************************/
1089 static int CPUCapabilities( void )
1091 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1093 #if defined( SYS_BEOS )
1094 i_capabilities |= CPU_CAPABILITY_486
1095 | CPU_CAPABILITY_586
1096 | CPU_CAPABILITY_MMX;
1098 return( i_capabilities );
1100 #elif defined( SYS_DARWIN )
1101 struct host_basic_info hi;
1103 host_name_port_t host;
1106 char *psz_name, *psz_subname;
1108 /* Should 'never' fail? */
1109 host = mach_host_self();
1111 i_size = sizeof( hi ) / sizeof( int );
1112 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1114 if( ret != KERN_SUCCESS )
1116 fprintf( stderr, "error: couldn't get CPU information\n" );
1117 return( i_capabilities );
1120 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1121 /* FIXME: need better way to detect newer proccessors.
1122 * could do strncmp(a,b,5), but that's real ugly */
1123 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1125 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1128 return( i_capabilities );
1130 #elif defined( __i386__ )
1131 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1132 volatile boolean_t b_amd;
1134 signal( SIGILL, InstructionSignalHandler );
1136 /* test for a 486 CPU */
1137 asm volatile ( "pushfl\n\t"
1139 "movl %%eax, %%ebx\n\t"
1140 "xorl $0x200000, %%eax\n\t"
1150 if( i_eax == i_ebx )
1152 signal( SIGILL, NULL );
1153 return( i_capabilities );
1156 i_capabilities |= CPU_CAPABILITY_486;
1158 /* the CPU supports the CPUID instruction - get its level */
1159 cpuid( 0x00000000 );
1163 signal( SIGILL, NULL );
1164 return( i_capabilities );
1167 /* FIXME: this isn't correct, since some 486s have cpuid */
1168 i_capabilities |= CPU_CAPABILITY_586;
1170 /* borrowed from mpeg2dec */
1171 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1172 && ( i_edx == 0x69746e65 );
1174 /* test for the MMX flag */
1175 cpuid( 0x00000001 );
1177 if( ! (i_edx & 0x00800000) )
1179 signal( SIGILL, NULL );
1180 return( i_capabilities );
1183 i_capabilities |= CPU_CAPABILITY_MMX;
1185 if( i_edx & 0x02000000 )
1187 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1190 /* We test if OS support the SSE instructions */
1192 if( setjmp( env ) == 0 )
1194 /* Test a SSE instruction */
1195 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1198 if( i_illegal == 0 )
1200 i_capabilities |= CPU_CAPABILITY_SSE;
1204 fprintf( stderr, "warning: your OS doesn't have support for "
1205 "SSE instructions, "
1206 "some optimizations\nwill be disabled\n" );
1208 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1214 /* test for additional capabilities */
1215 cpuid( 0x80000000 );
1217 if( i_eax < 0x80000001 )
1219 signal( SIGILL, NULL );
1220 return( i_capabilities );
1223 /* list these additional capabilities */
1224 cpuid( 0x80000001 );
1227 if( i_edx & 0x80000000 )
1230 if( setjmp( env ) == 0 )
1232 /* Test a 3D Now! instruction */
1233 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1236 if( i_illegal == 0 )
1238 i_capabilities |= CPU_CAPABILITY_3DNOW;
1243 if( b_amd && ( i_edx & 0x00400000 ) )
1245 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1248 signal( SIGILL, NULL );
1249 return( i_capabilities );
1251 #elif defined( __powerpc__ )
1253 /* Test for Altivec */
1254 signal( SIGILL, InstructionSignalHandler );
1258 if( setjmp( env ) == 0 )
1260 asm volatile ("mtspr 256,%0" : : "r" (-1));
1263 if( i_illegal == 0 )
1265 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1269 signal( SIGILL, NULL );
1270 return( i_capabilities );
1273 /* default behaviour */
1274 return( i_capabilities );
1279 /*****************************************************************************
1280 * RedirectSTDOUT: redirect stdout and stderr to a file
1281 *****************************************************************************
1282 * This function will redirect stdout and stderr to a file if the user has
1284 *****************************************************************************/
1285 static int RedirectSTDOUT( void )
1287 int i_stdout_filedesc;
1288 char *psz_stdout_filename;
1290 psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1291 INTF_STDOUT_DEFAULT );
1292 if( strcmp( "", psz_stdout_filename ) != 0 )
1295 i_stdout_filedesc = open( psz_stdout_filename,
1296 O_CREAT | O_TRUNC | O_RDWR,
1297 S_IREAD | S_IWRITE );
1299 if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1301 intf_ErrMsg( "warning: unable to redirect stdout" );
1304 if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1306 intf_ErrMsg( "warning: unable to redirect stderr" );
1309 close( i_stdout_filedesc );
1313 /* No stdout redirection has been asked so open a console */
1314 if( p_main->i_warning_level )
1324 /*****************************************************************************
1325 * ShowConsole: On Win32, create an output console for debug messages
1326 *****************************************************************************
1327 * This function is usefull only on Win32.
1328 *****************************************************************************/
1329 static void ShowConsole( void )
1333 freopen( "CONOUT$", "w", stdout );
1334 freopen( "CONOUT$", "w", stderr );
1335 freopen( "CONIN$", "r", stdin );