source: pacpusframework/branches/0.0.x/src/DBITEPlayerLib/DbtPlyEngine.cpp

Last change on this file was 91, checked in by DHERBOMEZ Gérald, 11 years ago

Improvement of the build system to avoid some workarounds

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