diff --git a/src/zm_monitor.h b/src/zm_monitor.h index 0c5184a79a..0313899804 100644 --- a/src/zm_monitor.h +++ b/src/zm_monitor.h @@ -1197,6 +1197,7 @@ class Monitor : public std::enable_shared_from_this { AVCodecContext *GetVideoCodecContext() const { return mVideoCodecContext; }; std::string GetSecondPath() const { return second_path; }; + int GetStreamReplayBuffer() const { return stream_replay_buffer; } std::string GetStreamSocketPath() const { return shared_data ? shared_data->stream_socket_path : ""; }; std::string GetRTSPStreamName() const { return rtsp_streamname; }; diff --git a/src/zm_monitorstream.cpp b/src/zm_monitorstream.cpp index 732483a0cc..682922cbec 100644 --- a/src/zm_monitorstream.cpp +++ b/src/zm_monitorstream.cpp @@ -561,6 +561,7 @@ void MonitorStream::runStream() { TimePoint last_mem_report = stream_start_time; const Seconds mem_report_interval = Seconds(30); + playback_buffer = MonitorStreamClampBuffer(playback_buffer, monitor->GetStreamReplayBuffer()); temp_image_buffer_count = playback_buffer; temp_read_index = temp_image_buffer_count.load(); temp_write_index = temp_image_buffer_count.load(); diff --git a/src/zm_monitorstream.h b/src/zm_monitorstream.h index e1a6b6057f..4927444720 100644 --- a/src/zm_monitorstream.h +++ b/src/zm_monitorstream.h @@ -37,6 +37,15 @@ inline int MonitorStreamBufferLevel(int write_index, int read_index, int buffer_ return (((write_index - read_index + buffer_count) % buffer_count) * 100) / buffer_count; } +// The buffer= value in the zms URL sizes an allocation, so a client may not ask +// for more than the monitor's own StreamReplayBuffer (a negative request, such +// as an unsigned value that wrapped, gets none). +inline int MonitorStreamClampBuffer(int requested, int allowed) { + if (requested <= 0 || allowed <= 0) + return 0; + return requested > allowed ? allowed : requested; +} + class MonitorStream : public StreamBase { protected: struct SwapImage { diff --git a/tests/zm_monitorstream.cpp b/tests/zm_monitorstream.cpp index f13464f8f5..1fe4f569d8 100644 --- a/tests/zm_monitorstream.cpp +++ b/tests/zm_monitorstream.cpp @@ -23,6 +23,7 @@ #include "zm_monitor.h" #include "zm_zone.h" +#include #include #include #include @@ -246,3 +247,24 @@ TEST_CASE("MonitorStream starts a new zoom from the whole frame after stored JPE CHECK(used.crop().Hi().x_ == fresh.crop().Hi().x_); CHECK(used.crop().Hi().y_ == fresh.crop().Hi().y_); } + +TEST_CASE("MonitorStreamClampBuffer") { + SECTION("a request above the monitor's replay buffer is cut to it") { + REQUIRE(MonitorStreamClampBuffer(20000000, 100) == 100); + REQUIRE(MonitorStreamClampBuffer(2000000000, 25) == 25); + } + + SECTION("a request within the replay buffer is kept") { + REQUIRE(MonitorStreamClampBuffer(50, 100) == 50); + REQUIRE(MonitorStreamClampBuffer(100, 100) == 100); + } + + SECTION("no buffer when either value is not positive") { + REQUIRE(MonitorStreamClampBuffer(0, 100) == 0); + REQUIRE(MonitorStreamClampBuffer(-5, 100) == 0); + REQUIRE(MonitorStreamClampBuffer(INT_MIN, 100) == 0); + REQUIRE(MonitorStreamClampBuffer(50, 0) == 0); + REQUIRE(MonitorStreamClampBuffer(50, -1) == 0); + REQUIRE(MonitorStreamClampBuffer(2000000000, 0) == 0); + } +}