Files
bolar_2/main/audio/playback_pipeline.h
2026-11-27 16:38:11 +08:00

152 lines
4.9 KiB
C++

#pragma once
#include "audio/audio_output.h"
#include "audio/opus_frame_decoder.h"
#include "audio/playback_controller.h"
#include "audio/voice_frame_processor.h"
#include "ports/event_publisher.h"
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <mutex>
namespace borla {
class PlaybackPipeline final {
public:
static constexpr uint32_t kSampleRate = 48000;//24000;
static constexpr uint32_t kFrameDurationMs = 60;
static constexpr size_t kSamplesPerFrame =
kSampleRate * kFrameDurationMs / 1000;
static constexpr size_t kMaxFrameBytes =
kSamplesPerFrame * sizeof(int16_t);
static constexpr size_t kMaxOpusBytes = 1536;
static constexpr size_t kFramePoolSize = 48;
explicit PlaybackPipeline(IAudioOutput& output);
~PlaybackPipeline();
void SetEventPublisher(IEventPublisher* publisher);
// True while a session still owns the speaker. Used to hold off a reboot
// until the prompt that explains the reboot has actually been heard.
bool Active() const
{
std::lock_guard<std::mutex> lock(mutex_);
return controller_.snapshot().active;
}
void SetReferenceSink(IPlaybackReferenceSink* sink)
{
reference_sink_.store(sink);
}
bool Initialize();
bool Start();
void Stop();
PlaybackStartResult StartSession(
PlaybackSource source,
PlaybackPriority priority,
PlaybackSessionId requested_id = 0);
bool SubmitOpus(
PlaybackSessionId session_id,
const uint8_t* data,
size_t size);
bool SubmitPcm(
PlaybackSessionId session_id,
const int16_t* samples,
size_t sample_count);
bool EndInput(PlaybackSessionId session_id);
void SetPaused(bool paused);
bool Cancel(PlaybackSessionId session_id = 0);
PlaybackSessionSnapshot Snapshot() const;
PlaybackMetrics Metrics() const;
private:
enum class FrameFormat : uint8_t {
kOpus,
kPcm,
};
struct FrameSlot {
PlaybackSessionId session_id = 0;
FrameFormat format = FrameFormat::kOpus;
uint16_t payload_size = 0;
uint16_t sample_count = 0;
alignas(int16_t) uint8_t payload[kMaxFrameBytes]{};
};
static void TaskEntry(void* context);
void Run();
void FlushPlayoutHealth();
bool SubmitFrame(
PlaybackSessionId session_id,
FrameFormat format,
const void* data,
size_t byte_count,
size_t sample_count);
void DrainReadyQueueLocked();
void ReleaseSlot(uint16_t slot_index);
void CheckTimeout();
void PublishPlaybackEvent(
AppEventType type,
PlaybackSessionId session_id,
PlaybackSessionId replacement_session_id = 0);
static uint64_t NowMs();
IAudioOutput& output_;
std::atomic<IEventPublisher*> publisher_{nullptr};
std::atomic<IPlaybackReferenceSink*> reference_sink_{nullptr};
OpusFrameDecoder decoder_;
mutable std::mutex mutex_;
PlaybackController controller_;
FrameSlot* frame_pool_ = nullptr;
int16_t* decode_buffer_ = nullptr;
int16_t* reference_buffer_ = nullptr;
QueueHandle_t free_slots_ = nullptr;
QueueHandle_t ready_frames_ = nullptr;
StaticQueue_t free_slots_storage_{};
StaticQueue_t ready_frames_storage_{};
uint8_t free_slots_queue_bytes_[kFramePoolSize * sizeof(uint16_t)]{};
uint8_t ready_frames_queue_bytes_[kFramePoolSize * sizeof(uint16_t)]{};
SemaphoreHandle_t stopped_signal_ = nullptr;
StaticSemaphore_t stopped_signal_storage_{};
TaskHandle_t task_ = nullptr;
bool initialized_ = false;
std::atomic<bool> running_{false};
std::atomic<bool> paused_{false};
// Playout health accounting, owned exclusively by the speaker task.
// The server paces frames in real time after a 6-frame (360 ms) burst,
// so the whole jitter cushion is that burst plus the 90 ms I2S DMA.
// These counters turn "偶发卡顿" into one measurable line per session:
// an inter-write gap > 60 ms means the cushion fully eroded, > 90 ms
// means the DMA drained and the break was audible.
PlaybackSessionId health_session_ = 0;
uint64_t last_write_end_us_ = 0;
bool health_skip_gap_ = false;
uint32_t health_frames_ = 0;
uint32_t health_gap_over_frame_ = 0;
uint32_t health_gap_over_dma_ = 0;
uint32_t health_max_gap_ms_ = 0;
uint32_t health_depth_min_ = 0;
uint32_t health_depth_max_ = 0;
// Set by StartSession/Cancel (application task); consumed by Run() right
// before Decode(), the only place allowed to touch decoder_ while the
// playback task lives. Decode() runs outside mutex_, so a direct Reset()
// from another task would race it.
std::atomic<bool> decoder_reset_pending_{false};
std::atomic<PlaybackSessionId> backpressure_logged_session_{0};
};
} // namespace borla