mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2026-04-20 23:20:10 +00:00
Request 20 ms audio frames on Steam Link to reduce CPU overhead
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user