Android/openslES: set audio in paused/resumed state for Android event loop
authorSylvain Becker <sylvain.becker@gmail.com>
Mon, 14 Jan 2019 12:33:29 +0100
changeset 12541f0b89ca0ad7d
parent 12540 2660c01ff7da
child 12542 c86d239a58f0
Android/openslES: set audio in paused/resumed state for Android event loop
And also in "stopped" state before closing the device.
src/audio/openslES/SDL_openslES.c
src/audio/openslES/SDL_openslES.h
src/video/android/SDL_androidevents.c
     1.1 --- a/src/audio/openslES/SDL_openslES.c	Mon Jan 14 10:58:57 2019 +0100
     1.2 +++ b/src/audio/openslES/SDL_openslES.c	Mon Jan 14 12:33:29 2019 +0100
     1.3 @@ -34,12 +34,15 @@
     1.4  
     1.5  #define LOG_TAG "SDL_openslES"
     1.6  
     1.7 -/*#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) */
     1.8 -/*#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) */
     1.9 -/*#define LOGI(...) do {} while (0) */
    1.10 -/*#define LOGE(...) do {} while (0) */
    1.11 +#if 0
    1.12 +#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
    1.13 +#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
    1.14 +// #define LOGI(...) do {} while (0)
    1.15 +// #define LOGE(...) do {} while (0)
    1.16 +#else
    1.17  #define LOGI(...)
    1.18  #define LOGE(...)
    1.19 +#endif
    1.20  
    1.21  /* engine interfaces */
    1.22  static SLObjectItf engineObject = NULL;
    1.23 @@ -54,7 +57,7 @@
    1.24  
    1.25  /* buffer queue player interfaces */
    1.26  static SLObjectItf                   bqPlayerObject = NULL;
    1.27 -static SLPlayItf                     bqPlayerPlay;
    1.28 +static SLPlayItf                     bqPlayerItf;
    1.29  static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
    1.30  /*static SLEffectSendItf          bqPlayerEffectSend; */
    1.31  static SLMuteSoloItf                 bqPlayerMuteSolo;
    1.32 @@ -162,7 +165,7 @@
    1.33  static void openslES_DestroyEngine()
    1.34  {
    1.35      LOGI("openslES_DestroyEngine()");
    1.36 -   
    1.37 +
    1.38  //        openslES_DestroyPCMPlayer(this);
    1.39  //    openslES_DestroyPCMRecorder(this);
    1.40  
    1.41 @@ -236,11 +239,12 @@
    1.42  
    1.43      SLDataFormat_PCM format_pcm;
    1.44  
    1.45 +    SDL_AudioFormat test_format = 0;
    1.46      SLresult result;
    1.47      int i;
    1.48  
    1.49  #if 0
    1.50 -      SDL_AudioFormat test_format;
    1.51 +
    1.52        test_format = SDL_FirstAudioFormat( this->spec.format );
    1.53  
    1.54        while (test_format != 0) {
    1.55 @@ -349,7 +353,7 @@
    1.56      }
    1.57  
    1.58      /* get the play interface */
    1.59 -    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
    1.60 +    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerItf);
    1.61      if (SL_RESULT_SUCCESS != result) {
    1.62          LOGE("SL_IID_PLAY interface get failed");
    1.63          goto failed;
    1.64 @@ -396,7 +400,7 @@
    1.65      }
    1.66  
    1.67      /* set the player's state to playing */
    1.68 -    result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
    1.69 +    result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_PLAYING);
    1.70      if (SL_RESULT_SUCCESS != result) {
    1.71          LOGE("Play set state failed");
    1.72          goto failed;
    1.73 @@ -433,6 +437,13 @@
    1.74  openslES_DestroyPCMPlayer(_THIS)
    1.75  {
    1.76      struct SDL_PrivateAudioData *audiodata = (struct SDL_PrivateAudioData *) this->hidden;
    1.77 +    SLresult result;
    1.78 +
    1.79 +    /* set the player's state to 'stopped' */
    1.80 +    result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_STOPPED);
    1.81 +    if (SL_RESULT_SUCCESS != result) {
    1.82 +        SDL_SetError("Stopped set state failed");
    1.83 +    }
    1.84  
    1.85      /* destroy buffer queue audio player object, and invalidate all associated interfaces */
    1.86      if (bqPlayerObject != NULL) {
    1.87 @@ -440,7 +451,7 @@
    1.88          (*bqPlayerObject)->Destroy(bqPlayerObject);
    1.89  
    1.90          bqPlayerObject = NULL;
    1.91 -        bqPlayerPlay = NULL;
    1.92 +        bqPlayerItf = NULL;
    1.93          bqPlayerBufferQueue = NULL;
    1.94          /* bqPlayerEffectSend = NULL; */
    1.95          bqPlayerMuteSolo = NULL;
    1.96 @@ -488,7 +499,7 @@
    1.97          LOGI("openslES_CloseDevice( ) for playing");
    1.98          openslES_DestroyPCMPlayer(this);
    1.99      }
   1.100 -    
   1.101 +
   1.102      SDL_free(this->hidden);
   1.103  
   1.104      return;
   1.105 @@ -587,6 +598,28 @@
   1.106      "openslES", "opensl ES audio driver", openslES_Init, 0
   1.107  };
   1.108  
   1.109 +void openslES_ResumeDevices()
   1.110 +{
   1.111 +    SLresult result;
   1.112 +
   1.113 +    /* set the player's state to 'playing' */
   1.114 +    result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_PLAYING);
   1.115 +    if (SL_RESULT_SUCCESS != result) {
   1.116 +        SDL_SetError("Play set state failed");
   1.117 +    }
   1.118 +}
   1.119 +
   1.120 +void openslES_PauseDevices()
   1.121 +{
   1.122 +    SLresult result;
   1.123 +
   1.124 +    /* set the player's state to 'paused' */
   1.125 +    result = (*bqPlayerItf)->SetPlayState(bqPlayerItf, SL_PLAYSTATE_PAUSED);
   1.126 +    if (SL_RESULT_SUCCESS != result) {
   1.127 +        SDL_SetError("Playe set state failed");
   1.128 +    }
   1.129 +}
   1.130 +
   1.131  #endif /* SDL_AUDIO_DRIVER_OPENSLES */
   1.132  
   1.133  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/audio/openslES/SDL_openslES.h	Mon Jan 14 10:58:57 2019 +0100
     2.2 +++ b/src/audio/openslES/SDL_openslES.h	Mon Jan 14 12:33:29 2019 +0100
     2.3 @@ -42,6 +42,9 @@
     2.4  #endif
     2.5  };
     2.6  
     2.7 +void openslES_ResumeDevices(void);
     2.8 +void openslES_PauseDevices(void);
     2.9 +
    2.10  #endif /* _SDL_openslesaudio_h */
    2.11  
    2.12  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/android/SDL_androidevents.c	Mon Jan 14 10:58:57 2019 +0100
     3.2 +++ b/src/video/android/SDL_androidevents.c	Mon Jan 14 12:33:29 2019 +0100
     3.3 @@ -30,9 +30,10 @@
     3.4  #include "SDL_androidkeyboard.h"
     3.5  #include "SDL_androidwindow.h"
     3.6  
     3.7 -#if !SDL_AUDIO_DISABLED
     3.8  /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
     3.9   * because of THIS redefinition */
    3.10 +
    3.11 +#if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_ANDROID
    3.12  extern void ANDROIDAUDIO_ResumeDevices(void);
    3.13  extern void ANDROIDAUDIO_PauseDevices(void);
    3.14  #else
    3.15 @@ -40,6 +41,14 @@
    3.16  static void ANDROIDAUDIO_PauseDevices(void) {}
    3.17  #endif
    3.18  
    3.19 +#if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_OPENSLES
    3.20 +extern void openslES_ResumeDevices(void);
    3.21 +extern void openslES_PauseDevices(void);
    3.22 +#else
    3.23 +static void openslES_ResumeDevices(void) {}
    3.24 +static void openslES_PauseDevices(void) {}
    3.25 +#endif
    3.26 +
    3.27  /* Number of 'type' events in the event queue */
    3.28  static int
    3.29  SDL_NumberOfEvents(Uint32 type)
    3.30 @@ -95,12 +104,14 @@
    3.31          SDL_UnlockMutex(Android_ActivityMutex);
    3.32  
    3.33          ANDROIDAUDIO_PauseDevices();
    3.34 +        openslES_PauseDevices();
    3.35  
    3.36          if (SDL_SemWait(Android_ResumeSem) == 0) {
    3.37  
    3.38              isPaused = 0;
    3.39  
    3.40              ANDROIDAUDIO_ResumeDevices();
    3.41 +            openslES_ResumeDevices();
    3.42  
    3.43              /* Restore the GL Context from here, as this operation is thread dependent */
    3.44              if (!SDL_HasEvent(SDL_QUIT)) {
    3.45 @@ -111,7 +122,6 @@
    3.46  
    3.47              /* Make sure SW Keyboard is restored when an app becomes foreground */
    3.48              if (SDL_IsTextInputActive()) {
    3.49 -                SDL_VideoDevice *_this = SDL_GetVideoDevice();
    3.50                  Android_StartTextInput(_this); /* Only showTextInput */
    3.51              }
    3.52          }
    3.53 @@ -144,6 +154,7 @@
    3.54              isPaused = 0;
    3.55  
    3.56              ANDROIDAUDIO_ResumeDevices();
    3.57 +            openslES_ResumeDevices();
    3.58  
    3.59              /* Restore the GL Context from here, as this operation is thread dependent */
    3.60              if (!SDL_HasEvent(SDL_QUIT)) {
    3.61 @@ -154,7 +165,6 @@
    3.62  
    3.63              /* Make sure SW Keyboard is restored when an app becomes foreground */
    3.64              if (SDL_IsTextInputActive()) {
    3.65 -                SDL_VideoDevice *_this = SDL_GetVideoDevice();
    3.66                  Android_StartTextInput(_this); /* Only showTextInput */
    3.67              }
    3.68          }
    3.69 @@ -166,6 +176,7 @@
    3.70              SDL_UnlockMutex(Android_ActivityMutex);
    3.71  
    3.72              ANDROIDAUDIO_PauseDevices();
    3.73 +            openslES_PauseDevices();
    3.74  
    3.75              isPaused = 1;
    3.76          }