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
73 #define OPT_STEREO 152
76 #define OPT_NOVIDEO 160
78 #define OPT_DISPLAY 162
80 #define OPT_HEIGHT 164
86 #define OPT_SERVER 171
88 #define OPT_BROADCAST 173
91 #define OPT_SYNCHRO 180
93 #define OPT_WARNING 190
102 static const struct option longopts[] =
104 /* name, has_arg, flag, val */
106 /* General/common options */
107 { "help", 0, 0, 'h' },
108 { "longhelp", 0, 0, 'H' },
109 { "version", 0, 0, 'v' },
112 { "noaudio", 0, 0, OPT_NOAUDIO },
113 { "aout", 1, 0, OPT_AOUT },
114 { "stereo", 0, 0, OPT_STEREO },
115 { "mono", 0, 0, OPT_MONO },
118 { "novideo", 0, 0, OPT_NOVIDEO },
119 { "vout", 1, 0, OPT_VOUT },
120 { "display", 1, 0, OPT_DISPLAY },
121 { "width", 1, 0, OPT_WIDTH },
122 { "height", 1, 0, OPT_HEIGHT },
123 { "grayscale", 0, 0, 'g' },
124 { "color", 0, 0, OPT_COLOR },
125 { "idct", 1, 0, OPT_IDCT },
126 { "yuv", 1, 0, OPT_YUV },
129 { "dvdaudio", 1, 0, 'a' },
130 { "dvdchannel", 1, 0, 'c' },
131 { "dvdsubtitle", 1, 0, 's' },
134 { "vlans", 0, 0, OPT_VLANS },
135 { "server", 1, 0, OPT_SERVER },
136 { "port", 1, 0, OPT_PORT },
137 { "broadcast", 0, 0, OPT_BROADCAST },
138 { "dvd", 0, 0, OPT_DVD },
140 /* Synchro options */
141 { "synchro", 1, 0, OPT_SYNCHRO },
143 /* Interface messages */
144 { "warning", 1, 0, OPT_WARNING },
149 static const char *psz_shortopts = "hHvga:s:c:";
153 /*****************************************************************************
154 * Global variable program_data - this is the one and only, see main.h
155 *****************************************************************************/
158 /*****************************************************************************
160 *****************************************************************************/
161 static void SetDefaultConfiguration ( void );
162 static int GetConfiguration ( int i_argc, char *ppsz_argv[],
164 static void Usage ( int i_fashion );
165 static void Version ( void );
167 static void InitSignalHandler ( void );
168 static void SimpleSignalHandler ( int i_signal );
169 static void FatalSignalHandler ( int i_signal );
171 /*****************************************************************************
172 * main: parse command line, start interface and spawn threads
173 *****************************************************************************
174 * Steps during program execution are:
175 * -configuration parsing and messages interface initialization
176 * -opening of audio output device and some global modules
177 * -execution of interface, which exit on error or on user request
178 * -closing of audio output device and some global modules
179 * On error, the spawned threads are canceled, and the open devices closed.
180 *****************************************************************************/
181 int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
183 main_t main_data; /* root of all data - see main.h */
185 p_main = &main_data; /* set up the global variable */
188 * System specific initialization code
195 * Test if our code is likely to run on this CPU
198 if( !( TestCPU() & CPU_CAPABILITY_MMX ) )
200 fprintf( stderr, "Sorry, this program needs an MMX processor. "
201 "Please run the non-MMX version.\n" );
207 * Initialize messages interface
209 p_main->p_msg = intf_MsgCreate();
210 if( !p_main->p_msg ) /* start messages interface */
212 fprintf( stderr, "error: can't initialize messages interface (%s)\n",
220 if( GetConfiguration( i_argc, ppsz_argv, ppsz_env ) ) /* parse cmd line */
227 * Initialize playlist and get commandline files
229 p_main->p_playlist = playlist_Create( );
230 if( !p_main->p_playlist )
232 intf_ErrMsg( "playlist error: playlist initialization failed" );
236 playlist_Init( p_main->p_playlist, optind );
239 * Initialize plugin bank
241 p_main->p_bank = bank_Create( );
242 if( !p_main->p_bank )
244 intf_ErrMsg( "plugin error: plugin bank initialization failed" );
245 playlist_Destroy( p_main->p_playlist );
249 bank_Init( p_main->p_bank );
252 * Initialize module bank
254 p_main->p_module_bank = module_CreateBank( );
255 if( !p_main->p_module_bank )
257 intf_ErrMsg( "module error: module bank initialization failed" );
258 bank_Destroy( p_main->p_bank );
259 playlist_Destroy( p_main->p_playlist );
263 module_InitBank( p_main->p_module_bank );
266 * Initialize shared resources and libraries
268 /* FIXME: no VLANs */
270 if( p_main->b_vlans && input_VlanCreate() )
272 /* On error during vlans initialization, switch off vlans */
273 intf_Msg( "Virtual LANs initialization failed : "
274 "vlans management is deactivated" );
282 p_main->p_intf = intf_Create();
284 if( p_main->p_intf != NULL )
287 * Set signal handling policy for all threads
292 * Open audio device and start aout thread
294 if( p_main->b_audio )
296 p_main->p_aout = aout_CreateThread( NULL );
297 if( p_main->p_aout == NULL )
299 /* On error during audio initialization, switch off audio */
300 intf_ErrMsg( "aout error: audio initialization failed,"
301 " audio is deactivated" );
307 * This is the main loop
309 intf_Run( p_main->p_intf );
311 intf_Destroy( p_main->p_intf );
316 if( p_main->b_audio )
318 aout_DestroyThread( p_main->p_aout, NULL );
323 * Free shared resources and libraries
327 if( p_main->b_vlans )
336 module_DestroyBank( p_main->p_module_bank );
341 bank_Destroy( p_main->p_bank );
346 playlist_Destroy( p_main->p_playlist );
350 * System specific cleaning code
356 * Terminate messages interface and program
358 intf_Msg( "intf: program terminated." );
364 /*****************************************************************************
365 * main_GetIntVariable: get the int value of an environment variable
366 *****************************************************************************
367 * This function is used to read some default parameters in modules.
368 *****************************************************************************/
369 int main_GetIntVariable( char *psz_name, int i_default )
371 char * psz_env; /* environment value */
372 char * psz_end; /* end of parsing index */
373 long int i_value; /* value */
375 psz_env = getenv( psz_name );
378 i_value = strtol( psz_env, &psz_end, 0 );
379 if( (*psz_env != '\0') && (*psz_end == '\0') )
387 /*****************************************************************************
388 * main_GetPszVariable: get the string value of an environment variable
389 *****************************************************************************
390 * This function is used to read some default parameters in modules.
391 *****************************************************************************/
392 char * main_GetPszVariable( char *psz_name, char *psz_default )
396 psz_env = getenv( psz_name );
401 return( psz_default );
404 /*****************************************************************************
405 * main_PutPszVariable: set the string value of an environment variable
406 *****************************************************************************
407 * This function is used to set some default parameters in modules. The use of
408 * this function will cause some memory leak: since some systems use the pointer
409 * passed to putenv to store the environment string, it can't be freed.
410 *****************************************************************************/
411 void main_PutPszVariable( char *psz_name, char *psz_value )
415 psz_env = malloc( strlen(psz_name) + strlen(psz_value) + 2 );
416 if( psz_env == NULL )
418 intf_ErrMsg( "intf error: cannot create psz_env (%s)",
423 sprintf( psz_env, "%s=%s", psz_name, psz_value );
424 if( putenv( psz_env ) )
426 intf_ErrMsg( "intf error: cannot putenv (%s)", strerror(errno) );
431 /*****************************************************************************
432 * main_PutIntVariable: set the integer value of an environment variable
433 *****************************************************************************
434 * This function is used to set some default parameters in modules. The use of
435 * this function will cause some memory leak: since some systems use the pointer
436 * passed to putenv to store the environment string, it can't be freed.
437 *****************************************************************************/
438 void main_PutIntVariable( char *psz_name, int i_value )
440 char psz_value[ 256 ]; /* buffer for value */
442 sprintf( psz_value, "%d", i_value );
443 main_PutPszVariable( psz_name, psz_value );
446 /* following functions are local */
448 /*****************************************************************************
449 * SetDefaultConfiguration: set default options
450 *****************************************************************************
451 * This function is called by GetConfiguration before command line is parsed.
452 * It sets all the default values required later by the program. At this stage,
453 * most structure are not yet allocated, so initialization must be done using
455 *****************************************************************************/
456 static void SetDefaultConfiguration( void )
459 * All features are activated by default except vlans
467 /*****************************************************************************
468 * GetConfiguration: parse command line
469 *****************************************************************************
470 * Parse command line and configuration file for configuration. If the inline
471 * help is requested, the function Usage() is called and the function returns
472 * -1 (causing main() to exit). The messages interface is initialized at this
473 * stage, but most structures are not allocated, so only environment should
475 *****************************************************************************/
476 static int GetConfiguration( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
481 /* Set default configuration and copy arguments */
482 p_main->i_argc = i_argc;
483 p_main->ppsz_argv = ppsz_argv;
484 p_main->ppsz_env = ppsz_env;
485 SetDefaultConfiguration();
487 intf_MsgImm( COPYRIGHT_MESSAGE );
489 /* Get the executable name (similar to the basename command) */
490 p_main->psz_arg0 = p_pointer = ppsz_argv[ 0 ];
493 if( *p_pointer == '/' )
495 p_main->psz_arg0 = ++p_pointer;
503 /* Parse command line options */
506 while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
510 /* General/common options */
511 case 'h': /* -h, --help */
515 case 'H': /* -H, --longhelp */
519 case 'v': /* -v, --version */
525 case OPT_NOAUDIO: /* --noaudio */
528 case OPT_AOUT: /* --aout */
529 main_PutPszVariable( AOUT_METHOD_VAR, optarg );
531 case OPT_STEREO: /* --stereo */
532 main_PutIntVariable( AOUT_STEREO_VAR, 1 );
534 case OPT_MONO: /* --mono */
535 main_PutIntVariable( AOUT_STEREO_VAR, 0 );
539 case OPT_NOVIDEO: /* --novideo */
542 case OPT_VOUT: /* --vout */
543 main_PutPszVariable( VOUT_METHOD_VAR, optarg );
545 case OPT_DISPLAY: /* --display */
546 main_PutPszVariable( VOUT_DISPLAY_VAR, optarg );
548 case OPT_WIDTH: /* --width */
549 main_PutPszVariable( VOUT_WIDTH_VAR, optarg );
551 case OPT_HEIGHT: /* --height */
552 main_PutPszVariable( VOUT_HEIGHT_VAR, optarg );
555 case 'g': /* -g, --grayscale */
556 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 1 );
558 case OPT_COLOR: /* --color */
559 main_PutIntVariable( VOUT_GRAYSCALE_VAR, 0 );
561 case OPT_IDCT: /* --idct */
562 main_PutPszVariable( IDCT_METHOD_VAR, optarg );
564 case OPT_YUV: /* --yuv */
565 main_PutPszVariable( YUV_METHOD_VAR, optarg );
570 if ( ! strcmp(optarg, "ac3") )
571 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
572 else if ( ! strcmp(optarg, "lpcm") )
573 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_LPCM );
574 else if ( ! strcmp(optarg, "off") )
575 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_NOAUDIO );
577 main_PutIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_MPEG );
580 main_PutIntVariable( INPUT_DVD_CHANNEL_VAR, atoi(optarg) );
583 main_PutIntVariable( INPUT_DVD_SUBTITLE_VAR, atoi(optarg) );
587 case OPT_VLANS: /* --vlans */
590 case OPT_SERVER: /* --server */
591 main_PutPszVariable( INPUT_SERVER_VAR, optarg );
593 case OPT_PORT: /* --port */
594 main_PutPszVariable( INPUT_PORT_VAR, optarg );
596 case OPT_BROADCAST: /* --broadcast */
597 main_PutIntVariable( INPUT_BROADCAST_VAR, 1 );
599 case OPT_DVD: /* --dvd */
603 /* Synchro options */
605 main_PutPszVariable( VPAR_SYNCHRO_VAR, optarg );
608 /* Interface warning messages level */
609 case OPT_WARNING: /* --warning */
610 main_PutIntVariable( INTF_WARNING_VAR, atoi(optarg) );
613 /* Internal error: unknown option */
616 intf_ErrMsg( "intf error: unknown option `%s'", ppsz_argv[optind - 1] );
624 /* Parse command line parameters - no check is made for these options */
625 for( i_opt = optind; i_opt < i_argc; i_opt++ )
627 putenv( ppsz_argv[ i_opt ] );
632 /*****************************************************************************
633 * Usage: print program usage
634 *****************************************************************************
635 * Print a short inline help. Message interface is initialized at this stage.
636 *****************************************************************************/
637 static void Usage( int i_fashion )
640 intf_Msg( "Usage: %s [options] [parameters] [file]...",
643 if( i_fashion == USAGE )
645 intf_Msg( "Try `%s --help' for more information.",
651 intf_Msg( "\nOptions:"
652 "\n --noaudio \tdisable audio"
653 "\n --aout <module> \taudio output method"
654 "\n --stereo, --mono \tstereo/mono audio"
656 "\n --novideo \tdisable video"
657 "\n --vout <module> \tvideo output method"
658 "\n --display <display> \tdisplay string"
659 "\n --width <w>, --height <h> \tdisplay dimensions"
660 "\n -g, --grayscale \tgrayscale output"
661 "\n --color \tcolor output"
662 "\n --idct <module> \tIDCT method"
663 "\n --yuv <module> \tYUV method"
664 "\n --synchro <type> \tforce synchro algorithm"
666 "\n --dvd \tDVD mode"
667 "\n -a, --dvdaudio <type> \tchoose DVD audio type"
668 "\n -c, --dvdchannel <channel> \tchoose DVD audio channel"
669 "\n -s, --dvdsubtitle <channel> \tchoose DVD subtitle channel"
671 "\n --vlans \tenable vlans"
672 "\n --server <host> \tvideo server address"
673 "\n --port <port> \tvideo server port"
674 "\n --broadcast \tlisten to a broadcast"
676 "\n --warning <level> \tdisplay warning messages"
678 "\n -h, --help \tprint help and exit"
679 "\n -H, --longhelp \tprint long help and exit"
680 "\n -v, --version \toutput version information and exit" );
682 if( i_fashion == SHORT_HELP )
685 /* Interface parameters */
686 intf_Msg( "\nInterface parameters:\n"
687 "\n " INTF_INIT_SCRIPT_VAR "=<filename> \tinitialization script"
688 "\n " INTF_CHANNELS_VAR "=<filename> \tchannels list"
689 "\n " INTF_WARNING_VAR "=<level> \twarning level" );
691 /* Audio parameters */
692 intf_Msg( "\nAudio parameters:"
693 "\n " AOUT_METHOD_VAR "=<method name> \taudio method"
694 "\n " AOUT_DSP_VAR "=<filename> \tdsp device path"
695 "\n " AOUT_STEREO_VAR "={1|0} \tstereo or mono output"
696 "\n " AOUT_RATE_VAR "=<rate> \toutput rate" );
698 /* Video parameters */
699 intf_Msg( "\nVideo parameters:"
700 "\n " VOUT_METHOD_VAR "=<method name> \tdisplay method"
701 "\n " VOUT_DISPLAY_VAR "=<display name> \tdisplay used"
702 "\n " VOUT_WIDTH_VAR "=<width> \tdisplay width"
703 "\n " VOUT_HEIGHT_VAR "=<height> \tdislay height"
704 "\n " VOUT_FB_DEV_VAR "=<filename> \tframebuffer device path"
705 "\n " VOUT_GRAYSCALE_VAR "={1|0} \tgrayscale or color output"
706 "\n " IDCT_METHOD_VAR "=<method name> \tIDCT method"
707 "\n " YUV_METHOD_VAR "=<method name> \tYUV method"
708 "\n " VPAR_SYNCHRO_VAR "={I|I+|IP|IP+|IPB} \tsynchro algorithm" );
711 intf_Msg( "\nDVD parameters:"
712 "\n " INPUT_DVD_DEVICE_VAR "=<device> \tDVD device"
713 "\n " INPUT_DVD_AUDIO_VAR "={ac3|lpcm|mpeg|off} \taudio type"
714 "\n " INPUT_DVD_CHANNEL_VAR "=[0-15] \taudio channel"
715 "\n " INPUT_DVD_SUBTITLE_VAR "=[0-31] \tsubtitle channel" );
717 /* Input parameters */
718 intf_Msg( "\nInput parameters:\n"
719 "\n " INPUT_SERVER_VAR "=<hostname> \tvideo server"
720 "\n " INPUT_PORT_VAR "=<port> \tvideo server port"
721 "\n " INPUT_IFACE_VAR "=<interface> \tnetwork interface"
722 "\n " INPUT_BROADCAST_VAR "={1|0} \tbroadcast mode"
723 "\n " INPUT_VLAN_SERVER_VAR "=<hostname> \tvlan server"
724 "\n " INPUT_VLAN_PORT_VAR "=<port> \tvlan server port"
729 /*****************************************************************************
730 * Version: print complete program version
731 *****************************************************************************
732 * Print complete program version and build number.
733 *****************************************************************************/
734 static void Version( void )
736 intf_Msg( VERSION_MESSAGE
737 "This program comes with NO WARRANTY, to the extent permitted by law.\n"
738 "You may redistribute it under the terms of the GNU General Public License;\n"
739 "see the file named COPYING for details.\n"
740 "Written by the VideoLAN team at Ecole Centrale, Paris." );
744 /*****************************************************************************
745 * InitSignalHandler: system signal handler initialization
746 *****************************************************************************
747 * Set the signal handlers. SIGTERM is not intercepted, because we need at
748 * at least a method to kill the program when all other methods failed, and
749 * when we don't want to use SIGKILL.
750 *****************************************************************************/
751 static void InitSignalHandler( void )
753 /* Termination signals */
754 signal( SIGHUP, FatalSignalHandler );
755 signal( SIGINT, FatalSignalHandler );
756 signal( SIGQUIT, FatalSignalHandler );
759 signal( SIGALRM, SimpleSignalHandler );
760 signal( SIGPIPE, SimpleSignalHandler );
764 /*****************************************************************************
765 * SimpleSignalHandler: system signal handler
766 *****************************************************************************
767 * This function is called when a non fatal signal is received by the program.
768 *****************************************************************************/
769 static void SimpleSignalHandler( int i_signal )
771 /* Acknowledge the signal received */
772 intf_WarnMsg(0, "intf: ignoring signal %d", i_signal );
776 /*****************************************************************************
777 * FatalSignalHandler: system signal handler
778 *****************************************************************************
779 * This function is called when a fatal signal is received by the program.
780 * It tries to end the program in a clean way.
781 *****************************************************************************/
782 static void FatalSignalHandler( int i_signal )
784 /* Once a signal has been trapped, the termination sequence will be armed and
785 * following signals will be ignored to avoid sending messages to an interface
786 * having been destroyed */
787 signal( SIGHUP, SIG_IGN );
788 signal( SIGINT, SIG_IGN );
789 signal( SIGQUIT, SIG_IGN );
791 /* Acknowledge the signal received */
792 intf_ErrMsgImm("intf error: signal %d received, exiting", i_signal );
794 /* Try to terminate everything - this is done by requesting the end of the
795 * interface thread */
796 p_main->p_intf->b_die = 1;