Decode directly into the audio renderer's buffer to avoid a copy

This commit is contained in:
Cameron Gutman
2019-05-01 21:27:41 -07:00
parent 187f47a353
commit 21f2b1224a
9 changed files with 70 additions and 63 deletions

View File

@@ -9,9 +9,7 @@
SLAudioRenderer::SLAudioRenderer()
: m_AudioContext(nullptr),
m_AudioStream(nullptr),
m_AudioBuffer(nullptr),
m_AudioBufferBytesFilled(0)
m_AudioStream(nullptr)
{
SLAudio_SetLogFunction(SLAudioRenderer::slLogCallback, nullptr);
}
@@ -25,7 +23,7 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
return false;
}
m_AudioBufferSize = SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount * FRAMES_PER_SUBMISSION;
m_AudioBufferSize = SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount;
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
opusConfig->sampleRate,
opusConfig->channelCount,
@@ -43,16 +41,14 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
return true;
}
void* SLAudioRenderer::getAudioBuffer(int* size)
{
SDL_assert(*size == m_AudioBufferSize);
return SLAudio_BeginFrame(m_AudioStream);
}
SLAudioRenderer::~SLAudioRenderer()
{
if (m_AudioBufferBytesFilled != 0) {
// We had a buffer in flight when we quit. Just in case
// SLAudio doesn't handle this properly, we'll zero and submit
// it just to be safe.
memset(m_AudioBuffer, 0, m_AudioBufferSize);
SLAudio_SubmitFrame(m_AudioStream);
}
if (m_AudioStream != nullptr) {
SLAudio_FreeStream(m_AudioStream);
}
@@ -62,28 +58,9 @@ SLAudioRenderer::~SLAudioRenderer()
}
}
bool SLAudioRenderer::submitAudio(short* audioBuffer, int audioSize)
bool SLAudioRenderer::submitAudio(int)
{
if (m_AudioBufferBytesFilled == 0) {
// Get a new audio buffer from SLAudio
m_AudioBuffer = (char*)SLAudio_BeginFrame(m_AudioStream);
if (m_AudioBuffer == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SLAudio_BeginFrame() failed");
return true;
}
}
// Accumulate several frames of audio before submitting to reduce CPU load
SDL_assert(audioSize <= m_AudioBufferSize - m_AudioBufferBytesFilled);
memcpy(&m_AudioBuffer[m_AudioBufferBytesFilled], audioBuffer, audioSize);
m_AudioBufferBytesFilled += audioSize;
// Submit the buffer when it's full
if (m_AudioBufferBytesFilled == m_AudioBufferSize) {
SLAudio_SubmitFrame(m_AudioStream);
m_AudioBufferBytesFilled = 0;
}
SLAudio_SubmitFrame(m_AudioStream);
return true;
}