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" /* TestMMX() */
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
73 #define OPT_STEREO 152
76 #define OPT_NOVIDEO 160
78 #define OPT_DISPLAY 162
80 #define OPT_HEIGHT 164
85 #define OPT_SERVER 171
87 #define OPT_BROADCAST 173
89 #define OPT_SYNCHRO 180
91 #define OPT_WARNING 190
100 static const struct option longopts[] =
102 /* name, has_arg, flag, val */
104 /* General/common options */
105 { "help", 0, 0, 'h' },
106 { "longhelp", 0, 0, 'H' },
107 { "version", 0, 0, 'v' },
110 { "noaudio", 0, 0, OPT_NOAUDIO },
111 { "aout", 1, 0, OPT_AOUT },
112 { "stereo", 0, 0, OPT_STEREO },
113 { "mono", 0, 0, OPT_MONO },
116 { "novideo", 0, 0, OPT_NOVIDEO },
117 { "vout", 1, 0, OPT_VOUT },
118 { "display", 1, 0, OPT_DISPLAY },
119 { "width", 1, 0, OPT_WIDTH },
120 { "height", 1, 0, OPT_HEIGHT },
121 { "grayscale", 0, 0, 'g' },
122 { "color", 0, 0, OPT_COLOR },
123 { "yuv", 1, 0, OPT_YUV },
126 { "dvdaudio", 1, 0, 'a' },
127 { "dvdchannel", 1, 0, 'c' },
128 { "dvdsubtitle", 1, 0, 's' },
131 { "vlans", 0, 0, OPT_VLANS },
132 { "server", 1, 0, OPT_SERVER },
133 { "port", 1, 0, OPT_PORT },
134 { "broadcast", 0, 0, OPT_BROADCAST },
136 /* Synchro options */
137 { "synchro", 1, 0, OPT_SYNCHRO },
139 /* Interface messages */
140 { "warning", 1, 0, OPT_WARNING },
145 static const char *psz_shortopts = "hHvga:s:c:";
149 /*****************************************************************************
150 * Global variable program_data - this is the one and only, see main.h
151 *****************************************************************************/
154 /*****************************************************************************
156 *****************************************************************************/
157 static void SetDefaultConfiguration ( void );
158 static int GetConfiguration ( int i_argc, char *ppsz_argv[],
160 static void Usage ( int i_fashion );
161 static void Version ( void );
163 static void InitSignalHandler ( void );
164 static void SimpleSignalHandler ( int i_signal );
165 static void FatalSignalHandler ( int i_signal );
167 int TestMMX ( void );
170 /*****************************************************************************
171 * main: parse command line, start interface and spawn threads
172 *****************************************************************************
173 * Steps during program execution are:
174 * -configuration parsing and messages interface initialization
175 * -opening of audio output device and some global modules
176 * -execution of interface, which exit on error or on user request
177 * -closing of audio output device and some global modules
178 * On error, the spawned threads are canceled, and the open devices closed.
179 *****************************************************************************/
180 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
182 main_t main_data; /* root of all data - see main.h */
184 p_main = &main_data; /* set up the global variable */
187 * System specific initialization code
194 * Test if our code is likely to run on this CPU
199 fprintf( stderr, "Sorry, this program needs an MMX processor. "
200 "Please run the non-MMX version.\n" );
206 * Initialize messages interface
208 p_main->p_msg = intf_MsgCreate();
209 if( !p_main->p_msg ) /* start messages interface */
211 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
217 * Set signal handling policy up for all the threads that will be created
219 InitSignalHandler(); /* prepare signals for interception */
224 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
231 * Initialize playlist and get commandline files
233 p_main->p_playlist = playlist_Create( );
234 if( !p_main->p_playlist )
236 intf_Msg( "Playlist initialization failed" );
240 playlist_Init( p_main->p_playlist, optind );
243 * Initialize plugin bank
245 p_main->p_bank = bank_Create( );
246 if( !p_main->p_bank )
248 intf_Msg( "Plugin bank initialization failed" );
249 playlist_Destroy( p_main->p_playlist );
253 bank_Init( p_main->p_bank );
256 * Initialize module bank
258 p_main->p_module_bank = module_CreateBank( );
259 if( !p_main->p_module_bank )
261 intf_Msg( "Module bank initialization failed" );
262 bank_Destroy( p_main->p_bank );
263 playlist_Destroy( p_main->p_playlist );
267 module_InitBank( p_main->p_module_bank );
270 * Initialize shared resources and libraries
272 /* FIXME: no VLANs */
274 if( p_main->b_vlans && input_VlanCreate() )
276 /* On error during vlans initialization, switch off vlans */
277 intf_Msg( "Virtual LANs initialization failed : "
278 "vlans management is deactivated" );
284 * Open audio device and start aout thread
286 if( p_main->b_audio )
288 p_main->p_aout = aout_CreateThread( NULL );
289 if( p_main->p_aout == NULL )
291 /* On error during audio initialization, switch off audio */
292 intf_Msg( "Audio initialization failed : audio is deactivated" );
300 p_main->p_intf = intf_Create();
301 if( p_main->p_intf != NULL )
304 * This is the main loop
306 intf_Run( p_main->p_intf );
308 intf_Destroy( p_main->p_intf );
314 if( p_main->b_audio )
316 aout_DestroyThread( p_main->p_aout, NULL );
320 * Free shared resources and libraries
324 if( p_main->b_vlans )
333 module_DestroyBank( p_main->p_module_bank );
338 bank_Destroy( p_main->p_bank );
343 playlist_Destroy( p_main->p_playlist );
347 * System specific cleaning code
353 * Terminate messages interface and program
355 intf_Msg( "Program terminated." );
361 /*****************************************************************************
362 * main_GetIntVariable: get the int value of an environment variable
363 *****************************************************************************
364 * This function is used to read some default parameters in modules.
365 *****************************************************************************/
366 int main_GetIntVariable( char *psz_name, int i_default )
368 char * psz_env; /* environment value */
369 char * psz_end; /* end of parsing index */
370 long int i_value; /* value */
372 psz_env = getenv( psz_name );
375 i_value = strtol( psz_env, &psz_end, 0 );
376 if( (*psz_env != '\0') && (*psz_end == '\0') )
384 /*****************************************************************************
385 * main_GetPszVariable: get the string value of an environment variable
386 *****************************************************************************
387 * This function is used to read some default parameters in modules.
388 *****************************************************************************/
389 char * main_GetPszVariable( char *psz_name, char *psz_default )
393 psz_env = getenv( psz_name );
398 return( psz_default );
401 /*****************************************************************************
402 * main_PutPszVariable: set the string value of an environment variable
403 *****************************************************************************
404 * This function is used to set some default parameters in modules. The use of
405 * this function will cause some memory leak: since some systems use the pointer
406 * passed to putenv to store the environment string, it can't be freed.
407 *****************************************************************************/
408 void main_PutPszVariable( char *psz_name, char *psz_value )
412 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
413 if( psz_env == NULL )
415 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
420 sprintf( psz_env, "%s=%s", psz_name, psz_value );
421 if( putenv( psz_env ) )
423 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
428 /*****************************************************************************
429 * main_PutIntVariable: set the integer value of an environment variable
430 *****************************************************************************
431 * This function is used to set some default parameters in modules. The use of
432 * this function will cause some memory leak: since some systems use the pointer
433 * passed to putenv to store the environment string, it can't be freed.
434 *****************************************************************************/
435 void main_PutIntVariable( char *psz_name, int i_value )
437 char psz_value[ 256 ]; /* buffer for value */
439 sprintf( psz_value, "%d", i_value );
440 main_PutPszVariable( psz_name, psz_value );
443 /* following functions are local */
445 /*****************************************************************************
446 * SetDefaultConfiguration: set default options
447 *****************************************************************************
448 * This function is called by GetConfiguration before command line is parsed.
449 * It sets all the default values required later by the program. At this stage,
450 * most structure are not yet allocated, so initialization must be done using
452 *****************************************************************************/
453 static void SetDefaultConfiguration( void )
456 * All features are activated by default except vlans
463 /*****************************************************************************
464 * GetConfiguration: parse command line
465 *****************************************************************************
466 * Parse command line and configuration file for configuration. If the inline
467 * help is requested, the function Usage() is called and the function returns
468 * -1 (causing main() to exit). The messages interface is initialized at this
469 * stage, but most structures are not allocated, so only environment should
471 *****************************************************************************/
472 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
477 /* Set default configuration and copy arguments */
478 p_main->i_argc = i_argc;
479 p_main->ppsz_argv = ppsz_argv;
480 p_main->ppsz_env = ppsz_env;
481 SetDefaultConfiguration();
483 intf_MsgImm( COPYRIGHT_MESSAGE );
485 /* Get the executable name (similar to the basename command) */
486 p_main->psz_arg0 = p_pointer = ppsz_argv[ 0 ];
489 if( *p_pointer == '/' )
491 p_main->psz_arg0 = ++p_pointer;
499 /* Parse command line options */
502 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
506 /* General/common options */
507 case 'h': /* -h, --help */
511 case 'H': /* -H, --longhelp */
515 case 'v': /* -v, --version */
521 case OPT_NOAUDIO: /* --noaudio */
524 case OPT_AOUT: /* --aout */
525 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
527 case OPT_STEREO: /* --stereo */
528 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
530 case OPT_MONO: /* --mono */
531 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
535 case OPT_NOVIDEO: /* --novideo */
538 case OPT_VOUT: /* --vout */
539 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
541 case OPT_DISPLAY: /* --display */
542 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
544 case OPT_WIDTH: /* --width */
545 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
547 case OPT_HEIGHT: /* --height */
548 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
551 case 'g': /* -g, --grayscale */
552 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
554 case OPT_COLOR: /* --color */
555 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
557 case OPT_YUV: /* --yuv */
558 main_PutPszVariable( YUV_METHOD_VAR, optarg );
563 if ( ! strcmp(optarg, "ac3") )
564 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
565 else if ( ! strcmp(optarg, "lpcm") )
566 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
567 else if ( ! strcmp(optarg, "off") )
568 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
570 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
573 main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
576 main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
580 case OPT_VLANS: /* --vlans */
583 case OPT_SERVER: /* --server */
584 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
586 case OPT_PORT: /* --port */
587 main_PutPszVariable( INPUT_PORT_VAR, optarg );
589 case OPT_BROADCAST: /* --broadcast */
590 main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
593 /* Synchro options */
595 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
598 /* Interface warning messages level */
599 case OPT_WARNING: /* --warning */
600 main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
603 /* Internal error: unknown option */
606 intf_ErrMsg( "intf error: unknown option `%s'", ppsz_argv[optind - 1] );
614 /* Parse command line parameters - no check is made for these options */
615 for( i_opt = optind; i_opt < i_argc; i_opt++ )
617 putenv( ppsz_argv[ i_opt ] );
622 /*****************************************************************************
623 * Usage: print program usage
624 *****************************************************************************
625 * Print a short inline help. Message interface is initialized at this stage.
626 *****************************************************************************/
627 static void Usage( int i_fashion )
630 intf_Msg( "Usage: %s [options] [parameters] [file]...",
633 if( i_fashion == USAGE )
635 intf_Msg( "Try `%s --help' for more information.",
641 intf_Msg( "\nOptions:"
642 "\n --noaudio \tdisable audio"
643 "\n --aout <plugin> \taudio output method"
644 "\n --stereo, --mono \tstereo/mono audio"
646 "\n --novideo \tdisable video"
647 "\n --vout <plugin> \tvideo output method"
648 "\n --display <display> \tdisplay string"
649 "\n --width <w>, --height <h> \tdisplay dimensions"
650 "\n -g, --grayscale \tgrayscale output"
651 "\n --color \tcolor output"
653 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
654 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
655 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
657 "\n --vlans \tenable vlans"
658 "\n --server <host> \tvideo server address"
659 "\n --port <port> \tvideo server port"
660 "\n --broadcast \tlisten to a broadcast"
662 "\n --synchro <type> \tforce synchro algorithm"
664 "\n --warning <level> \tdisplay warning messages"
666 "\n -h, --help \tprint help and exit"
667 "\n -H, --longhelp \tprint long help and exit"
668 "\n -v, --version \toutput version information and exit" );
670 if( i_fashion == SHORT_HELP )
673 /* Interface parameters */
674 intf_Msg( "\nInterface parameters:\n"
675 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
676 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
677 "\n " INTF_WARNING_VAR "=<level> \twarning level" );
679 /* Audio parameters */
680 intf_Msg( "\nAudio parameters:"
681 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
682 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
683 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
684 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
686 /* Video parameters */
687 intf_Msg( "\nVideo parameters:"
688 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
689 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
690 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
691 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
692 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
693 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output" );
696 intf_Msg( "\nDVD parameters:"
697 "\n " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
698 "\n " INPUT_DVD_CHANNEL_VAR "=[0-15] \taudio channel"
699 "\n " INPUT_DVD_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
701 /* Input parameters */
702 intf_Msg( "\nInput parameters:\n"
703 "\n " INPUT_SERVER_VAR "=<hostname> \tvideo server"
704 "\n " INPUT_PORT_VAR "=<port> \tvideo server port"
705 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
706 "\n " INPUT_BROADCAST_VAR "={1|0} \tbroadcast mode"
707 "\n " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server"
708 "\n " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port" );
710 /* Synchro parameters */
711 intf_Msg( "\nSynchro parameters:"
712 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm");
715 /*****************************************************************************
716 * Version: print complete program version
717 *****************************************************************************
718 * Print complete program version and build number.
719 *****************************************************************************/
720 static void Version( void )
722 intf_Msg( VERSION_MESSAGE
723 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
724 "You may redistribute it under the terms of the GNU General Public License;\n"
725 "see the file named COPYING for details.\n"
726 "Written by the VideoLAN team at Ecole Centrale, Paris." );
730 /*****************************************************************************
731 * InitSignalHandler: system signal handler initialization
732 *****************************************************************************
733 * Set the signal handlers. SIGTERM is not intercepted, because we need at
734 * at least a method to kill the program when all other methods failed, and
735 * when we don't want to use SIGKILL.
736 *****************************************************************************/
737 static void InitSignalHandler( void )
739 /* Termination signals */
740 signal( SIGHUP, FatalSignalHandler );
741 signal( SIGINT, FatalSignalHandler );
742 signal( SIGQUIT, FatalSignalHandler );
745 signal( SIGALRM, SimpleSignalHandler );
746 signal( SIGPIPE, SimpleSignalHandler );
750 /*****************************************************************************
751 * SimpleSignalHandler: system signal handler
752 *****************************************************************************
753 * This function is called when a non fatal signal is received by the program.
754 *****************************************************************************/
755 static void SimpleSignalHandler( int i_signal )
757 /* Acknowledge the signal received */
758 intf_WarnMsg(0, "intf: ignoring signal %d", i_signal );
762 /*****************************************************************************
763 * FatalSignalHandler: system signal handler
764 *****************************************************************************
765 * This function is called when a fatal signal is received by the program.
766 * It tries to end the program in a clean way.
767 *****************************************************************************/
768 static void FatalSignalHandler( int i_signal )
770 /* Once a signal has been trapped, the termination sequence will be armed and
771 * following signals will be ignored to avoid sending messages to an interface
772 * having been destroyed */
773 signal( SIGHUP, SIG_IGN );
774 signal( SIGINT, SIG_IGN );
775 signal( SIGQUIT, SIG_IGN );
777 /* Acknowledge the signal received */
778 intf_ErrMsgImm("intf error: signal %d received, exiting", i_signal );
780 /* Try to terminate everything - this is done by requesting the end of the
781 * interface thread */
782 p_main->p_intf->b_die = 1;