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

@ -41,6 +41,7 @@ bool Session::testAudio(int audioConfiguration)
// the renderer the channel count and sample rate.
OPUS_MULTISTREAM_CONFIGURATION opusConfig = {};
opusConfig.sampleRate = 48000;
opusConfig.samplesPerFrame = 240;
switch (audioConfiguration)
{
@ -143,7 +144,7 @@ void Session::arDecodeAndPlaySample(char* sampleData, int sampleLength)
s_ActiveSession->m_AudioSampleCount++;
if (s_ActiveSession->m_AudioRenderer != nullptr) {
int desiredSize = sizeof(short) * SAMPLES_PER_FRAME * s_ActiveSession->m_AudioConfig.channelCount;
int desiredSize = sizeof(short) * s_ActiveSession->m_AudioConfig.samplesPerFrame * s_ActiveSession->m_AudioConfig.channelCount;
void* buffer = s_ActiveSession->m_AudioRenderer->getAudioBuffer(&desiredSize);
if (buffer == nullptr) {
return;

View File

@ -31,7 +31,7 @@ bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION*
// frames contain a non-power of 2 number of samples,
// so the slop would require buffering another full frame.
// Specifying non-Po2 seems to work for our supported platforms.
want.samples = SAMPLES_PER_FRAME;
want.samples = opusConfig->samplesPerFrame;
m_AudioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (m_AudioDevice == 0) {
@ -41,7 +41,7 @@ bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION*
return false;
}
m_AudioBuffer = malloc(SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount);
m_AudioBuffer = malloc(opusConfig->samplesPerFrame * sizeof(short) * opusConfig->channelCount);
if (m_AudioBuffer == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Failed to allocate audio buffer");

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;
}

View File

@ -22,7 +22,5 @@ private:
CSLAudioContext* m_AudioContext;
CSLAudioStream* m_AudioStream;
char* m_AudioBuffer;
int m_AudioBufferSize;
int m_AudioBufferBytesFilled;
};

View File

@ -260,7 +260,7 @@ bool SoundIoAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATI
m_RingBuffer = soundio_ring_buffer_create(m_SoundIo,
m_OutputStream->bytes_per_sample *
m_OpusChannelCount *
SAMPLES_PER_FRAME *
opusConfig->samplesPerFrame *
packetsToBuffer);
if (m_RingBuffer == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,

@ -1 +1 @@
Subproject commit 1154cb1d3db53215e46b3c7d2476b27d52d0dfbf
Subproject commit 59481c085a7f774c5d30374636f8bc75da7c676c