Created
May 8, 2013 16:18
-
-
Save shaobin0604/5541602 to your computer and use it in GitHub Desktop.
ao code from csipsimple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| JavaVM *android_jvm; | |
| JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) | |
| { | |
| android_jvm = vm; | |
| return JNI_VERSION_1_4; | |
| } | |
| static pj_bool_t attach_jvm(JNIEnv **jni_env) | |
| { | |
| if ((*android_jvm)->GetEnv(android_jvm, (void **)jni_env, | |
| JNI_VERSION_1_4) < 0) | |
| { | |
| if ((*android_jvm)->AttachCurrentThread(android_jvm, jni_env, NULL) < 0) | |
| { | |
| jni_env = NULL; | |
| return PJ_FALSE; | |
| } | |
| return PJ_TRUE; | |
| } | |
| return PJ_FALSE; | |
| } | |
| #define detach_jvm(attached) \ | |
| if (attached) \ | |
| (*android_jvm)->DetachCurrentThread(android_jvm); | |
| /* Thread priority utils */ | |
| /* TODO : port it to pj_thread functions */ | |
| #define THREAD_PRIORITY_AUDIO -16 | |
| #define THREAD_PRIORITY_URGENT_AUDIO -19 | |
| pj_status_t set_android_thread_priority(int priority) | |
| { | |
| jclass process_class; | |
| jmethodID set_prio_method; | |
| jthrowable exc; | |
| pj_status_t result = PJ_SUCCESS; | |
| JNIEnv *jni_env = 0; | |
| pj_bool_t attached = attach_jvm(&jni_env); | |
| PJ_ASSERT_RETURN(jni_env, PJ_FALSE); | |
| /* Get pointer to the java class */ | |
| process_class = (jclass)(*jni_env)->NewGlobalRef(jni_env, | |
| (*jni_env)->FindClass(jni_env, "android/os/Process")); | |
| if (process_class == 0) { | |
| PJ_LOG(4, (THIS_FILE, "Unable to find os process class")); | |
| result = PJ_EIGNORED; | |
| goto on_return; | |
| } | |
| /* Get the id of set thread priority function */ | |
| set_prio_method = (*jni_env)->GetStaticMethodID(jni_env, process_class, | |
| "setThreadPriority", | |
| "(I)V"); | |
| if (set_prio_method == 0) { | |
| PJ_LOG(4, (THIS_FILE, "Unable to find setThreadPriority() method")); | |
| result = PJ_EIGNORED; | |
| goto on_return; | |
| } | |
| /* Set the thread priority */ | |
| (*jni_env)->CallStaticVoidMethod(jni_env, process_class, set_prio_method, | |
| priority); | |
| exc = (*jni_env)->ExceptionOccurred(jni_env); | |
| if (exc) { | |
| (*jni_env)->ExceptionDescribe(jni_env); | |
| (*jni_env)->ExceptionClear(jni_env); | |
| PJ_LOG(4, (THIS_FILE, "Failure in setting thread priority using " | |
| "Java API, fallback to setpriority()")); | |
| setpriority(PRIO_PROCESS, 0, priority); | |
| } else { | |
| PJ_LOG(4, (THIS_FILE, "Setting thread priority successful")); | |
| } | |
| on_return: | |
| detach_jvm(attached); | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment