From 46c9e447c84339c74d58a55322e6ecf3e6ba2058 Mon Sep 17 00:00:00 2001 From: Gabriel Jacobo Date: Thu, 8 Aug 2013 21:25:09 -0300 Subject: [PATCH] Start Android Audio Thread after audio buffer and AudioTrack are ready. Also, it starts the Audio Thread from the native side, putting the code in line with other backends. --- .../src/org/libsdl/app/SDLActivity.java | 36 +++---------------- src/audio/android/SDL_androidaudio.c | 21 ++++++----- src/audio/android/SDL_androidaudio.h | 2 ++ src/core/android/SDL_android.c | 13 +------ 4 files changed, 20 insertions(+), 52 deletions(-) diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java index b740d3f46..ed6f4ef32 100644 --- a/android-project/src/org/libsdl/app/SDLActivity.java +++ b/android-project/src/org/libsdl/app/SDLActivity.java @@ -252,8 +252,6 @@ public static native void onNativeTouch(int touchDevId, int pointerFingerId, int action, float x, float y, float p); public static native void onNativeAccel(float x, float y, float z); - public static native void nativeRunAudioThread(); - // Java functions called from C @@ -503,31 +501,15 @@ public static int audioInit(int sampleRate, boolean is16Bit, boolean isStereo, i mAudioTrack = null; return -1; } + + mAudioTrack.play(); } - - 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() { - if (mAudioThread == null) { - mAudioThread = new Thread(new Runnable() { - @Override - public void run() { - mAudioTrack.play(); - nativeRunAudioThread(); - } - }); - - // I'd take REALTIME if I could get it! - mAudioThread.setPriority(Thread.MAX_PRIORITY); - mAudioThread.start(); - } - } - public static void audioWriteShortBuffer(short[] buffer) { for (int i = 0; i < buffer.length; ) { int result = mAudioTrack.write(buffer, i, buffer.length - i); @@ -565,17 +547,6 @@ public static void audioWriteByteBuffer(byte[] buffer) { } public static void audioQuit() { - if (mAudioThread != null) { - try { - mAudioThread.join(); - } catch(Exception e) { - Log.v("SDL", "Problem stopping audio thread: " + e); - } - mAudioThread = null; - - //Log.v("SDL", "Finished waiting for audio thread"); - } - if (mAudioTrack != null) { mAudioTrack.stop(); mAudioTrack = null; @@ -932,3 +903,4 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) { public native void nativeSetComposingText(String text, int newCursorPosition); } + diff --git a/src/audio/android/SDL_androidaudio.c b/src/audio/android/SDL_androidaudio.c index 45c128282..ea8301bdf 100644 --- a/src/audio/android/SDL_androidaudio.c +++ b/src/audio/android/SDL_androidaudio.c @@ -50,7 +50,7 @@ AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture) audioDevice = this; - this->hidden = SDL_malloc(sizeof(*(this->hidden))); + this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(sizeof(*(this->hidden))); if (!this->hidden) { return SDL_OutOfMemory(); } @@ -91,6 +91,13 @@ AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture) /* Init failed? */ return SDL_SetError("Java-side initialization failed!"); } + + /* Audio thread is started here, after audio buffers and Java's AudioTrack are in place and ready to go */ + this->thread = SDL_CreateThread(SDL_RunAudio, "AndroidAudioThread", this); + if (this->thread == NULL) { + AndroidAUD_CloseDevice(this); + return SDL_SetError("Couldn't create audio thread"); + } return 0; } @@ -110,6 +117,10 @@ AndroidAUD_GetDeviceBuf(_THIS) static void AndroidAUD_CloseDevice(_THIS) { + /* At this point SDL_CloseAudioDevice via close_audio_device took care of terminating the audio thread + so it's safe to terminate the Java side buffer and AudioTrack + */ + if (this->hidden != NULL) { SDL_free(this->hidden); this->hidden = NULL; @@ -143,13 +154,7 @@ AudioBootStrap ANDROIDAUD_bootstrap = { "android", "SDL Android audio driver", AndroidAUD_Init, 0 }; -/* Called by the Java code to start the audio processing on a thread */ -void -Android_RunAudioThread() -{ - SDL_RunAudio(audioDevice); -} - #endif /* SDL_AUDIO_DRIVER_ANDROID */ /* vi: set ts=4 sw=4 expandtab: */ + diff --git a/src/audio/android/SDL_androidaudio.h b/src/audio/android/SDL_androidaudio.h index 383c708b7..c02ad1a7a 100644 --- a/src/audio/android/SDL_androidaudio.h +++ b/src/audio/android/SDL_androidaudio.h @@ -32,6 +32,8 @@ struct SDL_PrivateAudioData { }; +static void AndroidAUD_CloseDevice(_THIS); + #endif /* _SDL_androidaudio_h */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c index 059a2291f..b7d1ff6d1 100644 --- a/src/core/android/SDL_android.c +++ b/src/core/android/SDL_android.c @@ -47,9 +47,6 @@ /* Uncomment this to log messages entering and exiting methods in this file */ //#define DEBUG_JNI -/* Implemented in audio/android/SDL_androidaudio.c */ -extern void Android_RunAudioThread(); - static void Android_JNI_ThreadDestroyed(void*); /******************************************************************************* @@ -245,15 +242,6 @@ void Java_org_libsdl_app_SDLActivity_nativeResume( } } -void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread( - JNIEnv* env, jclass cls) -{ - /* This is the audio thread, with a different environment */ - Android_JNI_SetupThread(); - - Android_RunAudioThread(); -} - void Java_org_libsdl_app_SDLInputConnection_nativeCommitText( JNIEnv* env, jclass cls, jstring text, jint newCursorPosition) @@ -1374,3 +1362,4 @@ const char * SDL_AndroidGetExternalStoragePath() #endif /* __ANDROID__ */ /* vi: set ts=4 sw=4 expandtab: */ +