From a05bd3c47ef4d77411c150e5bdbd1efacc5bde68 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 19 Feb 2024 23:13:19 -0500 Subject: [PATCH] audio: rewrite IAudioOut --- src/audio_core/device/audio_buffers.h | 4 +- src/core/CMakeLists.txt | 2 + src/core/hle/service/audio/audio_in.cpp | 26 ++- src/core/hle/service/audio/audio_in.h | 9 +- src/core/hle/service/audio/audio_out.cpp | 146 ++++++++++++++++ src/core/hle/service/audio/audio_out.h | 58 +++++++ src/core/hle/service/audio/audout_u.cpp | 207 +---------------------- 7 files changed, 236 insertions(+), 216 deletions(-) create mode 100644 src/core/hle/service/audio/audio_out.cpp create mode 100644 src/core/hle/service/audio/audio_out.h diff --git a/src/audio_core/device/audio_buffers.h b/src/audio_core/device/audio_buffers.h index 5d8ed0ef7..25da4c8a2 100644 --- a/src/audio_core/device/audio_buffers.h +++ b/src/audio_core/device/audio_buffers.h @@ -146,11 +146,11 @@ public: break; } - tags[released++] = tag; - if (released >= tags.size()) { break; } + + tags[released++] = tag; } return released; diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 51fe8c500..1255ee6bf 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -493,6 +493,8 @@ 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.cpp + hle/service/audio/audio_out.h hle/service/audio/audio.cpp hle/service/audio/audio.h hle/service/audio/audio_controller.cpp diff --git a/src/core/hle/service/audio/audio_in.cpp b/src/core/hle/service/audio/audio_in.cpp index d847e0fc8..31d136077 100644 --- a/src/core/hle/service/audio/audio_in.cpp +++ b/src/core/hle/service/audio/audio_in.cpp @@ -21,12 +21,12 @@ IAudioIn::IAudioIn(Core::System& system_, Manager& manager, size_t session_id, {2, C<&IAudioIn::Stop>, "Stop"}, {3, C<&IAudioIn::AppendAudioInBuffer>, "AppendAudioInBuffer"}, {4, C<&IAudioIn::RegisterBufferEvent>, "RegisterBufferEvent"}, - {5, C<&IAudioIn::GetReleasedAudioInBuffer>, "GetReleasedAudioInBuffer"}, + {5, C<&IAudioIn::GetReleasedAudioInBuffers>, "GetReleasedAudioInBuffers"}, {6, C<&IAudioIn::ContainsAudioInBuffer>, "ContainsAudioInBuffer"}, {7, C<&IAudioIn::AppendAudioInBuffer>, "AppendUacInBuffer"}, - {8, C<&IAudioIn::AppendAudioInBuffer>, "AppendAudioInBufferAuto"}, - {9, C<&IAudioIn::GetReleasedAudioInBuffer>, "GetReleasedAudioInBuffersAuto"}, - {10, C<&IAudioIn::AppendAudioInBuffer>, "AppendUacInBufferAuto"}, + {8, C<&IAudioIn::AppendAudioInBufferAuto>, "AppendAudioInBufferAuto"}, + {9, C<&IAudioIn::GetReleasedAudioInBuffersAuto>, "GetReleasedAudioInBuffersAuto"}, + {10, C<&IAudioIn::AppendAudioInBufferAuto>, "AppendUacInBufferAuto"}, {11, C<&IAudioIn::GetAudioInBufferCount>, "GetAudioInBufferCount"}, {12, C<&IAudioIn::SetDeviceGain>, "SetDeviceGain"}, {13, C<&IAudioIn::GetDeviceGain>, "GetDeviceGain"}, @@ -69,6 +69,11 @@ Result IAudioIn::Stop() { Result IAudioIn::AppendAudioInBuffer(InArray buffer, u64 buffer_client_ptr) { + R_RETURN(this->AppendAudioInBufferAuto(buffer, buffer_client_ptr)); +} + +Result IAudioIn::AppendAudioInBufferAuto(InArray buffer, + u64 buffer_client_ptr) { if (buffer.empty()) { LOG_ERROR(Service_Audio, "Input buffer is too small for an AudioInBuffer!"); R_THROW(Audio::ResultInsufficientBuffer); @@ -87,8 +92,17 @@ Result IAudioIn::RegisterBufferEvent(OutCopyHandle out_e R_SUCCEED(); } -Result IAudioIn::GetReleasedAudioInBuffer(OutArray out_audio_buffer, - Out out_count) { +Result IAudioIn::GetReleasedAudioInBuffers(OutArray out_audio_buffer, + Out out_count) { + R_RETURN(this->GetReleasedAudioInBuffersAuto(out_audio_buffer, out_count)); +} + +Result IAudioIn::GetReleasedAudioInBuffersAuto( + OutArray out_audio_buffer, Out out_count) { + + if (!out_audio_buffer.empty()) { + out_audio_buffer[0] = 0; + } *out_count = impl->GetReleasedBuffers(out_audio_buffer); LOG_TRACE(Service_Audio, "called. Session {} released {} buffers", diff --git a/src/core/hle/service/audio/audio_in.h b/src/core/hle/service/audio/audio_in.h index 8d6398ef6..3fe1e1e87 100644 --- a/src/core/hle/service/audio/audio_in.h +++ b/src/core/hle/service/audio/audio_in.h @@ -28,9 +28,14 @@ public: Result AppendAudioInBuffer( InArray buffer, u64 buffer_client_ptr); + Result AppendAudioInBufferAuto( + InArray buffer, + u64 buffer_client_ptr); Result RegisterBufferEvent(OutCopyHandle out_event); - Result GetReleasedAudioInBuffer(OutArray out_audio_buffer, - Out out_count); + Result GetReleasedAudioInBuffers(OutArray out_audio_buffer, + Out out_count); + Result GetReleasedAudioInBuffersAuto(OutArray out_audio_buffer, + Out out_count); Result ContainsAudioInBuffer(Out out_contains_buffer, u64 buffer_client_ptr); Result GetAudioInBufferCount(Out out_buffer_count); Result SetDeviceGain(f32 device_gain); diff --git a/src/core/hle/service/audio/audio_out.cpp b/src/core/hle/service/audio/audio_out.cpp new file mode 100644 index 000000000..cd2dc1f6f --- /dev/null +++ b/src/core/hle/service/audio/audio_out.cpp @@ -0,0 +1,146 @@ +// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "audio_core/out/audio_out.h" +#include "audio_core/out/audio_out_system.h" +#include "core/hle/kernel/k_process.h" +#include "core/hle/service/audio/audio_out.h" +#include "core/hle/service/cmif_serialization.h" +#include "core/hle/service/kernel_helpers.h" +#include "core/hle/service/service.h" + +namespace Service::Audio { +using namespace AudioCore::AudioOut; + +IAudioOut::IAudioOut(Core::System& system_, Manager& manager, size_t session_id, + const std::string& device_name, const AudioOutParameter& in_params, + Kernel::KProcess* handle, u64 applet_resource_user_id) + : ServiceFramework{system_, "IAudioOut"}, service_context{system_, "IAudioOut"}, + event{service_context.CreateEvent("AudioOutEvent")}, process{handle}, + impl{std::make_shared(system_, manager, event, session_id)} { + + // clang-format off + static const FunctionInfo functions[] = { + {0, C<&IAudioOut::GetAudioOutState>, "GetAudioOutState"}, + {1, C<&IAudioOut::Start>, "Start"}, + {2, C<&IAudioOut::Stop>, "Stop"}, + {3, C<&IAudioOut::AppendAudioOutBuffer>, "AppendAudioOutBuffer"}, + {4, C<&IAudioOut::RegisterBufferEvent>, "RegisterBufferEvent"}, + {5, C<&IAudioOut::GetReleasedAudioOutBuffers>, "GetReleasedAudioOutBuffers"}, + {6, C<&IAudioOut::ContainsAudioOutBuffer>, "ContainsAudioOutBuffer"}, + {7, C<&IAudioOut::AppendAudioOutBufferAuto>, "AppendAudioOutBufferAuto"}, + {8, C<&IAudioOut::GetReleasedAudioOutBuffersAuto>, "GetReleasedAudioOutBuffersAuto"}, + {9, C<&IAudioOut::GetAudioOutBufferCount>, "GetAudioOutBufferCount"}, + {10, C<&IAudioOut::GetAudioOutPlayedSampleCount>, "GetAudioOutPlayedSampleCount"}, + {11, C<&IAudioOut::FlushAudioOutBuffers>, "FlushAudioOutBuffers"}, + {12, C<&IAudioOut::SetAudioOutVolume>, "SetAudioOutVolume"}, + {13, C<&IAudioOut::GetAudioOutVolume>, "GetAudioOutVolume"}, + }; + // clang-format on + RegisterHandlers(functions); + + process->Open(); +} + +IAudioOut::~IAudioOut() { + impl->Free(); + service_context.CloseEvent(event); + process->Close(); +} + +Result IAudioOut::GetAudioOutState(Out out_state) { + *out_state = static_cast(impl->GetState()); + LOG_DEBUG(Service_Audio, "called. state={}", *out_state); + R_SUCCEED(); +} + +Result IAudioOut::Start() { + LOG_DEBUG(Service_Audio, "called"); + R_RETURN(impl->StartSystem()); +} + +Result IAudioOut::Stop() { + LOG_DEBUG(Service_Audio, "called"); + R_RETURN(impl->StopSystem()); +} + +Result IAudioOut::AppendAudioOutBuffer( + InArray audio_out_buffer, u64 buffer_client_ptr) { + R_RETURN(this->AppendAudioOutBufferAuto(audio_out_buffer, buffer_client_ptr)); +} + +Result IAudioOut::AppendAudioOutBufferAuto( + InArray audio_out_buffer, u64 buffer_client_ptr) { + if (audio_out_buffer.empty()) { + LOG_ERROR(Service_Audio, "Input buffer is too small for an AudioOutBuffer!"); + R_THROW(Audio::ResultInsufficientBuffer); + } + + LOG_TRACE(Service_Audio, "called. Session {} Appending buffer {:08X}", + impl->GetSystem().GetSessionId(), buffer_client_ptr); + R_RETURN(impl->AppendBuffer(audio_out_buffer[0], buffer_client_ptr)); +} + +Result IAudioOut::RegisterBufferEvent(OutCopyHandle out_event) { + LOG_DEBUG(Service_Audio, "called"); + *out_event = &impl->GetBufferEvent(); + R_SUCCEED(); +} + +Result IAudioOut::GetReleasedAudioOutBuffers( + OutArray out_audio_buffer, Out out_count) { + R_RETURN(this->GetReleasedAudioOutBuffersAuto(out_audio_buffer, out_count)); +} + +Result IAudioOut::GetReleasedAudioOutBuffersAuto( + OutArray out_audio_buffer, Out out_count) { + + if (!out_audio_buffer.empty()) { + out_audio_buffer[0] = 0; + } + *out_count = impl->GetReleasedBuffers(out_audio_buffer); + + LOG_TRACE(Service_Audio, "called. Session {} released {} buffers", + impl->GetSystem().GetSessionId(), *out_count); + R_SUCCEED(); +} + +Result IAudioOut::ContainsAudioOutBuffer(Out out_contains_buffer, u64 buffer_client_ptr) { + *out_contains_buffer = impl->ContainsAudioBuffer(buffer_client_ptr); + + LOG_DEBUG(Service_Audio, "called. Is buffer {:08X} registered? {}", buffer_client_ptr, + *out_contains_buffer); + R_SUCCEED(); +} + +Result IAudioOut::GetAudioOutBufferCount(Out out_buffer_count) { + *out_buffer_count = impl->GetBufferCount(); + LOG_DEBUG(Service_Audio, "called. Buffer count={}", *out_buffer_count); + R_SUCCEED(); +} + +Result IAudioOut::GetAudioOutPlayedSampleCount(Out out_played_sample_count) { + *out_played_sample_count = impl->GetPlayedSampleCount(); + LOG_DEBUG(Service_Audio, "called. Played samples={}", *out_played_sample_count); + R_SUCCEED(); +} + +Result IAudioOut::FlushAudioOutBuffers(Out out_flushed) { + *out_flushed = impl->FlushAudioOutBuffers(); + LOG_DEBUG(Service_Audio, "called. Were any buffers flushed? {}", *out_flushed); + R_SUCCEED(); +} + +Result IAudioOut::SetAudioOutVolume(f32 volume) { + LOG_DEBUG(Service_Audio, "called. Volume={}", volume); + impl->SetVolume(volume); + R_SUCCEED(); +} + +Result IAudioOut::GetAudioOutVolume(Out out_volume) { + *out_volume = impl->GetVolume(); + LOG_DEBUG(Service_Audio, "called. Volume={}", *out_volume); + R_SUCCEED(); +} + +} // namespace Service::Audio diff --git a/src/core/hle/service/audio/audio_out.h b/src/core/hle/service/audio/audio_out.h new file mode 100644 index 000000000..779b213e7 --- /dev/null +++ b/src/core/hle/service/audio/audio_out.h @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: Copyright 2024 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_system.h" +#include "core/hle/service/cmif_types.h" +#include "core/hle/service/kernel_helpers.h" +#include "core/hle/service/service.h" + +namespace Kernel { +class KReadableEvent; +} + +namespace Service::Audio { + +class IAudioOut : public ServiceFramework { +public: + explicit IAudioOut(Core::System& system_, AudioCore::AudioOut::Manager& manager, + size_t session_id, const std::string& device_name, + const AudioCore::AudioOut::AudioOutParameter& in_params, + Kernel::KProcess* handle, u64 applet_resource_user_id); + ~IAudioOut() override; + + std::shared_ptr GetImpl() { + return impl; + } + + Result GetAudioOutState(Out out_state); + Result Start(); + Result Stop(); + Result AppendAudioOutBuffer( + InArray audio_out_buffer, + u64 buffer_client_ptr); + Result AppendAudioOutBufferAuto( + InArray audio_out_buffer, + u64 buffer_client_ptr); + Result RegisterBufferEvent(OutCopyHandle out_event); + Result GetReleasedAudioOutBuffers(OutArray out_audio_buffer, + Out out_count); + Result GetReleasedAudioOutBuffersAuto(OutArray out_audio_buffer, + Out out_count); + Result ContainsAudioOutBuffer(Out out_contains_buffer, u64 buffer_client_ptr); + Result GetAudioOutBufferCount(Out out_buffer_count); + Result GetAudioOutPlayedSampleCount(Out out_played_sample_count); + Result FlushAudioOutBuffers(Out out_flushed); + Result SetAudioOutVolume(f32 volume); + Result GetAudioOutVolume(Out out_volume); + +private: + KernelHelpers::ServiceContext service_context; + Kernel::KEvent* event; + Kernel::KProcess* process; + std::shared_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 index 8cc7b69f4..5364177ba 100644 --- a/src/core/hle/service/audio/audout_u.cpp +++ b/src/core/hle/service/audio/audout_u.cpp @@ -1,220 +1,15 @@ // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#include -#include -#include - -#include "audio_core/out/audio_out_system.h" -#include "audio_core/renderer/audio_device.h" -#include "common/common_funcs.h" -#include "common/logging/log.h" -#include "common/scratch_buffer.h" #include "common/string_util.h" -#include "common/swap.h" -#include "core/core.h" -#include "core/hle/kernel/k_event.h" +#include "core/hle/service/audio/audio_out.h" #include "core/hle/service/audio/audout_u.h" -#include "core/hle/service/audio/errors.h" #include "core/hle/service/ipc_helpers.h" #include "core/memory.h" namespace Service::Audio { using namespace AudioCore::AudioOut; -class IAudioOut final : public ServiceFramework { -public: - explicit IAudioOut(Core::System& system_, AudioCore::AudioOut::Manager& manager, - size_t session_id, const std::string& device_name, - const AudioOutParameter& in_params, Kernel::KProcess* handle, - u64 applet_resource_user_id) - : ServiceFramework{system_, "IAudioOut"}, service_context{system_, "IAudioOut"}, - event{service_context.CreateEvent("AudioOutEvent")}, process{handle}, - impl{std::make_shared(system_, manager, event, session_id)} { - - // clang-format off - static const FunctionInfo functions[] = { - {0, &IAudioOut::GetAudioOutState, "GetAudioOutState"}, - {1, &IAudioOut::Start, "Start"}, - {2, &IAudioOut::Stop, "Stop"}, - {3, &IAudioOut::AppendAudioOutBuffer, "AppendAudioOutBuffer"}, - {4, &IAudioOut::RegisterBufferEvent, "RegisterBufferEvent"}, - {5, &IAudioOut::GetReleasedAudioOutBuffers, "GetReleasedAudioOutBuffers"}, - {6, &IAudioOut::ContainsAudioOutBuffer, "ContainsAudioOutBuffer"}, - {7, &IAudioOut::AppendAudioOutBuffer, "AppendAudioOutBufferAuto"}, - {8, &IAudioOut::GetReleasedAudioOutBuffers, "GetReleasedAudioOutBuffersAuto"}, - {9, &IAudioOut::GetAudioOutBufferCount, "GetAudioOutBufferCount"}, - {10, &IAudioOut::GetAudioOutPlayedSampleCount, "GetAudioOutPlayedSampleCount"}, - {11, &IAudioOut::FlushAudioOutBuffers, "FlushAudioOutBuffers"}, - {12, &IAudioOut::SetAudioOutVolume, "SetAudioOutVolume"}, - {13, &IAudioOut::GetAudioOutVolume, "GetAudioOutVolume"}, - }; - // clang-format on - RegisterHandlers(functions); - - process->Open(); - } - - ~IAudioOut() override { - impl->Free(); - service_context.CloseEvent(event); - process->Close(); - } - - [[nodiscard]] std::shared_ptr GetImpl() { - return impl; - } - -private: - void GetAudioOutState(HLERequestContext& ctx) { - const auto state = static_cast(impl->GetState()); - - LOG_DEBUG(Service_Audio, "called. State={}", state); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(state); - } - - void Start(HLERequestContext& ctx) { - LOG_DEBUG(Service_Audio, "called"); - - auto result = impl->StartSystem(); - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(result); - } - - void Stop(HLERequestContext& ctx) { - LOG_DEBUG(Service_Audio, "called"); - - auto result = impl->StopSystem(); - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(result); - } - - void AppendAudioOutBuffer(HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - u64 tag = rp.PopRaw(); - - const auto in_buffer_size{ctx.GetReadBufferSize()}; - if (in_buffer_size < sizeof(AudioOutBuffer)) { - LOG_ERROR(Service_Audio, "Input buffer is too small for an AudioOutBuffer!"); - } - - const auto& in_buffer = ctx.ReadBuffer(); - AudioOutBuffer buffer{}; - std::memcpy(&buffer, in_buffer.data(), sizeof(AudioOutBuffer)); - - LOG_TRACE(Service_Audio, "called. Session {} Appending buffer {:08X}", - impl->GetSystem().GetSessionId(), tag); - - auto result = impl->AppendBuffer(buffer, tag); - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(result); - } - - void RegisterBufferEvent(HLERequestContext& ctx) { - LOG_DEBUG(Service_Audio, "called"); - - auto& buffer_event = impl->GetBufferEvent(); - - IPC::ResponseBuilder rb{ctx, 2, 1}; - rb.Push(ResultSuccess); - rb.PushCopyObjects(buffer_event); - } - - void GetReleasedAudioOutBuffers(HLERequestContext& ctx) { - const auto write_buffer_size = ctx.GetWriteBufferNumElements(); - released_buffer.resize_destructive(write_buffer_size); - released_buffer[0] = 0; - - const auto count = impl->GetReleasedBuffers(released_buffer); - - ctx.WriteBuffer(released_buffer); - - LOG_TRACE(Service_Audio, "called. Session {} released {} buffers", - impl->GetSystem().GetSessionId(), count); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(count); - } - - void ContainsAudioOutBuffer(HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - - const u64 tag{rp.Pop()}; - const auto buffer_queued{impl->ContainsAudioBuffer(tag)}; - - LOG_DEBUG(Service_Audio, "called. Is buffer {:08X} registered? {}", tag, buffer_queued); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(buffer_queued); - } - - void GetAudioOutBufferCount(HLERequestContext& ctx) { - const auto buffer_count = impl->GetBufferCount(); - - LOG_DEBUG(Service_Audio, "called. Buffer count={}", buffer_count); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(buffer_count); - } - - void GetAudioOutPlayedSampleCount(HLERequestContext& ctx) { - const auto samples_played = impl->GetPlayedSampleCount(); - - LOG_DEBUG(Service_Audio, "called. Played samples={}", samples_played); - - IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(ResultSuccess); - rb.Push(samples_played); - } - - void FlushAudioOutBuffers(HLERequestContext& ctx) { - bool flushed{impl->FlushAudioOutBuffers()}; - - LOG_DEBUG(Service_Audio, "called. Were any buffers flushed? {}", flushed); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(flushed); - } - - void SetAudioOutVolume(HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const auto volume = rp.Pop(); - - LOG_DEBUG(Service_Audio, "called. Volume={}", volume); - - impl->SetVolume(volume); - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(ResultSuccess); - } - - void GetAudioOutVolume(HLERequestContext& ctx) { - const auto volume = impl->GetVolume(); - - LOG_DEBUG(Service_Audio, "called. Volume={}", volume); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(ResultSuccess); - rb.Push(volume); - } - - KernelHelpers::ServiceContext service_context; - Kernel::KEvent* event; - Kernel::KProcess* process; - std::shared_ptr impl; - Common::ScratchBuffer released_buffer; -}; - AudOutU::AudOutU(Core::System& system_) : ServiceFramework{system_, "audout:u"}, service_context{system_, "AudOutU"}, impl{std::make_unique(system_)} {