186 lines
5.8 KiB
C++
186 lines
5.8 KiB
C++
#include "audio/downlink_preroll_buffer.h"
|
|
#include "audio/pcm_rate_converter.h"
|
|
#include "protocol/binary_audio_protocol.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
using namespace borla;
|
|
|
|
class CollectingVisitor final : public IBinaryAudioFrameVisitor {
|
|
public:
|
|
bool OnOpusFrame(const uint8_t* data, size_t size) override
|
|
{
|
|
frames.emplace_back(data, data + size);
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::vector<uint8_t>> frames;
|
|
};
|
|
|
|
void TestExactFrameRateConversion()
|
|
{
|
|
std::array<int16_t, Pcm24kTo16kConverter::kInputSamples> input{};
|
|
std::array<int16_t, Pcm24kTo16kConverter::kOutputSamples> output{};
|
|
for (size_t index = 0; index < input.size(); ++index) {
|
|
input[index] = static_cast<int16_t>(index);
|
|
}
|
|
|
|
Pcm24kTo16kConverter converter;
|
|
assert(converter.Convert(
|
|
input.data(), input.size(), output.data(), output.size()));
|
|
assert(output[0] == 0);
|
|
assert(output[1] == 1);
|
|
assert(output[2] == 3);
|
|
assert(output[3] == 4);
|
|
assert(output.back() == 1438);
|
|
assert(!converter.Convert(
|
|
input.data(), input.size() - 1, output.data(), output.size()));
|
|
}
|
|
|
|
void TestBatchRoundTrip()
|
|
{
|
|
BinaryAudioBatchBuilder builder;
|
|
const std::array<uint8_t, 3> first{1, 2, 3};
|
|
const std::array<uint8_t, 4> second{4, 5, 6, 7};
|
|
assert(builder.Push(first.data(), first.size()));
|
|
assert(builder.Push(second.data(), second.size()));
|
|
|
|
std::array<uint8_t, kBinaryAudioMaximumPacketBytes> packet{};
|
|
size_t packet_size = 0;
|
|
assert(builder.Build(packet.data(), packet.size(), &packet_size));
|
|
assert(packet[0] == kBinaryAudioTypeOpus);
|
|
assert(packet[1] == kBinaryAudioFlagBatch);
|
|
assert(packet_size == 4 + 2 + first.size() + 2 + second.size());
|
|
|
|
CollectingVisitor visitor;
|
|
size_t frames = 0;
|
|
assert(VisitBinaryAudioPacket(
|
|
packet.data(), packet_size, visitor, &frames));
|
|
assert(frames == 2);
|
|
assert(visitor.frames[0] ==
|
|
std::vector<uint8_t>(first.begin(), first.end()));
|
|
assert(visitor.frames[1] ==
|
|
std::vector<uint8_t>(second.begin(), second.end()));
|
|
}
|
|
|
|
void TestSingleAndMalformedPackets()
|
|
{
|
|
const std::array<uint8_t, 3> payload{9, 8, 7};
|
|
std::array<uint8_t, 16> built{};
|
|
size_t built_size = 0;
|
|
assert(BuildBinaryAudioSingleFrame(
|
|
payload.data(), payload.size(),
|
|
built.data(), built.size(), &built_size));
|
|
assert(built_size == 7);
|
|
assert(built[0] == kBinaryAudioTypeOpus);
|
|
assert(built[1] == 0);
|
|
|
|
const std::array<uint8_t, 7> single{
|
|
0, 0, 0, 3, 9, 8, 7};
|
|
assert(std::equal(built.begin(), built.begin() + built_size,
|
|
single.begin()));
|
|
CollectingVisitor visitor;
|
|
assert(VisitBinaryAudioPacket(
|
|
single.data(), single.size(), visitor));
|
|
assert(visitor.frames.size() == 1);
|
|
|
|
auto malformed = single;
|
|
malformed[3] = 4;
|
|
CollectingVisitor invalid_visitor;
|
|
assert(!VisitBinaryAudioPacket(
|
|
malformed.data(), malformed.size(), invalid_visitor));
|
|
|
|
const std::array<uint8_t, 8> zero_frame{
|
|
0, 1, 0, 4, 0, 0, 1, 2};
|
|
assert(!VisitBinaryAudioPacket(
|
|
zero_frame.data(), zero_frame.size(), invalid_visitor));
|
|
|
|
const std::array<uint8_t, 11> valid_then_broken{
|
|
0, 1, 0, 7, 0, 2, 1, 2, 0, 3, 9};
|
|
CollectingVisitor atomic_visitor;
|
|
assert(!VisitBinaryAudioPacket(
|
|
valid_then_broken.data(),
|
|
valid_then_broken.size(),
|
|
atomic_visitor));
|
|
assert(atomic_visitor.frames.empty());
|
|
}
|
|
|
|
void TestBatchCapacity()
|
|
{
|
|
BinaryAudioBatchBuilder builder;
|
|
std::array<uint8_t, kBinaryAudioMaximumUplinkOpusBytes> frame{};
|
|
for (size_t index = 0;
|
|
index < kBinaryAudioBatchFrames;
|
|
++index) {
|
|
assert(builder.Push(frame.data(), frame.size()));
|
|
}
|
|
assert(builder.Full());
|
|
assert(!builder.Push(frame.data(), frame.size()));
|
|
builder.Reset();
|
|
assert(builder.Empty());
|
|
}
|
|
|
|
void TestDownlinkPrerollPreservesOrderAndSessionIsolation()
|
|
{
|
|
DownlinkPrerollBuffer preroll;
|
|
const std::array<uint8_t, 3> first{1, 2, 3};
|
|
const std::array<uint8_t, 2> second{4, 5};
|
|
assert(preroll.Push(41, first.data(), first.size()));
|
|
assert(preroll.Push(41, second.data(), second.size()));
|
|
assert(preroll.session_id() == 41);
|
|
assert(preroll.frame_count() == 2);
|
|
assert(preroll.FrameSize(0) == first.size());
|
|
assert(std::memcmp(
|
|
preroll.FrameData(0), first.data(), first.size()) == 0);
|
|
assert(std::memcmp(
|
|
preroll.FrameData(1), second.data(), second.size()) == 0);
|
|
|
|
assert(preroll.Push(42, second.data(), second.size()));
|
|
assert(preroll.session_id() == 42);
|
|
assert(preroll.frame_count() == 1);
|
|
assert(preroll.FrameData(1) == nullptr);
|
|
|
|
preroll.Reset();
|
|
assert(preroll.session_id() == 0);
|
|
assert(preroll.frame_count() == 0);
|
|
}
|
|
|
|
void TestDownlinkPrerollIsBounded()
|
|
{
|
|
DownlinkPrerollBuffer preroll;
|
|
std::array<uint8_t, kBinaryAudioMaximumOpusBytes> frame{};
|
|
for (size_t index = 0;
|
|
index < DownlinkPrerollBuffer::kCapacity;
|
|
++index) {
|
|
frame[0] = static_cast<uint8_t>(index);
|
|
assert(preroll.Push(7, frame.data(), frame.size()));
|
|
}
|
|
assert(!preroll.Push(7, frame.data(), frame.size()));
|
|
assert(!preroll.Push(0, frame.data(), frame.size()));
|
|
assert(!preroll.Push(7, nullptr, frame.size()));
|
|
std::array<uint8_t, kBinaryAudioMaximumOpusBytes + 1> oversized{};
|
|
assert(!preroll.Push(7, oversized.data(), oversized.size()));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
TestExactFrameRateConversion();
|
|
TestBatchRoundTrip();
|
|
TestSingleAndMalformedPackets();
|
|
TestBatchCapacity();
|
|
TestDownlinkPrerollPreservesOrderAndSessionIsolation();
|
|
TestDownlinkPrerollIsBounded();
|
|
std::cout << "audio data plane tests passed\n";
|
|
return 0;
|
|
}
|