audio: Wait for samples on the emulated DSP side to avoid desyncs
Waiting on the host side is inaccurate and leads to desyncs in the event of the sink missing a deadline that require stalls to fix. By waiting for the sink to have space before even starting rendering such desyncs can be avoided.
This commit is contained in:
parent
d8fc3f403b
commit
ea5dd02db9
6 changed files with 28 additions and 24 deletions
|
@ -189,6 +189,8 @@ void AudioRenderer::ThreadFunc() {
|
||||||
max_time = std::min(command_buffer.time_limit, max_time);
|
max_time = std::min(command_buffer.time_limit, max_time);
|
||||||
command_list_processor.SetProcessTimeMax(max_time);
|
command_list_processor.SetProcessTimeMax(max_time);
|
||||||
|
|
||||||
|
streams[index]->WaitFreeSpace();
|
||||||
|
|
||||||
// Process the command list
|
// Process the command list
|
||||||
{
|
{
|
||||||
MICROPROFILE_SCOPE(Audio_Renderer);
|
MICROPROFILE_SCOPE(Audio_Renderer);
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
#include "common/reader_writer_queue.h"
|
#include "common/reader_writer_queue.h"
|
||||||
#include "common/thread.h"
|
#include "common/thread.h"
|
||||||
|
#include "common/polyfill_thread.h"
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
namespace Timing {
|
namespace Timing {
|
||||||
|
|
|
@ -17,11 +17,7 @@ MICROPROFILE_DEFINE(Audio_RenderSystemManager, "Audio", "Render System Manager",
|
||||||
namespace AudioCore::AudioRenderer {
|
namespace AudioCore::AudioRenderer {
|
||||||
|
|
||||||
SystemManager::SystemManager(Core::System& core_)
|
SystemManager::SystemManager(Core::System& core_)
|
||||||
: core{core_}, adsp{core.AudioCore().GetADSP()}, mailbox{adsp.GetRenderMailbox()},
|
: core{core_}, adsp{core.AudioCore().GetADSP()}, mailbox{adsp.GetRenderMailbox()} {}
|
||||||
thread_event{Core::Timing::CreateEvent(
|
|
||||||
"AudioRendererSystemManager", [this](std::uintptr_t, s64 time, std::chrono::nanoseconds) {
|
|
||||||
return ThreadFunc2(time);
|
|
||||||
})} {}
|
|
||||||
|
|
||||||
SystemManager::~SystemManager() {
|
SystemManager::~SystemManager() {
|
||||||
Stop();
|
Stop();
|
||||||
|
@ -32,8 +28,6 @@ bool SystemManager::InitializeUnsafe() {
|
||||||
if (adsp.Start()) {
|
if (adsp.Start()) {
|
||||||
active = true;
|
active = true;
|
||||||
thread = std::jthread([this](std::stop_token stop_token) { ThreadFunc(); });
|
thread = std::jthread([this](std::stop_token stop_token) { ThreadFunc(); });
|
||||||
core.CoreTiming().ScheduleLoopingEvent(std::chrono::nanoseconds(0), RENDER_TIME,
|
|
||||||
thread_event);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +38,6 @@ void SystemManager::Stop() {
|
||||||
if (!active) {
|
if (!active) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
core.CoreTiming().UnscheduleEvent(thread_event, {});
|
|
||||||
active = false;
|
active = false;
|
||||||
update.store(true);
|
update.store(true);
|
||||||
update.notify_all();
|
update.notify_all();
|
||||||
|
@ -110,16 +103,7 @@ void SystemManager::ThreadFunc() {
|
||||||
|
|
||||||
adsp.Signal();
|
adsp.Signal();
|
||||||
adsp.Wait();
|
adsp.Wait();
|
||||||
|
|
||||||
update.wait(false);
|
|
||||||
update.store(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::chrono::nanoseconds> SystemManager::ThreadFunc2(s64 time) {
|
|
||||||
update.store(true);
|
|
||||||
update.notify_all();
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace AudioCore::AudioRenderer
|
} // namespace AudioCore::AudioRenderer
|
||||||
|
|
|
@ -68,11 +68,6 @@ private:
|
||||||
*/
|
*/
|
||||||
void ThreadFunc();
|
void ThreadFunc();
|
||||||
|
|
||||||
/**
|
|
||||||
* Signalling core timing thread to run ThreadFunc.
|
|
||||||
*/
|
|
||||||
std::optional<std::chrono::nanoseconds> ThreadFunc2(s64 time);
|
|
||||||
|
|
||||||
enum class StreamState {
|
enum class StreamState {
|
||||||
Filling,
|
Filling,
|
||||||
Steady,
|
Steady,
|
||||||
|
@ -95,8 +90,6 @@ private:
|
||||||
ADSP::ADSP& adsp;
|
ADSP::ADSP& adsp;
|
||||||
/// AudioRenderer mailbox for communication
|
/// AudioRenderer mailbox for communication
|
||||||
ADSP::AudioRenderer_Mailbox* mailbox{};
|
ADSP::AudioRenderer_Mailbox* mailbox{};
|
||||||
/// Core timing event to signal main thread
|
|
||||||
std::shared_ptr<Core::Timing::EventType> thread_event;
|
|
||||||
/// Atomic for main thread to wait on
|
/// Atomic for main thread to wait on
|
||||||
std::atomic<bool> update{};
|
std::atomic<bool> update{};
|
||||||
};
|
};
|
||||||
|
|
|
@ -205,6 +205,10 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
|
||||||
// If we're paused or going to shut down, we don't want to consume buffers as coretiming is
|
// If we're paused or going to shut down, we don't want to consume buffers as coretiming is
|
||||||
// paused and we'll desync, so just play silence.
|
// paused and we'll desync, so just play silence.
|
||||||
if (system.IsPaused() || system.IsShuttingDown()) {
|
if (system.IsPaused() || system.IsShuttingDown()) {
|
||||||
|
if (system.IsShuttingDown()) {
|
||||||
|
release_cv.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
static constexpr std::array<s16, 6> silence{};
|
static constexpr std::array<s16, 6> silence{};
|
||||||
for (size_t i = frames_written; i < num_frames; i++) {
|
for (size_t i = frames_written; i < num_frames; i++) {
|
||||||
std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes);
|
std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes);
|
||||||
|
@ -240,6 +244,12 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
|
||||||
}
|
}
|
||||||
// Successfully dequeued a new buffer.
|
// Successfully dequeued a new buffer.
|
||||||
queued_buffers--;
|
queued_buffers--;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::unique_lock lk{release_mutex};
|
||||||
|
}
|
||||||
|
|
||||||
|
release_cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the minimum frames available between the currently playing buffer, and the
|
// Get the minimum frames available between the currently playing buffer, and the
|
||||||
|
@ -303,4 +313,9 @@ u64 SinkStream::GetExpectedPlayedSampleCount() {
|
||||||
return std::min<u64>(exp_played_sample_count, max_played_sample_count);
|
return std::min<u64>(exp_played_sample_count, max_played_sample_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SinkStream::WaitFreeSpace() {
|
||||||
|
std::unique_lock lk{release_mutex};
|
||||||
|
release_cv.wait(lk, [this]() { return queued_buffers < max_queue_size || system.IsShuttingDown(); });
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace AudioCore::Sink
|
} // namespace AudioCore::Sink
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include "common/reader_writer_queue.h"
|
#include "common/reader_writer_queue.h"
|
||||||
#include "common/ring_buffer.h"
|
#include "common/ring_buffer.h"
|
||||||
#include "common/thread.h"
|
#include "common/thread.h"
|
||||||
|
#include "common/polyfill_thread.h"
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
class System;
|
class System;
|
||||||
|
@ -219,6 +220,11 @@ public:
|
||||||
*/
|
*/
|
||||||
u64 GetExpectedPlayedSampleCount();
|
u64 GetExpectedPlayedSampleCount();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits for free space in the sample ring buffer
|
||||||
|
*/
|
||||||
|
void WaitFreeSpace();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// Core system
|
/// Core system
|
||||||
Core::System& system;
|
Core::System& system;
|
||||||
|
@ -258,6 +264,9 @@ private:
|
||||||
f32 system_volume{1.0f};
|
f32 system_volume{1.0f};
|
||||||
/// Set via IAudioDevice service calls
|
/// Set via IAudioDevice service calls
|
||||||
f32 device_volume{1.0f};
|
f32 device_volume{1.0f};
|
||||||
|
/// Signalled when ring buffer entries are consumed
|
||||||
|
std::condition_variable release_cv;
|
||||||
|
std::mutex release_mutex;
|
||||||
std::mutex stall_guard;
|
std::mutex stall_guard;
|
||||||
std::unique_lock<std::mutex> stalled_lock;
|
std::unique_lock<std::mutex> stalled_lock;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue