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.
This commit is contained in:
Gabriel Jacobo 2013-08-08 21:25:09 -03:00
parent 98a222e974
commit 610264fb0b
4 changed files with 20 additions and 52 deletions

View file

@ -252,8 +252,6 @@ public class SDLActivity extends Activity {
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 class SDLActivity extends Activity {
mAudioTrack = null;
return -1;
}
}
audioStartThread();
mAudioTrack.play();
}
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 class SDLActivity extends Activity {
}
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 @@ class SDLInputConnection extends BaseInputConnection {
public native void nativeSetComposingText(String text, int newCursorPosition);
}

View file

@ -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();
}
@ -92,6 +92,13 @@ AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
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: */

View file

@ -32,6 +32,8 @@ struct SDL_PrivateAudioData
{
};
static void AndroidAUD_CloseDevice(_THIS);
#endif /* _SDL_androidaudio_h */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -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: */