From ef8d4123db4f17cb1750d715e7ed2b4ab7f64e03 Mon Sep 17 00:00:00 2001 From: CrystalP Date: Thu, 19 Sep 2024 13:37:42 -0400 Subject: [PATCH] [XAudio2] avoid leak + fix voice creation for closest match --- xbmc/cores/AudioEngine/Sinks/AESinkXAudio.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/xbmc/cores/AudioEngine/Sinks/AESinkXAudio.cpp b/xbmc/cores/AudioEngine/Sinks/AESinkXAudio.cpp index e250d55f70..add7388a70 100644 --- a/xbmc/cores/AudioEngine/Sinks/AESinkXAudio.cpp +++ b/xbmc/cores/AudioEngine/Sinks/AESinkXAudio.cpp @@ -596,6 +596,9 @@ bool CAESinkXAudio::InitializeInternal(std::string deviceId, AEAudioFormat &form WASAPISampleRates[j] > XAUDIO2_MAX_SAMPLE_RATE) continue; + SafeDestroyVoice(&m_sourceVoice); + SafeDestroyVoice(&m_masterVoice); + wfxex.Format.nSamplesPerSec = WASAPISampleRates[i]; wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign; @@ -622,9 +625,19 @@ bool CAESinkXAudio::InitializeInternal(std::string deviceId, AEAudioFormat &form if (closestMatch >= 0) { + // Closest match may be different from the last successful sample rate tested + SafeDestroyVoice(&m_sourceVoice); + SafeDestroyVoice(&m_masterVoice); + wfxex.Format.nSamplesPerSec = WASAPISampleRates[closestMatch]; wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign; - goto initialize; + + if (SUCCEEDED(m_xAudio2->CreateMasteringVoice(&m_masterVoice, wfxex.Format.nChannels, + wfxex.Format.nSamplesPerSec, 0, + device.c_str(), nullptr, streamCategory)) && + SUCCEEDED(m_xAudio2->CreateSourceVoice(&m_sourceVoice, &wfxex.Format, 0, + XAUDIO2_DEFAULT_FREQ_RATIO, &m_voiceCallback))) + goto initialize; } } } -- 2.11.4.GIT