00001 #include <pthread.h> 00002 #include <semaphore.h> 00003 #include <time.h> 00004 00005 #include <FCam/Event.h> 00006 #include <FCam/Action.h> 00007 #include <FCam/Dummy/Sensor.h> 00008 #include <FCam/Dummy/Platform.h> 00009 00010 00011 #include "Daemon.h" 00012 #include "../Debug.h" 00013 00014 00015 namespace FCam { 00016 namespace Dummy { 00017 00018 Sensor::Sensor(): FCam::Sensor(), daemon(NULL), shotsPending_(0) { 00019 dprintf(DBG_MINOR, "Initializing dummy simulator sensor.\n"); 00020 pthread_mutex_init(&requestMutex, NULL); 00021 } 00022 00023 Sensor::~Sensor() { 00024 dprintf(DBG_MINOR, "Destroying dummy simulator sensor.\n"); 00025 stop(); 00026 pthread_mutex_destroy(&requestMutex); 00027 } 00028 00029 void Sensor::capture(const FCam::Shot &s) { 00030 Shot dummyShot(s); 00031 dummyShot.id = s.id; 00032 capture(dummyShot); 00033 } 00034 00035 void Sensor::capture(const Shot &shot) { 00036 dprintf(DBG_MINOR, "Queuing capture request.\n"); 00037 start(); 00038 00039 _Frame *f = new _Frame; 00040 f->_shot = shot; 00041 f->_shot.id = shot.id; 00042 00043 pthread_mutex_lock(&requestMutex); 00044 shotsPending_++; 00045 daemon->requestQueue.push(f); 00046 pthread_mutex_unlock(&requestMutex); 00047 00048 daemon->launchThreads(); 00049 } 00050 00051 void Sensor::capture(const std::vector<FCam::Shot> &burst) { 00052 std::vector<Shot> dummyBurst; 00053 dummyBurst.reserve(burst.size()); 00054 for (unsigned int i=0; i < burst.size(); i++) { 00055 dummyBurst.push_back(Shot(burst[i])); 00056 dummyBurst[i].id = burst[i].id; 00057 } 00058 capture(dummyBurst); 00059 } 00060 00061 void Sensor::capture(const std::vector<Shot> &burst) { 00062 dprintf(DBG_MINOR, "Queuing capture request burst.\n"); 00063 start(); 00064 00065 std::vector<_Frame *> frames; 00066 00067 for (size_t i=0; i < burst.size(); i++) { 00068 _Frame *f = new _Frame; 00069 f->_shot = burst[i]; 00070 f->_shot.id = burst[i].id; 00071 00072 frames.push_back(f); 00073 } 00074 00075 pthread_mutex_lock(&requestMutex); 00076 for (size_t i=0; i < frames.size(); i++) { 00077 shotsPending_++; 00078 daemon->requestQueue.push(frames[i]); 00079 } 00080 pthread_mutex_unlock(&requestMutex); 00081 00082 daemon->launchThreads(); 00083 } 00084 00085 void Sensor::stream(const FCam::Shot &s) { 00086 Shot dummyShot(s); 00087 dummyShot.id = s.id; 00088 stream(dummyShot); 00089 } 00090 00091 void Sensor::stream(const Shot &shot) { 00092 dprintf(DBG_MINOR, "Configuring streaming shot.\n"); 00093 pthread_mutex_lock(&requestMutex); 00094 streamingShot.clear(); 00095 streamingShot.push_back(shot); 00096 streamingShot[0].id = shot.id; 00097 pthread_mutex_unlock(&requestMutex); 00098 00099 start(); 00100 if (daemon->requestQueue.size() == 0) { capture(streamingShot); } 00101 } 00102 00103 void Sensor::stream(const std::vector<FCam::Shot> &burst) { 00104 std::vector<Shot> dummyBurst; 00105 dummyBurst.reserve(burst.size()); 00106 for (unsigned int i=0; i < burst.size(); i++) { 00107 dummyBurst.push_back(burst[i]); 00108 dummyBurst[i].id = burst[i].id; 00109 } 00110 stream(dummyBurst); 00111 } 00112 00113 void Sensor::stream(const std::vector<Shot> &burst) { 00114 dprintf(DBG_MINOR, "Configuring streaming burst.\n"); 00115 pthread_mutex_lock(&requestMutex); 00116 streamingShot = burst; 00117 for (size_t i=0; i < burst.size(); i++) { 00118 streamingShot[i].id = burst[i].id; 00119 } 00120 pthread_mutex_unlock(&requestMutex); 00121 00122 start(); 00123 if (daemon->requestQueue.size() == 0) { capture(streamingShot); } 00124 } 00125 00126 bool Sensor::streaming() { 00127 return streamingShot.size() > 0; 00128 } 00129 00130 void Sensor::stopStreaming() { 00131 dprintf(DBG_MINOR, "Stopping streaming.\n"); 00132 pthread_mutex_lock(&requestMutex); 00133 streamingShot.clear(); 00134 pthread_mutex_unlock(&requestMutex); 00135 } 00136 00137 void Sensor::start() { 00138 dprintf(4, "Creating and launching daemon.\n"); 00139 if (daemon) { return; } 00140 daemon = new Daemon(this); 00141 if (streamingShot.size()) { daemon->launchThreads(); } 00142 dprintf(4, "Daemon created.\n"); 00143 } 00144 00145 void Sensor::stop() { 00146 dprintf(4, "Destroying daemon.\n"); 00147 if (!daemon) { return; } 00148 delete daemon; 00149 daemon = NULL; 00150 } 00151 00152 FCam::Dummy::Frame Sensor::getFrame() { 00153 dprintf(DBG_MINOR, "Getting a frame.\n"); 00154 if (!daemon) { 00155 Frame invalid; 00156 error(Event::SensorStoppedError, this, "Can't request frame before calling capture or stream\n"); 00157 return invalid; 00158 } 00159 00160 _Frame *_f; 00161 _f = daemon->frameQueue.pull(); 00162 00163 Frame frame(_f); 00164 00165 shotsPending_--; 00166 00167 dprintf(DBG_MINOR, "Frame received.\n"); 00168 return frame; 00169 } 00170 00171 int Sensor::rollingShutterTime(const FCam::Shot &s) const { 00172 return 0; 00173 } 00174 00175 void Sensor::generateRequest() { 00176 dprintf(4, "GenerateRequest called by daemon.\n"); 00177 pthread_mutex_lock(&requestMutex); 00178 if (streamingShot.size()) { 00179 for (size_t i = 0; i < streamingShot.size(); i++) { 00180 _Frame *f = new _Frame; 00181 f->_shot = streamingShot[i]; 00182 f->_shot.id = streamingShot[i].id; 00183 shotsPending_++; 00184 daemon->requestQueue.push(f); 00185 } 00186 } 00187 pthread_mutex_unlock(&requestMutex); 00188 } 00189 00190 00191 void Sensor::enforceDropPolicy() { 00192 00193 } 00194 00195 int Sensor::framesPending() const { 00196 if (!daemon) { return 0; } 00197 return daemon->frameQueue.size(); 00198 } 00199 00200 int Sensor::shotsPending() const { 00201 return shotsPending_; 00202 } 00203 00204 } 00205 }