// SPDX-FileCopyrightText: Copyright 2026 Gloria Goertz, Mireille Meyer // SPDX-License-Identifier: BSD-2-Clause #include namespace atom::Audio::Implementation { Result> PulseAudioManager::CreateRenderer(const Format& format, const u32 desiredCapacity) { if (m_state != PA_CONTEXT_READY) { return Status::Expired; } return CreateRendererImpl(nullptr, format, desiredCapacity); } Result> PulseAudioManager::CreateRenderer(const DeviceInfo& info, const Format& format, const u32 desiredCapacity) { if (!info.IsValid()) { return Status::InvalidParameters; } if (m_state != PA_CONTEXT_READY) { return Status::Expired; } return CreateRendererImpl(reinterpret_cast(info.m_id.get()), format, desiredCapacity); } Result> PulseAudioManager::CreateRendererImpl(const char* device, const Format& format, const u32 desiredCapacity) { usize bits; pa_sample_format_t pulseFormat; switch (format.type) { case Format::Type::F32_PCM: { bits = 32; pulseFormat = PA_SAMPLE_FLOAT32LE; break; } case Format::Type::S32_PCM: { bits = 32; pulseFormat = PA_SAMPLE_S32LE; break; } case Format::Type::S24_PCM: { bits = 24; pulseFormat = PA_SAMPLE_S24LE; break; } case Format::Type::S16_PCM: { bits = 16; pulseFormat = PA_SAMPLE_S16LE; break; } } const pa_sample_spec spec = { .format = pulseFormat, .rate = format.rate, .channels = format.channels }; pa_stream* stream = pa_stream_new(m_context, m_name.c_str(), &spec, nullptr); if (stream == nullptr) { const int error = -pa_context_errno(m_context); switch (error) { // TODO(Gloria): Handle more errors default: { ATOM_PANIC("pa_stream_new failed with {}", error); } } } const usize blockSize = format.channels * (bits / 8); auto renderer = std::unique_ptr(new PulseAudioRenderer(this, stream, desiredCapacity, blockSize)); pa_stream_set_state_callback(renderer->m_stream, PulseAudioRenderer::StateCallback, renderer.get()); pa_buffer_attr attributes = { .maxlength = u32(-1), .tlength = desiredCapacity * (u32(bits) / 8), .prebuf = u32(-1), .minreq = u32(-1) }; const auto flags = static_cast(PA_STREAM_START_CORKED | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY); const int result = -pa_stream_connect_playback(renderer->m_stream, device, &attributes, flags, nullptr, nullptr); switch (result) { case PA_OK: { break; } default: { ATOM_PANIC("pa_stream_connect_playback failed with {}", result); } } while (renderer->m_state != PA_STREAM_READY) { switch (renderer->m_state) { case PA_STREAM_UNCONNECTED: case PA_STREAM_CREATING: { pa_threaded_mainloop_wait(m_loop); break; } case PA_STREAM_READY: { break; } default: { const int error = -pa_context_errno(m_context); switch (error) { // TODO(Gloria): Handle more errors default: { ATOM_PANIC("pa_stream_connect_playback failed with {}", error); } } } } } return Result>(std::move(renderer)); } PulseAudioRenderer::~PulseAudioRenderer() { pa_stream_disconnect(m_stream); pa_stream_unref(m_stream); } Status PulseAudioRenderer::Start() { if (m_state != PA_STREAM_READY) { return Status::Expired; } pa_operation* operation = pa_stream_cork(m_stream, false, nullptr, nullptr); return m_manager->HandleOperation(operation, "pa_stream_cork"); } Status PulseAudioRenderer::Stop() { if (m_state != PA_STREAM_READY) { return Status::Expired; } pa_operation* operation = pa_stream_drain(m_stream, nullptr, nullptr); ATOM_FORWARD_ERROR(m_manager->HandleOperation(operation, "pa_stream_drain")) operation = pa_stream_cork(m_stream, false, nullptr, nullptr); return m_manager->HandleOperation(operation, "pa_stream_cork"); } Status PulseAudioRenderer::Submit(std::span frames) { if (m_state != PA_STREAM_READY) { return Status::Expired; } const usize size = frames.size_bytes(); if (size % m_blockSize != 0) { return Status::InvalidParameters; } const int result = -pa_stream_write(m_stream, frames.data(), size, nullptr, 0, PA_SEEK_RELATIVE); switch (result) { case PA_OK: { break; } default: { ATOM_PANIC("pa_stream_write failed with {}", result); } } return Status::Success; } Result PulseAudioRenderer::GetQueuedFrameCount() { ATOM_UNWRAP_OR_FORWARD_ERROR(u32, capacity, GetFrameCapacity()) const usize size = pa_stream_writable_size(m_stream); if (size == static_cast(-1)) { const int error = -pa_context_errno(m_manager->m_context); switch (error) { // TODO(Gloria): handle more errors default: { ATOM_PANIC("pa_stream_writable_size failed with {}", error); } } } return capacity - size; } void PulseAudioRenderer::StateCallback(pa_stream* stream, void* userdata) { auto renderer = static_cast(userdata); ATOM_ASSERT(renderer != nullptr, "userdata is nullptr"); if (stream != renderer->m_stream) { return; // this often happens during destruction, just assume it's none of our business } renderer->m_state = pa_stream_get_state(renderer->m_stream); } }