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