Files
zoneminder/tests/zm_rtsp_server_nal_split.cpp
T

90 lines
2.9 KiB
C++

/*
* 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 the payloads the packer hands to the RTSP layer
class CapturingH264Source : public H264_ZoneMinderStreamSource {
public:
explicit CapturingH264Source(std::shared_ptr<xop::RtspServer> &server) :
H264_ZoneMinderStreamSource(server, 0, xop::channel_0) {}
~CapturingH264Source() override { StopAndJoin(); }
void PushFrame(const uint8_t *data, size_t size, int64_t, bool last) override {
std::lock_guard<std::mutex> lock(record_mutex);
pushed.emplace_back(data, data + size);
if (last) {
done = true;
record_cv.notify_all();
}
}
bool Wait() {
std::unique_lock<std::mutex> lock(record_mutex);
return record_cv.wait_for(lock, std::chrono::seconds(5), [this] { return done; });
}
std::mutex record_mutex;
std::condition_variable record_cv;
bool done = false;
std::vector<std::vector<uint8_t>> pushed;
};
} // namespace
TEST_CASE("RTSP NALs do not carry trailing zero bytes", "[rtsp_nal_split]") {
std::shared_ptr<xop::RtspServer> server;
CapturingH264Source source(server);
// SPS, then zero padding and a 4-byte start code (00 00 00 00 00 01), then
// an IDR slice that ends in padding
std::vector<uint8_t> au = {
0, 0, 0, 1, 0x67, 0x42, 0x00, 0x1F,
0, 0, 0,
0, 0, 0, 1, 0x65, 0x88, 0x84, 0x21,
0, 0, 0
};
source.OnPacket(au.data(), au.size(), 1000);
REQUIRE(source.Wait());
source.StopAndJoin();
REQUIRE(source.pushed.size() == 2);
CHECK(source.pushed[0] == std::vector<uint8_t>{0x67, 0x42, 0x00, 0x1F});
CHECK(source.pushed[1] == std::vector<uint8_t>{0x65, 0x88, 0x84, 0x21});
}
TEST_CASE("RTSP timestamps never reach xop as 0", "[rtsp_nal_split]") {
// a missing pts is left 0 so xop uses the wall clock; a real 0 must not be
AVRational tb = {1, 90000};
CHECK(ZoneMinderStreamVideoSource::RtpTimestamp(AV_NOPTS_VALUE, tb) == 0); // wall clock, not INT64_MIN
CHECK(ZoneMinderStreamVideoSource::RtpTimestamp(0, tb) != 0);
CHECK(ZoneMinderStreamVideoSource::RtpTimestamp(1000000, tb) == 90000);
}
#endif // HAVE_RTSP_SERVER