2019-03-23 06:08:10 +00:00
|
|
|
#include "slaud.h"
|
|
|
|
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
SLAudioRenderer::SLAudioRenderer()
|
|
|
|
: m_AudioContext(nullptr),
|
2019-05-12 02:09:59 +00:00
|
|
|
m_AudioStream(nullptr),
|
|
|
|
m_AudioBuffer(nullptr)
|
2019-03-23 06:08:10 +00:00
|
|
|
{
|
2019-04-28 18:01:54 +00:00
|
|
|
SLAudio_SetLogFunction(SLAudioRenderer::slLogCallback, nullptr);
|
2019-03-23 06:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SLAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* opusConfig)
|
|
|
|
{
|
|
|
|
m_AudioContext = SLAudio_CreateContext();
|
|
|
|
if (m_AudioContext == nullptr) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SLAudio_CreateContext() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-21 06:57:27 +00:00
|
|
|
// This number is pretty conservative (especially for surround), but
|
|
|
|
// it's hard to avoid since we get crushed by CPU limitations.
|
|
|
|
m_MaxQueuedAudioMs = 40 * opusConfig->channelCount / 2;
|
|
|
|
|
2019-05-12 02:09:59 +00:00
|
|
|
m_FrameDuration = opusConfig->samplesPerFrame / 48;
|
2019-05-04 22:46:11 +00:00
|
|
|
m_AudioBufferSize = opusConfig->samplesPerFrame * sizeof(short) * opusConfig->channelCount;
|
2019-03-23 06:08:10 +00:00
|
|
|
m_AudioStream = SLAudio_CreateStream(m_AudioContext,
|
|
|
|
opusConfig->sampleRate,
|
|
|
|
opusConfig->channelCount,
|
2019-04-28 19:55:04 +00:00
|
|
|
m_AudioBufferSize,
|
2019-03-23 06:08:10 +00:00
|
|
|
1);
|
|
|
|
if (m_AudioStream == nullptr) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SLAudio_CreateStream() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2019-05-12 02:09:59 +00:00
|
|
|
"Using SLAudio renderer with %d ms frames",
|
|
|
|
m_FrameDuration);
|
2019-03-23 06:08:10 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-02 04:27:41 +00:00
|
|
|
void* SLAudioRenderer::getAudioBuffer(int* size)
|
2019-03-23 06:08:10 +00:00
|
|
|
{
|
2019-05-04 22:46:11 +00:00
|
|
|
SDL_assert(*size == m_AudioBufferSize);
|
2019-05-12 02:09:59 +00:00
|
|
|
|
|
|
|
if (m_AudioBuffer == nullptr) {
|
|
|
|
m_AudioBuffer = SLAudio_BeginFrame(m_AudioStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_AudioBuffer;
|
2019-05-02 04:27:41 +00:00
|
|
|
}
|
2019-04-28 19:55:04 +00:00
|
|
|
|
2019-05-02 04:27:41 +00:00
|
|
|
SLAudioRenderer::~SLAudioRenderer()
|
|
|
|
{
|
2019-05-12 02:09:59 +00:00
|
|
|
if (m_AudioBuffer != nullptr) {
|
|
|
|
memset(m_AudioBuffer, 0, m_AudioBufferSize);
|
|
|
|
SLAudio_SubmitFrame(m_AudioStream);
|
|
|
|
}
|
|
|
|
|
2019-03-23 06:08:10 +00:00
|
|
|
if (m_AudioStream != nullptr) {
|
|
|
|
SLAudio_FreeStream(m_AudioStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_AudioContext != nullptr) {
|
|
|
|
SLAudio_FreeContext(m_AudioContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 02:09:59 +00:00
|
|
|
bool SLAudioRenderer::submitAudio(int bytesWritten)
|
2019-03-23 06:08:10 +00:00
|
|
|
{
|
2019-05-12 02:09:59 +00:00
|
|
|
if (bytesWritten == 0) {
|
|
|
|
// This buffer will be reused next time
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-21 06:57:27 +00:00
|
|
|
if (LiGetPendingAudioFrames() * m_FrameDuration < m_MaxQueuedAudioMs) {
|
2019-05-12 02:09:59 +00:00
|
|
|
SLAudio_SubmitFrame(m_AudioStream);
|
|
|
|
m_AudioBuffer = nullptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Too many queued audio frames: %d",
|
|
|
|
LiGetPendingAudioFrames());
|
|
|
|
}
|
|
|
|
|
2019-03-23 06:08:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-28 18:01:54 +00:00
|
|
|
|
2019-05-02 05:31:52 +00:00
|
|
|
void SLAudioRenderer::slLogCallback(void*, ESLAudioLog logLevel, const char *message)
|
2019-04-28 18:01:54 +00:00
|
|
|
{
|
|
|
|
SDL_LogPriority priority;
|
|
|
|
|
|
|
|
switch (logLevel)
|
|
|
|
{
|
|
|
|
case k_ESLAudioLogError:
|
|
|
|
priority = SDL_LOG_PRIORITY_ERROR;
|
|
|
|
break;
|
|
|
|
case k_ESLAudioLogWarning:
|
|
|
|
priority = SDL_LOG_PRIORITY_WARN;
|
|
|
|
break;
|
|
|
|
case k_ESLAudioLogInfo:
|
|
|
|
priority = SDL_LOG_PRIORITY_INFO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case k_ESLAudioLogDebug:
|
|
|
|
priority = SDL_LOG_PRIORITY_DEBUG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
priority,
|
|
|
|
"SLAudio: %s",
|
|
|
|
message);
|
|
|
|
}
|