• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/N900/Daemon.h

00001 #ifndef FCAM_N900_DAEMON_H
00002 #define FCAM_N900_DAEMON_H
00003 
00004 #include <queue>
00005 #include <pthread.h>
00006 #include <semaphore.h>
00007 
00008 #include "FCam/Frame.h"
00009 #include "FCam/N900/Sensor.h"
00010 #include "FCam/TSQueue.h"
00011 #include "FCam/N900/Frame.h"
00012 
00013 #include "V4L2Sensor.h"
00014 
00015 
00016 
00017 namespace FCam { namespace N900 {
00018 
00019     // The daemon acts as a layer over /dev/video0. It accepts frame
00020     // requests and returns frames that (hopefully) meet those requests.
00021     class Daemon {
00022     public:
00023         // A class for actions slaved to a frame
00024         class Action {
00025         public:
00026             // When should this action run?
00027             // The run-time will set this value when it knows when the
00028             // start of the associated exposure is
00029             Time time;
00030             FCam::Action *action;
00031                 
00032             bool operator<(const Action &other) const {
00033                 // I'm lower priority than another action if I occur later
00034                 return time > other.time;
00035             }
00036                 
00037             bool operator>(const Action &other) const {
00038                 // I'm higher priority than another action if I occur earlier
00039                 return time < other.time;
00040             }
00041         };
00042             
00043             
00044         Daemon(Sensor *sensor);
00045         ~Daemon();
00046             
00047         // enforce a drop policy on the frame queue
00048         void setDropPolicy(Sensor::DropPolicy p, int f);
00049 
00050         // The user-space puts partially constructed frames on this
00051         // queue. It is consumed by the setter thread.
00052         TSQueue<_Frame *> requestQueue;
00053 
00054         // The handler thread puts mostly constructed frames on this
00055         // queue. It is consumed by user-space.
00056         TSQueue<_Frame *> frameQueue;
00057 
00058         void launchThreads();
00059 
00060     private:
00061 
00062         // Access to the V4L2 layer of the sensor
00063         V4L2Sensor *v4l2Sensor;
00064 
00065         // Access to the FCam sensor object
00066         Sensor *sensor;
00067 
00068         bool stop;
00069 
00070         // The frameQueue may not grow beyond this limit
00071         size_t frameLimit;
00072         // What should I do if the frame policy tries to grow beyond the limit
00073         Sensor::DropPolicy dropPolicy;
00074         void enforceDropPolicy();   
00075 
00076         // The setter thread puts in flight requests on this queue, which
00077         // is consumed by the handler thread
00078         TSQueue<_Frame *> inFlightQueue;
00079             
00080         // Sometimes the setter needs to access the camera exclusively
00081         // (e.g. to do a pipeline flush). This mutex and flag are used for
00082         // coordinating such things.
00083         pthread_mutex_t cameraMutex;
00084         // The setter thread requests a pipeline flush by setting this
00085         // flag high and waiting on the above mutex.
00086         bool pipelineFlush;
00087 
00088         // The setter thread also queues up RT actions on this priority
00089         // queue, which is consumed by the actions thread
00090         std::priority_queue<Action> actionQueue;
00091         pthread_mutex_t actionQueueMutex;
00092         sem_t actionQueueSemaphore;
00093 
00094         // The component of the daemon that sets exposure and gain
00095         void runSetter();   
00096         pthread_t setterThread;
00097         void tickSetter(Time hs_vs);
00098         bool setterRunning;
00099         // The current state of the camera
00100         _Frame current;
00101         Shot lastGoodShot;
00102 
00103         // The component that handles frames returned by V4L2
00104         void runHandler();
00105         pthread_t handlerThread;
00106         bool handlerRunning;
00107 
00108         // The component that executes RT actions
00109         void runAction();
00110         pthread_t actionThread;
00111         bool actionRunning;
00112 
00113         int daemon_fd;
00114 
00115         // Have the threads been launched?
00116         bool threadsLaunched;
00117 
00118         friend void *daemon_setter_thread_(void *arg);
00119         friend void *daemon_handler_thread_(void *arg);
00120         friend void *daemon_action_thread_(void *arg);
00121     };
00122 
00123 }
00124 }
00125 
00126 #endif

Generated on Fri Sep 24 2010 15:53:00 for FCam by  doxygen 1.7.1