From 9aa826d25eea2dbab0dda0b454d29077e8f4973d Mon Sep 17 00:00:00 2001 From: Jean-Philip Desjardins Date: Thu, 31 Mar 2016 00:28:23 -0400 Subject: [PATCH] Sound should now work with OpenSL. --- Source/ui_android/SH_OpenSL.cpp | 25 ++++++++++++++++++++++++- Source/ui_android/SH_OpenSL.h | 10 ++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Source/ui_android/SH_OpenSL.cpp b/Source/ui_android/SH_OpenSL.cpp index d82b3d362..7af9907d9 100644 --- a/Source/ui_android/SH_OpenSL.cpp +++ b/Source/ui_android/SH_OpenSL.cpp @@ -54,7 +54,7 @@ void CSH_OpenSL::CreateAudioPlayer() SLDataLocator_AndroidSimpleBufferQueue bufferQueueLocator = {}; bufferQueueLocator.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE; - bufferQueueLocator.numBuffers = 2; + bufferQueueLocator.numBuffers = BUFFER_COUNT; SLDataFormat_PCM dataFormat = {}; dataFormat.formatType = SL_DATAFORMAT_PCM; @@ -90,6 +90,23 @@ void CSH_OpenSL::CreateAudioPlayer() result = (*m_playerObject)->GetInterface(m_playerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &m_playerQueue); assert(result == SL_RESULT_SUCCESS); + + result = (*m_playerQueue)->RegisterCallback(m_playerQueue, &CSH_OpenSL::QueueCallback, this); + assert(result == SL_RESULT_SUCCESS); + + result = (*m_playerPlay)->SetPlayState(m_playerPlay, SL_PLAYSTATE_PLAYING); + assert(result == SL_RESULT_SUCCESS); +} + +void CSH_OpenSL::QueueCallback(SLAndroidSimpleBufferQueueItf, void* context) +{ + reinterpret_cast(context)->QueueCallbackImpl(); +} + +void CSH_OpenSL::QueueCallbackImpl() +{ + assert(m_bufferCount != BUFFER_COUNT); + m_bufferCount++; } void CSH_OpenSL::Reset() @@ -100,16 +117,22 @@ void CSH_OpenSL::Reset() result = (*m_playerQueue)->Clear(m_playerQueue); assert(result == SL_RESULT_SUCCESS); + + m_bufferCount = BUFFER_COUNT; } void CSH_OpenSL::Write(int16* buffer, unsigned int sampleCount, unsigned int) { + if(m_bufferCount == 0) return; + assert(m_playerQueue != nullptr); SLresult result = SL_RESULT_SUCCESS; result = (*m_playerQueue)->Enqueue(m_playerQueue, buffer, sampleCount * sizeof(int16)); assert(result == SL_RESULT_SUCCESS); + + m_bufferCount--; } bool CSH_OpenSL::HasFreeBuffers() diff --git a/Source/ui_android/SH_OpenSL.h b/Source/ui_android/SH_OpenSL.h index b36f21908..b137eab7b 100644 --- a/Source/ui_android/SH_OpenSL.h +++ b/Source/ui_android/SH_OpenSL.h @@ -18,9 +18,17 @@ public: void RecycleBuffers() override; private: + enum + { + BUFFER_COUNT = 5, + }; + void CreateOutputMix(); void CreateAudioPlayer(); + static void QueueCallback(SLAndroidSimpleBufferQueueItf, void*); + void QueueCallbackImpl(); + SLObjectItf m_engineObject = nullptr; SLEngineItf m_engine = nullptr; @@ -29,4 +37,6 @@ private: SLObjectItf m_playerObject = nullptr; SLPlayItf m_playerPlay = nullptr; SLAndroidSimpleBufferQueueItf m_playerQueue = nullptr; + + uint32 m_bufferCount = BUFFER_COUNT; };