/***************************************************************************** * ijksdl_android.c ***************************************************************************** * * copyright (c) 2013 Zhang Rui * * This file is part of ijkPlayer. * * ijkPlayer is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * ijkPlayer is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with ijkPlayer; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "ijksdl_android_jni.h" #include "ijksdl_inc_internal_android.h" #include "android_audiotrack.h" static JavaVM *g_jvm; static pthread_key_t g_thread_key; static pthread_once_t g_key_once = PTHREAD_ONCE_INIT; JavaVM *SDL_AndroidJni_GetJvm() { return g_jvm; } static void SDL_AndroidJni_ThreadDestroyed(void* value) { JNIEnv *env = (JNIEnv*) value; if (env != NULL) { (*g_jvm)->DetachCurrentThread(g_jvm); pthread_setspecific(g_thread_key, NULL); } } static void make_thread_key() { pthread_key_create(&g_thread_key, SDL_AndroidJni_ThreadDestroyed); } jint SDL_AndroidJni_SetupThreadEnv(JNIEnv **p_env) { JavaVM *jvm = g_jvm; if (!jvm) { ALOGE("SDL_AndroidJni_GetJvm: AttachCurrentThread: NULL jvm"); return -1; } pthread_once(&g_key_once, make_thread_key); JNIEnv *env = (JNIEnv*) pthread_getspecific(g_thread_key); if (env) { *p_env = env; return 0; } if ((*jvm)->AttachCurrentThread(jvm, &env, NULL) == JNI_OK) { pthread_setspecific(g_thread_key, env); *p_env = env; return 0; } return -1; } JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { int retval; JNIEnv* env = NULL; g_jvm = vm; if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) { return -1; } retval = sdl_audiotrack_global_init(env); JNI_CHECK_RET(retval == 0, env, NULL, NULL, -1); return JNI_VERSION_1_4; } JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved) { }