From 3206bfd3a86d99c68352f216defb8bf243a6720f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 31 Oct 2026 19:08:27 -0400 Subject: [PATCH] fix: report a passthrough keyframe interval longer than MaxImageBufferCount Passthrough recording keeps every packet back to a video keyframe. When the camera's keyframe interval is longer than MaxImageBufferCount, queuePacket() has no later keyframe to trim to, so the packet queue holds the whole GOP whatever MaxImageBufferCount says. #5359 bounds the decoded copies in that case, which keeps memory down, but the queue still isn't working as configured and nothing said so specifically: the existing "queue is full" Warning also covers analysis falling behind. Log an Error naming MaxImageBufferCount and the fix (raise it above the keyframe interval, or shorten the camera's GOP) as soon as a GOP passes the limit with keep_keyframes on. Report it once; setMaxVideoPackets(), setKeepKeyframes() (a RELOAD) and clear() reset it. setKeepKeyframes() now takes the queue mutex, like the other setters. refs #5358 Co-Authored-By: Claude Opus 5.5 --- src/zm_packetqueue.cpp | 23 ++++++++++++++++++- src/zm_packetqueue.h | 6 ++++- tests/zm_packetqueue.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index 2e6d4cba6b..8e85bfb7d3 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -45,7 +45,8 @@ PacketQueue::PacketQueue(): next_queue_index_(0), first_queue_index_(0), released_before_(0), - capped_before_(0) + capped_before_(0), + keyframe_interval_over_limit_(false) { } @@ -126,6 +127,18 @@ bool PacketQueue::queuePacket(std::shared_ptr add_packet) { max_keyframe_interval_ = frames_since_last_keyframe_; Debug(1, "Have new keyframe interval %d", max_keyframe_interval_.load()); } + // Passthrough keeps every packet back to a keyframe, so a GOP longer + // than MaxImageBufferCount can't be trimmed: the queue holds all of it, + // and releaseDecoded() can only bound the decoded copies. + if (keep_keyframes and (max_video_packet_count > 0) and !keyframe_interval_over_limit_ + and (frames_since_last_keyframe_ + 1 > max_video_packet_count)) { + keyframe_interval_over_limit_ = true; + Error("The camera's keyframe interval is longer than MaxImageBufferCount (%d frames). " + "Passthrough recording has to keep a whole keyframe interval of packets, so the packet queue " + "can't stay within MaxImageBufferCount. Raise MaxImageBufferCount above the keyframe interval " + "or shorten the camera's keyframe interval (GOP).", + max_video_packet_count); + } } else { frames_since_last_keyframe_ = 0; if (!max_keyframe_interval_) max_keyframe_interval_ = 1; @@ -562,6 +575,7 @@ void PacketQueue::clear() { packet_counts.reset(); max_stream_id = -1; max_keyframe_interval_ = 0; + keyframe_interval_over_limit_ = false; first_queue_index_ = next_queue_index_; Debug(1, "Packetqueue is clear, notifying"); @@ -929,6 +943,13 @@ void PacketQueue::setMaxVideoPackets(int p) { Debug(1, "Setting max_video_packet_count to %d", p); if (max_video_packet_count < 0) max_video_packet_count = 0; + keyframe_interval_over_limit_ = false; +} + +void PacketQueue::setKeepKeyframes(bool k) { + std::lock_guard lck(mutex); + keep_keyframes = k; + keyframe_interval_over_limit_ = false; } void PacketQueue::setPreEventVideoPackets(int p) { diff --git a/src/zm_packetqueue.h b/src/zm_packetqueue.h index a3f1f673d5..db7c3413de 100644 --- a/src/zm_packetqueue.h +++ b/src/zm_packetqueue.h @@ -60,6 +60,9 @@ class PacketQueue { uint64_t first_queue_index_; // queue_index of the first packet queued since the last clear() uint64_t released_before_; // releaseDecoded() has handled every packet before this queue_index uint64_t capped_before_; // decoded data freed under max_video_packet_count before this queue_index + // A GOP longer than max_video_packet_count was seen with keep_keyframes on and + // reported; cleared when either setting changes or the queue is cleared. + bool keyframe_interval_over_limit_; public: PacketQueue(); @@ -70,7 +73,8 @@ class PacketQueue { int addStream(); void setMaxVideoPackets(int p); void setPreEventVideoPackets(int p); - void setKeepKeyframes(bool k) { keep_keyframes = k; }; + void setKeepKeyframes(bool k); + bool keyframe_interval_over_limit() const { return keyframe_interval_over_limit_; } bool queuePacket(std::shared_ptr packet); void stop(); diff --git a/tests/zm_packetqueue.cpp b/tests/zm_packetqueue.cpp index 48cafa83c6..fa2ca539e3 100644 --- a/tests/zm_packetqueue.cpp +++ b/tests/zm_packetqueue.cpp @@ -303,3 +303,52 @@ TEST_CASE("PacketQueue: decoded data is bounded by MaxImageBufferCount") { queue.stop(); queue.clear(); } + +// With passthrough the queue keeps every packet back to a keyframe, so a camera +// keyframe interval longer than MaxImageBufferCount leaves queuePacket() nothing +// to trim to: the queue holds the whole GOP whatever MaxImageBufferCount says. +// That is a misconfiguration, reported once as an Error. +TEST_CASE("PacketQueue: a keyframe interval longer than MaxImageBufferCount is reported") { + PacketQueue queue; + queue.addStream(); + queue.setMaxVideoPackets(10); + packetqueue_iterator *it = queue.get_video_it(false); + REQUIRE(it != nullptr); + + auto gop = [&](int count) { + for (int i = 0; i < count; i++) REQUIRE(queue.queuePacket(video_packet(i == 0))); + }; + + SECTION("passthrough, GOP within the limit") { + queue.setKeepKeyframes(true); + gop(10); + gop(10); + REQUIRE_FALSE(queue.keyframe_interval_over_limit()); + } + + SECTION("passthrough, GOP over the limit") { + queue.setKeepKeyframes(true); + gop(11); + REQUIRE(queue.keyframe_interval_over_limit()); + } + + SECTION("not passthrough: the queue trims to the limit itself") { + queue.setKeepKeyframes(false); + gop(30); + REQUIRE_FALSE(queue.keyframe_interval_over_limit()); + } + + SECTION("raising MaxImageBufferCount clears it") { + queue.setKeepKeyframes(true); + gop(11); + REQUIRE(queue.keyframe_interval_over_limit()); + queue.setMaxVideoPackets(20); + REQUIRE_FALSE(queue.keyframe_interval_over_limit()); + gop(11); + REQUIRE_FALSE(queue.keyframe_interval_over_limit()); + } + + queue.free_it(it); + queue.stop(); + queue.clear(); +}