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.122 2001/11/12 12:54:16 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 */
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"
95 #include "netutils.h" /* network_ChannelJoin */
99 /*****************************************************************************
100 * Command line options constants. If something is changed here, be sure that
101 * GetConfiguration and Usage are also changed.
102 *****************************************************************************/
104 /* Long options return values - note that values corresponding to short options
105 * chars, and in general any regular char, should be avoided */
106 #define OPT_NOAUDIO 150
107 #define OPT_STEREO 151
109 #define OPT_SPDIF 153
110 #define OPT_VOLUME 154
112 #define OPT_NOVIDEO 160
113 #define OPT_DISPLAY 161
114 #define OPT_WIDTH 162
115 #define OPT_HEIGHT 163
116 #define OPT_COLOR 164
117 #define OPT_FULLSCREEN 165
118 #define OPT_OVERLAY 166
119 #define OPT_XVADAPTOR 167
122 #define OPT_CHANNELS 170
123 #define OPT_SERVER 171
125 #define OPT_BROADCAST 173
126 #define OPT_CHANNELSERVER 174
128 #define OPT_INPUT 180
129 #define OPT_MOTION 181
132 #define OPT_DOWNMIX 184
133 #define OPT_IMDCT 185
134 #define OPT_DVDCSS 186
136 #define OPT_SYNCHRO 190
137 #define OPT_WARNING 191
138 #define OPT_VERSION 192
139 #define OPT_STDOUT 193
140 #define OPT_STATS 194
147 /* Needed for x86 CPU capabilities detection */
149 asm volatile ( "cpuid" \
158 static const struct option longopts[] =
160 /* name, has_arg, flag, val */
162 /* General/common options */
163 { "help", 0, 0, 'h' },
164 { "longhelp", 0, 0, 'H' },
165 { "version", 0, 0, OPT_VERSION },
167 /* Interface options */
168 { "intf", 1, 0, 'I' },
169 { "warning", 1, 0, OPT_WARNING },
170 { "stdout", 1, 0, OPT_STDOUT },
171 { "stats", 0, 0, OPT_STATS },
174 { "noaudio", 0, 0, OPT_NOAUDIO },
175 { "aout", 1, 0, 'A' },
176 { "stereo", 0, 0, OPT_STEREO },
177 { "mono", 0, 0, OPT_MONO },
178 { "spdif", 0, 0, OPT_SPDIF },
179 { "downmix", 1, 0, OPT_DOWNMIX },
180 { "imdct", 1, 0, OPT_IMDCT },
181 { "volume", 1, 0, OPT_VOLUME },
184 { "novideo", 0, 0, OPT_NOVIDEO },
185 { "vout", 1, 0, 'V' },
186 { "display", 1, 0, OPT_DISPLAY },
187 { "width", 1, 0, OPT_WIDTH },
188 { "height", 1, 0, OPT_HEIGHT },
189 { "grayscale", 0, 0, 'g' },
190 { "color", 0, 0, OPT_COLOR },
191 { "motion", 1, 0, OPT_MOTION },
192 { "idct", 1, 0, OPT_IDCT },
193 { "yuv", 1, 0, OPT_YUV },
194 { "fullscreen", 0, 0, OPT_FULLSCREEN },
195 { "overlay", 0, 0, OPT_OVERLAY },
196 { "xvadaptor", 1, 0, OPT_XVADAPTOR },
197 { "smp", 1, 0, OPT_SMP },
200 { "dvdtitle", 1, 0, 't' },
201 { "dvdchapter", 1, 0, 'T' },
202 { "dvdangle", 1, 0, 'u' },
203 { "dvdaudio", 1, 0, 'a' },
204 { "dvdchannel", 1, 0, 'c' },
205 { "dvdsubtitle", 1, 0, 's' },
206 { "dvdcss_method", 1, 0, OPT_DVDCSS },
209 { "input", 1, 0, OPT_INPUT },
210 { "server", 1, 0, OPT_SERVER },
211 { "port", 1, 0, OPT_PORT },
212 { "broadcast", 1, 0, OPT_BROADCAST },
213 { "channels", 0, 0, OPT_CHANNELS },
214 { "channelserver", 1, 0, OPT_CHANNELSERVER },
216 /* Synchro options */
217 { "synchro", 1, 0, OPT_SYNCHRO },
222 static const char *psz_shortopts = "hHvgt:T:u:a:s:c:I:A:V:";
224 /*****************************************************************************
225 * Global variable program_data - these are the only ones, see main.h and
227 *****************************************************************************/
229 module_bank_t *p_module_bank;
230 aout_bank_t *p_aout_bank;
231 vout_bank_t *p_vout_bank;
233 /*****************************************************************************
235 *****************************************************************************/
236 static int GetConfiguration ( int *pi_argc, char *ppsz_argv[],
238 static int GetFilenames ( int i_argc, char *ppsz_argv[] );
239 static void Usage ( int i_fashion );
240 static void Version ( void );
242 static void InitSignalHandler ( void );
243 static void SimpleSignalHandler ( int i_signal );
244 static void FatalSignalHandler ( int i_signal );
245 static void InstructionSignalHandler( int i_signal );
246 static int CPUCapabilities ( void );
248 static int RedirectSTDOUT ( void );
249 static void ShowConsole ( void );
252 static int i_illegal;
254 /*****************************************************************************
255 * main: parse command line, start interface and spawn threads
256 *****************************************************************************
257 * Steps during program execution are:
258 * -configuration parsing and messages interface initialization
259 * -opening of audio output device and some global modules
260 * -execution of interface, which exit on error or on user request
261 * -closing of audio output device and some global modules
262 * On error, the spawned threads are canceled, and the open devices closed.
263 *****************************************************************************/
264 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
266 main_t main_data; /* root of all data - see main.h */
267 module_bank_t module_bank;
268 aout_bank_t aout_bank;
269 vout_bank_t vout_bank;
271 p_main = &main_data; /* set up the global variables */
272 p_module_bank = &module_bank;
273 p_aout_bank = &aout_bank;
274 p_vout_bank = &vout_bank;
277 * Initialize threads system
282 * Test if our code is likely to run on this CPU
284 p_main->i_cpu_capabilities = CPUCapabilities();
287 * System specific initialization code
289 #if defined( SYS_BEOS ) || defined( SYS_DARWIN )
290 system_Init( &i_argc, ppsz_argv, ppsz_env );
292 #elif defined( WIN32 )
293 _fmode = _O_BINARY; /* sets the default file-translation mode on Win32 */
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 )
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: /* --dvdcss */
776 main_PutPszVariable( INPUT_DVDCSS_VAR, optarg );
780 case OPT_INPUT: /* --input */
781 main_PutPszVariable( INPUT_METHOD_VAR, optarg );
783 case OPT_CHANNELS: /* --channels */
784 main_PutIntVariable( INPUT_NETWORK_CHANNEL_VAR, 1 );
786 case OPT_CHANNELSERVER: /* --channelserver */
787 main_PutPszVariable( INPUT_CHANNEL_SERVER_VAR, optarg );
789 case OPT_SERVER: /* --server */
790 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
792 case OPT_PORT: /* --port */
793 main_PutPszVariable( INPUT_PORT_VAR, optarg );
795 case OPT_BROADCAST: /* --broadcast */
796 main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
797 main_PutPszVariable( INPUT_BCAST_ADDR_VAR, optarg );
800 /* Synchro options */
802 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
805 /* Internal error: unknown option */
810 intf_ErrMsg( "intf error: unknown option `%s'",
813 #ifdef WIN32 /* Pause the console because it's destroyed when we exit */
814 if( strcmp( "", main_GetPszVariable( INTF_STDOUT_VAR,
815 INTF_STDOUT_DEFAULT ) ) == 0 )
817 /* No stdout redirection has been asked for */
818 intf_MsgImm( "\nPress the RETURN key to continue..." );
827 if( p_main->i_warning_level < 0 )
829 p_main->i_warning_level = 0;
835 /*****************************************************************************
836 * GetFilenames: parse command line options which are not flags
837 *****************************************************************************
838 * Parse command line for input files.
839 *****************************************************************************/
840 static int GetFilenames( int i_argc, char *ppsz_argv[] )
844 /* We assume that the remaining parameters are filenames */
845 for( i_opt = optind; i_opt < i_argc; i_opt++ )
847 intf_PlaylistAdd( p_main->p_playlist, PLAYLIST_END,
848 ppsz_argv[ i_opt ] );
854 /*****************************************************************************
855 * Usage: print program usage
856 *****************************************************************************
857 * Print a short inline help. Message interface is initialized at this stage.
858 *****************************************************************************/
859 static void Usage( int i_fashion )
862 intf_MsgImm( "Usage: %s [options] [parameters] [file]...",
865 if( i_fashion == USAGE )
867 intf_MsgImm( "Try `%s --help' for more information.",
873 intf_MsgImm( "\nOptions:"
874 "\n -I, --intf <module> \tinterface method"
875 "\n -v, --verbose \tverbose mode (cumulative)"
876 "\n --stdout <filename> \tredirect console stdout"
878 "\n --noaudio \tdisable audio"
879 "\n -A, --aout <module> \taudio output method"
880 "\n --stereo, --mono \tstereo/mono audio"
881 "\n --spdif \tAC3 pass-through mode"
882 "\n --downmix <module> \tAC3 downmix method"
883 "\n --imdct <module> \tAC3 IMDCT method"
884 "\n --volume [0..1024] \tVLC output volume"
886 "\n --novideo \tdisable video"
887 "\n -V, --vout <module> \tvideo output method"
888 "\n --display <display> \tdisplay string"
889 "\n --width <w>, --height <h> \tdisplay dimensions"
890 "\n -g, --grayscale \tgrayscale output"
891 "\n --fullscreen \tfullscreen output"
892 "\n --overlay \taccelerated display"
893 "\n --xvadaptor <adaptor> \tXVideo adaptor"
894 "\n --color \tcolor output"
895 "\n --motion <module> \tmotion compensation method"
896 "\n --idct <module> \tIDCT method"
897 "\n --yuv <module> \tYUV method"
898 "\n --synchro <type> \tforce synchro algorithm"
899 "\n --smp <number of threads> \tuse several processors"
901 "\n -t, --dvdtitle <num> \tchoose DVD title"
902 "\n -T, --dvdchapter <num> \tchoose DVD chapter"
903 "\n -u, --dvdangle <num> \tchoose DVD angle"
904 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
905 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
906 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
907 "\n , --dvdcss <method> \tselect DVDCSS decryption method"
909 "\n --input \tinput method"
910 "\n --channels \tenable channels"
911 "\n --channelserver <host> \tchannel server address"
912 "\n --server <host> \tvideo server address"
913 "\n --port <port> \tvideo server port"
914 "\n --broadcast \tlisten to a broadcast"
916 "\n -h, --help \tprint help and exit"
917 "\n -H, --longhelp \tprint long help and exit"
918 "\n --version \toutput version information and exit" );
920 if( i_fashion == SHORT_HELP )
923 /* Interface parameters */
924 intf_MsgImm( "\nInterface parameters:"
925 "\n " INTF_METHOD_VAR "=<method name> \tinterface method"
926 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
927 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
928 "\n " INTF_STDOUT_VAR "=<filename> \tredirect console stdout" );
930 /* Audio parameters */
931 intf_MsgImm( "\nAudio parameters:"
932 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
933 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
934 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
935 "\n " AOUT_SPDIF_VAR "={1|0} \tAC3 pass-through mode"
936 "\n " DOWNMIX_METHOD_VAR "=<method name> \tAC3 downmix method"
937 "\n " IMDCT_METHOD_VAR "=<method name> \tAC3 IMDCT method"
938 "\n " AOUT_VOLUME_VAR "=[0..1024] \tVLC output volume"
939 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
941 /* Video parameters */
942 intf_MsgImm( "\nVideo parameters:"
943 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
944 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
945 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
946 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
947 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
948 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
949 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
950 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
951 "\n " VOUT_XVADAPTOR_VAR "=<adaptor> \tXVideo adaptor"
952 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
953 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
954 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
955 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
956 "\n " VDEC_SMP_VAR "=<number of threads> \tuse several processors" );
959 intf_MsgImm( "\nDVD parameters:"
960 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
961 "\n " INPUT_TITLE_VAR "=<title> \ttitle number"
962 "\n " INPUT_CHAPTER_VAR "=<chapter> \tchapter number"
963 "\n " INPUT_ANGLE_VAR "=<angle> \tangle number"
964 "\n " INPUT_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
965 "\n " INPUT_CHANNEL_VAR "=[0-15] \taudio channel"
966 "\n " INPUT_SUBTITLE_VAR "=[0-31] \tsubtitle channel"
967 "\n " INPUT_DVDCSS_VAR "={csskey|disc|title} \tdvdcss method" );
969 /* Input parameters */
970 intf_MsgImm( "\nInput parameters:"
971 "\n " INPUT_SERVER_VAR "=<hostname> \tvideo server"
972 "\n " INPUT_PORT_VAR "=<port> \tvideo server port"
973 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
974 "\n " INPUT_BCAST_ADDR_VAR "=<addr> \tbroadcast mode"
975 "\n " INPUT_CHANNEL_SERVER_VAR "=<hostname> \tchannel server"
976 "\n " INPUT_CHANNEL_PORT_VAR "=<port> \tchannel server port" );
980 /*****************************************************************************
981 * Version: print complete program version
982 *****************************************************************************
983 * Print complete program version and build number.
984 *****************************************************************************/
985 static void Version( void )
987 intf_MsgImm( VERSION_MESSAGE
988 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
989 "You may redistribute it under the terms of the GNU General Public License;\n"
990 "see the file named COPYING for details.\n"
991 "Written by the VideoLAN team at Ecole Centrale, Paris." );
994 /*****************************************************************************
995 * InitSignalHandler: system signal handler initialization
996 *****************************************************************************
997 * Set the signal handlers. SIGTERM is not intercepted, because we need at
998 * at least a method to kill the program when all other methods failed, and
999 * when we don't want to use SIGKILL.
1000 *****************************************************************************/
1001 static void InitSignalHandler( void )
1003 /* Termination signals */
1005 signal( SIGINT, FatalSignalHandler );
1006 signal( SIGHUP, FatalSignalHandler );
1007 signal( SIGQUIT, FatalSignalHandler );
1010 signal( SIGALRM, SimpleSignalHandler );
1011 signal( SIGPIPE, SimpleSignalHandler );
1016 /*****************************************************************************
1017 * SimpleSignalHandler: system signal handler
1018 *****************************************************************************
1019 * This function is called when a non fatal signal is received by the program.
1020 *****************************************************************************/
1021 static void SimpleSignalHandler( int i_signal )
1023 /* Acknowledge the signal received */
1024 intf_WarnMsg( 0, "intf: ignoring signal %d", i_signal );
1028 /*****************************************************************************
1029 * FatalSignalHandler: system signal handler
1030 *****************************************************************************
1031 * This function is called when a fatal signal is received by the program.
1032 * It tries to end the program in a clean way.
1033 *****************************************************************************/
1034 static void FatalSignalHandler( int i_signal )
1036 /* Once a signal has been trapped, the termination sequence will be
1037 * armed and following signals will be ignored to avoid sending messages
1038 * to an interface having been destroyed */
1040 signal( SIGINT, SIG_IGN );
1041 signal( SIGHUP, SIG_IGN );
1042 signal( SIGQUIT, SIG_IGN );
1045 /* Acknowledge the signal received */
1046 intf_ErrMsgImm( "intf error: signal %d received, exiting", i_signal );
1048 /* Try to terminate everything - this is done by requesting the end of the
1049 * interface thread */
1050 p_main->p_intf->b_die = 1;
1053 /*****************************************************************************
1054 * InstructionSignalHandler: system signal handler
1055 *****************************************************************************
1056 * This function is called when a illegal instruction signal is received by
1058 * We use this function to test OS and CPU_Capabilities
1059 *****************************************************************************/
1060 static void InstructionSignalHandler( int i_signal )
1062 /* Once a signal has been trapped, the termination sequence will be
1063 * armed and following signals will be ignored to avoid sending messages
1064 * to an interface having been destroyed */
1066 /* Acknowledge the signal received */
1069 #ifdef HAVE_SIGRELSE
1070 sigrelse( i_signal );
1075 /*****************************************************************************
1076 * CPUCapabilities: list the processors MMX support and other capabilities
1077 *****************************************************************************
1078 * This function is called to list extensions the CPU may have.
1079 *****************************************************************************/
1080 static int CPUCapabilities( void )
1082 volatile int i_capabilities = CPU_CAPABILITY_NONE;
1084 #if defined( SYS_BEOS )
1085 i_capabilities |= CPU_CAPABILITY_486
1086 | CPU_CAPABILITY_586
1087 | CPU_CAPABILITY_MMX;
1089 return( i_capabilities );
1091 #elif defined( SYS_DARWIN )
1092 struct host_basic_info hi;
1094 host_name_port_t host;
1097 char *psz_name, *psz_subname;
1099 /* Should 'never' fail? */
1100 host = mach_host_self();
1102 i_size = sizeof( hi ) / sizeof( int );
1103 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
1105 if( ret != KERN_SUCCESS )
1107 fprintf( stderr, "error: couldn't get CPU information\n" );
1108 return( i_capabilities );
1111 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
1112 /* FIXME: need better way to detect newer proccessors.
1113 * could do strncmp(a,b,5), but that's real ugly */
1114 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
1116 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1119 return( i_capabilities );
1121 #elif defined( __i386__ )
1122 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
1123 volatile boolean_t b_amd;
1125 signal( SIGILL, InstructionSignalHandler );
1127 /* test for a 486 CPU */
1128 asm volatile ( "pushfl\n\t"
1130 "movl %%eax, %%ebx\n\t"
1131 "xorl $0x200000, %%eax\n\t"
1141 if( i_eax == i_ebx )
1143 signal( SIGILL, NULL );
1144 return( i_capabilities );
1147 i_capabilities |= CPU_CAPABILITY_486;
1149 /* the CPU supports the CPUID instruction - get its level */
1150 cpuid( 0x00000000 );
1154 signal( SIGILL, NULL );
1155 return( i_capabilities );
1158 /* FIXME: this isn't correct, since some 486s have cpuid */
1159 i_capabilities |= CPU_CAPABILITY_586;
1161 /* borrowed from mpeg2dec */
1162 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
1163 && ( i_edx == 0x69746e65 );
1165 /* test for the MMX flag */
1166 cpuid( 0x00000001 );
1168 if( ! (i_edx & 0x00800000) )
1170 signal( SIGILL, NULL );
1171 return( i_capabilities );
1174 i_capabilities |= CPU_CAPABILITY_MMX;
1176 if( i_edx & 0x02000000 )
1178 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1181 /* We test if OS support the SSE instructions */
1183 if( setjmp( env ) == 0 )
1185 /* Test a SSE instruction */
1186 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
1189 if( i_illegal == 0 )
1191 i_capabilities |= CPU_CAPABILITY_SSE;
1195 fprintf( stderr, "warning: your OS doesn't have support for "
1196 "SSE instructions, "
1197 "some optimizations\nwill be disabled\n" );
1199 fprintf( stderr, "(you will need Linux kernel 2.4.x or later)\n" );
1205 /* test for additional capabilities */
1206 cpuid( 0x80000000 );
1208 if( i_eax < 0x80000001 )
1210 signal( SIGILL, NULL );
1211 return( i_capabilities );
1214 /* list these additional capabilities */
1215 cpuid( 0x80000001 );
1218 if( i_edx & 0x80000000 )
1221 if( setjmp( env ) == 0 )
1223 /* Test a 3D Now! instruction */
1224 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
1227 if( i_illegal == 0 )
1229 i_capabilities |= CPU_CAPABILITY_3DNOW;
1234 if( b_amd && ( i_edx & 0x00400000 ) )
1236 i_capabilities |= CPU_CAPABILITY_MMXEXT;
1239 signal( SIGILL, NULL );
1240 return( i_capabilities );
1242 #elif defined( __powerpc__ )
1244 /* Test for Altivec */
1245 signal( SIGILL, InstructionSignalHandler );
1249 if( setjmp( env ) == 0 )
1251 asm volatile ("mtspr 256,%0" : : "r" (-1));
1254 if( i_illegal == 0 )
1256 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
1260 signal( SIGILL, NULL );
1261 return( i_capabilities );
1264 /* default behaviour */
1265 return( i_capabilities );
1270 /*****************************************************************************
1271 * RedirectSTDOUT: redirect stdout and stderr to a file
1272 *****************************************************************************
1273 * This function will redirect stdout and stderr to a file if the user has
1275 *****************************************************************************/
1276 static int RedirectSTDOUT( void )
1278 int i_stdout_filedesc;
1279 char *psz_stdout_filename;
1281 psz_stdout_filename = main_GetPszVariable( INTF_STDOUT_VAR,
1282 INTF_STDOUT_DEFAULT );
1283 if( strcmp( "", psz_stdout_filename ) != 0 )
1286 i_stdout_filedesc = open( psz_stdout_filename,
1287 O_CREAT | O_TRUNC | O_RDWR,
1288 S_IREAD | S_IWRITE );
1290 if( dup2( i_stdout_filedesc, fileno(stdout) ) == -1 )
1292 intf_ErrMsg( "warning: unable to redirect stdout" );
1295 if( dup2( i_stdout_filedesc, fileno(stderr) ) == -1 )
1297 intf_ErrMsg( "warning: unable to redirect stderr" );
1300 close( i_stdout_filedesc );
1304 /* No stdout redirection has been asked so open a console */
1305 if( p_main->i_warning_level )
1315 /*****************************************************************************
1316 * ShowConsole: On Win32, create an output console for debug messages
1317 *****************************************************************************
1318 * This function is usefull only on Win32.
1319 *****************************************************************************/
1320 static void ShowConsole( void )
1324 freopen( "CONOUT$", "w", stdout );
1325 freopen( "CONOUT$", "w", stderr );
1326 freopen( "CONIN$", "r", stdin );