From 6a11b23aafc401fb336257599ab99ccccec5a61a Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 15 Mar 2021 15:08:59 -0400 Subject: [PATCH] Add decoder thread --- src/zm_decoder_thread.cpp | 54 +++++++++++++++++++++++++++++++++++++++ src/zm_decoder_thread.h | 29 +++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/zm_decoder_thread.cpp create mode 100644 src/zm_decoder_thread.h diff --git a/src/zm_decoder_thread.cpp b/src/zm_decoder_thread.cpp new file mode 100644 index 000000000..e8023e905 --- /dev/null +++ b/src/zm_decoder_thread.cpp @@ -0,0 +1,54 @@ +#include "zm_decoder_thread.h" + +#include "zm_monitor.h" +#include "zm_signal.h" +#include "zm_utils.h" + +//DecoderThread::DecoderThread(std::shared_ptr monitor) : +DecoderThread::DecoderThread(Monitor * monitor) : + monitor_(monitor), terminate_(false) { + //monitor_(std::move(monitor)), terminate_(false) { + thread_ = std::thread(&DecoderThread::Run, this); +} + +DecoderThread::~DecoderThread() { + Stop(); + if (thread_.joinable()) + thread_.join(); +} + +void DecoderThread::Run() { + Debug(2, "DecoderThread::Run() for %d", monitor_->Id()); + + //Microseconds decoder_rate = Microseconds(monitor_->GetDecoderRate()); + //Seconds decoder_update_delay = Seconds(monitor_->GetDecoderUpdateDelay()); + //Debug(2, "DecoderThread::Run() have update delay %d", decoder_update_delay); + + //TimePoint last_decoder_update_time = std::chrono::steady_clock::now(); + //TimePoint cur_time; + + while (!(terminate_ or zm_terminate)) { + // Some periodic updates are required for variable capturing framerate + //if (decoder_update_delay != Seconds::zero()) { + //cur_time = std::chrono::steady_clock::now(); + //Debug(2, "Updating adaptive skip"); + //if ((cur_time - last_decoder_update_time) > decoder_update_delay) { + //decoder_rate = Microseconds(monitor_->GetDecoderRate()); + //last_decoder_update_time = cur_time; + //} + //} + + if (!monitor_->Decode()) { + //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); + //} + //} else if (decoder_rate != Microseconds::zero()) { + //Debug(2, "Sleeping for %" PRId64 " us", int64(decoder_rate.count())); + //std::this_thread::sleep_for(decoder_rate); + //} else { + //Debug(2, "Not sleeping"); + } + } +} diff --git a/src/zm_decoder_thread.h b/src/zm_decoder_thread.h new file mode 100644 index 000000000..703a7e1e1 --- /dev/null +++ b/src/zm_decoder_thread.h @@ -0,0 +1,29 @@ +#ifndef ZM_DECODER_THREAD_H +#define ZM_DECODER_THREAD_H + +#include +#include +#include + +class Monitor; + +class DecoderThread { + public: + explicit DecoderThread(Monitor* monitor); + //explicit DecoderThread(std::shared_ptr monitor); + ~DecoderThread(); + DecoderThread(DecoderThread &rhs) = delete; + DecoderThread(DecoderThread &&rhs) = delete; + + void Stop() { terminate_ = true; } + + private: + void Run(); + + Monitor* monitor_; + //std::shared_ptr monitor_; + std::atomic terminate_; + std::thread thread_; +}; + +#endif