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() */
49 #include "stream_control.h"
50 #include "input_ext-intf.h"
53 #include "interface.h"
55 #include "audio_output.h"
58 #include "beos_specific.h"
63 /*****************************************************************************
64 * Command line options constants. If something is changed here, be sure that
65 * GetConfiguration and Usage are also changed.
66 *****************************************************************************/
68 /* Long options return values - note that values corresponding to short options
69 * chars, and in general any regular char, should be avoided */
70 #define OPT_NOAUDIO 150
72 #define OPT_STEREO 152
75 #define OPT_NOVIDEO 160
77 #define OPT_DISPLAY 162
79 #define OPT_HEIGHT 164
84 #define OPT_SERVER 171
86 #define OPT_BROADCAST 173
88 #define OPT_SYNCHRO 180
90 #define OPT_WARNING 190
99 static const struct option longopts[] =
101 /* name, has_arg, flag, val */
103 /* General/common options */
104 { "help", 0, 0, 'h' },
105 { "longhelp", 0, 0, 'H' },
106 { "version", 0, 0, 'v' },
109 { "noaudio", 0, 0, OPT_NOAUDIO },
110 { "aout", 1, 0, OPT_AOUT },
111 { "stereo", 0, 0, OPT_STEREO },
112 { "mono", 0, 0, OPT_MONO },
115 { "novideo", 0, 0, OPT_NOVIDEO },
116 { "vout", 1, 0, OPT_VOUT },
117 { "display", 1, 0, OPT_DISPLAY },
118 { "width", 1, 0, OPT_WIDTH },
119 { "height", 1, 0, OPT_HEIGHT },
120 { "grayscale", 0, 0, 'g' },
121 { "color", 0, 0, OPT_COLOR },
122 { "yuv", 1, 0, OPT_YUV },
125 { "dvdaudio", 1, 0, 'a' },
126 { "dvdchannel", 1, 0, 'c' },
127 { "dvdsubtitle", 1, 0, 's' },
130 { "vlans", 0, 0, OPT_VLANS },
131 { "server", 1, 0, OPT_SERVER },
132 { "port", 1, 0, OPT_PORT },
133 { "broadcast", 0, 0, OPT_BROADCAST },
135 /* Synchro options */
136 { "synchro", 1, 0, OPT_SYNCHRO },
138 /* Interface messages */
139 { "warning", 1, 0, OPT_WARNING },
144 static const char *psz_shortopts = "hHvga:s:c:";
148 /*****************************************************************************
149 * Global variable program_data - this is the one and only, see main.h
150 *****************************************************************************/
153 /*****************************************************************************
155 *****************************************************************************/
156 static void SetDefaultConfiguration ( void );
157 static int GetConfiguration ( int i_argc, char *ppsz_argv[],
159 static void Usage ( int i_fashion );
160 static void Version ( void );
162 static void InitSignalHandler ( void );
163 static void SimpleSignalHandler ( int i_signal );
164 static void FatalSignalHandler ( int i_signal );
166 int TestMMX ( void );
169 /*****************************************************************************
170 * main: parse command line, start interface and spawn threads
171 *****************************************************************************
172 * Steps during program execution are:
173 * -configuration parsing and messages interface initialization
174 * -opening of audio output device and some global modules
175 * -execution of interface, which exit on error or on user request
176 * -closing of audio output device and some global modules
177 * On error, the spawned threads are canceled, and the open devices closed.
178 *****************************************************************************/
179 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
181 main_t main_data; /* root of all data - see main.h */
183 p_main = &main_data; /* set up the global variable */
186 * System specific initialization code
193 * Test if our code is likely to run on this CPU
198 fprintf( stderr, "Sorry, this program needs an MMX processor. "
199 "Please run the non-MMX version.\n" );
205 * Initialize messages interface
207 p_main->p_msg = intf_MsgCreate();
208 if( !p_main->p_msg ) /* start messages interface */
210 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
216 * Set signal handling policy up for all the threads that will be created
218 InitSignalHandler(); /* prepare signals for interception */
223 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
230 * Initialize playlist and get commandline files
232 p_main->p_playlist = playlist_Create( );
233 if( !p_main->p_playlist )
235 intf_Msg( "Playlist initialization failed" );
239 playlist_Init( p_main->p_playlist, optind );
242 * Initialize plugin bank
244 p_main->p_bank = bank_Create( );
245 if( !p_main->p_bank )
247 intf_Msg( "Plugin bank initialization failed" );
248 playlist_Destroy( p_main->p_playlist );
252 bank_Init( p_main->p_bank );
255 * Initialize shared resources and libraries
257 /* FIXME: no VLANs */
259 if( p_main->b_vlans && input_VlanCreate() )
261 /* On error during vlans initialization, switch off vlans */
262 intf_Msg( "Virtual LANs initialization failed : "
263 "vlans management is deactivated" );
269 * Open audio device and start aout thread
271 if( p_main->b_audio )
273 p_main->p_aout = aout_CreateThread( NULL );
274 if( p_main->p_aout == NULL )
276 /* On error during audio initialization, switch off audio */
277 intf_Msg( "Audio initialization failed : audio is deactivated" );
285 p_main->p_intf = intf_Create();
286 if( p_main->p_intf != NULL )
289 * This is the main loop
291 intf_Run( p_main->p_intf );
293 intf_Destroy( p_main->p_intf );
299 if( p_main->b_audio )
301 aout_DestroyThread( p_main->p_aout, NULL );
305 * Free shared resources and libraries
309 if( p_main->b_vlans )
318 bank_Destroy( p_main->p_bank );
323 playlist_Destroy( p_main->p_playlist );
327 * System specific cleaning code
333 * Terminate messages interface and program
335 intf_Msg( "Program terminated." );
341 /*****************************************************************************
342 * main_GetIntVariable: get the int value of an environment variable
343 *****************************************************************************
344 * This function is used to read some default parameters in modules.
345 *****************************************************************************/
346 int main_GetIntVariable( char *psz_name, int i_default )
348 char * psz_env; /* environment value */
349 char * psz_end; /* end of parsing index */
350 long int i_value; /* value */
352 psz_env = getenv( psz_name );
355 i_value = strtol( psz_env, &psz_end, 0 );
356 if( (*psz_env != '\0') && (*psz_end == '\0') )
364 /*****************************************************************************
365 * main_GetPszVariable: get the string value of an environment variable
366 *****************************************************************************
367 * This function is used to read some default parameters in modules.
368 *****************************************************************************/
369 char * main_GetPszVariable( char *psz_name, char *psz_default )
373 psz_env = getenv( psz_name );
378 return( psz_default );
381 /*****************************************************************************
382 * main_PutPszVariable: set the string value of an environment variable
383 *****************************************************************************
384 * This function is used to set some default parameters in modules. The use of
385 * this function will cause some memory leak: since some systems use the pointer
386 * passed to putenv to store the environment string, it can't be freed.
387 *****************************************************************************/
388 void main_PutPszVariable( char *psz_name, char *psz_value )
392 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
393 if( psz_env == NULL )
395 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
400 sprintf( psz_env, "%s=%s", psz_name, psz_value );
401 if( putenv( psz_env ) )
403 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
408 /*****************************************************************************
409 * main_PutIntVariable: set the integer value of an environment variable
410 *****************************************************************************
411 * This function is used to set some default parameters in modules. The use of
412 * this function will cause some memory leak: since some systems use the pointer
413 * passed to putenv to store the environment string, it can't be freed.
414 *****************************************************************************/
415 void main_PutIntVariable( char *psz_name, int i_value )
417 char psz_value[ 256 ]; /* buffer for value */
419 sprintf( psz_value, "%d", i_value );
420 main_PutPszVariable( psz_name, psz_value );
423 /* following functions are local */
425 /*****************************************************************************
426 * SetDefaultConfiguration: set default options
427 *****************************************************************************
428 * This function is called by GetConfiguration before command line is parsed.
429 * It sets all the default values required later by the program. At this stage,
430 * most structure are not yet allocated, so initialization must be done using
432 *****************************************************************************/
433 static void SetDefaultConfiguration( void )
436 * All features are activated by default except vlans
443 /*****************************************************************************
444 * GetConfiguration: parse command line
445 *****************************************************************************
446 * Parse command line and configuration file for configuration. If the inline
447 * help is requested, the function Usage() is called and the function returns
448 * -1 (causing main() to exit). The messages interface is initialized at this
449 * stage, but most structures are not allocated, so only environment should
451 *****************************************************************************/
452 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
457 /* Set default configuration and copy arguments */
458 p_main->i_argc = i_argc;
459 p_main->ppsz_argv = ppsz_argv;
460 p_main->ppsz_env = ppsz_env;
461 SetDefaultConfiguration();
463 intf_MsgImm( COPYRIGHT_MESSAGE );
465 /* Get the executable name (similar to the basename command) */
466 p_main->psz_arg0 = p_pointer = ppsz_argv[ 0 ];
469 if( *p_pointer == '/' )
471 p_main->psz_arg0 = ++p_pointer;
479 /* Parse command line options */
482 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
486 /* General/common options */
487 case 'h': /* -h, --help */
491 case 'H': /* -H, --longhelp */
495 case 'v': /* -v, --version */
501 case OPT_NOAUDIO: /* --noaudio */
504 case OPT_AOUT: /* --aout */
505 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
507 case OPT_STEREO: /* --stereo */
508 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
510 case OPT_MONO: /* --mono */
511 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
515 case OPT_NOVIDEO: /* --novideo */
518 case OPT_VOUT: /* --vout */
519 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
521 case OPT_DISPLAY: /* --display */
522 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
524 case OPT_WIDTH: /* --width */
525 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
527 case OPT_HEIGHT: /* --height */
528 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
531 case 'g': /* -g, --grayscale */
532 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
534 case OPT_COLOR: /* --color */
535 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
537 case OPT_YUV: /* --yuv */
538 main_PutPszVariable( YUV_METHOD_VAR, optarg );
543 if ( ! strcmp(optarg, "ac3") )
544 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
545 else if ( ! strcmp(optarg, "lpcm") )
546 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
547 else if ( ! strcmp(optarg, "off") )
548 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
550 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
553 main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
556 main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
560 case OPT_VLANS: /* --vlans */
563 case OPT_SERVER: /* --server */
564 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
566 case OPT_PORT: /* --port */
567 main_PutPszVariable( INPUT_PORT_VAR, optarg );
569 case OPT_BROADCAST: /* --broadcast */
570 main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
573 /* Synchro options */
575 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
578 /* Interface warning messages level */
579 case OPT_WARNING: /* --warning */
580 main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
583 /* Internal error: unknown option */
586 intf_ErrMsg( "intf error: unknown option `%s'", ppsz_argv[optind - 1] );
594 /* Parse command line parameters - no check is made for these options */
595 for( i_opt = optind; i_opt < i_argc; i_opt++ )
597 putenv( ppsz_argv[ i_opt ] );
602 /*****************************************************************************
603 * Usage: print program usage
604 *****************************************************************************
605 * Print a short inline help. Message interface is initialized at this stage.
606 *****************************************************************************/
607 static void Usage( int i_fashion )
610 intf_Msg( "Usage: %s [options] [parameters] [file]...",
613 if( i_fashion == USAGE )
615 intf_Msg( "Try `%s --help' for more information.",
621 intf_Msg( "\nOptions:"
622 "\n --noaudio \tdisable audio"
623 "\n --aout <plugin> \taudio output method"
624 "\n --stereo, --mono \tstereo/mono audio"
626 "\n --novideo \tdisable video"
627 "\n --vout <plugin> \tvideo output method"
628 "\n --display <display> \tdisplay string"
629 "\n --width <w>, --height <h> \tdisplay dimensions"
630 "\n -g, --grayscale \tgrayscale output"
631 "\n --color \tcolor output"
633 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
634 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
635 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
637 "\n --vlans \tenable vlans"
638 "\n --server <host> \tvideo server address"
639 "\n --port <port> \tvideo server port"
640 "\n --broadcast \tlisten to a broadcast"
642 "\n --synchro <type> \tforce synchro algorithm"
644 "\n --warning <level> \tdisplay warning messages"
646 "\n -h, --help \tprint help and exit"
647 "\n -H, --longhelp \tprint long help and exit"
648 "\n -v, --version \toutput version information and exit" );
650 if( i_fashion == SHORT_HELP )
653 /* Interface parameters */
654 intf_Msg( "\nInterface parameters:\n"
655 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
656 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
657 "\n " INTF_WARNING_VAR "=<level> \twarning level" );
659 /* Audio parameters */
660 intf_Msg( "\nAudio parameters:"
661 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
662 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
663 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
664 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
666 /* Video parameters */
667 intf_Msg( "\nVideo parameters:"
668 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
669 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
670 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
671 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
672 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
673 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output" );
676 intf_Msg( "\nDVD parameters:"
677 "\n " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
678 "\n " INPUT_DVD_CHANNEL_VAR "=[0-15] \taudio channel"
679 "\n " INPUT_DVD_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
681 /* Input parameters */
682 intf_Msg( "\nInput parameters:\n"
683 "\n " INPUT_SERVER_VAR "=<hostname> \tvideo server"
684 "\n " INPUT_PORT_VAR "=<port> \tvideo server port"
685 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
686 "\n " INPUT_BROADCAST_VAR "={1|0} \tbroadcast mode"
687 "\n " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server"
688 "\n " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port" );
690 /* Synchro parameters */
691 intf_Msg( "\nSynchro parameters:"
692 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm");
695 /*****************************************************************************
696 * Version: print complete program version
697 *****************************************************************************
698 * Print complete program version and build number.
699 *****************************************************************************/
700 static void Version( void )
702 intf_Msg( VERSION_MESSAGE
703 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
704 "You may redistribute it under the terms of the GNU General Public License;\n"
705 "see the file named COPYING for details.\n"
706 "Written by the VideoLAN team at Ecole Centrale, Paris." );
710 /*****************************************************************************
711 * InitSignalHandler: system signal handler initialization
712 *****************************************************************************
713 * Set the signal handlers. SIGTERM is not intercepted, because we need at
714 * at least a method to kill the program when all other methods failed, and
715 * when we don't want to use SIGKILL.
716 *****************************************************************************/
717 static void InitSignalHandler( void )
719 /* Termination signals */
720 signal( SIGHUP, FatalSignalHandler );
721 signal( SIGINT, FatalSignalHandler );
722 signal( SIGQUIT, FatalSignalHandler );
725 signal( SIGALRM, SimpleSignalHandler );
726 signal( SIGPIPE, SimpleSignalHandler );
730 /*****************************************************************************
731 * SimpleSignalHandler: system signal handler
732 *****************************************************************************
733 * This function is called when a non fatal signal is received by the program.
734 *****************************************************************************/
735 static void SimpleSignalHandler( int i_signal )
737 /* Acknowledge the signal received */
738 intf_WarnMsg(0, "intf: ignoring signal %d", i_signal );
742 /*****************************************************************************
743 * FatalSignalHandler: system signal handler
744 *****************************************************************************
745 * This function is called when a fatal signal is received by the program.
746 * It tries to end the program in a clean way.
747 *****************************************************************************/
748 static void FatalSignalHandler( int i_signal )
750 /* Once a signal has been trapped, the termination sequence will be armed and
751 * following signals will be ignored to avoid sending messages to an interface
752 * having been destroyed */
753 signal( SIGHUP, SIG_IGN );
754 signal( SIGINT, SIG_IGN );
755 signal( SIGQUIT, SIG_IGN );
757 /* Acknowledge the signal received */
758 intf_ErrMsgImm("intf error: signal %d received, exiting", i_signal );
760 /* Try to terminate everything - this is done by requesting the end of the
761 * interface thread */
762 p_main->p_intf->b_die = 1;