Fix for bug 2001, verify that the AudioTrack is properly initialized
authorGabriel Jacobo <gabomdq@gmail.com>
Wed, 31 Jul 2013 10:04:59 -0300
changeset 7552 463f78515a07
parent 7551 823c0c5b2d8d
child 7553 f0091f8eae33
Fix for bug 2001, verify that the AudioTrack is properly initialized ...and fail to initialize the audio backend if it's not!
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Tue Jul 30 23:33:32 2013 -0700
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jul 31 10:04:59 2013 -0300
@@ -452,7 +452,7 @@
     }
 
     // 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 @@
         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() {
--- a/src/core/android/SDL_android.c	Tue Jul 30 23:33:32 2013 -0700
+++ b/src/core/android/SDL_android.c	Wed Jul 31 10:04:59 2013 -0300
@@ -123,7 +123,7 @@
     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 @@
     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. */