Skip to content

Commit

Permalink
Android/openslES: set audio in paused/resumed state for Android event…
Browse files Browse the repository at this point in the history
… loop

And also in "stopped" state before closing the device.
  • Loading branch information
1bsyl committed Jan 14, 2019
1 parent 59c8c7b commit 955d878
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
55 changes: 44 additions & 11 deletions src/audio/openslES/SDL_openslES.c
Expand Up @@ -34,12 +34,15 @@

#define LOG_TAG "SDL_openslES"

/*#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
/*#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
/*#define LOGI(...) do {} while (0) */
/*#define LOGE(...) do {} while (0) */
#if 0
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
// #define LOGI(...) do {} while (0)
// #define LOGE(...) do {} while (0)
#else
#define LOGI(...)
#define LOGE(...)
#endif

/* engine interfaces */
static SLObjectItf engineObject = NULL;
Expand All @@ -54,7 +57,7 @@ static SLObjectItf outputMixObject = NULL;

/* buffer queue player interfaces */
static SLObjectItf bqPlayerObject = NULL;
static SLPlayItf bqPlayerPlay;
static SLPlayItf bqPlayerItf;
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
/*static SLEffectSendItf bqPlayerEffectSend; */
static SLMuteSoloItf bqPlayerMuteSolo;
Expand Down Expand Up @@ -162,7 +165,7 @@ static void openslES_DestroyPCMRecorder(_THIS);
static void openslES_DestroyEngine()
{
LOGI("openslES_DestroyEngine()");

// openslES_DestroyPCMPlayer(this);
// openslES_DestroyPCMRecorder(this);

Expand Down Expand Up @@ -236,11 +239,12 @@ openslES_CreatePCMPlayer(_THIS)

SLDataFormat_PCM format_pcm;

SDL_AudioFormat test_format = 0;
SLresult result;
int i;

#if 0
SDL_AudioFormat test_format;

test_format = SDL_FirstAudioFormat( this->spec.format );

while (test_format != 0) {
Expand Down Expand Up @@ -349,7 +353,7 @@ openslES_CreatePCMPlayer(_THIS)
}

/* 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) {
LOGE("SL_IID_PLAY interface get failed");
goto failed;
Expand Down Expand Up @@ -396,7 +400,7 @@ openslES_CreatePCMPlayer(_THIS)
}

/* 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) {
LOGE("Play set state failed");
goto failed;
Expand Down Expand Up @@ -433,14 +437,21 @@ static void
openslES_DestroyPCMPlayer(_THIS)
{
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 */
if (bqPlayerObject != NULL) {

(*bqPlayerObject)->Destroy(bqPlayerObject);

bqPlayerObject = NULL;
bqPlayerPlay = NULL;
bqPlayerItf = NULL;
bqPlayerBufferQueue = NULL;
/* bqPlayerEffectSend = NULL; */
bqPlayerMuteSolo = NULL;
Expand Down Expand Up @@ -488,7 +499,7 @@ openslES_CloseDevice(_THIS)
LOGI("openslES_CloseDevice( ) for playing");
openslES_DestroyPCMPlayer(this);
}

SDL_free(this->hidden);

return;
Expand Down Expand Up @@ -587,6 +598,28 @@ AudioBootStrap openslES_bootstrap = {
"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 */

/* vi: set ts=4 sw=4 expandtab: */
3 changes: 3 additions & 0 deletions src/audio/openslES/SDL_openslES.h
Expand Up @@ -42,6 +42,9 @@ struct SDL_PrivateAudioData
#endif
};

void openslES_ResumeDevices(void);
void openslES_PauseDevices(void);

#endif /* _SDL_openslesaudio_h */

/* vi: set ts=4 sw=4 expandtab: */
17 changes: 14 additions & 3 deletions src/video/android/SDL_androidevents.c
Expand Up @@ -30,16 +30,25 @@
#include "SDL_androidkeyboard.h"
#include "SDL_androidwindow.h"

#if !SDL_AUDIO_DISABLED
/* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
* because of THIS redefinition */

#if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_ANDROID
extern void ANDROIDAUDIO_ResumeDevices(void);
extern void ANDROIDAUDIO_PauseDevices(void);
#else
static void ANDROIDAUDIO_ResumeDevices(void) {}
static void ANDROIDAUDIO_PauseDevices(void) {}
#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 */
static int
SDL_NumberOfEvents(Uint32 type)
Expand Down Expand Up @@ -95,12 +104,14 @@ Android_PumpEvents(_THIS)
SDL_UnlockMutex(Android_ActivityMutex);

ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();

if (SDL_SemWait(Android_ResumeSem) == 0) {

isPaused = 0;

ANDROIDAUDIO_ResumeDevices();
openslES_ResumeDevices();

/* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) {
Expand All @@ -111,7 +122,6 @@ Android_PumpEvents(_THIS)

/* Make sure SW Keyboard is restored when an app becomes foreground */
if (SDL_IsTextInputActive()) {
SDL_VideoDevice *_this = SDL_GetVideoDevice();
Android_StartTextInput(_this); /* Only showTextInput */
}
}
Expand Down Expand Up @@ -144,6 +154,7 @@ Android_PumpEvents(_THIS)
isPaused = 0;

ANDROIDAUDIO_ResumeDevices();
openslES_ResumeDevices();

/* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) {
Expand All @@ -154,7 +165,6 @@ Android_PumpEvents(_THIS)

/* Make sure SW Keyboard is restored when an app becomes foreground */
if (SDL_IsTextInputActive()) {
SDL_VideoDevice *_this = SDL_GetVideoDevice();
Android_StartTextInput(_this); /* Only showTextInput */
}
}
Expand All @@ -166,6 +176,7 @@ Android_PumpEvents(_THIS)
SDL_UnlockMutex(Android_ActivityMutex);

ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();

isPaused = 1;
}
Expand Down

0 comments on commit 955d878

Please sign in to comment.