Skip to content

Commit

Permalink
[Android] Better fix for #2480, pause/resume audio
Browse files Browse the repository at this point in the history
  • Loading branch information
gabomdq committed Sep 18, 2014
1 parent 5f9ea7e commit 4765805
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
17 changes: 4 additions & 13 deletions src/audio/SDL_audio.c
Expand Up @@ -1348,26 +1348,17 @@ void
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
{
SDL_AudioDevice *device = get_audio_device(devid);
if (device && device->paused != pause_on) {
if (pause_on) {
current_audio.impl.LockDevice(device);
}
if (device) {
current_audio.impl.LockDevice(device);
device->paused = pause_on;
if (!pause_on) {
current_audio.impl.UnlockDevice(device);
}
current_audio.impl.UnlockDevice(device);
}
}

void
SDL_PauseAudio(int pause_on)
{
int id;
for (id = 0; id < SDL_arraysize(open_devices); id++) {
if (open_devices[id] != NULL) {
SDL_PauseAudioDevice(id+1, pause_on);
}
}
SDL_PauseAudioDevice(1, pause_on);
}


Expand Down
46 changes: 45 additions & 1 deletion src/audio/android/SDL_androidaudio.c
Expand Up @@ -32,7 +32,7 @@

#include <android/log.h>

static void * audioDevice;
static SDL_AudioDevice* audioDevice = NULL;

static int
AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
Expand All @@ -49,6 +49,11 @@ AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
}

audioDevice = this;

this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
if (this->hidden == NULL) {
return SDL_OutOfMemory();
}

test_format = SDL_FirstAudioFormat(this->spec.format);
while (test_format != 0) { /* no "UNKNOWN" constant */
Expand Down Expand Up @@ -110,6 +115,10 @@ AndroidAUD_CloseDevice(_THIS)
Android_JNI_CloseAudioDevice();

if (audioDevice == this) {
if (audioDevice->hidden != NULL) {
SDL_free(this->hidden);
this->hidden = NULL;
}
audioDevice = NULL;
}
}
Expand All @@ -135,6 +144,41 @@ AudioBootStrap ANDROIDAUD_bootstrap = {
"android", "SDL Android audio driver", AndroidAUD_Init, 0
};

/* Pause (block) all non already paused audio devices by taking their mixer lock */
void AndroidAUD_PauseDevices(void)
{
/* TODO: Handle multiple devices? */
struct SDL_PrivateAudioData *private;
if(audioDevice != NULL && audioDevice->hidden != NULL) {
private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
if (audioDevice->paused) {
/* The device is already paused, leave it alone */
private->resume = SDL_FALSE;
}
else {
SDL_LockMutex(audioDevice->mixer_lock);
audioDevice->paused = SDL_TRUE;
private->resume = SDL_TRUE;
}
}
}

/* Resume (unblock) all non already paused audio devices by releasing their mixer lock */
void AndroidAUD_ResumeDevices(void)
{
/* TODO: Handle multiple devices? */
struct SDL_PrivateAudioData *private;
if(audioDevice != NULL && audioDevice->hidden != NULL) {
private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
if (private->resume) {
audioDevice->paused = SDL_FALSE;
private->resume = SDL_FALSE;
SDL_UnlockMutex(audioDevice->mixer_lock);
}
}
}


#endif /* SDL_AUDIO_DRIVER_ANDROID */

/* vi: set ts=4 sw=4 expandtab: */
Expand Down
2 changes: 2 additions & 0 deletions src/audio/android/SDL_androidaudio.h
Expand Up @@ -30,6 +30,8 @@

struct SDL_PrivateAudioData
{
/* Resume device if it was paused automatically */
int resume;
};

static void AndroidAUD_CloseDevice(_THIS);
Expand Down
9 changes: 6 additions & 3 deletions src/video/android/SDL_androidevents.c
Expand Up @@ -29,8 +29,11 @@
#include "SDL_events.h"
#include "SDL_androidwindow.h"


void android_egl_context_backup();
void android_egl_context_restore();
void AndroidAUD_ResumeDevices(void);
void AndroidAUD_PauseDevices(void);

void
android_egl_context_restore()
Expand Down Expand Up @@ -74,14 +77,14 @@ Android_PumpEvents(_THIS)
if (isPaused && !isPausing) {
/* Make sure this is the last thing we do before pausing */
android_egl_context_backup();
SDL_PauseAudio(1);
AndroidAUD_PauseDevices();
if(SDL_SemWait(Android_ResumeSem) == 0) {
#else
if (isPaused) {
if(SDL_SemTryWait(Android_ResumeSem) == 0) {
#endif
isPaused = 0;
SDL_PauseAudio(0);
AndroidAUD_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) {
android_egl_context_restore();
Expand All @@ -104,7 +107,7 @@ Android_PumpEvents(_THIS)
#else
if(SDL_SemTryWait(Android_PauseSem) == 0) {
android_egl_context_backup();
SDL_PauseAudio(1);
AndroidAUD_PauseDevices();
isPaused = 1;
}
#endif
Expand Down

0 comments on commit 4765805

Please sign in to comment.