mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2025-07-04 00:36:36 +00:00
Batch audio frames on Steam Link to save CPU cycles
This commit is contained in:
parent
477fa8fedf
commit
2bf552f801
@ -2,9 +2,16 @@
|
|||||||
|
|
||||||
#include <SDL.h>
|
#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()
|
SLAudioRenderer::SLAudioRenderer()
|
||||||
: m_AudioContext(nullptr),
|
: m_AudioContext(nullptr),
|
||||||
m_AudioStream(nullptr)
|
m_AudioStream(nullptr),
|
||||||
|
m_AudioBuffer(nullptr),
|
||||||
|
m_AudioBufferBytesFilled(0)
|
||||||
{
|
{
|
||||||
SLAudio_SetLogFunction(SLAudioRenderer::slLogCallback, nullptr);
|
SLAudio_SetLogFunction(SLAudioRenderer::slLogCallback, nullptr);
|
||||||
}
|
}
|
||||||
@ -18,10 +25,11 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_AudioBufferSize = SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount * FRAMES_PER_SUBMISSION;
|
||||||
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
|
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
|
||||||
opusConfig->sampleRate,
|
opusConfig->sampleRate,
|
||||||
opusConfig->channelCount,
|
opusConfig->channelCount,
|
||||||
SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount,
|
m_AudioBufferSize,
|
||||||
1);
|
1);
|
||||||
if (m_AudioStream == nullptr) {
|
if (m_AudioStream == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
@ -37,6 +45,14 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
|
|||||||
|
|
||||||
SLAudioRenderer::~SLAudioRenderer()
|
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) {
|
if (m_AudioStream != nullptr) {
|
||||||
SLAudio_FreeStream(m_AudioStream);
|
SLAudio_FreeStream(m_AudioStream);
|
||||||
}
|
}
|
||||||
@ -48,11 +64,24 @@ SLAudioRenderer::~SLAudioRenderer()
|
|||||||
|
|
||||||
bool SLAudioRenderer::submitAudio(short* audioBuffer, int audioSize)
|
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) {
|
// Accumulate several frames of audio before submitting to reduce CPU load
|
||||||
memcpy(outputBuffer, audioBuffer, audioSize);
|
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);
|
SLAudio_SubmitFrame(m_AudioStream);
|
||||||
|
m_AudioBufferBytesFilled = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -19,4 +19,7 @@ private:
|
|||||||
|
|
||||||
CSLAudioContext* m_AudioContext;
|
CSLAudioContext* m_AudioContext;
|
||||||
CSLAudioStream* m_AudioStream;
|
CSLAudioStream* m_AudioStream;
|
||||||
|
char* m_AudioBuffer;
|
||||||
|
int m_AudioBufferSize;
|
||||||
|
int m_AudioBufferBytesFilled;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user