sdlgrab.C

Go to the documentation of this file.
00001 /*!@file AppPsycho/sdlgrab.C Test framegrabber and SDL overlay display */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/sdlgrab.C $
00035 // $Id: sdlgrab.C 7241 2006-10-04 23:13:30Z rjpeters $
00036 //
00037 
00038 #include "Component/ComponentOpts.H"
00039 #include "Component/ModelManager.H"
00040 #include "Devices/DeviceOpts.H"
00041 #include "Devices/FrameGrabberConfigurator.H"
00042 #include "Image/Image.H"
00043 #include "Psycho/PsychoDisplay.H"
00044 #include "Raster/GenericFrame.H"
00045 #include "Transport/FrameIstream.H"
00046 #include "Util/AllocAux.H"
00047 #include "Util/Types.H"
00048 #include "Util/csignals.H"
00049 #include "Video/VideoFrame.H"
00050 #include "rutz/time.h"
00051 
00052 #include <SDL/SDL.h>
00053 #include <unistd.h> // for sync()
00054 
00055 //! Test framebrabber and SDL overlay display
00056 int submain(const int argc, char** argv)
00057 {
00058   MYLOGVERB = LOG_INFO;  // suppress debug messages
00059 
00060   // 'volatile' because we will modify this from signal handlers
00061   volatile int signum = 0;
00062   catchsignals(&signum);
00063 
00064   // Instantiate a ModelManager:
00065   ModelManager manager("SDLgrab");
00066 
00067   // Instantiate our various ModelComponents:
00068   nub::soft_ref<FrameGrabberConfigurator>
00069     fgc(new FrameGrabberConfigurator(manager));
00070   manager.addSubComponent(fgc);
00071 
00072   nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00073   manager.addSubComponent(d);
00074 
00075   nub::soft_ref<EventLog> el(new EventLog(manager));
00076   manager.addSubComponent(el);
00077 
00078   d->setEventLog(el);
00079 
00080   // select a V4L grabber in 640x480 YUV420P by default:
00081   manager.exportOptions(MC_RECURSE);
00082   manager.setOptionValString(&OPT_FrameGrabberType, "V4L");
00083   manager.setOptionValString(&OPT_FrameGrabberMode, "UYVY");
00084   manager.setOptionValString(&OPT_FrameGrabberDims, "640x480");
00085   manager.setOptionValString(&OPT_DeinterlacerType, "Bob");
00086 
00087   manager.setOptionValString(&OPT_EventLogFileName, "psychodata.psy");
00088 
00089   // Parse command-line:
00090   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false)
00091     return 1;
00092 
00093   // do a few post-command-line configs:
00094   nub::soft_ref<FrameIstream> gb = fgc -> getFrameGrabber();
00095 
00096   // let's get all our ModelComponent instances started:
00097   manager.start();
00098 
00099   d->setDesiredRefreshDelayUsec(gb->getNaturalFrameTime().usecs(), 0.2F);
00100 
00101   // main loop:
00102   while (1)
00103     {
00104       // give a chance to other processes (useful on single-CPU machines):
00105       usleep(250000);
00106       sync();
00107 
00108       d->clearScreen();
00109       d->displayText("[SPACE] start/stop - [ESC] quit");
00110 
00111       while (1)
00112         {
00113           const char key = d->checkForKey();
00114           if (key == ' ') break;
00115           else
00116             {
00117               if (signum != 0)
00118                 {
00119                   LINFO("quitting because %s was caught", signame(signum));
00120                   return -1;
00121                 }
00122 
00123               usleep(10000);
00124             }
00125         }
00126 
00127       // create an overlay:
00128       d->createVideoOverlay(gb->peekFrameSpec().videoFormat);
00129 
00130       // get the frame grabber to start streaming:
00131       gb->startStream();
00132 
00133       // grab, display:
00134       bool keepgoing = true; int frame = 0;
00135       rutz::time start = rutz::time::wall_clock_now();
00136       while (keepgoing)
00137         {
00138           if (signum != 0)
00139             {
00140               LINFO("quitting because %s was caught", signame(signum));
00141               return -1;
00142             }
00143 
00144           // display the raw frame buffer as an overlay
00145           d->displayVideoOverlay(gb->readFrame().asVideo(),
00146                                  frame, SDLdisplay::NO_WAIT);
00147 
00148           ++frame;
00149 
00150           const char key = d->checkForKey();
00151 
00152           // stop if <SPACE> is pressed, and abort if <ESC> by using
00153           // the built-in ESC check in PsychoDisplay::checkForKey():
00154           if (key == ' ') keepgoing = false;
00155         }
00156       rutz::time stop = rutz::time::wall_clock_now();
00157 
00158       invt_allocation_show_stats();
00159 
00160       const double secs = (stop-start).sec();
00161       LINFO("%d frames in %.02f sec (~%.02f fps)", frame, secs, frame/secs);
00162 
00163       // destroy the overlay. Somehow, mixing overlay displays and
00164       // normal displays does not work. With a single overlay created
00165       // before this loop and never destroyed, the first video plays
00166       // ok but the other ones don't show up:
00167       d->destroyYUVoverlay();
00168       d->clearScreen();  // sometimes 2 clearScreen() are necessary
00169       d->clearScreen();  // sometimes 2 clearScreen() are necessary
00170     }
00171 
00172   // stop all our ModelComponents
00173   manager.stop();
00174 
00175   // all done!
00176   return 0;
00177 }
00178 
00179 extern "C" int main(const int argc, char** argv)
00180 {
00181   try
00182     {
00183       return submain(argc, argv);
00184     }
00185   catch (...)
00186     {
00187       REPORT_CURRENT_EXCEPTION;
00188     }
00189 
00190   return 1;
00191 }
00192 
00193 // ######################################################################
00194 /* So things look consistent in everyone's emacs... */
00195 /* Local Variables: */
00196 /* indent-tabs-mode: nil */
00197 /* End: */
Generated on Sun May 8 08:40:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3