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

Last change on this file since 57 was 31, checked in by sgosseli, 12 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

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