2022-07-17 00:48:45 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#include "audio_core/audio_manager.h"
|
|
|
|
#include "core/core.h"
|
2022-09-21 15:43:53 +02:00
|
|
|
#include "core/hle/service/audio/errors.h"
|
2022-07-17 00:48:45 +02:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2022-09-21 15:25:00 +02:00
|
|
|
AudioManager::AudioManager() {
|
2022-07-17 00:48:45 +02:00
|
|
|
thread = std::jthread([this]() { ThreadFunc(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioManager::Shutdown() {
|
|
|
|
running = false;
|
|
|
|
events.SetAudioEvent(Event::Type::Max, true);
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result AudioManager::SetOutManager(BufferEventFunc buffer_func) {
|
|
|
|
if (!running) {
|
2023-03-07 01:04:12 +01:00
|
|
|
return Service::Audio::ResultOperationFailed;
|
2022-07-17 00:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::scoped_lock l{lock};
|
|
|
|
|
|
|
|
const auto index{events.GetManagerIndex(Event::Type::AudioOutManager)};
|
|
|
|
if (buffer_events[index] == nullptr) {
|
2022-09-21 15:32:12 +02:00
|
|
|
buffer_events[index] = std::move(buffer_func);
|
2022-07-17 00:48:45 +02:00
|
|
|
needs_update = true;
|
|
|
|
events.SetAudioEvent(Event::Type::AudioOutManager, true);
|
|
|
|
}
|
|
|
|
return ResultSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result AudioManager::SetInManager(BufferEventFunc buffer_func) {
|
|
|
|
if (!running) {
|
2023-03-07 01:04:12 +01:00
|
|
|
return Service::Audio::ResultOperationFailed;
|
2022-07-17 00:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::scoped_lock l{lock};
|
|
|
|
|
|
|
|
const auto index{events.GetManagerIndex(Event::Type::AudioInManager)};
|
|
|
|
if (buffer_events[index] == nullptr) {
|
2022-09-21 15:32:12 +02:00
|
|
|
buffer_events[index] = std::move(buffer_func);
|
2022-07-17 00:48:45 +02:00
|
|
|
needs_update = true;
|
|
|
|
events.SetAudioEvent(Event::Type::AudioInManager, true);
|
|
|
|
}
|
|
|
|
return ResultSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioManager::SetEvent(const Event::Type type, const bool signalled) {
|
|
|
|
events.SetAudioEvent(type, signalled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioManager::ThreadFunc() {
|
|
|
|
std::unique_lock l{events.GetAudioEventLock()};
|
|
|
|
events.ClearEvents();
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
while (running) {
|
2022-09-21 15:35:20 +02:00
|
|
|
const auto timed_out{events.Wait(l, std::chrono::seconds(2))};
|
2022-07-17 00:48:45 +02:00
|
|
|
|
|
|
|
if (events.CheckAudioEventSet(Event::Type::Max)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < buffer_events.size(); i++) {
|
2022-09-21 15:35:20 +02:00
|
|
|
const auto event_type = static_cast<Event::Type>(i);
|
|
|
|
|
|
|
|
if (events.CheckAudioEventSet(event_type) || timed_out) {
|
2022-07-17 00:48:45 +02:00
|
|
|
if (buffer_events[i]) {
|
|
|
|
buffer_events[i]();
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 15:35:20 +02:00
|
|
|
events.SetAudioEvent(event_type, false);
|
2022-07-17 00:48:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore
|