From 9ac66f7d1d4e157631d167883e4db5f14e3ebc4f Mon Sep 17 00:00:00 2001 From: Jash Date: Sat, 14 Nov 2026 21:06:58 +0530 Subject: [PATCH] fix: set the RTP marker bit only on the last packet of an access unit refs #5886 --- src/zm_rtsp_server_frame.h | 6 +- src/zm_rtsp_server_stream_audio_source.cpp | 2 +- src/zm_rtsp_server_stream_audio_source.h | 2 +- src/zm_rtsp_server_stream_source.cpp | 12 +-- src/zm_rtsp_server_stream_source.h | 2 +- src/zm_rtsp_server_stream_video_source.cpp | 3 +- src/zm_rtsp_server_stream_video_source.h | 2 +- tests/CMakeLists.txt | 1 + tests/zm_rtsp_server_marker.cpp | 106 +++++++++++++++++++++ 9 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 tests/zm_rtsp_server_marker.cpp diff --git a/src/zm_rtsp_server_frame.h b/src/zm_rtsp_server_frame.h index f8e7169f84..a3d64acea7 100644 --- a/src/zm_rtsp_server_frame.h +++ b/src/zm_rtsp_server_frame.h @@ -15,10 +15,11 @@ const char H264shortmarker[] = {0,0,1}; class NAL_Frame { public: - NAL_Frame(unsigned char * buffer, size_t size, int64 pts) : + NAL_Frame(unsigned char * buffer, size_t size, int64 pts, bool last = true) : m_buffer(nullptr), m_size(size), m_pts(pts), + m_last(last), m_ref_count(1) { m_buffer = new unsigned char[m_size]; if (buffer) { @@ -38,6 +39,8 @@ class NAL_Frame { size_t size(size_t new_size) { m_size=new_size; return m_size; }; size_t nal_size() const { return m_size-4; }; int64_t pts() const { return m_pts; }; + // Last NAL of its access unit: the RTP marker bit goes on its last packet + bool last() const { return m_last; }; bool check() const { // Look for marker at beginning unsigned char *marker = (unsigned char*)memmem(m_buffer, sizeof(H264marker), H264marker, sizeof(H264marker)); @@ -72,6 +75,7 @@ class NAL_Frame { public: int64 m_pts; private: + bool m_last; int m_ref_count; }; #endif // HAVE_RTSP_SERVER diff --git a/src/zm_rtsp_server_stream_audio_source.cpp b/src/zm_rtsp_server_stream_audio_source.cpp index 3d1f788796..51f58d5e5b 100644 --- a/src/zm_rtsp_server_stream_audio_source.cpp +++ b/src/zm_rtsp_server_stream_audio_source.cpp @@ -37,7 +37,7 @@ int ZoneMinderStreamAudioSource::getFrequencyIndex() { return -1; } -void ZoneMinderStreamAudioSource::PushFrame(const uint8_t *data, size_t size, int64_t pts) { +void ZoneMinderStreamAudioSource::PushFrame(const uint8_t *data, size_t size, int64_t pts, bool last) { xop::AVFrame frame(data, size); frame.type = xop::AUDIO_FRAME; frame.timestamp = av_rescale_q(pts, AV_TIME_BASE_Q, m_timeBase); diff --git a/src/zm_rtsp_server_stream_audio_source.h b/src/zm_rtsp_server_stream_audio_source.h index fdabb22c44..71868156e6 100644 --- a/src/zm_rtsp_server_stream_audio_source.h +++ b/src/zm_rtsp_server_stream_audio_source.h @@ -47,7 +47,7 @@ class ZoneMinderStreamAudioSource : public ZoneMinderStreamSource { int getChannels() const { return channels; }; protected: - void PushFrame(const uint8_t *data, size_t size, int64_t pts) override; + void PushFrame(const uint8_t *data, size_t size, int64_t pts, bool last) override; protected: std::string config; diff --git a/src/zm_rtsp_server_stream_source.cpp b/src/zm_rtsp_server_stream_source.cpp index 75f9786f2d..3071ce35fa 100644 --- a/src/zm_rtsp_server_stream_source.cpp +++ b/src/zm_rtsp_server_stream_source.cpp @@ -66,7 +66,7 @@ void ZoneMinderStreamSource::OnPacket(const uint8_t *data, size_t size, int64_t while (!stop_ && framesList.size()) { std::pair nal = framesList.front(); framesList.pop_front(); - m_nalQueue.push(new NAL_Frame(nal.first, nal.second, pts)); + m_nalQueue.push(new NAL_Frame(nal.first, nal.second, pts, framesList.empty())); } } condition_.notify_all(); @@ -111,7 +111,7 @@ void ZoneMinderStreamSource::WriteRun() { fuNal.buffer()[1] = nalSrc[1]; // Payload header (2nd byte) fuNal.buffer()[2] = 0x80 | nalUnitType; // FU header (with S bit) } - PushFrame(fuNal.buffer(), fuNal.size(), fuNal.pts()); + PushFrame(fuNal.buffer(), fuNal.size(), fuNal.pts(), false); nalRemaining -= maxNalSize-1; nalSrc += maxNalSize-1; int nal_count = 0; @@ -125,15 +125,15 @@ void ZoneMinderStreamSource::WriteRun() { headerSize = 3; } while (nalRemaining && !stop_) { - if (nalRemaining <= maxNalSize-headerSize) { - // This is the last fragment: + bool lastFragment = nalRemaining <= maxNalSize-headerSize; + if (lastFragment) { fuNal.buffer()[headerSize-1] |= 0x40; // set the E bit in the FU header } fuNalSize = (nalRemaining < maxNalSize-headerSize) ? nalRemaining : maxNalSize-headerSize; fuNal.size(fuNalSize+headerSize); memcpy(fuNal.buffer()+headerSize, nalSrc, fuNalSize); - PushFrame(fuNal.buffer(), fuNal.size(), fuNal.pts()); + PushFrame(fuNal.buffer(), fuNal.size(), fuNal.pts(), nal->last() && lastFragment); nalRemaining -= fuNalSize; nalSrc += fuNalSize; nal_count += 1; @@ -141,7 +141,7 @@ void ZoneMinderStreamSource::WriteRun() { Debug(3, "Sending %d NALs @ %zu and 1 @ %zu", nal_count, maxNalSize, fuNal.size()); } else { Debug(3, "Pushing nal of size %zu at %" PRId64, nal->size(), nal->pts()); - PushFrame(nal->buffer(), nal->size(), nal->pts()); + PushFrame(nal->buffer(), nal->size(), nal->pts(), nal->last()); } delete nal; nal = nullptr; diff --git a/src/zm_rtsp_server_stream_source.h b/src/zm_rtsp_server_stream_source.h index 12535fa644..4694665efc 100644 --- a/src/zm_rtsp_server_stream_source.h +++ b/src/zm_rtsp_server_stream_source.h @@ -59,7 +59,7 @@ class ZoneMinderStreamSource { protected: void WriteRun(); - virtual void PushFrame(const uint8_t *data, size_t size, int64_t pts) = 0; + virtual void PushFrame(const uint8_t *data, size_t size, int64_t pts, bool last) = 0; // split packet in frames virtual std::list< std::pair > splitFrames(unsigned char* frame, size_t &frameSize); virtual unsigned char *extractFrame(unsigned char *data, size_t& size, size_t& outsize); diff --git a/src/zm_rtsp_server_stream_video_source.cpp b/src/zm_rtsp_server_stream_video_source.cpp index d06a309ed7..45f60474f3 100644 --- a/src/zm_rtsp_server_stream_video_source.cpp +++ b/src/zm_rtsp_server_stream_video_source.cpp @@ -22,8 +22,9 @@ ZoneMinderStreamVideoSource::ZoneMinderStreamVideoSource( m_timeBase = {1, 90000}; } -void ZoneMinderStreamVideoSource::PushFrame(const uint8_t *data, size_t size, int64_t pts) { +void ZoneMinderStreamVideoSource::PushFrame(const uint8_t *data, size_t size, int64_t pts, bool last) { xop::AVFrame frame(data, size); + frame.last = last; frame.timestamp = av_rescale_q(pts, AV_TIME_BASE_Q, m_timeBase); m_rtspServer->PushFrame(m_sessionId, m_channelId, frame); } diff --git a/src/zm_rtsp_server_stream_video_source.h b/src/zm_rtsp_server_stream_video_source.h index 6a2c2314f1..5736e5146b 100644 --- a/src/zm_rtsp_server_stream_video_source.h +++ b/src/zm_rtsp_server_stream_video_source.h @@ -39,7 +39,7 @@ class ZoneMinderStreamVideoSource: public ZoneMinderStreamSource { xop::MediaChannelId channelId ); protected: - void PushFrame(const uint8_t *data, size_t size, int64_t pts) override; + void PushFrame(const uint8_t *data, size_t size, int64_t pts, bool last) override; protected: int m_width; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b1a0a763b1..9f0eb10e2c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -56,6 +56,7 @@ set(TEST_SOURCES zm_rgb_luma.cpp zm_rtsp_deprecation.cpp zm_rtsp_server_pts_tracker.cpp + zm_rtsp_server_marker.cpp zm_rtsp_server_session_tracker.cpp zm_signal.cpp zm_stream.cpp diff --git a/tests/zm_rtsp_server_marker.cpp b/tests/zm_rtsp_server_marker.cpp new file mode 100644 index 0000000000..543e4c9d8b --- /dev/null +++ b/tests/zm_rtsp_server_marker.cpp @@ -0,0 +1,106 @@ +/* + * 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_config.h" + +#if HAVE_RTSP_SERVER +#include "zm_rtsp_server_stream_h264_source.h" + +#include +#include +#include +#include + +namespace { + +// Records what the packer hands to the RTSP layer instead of sending it +class RecordingH264Source : public H264_ZoneMinderStreamSource { + public: + RecordingH264Source(std::shared_ptr &server) : + H264_ZoneMinderStreamSource(server, 0, xop::channel_0) {} + ~RecordingH264Source() override { StopAndJoin(); } + + struct Pushed { + size_t size; + bool last; + }; + + void PushFrame(const uint8_t *data, size_t size, int64_t pts, bool last) override { + std::lock_guard lock(record_mutex); + pushed.push_back({size, last}); + if (last) + record_cv.notify_all(); + } + + bool WaitForLast() { + std::unique_lock lock(record_mutex); + return record_cv.wait_for(lock, std::chrono::seconds(5), [this] { + for (const Pushed &p : pushed) + if (p.last) return true; + return false; + }); + } + + std::mutex record_mutex; + std::condition_variable record_cv; + std::vector pushed; +}; + +std::vector AnnexB(std::initializer_list> nals) { + std::vector out; + for (const auto &nal : nals) { + out.insert(out.end(), {0, 0, 0, 1, nal.first}); + out.insert(out.end(), nal.second - 1, 0x55); + } + return out; +} + +} // namespace + +TEST_CASE("RTSP marker bit is set on the last packet of an access unit only", "[rtsp_marker]") { + std::shared_ptr server; + RecordingH264Source source(server); + + // AUD, SPS, PPS, an IDR slice that needs fragmenting, then a small slice + std::vector au = AnnexB({{0x09, 2}, {0x67, 20}, {0x68, 6}, {0x65, 4000}, {0x41, 100}}); + source.OnPacket(au.data(), au.size(), 1000); + REQUIRE(source.WaitForLast()); + source.StopAndJoin(); + + REQUIRE(source.pushed.size() > 5); // the IDR slice went out as several FU-A packets + for (size_t i = 0; i + 1 < source.pushed.size(); i++) + CHECK_FALSE(source.pushed[i].last); + CHECK(source.pushed.back().last); +} + +TEST_CASE("RTSP marker bit lands on the last fragment of a trailing large NAL", "[rtsp_marker]") { + std::shared_ptr server; + RecordingH264Source source(server); + + std::vector au = AnnexB({{0x09, 2}, {0x65, 5000}}); + source.OnPacket(au.data(), au.size(), 1000); + REQUIRE(source.WaitForLast()); + source.StopAndJoin(); + + REQUIRE(source.pushed.size() > 2); + for (size_t i = 0; i + 1 < source.pushed.size(); i++) + CHECK_FALSE(source.pushed[i].last); + CHECK(source.pushed.back().last); +} +#endif // HAVE_RTSP_SERVER