Android/openslES: set audio in paused/resumed state for Android event loop

And also in "stopped" state before closing the device.
This commit is contained in:
Sylvain Becker 2019-01-14 12:33:29 +01:00
parent 59c8c7b684
commit 955d87894b
3 changed files with 61 additions and 14 deletions

View File

@ -34,12 +34,15 @@
#define LOG_TAG "SDL_openslES" #define LOG_TAG "SDL_openslES"
/*#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */ #if 0
/*#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */ #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
/*#define LOGI(...) do {} while (0) */ #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
/*#define LOGE(...) do {} while (0) */ // #define LOGI(...) do {} while (0)
// #define LOGE(...) do {} while (0)
#else
#define LOGI(...) #define LOGI(...)
#define LOGE(...) #define LOGE(...)
#endif
/* engine interfaces */ /* engine interfaces */
static SLObjectItf engineObject = NULL; static SLObjectItf engineObject = NULL;
@ -54,7 +57,7 @@ static SLObjectItf outputMixObject = NULL;
/* buffer queue player interfaces */ /* buffer queue player interfaces */
static SLObjectItf bqPlayerObject = NULL; static SLObjectItf bqPlayerObject = NULL;
static SLPlayItf bqPlayerPlay; static SLPlayItf bqPlayerItf;
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue; static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
/*static SLEffectSendItf bqPlayerEffectSend; */ /*static SLEffectSendItf bqPlayerEffectSend; */
static SLMuteSoloItf bqPlayerMuteSolo; static SLMuteSoloItf bqPlayerMuteSolo;
@ -162,7 +165,7 @@ static void openslES_DestroyPCMRecorder(_THIS);
static void openslES_DestroyEngine() static void openslES_DestroyEngine()
{ {
LOGI("openslES_DestroyEngine()"); LOGI("openslES_DestroyEngine()");
// openslES_DestroyPCMPlayer(this); // openslES_DestroyPCMPlayer(this);
// openslES_DestroyPCMRecorder(this); // openslES_DestroyPCMRecorder(this);
@ -236,11 +239,12 @@ openslES_CreatePCMPlayer(_THIS)
SLDataFormat_PCM format_pcm; SLDataFormat_PCM format_pcm;
SDL_AudioFormat test_format = 0;
SLresult result; SLresult result;
int i; int i;
#if 0 #if 0
SDL_AudioFormat test_format;
test_format = SDL_FirstAudioFormat( this->spec.format ); test_format = SDL_FirstAudioFormat( this->spec.format );
while (test_format != 0) { while (test_format != 0) {
@ -349,7 +353,7 @@ openslES_CreatePCMPlayer(_THIS)
} }
/* get the play interface */ /* get the play interface */
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay); result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerItf);
if (SL_RESULT_SUCCESS != result) { if (SL_RESULT_SUCCESS != result) {
LOGE("SL_IID_PLAY interface get failed"); LOGE("SL_IID_PLAY interface get failed");
goto failed; goto failed;
@ -396,7 +400,7 @@ openslES_CreatePCMPlayer(_THIS)
} }
/* set the player's state to playing */ /* set the player's state to playing */
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING); result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_PLAYING);
if (SL_RESULT_SUCCESS != result) { if (SL_RESULT_SUCCESS != result) {
LOGE("Play set state failed"); LOGE("Play set state failed");
goto failed; goto failed;
@ -433,6 +437,13 @@ static void
openslES_DestroyPCMPlayer(_THIS) openslES_DestroyPCMPlayer(_THIS)
{ {
struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden; struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
SLresult result;
/* set the player's state to 'stopped' */
result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_STOPPED);
if (SL_RESULT_SUCCESS != result) {
SDL_SetError("Stopped set state failed");
}
/* destroy buffer queue audio player object, and invalidate all associated interfaces */ /* destroy buffer queue audio player object, and invalidate all associated interfaces */
if (bqPlayerObject != NULL) { if (bqPlayerObject != NULL) {
@ -440,7 +451,7 @@ openslES_DestroyPCMPlayer(_THIS)
(*bqPlayerObject)->Destroy(bqPlayerObject); (*bqPlayerObject)->Destroy(bqPlayerObject);
bqPlayerObject = NULL; bqPlayerObject = NULL;
bqPlayerPlay = NULL; bqPlayerItf = NULL;
bqPlayerBufferQueue = NULL; bqPlayerBufferQueue = NULL;
/* bqPlayerEffectSend = NULL; */ /* bqPlayerEffectSend = NULL; */
bqPlayerMuteSolo = NULL; bqPlayerMuteSolo = NULL;
@ -488,7 +499,7 @@ openslES_CloseDevice(_THIS)
LOGI("openslES_CloseDevice( ) for playing"); LOGI("openslES_CloseDevice( ) for playing");
openslES_DestroyPCMPlayer(this); openslES_DestroyPCMPlayer(this);
} }
SDL_free(this->hidden); SDL_free(this->hidden);
return; return;
@ -587,6 +598,28 @@ AudioBootStrap openslES_bootstrap = {
"openslES", "opensl ES audio driver", openslES_Init, 0 "openslES", "opensl ES audio driver", openslES_Init, 0
}; };
void openslES_ResumeDevices()
{
SLresult result;
/* set the player's state to 'playing' */
result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_PLAYING);
if (SL_RESULT_SUCCESS != result) {
SDL_SetError("Play set state failed");
}
}
void openslES_PauseDevices()
{
SLresult result;
/* set the player's state to 'paused' */
result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_PAUSED);
if (SL_RESULT_SUCCESS != result) {
SDL_SetError("Playe set state failed");
}
}
#endif /* SDL_AUDIO_DRIVER_OPENSLES */ #endif /* SDL_AUDIO_DRIVER_OPENSLES */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -42,6 +42,9 @@ struct SDL_PrivateAudioData
#endif #endif
}; };
void openslES_ResumeDevices(void);
void openslES_PauseDevices(void);
#endif /* _SDL_openslesaudio_h */ #endif /* _SDL_openslesaudio_h */
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -30,9 +30,10 @@
#include "SDL_androidkeyboard.h" #include "SDL_androidkeyboard.h"
#include "SDL_androidwindow.h" #include "SDL_androidwindow.h"
#if !SDL_AUDIO_DISABLED
/* Can't include sysaudio "../../audio/android/SDL_androidaudio.h" /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
* because of THIS redefinition */ * because of THIS redefinition */
#if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_ANDROID
extern void ANDROIDAUDIO_ResumeDevices(void); extern void ANDROIDAUDIO_ResumeDevices(void);
extern void ANDROIDAUDIO_PauseDevices(void); extern void ANDROIDAUDIO_PauseDevices(void);
#else #else
@ -40,6 +41,14 @@ static void ANDROIDAUDIO_ResumeDevices(void) {}
static void ANDROIDAUDIO_PauseDevices(void) {} static void ANDROIDAUDIO_PauseDevices(void) {}
#endif #endif
#if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_OPENSLES
extern void openslES_ResumeDevices(void);
extern void openslES_PauseDevices(void);
#else
static void openslES_ResumeDevices(void) {}
static void openslES_PauseDevices(void) {}
#endif
/* Number of 'type' events in the event queue */ /* Number of 'type' events in the event queue */
static int static int
SDL_NumberOfEvents(Uint32 type) SDL_NumberOfEvents(Uint32 type)
@ -95,12 +104,14 @@ Android_PumpEvents(_THIS)
SDL_UnlockMutex(Android_ActivityMutex); SDL_UnlockMutex(Android_ActivityMutex);
ANDROIDAUDIO_PauseDevices(); ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();
if (SDL_SemWait(Android_ResumeSem) == 0) { if (SDL_SemWait(Android_ResumeSem) == 0) {
isPaused = 0; isPaused = 0;
ANDROIDAUDIO_ResumeDevices(); ANDROIDAUDIO_ResumeDevices();
openslES_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */ /* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) { if (!SDL_HasEvent(SDL_QUIT)) {
@ -111,7 +122,6 @@ Android_PumpEvents(_THIS)
/* Make sure SW Keyboard is restored when an app becomes foreground */ /* Make sure SW Keyboard is restored when an app becomes foreground */
if (SDL_IsTextInputActive()) { if (SDL_IsTextInputActive()) {
SDL_VideoDevice *_this = SDL_GetVideoDevice();
Android_StartTextInput(_this); /* Only showTextInput */ Android_StartTextInput(_this); /* Only showTextInput */
} }
} }
@ -144,6 +154,7 @@ Android_PumpEvents(_THIS)
isPaused = 0; isPaused = 0;
ANDROIDAUDIO_ResumeDevices(); ANDROIDAUDIO_ResumeDevices();
openslES_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */ /* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) { if (!SDL_HasEvent(SDL_QUIT)) {
@ -154,7 +165,6 @@ Android_PumpEvents(_THIS)
/* Make sure SW Keyboard is restored when an app becomes foreground */ /* Make sure SW Keyboard is restored when an app becomes foreground */
if (SDL_IsTextInputActive()) { if (SDL_IsTextInputActive()) {
SDL_VideoDevice *_this = SDL_GetVideoDevice();
Android_StartTextInput(_this); /* Only showTextInput */ Android_StartTextInput(_this); /* Only showTextInput */
} }
} }
@ -166,6 +176,7 @@ Android_PumpEvents(_THIS)
SDL_UnlockMutex(Android_ActivityMutex); SDL_UnlockMutex(Android_ActivityMutex);
ANDROIDAUDIO_PauseDevices(); ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();
isPaused = 1; isPaused = 1;
} }