Skip to content

Commit

Permalink
Fixed bug 3930 - Android, set thread priorities and names
Browse files Browse the repository at this point in the history
SDLActivity thread priority is unchanged, by default -10 (THREAD_PRIORITY_VIDEO).

SDLAudio thread priority was -4 (SDL_SetThreadPriority was ignored) and is now -16 (THREAD_PRIORITY_AUDIO).

SDLThread thread priority was 0 (THREAD_PRIORITY_DEFAULT) and is -4 (THREAD_PRIORITY_DISPLAY).
  • Loading branch information
1bsyl committed Jan 10, 2019
1 parent 0e0e027 commit d23c2f0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
13 changes: 13 additions & 0 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Expand Up @@ -193,6 +193,12 @@ protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate()");
super.onCreate(savedInstanceState);

try {
Thread.currentThread().setName("SDLActivity");
} catch (Exception e) {
Log.v(TAG, "modify thread properties failed " + e.toString());
}

// Load shared libraries
String errorMsgBrokenLib = "";
try {
Expand Down Expand Up @@ -1494,7 +1500,14 @@ public void run() {
String function = SDLActivity.mSingleton.getMainFunction();
String[] arguments = SDLActivity.mSingleton.getArguments();

try {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
} catch (Exception e) {
Log.v("SDL", "modify thread properties failed " + e.toString());
}

Log.v("SDL", "Running main function " + function + " from library " + library);

SDLActivity.nativeRunMain(library, function, arguments);

Log.v("SDL", "Finished main function");
Expand Down
Expand Up @@ -73,7 +73,7 @@ protected static int[] open(boolean isCapture, int sampleRate, int audioFormat,
sampleSize = 2;
break;
}

if (isCapture) {
switch (desiredChannels) {
case 1:
Expand Down Expand Up @@ -298,7 +298,7 @@ public static void audioWriteByteBuffer(byte[] buffer) {
Log.e(TAG, "Attempted to make audio call with uninitialized audio!");
return;
}

for (int i = 0; i < buffer.length; ) {
int result = mAudioTrack.write(buffer, i, buffer.length - i);
if (result > 0) {
Expand Down Expand Up @@ -364,5 +364,24 @@ public static void captureClose() {
}
}

/** This method is called by SDL using JNI. */
public static void audioSetThreadPriority(boolean iscapture, int device_id) {
try {

/* Set thread name */
if (iscapture) {
Thread.currentThread().setName("SDLAudioC" + device_id);
} else {
Thread.currentThread().setName("SDLAudioP" + device_id);
}

/* Set thread priority */
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);

} catch (Exception e) {
Log.v(TAG, "modify thread properties failed " + e.toString());
}
}

public static native int nativeSetupJNI();
}
16 changes: 16 additions & 0 deletions src/audio/SDL_audio.c
Expand Up @@ -695,8 +695,16 @@ SDL_RunAudio(void *devicep)

SDL_assert(!device->iscapture);

#if SDL_AUDIO_DRIVER_ANDROID
{
/* Set thread priority to THREAD_PRIORITY_AUDIO */
extern void Android_JNI_AudioSetThreadPriority(int, int);
Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
}
#else
/* The audio mixing is always a high priority thread */
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
#endif

/* Perform any thread setup */
device->threadid = SDL_ThreadID();
Expand Down Expand Up @@ -792,8 +800,16 @@ SDL_CaptureAudio(void *devicep)

SDL_assert(device->iscapture);

#if SDL_AUDIO_DRIVER_ANDROID
{
/* Set thread priority to THREAD_PRIORITY_AUDIO */
extern void Android_JNI_AudioSetThreadPriority(int, int);
Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
}
#else
/* The audio mixing is always a high priority thread */
SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
#endif

/* Perform any thread setup */
device->threadid = SDL_ThreadID();
Expand Down
11 changes: 10 additions & 1 deletion src/core/android/SDL_android.c
Expand Up @@ -268,6 +268,7 @@ static jmethodID midCaptureReadByteBuffer;
static jmethodID midCaptureReadShortBuffer;
static jmethodID midCaptureReadFloatBuffer;
static jmethodID midCaptureClose;
static jmethodID midAudioSetThreadPriority;

/* controller manager */
static jclass mControllerManagerClass;
Expand Down Expand Up @@ -442,9 +443,11 @@ JNIEXPORT void JNICALL SDL_JAVA_AUDIO_INTERFACE(nativeSetupJNI)(JNIEnv *mEnv, jc
"captureReadFloatBuffer", "([FZ)I");
midCaptureClose = (*mEnv)->GetStaticMethodID(mEnv, mAudioManagerClass,
"captureClose", "()V");
midAudioSetThreadPriority = (*mEnv)->GetStaticMethodID(mEnv, mAudioManagerClass,
"audioSetThreadPriority", "(ZI)V");

if (!midAudioOpen || !midAudioWriteByteBuffer || !midAudioWriteShortBuffer || !midAudioWriteFloatBuffer || !midAudioClose ||
!midCaptureOpen || !midCaptureReadByteBuffer || !midCaptureReadShortBuffer || !midCaptureReadFloatBuffer || !midCaptureClose) {
!midCaptureOpen || !midCaptureReadByteBuffer || !midCaptureReadShortBuffer || !midCaptureReadFloatBuffer || !midCaptureClose || !midAudioSetThreadPriority) {
__android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLAudioManager.java?");
}

Expand Down Expand Up @@ -1467,6 +1470,12 @@ void Android_JNI_CloseAudioDevice(const int iscapture)
}
}

void Android_JNI_AudioSetThreadPriority(int iscapture, int device_id)
{
JNIEnv *env = Android_JNI_GetEnv();
(*env)->CallStaticVoidMethod(env, mAudioManagerClass, midAudioSetThreadPriority, iscapture, device_id);
}

/* Test for an exception and call SDL_SetError with its detail if one occurs */
/* If the parameter silent is truthy then SDL_SetError() will not be called. */
static SDL_bool Android_JNI_ExceptionOccurred(SDL_bool silent)
Expand Down
1 change: 1 addition & 0 deletions src/core/android/SDL_android.h
Expand Up @@ -55,6 +55,7 @@ extern void Android_JNI_WriteAudioBuffer(void);
extern int Android_JNI_CaptureAudioBuffer(void *buffer, int buflen);
extern void Android_JNI_FlushCapturedAudio(void);
extern void Android_JNI_CloseAudioDevice(const int iscapture);
extern void Android_JNI_AudioSetThreadPriority(int iscapture, int device_id);

/* Detecting device type */
extern SDL_bool Android_IsDeXMode();
Expand Down

0 comments on commit d23c2f0

Please sign in to comment.