Skip to content

Commit

Permalink
[xaudio2] wait for buffer release before destroying the source voice.
Browse files Browse the repository at this point in the history
Stop and FlushSourceBuffers are async and callbacks on the queued buffers don't seem to be called when destroying the voice, with a leak of the buffers' memory.
  • Loading branch information
CrystalP committed Sep 6, 2024
1 parent bf47426 commit b3b7410
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion xbmc/cores/AudioEngine/Sinks/AESinkXAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,26 @@ void CAESinkXAudio::Deinitialize()
{
m_sourceVoice->Stop();
m_sourceVoice->FlushSourceBuffers();

// Stop and FlushSourceBuffers are async, wait for queued buffers to be released by XAudio2.
// callbacks don't seem to be called otherwise, with memory leakage.
XAUDIO2_VOICE_STATE state{};
do
{
if (WAIT_OBJECT_0 != WaitForSingleObject(m_voiceCallback.mBufferEnd.get(), 500))
{
CLog::LogF(LOGERROR, "timeout waiting for buffer flush - possible buffer memory leak");
break;
}
m_sourceVoice->GetState(&state, 0);
} while (state.BuffersQueued > 0);

m_sinkFrames = 0;
m_framesInBuffers = 0;
}
catch (...)
{
CLog::Log(LOGDEBUG, "{}: Invalidated source voice - Releasing", __FUNCTION__);
CLog::Log(LOGERROR, "{}: Invalidated source voice - Releasing", __FUNCTION__);
}
}
m_running = false;
Expand Down

0 comments on commit b3b7410

Please sign in to comment.