From 441dafb067d6e4590a69eb97f5d8a10300c6abdf Mon Sep 17 00:00:00 2001 From: Nic Boet Date: Mon, 26 Oct 2026 21:06:06 -0500 Subject: [PATCH] fix: bound decoded frames in the packet queue by MaxImageBufferCount refs #4860 With passthrough recording the packet queue keeps every packet back to a video keyframe. When the camera's keyframe interval is longer than MaxImageBufferCount, queuePacket() finds no later keyframe to trim to, so the queue holds the whole GOP and releaseDecoded() keeps the decoded frame and image of each packet back to that keyframe: about 7.4 MB per 5MP frame, 2 GB and more per monitor. releaseDecoded() now also frees the decoded data of all but the newest MaxImageBufferCount video packets, never inside the pre-event window and only behind every iterator. The compressed packets stay, so an event still starts on the keyframe and records all of its video; it writes no capture JPEGs for the frames that lost their image. fixes #5358 --- src/zm_packetqueue.cpp | 45 +++++++++++++++++++++---- src/zm_packetqueue.h | 1 + tests/zm_packetqueue.cpp | 71 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index bb6105f776..2e6d4cba6b 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -25,6 +25,7 @@ #include "zm_packet.h" #include "zm_signal.h" +#include #include #include @@ -43,7 +44,8 @@ PacketQueue::PacketQueue(): clear_packets_pending_(false), next_queue_index_(0), first_queue_index_(0), - released_before_(0) + released_before_(0), + capped_before_(0) { } @@ -458,17 +460,46 @@ void PacketQueue::releaseDecoded() { --it; } if (pre_event > 0) return; // the whole queue is still within reach of an event + const uint64_t pre_event_start = (*it)->queue_index; if (keep_keyframes) { while (it != pktQueue.begin() and !((*it)->packet->stream_index == video_stream_id and (*it)->keyframe)) --it; } const uint64_t boundary = (*it)->queue_index; - if (boundary <= released_before_) return; - - for (auto p = pktQueue.begin(); p != it; ++p) { - if ((*p)->queue_index < released_before_) continue; - to_release.push_back(*p); + if (boundary > released_before_) { + for (auto p = pktQueue.begin(); p != it; ++p) { + if ((*p)->queue_index < released_before_ or (*p)->queue_index < capped_before_) continue; + to_release.push_back(*p); + } + released_before_ = boundary; + } + + // Passthrough keeps every packet back to a keyframe. When the keyframe + // interval is longer than MaxImageBufferCount, queuePacket() finds no later + // keyframe to trim to and the queue holds the whole GOP, each packet with a + // decoded frame: several MB each at high resolutions, GBs per queue. Bound + // the decoded data instead: only the newest max_video_packet_count video + // packets keep it, and never less than the pre-event window. The compressed + // packets stay, so an event still starts on the keyframe with all its video; + // it writes no capture JPEGs for the frames that lost their image, as for + // frames before released_before_ (refs #4860). + if (keep_keyframes and max_video_packet_count > 0) { + auto oldest_kept = pktQueue.end(); + int video_packets = 0; + while (oldest_kept != pktQueue.begin() and video_packets < max_video_packet_count) { + --oldest_kept; + if ((*oldest_kept)->packet->stream_index == video_stream_id) video_packets++; + } + if (video_packets == max_video_packet_count) { + const uint64_t cap = std::min((*oldest_kept)->queue_index, pre_event_start); + if (cap > capped_before_) { + for (auto p = pktQueue.begin(); p != pktQueue.end() and (*p)->queue_index < cap; ++p) { + if ((*p)->queue_index < released_before_ or (*p)->queue_index < capped_before_) continue; + to_release.push_back(*p); + } + capped_before_ = cap; + } + } } - released_before_ = boundary; } // Free outside the queue lock, like clearPackets(). Nothing should hold these diff --git a/src/zm_packetqueue.h b/src/zm_packetqueue.h index 26c257a37c..a3f1f673d5 100644 --- a/src/zm_packetqueue.h +++ b/src/zm_packetqueue.h @@ -59,6 +59,7 @@ class PacketQueue { uint64_t next_queue_index_; 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 public: PacketQueue(); diff --git a/tests/zm_packetqueue.cpp b/tests/zm_packetqueue.cpp index 7b8b96ebec..48cafa83c6 100644 --- a/tests/zm_packetqueue.cpp +++ b/tests/zm_packetqueue.cpp @@ -232,3 +232,74 @@ TEST_CASE("PacketQueue: an event never starts on a released packet") { queue.stop(); queue.clear(); } + +// With passthrough the queue keeps every packet back to a keyframe. A keyframe +// interval longer than MaxImageBufferCount leaves queuePacket() no later +// keyframe to trim to, so the whole GOP stays queued; releaseDecoded() keeps the +// decoded data of only the newest MaxImageBufferCount video packets, never less +// than the pre-event window, and leaves the compressed packets for the event's +// video. refs #4860 +TEST_CASE("PacketQueue: decoded data is bounded by MaxImageBufferCount") { + PacketQueue queue; + queue.addStream(); + queue.setKeepKeyframes(true); + + packetqueue_iterator *holder_it = queue.get_video_it(false); + REQUIRE(holder_it != nullptr); + packetqueue_iterator *analysis_it = nullptr; + + // One keyframe, then a GOP longer than the queue's limit; analysis on the + // newest packet. + auto fill = [&](int count) { + std::vector> packets; + for (int i = 0; i < count; i++) { + packets.push_back(decoded_video_packet(i == 0)); + REQUIRE(queue.queuePacket(packets.back())); + } + while (queue.increment_it(holder_it, false)) {} + analysis_it = queue.get_video_it(false); + for (int i = 1; i < count; i++) REQUIRE(queue.increment_it(analysis_it, false)); + REQUIRE(*(*analysis_it) == packets.back()); + return packets; + }; + + SECTION("the newest MaxImageBufferCount packets keep their images") { + queue.setMaxVideoPackets(20); + queue.setPreEventVideoPackets(5); + auto packets = fill(30); + // queuePacket() found no second keyframe to trim to: everything is queued. + REQUIRE(queue.size() == 30); + queue.releaseDecoded(); + REQUIRE(packets[9]->image == nullptr); + REQUIRE(packets[10]->image != nullptr); + REQUIRE(packets[29]->image != nullptr); + + // The event still starts on the keyframe, for the whole of its video. + packetqueue_iterator *start = queue.get_event_start_packet_it(*analysis_it, 5); + REQUIRE(*(*start) == packets[0]); + queue.free_it(start); + } + + SECTION("the pre-event window keeps its images above the limit") { + queue.setMaxVideoPackets(10); + queue.setPreEventVideoPackets(15); + auto packets = fill(30); + queue.releaseDecoded(); + // 15 back from packet 29 is 14. + REQUIRE(packets[13]->image == nullptr); + REQUIRE(packets[14]->image != nullptr); + } + + SECTION("no limit: images back to the keyframe stay") { + queue.setMaxVideoPackets(0); + queue.setPreEventVideoPackets(5); + auto packets = fill(30); + queue.releaseDecoded(); + REQUIRE(packets[0]->image != nullptr); + } + + queue.free_it(analysis_it); + queue.free_it(holder_it); + queue.stop(); + queue.clear(); +}