[Android] Better fix for #2480, pause/resume audio
authorGabriel Jacobo <gabomdq@gmail.com>
Thu, 18 Sep 2014 11:03:34 -0300
changeset 9148e9b6e9f4a10e
parent 9147 6bf589c8d549
child 9149 e910a192f68f
[Android] Better fix for #2480, pause/resume audio
src/audio/SDL_audio.c
src/audio/android/SDL_androidaudio.c
src/audio/android/SDL_androidaudio.h
src/video/android/SDL_androidevents.c
     1.1 --- a/src/audio/SDL_audio.c	Wed Sep 17 14:49:36 2014 -0400
     1.2 +++ b/src/audio/SDL_audio.c	Thu Sep 18 11:03:34 2014 -0300
     1.3 @@ -1348,26 +1348,17 @@
     1.4  SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
     1.5  {
     1.6      SDL_AudioDevice *device = get_audio_device(devid);
     1.7 -    if (device && device->paused != pause_on) {
     1.8 -        if (pause_on) {
     1.9 -            current_audio.impl.LockDevice(device);
    1.10 -        }
    1.11 +    if (device) {
    1.12 +        current_audio.impl.LockDevice(device);
    1.13          device->paused = pause_on;
    1.14 -        if (!pause_on) {
    1.15 -            current_audio.impl.UnlockDevice(device);
    1.16 -        }
    1.17 +        current_audio.impl.UnlockDevice(device);
    1.18      }
    1.19  }
    1.20  
    1.21  void
    1.22  SDL_PauseAudio(int pause_on)
    1.23  {
    1.24 -    int id;
    1.25 -    for (id = 0; id < SDL_arraysize(open_devices); id++) {
    1.26 -        if (open_devices[id] != NULL) {
    1.27 -            SDL_PauseAudioDevice(id+1, pause_on);
    1.28 -        }
    1.29 -    }
    1.30 +    SDL_PauseAudioDevice(1, pause_on);
    1.31  }
    1.32  
    1.33  
     2.1 --- a/src/audio/android/SDL_androidaudio.c	Wed Sep 17 14:49:36 2014 -0400
     2.2 +++ b/src/audio/android/SDL_androidaudio.c	Thu Sep 18 11:03:34 2014 -0300
     2.3 @@ -32,7 +32,7 @@
     2.4  
     2.5  #include <android/log.h>
     2.6  
     2.7 -static void * audioDevice;
     2.8 +static SDL_AudioDevice* audioDevice = NULL;
     2.9  
    2.10  static int
    2.11  AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
    2.12 @@ -49,6 +49,11 @@
    2.13      }
    2.14  
    2.15      audioDevice = this;
    2.16 +    
    2.17 +    this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
    2.18 +    if (this->hidden == NULL) {
    2.19 +        return SDL_OutOfMemory();
    2.20 +    }
    2.21  
    2.22      test_format = SDL_FirstAudioFormat(this->spec.format);
    2.23      while (test_format != 0) { /* no "UNKNOWN" constant */
    2.24 @@ -110,6 +115,10 @@
    2.25      Android_JNI_CloseAudioDevice();
    2.26  
    2.27      if (audioDevice == this) {
    2.28 +        if (audioDevice->hidden != NULL) {
    2.29 +            SDL_free(this->hidden);
    2.30 +            this->hidden = NULL;
    2.31 +        }
    2.32          audioDevice = NULL;
    2.33      }
    2.34  }
    2.35 @@ -135,6 +144,41 @@
    2.36      "android", "SDL Android audio driver", AndroidAUD_Init, 0
    2.37  };
    2.38  
    2.39 +/* Pause (block) all non already paused audio devices by taking their mixer lock */
    2.40 +void AndroidAUD_PauseDevices(void)
    2.41 +{
    2.42 +    /* TODO: Handle multiple devices? */
    2.43 +    struct SDL_PrivateAudioData *private;
    2.44 +    if(audioDevice != NULL && audioDevice->hidden != NULL) {
    2.45 +        private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
    2.46 +        if (audioDevice->paused) {
    2.47 +            /* The device is already paused, leave it alone */
    2.48 +            private->resume = SDL_FALSE;
    2.49 +        }
    2.50 +        else {
    2.51 +            SDL_LockMutex(audioDevice->mixer_lock);
    2.52 +            audioDevice->paused = SDL_TRUE;
    2.53 +            private->resume = SDL_TRUE;
    2.54 +        }
    2.55 +    }
    2.56 +}
    2.57 +
    2.58 +/* Resume (unblock) all non already paused audio devices by releasing their mixer lock */
    2.59 +void AndroidAUD_ResumeDevices(void)
    2.60 +{
    2.61 +    /* TODO: Handle multiple devices? */
    2.62 +    struct SDL_PrivateAudioData *private;
    2.63 +    if(audioDevice != NULL && audioDevice->hidden != NULL) {
    2.64 +        private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
    2.65 +        if (private->resume) {
    2.66 +            audioDevice->paused = SDL_FALSE;
    2.67 +            private->resume = SDL_FALSE;
    2.68 +            SDL_UnlockMutex(audioDevice->mixer_lock);
    2.69 +        }
    2.70 +    }
    2.71 +}
    2.72 +
    2.73 +
    2.74  #endif /* SDL_AUDIO_DRIVER_ANDROID */
    2.75  
    2.76  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/audio/android/SDL_androidaudio.h	Wed Sep 17 14:49:36 2014 -0400
     3.2 +++ b/src/audio/android/SDL_androidaudio.h	Thu Sep 18 11:03:34 2014 -0300
     3.3 @@ -30,6 +30,8 @@
     3.4  
     3.5  struct SDL_PrivateAudioData
     3.6  {
     3.7 +    /* Resume device if it was paused automatically */
     3.8 +    int resume;
     3.9  };
    3.10  
    3.11  static void AndroidAUD_CloseDevice(_THIS);
     4.1 --- a/src/video/android/SDL_androidevents.c	Wed Sep 17 14:49:36 2014 -0400
     4.2 +++ b/src/video/android/SDL_androidevents.c	Thu Sep 18 11:03:34 2014 -0300
     4.3 @@ -29,8 +29,11 @@
     4.4  #include "SDL_events.h"
     4.5  #include "SDL_androidwindow.h"
     4.6  
     4.7 +
     4.8  void android_egl_context_backup();
     4.9  void android_egl_context_restore();
    4.10 +void AndroidAUD_ResumeDevices(void);
    4.11 +void AndroidAUD_PauseDevices(void);
    4.12  
    4.13  void 
    4.14  android_egl_context_restore() 
    4.15 @@ -74,14 +77,14 @@
    4.16      if (isPaused && !isPausing) {
    4.17          /* Make sure this is the last thing we do before pausing */
    4.18          android_egl_context_backup();
    4.19 -        SDL_PauseAudio(1);
    4.20 +        AndroidAUD_PauseDevices();
    4.21          if(SDL_SemWait(Android_ResumeSem) == 0) {
    4.22  #else
    4.23      if (isPaused) {
    4.24          if(SDL_SemTryWait(Android_ResumeSem) == 0) {
    4.25  #endif
    4.26              isPaused = 0;
    4.27 -            SDL_PauseAudio(0);
    4.28 +            AndroidAUD_ResumeDevices();
    4.29              /* Restore the GL Context from here, as this operation is thread dependent */
    4.30              if (!SDL_HasEvent(SDL_QUIT)) {
    4.31                  android_egl_context_restore();
    4.32 @@ -104,7 +107,7 @@
    4.33  #else
    4.34          if(SDL_SemTryWait(Android_PauseSem) == 0) {
    4.35              android_egl_context_backup();
    4.36 -            SDL_PauseAudio(1);
    4.37 +            AndroidAUD_PauseDevices();
    4.38              isPaused = 1;
    4.39          }
    4.40  #endif