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