From fee5ecd72f68e76eb1295d4a522a82b90c3398aa Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 21 Mar 2021 23:07:19 +0100 Subject: [PATCH 1/2] Monitor: Make analysis_thread a unique_ptr The AnalysisThread object is owned by the Monitor. Signal this by using unique_ptr. --- src/zm_analysis_thread.cpp | 6 ++---- src/zm_analysis_thread.h | 9 ++++----- src/zm_monitor.cpp | 15 ++++++++------- src/zm_monitor.h | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/zm_analysis_thread.cpp b/src/zm_analysis_thread.cpp index 7a6b01bbb..7aa037074 100644 --- a/src/zm_analysis_thread.cpp +++ b/src/zm_analysis_thread.cpp @@ -4,10 +4,8 @@ #include "zm_signal.h" #include "zm_utils.h" -//AnalysisThread::AnalysisThread(std::shared_ptr monitor) : -AnalysisThread::AnalysisThread(Monitor* monitor) : +AnalysisThread::AnalysisThread(Monitor *monitor) : monitor_(monitor), terminate_(false) { - //monitor_(std::move(monitor)), terminate_(false) { thread_ = std::thread(&AnalysisThread::Run, this); } @@ -43,7 +41,7 @@ void AnalysisThread::Run() { Debug(2, "Analyzing"); if (!monitor_->Analyse()) { - if ( !(terminate_ or zm_terminate) ) { + 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); diff --git a/src/zm_analysis_thread.h b/src/zm_analysis_thread.h index 7fc0fd8c9..e5b95601f 100644 --- a/src/zm_analysis_thread.h +++ b/src/zm_analysis_thread.h @@ -1,15 +1,15 @@ #ifndef ZM_ANALYSIS_THREAD_H #define ZM_ANALYSIS_THREAD_H -class Monitor; #include #include #include +class Monitor; + class AnalysisThread { public: - explicit AnalysisThread(Monitor* monitor); - //explicit AnalysisThread(std::shared_ptr monitor); + explicit AnalysisThread(Monitor *monitor); ~AnalysisThread(); AnalysisThread(AnalysisThread &rhs) = delete; AnalysisThread(AnalysisThread &&rhs) = delete; @@ -19,8 +19,7 @@ class AnalysisThread { private: void Run(); - Monitor* monitor_; - //std::shared_ptr monitor_; + Monitor *monitor_; std::atomic terminate_; std::thread thread_; }; diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 7e18dd73f..de3740715 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -3216,7 +3216,7 @@ int Monitor::PrimeCapture() { if (!analysis_it) analysis_it = packetqueue.get_video_it(false); if (!analysis_thread) { Debug(1, "Starting an analysis thread for monitor (%d)", id); - analysis_thread = new AnalysisThread(this); + analysis_thread = ZM::make_unique(this); } } else { @@ -3229,17 +3229,18 @@ int Monitor::PreCapture() const { return camera->PreCapture(); } int Monitor::PostCapture() const { return camera->PostCapture(); } int Monitor::Close() { // Because the stream indexes may change we have to clear out the packetqueue - if (decoder) decoder->Stop(); - if (analysis_thread) analysis_thread->Stop(); + if (decoder) { + decoder->Stop(); + } + if (analysis_thread) { + analysis_thread->Stop(); + } packetqueue.clear(); + if (decoder) { delete decoder; decoder = nullptr; } - if (analysis_thread) { - delete analysis_thread; - analysis_thread = nullptr; - } std::lock_guard lck(event_mutex); if (event) { Info("%s: image_count:%d - Closing event %" PRIu64 ", shutting down", name, image_count, event->Id()); diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 5543ed4ef..a48870397 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -376,7 +376,7 @@ protected: VideoStore *videoStore; PacketQueue packetqueue; packetqueue_iterator *analysis_it; - AnalysisThread *analysis_thread; + std::unique_ptr analysis_thread; packetqueue_iterator *decoder_it; DecoderThread *decoder; AVFrame *dest_frame; // Used by decoding thread doing colorspace conversions From 817da4e621d3b417c55920086810f93a4173f32f Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 21 Mar 2021 23:32:22 +0100 Subject: [PATCH 2/2] Monitor: Make decoder a unique_ptr The DecoderThread object is owned by the Monitor. Signal this by using unique_ptr. --- src/zm_decoder_thread.cpp | 34 ++-------------------------------- src/zm_decoder_thread.h | 6 ++---- src/zm_monitor.cpp | 6 +----- src/zm_monitor.h | 2 +- 4 files changed, 6 insertions(+), 42 deletions(-) diff --git a/src/zm_decoder_thread.cpp b/src/zm_decoder_thread.cpp index 3d522830d..ae5c8edc1 100644 --- a/src/zm_decoder_thread.cpp +++ b/src/zm_decoder_thread.cpp @@ -3,10 +3,8 @@ #include "zm_monitor.h" #include "zm_signal.h" -//DecoderThread::DecoderThread(std::shared_ptr monitor) : -DecoderThread::DecoderThread(Monitor * monitor) : +DecoderThread::DecoderThread(Monitor *monitor) : monitor_(monitor), terminate_(false) { - //monitor_(std::move(monitor)), terminate_(false) { thread_ = std::thread(&DecoderThread::Run, this); } @@ -19,35 +17,7 @@ DecoderThread::~DecoderThread() { 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"); - } + monitor_->Decode(); } } diff --git a/src/zm_decoder_thread.h b/src/zm_decoder_thread.h index 703a7e1e1..24e155c72 100644 --- a/src/zm_decoder_thread.h +++ b/src/zm_decoder_thread.h @@ -9,8 +9,7 @@ class Monitor; class DecoderThread { public: - explicit DecoderThread(Monitor* monitor); - //explicit DecoderThread(std::shared_ptr monitor); + explicit DecoderThread(Monitor *monitor); ~DecoderThread(); DecoderThread(DecoderThread &rhs) = delete; DecoderThread(DecoderThread &&rhs) = delete; @@ -20,8 +19,7 @@ class DecoderThread { private: void Run(); - Monitor* monitor_; - //std::shared_ptr monitor_; + Monitor *monitor_; std::atomic terminate_; std::thread thread_; }; diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index de3740715..7e30b723a 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -3210,7 +3210,7 @@ int Monitor::PrimeCapture() { if (decoding_enabled) { if (!decoder_it) decoder_it = packetqueue.get_video_it(false); - if (!decoder) decoder = new DecoderThread(this); + if (!decoder) decoder = ZM::make_unique(this); } if (!analysis_it) analysis_it = packetqueue.get_video_it(false); @@ -3237,10 +3237,6 @@ int Monitor::Close() { } packetqueue.clear(); - if (decoder) { - delete decoder; - decoder = nullptr; - } std::lock_guard lck(event_mutex); if (event) { Info("%s: image_count:%d - Closing event %" PRIu64 ", shutting down", name, image_count, event->Id()); diff --git a/src/zm_monitor.h b/src/zm_monitor.h index a48870397..7bdb0febb 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -378,7 +378,7 @@ protected: packetqueue_iterator *analysis_it; std::unique_ptr analysis_thread; packetqueue_iterator *decoder_it; - DecoderThread *decoder; + std::unique_ptr decoder; AVFrame *dest_frame; // Used by decoding thread doing colorspace conversions SwsContext *convert_context; std::thread close_event_thread;