source: pacpusframework/trunk/src/DBITEPlayerLib/DbtPlyEngine.cpp@ 20

Last change on this file since 20 was 3, checked in by sgosseli, 12 years ago
  • Add the existing Pacpus files from pacpusdev and pacpuscore.
  • Provide a clean build system based on multiple CMake files.
File size: 10.1 KB
Line 
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
20using namespace std;
21
22// Construction de la fabrique de composant DbtPlyEngine
23//static ComponentFactory<DbtPlyEngine> factory("DbtPlyEngine");
24
25namespace pacpus {
26
27DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyEngine");
28
29static const string kPropertyDataDirectory = "datadir";
30
31static const string kPropertyLoggerConfiguration = "log-config-file";
32
33static const string kPropertyReplayMode = "replay_mode";
34static const string kPropertyReplayModeLastData = "1";
35static const string kPropertyReplayModeAllData = "2";
36
37typedef float SpeedType;
38static const SpeedType kInitialSpeed = 1.0;
39static const SpeedType kMinSpeed = 1.0 / 32;
40static const SpeedType kMaxSpeed = 32;
41
42////////////////////////////////////////////////////////////////////////////////
43/// Constructor
44DbtPlyEngine::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.
64DbtPlyEngine::~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.
72QString DbtPlyEngine::getDataDir()
73{
74 return dataDir_;
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Slot.
79/// called when trigger reaches a new timer event
80void DbtPlyEngine::engReceiver()
81{
82 sync_->release();
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// @returns @b true if the engine is in the PLAY_STATE, @b false otherwise
87bool 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()
95void 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
137void 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
151ComponentBase::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
202void DbtPlyEngine::startActivity()
203{
204 LOG_INFO("Starting...");
205
206 THREAD_ALIVE = true;
207 start();
208}
209
210////////////////////////////////////////////////////////////////////////////////
211/// The stop function of the engine
212void 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
222void 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
238void 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
247void 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////////////////////////////////////////////////////////////////////////////////
262void DbtPlyEngine::stopEvent()
263{
264 LOG_DEBUG("Clicked: stop");
265 mCurrentState->stop(*this);
266}
267
268////////////////////////////////////////////////////////////////////////////////
269void DbtPlyEngine::speedUpEvent()
270{
271 LOG_DEBUG("Clicked: speed Up");
272 mCurrentState->speedUp(*this);
273}
274
275////////////////////////////////////////////////////////////////////////////////
276void DbtPlyEngine::speedDownEvent()
277{
278 LOG_DEBUG("Clicked: speed down");
279 mCurrentState->speedDown(*this);
280}
281
282////////////////////////////////////////////////////////////////////////////////
283const DbtPlyEngineState * DbtPlyEngine::getState()
284{
285 return mCurrentState;
286}
287
288////////////////////////////////////////////////////////////////////////////////
289void 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
301void 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
313void 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
325void 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
Note: See TracBrowser for help on using the repository browser.