[3] | 1 | /*********************************************************************
|
---|
| 2 | // created: 2007/04/12 - 16:30
|
---|
| 3 | //
|
---|
| 4 | // author: Elie Al Alam & Gerald Dherbomez
|
---|
| 5 | //
|
---|
| 6 | // version: $Id: DbtPlyEngine.cpp 1020 2012-12-19 09:39:40Z kurdejma $
|
---|
| 7 | //
|
---|
| 8 | // purpose: Dbite Player Engine implementation
|
---|
| 9 | *********************************************************************/
|
---|
| 10 |
|
---|
| 11 | #include "DbitePlayer/DbtPlyEngine.h"
|
---|
| 12 |
|
---|
| 13 | #include "kernel/ComponentManager.h"
|
---|
| 14 | #include "kernel/Log.h"
|
---|
| 15 |
|
---|
| 16 | #include <cassert>
|
---|
| 17 | #include <limits>
|
---|
| 18 | #include <QDir>
|
---|
| 19 |
|
---|
| 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | // Construction de la fabrique de composant DbtPlyEngine
|
---|
| 23 | //static ComponentFactory<DbtPlyEngine> factory("DbtPlyEngine");
|
---|
| 24 |
|
---|
| 25 | namespace pacpus {
|
---|
| 26 |
|
---|
| 27 | DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyEngine");
|
---|
| 28 |
|
---|
| 29 | static const string kPropertyDataDirectory = "datadir";
|
---|
| 30 |
|
---|
| 31 | static const string kPropertyLoggerConfiguration = "log-config-file";
|
---|
| 32 |
|
---|
| 33 | static const string kPropertyReplayMode = "replay_mode";
|
---|
| 34 | static const string kPropertyReplayModeLastData = "1";
|
---|
| 35 | static const string kPropertyReplayModeAllData = "2";
|
---|
| 36 |
|
---|
| 37 | typedef float SpeedType;
|
---|
| 38 | static const SpeedType kInitialSpeed = 1.0;
|
---|
| 39 | static const SpeedType kMinSpeed = 1.0 / 32;
|
---|
| 40 | static const SpeedType kMaxSpeed = 32;
|
---|
| 41 |
|
---|
| 42 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 43 | /// Constructor
|
---|
| 44 | DbtPlyEngine::DbtPlyEngine(QString name)
|
---|
| 45 | : ComponentBase(name)
|
---|
| 46 | , mCurrentState(StoppedState::getInstance())
|
---|
| 47 | , mSpeed(kInitialSpeed)
|
---|
| 48 | , mIsReverse(false)
|
---|
| 49 | {
|
---|
| 50 | LOG_TRACE("constructor");
|
---|
| 51 |
|
---|
| 52 | sync_ = new QSemaphore(1);
|
---|
| 53 | sync_->acquire();
|
---|
| 54 | direction_ = 1;
|
---|
| 55 | tMinMaxSem_ = new QSemaphore(1);
|
---|
| 56 | tMinMaxSem_->release();
|
---|
| 57 | tDbtMin_ = numeric_limits<road_time_t>::max();
|
---|
| 58 | tDbtMax_ = 0;
|
---|
| 59 | dataDir_ = QString::null;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 63 | /// Destructor.
|
---|
| 64 | DbtPlyEngine::~DbtPlyEngine()
|
---|
| 65 | {
|
---|
| 66 | LOG_TRACE("destructor");
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 70 | /// Returns the directory where the data are stored.
|
---|
| 71 | /// @return QString::null if directory is invalid.
|
---|
| 72 | QString DbtPlyEngine::getDataDir()
|
---|
| 73 | {
|
---|
| 74 | return dataDir_;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 78 | /// Slot.
|
---|
| 79 | /// called when trigger reaches a new timer event
|
---|
| 80 | void DbtPlyEngine::engReceiver()
|
---|
| 81 | {
|
---|
| 82 | sync_->release();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 86 | /// @returns @b true if the engine is in the PLAY_STATE, @b false otherwise
|
---|
| 87 | bool DbtPlyEngine::isPlaying()
|
---|
| 88 | {
|
---|
| 89 | return mCurrentState->isPlaying();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 93 | /// main loop of the thread
|
---|
| 94 | /// entry point for a new thread after the invocation of QThread::start()
|
---|
| 95 | void DbtPlyEngine::run()
|
---|
| 96 | {
|
---|
| 97 | LOG_DEBUG("run");
|
---|
| 98 |
|
---|
| 99 | // We are waiting the first trigger signal to init some values
|
---|
| 100 | // before entering in the while loop
|
---|
| 101 | sync_->acquire();
|
---|
| 102 | lastTDbt_ = tDbtMin_;
|
---|
| 103 |
|
---|
| 104 | // For statistics purpose
|
---|
| 105 | static const size_t kDeltaArraySize = 1000;
|
---|
| 106 | int deltaTDbtTab[kDeltaArraySize];
|
---|
| 107 | int i = 0;
|
---|
| 108 |
|
---|
| 109 | while (THREAD_ALIVE) {
|
---|
| 110 | tNow_ = road_time();
|
---|
| 111 | float elapsedTime = tNow_ - lastTNow_;
|
---|
| 112 | int deltaTDbt = direction_ * elapsedTime * mSpeed;
|
---|
| 113 | tDbt_ = lastTDbt_ + deltaTDbt;
|
---|
| 114 | deltaTDbtTab[(i++) % kDeltaArraySize] = elapsedTime;
|
---|
| 115 |
|
---|
| 116 | if ((tDbt_ <= tDbtMax_) && (tDbt_ >= tDbtMin_)) {
|
---|
| 117 | Q_EMIT play(tDbt_, tNow_, mIsReverse);
|
---|
| 118 | Q_EMIT curReplayTime(tDbt_);
|
---|
| 119 | lastTNow_ = tNow_ ;
|
---|
| 120 | lastTDbt_ = tDbt_ ;
|
---|
| 121 | } else {
|
---|
| 122 | lastTNow_ = tNow_ ;
|
---|
| 123 | lastTDbt_ = tDbt_ ;
|
---|
| 124 | LOG_INFO("Reading is finished.");
|
---|
| 125 |
|
---|
| 126 | mCurrentState->stop(*this);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | // The waiting is placed at the end due to the init before the while loop
|
---|
| 130 | sync_->acquire();
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 135 | /// Slot.
|
---|
| 136 | /// change the direction of replaying : normal or reverse
|
---|
| 137 | void DbtPlyEngine::changeDirection(bool reverse)
|
---|
| 138 | {
|
---|
| 139 | if (reverse) {
|
---|
| 140 | direction_ = -1;
|
---|
| 141 | mIsReverse = true;
|
---|
| 142 | } else {
|
---|
| 143 | direction_ = +1;
|
---|
| 144 | mIsReverse = false;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 149 | /// Configuration method of the engine
|
---|
| 150 | /// called automatically by the component manager
|
---|
| 151 | ComponentBase::COMPONENT_CONFIGURATION DbtPlyEngine::configureComponent(XmlComponentConfig /*config*/)
|
---|
| 152 | {
|
---|
| 153 | // datadir
|
---|
| 154 | dataDir_ = param.getProperty(kPropertyDataDirectory.c_str());
|
---|
| 155 | LOG_INFO("property " << kPropertyDataDirectory.c_str() << "=\""
|
---|
| 156 | << dataDir_ << "\"");
|
---|
| 157 | if (dataDir_.isNull()) {
|
---|
| 158 | LOG_FATAL("The data directory '" << componentName << "' is invalid or unavailable!");
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | dataDir_ = QDir::toNativeSeparators(dataDir_);
|
---|
| 162 | if (!dataDir_.endsWith(QDir::separator())) {
|
---|
| 163 | dataDir_ += QDir::separator();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 167 | // logger configuration
|
---|
| 168 | QString loggerConfig = param.getProperty(kPropertyLoggerConfiguration.c_str());
|
---|
| 169 | LOG_INFO("property " << kPropertyLoggerConfiguration.c_str() << "=\""
|
---|
| 170 | << loggerConfig << "\"");
|
---|
| 171 | if (!loggerConfig.isNull()) {
|
---|
| 172 | LOG_INFO("configuring logger with file '" << loggerConfig << "'");
|
---|
| 173 | LogConfigurator::configureLoggerWithFile(loggerConfig.toStdString().c_str());
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 177 | // Replay Mode
|
---|
| 178 | QString replayModeValue = param.getProperty(kPropertyReplayMode.c_str());
|
---|
| 179 | LOG_INFO("property " << kPropertyReplayMode.c_str() << "=\""
|
---|
| 180 | << replayModeValue << "\"");
|
---|
| 181 | if (replayModeValue.isNull()) {
|
---|
| 182 | LOG_INFO("property " << kPropertyReplayMode.c_str() << " unset."
|
---|
| 183 | << " Set to default = 1.");
|
---|
| 184 | replayMode_ = PlayModeLastData;
|
---|
| 185 | } else {
|
---|
| 186 | if (kPropertyReplayModeLastData == replayModeValue.toStdString()) {
|
---|
| 187 | replayMode_ = PlayModeLastData;
|
---|
| 188 | } else if (kPropertyReplayModeAllData == replayModeValue.toStdString()) {
|
---|
| 189 | replayMode_ = PlayModeAllData;
|
---|
| 190 | } else {
|
---|
| 191 | LOG_WARN("unknown " << kPropertyReplayMode.c_str() << " '" << replayModeValue << "'."
|
---|
| 192 | << " Set to default = 1.");
|
---|
| 193 | replayMode_ = PlayModeLastData;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | return ComponentBase::CONFIGURED_OK;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 201 | /// The start function of the engine
|
---|
| 202 | void DbtPlyEngine::startActivity()
|
---|
| 203 | {
|
---|
| 204 | LOG_INFO("Starting...");
|
---|
| 205 |
|
---|
| 206 | THREAD_ALIVE = true;
|
---|
| 207 | start();
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 211 | /// The stop function of the engine
|
---|
| 212 | void DbtPlyEngine::stopActivity()
|
---|
| 213 | {
|
---|
| 214 | LOG_TRACE("stopping activity...");
|
---|
| 215 |
|
---|
| 216 | THREAD_ALIVE = false;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 220 | /// Slot.
|
---|
| 221 | /// called by each file manager to provide min and max time of their data
|
---|
| 222 | void DbtPlyEngine::tMinMax(road_time_t tMin, road_time_t tMax)
|
---|
| 223 | {
|
---|
| 224 | tMinMaxSem_->acquire();
|
---|
| 225 | if (tDbtMin_>tMin) {
|
---|
| 226 | tDbtMin_= tMin;
|
---|
| 227 | }
|
---|
| 228 | if (tDbtMax_<tMax) {
|
---|
| 229 | tDbtMax_ = tMax;
|
---|
| 230 | }
|
---|
| 231 | Q_EMIT timeMinMax(tDbtMin_, tDbtMax_);
|
---|
| 232 | tMinMaxSem_->release();
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 236 | /// Slot.
|
---|
| 237 | /// called when the time slider is pressed
|
---|
| 238 | void DbtPlyEngine::pauseEvent()
|
---|
| 239 | {
|
---|
| 240 | LOG_DEBUG("Clicked: pause");
|
---|
| 241 | mCurrentState->pause(*this);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 245 | /// Slot.
|
---|
| 246 | /// called when the time slider is released
|
---|
| 247 | void DbtPlyEngine::playEvent()
|
---|
| 248 | {
|
---|
| 249 | LOG_DEBUG("Clicked: play");
|
---|
| 250 |
|
---|
| 251 | // get user interface
|
---|
| 252 | ComponentManager * mgr = ComponentManager::getInstance();
|
---|
| 253 | DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
|
---|
| 254 | assert(ui);
|
---|
| 255 | // get time value from slider
|
---|
| 256 | lastTDbt_ = ui->getTime() + tDbtMin_;
|
---|
| 257 |
|
---|
| 258 | mCurrentState->play(*this);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 262 | void DbtPlyEngine::stopEvent()
|
---|
| 263 | {
|
---|
| 264 | LOG_DEBUG("Clicked: stop");
|
---|
| 265 | mCurrentState->stop(*this);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 269 | void DbtPlyEngine::speedUpEvent()
|
---|
| 270 | {
|
---|
| 271 | LOG_DEBUG("Clicked: speed Up");
|
---|
| 272 | mCurrentState->speedUp(*this);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 276 | void DbtPlyEngine::speedDownEvent()
|
---|
| 277 | {
|
---|
| 278 | LOG_DEBUG("Clicked: speed down");
|
---|
| 279 | mCurrentState->speedDown(*this);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 283 | const DbtPlyEngineState * DbtPlyEngine::getState()
|
---|
| 284 | {
|
---|
| 285 | return mCurrentState;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 289 | void DbtPlyEngine::setState(DbtPlyEngineState * newState)
|
---|
| 290 | {
|
---|
| 291 | assert(newState);
|
---|
| 292 | LOG_DEBUG(mCurrentState->toString() << " => " << newState->toString());
|
---|
| 293 | mCurrentState = newState;
|
---|
| 294 |
|
---|
| 295 | Q_EMIT displayStateSig(mCurrentState, mSpeed);
|
---|
| 296 |
|
---|
| 297 | LOG_DEBUG("tDbt = " << tDbt_ << "\tlastTDbt = " << lastTDbt_);
|
---|
| 298 | LOG_DEBUG("tNow = " << tNow_ << "\tlastTNow = " << lastTNow_);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | void DbtPlyEngine::speedUp()
|
---|
| 302 | {
|
---|
| 303 | mSpeed *= 2;
|
---|
| 304 | if (mSpeed > kMaxSpeed) {
|
---|
| 305 | mSpeed = kMaxSpeed;
|
---|
| 306 | }
|
---|
| 307 | assert(kMinSpeed <= mSpeed);
|
---|
| 308 | assert(mSpeed <= kMaxSpeed);
|
---|
| 309 | LOG_INFO("event: Speed Up, speed = " << mSpeed);
|
---|
| 310 | Q_EMIT displayStateSig(mCurrentState, mSpeed);
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | void DbtPlyEngine::speedDown()
|
---|
| 314 | {
|
---|
| 315 | mSpeed /= 2;
|
---|
| 316 | if (mSpeed < kMinSpeed) {
|
---|
| 317 | mSpeed = kMinSpeed;
|
---|
| 318 | }
|
---|
| 319 | assert(kMinSpeed <= mSpeed);
|
---|
| 320 | assert(mSpeed <= kMaxSpeed);
|
---|
| 321 | LOG_INFO("event: Speed Up, speed = " << mSpeed);
|
---|
| 322 | Q_EMIT displayStateSig(mCurrentState, mSpeed);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | void DbtPlyEngine::reset()
|
---|
| 326 | {
|
---|
| 327 | lastTNow_ = road_time();
|
---|
| 328 | lastTDbt_ = tDbtMin_;
|
---|
| 329 | mSpeed = kInitialSpeed;
|
---|
| 330 | Q_EMIT stopfile();
|
---|
| 331 |
|
---|
| 332 | // get user interface
|
---|
| 333 | ComponentManager * mgr = ComponentManager::getInstance();
|
---|
| 334 | DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
|
---|
| 335 | assert(ui);
|
---|
| 336 | // reset time value of the slider
|
---|
| 337 | ui->resetTime();
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | } // namespace pacpus
|
---|