75 lines
1.9 KiB
C++
75 lines
1.9 KiB
C++
#pragma once
|
|
|
|
#include "protocol/binary_audio_protocol.h"
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
|
|
namespace borla {
|
|
|
|
// Holds the small number of downlink frames that can arrive after the
|
|
// protocol has announced a TTS session but before Application has applied the
|
|
// corresponding BeginRemotePlayback effect. The object is allocated in
|
|
// PSRAM by EspAudioEngine so the fixed frame storage does not consume
|
|
// internal DRAM.
|
|
class DownlinkPrerollBuffer final {
|
|
public:
|
|
static constexpr size_t kCapacity = 8;
|
|
|
|
bool Push(
|
|
uint32_t session_id,
|
|
const uint8_t* data,
|
|
size_t size)
|
|
{
|
|
if (session_id == 0 || data == nullptr || size == 0 ||
|
|
size > kBinaryAudioMaximumOpusBytes) {
|
|
return false;
|
|
}
|
|
if (session_id_ != session_id) {
|
|
Reset(session_id);
|
|
}
|
|
if (frame_count_ >= frames_.size()) {
|
|
return false;
|
|
}
|
|
Frame& frame = frames_[frame_count_++];
|
|
frame.size = static_cast<uint16_t>(size);
|
|
std::memcpy(frame.payload.data(), data, size);
|
|
return true;
|
|
}
|
|
|
|
void Reset(uint32_t session_id = 0)
|
|
{
|
|
session_id_ = session_id;
|
|
frame_count_ = 0;
|
|
}
|
|
|
|
uint32_t session_id() const { return session_id_; }
|
|
size_t frame_count() const { return frame_count_; }
|
|
|
|
const uint8_t* FrameData(size_t index) const
|
|
{
|
|
return index < frame_count_
|
|
? frames_[index].payload.data()
|
|
: nullptr;
|
|
}
|
|
|
|
size_t FrameSize(size_t index) const
|
|
{
|
|
return index < frame_count_ ? frames_[index].size : 0;
|
|
}
|
|
|
|
private:
|
|
struct Frame {
|
|
uint16_t size = 0;
|
|
std::array<uint8_t, kBinaryAudioMaximumOpusBytes> payload{};
|
|
};
|
|
|
|
std::array<Frame, kCapacity> frames_{};
|
|
uint32_t session_id_ = 0;
|
|
size_t frame_count_ = 0;
|
|
};
|
|
|
|
} // namespace borla
|