2018-06-28 08:44:43 +00:00
|
|
|
#include "session.hpp"
|
|
|
|
|
2018-06-24 01:46:59 +00:00
|
|
|
#include <Limelight.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
#define MAX_CHANNELS 6
|
|
|
|
#define SAMPLES_PER_FRAME 240
|
2018-07-16 03:17:08 +00:00
|
|
|
#define MIN_QUEUED_FRAMES 2
|
|
|
|
#define MAX_QUEUED_FRAMES 4
|
2018-07-16 04:43:43 +00:00
|
|
|
#define STOP_THE_WORLD_LIMIT 20
|
2018-07-16 03:17:08 +00:00
|
|
|
#define DROP_RATIO_DENOM 32
|
2018-06-24 01:46:59 +00:00
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
SDL_AudioDeviceID Session::s_AudioDevice;
|
|
|
|
OpusMSDecoder* Session::s_OpusDecoder;
|
|
|
|
short Session::s_OpusDecodeBuffer[MAX_CHANNELS * SAMPLES_PER_FRAME];
|
|
|
|
int Session::s_ChannelCount;
|
2018-07-16 03:17:08 +00:00
|
|
|
int Session::s_PendingDrops;
|
2018-07-16 04:43:43 +00:00
|
|
|
int Session::s_PendingHardDrops;
|
2018-07-16 03:17:08 +00:00
|
|
|
unsigned int Session::s_SampleIndex;
|
2018-07-16 04:43:43 +00:00
|
|
|
Uint32 Session::s_BaselinePendingData;
|
2018-06-24 01:46:59 +00:00
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
int Session::sdlDetermineAudioConfiguration()
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
|
|
|
SDL_AudioSpec want, have;
|
|
|
|
SDL_AudioDeviceID dev;
|
2018-07-21 01:15:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_InitSubSystem(SDL_INIT_AUDIO) failed: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
return AUDIO_CONFIGURATION_STEREO;
|
|
|
|
}
|
2018-06-24 01:46:59 +00:00
|
|
|
|
|
|
|
SDL_zero(want);
|
|
|
|
want.freq = 48000;
|
|
|
|
want.format = AUDIO_S16;
|
|
|
|
want.channels = 6;
|
|
|
|
want.samples = 1024;
|
|
|
|
|
|
|
|
// Try to open for 5.1 surround sound, but allow SDL to tell us that's
|
|
|
|
// not available.
|
|
|
|
dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_CHANNELS_CHANGE);
|
|
|
|
if (dev == 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to open audio device");
|
|
|
|
// We'll probably have issues during audio stream init, but we'll
|
|
|
|
// try anyway
|
2018-07-21 01:15:46 +00:00
|
|
|
ret = AUDIO_CONFIGURATION_STEREO;
|
|
|
|
goto Exit;
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_CloseAudioDevice(dev);
|
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Audio device has %d channels", have.channels);
|
|
|
|
|
|
|
|
if (have.channels > 2) {
|
|
|
|
// We don't support quadraphonic or 7.1 surround, but SDL
|
|
|
|
// should be able to downmix or upmix better from 5.1 than
|
|
|
|
// from stereo, so use 5.1 in non-stereo cases.
|
2018-07-21 01:15:46 +00:00
|
|
|
ret = AUDIO_CONFIGURATION_51_SURROUND;
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-07-21 01:15:46 +00:00
|
|
|
ret = AUDIO_CONFIGURATION_STEREO;
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
2018-07-21 01:15:46 +00:00
|
|
|
|
|
|
|
Exit:
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
return ret;
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 18:54:12 +00:00
|
|
|
int Session::sdlAudioInit(int /* audioConfiguration */,
|
2018-06-28 08:44:43 +00:00
|
|
|
POPUS_MULTISTREAM_CONFIGURATION opusConfig,
|
2018-07-01 18:54:12 +00:00
|
|
|
void* /* arContext */, int /* arFlags */)
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
|
|
|
SDL_AudioSpec want, have;
|
|
|
|
int error;
|
|
|
|
|
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());
|
|
|
|
return AUDIO_CONFIGURATION_STEREO;
|
|
|
|
}
|
|
|
|
|
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-06-28 08:44:43 +00:00
|
|
|
s_AudioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
|
|
|
|
if (s_AudioDevice == 0) {
|
2018-06-24 01:46:59 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to open audio device: %s",
|
|
|
|
SDL_GetError());
|
2018-07-21 01:15:46 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
2018-06-24 01:46:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
s_OpusDecoder = opus_multistream_decoder_create(opusConfig->sampleRate,
|
2018-06-24 01:46:59 +00:00
|
|
|
opusConfig->channelCount,
|
|
|
|
opusConfig->streams,
|
|
|
|
opusConfig->coupledStreams,
|
|
|
|
opusConfig->mapping,
|
|
|
|
&error);
|
2018-06-28 08:44:43 +00:00
|
|
|
if (s_OpusDecoder == NULL) {
|
2018-06-24 01:46:59 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to create decoder: %d",
|
|
|
|
error);
|
2018-06-28 08:44:43 +00:00
|
|
|
SDL_CloseAudioDevice(s_AudioDevice);
|
|
|
|
s_AudioDevice = 0;
|
2018-07-21 01:15:46 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
2018-06-24 01:46:59 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2018-07-16 04:43:43 +00:00
|
|
|
// SDL counts pending samples in the queued
|
|
|
|
// audio size using the WASAPI backend. This
|
|
|
|
// includes silence, which can throw off our
|
|
|
|
// pending data count. Get a baseline so we
|
|
|
|
// can exclude that data.
|
|
|
|
s_BaselinePendingData = 0;
|
2018-07-22 00:00:09 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
2018-07-16 04:43:43 +00:00
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
s_BaselinePendingData = qMax(s_BaselinePendingData, SDL_GetQueuedAudioSize(s_AudioDevice));
|
|
|
|
SDL_Delay(10);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
s_BaselinePendingData *= 2;
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Baseline pending audio data: %d bytes",
|
|
|
|
s_BaselinePendingData);
|
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
s_ChannelCount = opusConfig->channelCount;
|
2018-07-16 03:17:08 +00:00
|
|
|
s_SampleIndex = 0;
|
2018-07-16 04:43:43 +00:00
|
|
|
s_PendingDrops = s_PendingHardDrops = 0;
|
2018-06-24 01:46:59 +00:00
|
|
|
|
2018-07-28 23:06:26 +00:00
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Audio stream has %d channels",
|
|
|
|
opusConfig->channelCount);
|
|
|
|
|
2018-06-24 01:46:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
void Session::sdlAudioStart()
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
|
|
|
// Unpause the audio device
|
2018-06-28 08:44:43 +00:00
|
|
|
SDL_PauseAudioDevice(s_AudioDevice, 0);
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
void Session::sdlAudioStop()
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
|
|
|
// Pause the audio device
|
2018-06-28 08:44:43 +00:00
|
|
|
SDL_PauseAudioDevice(s_AudioDevice, 1);
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
void Session::sdlAudioCleanup()
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
2018-06-28 08:44:43 +00:00
|
|
|
SDL_CloseAudioDevice(s_AudioDevice);
|
|
|
|
s_AudioDevice = 0;
|
2018-06-24 01:46:59 +00:00
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
opus_multistream_decoder_destroy(s_OpusDecoder);
|
2018-07-21 01:15:46 +00:00
|
|
|
s_OpusDecoder = nullptr;
|
|
|
|
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
SDL_assert(!SDL_WasInit(SDL_INIT_AUDIO));
|
2018-06-24 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
void Session::sdlAudioDecodeAndPlaySample(char* sampleData, int sampleLength)
|
2018-06-24 01:46:59 +00:00
|
|
|
{
|
|
|
|
int samplesDecoded;
|
|
|
|
|
2018-07-16 03:17:08 +00:00
|
|
|
s_SampleIndex++;
|
|
|
|
|
2018-07-16 04:43:43 +00:00
|
|
|
Uint32 queuedAudio = qMax((int)SDL_GetQueuedAudioSize(s_AudioDevice) - (int)s_BaselinePendingData, 0);
|
2018-07-16 03:17:08 +00:00
|
|
|
Uint32 framesQueued = queuedAudio / (SAMPLES_PER_FRAME * s_ChannelCount * sizeof(short));
|
|
|
|
|
2018-08-15 00:39:29 +00:00
|
|
|
// We must check this prior to the below checks to ensure we don't
|
|
|
|
// underflow if framesQueued - s_PendingHardDrops < 0.
|
|
|
|
if (framesQueued <= MIN_QUEUED_FRAMES) {
|
|
|
|
s_PendingDrops = s_PendingHardDrops = 0;
|
|
|
|
}
|
2018-07-16 04:43:43 +00:00
|
|
|
// Pend enough drops to get us back to MIN_QUEUED_FRAMES
|
2018-08-15 00:39:29 +00:00
|
|
|
else if (framesQueued - s_PendingHardDrops > STOP_THE_WORLD_LIMIT) {
|
2018-07-16 04:43:43 +00:00
|
|
|
s_PendingHardDrops = framesQueued - MIN_QUEUED_FRAMES;
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Pending hard drop of %u audio frames",
|
|
|
|
s_PendingHardDrops);
|
|
|
|
}
|
|
|
|
else if (framesQueued - s_PendingHardDrops - s_PendingDrops > MAX_QUEUED_FRAMES) {
|
|
|
|
s_PendingDrops = framesQueued - MIN_QUEUED_FRAMES;
|
2018-07-16 03:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this frame should be dropped
|
2018-08-15 00:39:29 +00:00
|
|
|
if (s_PendingHardDrops != 0) {
|
2018-07-16 04:43:43 +00:00
|
|
|
// Hard drops happen all at once to forcefully
|
|
|
|
// resync with the source.
|
|
|
|
s_PendingHardDrops--;
|
|
|
|
return;
|
2018-07-16 03:17:08 +00:00
|
|
|
}
|
|
|
|
else if (s_PendingDrops != 0 && s_SampleIndex % DROP_RATIO_DENOM == 0) {
|
2018-07-16 04:43:43 +00:00
|
|
|
// Normal drops are interspersed with the audio data
|
|
|
|
// to hide the glitches.
|
2018-07-16 03:17:08 +00:00
|
|
|
s_PendingDrops--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-28 08:44:43 +00:00
|
|
|
samplesDecoded = opus_multistream_decode(s_OpusDecoder,
|
2018-06-24 01:46:59 +00:00
|
|
|
(unsigned char*)sampleData,
|
|
|
|
sampleLength,
|
2018-06-28 08:44:43 +00:00
|
|
|
s_OpusDecodeBuffer,
|
2018-06-24 01:46:59 +00:00
|
|
|
SAMPLES_PER_FRAME,
|
|
|
|
0);
|
|
|
|
if (samplesDecoded > 0) {
|
2018-06-28 08:44:43 +00:00
|
|
|
if (SDL_QueueAudio(s_AudioDevice,
|
|
|
|
s_OpusDecodeBuffer,
|
|
|
|
sizeof(short) * samplesDecoded * s_ChannelCount) < 0) {
|
2018-06-24 01:46:59 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to queue audio sample: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|