2018-09-13 13:23:06 +00:00
|
|
|
#include "sdl.h"
|
2018-06-28 08:44:43 +00:00
|
|
|
|
2018-06-24 01:46:59 +00:00
|
|
|
#include <Limelight.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
|
2018-09-13 13:23:06 +00:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
SdlAudioRenderer::SdlAudioRenderer()
|
2019-05-02 04:27:41 +00:00
|
|
|
: m_AudioDevice(0),
|
|
|
|
m_AudioBuffer(nullptr)
|
2018-09-13 13:23:06 +00:00
|
|
|
{
|
2018-07-21 01:15:46 +00:00
|
|
|
SDL_assert(!SDL_WasInit(SDL_INIT_AUDIO));
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_InitSubSystem(SDL_INIT_AUDIO) failed: %s",
|
|
|
|
SDL_GetError());
|
2018-09-13 14:46:01 +00:00
|
|
|
SDL_assert(SDL_WasInit(SDL_INIT_AUDIO));
|
2018-07-21 01:15:46 +00:00
|
|
|
}
|
2018-09-13 14:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION* opusConfig)
|
|
|
|
{
|
|
|
|
SDL_AudioSpec want, have;
|
2018-07-21 01:15:46 +00:00
|
|
|
|
2018-06-24 01:46:59 +00:00
|
|
|
SDL_zero(want);
|
|
|
|
want.freq = opusConfig->sampleRate;
|
|
|
|
want.format = AUDIO_S16;
|
|
|
|
want.channels = opusConfig->channelCount;
|
2018-07-16 03:17:08 +00:00
|
|
|
|
|
|
|
// This is supposed to be a power of 2, but our
|
|
|
|
// frames contain a non-power of 2 number of samples,
|
|
|
|
// so the slop would require buffering another full frame.
|
|
|
|
// Specifying non-Po2 seems to work for our supported platforms.
|
|
|
|
want.samples = SAMPLES_PER_FRAME;
|
2018-06-24 01:46:59 +00:00
|
|
|
|
2018-09-13 13:23:06 +00:00
|
|
|
m_AudioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
|
|
|
|
if (m_AudioDevice == 0) {
|
2018-06-24 01:46:59 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to open audio device: %s",
|
|
|
|
SDL_GetError());
|
2019-05-02 04:27:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_AudioBuffer = malloc(SAMPLES_PER_FRAME * sizeof(short) * opusConfig->channelCount);
|
|
|
|
if (m_AudioBuffer == nullptr) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to allocate audio buffer");
|
2018-09-13 14:46:01 +00:00
|
|
|
return false;
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 20:27:57 +00:00
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2018-09-15 23:00:36 +00:00
|
|
|
"Desired audio buffer: %u samples (%lu bytes)",
|
2018-09-15 20:27:57 +00:00
|
|
|
want.samples,
|
|
|
|
want.samples * sizeof(short) * want.channels);
|
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2018-09-15 23:00:36 +00:00
|
|
|
"Obtained audio buffer: %u samples (%u bytes)",
|
2018-09-15 20:27:57 +00:00
|
|
|
have.samples,
|
2018-09-15 23:00:36 +00:00
|
|
|
have.size);
|
2018-09-15 20:27:57 +00:00
|
|
|
|
2018-09-13 13:23:06 +00:00
|
|
|
// Start playback
|
|
|
|
SDL_PauseAudioDevice(m_AudioDevice, 0);
|
2018-07-28 23:06:26 +00:00
|
|
|
|
2018-09-13 14:46:01 +00:00
|
|
|
return true;
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 13:23:06 +00:00
|
|
|
SdlAudioRenderer::~SdlAudioRenderer()
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
2018-09-13 14:46:01 +00:00
|
|
|
if (m_AudioDevice != 0) {
|
|
|
|
// Stop playback
|
|
|
|
SDL_PauseAudioDevice(m_AudioDevice, 1);
|
|
|
|
SDL_CloseAudioDevice(m_AudioDevice);
|
|
|
|
}
|
2018-07-21 01:15:46 +00:00
|
|
|
|
2019-05-02 04:27:41 +00:00
|
|
|
if (m_AudioBuffer != nullptr) {
|
|
|
|
free(m_AudioBuffer);
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:15:46 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
SDL_assert(!SDL_WasInit(SDL_INIT_AUDIO));
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 04:27:41 +00:00
|
|
|
void* SdlAudioRenderer::getAudioBuffer(int*)
|
|
|
|
{
|
|
|
|
return m_AudioBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SdlAudioRenderer::submitAudio(int bytesWritten)
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
2019-05-02 04:27:41 +00:00
|
|
|
if (SDL_QueueAudio(m_AudioDevice, m_AudioBuffer, bytesWritten) < 0) {
|
2018-09-13 13:23:06 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to queue audio sample: %s",
|
|
|
|
SDL_GetError());
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
2018-10-02 08:21:42 +00:00
|
|
|
|
|
|
|
return true;
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|