Logo Search packages:      
Sourcecode: pwlib version File versions  Download package

void VidTest::Main (  ) [virtual]

User override function for the main execution routine of the thread. A descendent class must provide the code that will be executed in the thread within this function.

Note that the correct way for a thread to terminate is to return from this function.

Implements PThread.

Definition at line 113 of file main.cxx.

References PString::AsInteger(), PTrace::Blocks, PThread::Create(), PVideoOutputDevice::CreateDevice(), PVideoInputDevice::CreateDevice(), PVideoOutputDevice::CreateDeviceByName(), PVideoInputDevice::CreateDeviceByName(), PTrace::FileAndLine, PProcess::GetArguments(), PVideoDevice::GetChannel(), PVideoDevice::GetColourFormat(), PVideoDevice::GetDeviceName(), PVideoOutputDevice::GetDriverNames(), PVideoInputDevice::GetDriverNames(), PVideoOutputDevice::GetDriversDeviceNames(), PVideoInputDevice::GetDriversDeviceNames(), PVideoDevice::GetFrameHeight(), PVideoDevice::GetFrameRate(), PVideoDevice::GetFrameWidth(), PArgList::GetOptionCount(), PArgList::GetOptionString(), PVideoDevice::GetVFlipState(), PVideoDevice::GetVideoFormat(), PArgList::HasOption(), PTrace::Initialise(), PString::IsEmpty(), PThread::NoAutoDeleteThread, PThread::NormalPriority, PVideoDevice::Open(), PVideoInputDevice::Open(), PArgList::Parse(), PVideoDevice::SetChannel(), PVideoDevice::SetColourFormatConverter(), PVideoDevice::SetFrameRate(), PVideoDevice::SetFrameSizeConverter(), PVideoDevice::SetVFlipState(), PVideoDevice::SetVideoFormat(), PSemaphore::Signal(), PSyncPointAck::Signal(), PTrace::Thread, PTrace::Timestamp, and PString::ToUpper().

{
  PArgList & args = GetArguments();

  args.Parse("h-help."
             "-input-driver:"
             "I-input-device:"
             "-input-format:"
             "-input-channel:"
             "-output-driver:"
             "O-output-device:"
             "F-colour-format:"
             "S-frame-size:"
             "R-frame-rate:"
#if PTRACING
             "o-output:"             "-no-output."
             "t-trace."              "-no-trace."
#endif
       );

#if PTRACING
  PTrace::Initialise(args.GetOptionCount('t'),
                     args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL,
         PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
#endif

  if (args.HasOption('h')) {
    PError << "Available options are: " << endl
           << endl
           <<    "--help                : print this help message.\n"
           <<    "--input-driver  drv   : video grabber driver.\n"
           <<    "--input-device  dev   : video grabber device.\n"
           <<    "--input-format  fmt   : video grabber format (\"pal\"/\"ntsc\")\n"
           <<    "--input-channel num   : video grabber channel.\n"
           <<    "--output-driver drv   : video display driver to use.\n"
           <<    "--output-device dev   : video display device to use.\n"
           <<    "--colour-format fmt   : video colour size (\"rgb24\", \"yuv420\", etc)\n"
           <<    "--frame-size size     : video frame size (\"qcif\", \"cif\", WxH)\n"
           <<    "--frame-rate size     : video frame rate (frames/second)\n"
#if PTRACING
           <<    "-o or --output file   : file name for output of log messages\n"       
           <<    "-t or --trace         : degree of verbosity in error log (more times for more detail)\n"     
#endif
           << endl
           << " e.g. ./vidtest --videodevice Philips\\ 680\\ webcam --videosize small " << endl << endl;
    return;
  }

  PINDEX i;

  PString inputDriverName = args.GetOptionString("input-driver");
  if (!inputDriverName.IsEmpty()) {
    grabber = PVideoInputDevice::CreateDevice(inputDriverName);
    if (grabber == NULL) {
      cerr << "Cannot use input driver name \"" << inputDriverName << "\", must be one of:\n";
      PStringList drivers = PVideoInputDevice::GetDriverNames();
      for (i = 0; i < drivers.GetSize(); i++)
        cerr << "  " << drivers[i] << '\n';
      cerr << endl;
      return;
    }
  }

  PStringList devices = PVideoInputDevice::GetDriversDeviceNames(inputDriverName);
  if (devices.IsEmpty()) {
    cerr << "No video input devices present";
    if (!inputDriverName.IsEmpty())
      cerr << " for driver \"" << inputDriverName << '"';
    cerr << endl;
    return;
  }

  PString inputDeviceName = args.GetOptionString("input-device");
  if (inputDeviceName.IsEmpty())
    inputDeviceName = devices[0];
    
  if (grabber == NULL)
    grabber = PVideoInputDevice::CreateDeviceByName(inputDeviceName);

  if (grabber == NULL || !grabber->Open(inputDeviceName, false)) {
    cerr << "Cannot use input device name \"" << inputDeviceName << "\", must be one of:\n";
    for (i = 0; i < devices.GetSize(); i++)
      cerr << "  " << devices[i] << '\n';
    cerr << endl;
    return;
  }

  cout << "Grabber ";
  if (!inputDriverName.IsEmpty())
    cout << "driver \"" << inputDriverName << "\" and ";
  cout << "device \"" << grabber->GetDeviceName() << "\" opened." << endl;

  if (args.HasOption("input-format")) {
    PVideoDevice::VideoFormat format;
    PCaselessString formatString = args.GetOptionString("input-format");
    if (formatString == "PAL")
      format = PVideoDevice::PAL;
    else if (formatString == "NTSC")
      format = PVideoDevice::NTSC;
    else if (formatString == "SECAM")
      format = PVideoDevice::SECAM;
    else if (formatString == "Auto")
      format = PVideoDevice::Auto;
    else {
      cerr << "Illegal video input format name \"" << formatString << '"' << endl;
    return;
  }
    if (!grabber->SetVideoFormat(format)) {
      cerr << "Video input device could not be set to format \"" << formatString << '"' << endl;
      return;
    }
  }
  cout << "Grabber input format set to " << grabber->GetVideoFormat() << endl;

  if (args.HasOption("input-channel")) {
    int videoInput = args.GetOptionString("input-channel").AsInteger();
    if (!grabber->SetChannel(videoInput)) {
      cerr << "Video input device could not be set to channel " << videoInput << endl;
    return;
  }
  }
  cout << "Grabber input channel set to " << grabber->GetChannel() << endl;

  
  int frameRate;
  if (args.HasOption("frame-rate"))
    frameRate = args.GetOptionString("frame-rate").AsInteger();
  else
    frameRate = 15;

  if (!grabber->SetFrameRate(frameRate)) {
    cerr << "Video input device could not be set to frame rate " << frameRate << endl;
    return;
  }
  cout << "Grabber frame rate set to " << grabber->GetFrameRate() << endl;


  PString outputDriverName = args.GetOptionString("output-driver");
  if (!outputDriverName.IsEmpty()) {
    display = PVideoOutputDevice::CreateDevice(outputDriverName);
    if (display == NULL) {
      cerr << "Cannot use output driver name \"" << inputDriverName << "\", must be one of:\n";
      PStringList drivers = PVideoOutputDevice::GetDriverNames();
      for (i = 0; i < drivers.GetSize(); i++)
        cerr << "  " << drivers[i] << '\n';
      cerr << endl;
    return;
    }
  }

  devices = PVideoOutputDevice::GetDriversDeviceNames(outputDriverName);
  if (devices.IsEmpty()) {
    cerr << "No video output devices present";
    if (!outputDriverName.IsEmpty())
      cerr << " for driver \"" << outputDriverName << '"';
    cerr << endl;
    return;
  }

  PString outputDeviceName = args.GetOptionString("output-device");
  if (outputDeviceName.IsEmpty()) {
    outputDeviceName = devices[0];
    if (outputDeviceName == "NULL" && devices.GetSize() > 1)
      outputDeviceName = devices[1];
  }

  if (display == NULL)
    display = PVideoOutputDevice::CreateDeviceByName(outputDeviceName);

  if (display == NULL || !display->Open(outputDeviceName, false)) {
    cerr << "Cannot use output device name \"" << outputDeviceName << "\", must be one of:\n";
    for (i = 0; i < devices.GetSize(); i++)
      cerr << "  " << devices[i] << '\n';
    cerr << endl;
    return;
  }

  cout << "Display ";
  if (!outputDriverName.IsEmpty())
    cout << "driver \"" << outputDriverName << "\" and ";
  cout << "device \"" << display->GetDeviceName() << "\" opened." << endl;


  unsigned width, height;
  if (args.HasOption("frame-size")) {
    PString sizeString = args.GetOptionString("frame-size");
    if (!ParseSize(sizeString, width, height)) {
      cerr << "Illegal video frame size \"" << sizeString << '"' << endl;
      return;
    }
  }
  else {
    width = PVideoDevice::QCIFWidth;
    height = PVideoDevice::QCIFHeight;
  }
  
  if  (!grabber->SetFrameSizeConverter(width, height, FALSE)) {
    cerr << "Video input device could not be set to size " << width << 'x' << height << endl;
    return;
  }
  cout << "Grabber frame size set to " << grabber->GetFrameWidth() << 'x' << grabber->GetFrameHeight() << endl;

  if  (!display->SetFrameSizeConverter(width, height, FALSE)) {
    cerr << "Video output device could not be set to size " << width << 'x' << height << endl;
    return;
  }

  cout << "Display frame size set to " << display->GetFrameWidth() << 'x' << display->GetFrameHeight() << endl;


  PCaselessString colourFormat = args.GetOptionString("colour-format", "RGB24").ToUpper();
  if (!grabber->SetColourFormatConverter(colourFormat) ) {
    cerr << "Video input device could not be set to colour format \"" << colourFormat << '"' << endl;
    return;
  }

  cout << "Grabber colour format set to " << grabber->GetColourFormat() << " (";
  if (colourFormat == grabber->GetColourFormat())
    cout << "native";
  else
    cout << "converted to " << colourFormat;
  cout << ')' << endl;

  if (!display->SetColourFormatConverter(colourFormat)) {
    cerr << "Video output device could not be set to colour format \"" << colourFormat << '"' << endl;
    return;
 }

  cout << "Diaplay colour format set to " << display->GetColourFormat() << " (";
  if (colourFormat == display->GetColourFormat())
    cout << "native";
  else
    cout << "converted from " << colourFormat;
  cout << ')' << endl;


  PThread::Create(PCREATE_NOTIFIER(GrabAndDisplay), 0,
                  PThread::NoAutoDeleteThread, PThread::NormalPriority,
                  "GrabAndDisplay");

  // command line
  for (;;) {

    // display the prompt
    cout << "vidtest> " << flush;
    PCaselessString cmd;
    cin >> cmd;

    if (cmd == "q" || cmd == "x" || cmd == "quit" || cmd == "exit")
      break;

    if (cmd == "fg") {
      if (!grabber->SetVFlipState(!grabber->GetVFlipState()))
        cout << "\nCould not toggle Vflip state of video input device" << endl;
      continue;
    }

    if (cmd == "fd") {
      if (!display->SetVFlipState(!display->GetVFlipState()))
        cout << "\nCould not toggle Vflip state of video output device" << endl;
      continue;
    }

    unsigned width, height;
    if (ParseSize(cmd, width, height)) {
      pauseGrabAndDisplay.Signal();
      if  (!grabber->SetFrameSizeConverter(width, height, FALSE))
        cout << "Video input device could not be set to size " << width << 'x' << height << endl;
      if  (!display->SetFrameSizeConverter(width, height, FALSE))
        cout << "Video output device could not be set to size " << width << 'x' << height << endl;
      resumeGrabAndDisplay.Signal();
      continue;
    }

    cout << "Select:\n"
            "  fg     : Flip video input top to bottom\n"
            "  fd     : Flip video output top to bottom\n"
            "  qcif   : Set size of grab & display to qcif\n"
            "  cif    : Set size of grab & display to cif\n"
            "  WxH    : Set size of grab & display W by H\n"
            "  Q or X : Exit program\n" << endl;
  } // end for

  cout << "Exiting." << endl;
  exitGrabAndDisplay.Signal();
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index