diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index d9fb2731e8..7978f8f2c8 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -289,6 +289,7 @@ Monitor::Monitor() : first_alarm_count(0), last_alarm_count(0), last_signal(false), + assume_signal_on_next_packet(false), buffer_count(0), state(IDLE), last_motion_score(0), @@ -1131,6 +1132,7 @@ bool Monitor::connect() { shared_data->capturing = capturing; shared_data->recording = recording; shared_data->signal = false; + assume_signal_on_next_packet = true; shared_data->capture_fps = 0.0; shared_data->analysis_fps = 0.0; shared_data->latitude = latitude; @@ -1930,6 +1932,20 @@ bool Monitor::CheckSignal(const Image *image) { return false; } // end bool Monitor::CheckSignal(const Image *image) +bool Monitor::DecoderChecksSignal(DecodingOption decoding, int signal_check_points, bool has_viewers) { + if (signal_check_points <= 0) return false; + switch (decoding) { + case DECODING_ALWAYS: + case DECODING_KEYFRAMES: + case DECODING_KEYFRAMESONDEMAND: + return true; + case DECODING_ONDEMAND: + return has_viewers; + default: + return false; + } +} + void Monitor::CheckAction() { if (shared_data->action) { @@ -3093,8 +3109,13 @@ int Monitor::Capture() { // Don't want to do analysis on it, but we won't due to signal return -1; } else if (captureResult > 0) { - shared_data->signal = true; // Assume if getting packets that we are getting something useful. CheckSignalPoints can correct this later. - // If we captured, let's assume signal, Decode will detect further + // Getting packets: assume signal unless the decoder measures it + // (SignalCheckPoints). Writing true here as well made a lost-signal + // picture flip back to "signal" between decoded frames. + if (assume_signal_on_next_packet or !DecoderChecksSignal(decoding, signal_check_points, hasViewers())) { + shared_data->signal = true; + assume_signal_on_next_packet = false; + } if (decoding == DECODING_NONE) { shared_data->last_write_index = index; shared_data->last_write_time = std::chrono::system_clock::to_time_t(packet->timestamp); diff --git a/src/zm_monitor.h b/src/zm_monitor.h index ff255bdb37..a4d4bae490 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -716,6 +716,10 @@ class Monitor : public std::enable_shared_from_this { int first_alarm_count; int last_alarm_count; bool last_signal; + // Capture assumes signal on the first packet after connect() cleared it, + // even when the decoder measures it, so Analyse doesn't see a signal loss + // before the first decoded frame. Capture thread only. + bool assume_signal_on_next_packet; int buffer_count; State state; SystemTimePoint start_time; @@ -1147,6 +1151,10 @@ class Monitor : public std::enable_shared_from_this { // DetectBlack seems to be unused. Check it on zm_monitor.cpp for more info. //unsigned int DetectBlack( const Image &comp_image, Event::StringSet &zoneSet ); bool CheckSignal( const Image *image ); + // True when the decoder measures shared_data->signal (SignalCheckPoints) + // for the packets it decodes; Capture then leaves the flag to it, so the + // two threads don't overwrite each other's value. Pure, for tests. + static bool DecoderChecksSignal(DecodingOption decoding, int signal_check_points, bool has_viewers); bool Analyse(); bool setupConvertContext(const AVFrame *input_frame, const Image *image); // Write capture_image into image_buffer[index] without conversion and diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 75ee0f3b65..8d260e80a6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,6 +32,7 @@ set(TEST_SOURCES zm_mp4_sidx.cpp zm_packetqueue.cpp zm_monitor_action.cpp + zm_monitor_signal.cpp zm_onvif_alarm_termination.cpp zm_onvif_auth_error.cpp zm_onvif_poll_wait.cpp diff --git a/tests/zm_monitor_signal.cpp b/tests/zm_monitor_signal.cpp new file mode 100644 index 0000000000..5971ad09e3 --- /dev/null +++ b/tests/zm_monitor_signal.cpp @@ -0,0 +1,45 @@ +/* + * This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "zm_catch2.h" + +#include "zm_monitor.h" + +TEST_CASE("Monitor::DecoderChecksSignal") { + SECTION("without SignalCheckPoints nothing measures the signal") { + for (auto decoding : {Monitor::DECODING_NONE, Monitor::DECODING_ONDEMAND, Monitor::DECODING_KEYFRAMES, + Monitor::DECODING_KEYFRAMESONDEMAND, Monitor::DECODING_ALWAYS}) { + REQUIRE_FALSE(Monitor::DecoderChecksSignal(decoding, 0, true)); + REQUIRE_FALSE(Monitor::DecoderChecksSignal(decoding, 0, false)); + } + } + + SECTION("a decoder that always runs measures it") { + REQUIRE(Monitor::DecoderChecksSignal(Monitor::DECODING_ALWAYS, 10, false)); + REQUIRE(Monitor::DecoderChecksSignal(Monitor::DECODING_KEYFRAMES, 10, false)); + REQUIRE(Monitor::DecoderChecksSignal(Monitor::DECODING_KEYFRAMESONDEMAND, 10, false)); + } + + SECTION("on-demand decoding measures it only while someone watches") { + REQUIRE(Monitor::DecoderChecksSignal(Monitor::DECODING_ONDEMAND, 10, true)); + REQUIRE_FALSE(Monitor::DecoderChecksSignal(Monitor::DECODING_ONDEMAND, 10, false)); + } + + SECTION("no decoding: capture keeps setting it") { + REQUIRE_FALSE(Monitor::DecoderChecksSignal(Monitor::DECODING_NONE, 10, true)); + } +}