Fixed bug 3930 - Android, set thread priorities and names
authorSylvain Becker <sylvain.becker@gmail.com>
Thu, 10 Jan 2019 18:05:56 +0100
changeset 125212a567875fe8e
parent 12520 34126acf146f
child 12522 24bbe1496ef7
Fixed bug 3930 - Android, set thread priorities and names

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).
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java
src/audio/SDL_audio.c
src/core/android/SDL_android.c
src/core/android/SDL_android.h
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jan 10 16:04:52 2019 +0100
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jan 10 18:05:56 2019 +0100
     1.3 @@ -193,6 +193,12 @@
     1.4          Log.v(TAG, "onCreate()");
     1.5          super.onCreate(savedInstanceState);
     1.6  
     1.7 +        try {
     1.8 +            Thread.currentThread().setName("SDLActivity");
     1.9 +        } catch (Exception e) {
    1.10 +            Log.v(TAG, "modify thread properties failed " + e.toString());
    1.11 +        }
    1.12 +
    1.13          // Load shared libraries
    1.14          String errorMsgBrokenLib = "";
    1.15          try {
    1.16 @@ -1494,7 +1500,14 @@
    1.17          String function = SDLActivity.mSingleton.getMainFunction();
    1.18          String[] arguments = SDLActivity.mSingleton.getArguments();
    1.19  
    1.20 +        try {
    1.21 +            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
    1.22 +        } catch (Exception e) {
    1.23 +            Log.v("SDL", "modify thread properties failed " + e.toString());
    1.24 +        }
    1.25 +
    1.26          Log.v("SDL", "Running main function " + function + " from library " + library);
    1.27 +
    1.28          SDLActivity.nativeRunMain(library, function, arguments);
    1.29  
    1.30          Log.v("SDL", "Finished main function");
     2.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java	Thu Jan 10 16:04:52 2019 +0100
     2.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLAudioManager.java	Thu Jan 10 18:05:56 2019 +0100
     2.3 @@ -73,7 +73,7 @@
     2.4              sampleSize = 2;
     2.5              break;
     2.6          }
     2.7 - 
     2.8 +
     2.9          if (isCapture) {
    2.10              switch (desiredChannels) {
    2.11              case 1:
    2.12 @@ -298,7 +298,7 @@
    2.13              Log.e(TAG, "Attempted to make audio call with uninitialized audio!");
    2.14              return;
    2.15          }
    2.16 -        
    2.17 +
    2.18          for (int i = 0; i < buffer.length; ) {
    2.19              int result = mAudioTrack.write(buffer, i, buffer.length - i);
    2.20              if (result > 0) {
    2.21 @@ -364,5 +364,24 @@
    2.22          }
    2.23      }
    2.24  
    2.25 +    /** This method is called by SDL using JNI. */
    2.26 +    public static void audioSetThreadPriority(boolean iscapture, int device_id) {
    2.27 +        try {
    2.28 +
    2.29 +            /* Set thread name */
    2.30 +            if (iscapture) {
    2.31 +                Thread.currentThread().setName("SDLAudioC" + device_id);
    2.32 +            } else {
    2.33 +                Thread.currentThread().setName("SDLAudioP" + device_id);
    2.34 +            }
    2.35 +
    2.36 +            /* Set thread priority */
    2.37 +            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
    2.38 +
    2.39 +        } catch (Exception e) {
    2.40 +            Log.v(TAG, "modify thread properties failed " + e.toString());
    2.41 +        }
    2.42 +    }
    2.43 +
    2.44      public static native int nativeSetupJNI();
    2.45  }
     3.1 --- a/src/audio/SDL_audio.c	Thu Jan 10 16:04:52 2019 +0100
     3.2 +++ b/src/audio/SDL_audio.c	Thu Jan 10 18:05:56 2019 +0100
     3.3 @@ -695,8 +695,16 @@
     3.4  
     3.5      SDL_assert(!device->iscapture);
     3.6  
     3.7 +#if SDL_AUDIO_DRIVER_ANDROID
     3.8 +    {
     3.9 +        /* Set thread priority to THREAD_PRIORITY_AUDIO */
    3.10 +        extern void Android_JNI_AudioSetThreadPriority(int, int);
    3.11 +        Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
    3.12 +    }
    3.13 +#else
    3.14      /* The audio mixing is always a high priority thread */
    3.15      SDL_SetThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
    3.16 +#endif
    3.17  
    3.18      /* Perform any thread setup */
    3.19      device->threadid = SDL_ThreadID();
    3.20 @@ -792,8 +800,16 @@
    3.21  
    3.22      SDL_assert(device->iscapture);
    3.23  
    3.24 +#if SDL_AUDIO_DRIVER_ANDROID
    3.25 +    {
    3.26 +        /* Set thread priority to THREAD_PRIORITY_AUDIO */
    3.27 +        extern void Android_JNI_AudioSetThreadPriority(int, int);
    3.28 +        Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
    3.29 +    }
    3.30 +#else
    3.31      /* The audio mixing is always a high priority thread */
    3.32      SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
    3.33 +#endif
    3.34  
    3.35      /* Perform any thread setup */
    3.36      device->threadid = SDL_ThreadID();
     4.1 --- a/src/core/android/SDL_android.c	Thu Jan 10 16:04:52 2019 +0100
     4.2 +++ b/src/core/android/SDL_android.c	Thu Jan 10 18:05:56 2019 +0100
     4.3 @@ -268,6 +268,7 @@
     4.4  static jmethodID midCaptureReadShortBuffer;
     4.5  static jmethodID midCaptureReadFloatBuffer;
     4.6  static jmethodID midCaptureClose;
     4.7 +static jmethodID midAudioSetThreadPriority;
     4.8  
     4.9  /* controller manager */
    4.10  static jclass mControllerManagerClass;
    4.11 @@ -442,9 +443,11 @@
    4.12                                  "captureReadFloatBuffer", "([FZ)I");
    4.13      midCaptureClose = (*mEnv)->GetStaticMethodID(mEnv, mAudioManagerClass,
    4.14                                  "captureClose", "()V");
    4.15 +    midAudioSetThreadPriority = (*mEnv)->GetStaticMethodID(mEnv, mAudioManagerClass,
    4.16 +                                "audioSetThreadPriority", "(ZI)V");
    4.17  
    4.18      if (!midAudioOpen || !midAudioWriteByteBuffer || !midAudioWriteShortBuffer || !midAudioWriteFloatBuffer || !midAudioClose ||
    4.19 -       !midCaptureOpen || !midCaptureReadByteBuffer || !midCaptureReadShortBuffer || !midCaptureReadFloatBuffer || !midCaptureClose) {
    4.20 +       !midCaptureOpen || !midCaptureReadByteBuffer || !midCaptureReadShortBuffer || !midCaptureReadFloatBuffer || !midCaptureClose || !midAudioSetThreadPriority) {
    4.21          __android_log_print(ANDROID_LOG_WARN, "SDL", "Missing some Java callbacks, do you have the latest version of SDLAudioManager.java?");
    4.22      }
    4.23  
    4.24 @@ -1467,6 +1470,12 @@
    4.25      }
    4.26  }
    4.27  
    4.28 +void Android_JNI_AudioSetThreadPriority(int iscapture, int device_id)
    4.29 +{
    4.30 +    JNIEnv *env = Android_JNI_GetEnv();
    4.31 +    (*env)->CallStaticVoidMethod(env, mAudioManagerClass, midAudioSetThreadPriority, iscapture, device_id);
    4.32 +}
    4.33 +
    4.34  /* Test for an exception and call SDL_SetError with its detail if one occurs */
    4.35  /* If the parameter silent is truthy then SDL_SetError() will not be called. */
    4.36  static SDL_bool Android_JNI_ExceptionOccurred(SDL_bool silent)
     5.1 --- a/src/core/android/SDL_android.h	Thu Jan 10 16:04:52 2019 +0100
     5.2 +++ b/src/core/android/SDL_android.h	Thu Jan 10 18:05:56 2019 +0100
     5.3 @@ -55,6 +55,7 @@
     5.4  extern int Android_JNI_CaptureAudioBuffer(void *buffer, int buflen);
     5.5  extern void Android_JNI_FlushCapturedAudio(void);
     5.6  extern void Android_JNI_CloseAudioDevice(const int iscapture);
     5.7 +extern void Android_JNI_AudioSetThreadPriority(int iscapture, int device_id);
     5.8  
     5.9  /* Detecting device type */
    5.10  extern SDL_bool Android_IsDeXMode();