Skip to content

Commit e9c516e

Browse files
authored
Unified internal clocks (#355)
1 parent eec1fb6 commit e9c516e

30 files changed

+181
-98
lines changed

Diff for: include/utils/InternalClock.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* InternalClock.h
2+
*
3+
* MIT License
4+
*
5+
* Copyright (c) 2022 awawa-dev
6+
*
7+
* Project homesite: https://github.com/awawa-dev/HyperHDR
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in all
17+
* copies or substantial portions of the Software.
18+
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
* SOFTWARE.
26+
*/
27+
28+
#pragma once
29+
#include <QtGlobal>
30+
#include <chrono>
31+
32+
class InternalClock
33+
{
34+
public:
35+
static qint64 now();
36+
static qint64 nowPrecise();
37+
static bool isPreciseSteady();
38+
private:
39+
const static std::chrono::time_point<std::chrono::steady_clock> start;
40+
const static std::chrono::time_point<std::chrono::high_resolution_clock> startPrecise;
41+
};

Diff for: include/utils/Logger.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <utils/InternalClock.h>
4+
35
// QT includes
46
#include <QObject>
57
#include <QString>

Diff for: sources/api/API.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
// Qt includes
1515
#include <QResource>
16-
#include <QDateTime>
1716
#include <QCryptographicHash>
1817
#include <QImage>
1918
#include <QBuffer>

Diff for: sources/api/JsonAPI.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// Qt includes
99
#include <QCoreApplication>
1010
#include <QResource>
11-
#include <QDateTime>
1211
#include <QImage>
1312
#include <QBuffer>
1413
#include <QByteArray>
@@ -62,7 +61,7 @@ JsonAPI::JsonAPI(QString peerAddress, Logger* log, bool localConnection, QObject
6261
_jsonCB = new JsonCB(this);
6362
_streaming_logging_activated = false;
6463
_ledStreamTimer = new QTimer(this);
65-
_lastSendImage = QDateTime::currentMSecsSinceEpoch();
64+
_lastSendImage = InternalClock::now();
6665

6766
Q_INIT_RESOURCE(JSONRPC_schemas);
6867
}
@@ -364,7 +363,7 @@ void JsonAPI::handleServerInfoCommand(const QJsonObject& message, const QString&
364363

365364
// collect priority information
366365
QJsonArray priorities;
367-
uint64_t now = QDateTime::currentMSecsSinceEpoch();
366+
uint64_t now = InternalClock::now();
368367

369368
int currentPriority = -1;
370369
if (QThread::currentThread() == _hyperhdr->thread())
@@ -1753,7 +1752,7 @@ void JsonAPI::streamLedcolorsUpdate(const std::vector<ColorRgb>& ledColors)
17531752

17541753
void JsonAPI::setImage(const Image<ColorRgb>& image)
17551754
{
1756-
uint64_t _currentTime = QDateTime::currentMSecsSinceEpoch();
1755+
uint64_t _currentTime = InternalClock::now();
17571756

17581757
if (!_semaphore.tryAcquire() && (_lastSendImage < _currentTime && (_currentTime - _lastSendImage < 2000)))
17591758
return;

Diff for: sources/api/JsonCB.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <utils/LutCalibrator.h>
2929

3030
// qt
31-
#include <QDateTime>
3231
#include <QVariant>
3332

3433
// Image to led map helper
@@ -281,7 +280,7 @@ void JsonCB::handlePriorityUpdate()
281280
{
282281
QJsonObject data;
283282
QJsonArray priorities;
284-
uint64_t now = QDateTime::currentMSecsSinceEpoch();
283+
int64_t now = InternalClock::now();
285284
QList<int> activePriorities = _prioMuxer->getPriorities();
286285
activePriorities.removeAll(255);
287286
int currentPriority = _prioMuxer->getCurrentPriority();

Diff for: sources/base/AuthManager.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ void AuthManager::setAuthBlock(bool user)
9999
{
100100
// current timestamp +10 minutes
101101
if (user)
102-
_userAuthAttempts.append(QDateTime::currentMSecsSinceEpoch() + 600000);
102+
_userAuthAttempts.append(InternalClock::now() + 600000);
103103
else
104-
_tokenAuthAttempts.append(QDateTime::currentMSecsSinceEpoch() + 600000);
104+
_tokenAuthAttempts.append(InternalClock::now() + 600000);
105105

106106
_authBlockTimer->start();
107107
}
@@ -164,7 +164,7 @@ void AuthManager::setNewTokenRequest(QObject* caller, const QString& comment, co
164164
{
165165
if (!_pendingRequests.contains(id))
166166
{
167-
AuthDefinition newDef{ id, comment, caller, tan, uint64_t(QDateTime::currentMSecsSinceEpoch() + 180000) };
167+
AuthDefinition newDef{ id, comment, caller, tan, uint64_t(InternalClock::now() + 180000) };
168168
_pendingRequests[id] = newDef;
169169
_timer->start();
170170
emit newPendingTokenRequest(id, comment);
@@ -210,7 +210,7 @@ QVector<AuthManager::AuthDefinition> AuthManager::getPendingRequests() const
210210
AuthDefinition def;
211211
def.comment = entry.comment;
212212
def.id = entry.id;
213-
def.timeoutTime = entry.timeoutTime - QDateTime::currentMSecsSinceEpoch();
213+
def.timeoutTime = entry.timeoutTime - InternalClock::now();
214214
finalVec.append(def);
215215
}
216216
return finalVec;
@@ -249,7 +249,7 @@ void AuthManager::handleSettingsUpdate(settings::type type, const QJsonDocument&
249249

250250
void AuthManager::checkTimeout()
251251
{
252-
const uint64_t now = QDateTime::currentMSecsSinceEpoch();
252+
const uint64_t now = InternalClock::now();
253253

254254
QMapIterator<QString, AuthDefinition> i(_pendingRequests);
255255
while (i.hasNext())
@@ -273,7 +273,7 @@ void AuthManager::checkAuthBlockTimeout()
273273
// handle user auth block
274274
QVector<uint64_t>::iterator itu = _userAuthAttempts.begin();
275275
while (itu != _userAuthAttempts.end()) {
276-
if (*itu < (uint64_t)QDateTime::currentMSecsSinceEpoch())
276+
if (*itu < (uint64_t)InternalClock::now())
277277
itu = _userAuthAttempts.erase(itu);
278278
else
279279
++itu;
@@ -282,7 +282,7 @@ void AuthManager::checkAuthBlockTimeout()
282282
// handle token auth block
283283
QVector<uint64_t>::iterator it = _tokenAuthAttempts.begin();
284284
while (it != _tokenAuthAttempts.end()) {
285-
if (*it < (uint64_t)QDateTime::currentMSecsSinceEpoch())
285+
if (*it < (uint64_t)InternalClock::now())
286286
it = _tokenAuthAttempts.erase(it);
287287
else
288288
++it;

Diff for: sources/base/DetectionAutomatic.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <base/GrabberWrapper.h>
3030
#include <base/HyperHdrIManager.h>
3131
#include <cmath>
32-
#include <QDateTime>
3332

3433
DetectionAutomatic::DetectionAutomatic() :
3534
_log(Logger::getInstance("SIGNAL_AUTO")),
@@ -143,9 +142,9 @@ void DetectionAutomatic::calibration::reset()
143142

144143
backupHDR = 0;
145144
decimationFPS = 0;
146-
phaseStartTime = QDateTime::currentMSecsSinceEpoch() + 200;
147-
phaseEndTime = QDateTime::currentMSecsSinceEpoch() + 5000 + 200;
148-
endTime = QDateTime::currentMSecsSinceEpoch() + 60 * 1000;
145+
phaseStartTime = InternalClock::now() + 200;
146+
phaseEndTime = InternalClock::now() + 5000 + 200;
147+
endTime = InternalClock::now() + 60 * 1000;
149148
currentSDRframe = 0;
150149
currentHDRframe = 0;
151150
signature = "";
@@ -234,7 +233,7 @@ void DetectionAutomatic::calibration::buildPoints(int _width, int _height)
234233

235234
void DetectionAutomatic::calibrateFrame(Image<ColorRgb>& image)
236235
{
237-
qint64 time = QDateTime::currentMSecsSinceEpoch();
236+
qint64 time = InternalClock::now();
238237

239238
if (time > calibrationData.endTime || time > calibrationData.phaseEndTime)
240239
{
@@ -317,8 +316,8 @@ void DetectionAutomatic::calibrateFrame(Image<ColorRgb>& image)
317316
if (calibrationData.currentSDRframe == 100)
318317
{
319318
GrabberWrapper::getInstance()->setHdrToneMappingEnabled(1);
320-
calibrationData.phaseStartTime = QDateTime::currentMSecsSinceEpoch() + 500;
321-
calibrationData.phaseEndTime = QDateTime::currentMSecsSinceEpoch() + 3000 + 500;
319+
calibrationData.phaseStartTime = InternalClock::now() + 500;
320+
calibrationData.phaseEndTime = InternalClock::now() + 3000 + 500;
322321
calibrationData.currentPhase = calibrationPhase::WAITING_FOR_HDR;
323322
calibrationData.status = "Waiting for first HDR frame";
324323
Debug(_log, "%s", QSTRING_CSTR(calibrationData.status));
@@ -521,7 +520,7 @@ bool DetectionAutomatic::checkSignal(Image<ColorRgb>& image)
521520
if (hasSignal && !_noSignal)
522521
return true;
523522

524-
qint64 time = QDateTime::currentMSecsSinceEpoch();
523+
qint64 time = InternalClock::now();
525524

526525
if (!hasSignal && !_noSignal)
527526
{

Diff for: sources/base/HyperHdrInstance.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ void HyperHdrInstance::update()
660660
void HyperHdrInstance::updateResult(std::vector<ColorRgb> _ledBuffer)
661661
{
662662
// stats
663-
int64_t now = QDateTime::currentMSecsSinceEpoch();
663+
int64_t now = InternalClock::now();
664664
int64_t diff = now - _computeStats.statBegin;
665665
int64_t prevToken = _computeStats.token;
666666

Diff for: sources/base/ImageProcessingUnit.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
// Blacborder includes
3535
#include <blackborder/BlackBorderProcessor.h>
36-
#include <QDateTime>
3736

3837
using namespace hyperhdr;
3938

Diff for: sources/base/ImageProcessor.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
// Blacborder includes
88
#include <blackborder/BlackBorderProcessor.h>
9-
#include <QDateTime>
109

1110
using namespace hyperhdr;
1211

Diff for: sources/base/LinearSmoothing.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Qt includes
2-
#include <QDateTime>
32
#include <QTimer>
43
#include <QThread>
54

@@ -73,6 +72,11 @@ void LinearSmoothing::handleSettingsUpdate(settings::type type, const QJsonDocum
7372
{
7473
if (type == settings::type::SMOOTHING)
7574
{
75+
if (InternalClock::isPreciseSteady())
76+
Info(_log, "High resolution clock is steady (good)");
77+
else
78+
Warning(_log, "High resolution clock is NOT STEADY!");
79+
7680
QJsonObject obj = config.object();
7781

7882
if (enabled() != obj["enable"].toBool(true))
@@ -150,7 +154,7 @@ void LinearSmoothing::Antiflickering()
150154
{
151155
if (_antiFlickeringTreshold > 0 && _antiFlickeringStep > 0 && _previousValues.size() == _targetValues.size() && _previousValues.size() == _previousTimeouts.size())
152156
{
153-
int64_t now = QDateTime::currentMSecsSinceEpoch();
157+
int64_t now = InternalClock::nowPrecise();
154158

155159
for (size_t i = 0; i < _previousValues.size(); ++i)
156160
{
@@ -173,7 +177,7 @@ void LinearSmoothing::Antiflickering()
173177
(newColor.red != 0 || newColor.green != 0 || newColor.blue != 0) &&
174178
(oldColor.red != 0 || oldColor.green != 0 || oldColor.blue != 0))
175179
{
176-
if (_antiFlickeringTimeout <= 0 || now - timeout < _antiFlickeringTimeout)
180+
if (_antiFlickeringTimeout <= 0 || (now - timeout < _antiFlickeringTimeout && now > timeout))
177181
_targetValues[i] = _previousValues[i];
178182
else
179183
timeout = now;
@@ -189,21 +193,21 @@ void LinearSmoothing::Antiflickering()
189193

190194
void LinearSmoothing::LinearSetup(const std::vector<ColorRgb>& ledValues)
191195
{
192-
_targetTime = QDateTime::currentMSecsSinceEpoch() + _settlingTime;
196+
_targetTime = InternalClock::nowPrecise() + _settlingTime;
193197
_targetValues = ledValues;
194198

195199
/////////////////////////////////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
196200

197-
if (!_previousValues.empty() && (_previousValues.size() != _targetValues.size()))
201+
if ((!_previousValues.empty() && (_previousValues.size() != _targetValues.size())) || _previousTime > _targetTime)
198202
{
199-
Warning(_log, "Detect size changed. Previuos value: %d, new value: %d", _previousValues.size(), _targetValues.size());
203+
Warning(_log, "Detect %s has changed. Previuos value: %d, new value: %d", (_previousTime > _targetTime) ? "TIME" : "size", _previousValues.size(), _targetValues.size());
200204
_previousValues.clear();
201205
_previousTimeouts.clear();
202206
}
203207

204208
if (_previousValues.empty())
205209
{
206-
_previousTime = QDateTime::currentMSecsSinceEpoch();
210+
_previousTime = InternalClock::nowPrecise();
207211
_previousValues = ledValues;
208212
_previousTimeouts.clear();
209213
_previousTimeouts.resize(_previousValues.size(), _previousTime);
@@ -246,7 +250,7 @@ inline uint8_t LinearSmoothing::computeAdvColor(int limitMin, int limitAverage,
246250
void LinearSmoothing::LinearSmoothingProcessing(bool correction)
247251
{
248252
float kOrg, kMin, kMid, kAbove, kMax;
249-
int64_t now = QDateTime::currentMSecsSinceEpoch();
253+
int64_t now = InternalClock::nowPrecise();
250254
int64_t deltaTime = _targetTime - now;
251255
int64_t k;
252256

Diff for: sources/base/PriorityMuxer.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <limits>
44

55
// qt incl
6-
#include <QDateTime>
76
#include <QTimer>
87
#include <QDebug>
98

@@ -183,7 +182,7 @@ bool PriorityMuxer::setInput(int priority, const std::vector<ColorRgb>& ledColor
183182

184183
// calc final timeout
185184
if (timeout_ms > 0)
186-
timeout_ms = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
185+
timeout_ms = InternalClock::now() + timeout_ms;
187186

188187
InputInfo& input = _activeInputs[priority];
189188

@@ -228,7 +227,7 @@ bool PriorityMuxer::setInputImage(int priority, const Image<ColorRgb>& image, in
228227

229228
// calculate final timeout
230229
if (timeout_ms > 0)
231-
timeout_ms = QDateTime::currentMSecsSinceEpoch() + timeout_ms;
230+
timeout_ms = InternalClock::now() + timeout_ms;
232231

233232
InputInfo& input = _activeInputs[priority];
234233

@@ -308,7 +307,7 @@ void PriorityMuxer::clearAll(bool forceClearAll)
308307

309308
void PriorityMuxer::setCurrentTime()
310309
{
311-
const int64_t now = QDateTime::currentMSecsSinceEpoch();
310+
const int64_t now = InternalClock::now();
312311
int newPriority;
313312
_activeInputs.contains(0) ? newPriority = 0 : newPriority = PriorityMuxer::LOWEST_PRIORITY;
314313

Diff for: sources/base/SoundCapture.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void SoundCapture::handleSettingsUpdate(settings::type type, const QJsonDocument
118118

119119
uint32_t SoundCapture::getCaptureInstance()
120120
{
121-
uint32_t ret;
121+
uint32_t ret = 0;
122122

123123
try
124124
{

Diff for: sources/boblightserver/BoblightClientConnection.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
// Qt includes
1515
#include <QResource>
16-
#include <QDateTime>
1716
#include <QHostInfo>
1817

1918
#include <base/ImageProcessor.h>

Diff for: sources/effectengine/Animation_Plasma.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
* SOFTWARE.
2626
*/
27-
27+
#include <utils/InternalClock.h>
2828
#include <effectengine/Animation_Plasma.h>
2929

3030
Animation_Plasma::Animation_Plasma() :
3131
AnimationBase(ANIM_PLASMA)
3232
{
33-
start = QDateTime::currentMSecsSinceEpoch();
33+
start = InternalClock::now();
3434

3535
for (int h = 0; h < PAL_LEN; h++)
3636
{

0 commit comments

Comments
 (0)