Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fix for bug 2001, verify that the AudioTrack is properly initialized
Browse files Browse the repository at this point in the history
...and fail to initialize the audio backend if it's not!
  • Loading branch information
gabomdq committed Jul 31, 2013
1 parent 9095119 commit ce3a539
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 12 additions & 1 deletion android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions src/core/android/SDL_android.c
Expand Up @@ -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,
Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit ce3a539

Please sign in to comment.