mirror of
https://github.com/moonlight-stream/moonlight-qt.git
synced 2025-07-03 16:25:54 +00:00
Request 20 ms audio frames on Steam Link to reduce CPU overhead
This commit is contained in:
parent
53138d7c16
commit
bdbb03e16f
@ -41,6 +41,7 @@ bool Session::testAudio(int audioConfiguration)
|
|||||||
// the renderer the channel count and sample rate.
|
// the renderer the channel count and sample rate.
|
||||||
OPUS_MULTISTREAM_CONFIGURATION opusConfig = {};
|
OPUS_MULTISTREAM_CONFIGURATION opusConfig = {};
|
||||||
opusConfig.sampleRate = 48000;
|
opusConfig.sampleRate = 48000;
|
||||||
|
opusConfig.samplesPerFrame = 240;
|
||||||
|
|
||||||
switch (audioConfiguration)
|
switch (audioConfiguration)
|
||||||
{
|
{
|
||||||
@ -143,7 +144,7 @@ void Session::arDecodeAndPlaySample(char* sampleData, int sampleLength)
|
|||||||
s_ActiveSession->m_AudioSampleCount++;
|
s_ActiveSession->m_AudioSampleCount++;
|
||||||
|
|
||||||
if (s_ActiveSession->m_AudioRenderer != nullptr) {
|
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);
|
void* buffer = s_ActiveSession->m_AudioRenderer->getAudioBuffer(&desiredSize);
|
||||||
if (buffer == nullptr) {
|
if (buffer == nullptr) {
|
||||||
return;
|
return;
|
||||||
|
@ -31,7 +31,7 @@ bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION*
|
|||||||
// frames contain a non-power of 2 number of samples,
|
// frames contain a non-power of 2 number of samples,
|
||||||
// so the slop would require buffering another full frame.
|
// so the slop would require buffering another full frame.
|
||||||
// Specifying non-Po2 seems to work for our supported platforms.
|
// 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);
|
m_AudioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
|
||||||
if (m_AudioDevice == 0) {
|
if (m_AudioDevice == 0) {
|
||||||
@ -41,7 +41,7 @@ bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION*
|
|||||||
return false;
|
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) {
|
if (m_AudioBuffer == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
"Failed to allocate audio buffer");
|
"Failed to allocate audio buffer");
|
||||||
|
@ -2,16 +2,9 @@
|
|||||||
|
|
||||||
#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);
|
||||||
}
|
}
|
||||||
@ -25,7 +18,7 @@ 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_AudioBufferSize = opusConfig->samplesPerFrame * sizeof(short) * opusConfig->channelCount;
|
||||||
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
|
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
|
||||||
opusConfig->sampleRate,
|
opusConfig->sampleRate,
|
||||||
opusConfig->channelCount,
|
opusConfig->channelCount,
|
||||||
@ -45,29 +38,12 @@ bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* o
|
|||||||
|
|
||||||
void* SLAudioRenderer::getAudioBuffer(int* size)
|
void* SLAudioRenderer::getAudioBuffer(int* size)
|
||||||
{
|
{
|
||||||
SDL_assert(*size == m_AudioBufferSize / FRAMES_PER_SUBMISSION);
|
SDL_assert(*size == m_AudioBufferSize);
|
||||||
|
return SLAudio_BeginFrame(m_AudioStream);
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SLAudioRenderer::~SLAudioRenderer()
|
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) {
|
if (m_AudioStream != nullptr) {
|
||||||
SLAudio_FreeStream(m_AudioStream);
|
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);
|
SLAudio_SubmitFrame(m_AudioStream);
|
||||||
m_AudioBuffer = nullptr;
|
|
||||||
m_AudioBufferBytesFilled = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,5 @@ private:
|
|||||||
CSLAudioContext* m_AudioContext;
|
CSLAudioContext* m_AudioContext;
|
||||||
CSLAudioStream* m_AudioStream;
|
CSLAudioStream* m_AudioStream;
|
||||||
|
|
||||||
char* m_AudioBuffer;
|
|
||||||
int m_AudioBufferSize;
|
int m_AudioBufferSize;
|
||||||
int m_AudioBufferBytesFilled;
|
|
||||||
};
|
};
|
||||||
|
@ -260,7 +260,7 @@ bool SoundIoAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATI
|
|||||||
m_RingBuffer = soundio_ring_buffer_create(m_SoundIo,
|
m_RingBuffer = soundio_ring_buffer_create(m_SoundIo,
|
||||||
m_OutputStream->bytes_per_sample *
|
m_OutputStream->bytes_per_sample *
|
||||||
m_OpusChannelCount *
|
m_OpusChannelCount *
|
||||||
SAMPLES_PER_FRAME *
|
opusConfig->samplesPerFrame *
|
||||||
packetsToBuffer);
|
packetsToBuffer);
|
||||||
if (m_RingBuffer == nullptr) {
|
if (m_RingBuffer == nullptr) {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 1154cb1d3db53215e46b3c7d2476b27d52d0dfbf
|
Subproject commit 59481c085a7f774c5d30374636f8bc75da7c676c
|
Loading…
x
Reference in New Issue
Block a user