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

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.
  • Loading branch information
gabomdq committed Aug 9, 2013
1 parent 7d3b564 commit 46c9e447c84339c74d58a55322e6ecf3e6ba2058
@@ -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);

}

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

@@ -32,6 +32,8 @@ struct SDL_PrivateAudioData
{
};

static void AndroidAUD_CloseDevice(_THIS);

#endif /* _SDL_androidaudio_h */

/* vi: set ts=4 sw=4 expandtab: */
@@ -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: */

0 comments on commit 46c9e44

Please sign in to comment.