MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_AudioBounceBuffer.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#include <cstddef>
6#include <cstdint>
7#include <cstring>
8#include <vector>
9#include <catch2/catch_test_macros.hpp>
10#include "AudioBounceBuffer.hpp"
11
12using namespace mxl::lib::fabrics::ofi;
13
14namespace
15{
16 using Sample = std::uint32_t;
17
18 // 2 audio channels, each target channel has a ring buffer of 9 samples and we are unpacking 5 samples landing at head index 11.
19 // The start offset is (11 + 9 - 5) % 9 = 6 so each channel writes to target indices:
20 // 6, 7, 8, 0, 1
21 // which creates a split of 3 samples before wrap + 2 samples after wrap
22 constexpr auto ChannelCount = std::size_t{2};
23 constexpr auto TargetBufferLength = std::size_t{9};
24 constexpr auto HeadIndex = std::uint64_t{11};
25 constexpr auto SampleCount = std::size_t{5};
26 constexpr auto UntouchedSample = Sample{0xFFFFFFFFU};
27
28 std::uintptr_t regionBaseOf(void* ptr) noexcept
29 {
30 return reinterpret_cast<std::uintptr_t>(ptr);
31 }
32
33 std::size_t ringStartOffset(std::uint64_t headIndex, std::size_t count, std::size_t bufferLength) noexcept
34 {
35 return (headIndex + bufferLength - count) % bufferLength;
36 }
37
38 Sample& sampleAt(std::vector<Sample>& samples, std::size_t channel, std::size_t index, std::size_t bufferLength)
39 {
40 return samples[(channel * bufferLength) + index];
41 }
42}
43
44TEST_CASE("ofi: audio bounce buffer unpacks channel-major payload across ring wrap", "[ofi][AudioBounceBuffer]")
45{
46 auto target = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
47 auto targetSlice = mxlMutableWrappedMultiBufferSlice{};
49 HeadIndex, SampleCount, TargetBufferLength, sizeof(Sample), ChannelCount, reinterpret_cast<std::uint8_t*>(target.data()), targetSlice);
50
51 REQUIRE((targetSlice.base.fragments[0].size / sizeof(Sample)) == 3U);
52 REQUIRE((targetSlice.base.fragments[1].size / sizeof(Sample)) == 2U);
53
54 // channel 0 payload: 103, 104, 105, 106, 107
55 // channel 1 payload: 203, 204, 205, 206, 207
56 auto const payloadSamples = std::vector<Sample>{103U, 104U, 105U, 106U, 107U, 203U, 204U, 205U, 206U, 207U};
57 auto const payloadSize = payloadSamples.size() * sizeof(Sample);
58
59 auto const targetLayout = DataLayout::Continuous{.sampleSize = sizeof(Sample), .channelCount = ChannelCount, .bufferLength = TargetBufferLength};
60 auto bounceBuffer = AudioBounceBuffer{1, payloadSize, targetLayout};
61 auto bounceRegions = bounceBuffer.getRegions();
62 REQUIRE(bounceRegions.size() == 1U);
63 REQUIRE(bounceRegions.front().size == (sizeof(AudioEntryHeader) + payloadSize));
64
65 // Write directly into the bounce-buffer entry:
66 auto* entryData = reinterpret_cast<std::uint8_t*>(bounceRegions.front().base); // NOLINT
67 auto const header = AudioEntryHeader{.headIndex = HeadIndex, .count = SampleCount};
68 std::memcpy(entryData, &header, sizeof(header));
69 std::memcpy(entryData + sizeof(AudioEntryHeader), payloadSamples.data(), payloadSize);
70
71 // Let entry 0 of the bounce buffer copy its payload into the target ring buffer.
72 auto const outRegion = Region{regionBaseOf(target.data()), target.size() * sizeof(Sample), nullptr, nullptr, Region::Location::host()};
73 auto const unpackedHeader = bounceBuffer.unpack(0, outRegion);
74 REQUIRE(unpackedHeader.headIndex == HeadIndex);
75 REQUIRE(unpackedHeader.count == SampleCount);
76
77 // Verify that unpack did correctly consume channel-major payload
78 auto expectedTarget = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
79 auto const targetStartOffset = ringStartOffset(HeadIndex, SampleCount, TargetBufferLength);
80 for (auto channel = std::size_t{0}; channel < ChannelCount; channel++)
81 {
82 for (auto offset = std::size_t{0}; offset < SampleCount; offset++)
83 {
84 auto const targetIndex = (targetStartOffset + offset) % TargetBufferLength;
85 sampleAt(expectedTarget, channel, targetIndex, TargetBufferLength) = payloadSamples[(channel * SampleCount) + offset];
86 }
87 }
88
89 REQUIRE(target == expectedTarget);
90}
91
92// A header that has not arrived yet reads as count 0.
93TEST_CASE("ofi: audio bounce buffer unpacks an empty header into nothing", "[ofi][AudioBounceBuffer]")
94{
95 auto target = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
96 auto const targetLayout = DataLayout::Continuous{.sampleSize = sizeof(Sample), .channelCount = ChannelCount, .bufferLength = TargetBufferLength};
97 auto bounceBuffer = AudioBounceBuffer{1, SampleCount * ChannelCount * sizeof(Sample), targetLayout};
98
99 auto const outRegion = Region{regionBaseOf(target.data()), target.size() * sizeof(Sample), nullptr, nullptr, Region::Location::host()};
100 auto const unpackedHeader = bounceBuffer.unpack(0, outRegion);
101 REQUIRE(unpackedHeader.count == 0U);
102 REQUIRE(target == std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample));
103}
104
105// A count above the ring length would wrap onto itself and write past the channel.
106TEST_CASE("ofi: audio bounce buffer rejects a count longer than the ring", "[ofi][AudioBounceBuffer]")
107{
108 constexpr auto LongCount = TargetBufferLength + 1;
109 auto target = std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample);
110 auto const targetLayout = DataLayout::Continuous{.sampleSize = sizeof(Sample), .channelCount = ChannelCount, .bufferLength = TargetBufferLength};
111 auto bounceBuffer = AudioBounceBuffer{1, LongCount * ChannelCount * sizeof(Sample), targetLayout};
112
113 auto* entryData = reinterpret_cast<std::uint8_t*>(bounceBuffer.getRegions().front().base); // NOLINT
114 auto const header = AudioEntryHeader{.headIndex = HeadIndex, .count = LongCount};
115 std::memcpy(entryData, &header, sizeof(header));
116
117 auto const outRegion = Region{regionBaseOf(target.data()), target.size() * sizeof(Sample), nullptr, nullptr, Region::Location::host()};
118 REQUIRE_THROWS(bounceBuffer.unpack(0, outRegion));
119 REQUIRE(target == std::vector<Sample>(ChannelCount * TargetBufferLength, UntouchedSample));
120}
A bounce buffer for audio samples.
std::vector< Region > getRegions() const noexcept
Return the regions corresponding to the bounce buffer entries. Each region corresponds to one bounce ...
static void getMutableMultiBufferSlices(std::uint64_t index, std::size_t count, std::size_t bufferLength, std::size_t sampleWordSize, std::size_t channelCount, std::uint8_t *baseBufferPtr, mxlMutableWrappedMultiBufferSlice &slice) noexcept
static Location host() noexcept
specify a host memory location.
Definition Region.cpp:23
Represent a memory region (unregistered).
Definition Region.hpp:24
struct mxlMutableWrappedMultiBufferSlice_t mxlMutableWrappedMultiBufferSlice
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
Header for an audio bounce buffer entry.
Continuous layout variant of DataLayout.
TEST_CASE("ofi: audio bounce buffer unpacks channel-major payload across ring wrap", "[ofi][AudioBounceBuffer]")
uint64_t index
REQUIRE(timeDelta< 33 '366 '667LL)