2017-09-05 03:29:40 +08:00
|
|
|
#include "zm_analysis_thread.h"
|
|
|
|
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_signal.h"
|
2021-02-09 08:04:19 +08:00
|
|
|
#include "zm_utils.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
|
2021-02-08 02:12:39 +08:00
|
|
|
AnalysisThread::AnalysisThread(std::shared_ptr<Monitor> monitor) :
|
2021-02-09 07:35:16 +08:00
|
|
|
monitor_(std::move(monitor)), terminate_(false) {
|
|
|
|
thread_ = std::thread(&AnalysisThread::Run, this);
|
|
|
|
}
|
2017-09-05 03:29:40 +08:00
|
|
|
|
|
|
|
AnalysisThread::~AnalysisThread() {
|
2021-02-11 05:02:05 +08:00
|
|
|
Stop();
|
2021-02-09 07:35:16 +08:00
|
|
|
if (thread_.joinable())
|
|
|
|
thread_.join();
|
2017-09-05 03:29:40 +08:00
|
|
|
}
|
|
|
|
|
2021-02-09 07:35:16 +08:00
|
|
|
void AnalysisThread::Run() {
|
|
|
|
Debug(2, "AnalysisThread::Run()");
|
2017-09-05 03:29:40 +08:00
|
|
|
|
2021-02-09 08:04:19 +08:00
|
|
|
Microseconds analysis_rate = Microseconds(monitor_->GetAnalysisRate());
|
2021-02-09 08:18:01 +08:00
|
|
|
Seconds analysis_update_delay = Seconds(monitor_->GetAnalysisUpdateDelay());
|
|
|
|
|
2021-02-09 07:35:16 +08:00
|
|
|
monitor_->UpdateAdaptiveSkip();
|
2021-02-09 08:18:01 +08:00
|
|
|
|
|
|
|
TimePoint last_analysis_update_time = std::chrono::steady_clock::now();
|
|
|
|
TimePoint cur_time;
|
2017-09-05 03:29:40 +08:00
|
|
|
|
2021-02-09 07:35:16 +08:00
|
|
|
while (!(terminate_ or zm_terminate)) {
|
2017-09-05 03:29:40 +08:00
|
|
|
// Some periodic updates are required for variable capturing framerate
|
2021-02-09 08:18:01 +08:00
|
|
|
if (analysis_update_delay != Seconds::zero()) {
|
|
|
|
cur_time = std::chrono::steady_clock::now();
|
|
|
|
if ((cur_time - last_analysis_update_time) > analysis_update_delay) {
|
2021-02-09 08:04:19 +08:00
|
|
|
analysis_rate = Microseconds(monitor_->GetAnalysisRate());
|
2021-02-09 07:35:16 +08:00
|
|
|
monitor_->UpdateAdaptiveSkip();
|
2017-09-05 03:29:40 +08:00
|
|
|
last_analysis_update_time = cur_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 04:19:31 +08:00
|
|
|
Debug(2, "Analyzing");
|
2021-02-09 07:35:16 +08:00
|
|
|
if (!monitor_->Analyse()) {
|
2021-02-14 02:09:10 +08:00
|
|
|
if ( !(terminate_ or zm_terminate) ) {
|
|
|
|
Microseconds sleep_for = monitor_->Active() ? Microseconds(ZM_SAMPLE_RATE) : Microseconds(ZM_SUSPENDED_RATE);
|
|
|
|
Debug(2, "Sleeping for %" PRId64 "us", int64(sleep_for.count()));
|
|
|
|
std::this_thread::sleep_for(sleep_for);
|
|
|
|
}
|
2021-02-09 08:04:19 +08:00
|
|
|
} else if (analysis_rate != Microseconds::zero()) {
|
|
|
|
Debug(2, "Sleeping for %" PRId64 " us", int64(analysis_rate.count()));
|
|
|
|
std::this_thread::sleep_for(analysis_rate);
|
2017-11-28 21:29:03 +08:00
|
|
|
} else {
|
2021-02-09 08:04:19 +08:00
|
|
|
Debug(2, "Not sleeping");
|
2017-09-05 03:29:40 +08:00
|
|
|
}
|
2021-02-09 07:35:16 +08:00
|
|
|
}
|
|
|
|
}
|