From 657bf8c1a3dcb17d7508f5c6f0bf3d47b818a149 Mon Sep 17 00:00:00 2001 From: Xavier Sellier Date: Wed, 4 Apr 2018 14:22:41 -0400 Subject: [PATCH] Add check when pausing/unpausing audio OpenSL interface (android) --- platform/android/audio_driver_opensl.cpp | 169 +++-------------------- 1 file changed, 23 insertions(+), 146 deletions(-) diff --git a/platform/android/audio_driver_opensl.cpp b/platform/android/audio_driver_opensl.cpp index 345c14fe2b1..cae53a0478c 100644 --- a/platform/android/audio_driver_opensl.cpp +++ b/platform/android/audio_driver_opensl.cpp @@ -35,12 +35,7 @@ /* Structure for passing information to callback function */ -void AudioDriverOpenSL::_buffer_callback( - SLAndroidSimpleBufferQueueItf queueItf - /* SLuint32 eventFlags, - const void * pBuffer, - SLuint32 bufferSize, - SLuint32 dataUsed*/) { +void AudioDriverOpenSL::_buffer_callback(SLAndroidSimpleBufferQueueItf queueItf) { bool mix = true; @@ -74,34 +69,12 @@ void AudioDriverOpenSL::_buffer_callback( } (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size); - -#if 0 - SLresult res; - CallbackCntxt *pCntxt = (CallbackCntxt*)pContext; - if(pCntxt->pData < (pCntxt->pDataBase + pCntxt->size)) - { - res = (*queueItf)->Enqueue(queueItf, (void*) pCntxt->pData, - 2 * AUDIO_DATA_BUFFER_SIZE, SL_BOOLEAN_FALSE); /* Size given - in bytes. */ - CheckErr(res); - /* Increase data pointer by buffer size */ - pCntxt->pData += AUDIO_DATA_BUFFER_SIZE; - } - } -#endif } -void AudioDriverOpenSL::_buffer_callbacks( - SLAndroidSimpleBufferQueueItf queueItf, - /*SLuint32 eventFlags, - const void * pBuffer, - SLuint32 bufferSize, - SLuint32 dataUsed,*/ - void *pContext) { +void AudioDriverOpenSL::_buffer_callbacks(SLAndroidSimpleBufferQueueItf queueItf, void *pContext) { AudioDriverOpenSL *ad = (AudioDriverOpenSL *)pContext; - // ad->_buffer_callback(queueItf,eventFlags,pBuffer,bufferSize,dataUsed); ad->_buffer_callback(queueItf); } @@ -112,65 +85,6 @@ const char *AudioDriverOpenSL::get_name() const { return "Android"; } -#if 0 -int AudioDriverOpenSL::thread_func(SceSize args, void *argp) { - - AudioDriverOpenSL* ad = s_ad; - sceAudioOutput2Reserve(AUDIO_OUTPUT_SAMPLE); - - int half=0; - while(!ad->exit_thread) { - - int16_t *ptr = &ad->outbuff[AUDIO_OUTPUT_SAMPLE*2*half]; - - - - if (!ad->active) { - - for(int i=0;ilock(); - - ad->audio_server_process(AUDIO_OUTPUT_SAMPLE,ad->outbuff_32); - - ad->unlock(); - - const int32_t* src_buff=ad->outbuff_32; - - for(int i=0;i>16; - } - } - - - /* Output 16-bit PCM STEREO data that is in pcmBuf without changing the volume */ - sceAudioOutput2OutputBlocking( - SCE_AUDIO_VOLUME_0dB*3, //0db at 0x8000, that's obvious - ptr - ); - - if (half) - half=0; - else - half=1; - - } - - sceAudioOutput2Release(); - - sceKernelExitThread(SCE_KERNEL_EXIT_SUCCESS); - ad->thread_exited=true; - return SCE_KERNEL_EXIT_SUCCESS; - -} - -#endif Error AudioDriverOpenSL::init() { SLresult @@ -221,41 +135,24 @@ void AudioDriverOpenSL::start() { res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void *)&EngineItf); ERR_FAIL_COND(res != SL_RESULT_SUCCESS); + /* Initialize arrays required[] and iidArray[] */ SLboolean required[MAX_NUMBER_INTERFACES]; SLInterfaceID iidArray[MAX_NUMBER_INTERFACES]; -#if 0 - - for (int i=0; iCreateOutputMix(EngineItf, &OutputMix, 1, - iidArray, required); -#else - { const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB }; const SLboolean req[1] = { SL_BOOLEAN_FALSE }; res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, ids, req); } -#endif ERR_FAIL_COND(res != SL_RESULT_SUCCESS); // Realizing the Output Mix object in synchronous mode. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE); ERR_FAIL_COND(res != SL_RESULT_SUCCESS); SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT }; - // bufferQueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE; - // bufferQueue.numBuffers = BUFFER_COUNT; /* Four buffers in our buffer queue */ + /* Setup the format of the content in the buffer queue */ pcm.formatType = SL_DATAFORMAT_PCM; pcm.numChannels = 2; @@ -276,16 +173,11 @@ void AudioDriverOpenSL::start() { locator_outputmix.outputMix = OutputMix; audioSink.pLocator = (void *)&locator_outputmix; audioSink.pFormat = NULL; - /* Initialize the context for Buffer queue callbacks */ - // cntxt.pDataBase = (void*)&pcmData; - //cntxt.pData = cntxt.pDataBase; - //cntxt.size = sizeof(pcmData); - /* Set arrays required[] and iidArray[] for SEEK interface - (PlayItf is implicit) */ + + /* Set arrays required[] and iidArray[] for SEEK interface (PlayItf is implicit) */ required[0] = SL_BOOLEAN_TRUE; iidArray[0] = SL_IID_BUFFERQUEUE; /* Create the music player */ - { const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND }; const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE }; @@ -305,44 +197,18 @@ void AudioDriverOpenSL::start() { /* Setup to receive buffer queue event callbacks */ res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, _buffer_callbacks, this); ERR_FAIL_COND(res != SL_RESULT_SUCCESS); -/* Before we start set volume to -3dB (-300mB) */ -#if 0 - res = (*OutputMix)->GetInterface(OutputMix, SL_IID_VOLUME, - (void*)&volumeItf); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); - /* Setup the data source structure for the buffer queue */ - res = (*volumeItf)->SetVolumeLevel(volumeItf, -300); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); -#endif last_free = 0; -#if 1 + //fill up buffers for (int i = 0; i < BUFFER_COUNT; i++) { /* Enqueue a few buffers to get the ball rolling */ res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], 4 * buffer_size); /* Size given in */ } -#endif res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING); ERR_FAIL_COND(res != SL_RESULT_SUCCESS); -#if 0 - res = (*bufferQueueItf)->GetState(bufferQueueItf, &state); - ERR_FAIL_COND( res !=SL_RESULT_SUCCESS ); - while(state.count) - { - (*bufferQueueItf)->GetState(bufferQueueItf, &state); - } - /* Make sure player is stopped */ - res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); - CheckErr(res); - /* Destroy the player */ - (*player)->Destroy(player); - /* Destroy Output Mix object */ - (*OutputMix)->Destroy(OutputMix); -#endif - active = true; } @@ -375,19 +241,30 @@ void AudioDriverOpenSL::finish() { void AudioDriverOpenSL::set_pause(bool p_pause) { + SLresult res; + SLuint32 playState; + pause = p_pause; if (active) { - if (pause) { - (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED); - } else { - (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING); + res = (*playItf)->GetPlayState(playItf, &playState); + + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); + + if (pause && playState == SL_PLAYSTATE_PLAYING) { + res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED); + + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); + } else if (!pause && playState == SL_PLAYSTATE_PAUSED) { + res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING); + + ERR_FAIL_COND(res != SL_RESULT_SUCCESS); } } } AudioDriverOpenSL::AudioDriverOpenSL() { s_ad = this; - mutex = Mutex::create(); //NULL; + mutex = Mutex::create(); pause = false; }