2026-09-23 02:07:39

Signed-off-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
lizzie 2026-09-23 02:07:39 +00:00
parent ccb257c729
commit 737535dc2b
13 changed files with 52 additions and 94 deletions

View file

@ -19,9 +19,9 @@ ADSP::ADSP(Core::System& system, Sink::Sink& sink) {
} }
} }
void ADSP::NotifyShutdown() { void ADSP::Shutdown() {
audio_renderer->NotifyShutdown(); audio_renderer->Stop();
opus_decoder->NotifyShutdown(); opus_decoder->Shutdown();
} }
AudioRenderer::AudioRenderer& ADSP::AudioRenderer() { AudioRenderer::AudioRenderer& ADSP::AudioRenderer() {

View file

@ -45,7 +45,7 @@ public:
explicit ADSP(Core::System& system, Sink::Sink& sink); explicit ADSP(Core::System& system, Sink::Sink& sink);
~ADSP() = default; ~ADSP() = default;
void NotifyShutdown(); void Shutdown();
AudioRenderer::AudioRenderer& AudioRenderer(); AudioRenderer::AudioRenderer& AudioRenderer();
OpusDecoder::OpusDecoder& OpusDecoder(); OpusDecoder::OpusDecoder& OpusDecoder();

View file

@ -34,36 +34,19 @@ void AudioRenderer::Start() {
mailbox.Send(Direction::DSP, Message::InitializeOK); mailbox.Send(Direction::DSP, Message::InitializeOK);
if (mailbox.Receive(Direction::Host) != Message::InitializeOK) { if (mailbox.Receive(Direction::Host) != Message::InitializeOK) {
LOG_ERROR(Service_Audio, "Host Audio Renderer -- Failed to receive shutdown " LOG_ERROR(Service_Audio, "Host Audio Renderer -- Failed to receive shutdown message response from ADSP!");
"message response from ADSP!");
return;
}
running = true;
}
void AudioRenderer::NotifyShutdown() {
if (main_thread.joinable()) {
main_thread.request_stop();
mailbox.Send(Direction::DSP, Message::Shutdown);
main_thread.join();
} }
} }
void AudioRenderer::Stop() { void AudioRenderer::Stop() {
if (!running) {
return;
}
if (main_thread.joinable()) { if (main_thread.joinable()) {
main_thread.request_stop(); main_thread.request_stop();
mailbox.Send(Direction::DSP, Message::Shutdown); mailbox.Send(Direction::DSP, Message::Shutdown);
if (mailbox.Receive(Direction::Host, main_thread.get_stop_token()) != Message::Shutdown) { if (mailbox.Receive(Direction::Host, main_thread.get_stop_token()) != Message::Shutdown) {
LOG_ERROR(Service_Audio, "Host Audio Renderer -- Failed to receive shutdown " LOG_ERROR(Service_Audio, "Host Audio Renderer -- Failed to receive shutdown message response from ADSP!");
"message response from ADSP!");
} }
main_thread.join(); main_thread.join();
} }
for (auto& stream : streams) { for (auto& stream : streams) {
if (stream) { if (stream) {
stream->Stop(); stream->Stop();
@ -71,7 +54,6 @@ void AudioRenderer::Stop() {
stream = nullptr; stream = nullptr;
} }
} }
running = false;
} }
void AudioRenderer::Signal() { void AudioRenderer::Signal() {

View file

@ -104,8 +104,6 @@ private:
Mailbox mailbox; Mailbox mailbox;
/// Main thread /// Main thread
std::jthread main_thread{}; std::jthread main_thread{};
/// The current state
std::atomic<bool> running{};
/// Shared memory of input command buffers, set by host, read by DSP /// Shared memory of input command buffers, set by host, read by DSP
std::array<CommandBuffer, MaxRendererSessions> command_buffers{}; std::array<CommandBuffer, MaxRendererSessions> command_buffers{};
/// The command lists to process /// The command lists to process

View file

@ -82,11 +82,11 @@ void OpusDecoder::Main(std::stop_token stop_token) {
break; break;
switch (msg) { switch (msg) {
case Shutdown: case Message::Shutdown:
Send(Direction::Host, Message::ShutdownOK); Send(Direction::Host, Message::ShutdownOK);
return; return;
case GetWorkBufferSize: { case Message::GetWorkBufferSize: {
auto channel_count = static_cast<s32>(shared_memory->host_send_data[0]); auto channel_count = static_cast<s32>(shared_memory->host_send_data[0]);
ASSERT(IsValidChannelCount(channel_count)); ASSERT(IsValidChannelCount(channel_count));
@ -95,7 +95,7 @@ void OpusDecoder::Main(std::stop_token stop_token) {
Send(Direction::Host, Message::GetWorkBufferSizeOK); Send(Direction::Host, Message::GetWorkBufferSizeOK);
} break; } break;
case InitializeDecodeObject: { case Message::InitializeDecodeObject: {
auto buffer = shared_memory->host_send_data[0]; auto buffer = shared_memory->host_send_data[0];
auto buffer_size = shared_memory->host_send_data[1]; auto buffer_size = shared_memory->host_send_data[1];
auto sample_rate = static_cast<s32>(shared_memory->host_send_data[2]); auto sample_rate = static_cast<s32>(shared_memory->host_send_data[2]);
@ -112,7 +112,7 @@ void OpusDecoder::Main(std::stop_token stop_token) {
Send(Direction::Host, Message::InitializeDecodeObjectOK); Send(Direction::Host, Message::InitializeDecodeObjectOK);
} break; } break;
case ShutdownDecodeObject: { case Message::ShutdownDecodeObject: {
auto buffer = shared_memory->host_send_data[0]; auto buffer = shared_memory->host_send_data[0];
[[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1]; [[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1];
@ -122,7 +122,7 @@ void OpusDecoder::Main(std::stop_token stop_token) {
Send(Direction::Host, Message::ShutdownDecodeObjectOK); Send(Direction::Host, Message::ShutdownDecodeObjectOK);
} break; } break;
case DecodeInterleaved: { case Message::DecodeInterleaved: {
auto start_time = system.CoreTiming().GetGlobalTimeUs(); auto start_time = system.CoreTiming().GetGlobalTimeUs();
auto buffer = shared_memory->host_send_data[0]; auto buffer = shared_memory->host_send_data[0];
@ -160,19 +160,19 @@ void OpusDecoder::Main(std::stop_token stop_token) {
Send(Direction::Host, Message::DecodeInterleavedOK); Send(Direction::Host, Message::DecodeInterleavedOK);
} break; } break;
case MapMemory: { case Message::MapMemory: {
[[maybe_unused]] auto buffer = shared_memory->host_send_data[0]; [[maybe_unused]] auto buffer = shared_memory->host_send_data[0];
[[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1]; [[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1];
Send(Direction::Host, Message::MapMemoryOK); Send(Direction::Host, Message::MapMemoryOK);
} break; } break;
case UnmapMemory: { case Message::UnmapMemory: {
[[maybe_unused]] auto buffer = shared_memory->host_send_data[0]; [[maybe_unused]] auto buffer = shared_memory->host_send_data[0];
[[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1]; [[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1];
Send(Direction::Host, Message::UnmapMemoryOK); Send(Direction::Host, Message::UnmapMemoryOK);
} break; } break;
case GetWorkBufferSizeForMultiStream: { case Message::GetWorkBufferSizeForMultiStream: {
auto total_stream_count = static_cast<s32>(shared_memory->host_send_data[0]); auto total_stream_count = static_cast<s32>(shared_memory->host_send_data[0]);
auto stereo_stream_count = static_cast<s32>(shared_memory->host_send_data[1]); auto stereo_stream_count = static_cast<s32>(shared_memory->host_send_data[1]);
@ -183,7 +183,7 @@ void OpusDecoder::Main(std::stop_token stop_token) {
Send(Direction::Host, Message::GetWorkBufferSizeForMultiStreamOK); Send(Direction::Host, Message::GetWorkBufferSizeForMultiStreamOK);
} break; } break;
case InitializeMultiStreamDecodeObject: { case Message::InitializeMultiStreamDecodeObject: {
auto buffer = shared_memory->host_send_data[0]; auto buffer = shared_memory->host_send_data[0];
auto buffer_size = shared_memory->host_send_data[1]; auto buffer_size = shared_memory->host_send_data[1];
auto sample_rate = static_cast<s32>(shared_memory->host_send_data[2]); auto sample_rate = static_cast<s32>(shared_memory->host_send_data[2]);
@ -210,7 +210,7 @@ void OpusDecoder::Main(std::stop_token stop_token) {
Send(Direction::Host, Message::InitializeMultiStreamDecodeObjectOK); Send(Direction::Host, Message::InitializeMultiStreamDecodeObjectOK);
} break; } break;
case ShutdownMultiStreamDecodeObject: { case Message::ShutdownMultiStreamDecodeObject: {
auto buffer = shared_memory->host_send_data[0]; auto buffer = shared_memory->host_send_data[0];
[[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1]; [[maybe_unused]] auto buffer_size = shared_memory->host_send_data[1];
@ -220,7 +220,7 @@ void OpusDecoder::Main(std::stop_token stop_token) {
Send(Direction::Host, Message::ShutdownMultiStreamDecodeObjectOK); Send(Direction::Host, Message::ShutdownMultiStreamDecodeObjectOK);
} break; } break;
case DecodeInterleavedForMultiStream: { case Message::DecodeInterleavedForMultiStream: {
auto start_time = system.CoreTiming().GetGlobalTimeUs(); auto start_time = system.CoreTiming().GetGlobalTimeUs();
auto buffer = shared_memory->host_send_data[0]; auto buffer = shared_memory->host_send_data[0];
@ -265,10 +265,15 @@ void OpusDecoder::Main(std::stop_token stop_token) {
} }
} }
void OpusDecoder::NotifyShutdown() { void OpusDecoder::Shutdown() {
init_thread.request_stop(); if (init_thread.joinable()) {
main_thread.request_stop(); init_thread.request_stop();
Send(Direction::DSP, Message::Shutdown); init_thread.join(); //must explicitly wait for init to join
}
if (main_thread.joinable()) {
main_thread.request_stop();
Send(Direction::DSP, Message::Shutdown);
}
} }
} // namespace AudioCore::ADSP::OpusDecoder } // namespace AudioCore::ADSP::OpusDecoder

View file

@ -67,7 +67,7 @@ public:
shared_memory = &shared_memory_; shared_memory = &shared_memory_;
} }
void NotifyShutdown(); void Shutdown();
private: private:
/** /**

View file

@ -31,13 +31,9 @@ void AudioCore::CreateSinks() {
input_sink = Sink::CreateSinkFromID(sink_id.GetValue(), audio_input_device_id.GetValue()); input_sink = Sink::CreateSinkFromID(sink_id.GetValue(), audio_input_device_id.GetValue());
} }
void AudioCore::NotifyShutdown() {
audio_manager->NotifyShutdown();
adsp->NotifyShutdown();
}
void AudioCore::Shutdown() { void AudioCore::Shutdown() {
audio_manager->Shutdown(); audio_manager->Shutdown();
adsp->Shutdown();
} }
AudioManager& AudioCore::GetAudioManager() { AudioManager& AudioCore::GetAudioManager() {

View file

@ -24,8 +24,6 @@ public:
explicit AudioCore(Core::System& system); explicit AudioCore(Core::System& system);
~AudioCore(); ~AudioCore();
void NotifyShutdown();
/** /**
* Shutdown the audio core. * Shutdown the audio core.
*/ */

View file

@ -34,15 +34,10 @@ AudioManager::AudioManager() {
}); });
} }
void AudioManager::NotifyShutdown() {
events.SetAudioEvent(Event::Type::Max, true);
thread.request_stop();
}
void AudioManager::Shutdown() { void AudioManager::Shutdown() {
events.SetAudioEvent(Event::Type::Max, true);
if (thread.joinable()) { if (thread.joinable()) {
thread.request_stop(); thread.request_stop();
events.SetAudioEvent(Event::Type::Max, true);
thread.join(); thread.join();
} }
} }

View file

@ -39,9 +39,6 @@ class AudioManager {
public: public:
explicit AudioManager(); explicit AudioManager();
/// @brief Notify of impending shutdown
void NotifyShutdown();
/// @brief Shutdown the audio manager. /// @brief Shutdown the audio manager.
void Shutdown(); void Shutdown();

View file

@ -11,71 +11,64 @@
#include "audio_core/renderer/system_manager.h" #include "audio_core/renderer/system_manager.h"
#include "common/thread.h" #include "common/thread.h"
#include "core/core.h" #include "core/core.h"
#include "core/hle/kernel/kernel.h"
#include "core/core_timing.h" #include "core/core_timing.h"
namespace AudioCore::Renderer { namespace AudioCore::Renderer {
SystemManager::SystemManager(Core::System& core_) SystemManager::SystemManager(Core::System& core_)
: core{core_}, audio_renderer{core.AudioCore().ADSP().AudioRenderer()} {} : core{core_}
{}
SystemManager::~SystemManager() { SystemManager::~SystemManager() {
Stop(); Stop();
} }
void SystemManager::InitializeUnsafe() { void SystemManager::InitializeUnsafe() {
if (!active) { if (!thread.joinable()) {
active = true; thread = core.Kernel().RunOnHostCoreThread("AudioRenderSystemManager", [this]() {
audio_renderer.Start(); auto& audio_renderer = core.AudioCore().ADSP().AudioRenderer();
thread = std::jthread([this](std::stop_token stop_token) { audio_renderer.Start();
Common::SetCurrentThreadName("AudioRenderSystemManager"); auto const stop_token = thread.get_stop_token();
Common::SetCurrentThreadPriority(Common::ThreadPriority::High); while (!stop_token.stop_requested()) {
while (active && !stop_token.stop_requested()) {
{ {
std::scoped_lock l{mutex1}; std::scoped_lock lk{mutex};
for (auto system : systems) for (auto system : systems)
system->SendCommandToDsp(); system->SendCommandToDsp();
} }
audio_renderer.Signal(); audio_renderer.Signal();
audio_renderer.Wait(stop_token); audio_renderer.Wait(stop_token);
} }
audio_renderer.Stop();
}); });
} }
} }
void SystemManager::Stop() { void SystemManager::Stop() {
if (active) { if (thread.joinable()) {
active = false;
thread.request_stop(); thread.request_stop();
thread.join(); thread.join();
audio_renderer.Stop();
} }
} }
bool SystemManager::Add(System& system_) { bool SystemManager::Add(System& system_) {
std::scoped_lock l2{mutex2}; std::scoped_lock lk{mutex};
if (systems.size() + 1 > MaxRendererSessions) { if (systems.size() >= MaxRendererSessions) {
LOG_ERROR(Service_Audio, "Maximum AudioRenderer Systems active, cannot add more!"); LOG_ERROR(Service_Audio, "Maximum AudioRenderer Systems active, cannot add more!");
return false; return false;
} }
{ if (systems.empty())
std::scoped_lock l{mutex1}; InitializeUnsafe();
if (systems.empty())
InitializeUnsafe();
}
systems.push_back(&system_); systems.push_back(&system_);
return true; return true;
} }
bool SystemManager::Remove(System& system_) { bool SystemManager::Remove(System& system_) {
std::scoped_lock l2{mutex2}; std::scoped_lock lk{mutex};
{ if (systems.remove(&system_) == 0) {
std::scoped_lock l{mutex1}; LOG_ERROR(Service_Audio, "Failed to remove a render system, it was not found in the list!");
if (systems.remove(&system_) == 0) { return false;
LOG_ERROR(Service_Audio, "Failed to remove a render system, it was not found in the list!");
return false;
}
} }
if (systems.empty()) if (systems.empty())
Stop(); Stop();
return true; return true;

View file

@ -75,14 +75,8 @@ private:
std::list<System*> systems{}; std::list<System*> systems{};
/// Main worker thread for generating command lists /// Main worker thread for generating command lists
std::jthread thread; std::jthread thread;
/// Mutex for the systems /// Mutex for the reading and adding/removing systems
std::mutex mutex1{}; std::mutex mutex{};
/// Mutex for adding/removing systems
std::mutex mutex2{};
/// Is the system manager thread active?
std::atomic<bool> active{};
/// Reference to the ADSP's AudioRenderer for communication
::AudioCore::ADSP::AudioRenderer::AudioRenderer& audio_renderer;
}; };
} // namespace AudioCore::Renderer } // namespace AudioCore::Renderer

View file

@ -442,7 +442,7 @@ struct System::Impl {
kernel.ShutdownCores(); kernel.ShutdownCores();
// Notify services helpers of shutdown // Notify services helpers of shutdown
audio_core->NotifyShutdown(); audio_core->Shutdown();
// Wait for threads/services to join // Wait for threads/services to join
kernel.CloseServices(); kernel.CloseServices();