mirror of
https://github.com/moonlight-stream/moonlight-qt
synced 2024-11-16 08:17:58 +00:00
Batch audio frames on Steam Link to save CPU cycles
This commit is contained in:
parent
477fa8fedf
commit
2bf552f801
2 changed files with 37 additions and 5 deletions
|
@ -2,9 +2,16 @@
|
|||
|
||||
#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_AudioStream(nullptr),
|
||||
m_AudioBuffer(nullptr),
|
||||
m_AudioBufferBytesFilled(0)
|
||||
{
|
||||
SLAudio_SetLogFunction(SLAudioRenderer::slLogCallback, nullptr);
|
||||
}
|
||||
|
@ -18,10 +25,11 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
|
|||
return false;
|
||||
}
|
||||
|
||||
m_AudioBufferSize = SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount * FRAMES_PER_SUBMISSION;
|
||||
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
|
||||
opusConfig->sampleRate,
|
||||
opusConfig->channelCount,
|
||||
SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount,
|
||||
m_AudioBufferSize,
|
||||
1);
|
||||
if (m_AudioStream == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||
|
@ -37,6 +45,14 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
|
|||
|
||||
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);
|
||||
}
|
||||
|
@ -48,11 +64,24 @@ SLAudioRenderer::~SLAudioRenderer()
|
|||
|
||||
bool SLAudioRenderer::submitAudio(short* audioBuffer, int audioSize)
|
||||
{
|
||||
void* outputBuffer = SLAudio_BeginFrame(m_AudioStream);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (outputBuffer != nullptr) {
|
||||
memcpy(outputBuffer, audioBuffer, audioSize);
|
||||
// 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;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -19,4 +19,7 @@ private:
|
|||
|
||||
CSLAudioContext* m_AudioContext;
|
||||
CSLAudioStream* m_AudioStream;
|
||||
char* m_AudioBuffer;
|
||||
int m_AudioBufferSize;
|
||||
int m_AudioBufferBytesFilled;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue