fix: set the RTP marker bit only on the last packet of an access unit refs #5886

This commit is contained in:
Jash committed 2026-11-14 21:52:57 +05:30
1 parent eec823a705
commit 9ac66f7d1d
9 files changed
+124 -12

No files matched your search

+5 -1
View File
@@ -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
+1 -1
View File
@@ -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);
+1 -1
View File
@@ -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;
+6 -6
View File
@@ -66,7 +66,7 @@ void ZoneMinderStreamSource::OnPacket(const uint8_t *data, size_t size, int64_t
while (!stop_ && framesList.size()) {
std::pair<unsigned char*, size_t> 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;
+1 -1
View File
@@ -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<unsigned char*, size_t> > splitFrames(unsigned char* frame, size_t &frameSize);
virtual unsigned char *extractFrame(unsigned char *data, size_t& size, size_t& outsize);
+2 -1
View File
@@ -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);
}
+1 -1
View File
@@ -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;
+1
View File
@@ -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
+106
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "zm_catch2.h"
#include "zm_config.h"
#if HAVE_RTSP_SERVER
#include "zm_rtsp_server_stream_h264_source.h"
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <vector>
namespace {
// Records what the packer hands to the RTSP layer instead of sending it
class RecordingH264Source : public H264_ZoneMinderStreamSource {
public:
RecordingH264Source(std::shared_ptr<xop::RtspServer> &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<std::mutex> lock(record_mutex);
pushed.push_back({size, last});
if (last)
record_cv.notify_all();
}
bool WaitForLast() {
std::unique_lock<std::mutex> 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> pushed;
};
std::vector<uint8_t> AnnexB(std::initializer_list<std::pair<uint8_t, size_t>> nals) {
std::vector<uint8_t> 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<xop::RtspServer> server;
RecordingH264Source source(server);
// AUD, SPS, PPS, an IDR slice that needs fragmenting, then a small slice
std::vector<uint8_t> 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<xop::RtspServer> server;
RecordingH264Source source(server);
std::vector<uint8_t> 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