Request 20 ms audio frames on Steam Link to reduce CPU overhead

This commit is contained in:
Cameron Gutman
2019-05-04 15:46:11 -07:00
parent 53138d7c16
commit bdbb03e16f
6 changed files with 12 additions and 46 deletions

View File

@@ -2,16 +2,9 @@
#include <SDL.h>
// To reduce CPU load on the Steam Link, we need to accumulate several frames
// before submitting for playback. Higher frames per submission saves more CPU
// but increases audio latency.
#define FRAMES_PER_SUBMISSION 4
SLAudioRenderer::SLAudioRenderer()
: m_AudioContext(nullptr),
m_AudioStream(nullptr),
m_AudioBuffer(nullptr),
m_AudioBufferBytesFilled(0)
m_AudioStream(nullptr)
{
SLAudio_SetLogFunction(SLAudioRenderer::slLogCallback, nullptr);
}
@@ -25,7 +18,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 = opusConfig->samplesPerFrame * sizeof(short) * opusConfig->channelCount;
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
opusConfig->sampleRate,
opusConfig->channelCount,
@@ -45,29 +38,12 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
void* SLAudioRenderer::getAudioBuffer(int* size)
{
SDL_assert(*size == m_AudioBufferSize / FRAMES_PER_SUBMISSION);
if (m_AudioBuffer == nullptr) {
m_AudioBufferBytesFilled = 0;
m_AudioBuffer = (char*)SLAudio_BeginFrame(m_AudioStream);
if (m_AudioBuffer == nullptr) {
return nullptr;
}
}
return (void*)&m_AudioBuffer[m_AudioBufferBytesFilled];
SDL_assert(*size == m_AudioBufferSize);
return SLAudio_BeginFrame(m_AudioStream);
}
SLAudioRenderer::~SLAudioRenderer()
{
if (m_AudioBuffer != nullptr) {
// 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);
}
@@ -77,18 +53,9 @@ SLAudioRenderer::~SLAudioRenderer()
}
}
bool SLAudioRenderer::submitAudio(int bytesWritten)
bool SLAudioRenderer::submitAudio(int)
{
m_AudioBufferBytesFilled += bytesWritten;
// Submit the buffer when it's full
SDL_assert(m_AudioBufferBytesFilled <= m_AudioBufferSize);
if (m_AudioBufferBytesFilled == m_AudioBufferSize) {
SLAudio_SubmitFrame(m_AudioStream);
m_AudioBuffer = nullptr;
m_AudioBufferBytesFilled = 0;
}
SLAudio_SubmitFrame(m_AudioStream);
return true;
}