Streamer.C

Go to the documentation of this file.
00001 /*!@file Media/Streamer.C Base class for simple input-output streaming applications */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the 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: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Media/Streamer.C $
00035 // $Id: Streamer.C 9841 2008-06-20 22:05:12Z lior $
00036 //
00037 
00038 #ifndef MEDIA_STREAMER_C_DEFINED
00039 #define MEDIA_STREAMER_C_DEFINED
00040 
00041 #include "Media/Streamer.H"
00042 
00043 #include "Component/ModelManager.H"
00044 #include "Media/FrameSeries.H"
00045 #include "Raster/GenericFrame.H"
00046 #include "Raster/Raster.H"
00047 #include "Util/Pause.H"
00048 #include "Util/csignals.H"
00049 
00050 // ######################################################################
00051 Streamer::Streamer(const char* name)
00052   :
00053   itsManager(new ModelManager(name)),
00054   itsIfs(new InputFrameSeries(*itsManager)),
00055   itsOfs(new OutputFrameSeries(*itsManager))
00056 {
00057   itsManager->addSubComponent(itsIfs);
00058   itsManager->addSubComponent(itsOfs);
00059 }
00060 
00061 // ######################################################################
00062 Streamer::~Streamer()
00063 {}
00064 
00065 // ######################################################################
00066 int Streamer::run(const int argc, const char** argv,
00067                   const char* extraArgsDescription,
00068                   const int minExtraArgs, const int maxExtraArgs)
00069 {
00070   try
00071     {
00072       return this->tryRun(argc, argv, extraArgsDescription,
00073                           minExtraArgs, maxExtraArgs);
00074     }
00075   catch (...)
00076     {
00077       REPORT_CURRENT_EXCEPTION;
00078     }
00079 
00080   return 1;
00081 }
00082 
00083 // ######################################################################
00084 int Streamer::tryRun(const int argc, const char** argv,
00085                      const char* extraArgsDescription,
00086                      const int minExtraArgs, const int maxExtraArgs)
00087 {
00088   volatile int signum = 0;
00089   catchsignals(&signum);
00090 
00091   if (itsManager->parseCommandLine(argc, argv, extraArgsDescription,
00092                                    minExtraArgs, maxExtraArgs) == false)
00093     return(1);
00094 
00095   this->handleExtraArgs(*itsManager);
00096 
00097   itsManager->start();
00098 
00099   itsIfs->startStream();
00100 
00101   int c = 0;
00102 
00103   PauseWaiter p;
00104 
00105   while (true)
00106     {
00107       if (signum != 0)
00108         {
00109           LINFO("quitting because %s was caught", signame(signum));
00110           return -1;
00111         }
00112 
00113       if (itsOfs->becameVoid())
00114         {
00115           LINFO("quitting because output stream was closed or became void");
00116           return 0;
00117         }
00118 
00119       if (p.checkPause())
00120         continue;
00121 
00122       const FrameState is = itsIfs->updateNext();
00123       if (is == FRAME_COMPLETE)
00124         break;
00125 
00126       GenericFrame input = itsIfs->readFrame();
00127       if (!input.initialized())
00128         break;
00129 
00130       const FrameState os = itsOfs->updateNext();
00131 
00132       this->onFrame(input, *itsOfs, itsIfs->frame());
00133 
00134       if (os == FRAME_FINAL)
00135         break;
00136 
00137       LDEBUG("frame %d", c++);
00138 
00139       if (itsIfs->shouldWait() || itsOfs->shouldWait())
00140         Raster::waitForKey();
00141     }
00142 
00143   itsManager->stop();
00144 
00145   return 0;
00146 }
00147 
00148 // ######################################################################
00149 void Streamer::addComponent(const nub::ref<ModelComponent>& c)
00150 {
00151   itsManager->addSubComponent(c);
00152 }
00153 
00154 // ######################################################################
00155 OptionManager& Streamer::getManager()
00156 {
00157   return *itsManager;
00158 }
00159 
00160 // ######################################################################
00161 void Streamer::handleExtraArgs(const ModelManager& mgr)
00162 {
00163   // default implementation is a no-op
00164 }
00165 
00166 // ######################################################################
00167 /* So things look consistent in everyone's emacs... */
00168 /* Local Variables: */
00169 /* mode: c++ */
00170 /* indent-tabs-mode: nil */
00171 /* End: */
00172 
00173 #endif // MEDIA_STREAMER_C_DEFINED
Generated on Sun May 8 08:05:20 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3