From a45b8bc9bc64c9a86dd7c5a3e20e0996503754ae Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 19 Feb 2024 23:38:09 -0500 Subject: [PATCH] audio: rewrite IAudioOutManager --- src/core/CMakeLists.txt | 4 +- src/core/hle/service/audio/audio.cpp | 4 +- .../hle/service/audio/audio_out_manager.cpp | 103 +++++++++++++++ .../hle/service/audio/audio_out_manager.h | 44 +++++++ src/core/hle/service/audio/audout_u.cpp | 118 ------------------ src/core/hle/service/audio/audout_u.h | 37 ------ 6 files changed, 151 insertions(+), 159 deletions(-) create mode 100644 src/core/hle/service/audio/audio_out_manager.cpp create mode 100644 src/core/hle/service/audio/audio_out_manager.h delete mode 100644 src/core/hle/service/audio/audout_u.cpp delete mode 100644 src/core/hle/service/audio/audout_u.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 1255ee6bf..c0f828bce 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -493,14 +493,14 @@ add_library(core STATIC hle/service/audio/audio_in_manager.h hle/service/audio/audio_in.cpp hle/service/audio/audio_in.h + hle/service/audio/audio_out_manager.cpp + hle/service/audio/audio_out_manager.h hle/service/audio/audio_out.cpp hle/service/audio/audio_out.h hle/service/audio/audio.cpp hle/service/audio/audio.h hle/service/audio/audio_controller.cpp hle/service/audio/audio_controller.h - hle/service/audio/audout_u.cpp - hle/service/audio/audout_u.h hle/service/audio/audrec_a.cpp hle/service/audio/audrec_a.h hle/service/audio/audrec_u.cpp diff --git a/src/core/hle/service/audio/audio.cpp b/src/core/hle/service/audio/audio.cpp index 7dcfbbfad..aa5b9dbfe 100644 --- a/src/core/hle/service/audio/audio.cpp +++ b/src/core/hle/service/audio/audio.cpp @@ -5,7 +5,7 @@ #include "core/hle/service/audio/audio.h" #include "core/hle/service/audio/audio_controller.h" #include "core/hle/service/audio/audio_in_manager.h" -#include "core/hle/service/audio/audout_u.h" +#include "core/hle/service/audio/audio_out_manager.h" #include "core/hle/service/audio/audrec_a.h" #include "core/hle/service/audio/audrec_u.h" #include "core/hle/service/audio/audren_u.h" @@ -20,7 +20,7 @@ void LoopProcess(Core::System& system) { server_manager->RegisterNamedService("audctl", std::make_shared(system)); server_manager->RegisterNamedService("audin:u", std::make_shared(system)); - server_manager->RegisterNamedService("audout:u", std::make_shared(system)); + server_manager->RegisterNamedService("audout:u", std::make_shared(system)); server_manager->RegisterNamedService("audrec:a", std::make_shared(system)); server_manager->RegisterNamedService("audrec:u", std::make_shared(system)); server_manager->RegisterNamedService("audren:u", std::make_shared(system)); diff --git a/src/core/hle/service/audio/audio_out_manager.cpp b/src/core/hle/service/audio/audio_out_manager.cpp new file mode 100644 index 000000000..89cd6df94 --- /dev/null +++ b/src/core/hle/service/audio/audio_out_manager.cpp @@ -0,0 +1,103 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "common/string_util.h" +#include "core/hle/service/audio/audio_out.h" +#include "core/hle/service/audio/audio_out_manager.h" +#include "core/hle/service/cmif_serialization.h" +#include "core/memory.h" + +namespace Service::Audio { +using namespace AudioCore::AudioOut; + +IAudioOutManager::IAudioOutManager(Core::System& system_) + : ServiceFramework{system_, "audout:u"}, impl{std::make_unique(system_)} { + // clang-format off + static const FunctionInfo functions[] = { + {0, C<&IAudioOutManager::ListAudioOuts>, "ListAudioOuts"}, + {1, C<&IAudioOutManager::OpenAudioOut>, "OpenAudioOut"}, + {2, C<&IAudioOutManager::ListAudioOutsAuto>, "ListAudioOutsAuto"}, + {3, C<&IAudioOutManager::OpenAudioOutAuto>, "OpenAudioOutAuto"}, + }; + // clang-format on + + RegisterHandlers(functions); +} + +IAudioOutManager::~IAudioOutManager() = default; + +Result IAudioOutManager::ListAudioOuts( + OutArray out_audio_outs, Out out_count) { + R_RETURN(this->ListAudioOutsAuto(out_audio_outs, out_count)); +} + +Result IAudioOutManager::OpenAudioOut(Out out_parameter_internal, + Out> out_audio_out, + OutArray out_name, + InArray name, + AudioOutParameter parameter, + InCopyHandle process_handle, + ClientAppletResourceUserId aruid) { + R_RETURN(this->OpenAudioOutAuto(out_parameter_internal, out_audio_out, out_name, name, + parameter, process_handle, aruid)); +} + +Result IAudioOutManager::ListAudioOutsAuto( + OutArray out_audio_outs, Out out_count) { + if (!out_audio_outs.empty()) { + out_audio_outs[0] = AudioDeviceName("DeviceOut"); + *out_count = 1; + LOG_DEBUG(Service_Audio, "called. \nName=DeviceOut"); + } else { + *out_count = 0; + LOG_DEBUG(Service_Audio, "called. Empty buffer passed in."); + } + + R_SUCCEED(); +} + +Result IAudioOutManager::OpenAudioOutAuto( + Out out_parameter_internal, + Out> out_audio_out, + OutArray out_name, + InArray name, AudioOutParameter parameter, + InCopyHandle process_handle, ClientAppletResourceUserId aruid) { + if (!process_handle) { + LOG_ERROR(Service_Audio, "Failed to get process handle"); + R_THROW(ResultUnknown); + } + if (name.empty() || out_name.empty()) { + LOG_ERROR(Service_Audio, "Invalid buffers"); + R_THROW(ResultUnknown); + } + + size_t new_session_id{}; + R_TRY(impl->LinkToManager()); + R_TRY(impl->AcquireSessionId(new_session_id)); + + const auto name_buffer = std::span(reinterpret_cast(name[0].name.data()), 0x100); + const auto device_name = Common::StringFromBuffer(name_buffer); + + LOG_DEBUG(Service_Audio, "Opening new AudioOut, sessionid={}, free sessions={}", new_session_id, + impl->num_free_sessions); + + auto audio_out = std::make_shared(system, *impl, new_session_id, device_name, + parameter, process_handle.Get(), aruid.pid); + R_TRY(audio_out->GetImpl()->GetSystem().Initialize(device_name, parameter, process_handle.Get(), + aruid.pid)); + + *out_audio_out = audio_out; + impl->sessions[new_session_id] = audio_out->GetImpl(); + impl->applet_resource_user_ids[new_session_id] = aruid.pid; + + auto& out_system = impl->sessions[new_session_id]->GetSystem(); + *out_parameter_internal = + AudioOutParameterInternal{.sample_rate = out_system.GetSampleRate(), + .channel_count = out_system.GetChannelCount(), + .sample_format = static_cast(out_system.GetSampleFormat()), + .state = static_cast(out_system.GetState())}; + + R_SUCCEED(); +} + +} // namespace Service::Audio diff --git a/src/core/hle/service/audio/audio_out_manager.h b/src/core/hle/service/audio/audio_out_manager.h new file mode 100644 index 000000000..eaa27bc79 --- /dev/null +++ b/src/core/hle/service/audio/audio_out_manager.h @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "audio_core/audio_out_manager.h" +#include "audio_core/out/audio_out.h" +#include "core/hle/service/cmif_types.h" +#include "core/hle/service/service.h" + +namespace Service::Audio { + +using AudioDeviceName = AudioCore::Renderer::AudioDevice::AudioDeviceName; +class IAudioOut; + +class IAudioOutManager final : public ServiceFramework { +public: + explicit IAudioOutManager(Core::System& system_); + ~IAudioOutManager() override; + +private: + Result ListAudioOuts(OutArray out_audio_outs, + Out out_count); + Result OpenAudioOut(Out out_parameter_internal, + Out> out_audio_out, + OutArray out_name, + InArray name, + AudioCore::AudioOut::AudioOutParameter parameter, + InCopyHandle process_handle, + ClientAppletResourceUserId aruid); + Result ListAudioOutsAuto(OutArray out_audio_outs, + Out out_count); + Result OpenAudioOutAuto( + Out out_parameter_internal, + Out> out_audio_out, + OutArray out_name, + InArray name, + AudioCore::AudioOut::AudioOutParameter parameter, + InCopyHandle process_handle, ClientAppletResourceUserId aruid); + + std::unique_ptr impl; +}; + +} // namespace Service::Audio diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp deleted file mode 100644 index 5364177ba..000000000 --- a/src/core/hle/service/audio/audout_u.cpp +++ /dev/null @@ -1,118 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#include "common/string_util.h" -#include "core/hle/service/audio/audio_out.h" -#include "core/hle/service/audio/audout_u.h" -#include "core/hle/service/ipc_helpers.h" -#include "core/memory.h" - -namespace Service::Audio { -using namespace AudioCore::AudioOut; - -AudOutU::AudOutU(Core::System& system_) - : ServiceFramework{system_, "audout:u"}, service_context{system_, "AudOutU"}, - impl{std::make_unique(system_)} { - // clang-format off - static const FunctionInfo functions[] = { - {0, &AudOutU::ListAudioOuts, "ListAudioOuts"}, - {1, &AudOutU::OpenAudioOut, "OpenAudioOut"}, - {2, &AudOutU::ListAudioOuts, "ListAudioOutsAuto"}, - {3, &AudOutU::OpenAudioOut, "OpenAudioOutAuto"}, - }; - // clang-format on - - RegisterHandlers(functions); -} - -AudOutU::~AudOutU() = default; - -void AudOutU::ListAudioOuts(HLERequestContext& ctx) { - using namespace AudioCore::Renderer; - - std::scoped_lock l{impl->mutex}; - - const auto write_count = - static_cast(ctx.GetWriteBufferNumElements()); - std::vector device_names{}; - if (write_count > 0) { - device_names.emplace_back("DeviceOut"); - LOG_DEBUG(Service_Audio, "called. \nName=DeviceOut"); - } else { - LOG_DEBUG(Service_Audio, "called. Empty buffer passed in."); - } - - ctx.WriteBuffer(device_names); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(static_cast(device_names.size())); -} - -void AudOutU::OpenAudioOut(HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - auto in_params{rp.PopRaw()}; - auto applet_resource_user_id{rp.PopRaw()}; - const auto device_name_data{ctx.ReadBuffer()}; - auto device_name = Common::StringFromBuffer(device_name_data); - auto handle{ctx.GetCopyHandle(0)}; - - auto process{ctx.GetObjectFromHandle(handle)}; - if (process.IsNull()) { - LOG_ERROR(Service_Audio, "Failed to get process handle"); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultUnknown); - return; - } - - auto link{impl->LinkToManager()}; - if (link.IsError()) { - LOG_ERROR(Service_Audio, "Failed to link Audio Out to Audio Manager"); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(link); - return; - } - - size_t new_session_id{}; - auto result{impl->AcquireSessionId(new_session_id)}; - if (result.IsError()) { - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(result); - return; - } - - LOG_DEBUG(Service_Audio, "Opening new AudioOut, sessionid={}, free sessions={}", new_session_id, - impl->num_free_sessions); - - auto audio_out = - std::make_shared(system, *impl, new_session_id, device_name, in_params, - process.GetPointerUnsafe(), applet_resource_user_id); - result = audio_out->GetImpl()->GetSystem().Initialize( - device_name, in_params, process.GetPointerUnsafe(), applet_resource_user_id); - if (result.IsError()) { - LOG_ERROR(Service_Audio, "Failed to initialize the AudioOut System!"); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(result); - return; - } - - impl->sessions[new_session_id] = audio_out->GetImpl(); - impl->applet_resource_user_ids[new_session_id] = applet_resource_user_id; - - auto& out_system = impl->sessions[new_session_id]->GetSystem(); - AudioOutParameterInternal out_params{.sample_rate = out_system.GetSampleRate(), - .channel_count = out_system.GetChannelCount(), - .sample_format = - static_cast(out_system.GetSampleFormat()), - .state = static_cast(out_system.GetState())}; - - IPC::ResponseBuilder rb{ctx, 6, 0, 1}; - - ctx.WriteBuffer(out_system.GetName()); - - rb.Push(ResultSuccess); - rb.PushRaw(out_params); - rb.PushIpcInterface(audio_out); -} - -} // namespace Service::Audio diff --git a/src/core/hle/service/audio/audout_u.h b/src/core/hle/service/audio/audout_u.h deleted file mode 100644 index 8f288c6e0..000000000 --- a/src/core/hle/service/audio/audout_u.h +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -#include "audio_core/audio_out_manager.h" -#include "audio_core/out/audio_out.h" -#include "core/hle/service/kernel_helpers.h" -#include "core/hle/service/service.h" - -namespace Core { -class System; -} - -namespace AudioCore::AudioOut { -class Manager; -class Out; -} // namespace AudioCore::AudioOut - -namespace Service::Audio { - -class IAudioOut; - -class AudOutU final : public ServiceFramework { -public: - explicit AudOutU(Core::System& system_); - ~AudOutU() override; - -private: - void ListAudioOuts(HLERequestContext& ctx); - void OpenAudioOut(HLERequestContext& ctx); - - KernelHelpers::ServiceContext service_context; - std::unique_ptr impl; -}; - -} // namespace Service::Audio