Commit 955d87894b25916e75d5b90efb524a2bfd1794c1

Sylvain Becker 2019-01-14T12:33:29

Android/openslES: set audio in paused/resumed state for Android event loop And also in "stopped" state before closing the device.

diff --git a/src/audio/openslES/SDL_openslES.c b/src/audio/openslES/SDL_openslES.c
index 02efe1a..187e6e5 100644
--- a/src/audio/openslES/SDL_openslES.c
+++ b/src/audio/openslES/SDL_openslES.c
@@ -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;
@@ -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;
@@ -162,7 +165,7 @@ static void openslES_DestroyPCMRecorder(_THIS);
 static void openslES_DestroyEngine()
 {
     LOGI("openslES_DestroyEngine()");
-   
+
 //        openslES_DestroyPCMPlayer(this);
 //    openslES_DestroyPCMRecorder(this);
 
@@ -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) {
@@ -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;
@@ -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;
@@ -433,6 +437,13 @@ 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) {
@@ -440,7 +451,7 @@ openslES_DestroyPCMPlayer(_THIS)
         (*bqPlayerObject)->Destroy(bqPlayerObject);
 
         bqPlayerObject = NULL;
-        bqPlayerPlay = NULL;
+        bqPlayerItf = NULL;
         bqPlayerBufferQueue = NULL;
         /* bqPlayerEffectSend = NULL; */
         bqPlayerMuteSolo = NULL;
@@ -488,7 +499,7 @@ openslES_CloseDevice(_THIS)
         LOGI("openslES_CloseDevice( ) for playing");
         openslES_DestroyPCMPlayer(this);
     }
-    
+
     SDL_free(this->hidden);
 
     return;
@@ -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: */
diff --git a/src/audio/openslES/SDL_openslES.h b/src/audio/openslES/SDL_openslES.h
index 8ed2804..fde08e8 100644
--- a/src/audio/openslES/SDL_openslES.h
+++ b/src/audio/openslES/SDL_openslES.h
@@ -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: */
diff --git a/src/video/android/SDL_androidevents.c b/src/video/android/SDL_androidevents.c
index 92254ab..1dba149 100644
--- a/src/video/android/SDL_androidevents.c
+++ b/src/video/android/SDL_androidevents.c
@@ -30,9 +30,10 @@
 #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
@@ -40,6 +41,14 @@ 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)
@@ -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)) {
@@ -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 */
             }
         }
@@ -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)) {
@@ -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 */
             }
         }
@@ -166,6 +176,7 @@ Android_PumpEvents(_THIS)
             SDL_UnlockMutex(Android_ActivityMutex);
 
             ANDROIDAUDIO_PauseDevices();
+            openslES_PauseDevices();
 
             isPaused = 1;
         }