Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Fix for bug 2001, verify that the AudioTrack is properly initialized

...and fail to initialize the audio backend if it's not!
  • Loading branch information
gabomdq committed Jul 31, 2013
1 parent 9095119 commit ce3a53954d0d5d65f35b1f9047f01d314db5503d
Showing with 18 additions and 3 deletions.
  1. +12 −1 android-project/src/org/libsdl/app/SDLActivity.java
  2. +6 −2 src/core/android/SDL_android.c
@@ -452,7 +452,7 @@ public static void flipEGL() {
}

// Audio
public static void audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
public static int audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
@@ -467,9 +467,20 @@ public static void audioInit(int sampleRate, boolean is16Bit, boolean isStereo,
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);

// Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
// Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()

if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Log.e("SDL", "Failed during initialization of Audio Track");
return -1;
}

audioStartThread();

Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");

return 0;
}

public static void audioStartThread() {
@@ -123,7 +123,7 @@ void SDL_Android_Init(JNIEnv* mEnv, jclass cls)
midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"flipBuffers","()V");
midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"audioInit", "(IZZI)V");
"audioInit", "(IZZI)I");
midAudioWriteShortBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
"audioWriteShortBuffer", "([S)V");
midAudioWriteByteBuffer = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
@@ -456,7 +456,11 @@ int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, i
audioBuffer16Bit = is16Bit;
audioBufferStereo = channelCount > 1;

(*env)->CallStaticVoidMethod(env, mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
if ((*env)->CallStaticIntMethod(env, mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames) != 0) {
/* Error during audio initialization */
__android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: error on AudioTrack initialization!");
return 0;
}

/* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
* Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */

0 comments on commit ce3a539

Please sign in to comment.