main.c 17 KB
Newer Older
Michel Kaempf's avatar
Michel Kaempf committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*******************************************************************************
 * main.c: main vlc source
 * (c)1998 VideoLAN
 *******************************************************************************
 * Includes the main() function for vlc. Parses command line, start interface
 * and spawn threads.
 *******************************************************************************/

/*******************************************************************************
 * Preamble
 *******************************************************************************/
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/soundcard.h>
#include <sys/uio.h>
#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>

#include "config.h"
#include "common.h"
#include "mtime.h"
27
#include "vlc_thread.h"
Michel Kaempf's avatar
Michel Kaempf committed
28
#include "netutils.h"
29 30 31
#include "debug.h"

#include "intf_msg.h"
Michel Kaempf's avatar
Michel Kaempf committed
32 33 34

#include "input.h"
#include "input_vlan.h"
35
#include "input_netlist.h"
Michel Kaempf's avatar
Michel Kaempf committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#include "decoder_fifo.h"

#include "audio_output.h"
#include "audio_decoder.h"

#include "video.h"
#include "video_output.h"
#include "video_decoder.h"

#include "xconsole.h"
#include "interface.h"

#include "pgm_data.h"



/*
 * Command line options constants. If something is changed here, be sure that
 * GetConfiguration and Usage are also changed.
 */

/* Long options return values - note that values corresponding to short options
 * chars, and in general any regular char, should be avoided */
#define OPT_DISPLAY             130

#define OPT_CONSOLE_DISPLAY     140
#define OPT_CONSOLE_GEOMETRY    141

#define OPT_NOAUDIO             150
#define OPT_STEREO              151
#define OPT_MONO                152
#define OPT_RATE                153

#define OPT_NOVIDEO             160
#define OPT_XSHM                161
#define OPT_NOXSHM              162

#define OPT_NOVLANS             170
#define OPT_VLAN_SERVER         171
 
/* Long options */
static const struct option longopts[] =
{   
    /*  name,               has_arg,    flag,   val */     

    /* General/common options */
    {   "help",             0,          0,      'h' },          
    {   "display",          1,          0,      OPT_DISPLAY },

    /* Interface options */
    {   "console-display",  1,          0,      OPT_CONSOLE_DISPLAY }, 
    {   "console-geometry", 1,          0,      OPT_CONSOLE_GEOMETRY },

    /* Audio options */
    {   "noaudio",          0,          0,      OPT_NOAUDIO },       
    {   "stereo",           0,          0,      OPT_STEREO },
    {   "mono",             0,          0,      OPT_MONO },      
    {   "rate",             0,          0,      OPT_RATE },

    /* Video options */
    {   "novideo",          0,          0,      OPT_NOVIDEO },           
    {   "xshm",             0,          0,      OPT_XSHM },
    {   "noxshm",           0,          0,      OPT_NOXSHM },    

    /* VLAN management options */
    {   "novlans",          0,          0,      OPT_NOVLANS },
    {   "vlanserver",       1,          0,      OPT_VLAN_SERVER },                    

    {   0,                  0,          0,      0 }
};

/* Short options */
static const char *psz_shortopts = "h";

/*
 * Global variable program_data
 */
program_data_t *p_program_data;                              /* see pgm_data.h */

/*
 * Local prototypes
 */
static void SetDefaultConfiguration ( program_data_t *p_data );
static int  GetConfiguration        ( program_data_t *p_config, int i_argc, char *ppsz_argv[],
                                      char *ppsz_env[] );
static void Usage                   ( void );
static long GetIntParam             ( const char *psz_param, long i_min, long i_max, int *pi_err );
static void InitSignalHandler       ( void );
static void SignalHandler           ( int i_signal );

/*******************************************************************************
 * main: parse command line, start interface and spawn threads
 *******************************************************************************
 * Steps during program execution are:
 *      -configuration parsing and messages interface initialization
 *      -openning of audio output device
 *      -execution of interface, which exit on error or on user request
 *      -closing of audio output device
 * On error, the spawned threads are cancelled, and the openned devices closed.
 *******************************************************************************
 * ?? Signal handlers should be restored to default once the interface has
 * been closed, since they will cause a crash if the message interface is no
 * more active.
 *******************************************************************************/
int main( int i_argc, char *ppsz_argv[], char *ppsz_env[] )
{
    program_data_t  program_data;         /* root of all data - see pgm_data.h */
    
    /*
     * Read configuration, initialize messages interface and set up program
     */
    p_program_data = &program_data;              /* set up the global variable */
    if( intf_InitMsg( &program_data.intf_msg ) )   /* start messages interface */
    {
        fprintf(stderr, "intf critical: can't initialize messages interface (%s)\n",
                strerror(errno));
        return(errno);
    }
    if( GetConfiguration( &program_data,                 /* parse command line */
                          i_argc, ppsz_argv, ppsz_env ) )
    {
        intf_TerminateMsg( &program_data.intf_msg );
        return(errno);
    }
    intf_MsgImm( COPYRIGHT_MESSAGE );                 /* print welcome message */
    InitSignalHandler();                   /* prepare signals for interception */    

    /*
     * Initialize shared resources and libraries
     */
    if( program_data.cfg.b_vlans 
         && input_VlanMethodInit( &program_data.input_vlan_method, 
                                  program_data.cfg.psz_input_vlan_server,
                                  program_data.cfg.i_input_vlan_server_port) )
    {
        /* On error during vlans initialization, switch of vlans */
        intf_Msg("intf: switching off vlans\n");
        program_data.cfg.b_vlans = 0;
    }
    
    /*
     * Open audio device
     */
    if( program_data.cfg.b_audio )
    {                                                
        if( aout_Open( &program_data.aout_thread ) )
        {
            /* On error during audio initialization, switch of audio */
            intf_Msg("intf: switching off audio\n");
            program_data.cfg.b_audio = 0;
        }
        else if( aout_SpawnThread( &program_data.aout_thread ) )
        {
            aout_Close( &program_data.aout_thread );
            input_VlanMethodFree( &program_data.input_vlan_method );            
            intf_TerminateMsg( &program_data.intf_msg );
            return( -1 );
        }
        program_data.intf_thread.p_aout = &program_data.aout_thread;
    }
    
    /*
     * Run interface
     */
    intf_DbgMsg("intf debug: starting interface\n");
    intf_Run( &program_data.intf_thread );
    intf_DbgMsg("intf debug: interface terminated\n");

Michel Kaempf's avatar
Michel Kaempf committed
204
    /*
Michel Kaempf's avatar
Michel Kaempf committed
205 206
     * Close audio device
     */
Michel Kaempf's avatar
Michel Kaempf committed
207
    if( program_data.cfg.b_audio )
Michel Kaempf's avatar
Michel Kaempf committed
208 209 210
    {
        aout_CancelThread( &program_data.aout_thread );
        aout_Close( &program_data.aout_thread );
Michel Kaempf's avatar
Michel Kaempf committed
211
    }
Michel Kaempf's avatar
Michel Kaempf committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

    /*
     * Free shared resources and libraries
     */
    if( program_data.cfg.b_vlans )
    {        
        input_VlanMethodFree( &program_data.input_vlan_method );    
    }    

    /*
     * Terminate messages interface and program
     */
    intf_Msg( "program terminated.\n" );
    intf_TerminateMsg( &program_data.intf_msg );
    return( 0 );
}

/* following functions are local */

/*******************************************************************************
 * SetDefaultConfiguration: set default options
 *******************************************************************************
 * This function is called by GetConfiguration before command line is parsed.
 * It sets all the default values required later by the program. Note that
 * all properties must be initialized, wether they are command-line dependant
 * or not.
 *******************************************************************************/
static void SetDefaultConfiguration( program_data_t *p_data )
{
    /*
     * Audio output thread configuration 
     */
    p_data->cfg.b_audio = 1;                 /* audio is activated by default */
    p_data->aout_thread.dsp.psz_device = AOUT_DEFAULT_DEVICE;
    /* je rajouterai la dtection des formats supports quand le reste
     * marchera */
    p_data->aout_thread.dsp.i_format = AOUT_DEFAULT_FORMAT;
    p_data->aout_thread.dsp.b_stereo = AOUT_DEFAULT_STEREO;
    p_data->aout_thread.dsp.l_rate = AOUT_DEFAULT_RATE;

    /*
     * Interface thread configuration
     */
    /* X11 console */
    p_data->intf_thread.xconsole.psz_display =	NULL;
    p_data->intf_thread.xconsole.psz_geometry =	INTF_XCONSOLE_GEOMETRY;

    /* --- ?? following are ok */

    /*
     * Video output thread configuration
     */
    p_data->cfg.b_video =                   1;    
    p_data->vout_cfg.i_properties =         0;

    /* VLAN management */
Sam Hocevar's avatar
 
Sam Hocevar committed
268
    p_data->cfg.b_vlans =                   0;    
Michel Kaempf's avatar
Michel Kaempf committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    p_data->cfg.psz_input_vlan_server =     VLAN_DEFAULT_SERVER;
    p_data->cfg.i_input_vlan_server_port =  VLAN_DEFAULT_SERVER_PORT;    
}

/*******************************************************************************
 * GetConfiguration: parse command line
 *******************************************************************************
 * Parse command line and configuration file for configuration. If the inline
 * help is requested, the function Usage() is called and the function returns
 * -1 (causing main() to exit). Note than messages interface is initialized at
 * this stage.
 *******************************************************************************/
static int GetConfiguration( program_data_t *p_data, int i_argc, 
                             char *ppsz_argv[], char *ppsz_env[] )
{
    int c, i_err;

    /* Set default configuration and copy arguments */
    p_data->i_argc = i_argc;
    p_data->ppsz_argv = ppsz_argv;
    p_data->ppsz_env = ppsz_env;    
    SetDefaultConfiguration( p_data );

    /* Parse command line */
    opterr = 0;
    while( ( c = getopt_long( i_argc, ppsz_argv, psz_shortopts, longopts, 0 ) ) != EOF )
    {
        switch( c )
        {
        /* General/common options */   
        case 'h':                                                /* -h, --help */
            Usage();
            return( -1 );
            break;
        case OPT_DISPLAY:                                         /* --display */
            p_data->vout_cfg.psz_display = optarg;
            p_data->vout_cfg.i_properties |= VIDEO_CFG_DISPLAY;
            p_data->intf_thread.xconsole.psz_display = optarg;
            break;

        /* Interface options */
        case OPT_CONSOLE_DISPLAY:                         /* --console-display */
            p_data->intf_thread.xconsole.psz_display = optarg;
            break;
        case OPT_CONSOLE_GEOMETRY:                       /* --console-geometry */
            p_data->intf_thread.xconsole.psz_geometry = optarg;
            break;

        /* Audio options */
        case OPT_NOAUDIO:                                        /* --noaudio */
	    p_data->cfg.b_audio = 0;
            break;
        case OPT_STEREO:                                          /* --stereo */
	    p_data->aout_thread.dsp.b_stereo = 1;
            break;
        case OPT_MONO:                                              /* --mono */
	    p_data->aout_thread.dsp.b_stereo = 0;
            break;
        case OPT_RATE:                                              /* --rate */
	    p_data->aout_thread.dsp.l_rate = GetIntParam(optarg, AOUT_MIN_RATE, AOUT_MAX_RATE, &i_err );
            if( i_err )
            {
                return( EINVAL );
	    }
            break;

        /* Video options */
        case OPT_NOVIDEO:                                         /* --novideo */
            p_data->cfg.b_video = 0;
            break;       
        case OPT_XSHM:                                               /* --xshm */
            p_data->vout_cfg.b_shm_ext = 1;
            p_data->vout_cfg.i_properties |= VIDEO_CFG_SHM_EXT;
            break;
        case OPT_NOXSHM:                                           /* --noxshm */
            p_data->vout_cfg.b_shm_ext = 0;
            p_data->vout_cfg.i_properties |= VIDEO_CFG_SHM_EXT;
            break;

        /* VLAN management options */
        case OPT_NOVLANS:                                         /* --novlans */
            p_data->cfg.b_vlans = 0;
            break;
        case  OPT_VLAN_SERVER:                                 /* --vlanserver */
            p_data->cfg.i_input_vlan_server_port = ServerPort( optarg );
            p_data->cfg.psz_input_vlan_server = optarg;
            break;
	    
        /* Internal error: unknown option */
        case '?':                          
        default:
            intf_ErrMsg("intf error: unknown option '%s'\n", ppsz_argv[optind - 1]);
            return( EINVAL );
            break;
        }
    }

    return( 0 );
}

/*******************************************************************************
 * Usage: print program usage
 *******************************************************************************
 * Print a short inline help. Message interface is initialized at this stage.
 *******************************************************************************/
static void Usage( void )
{
    intf_Msg(COPYRIGHT_MESSAGE);
    /* General options */
    intf_Msg("usage: vlc [options...]\n" \
             "  -h, --help                      print usage\n" \
             );
    /* Audio options */
    intf_Msg("  --noaudio                       disable audio\n" \
             "  --stereo                        enable stereo\n" \
             "  --mono                          disable stereo\n"
             "  --rate <rate>                   audio output rate (kHz)\n" \
             );
    /* Video options */
    intf_Msg("  --novideo                       disable video\n" \
             "  --xshm, --noxshm                enable/disable use of XShm extension\n" \
             "  -d, --display <display>         set display name\n" \
             );

    /* VLAN management options */
    intf_Msg("  --novlans		                disable vlans\n" \
             "  --vlanserver <server[:port]>	set vlan server address\n" \
             );
}

/*******************************************************************************
 * GetIntParam: convert a string to an integer
 *******************************************************************************
 * This function convert a string to an integer, check range of the value
 * and that there is no error during convertion. pi_err is a pointer to an
 * error flag, which contains non 0 on error. This function prints its own
 * error messages.
 *******************************************************************************/
static long GetIntParam( const char *psz_param, long i_min, long i_max, int *pi_err )
{
    char *psz_endptr;
    long int i_value;

    i_value = strtol( psz_param, &psz_endptr, 0 );
    if( (psz_param[0] == '\0') && (*psz_endptr != '\0') )  /* conversion error */
    {
        intf_ErrMsg("intf error: conversion error ('%s' should be an integer between %ld and %ld)\n",
                    psz_param, i_min, i_max );
        *pi_err = EINVAL;
        return( 0 );
    }
    if( (i_value < i_min) || (i_value > i_max) )                /* range error */
    {
        intf_ErrMsg("intf error: range error ('%s' should be an integer between %ld and %ld)\n",
                    psz_param, i_min, i_max );
        *pi_err = EINVAL;
        return( 0 );
    }
    *pi_err = 0;
    return( i_value );
}
/*******************************************************************************
 * InitSignalHandler: system signal handler initialization
 *******************************************************************************
 * Set the signal handlers. SIGTERM is not intercepted, because we need at
 * at least a method to kill the program when all other methods failed, and 
 * when we don't want to use SIGKILL.
 *******************************************************************************/
static void InitSignalHandler( void )
{
  /* Termination signals */
  signal( SIGHUP, SignalHandler );
  signal( SIGINT, SignalHandler );
  signal( SIGQUIT, SignalHandler );
}

/*******************************************************************************
 * SignalHandler: system signal handler
 *******************************************************************************
 * This function is called when a signal is received by the program. It ignores
 * it or tries to terminate cleanly.
 *******************************************************************************/
static void SignalHandler( int i_signal )
{
  /* Once a signal has been trapped, the signal handler needs to be re-armed on
   * linux systems */
  signal( i_signal, SignalHandler );

  /* Acknowledge the signal received */
  intf_ErrMsgImm("intf: signal %d received\n", i_signal );

  /* Try to terminate everything */
  /* ?? this probably needs to be changed */
462
  p_program_data->intf_thread.b_die = 1;
Michel Kaempf's avatar
Michel Kaempf committed
463
}