From eaf66b4c9f9517bcb4f417260ad460fca4254bec Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Sat, 28 Jul 2018 13:27:59 -0400
Subject: [PATCH] audio_core: Move to audout_u impl.

- This is necessary so streams are created on the same thread.
---
 src/core/core.cpp                       | 2 --
 src/core/core.h                         | 7 -------
 src/core/hle/service/audio/audout_u.cpp | 8 ++++----
 src/core/hle/service/audio/audout_u.h   | 2 ++
 4 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/src/core/core.cpp b/src/core/core.cpp
index 186fa46dfd..b7f4b45323 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -177,7 +177,6 @@ System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
     }
 
     gpu_core = std::make_unique<Tegra::GPU>();
-    audio_core = std::make_unique<AudioCore::AudioOut>();
     telemetry_session = std::make_unique<Core::TelemetrySession>();
     service_manager = std::make_shared<Service::SM::ServiceManager>();
 
@@ -229,7 +228,6 @@ void System::Shutdown() {
     service_manager.reset();
     telemetry_session.reset();
     gpu_core.reset();
-    audio_core.reset();
 
     // Close all CPU/threading state
     cpu_barrier->NotifyEnd();
diff --git a/src/core/core.h b/src/core/core.h
index 6f4df775f3..c123fe4018 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -8,7 +8,6 @@
 #include <memory>
 #include <string>
 #include <thread>
-#include "audio_core/audio_out.h"
 #include "common/common_types.h"
 #include "core/arm/exclusive_monitor.h"
 #include "core/core_cpu.h"
@@ -132,11 +131,6 @@ public:
         return *gpu_core;
     }
 
-    /// Gets the AudioCore interface
-    AudioCore::AudioOut& AudioCore() {
-        return *audio_core;
-    }
-
     /// Gets the scheduler for the CPU core that is currently running
     Kernel::Scheduler& CurrentScheduler() {
         return *CurrentCpuCore().Scheduler();
@@ -201,7 +195,6 @@ private:
     /// AppLoader used to load the current executing application
     std::unique_ptr<Loader::AppLoader> app_loader;
     std::unique_ptr<Tegra::GPU> gpu_core;
-    std::unique_ptr<AudioCore::AudioOut> audio_core;
     std::shared_ptr<Tegra::DebugContext> debug_context;
     Kernel::SharedPtr<Kernel::Process> current_process;
     std::shared_ptr<ExclusiveMonitor> cpu_exclusive_monitor;
diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp
index a15d53ff82..ab37c2a694 100644
--- a/src/core/hle/service/audio/audout_u.cpp
+++ b/src/core/hle/service/audio/audout_u.cpp
@@ -25,9 +25,8 @@ constexpr int DefaultSampleRate{48000};
 
 class IAudioOut final : public ServiceFramework<IAudioOut> {
 public:
-    IAudioOut(AudoutParams audio_params)
-        : ServiceFramework("IAudioOut"), audio_params(audio_params),
-          audio_core(Core::System::GetInstance().AudioCore()) {
+    IAudioOut(AudoutParams audio_params, AudioCore::AudioOut& audio_core)
+        : ServiceFramework("IAudioOut"), audio_params(audio_params), audio_core(audio_core) {
 
         static const FunctionInfo functions[] = {
             {0, &IAudioOut::GetAudioOutState, "GetAudioOutState"},
@@ -195,7 +194,7 @@ void AudOutU::OpenAudioOutImpl(Kernel::HLERequestContext& ctx) {
     // TODO(bunnei): Support more than one IAudioOut interface. When we add this, ListAudioOutsImpl
     // will likely need to be updated as well.
     ASSERT_MSG(!audio_out_interface, "Unimplemented");
-    audio_out_interface = std::make_shared<IAudioOut>(std::move(params));
+    audio_out_interface = std::make_shared<IAudioOut>(std::move(params), *audio_core);
 
     IPC::ResponseBuilder rb{ctx, 6, 0, 1};
     rb.Push(RESULT_SUCCESS);
@@ -212,6 +211,7 @@ AudOutU::AudOutU() : ServiceFramework("audout:u") {
                                              {2, &AudOutU::ListAudioOutsImpl, "ListAudioOutsAuto"},
                                              {3, &AudOutU::OpenAudioOutImpl, "OpenAudioOutAuto"}};
     RegisterHandlers(functions);
+    audio_core = std::make_unique<AudioCore::AudioOut>();
 }
 
 } // namespace Service::Audio
diff --git a/src/core/hle/service/audio/audout_u.h b/src/core/hle/service/audio/audout_u.h
index bc43f1f447..e5c2184d58 100644
--- a/src/core/hle/service/audio/audout_u.h
+++ b/src/core/hle/service/audio/audout_u.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include "audio_core/audio_out.h"
 #include "core/hle/service/service.h"
 
 namespace Kernel {
@@ -33,6 +34,7 @@ public:
 
 private:
     std::shared_ptr<IAudioOut> audio_out_interface;
+    std::unique_ptr<AudioCore::AudioOut> audio_core;
 
     void ListAudioOutsImpl(Kernel::HLERequestContext& ctx);
     void OpenAudioOutImpl(Kernel::HLERequestContext& ctx);