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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
31 #include <stdio.h> /* sprintf() */
34 #include <getopt.h> /* getopt() */
37 #include <errno.h> /* ENOMEM */
38 #include <stdlib.h> /* getenv(), strtol(), */
39 #include <string.h> /* strerror() */
46 #include "tests.h" /* TestCPU() */
50 #include "stream_control.h"
51 #include "input_ext-intf.h"
54 #include "interface.h"
56 #include "audio_output.h"
59 #include "beos_specific.h"
64 /*****************************************************************************
65 * Command line options constants. If something is changed here, be sure that
66 * GetConfiguration and Usage are also changed.
67 *****************************************************************************/
69 /* Long options return values - note that values corresponding to short options
70 * chars, and in general any regular char, should be avoided */
71 #define OPT_NOAUDIO 150
72 #define OPT_STEREO 151
75 #define OPT_NOVIDEO 160
76 #define OPT_DISPLAY 161
78 #define OPT_HEIGHT 163
80 #define OPT_FULLSCREEN 165
81 #define OPT_OVERLAY 166
84 #define OPT_SERVER 171
86 #define OPT_BROADCAST 173
91 #define OPT_MOTION 182
95 #define OPT_SYNCHRO 190
96 #define OPT_WARNING 191
105 static const struct option longopts[] =
107 /* name, has_arg, flag, val */
109 /* General/common options */
110 { "help", 0, 0, 'h' },
111 { "longhelp", 0, 0, 'H' },
112 { "version", 0, 0, 'v' },
115 { "noaudio", 0, 0, OPT_NOAUDIO },
116 { "aout", 1, 0, OPT_AOUT },
117 { "stereo", 0, 0, OPT_STEREO },
118 { "mono", 0, 0, OPT_MONO },
121 { "novideo", 0, 0, OPT_NOVIDEO },
122 { "vout", 1, 0, OPT_VOUT },
123 { "display", 1, 0, OPT_DISPLAY },
124 { "width", 1, 0, OPT_WIDTH },
125 { "height", 1, 0, OPT_HEIGHT },
126 { "grayscale", 0, 0, 'g' },
127 { "color", 0, 0, OPT_COLOR },
128 { "motion", 1, 0, OPT_MOTION },
129 { "idct", 1, 0, OPT_IDCT },
130 { "yuv", 1, 0, OPT_YUV },
131 { "fullscreen", 0, 0, OPT_FULLSCREEN },
132 { "overlay", 0, 0, OPT_OVERLAY },
135 { "dvdaudio", 1, 0, 'a' },
136 { "dvdchannel", 1, 0, 'c' },
137 { "dvdsubtitle", 1, 0, 's' },
140 { "vlans", 0, 0, OPT_VLANS },
141 { "server", 1, 0, OPT_SERVER },
142 { "port", 1, 0, OPT_PORT },
143 { "broadcast", 0, 0, OPT_BROADCAST },
144 { "dvd", 0, 0, OPT_DVD },
146 /* Synchro options */
147 { "synchro", 1, 0, OPT_SYNCHRO },
149 /* Interface messages */
150 { "warning", 1, 0, OPT_WARNING },
155 static const char *psz_shortopts = "hHvga:s:c:";
159 /*****************************************************************************
160 * Global variable program_data - this is the one and only, see main.h
161 *****************************************************************************/
164 /*****************************************************************************
166 *****************************************************************************/
167 static void SetDefaultConfiguration ( void );
168 static int GetConfiguration ( int i_argc, char *ppsz_argv[],
170 static void Usage ( int i_fashion );
171 static void Version ( void );
173 static void InitSignalHandler ( void );
174 static void SimpleSignalHandler ( int i_signal );
175 static void FatalSignalHandler ( int i_signal );
177 /*****************************************************************************
178 * main: parse command line, start interface and spawn threads
179 *****************************************************************************
180 * Steps during program execution are:
181 * -configuration parsing and messages interface initialization
182 * -opening of audio output device and some global modules
183 * -execution of interface, which exit on error or on user request
184 * -closing of audio output device and some global modules
185 * On error, the spawned threads are canceled, and the open devices closed.
186 *****************************************************************************/
187 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
189 main_t main_data; /* root of all data - see main.h */
191 p_main = &main_data; /* set up the global variable */
194 * System specific initialization code
200 p_main->i_cpu_capabilities = CPUCapabilities();
203 * Test if our code is likely to run on this CPU
205 #if defined( __pentium__ ) || defined( __pentiumpro__ )
206 if( ! TestCPU( CPU_CAPABILITY_586 ) )
208 fprintf( stderr, "Sorry, this program needs a Pentium CPU.\n"
209 "Please try a version without Pentium support.\n" );
215 if( ! TestCPU( CPU_CAPABILITY_MMX ) )
217 fprintf( stderr, "Sorry, this program needs MMX extensions.\n"
218 "Please try a version without MMX support.\n" );
224 * Initialize messages interface
226 p_main->p_msg = intf_MsgCreate();
227 if( !p_main->p_msg ) /* start messages interface */
229 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
237 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
244 * Initialize playlist and get commandline files
246 p_main->p_playlist = playlist_Create( );
247 if( !p_main->p_playlist )
249 intf_ErrMsg( "playlist error: playlist initialization failed" );
253 playlist_Init( p_main->p_playlist, optind );
256 * Initialize plugin bank
258 p_main->p_bank = bank_Create( );
259 if( !p_main->p_bank )
261 intf_ErrMsg( "plugin error: plugin bank initialization failed" );
262 playlist_Destroy( p_main->p_playlist );
266 bank_Init( p_main->p_bank );
269 * Initialize module bank
271 p_main->p_module_bank = module_CreateBank( );
272 if( !p_main->p_module_bank )
274 intf_ErrMsg( "module error: module bank initialization failed" );
275 bank_Destroy( p_main->p_bank );
276 playlist_Destroy( p_main->p_playlist );
280 module_InitBank( p_main->p_module_bank );
283 * Initialize shared resources and libraries
285 /* FIXME: no VLANs */
287 if( p_main->b_vlans && input_VlanCreate() )
289 /* On error during vlans initialization, switch off vlans */
290 intf_Msg( "Virtual LANs initialization failed : "
291 "vlans management is deactivated" );
299 p_main->p_intf = intf_Create();
301 if( p_main->p_intf != NULL )
304 * Set signal handling policy for all threads
309 * Open audio device and start aout thread
311 if( p_main->b_audio )
313 p_main->p_aout = aout_CreateThread( NULL );
314 if( p_main->p_aout == NULL )
316 /* On error during audio initialization, switch off audio */
317 intf_ErrMsg( "aout error: audio initialization failed,"
318 " audio is deactivated" );
324 * This is the main loop
326 intf_Run( p_main->p_intf );
328 intf_Destroy( p_main->p_intf );
333 if( p_main->b_audio )
335 aout_DestroyThread( p_main->p_aout, NULL );
340 * Free shared resources and libraries
344 if( p_main->b_vlans )
353 module_DestroyBank( p_main->p_module_bank );
358 bank_Destroy( p_main->p_bank );
363 playlist_Destroy( p_main->p_playlist );
367 * System specific cleaning code
373 * Terminate messages interface and program
375 intf_Msg( "intf: program terminated." );
381 /*****************************************************************************
382 * main_GetIntVariable: get the int value of an environment variable
383 *****************************************************************************
384 * This function is used to read some default parameters in modules.
385 *****************************************************************************/
386 int main_GetIntVariable( char *psz_name, int i_default )
388 char * psz_env; /* environment value */
389 char * psz_end; /* end of parsing index */
390 long int i_value; /* value */
392 psz_env = getenv( psz_name );
395 i_value = strtol( psz_env, &psz_end, 0 );
396 if( (*psz_env != '\0') && (*psz_end == '\0') )
404 /*****************************************************************************
405 * main_GetPszVariable: get the string value of an environment variable
406 *****************************************************************************
407 * This function is used to read some default parameters in modules.
408 *****************************************************************************/
409 char * main_GetPszVariable( char *psz_name, char *psz_default )
413 psz_env = getenv( psz_name );
418 return( psz_default );
421 /*****************************************************************************
422 * main_PutPszVariable: set the string value of an environment variable
423 *****************************************************************************
424 * This function is used to set some default parameters in modules. The use of
425 * this function will cause some memory leak: since some systems use the pointer
426 * passed to putenv to store the environment string, it can't be freed.
427 *****************************************************************************/
428 void main_PutPszVariable( char *psz_name, char *psz_value )
432 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
433 if( psz_env == NULL )
435 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
440 sprintf( psz_env, "%s=%s", psz_name, psz_value );
441 if( putenv( psz_env ) )
443 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
448 /*****************************************************************************
449 * main_PutIntVariable: set the integer value of an environment variable
450 *****************************************************************************
451 * This function is used to set some default parameters in modules. The use of
452 * this function will cause some memory leak: since some systems use the pointer
453 * passed to putenv to store the environment string, it can't be freed.
454 *****************************************************************************/
455 void main_PutIntVariable( char *psz_name, int i_value )
457 char psz_value[ 256 ]; /* buffer for value */
459 sprintf( psz_value, "%d", i_value );
460 main_PutPszVariable( psz_name, psz_value );
463 /* following functions are local */
465 /*****************************************************************************
466 * SetDefaultConfiguration: set default options
467 *****************************************************************************
468 * This function is called by GetConfiguration before command line is parsed.
469 * It sets all the default values required later by the program. At this stage,
470 * most structure are not yet allocated, so initialization must be done using
472 *****************************************************************************/
473 static void SetDefaultConfiguration( void )
476 * All features are activated by default except vlans
484 /*****************************************************************************
485 * GetConfiguration: parse command line
486 *****************************************************************************
487 * Parse command line and configuration file for configuration. If the inline
488 * help is requested, the function Usage() is called and the function returns
489 * -1 (causing main() to exit). The messages interface is initialized at this
490 * stage, but most structures are not allocated, so only environment should
492 *****************************************************************************/
493 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
498 /* Set default configuration and copy arguments */
499 p_main->i_argc = i_argc;
500 p_main->ppsz_argv = ppsz_argv;
501 p_main->ppsz_env = ppsz_env;
502 SetDefaultConfiguration();
504 intf_MsgImm( COPYRIGHT_MESSAGE );
506 /* Get the executable name (similar to the basename command) */
507 p_main->psz_arg0 = p_pointer = ppsz_argv[ 0 ];
510 if( *p_pointer == '/' )
512 p_main->psz_arg0 = ++p_pointer;
520 /* Parse command line options */
523 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
527 /* General/common options */
528 case 'h': /* -h, --help */
532 case 'H': /* -H, --longhelp */
536 case 'v': /* -v, --version */
542 case OPT_NOAUDIO: /* --noaudio */
545 case OPT_AOUT: /* --aout */
546 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
548 case OPT_STEREO: /* --stereo */
549 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
551 case OPT_MONO: /* --mono */
552 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
556 case OPT_NOVIDEO: /* --novideo */
559 case OPT_VOUT: /* --vout */
560 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
562 case OPT_DISPLAY: /* --display */
563 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
565 case OPT_WIDTH: /* --width */
566 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
568 case OPT_HEIGHT: /* --height */
569 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
571 case 'g': /* -g, --grayscale */
572 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
574 case OPT_COLOR: /* --color */
575 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
577 case OPT_FULLSCREEN: /* --fullscreen */
578 main_PutIntVariable( VOUT_FULLSCREEN_VAR, 1 );
580 case OPT_OVERLAY: /* --overlay */
581 main_PutIntVariable( VOUT_OVERLAY_VAR, 1 );
583 case OPT_MOTION: /* --motion */
584 main_PutPszVariable( MOTION_METHOD_VAR, optarg );
586 case OPT_IDCT: /* --idct */
587 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
589 case OPT_YUV: /* --yuv */
590 main_PutPszVariable( YUV_METHOD_VAR, optarg );
595 if ( ! strcmp(optarg, "ac3") )
596 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
597 else if ( ! strcmp(optarg, "lpcm") )
598 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
599 else if ( ! strcmp(optarg, "mpeg") )
600 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
602 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
605 main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
608 main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
612 case OPT_VLANS: /* --vlans */
615 case OPT_SERVER: /* --server */
616 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
618 case OPT_PORT: /* --port */
619 main_PutPszVariable( INPUT_PORT_VAR, optarg );
621 case OPT_BROADCAST: /* --broadcast */
622 main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
624 case OPT_DVD: /* --dvd */
628 /* Synchro options */
630 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
633 /* Interface warning messages level */
634 case OPT_WARNING: /* --warning */
635 main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
638 /* Internal error: unknown option */
641 intf_ErrMsg( "intf error: unknown option `%s'", ppsz_argv[optind - 1] );
649 /* Parse command line parameters - no check is made for these options */
650 for( i_opt = optind; i_opt < i_argc; i_opt++ )
652 putenv( ppsz_argv[ i_opt ] );
657 /*****************************************************************************
658 * Usage: print program usage
659 *****************************************************************************
660 * Print a short inline help. Message interface is initialized at this stage.
661 *****************************************************************************/
662 static void Usage( int i_fashion )
665 intf_Msg( "Usage: %s [options] [parameters] [file]...",
668 if( i_fashion == USAGE )
670 intf_Msg( "Try `%s --help' for more information.",
676 intf_Msg( "\nOptions:"
677 "\n --noaudio \tdisable audio"
678 "\n --aout <module> \taudio output method"
679 "\n --stereo, --mono \tstereo/mono audio"
681 "\n --novideo \tdisable video"
682 "\n --vout <module> \tvideo output method"
683 "\n --display <display> \tdisplay string"
684 "\n --width <w>, --height <h> \tdisplay dimensions"
685 "\n -g, --grayscale \tgrayscale output"
686 "\n --color \tcolor output"
687 "\n --motion <module> \tmotion compensation method"
688 "\n --idct <module> \tIDCT method"
689 "\n --yuv <module> \tYUV method"
690 "\n --synchro <type> \tforce synchro algorithm"
692 "\n --dvd \tDVD mode"
693 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
694 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
695 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
697 "\n --vlans \tenable vlans"
698 "\n --server <host> \tvideo server address"
699 "\n --port <port> \tvideo server port"
700 "\n --broadcast \tlisten to a broadcast"
702 "\n --warning <level> \tdisplay warning messages"
704 "\n -h, --help \tprint help and exit"
705 "\n -H, --longhelp \tprint long help and exit"
706 "\n -v, --version \toutput version information and exit" );
708 if( i_fashion == SHORT_HELP )
711 /* Interface parameters */
712 intf_Msg( "\nInterface parameters:\n"
713 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
714 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
715 "\n " INTF_WARNING_VAR "=<level> \twarning level" );
717 /* Audio parameters */
718 intf_Msg( "\nAudio parameters:"
719 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
720 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
721 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
722 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
724 /* Video parameters */
725 intf_Msg( "\nVideo parameters:"
726 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
727 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
728 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
729 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
730 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
731 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
732 "\n " VOUT_FULLSCREEN_VAR "={1|0} \tfullscreen"
733 "\n " VOUT_OVERLAY_VAR "={1|0} \toverlay"
734 "\n " MOTION_METHOD_VAR "=<method name> \tmotion compensation method"
735 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
736 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
737 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm"
741 intf_Msg( "\nDVD parameters:"
742 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
743 "\n " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
744 "\n " INPUT_DVD_CHANNEL_VAR "=[0-15] \taudio channel"
745 "\n " INPUT_DVD_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
747 /* Input parameters */
748 intf_Msg( "\nInput parameters:\n"
749 "\n " INPUT_SERVER_VAR "=<hostname> \tvideo server"
750 "\n " INPUT_PORT_VAR "=<port> \tvideo server port"
751 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
752 "\n " INPUT_BROADCAST_VAR "={1|0} \tbroadcast mode"
753 "\n " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server"
754 "\n " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port"
759 /*****************************************************************************
760 * Version: print complete program version
761 *****************************************************************************
762 * Print complete program version and build number.
763 *****************************************************************************/
764 static void Version( void )
766 intf_Msg( VERSION_MESSAGE
767 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
768 "You may redistribute it under the terms of the GNU General Public License;\n"
769 "see the file named COPYING for details.\n"
770 "Written by the VideoLAN team at Ecole Centrale, Paris." );
774 /*****************************************************************************
775 * InitSignalHandler: system signal handler initialization
776 *****************************************************************************
777 * Set the signal handlers. SIGTERM is not intercepted, because we need at
778 * at least a method to kill the program when all other methods failed, and
779 * when we don't want to use SIGKILL.
780 *****************************************************************************/
781 static void InitSignalHandler( void )
783 /* Termination signals */
784 signal( SIGHUP, FatalSignalHandler );
785 signal( SIGINT, FatalSignalHandler );
786 signal( SIGQUIT, FatalSignalHandler );
789 signal( SIGALRM, SimpleSignalHandler );
790 signal( SIGPIPE, SimpleSignalHandler );
794 /*****************************************************************************
795 * SimpleSignalHandler: system signal handler
796 *****************************************************************************
797 * This function is called when a non fatal signal is received by the program.
798 *****************************************************************************/
799 static void SimpleSignalHandler( int i_signal )
801 /* Acknowledge the signal received */
802 intf_WarnMsg(0, "intf: ignoring signal %d", i_signal );
806 /*****************************************************************************
807 * FatalSignalHandler: system signal handler
808 *****************************************************************************
809 * This function is called when a fatal signal is received by the program.
810 * It tries to end the program in a clean way.
811 *****************************************************************************/
812 static void FatalSignalHandler( int i_signal )
814 /* Once a signal has been trapped, the termination sequence will be armed and
815 * following signals will be ignored to avoid sending messages to an interface
816 * having been destroyed */
817 signal( SIGHUP, SIG_IGN );
818 signal( SIGINT, SIG_IGN );
819 signal( SIGQUIT, SIG_IGN );
821 /* Acknowledge the signal received */
822 intf_ErrMsgImm("intf error: signal %d received, exiting", i_signal );
824 /* Try to terminate everything - this is done by requesting the end of the
825 * interface thread */
826 p_main->p_intf->b_die = 1;