ijksdl_android_jni.c 2.5 KB
Newer Older
Z
Zhang Rui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*****************************************************************************
 * ijksdl_android.c
 *****************************************************************************
 *
 * copyright (c) 2013 Zhang Rui <bbcallen@gmail.com>
 *
 * 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"

Z
Zhang Rui 已提交
26 27
#include "ijksdl_inc_internal_android.h"
#include "android_audiotrack.h"
Z
Zhang Rui 已提交
28

29 30 31 32
static JavaVM *g_jvm;

static pthread_key_t g_thread_key;
static pthread_once_t g_key_once = PTHREAD_ONCE_INIT;
Z
Zhang Rui 已提交
33 34 35 36 37 38

JavaVM *SDL_AndroidJni_GetJvm()
{
    return g_jvm;
}

39
static void SDL_AndroidJni_ThreadDestroyed(void* value)
Z
Zhang Rui 已提交
40
{
41 42 43 44
    JNIEnv *env = (JNIEnv*) value;
    if (env != NULL) {
        (*g_jvm)->DetachCurrentThread(g_jvm);
        pthread_setspecific(g_thread_key, NULL);
Z
Zhang Rui 已提交
45
    }
46
}
Z
Zhang Rui 已提交
47

48 49 50
static void make_thread_key()
{
    pthread_key_create(&g_thread_key, SDL_AndroidJni_ThreadDestroyed);
Z
Zhang Rui 已提交
51 52
}

53
jint SDL_AndroidJni_SetupThreadEnv(JNIEnv **p_env)
Z
Zhang Rui 已提交
54 55 56 57 58 59 60
{
    JavaVM *jvm = g_jvm;
    if (!jvm) {
        ALOGE("SDL_AndroidJni_GetJvm: AttachCurrentThread: NULL jvm");
        return -1;
    }

61 62 63
    pthread_once(&g_key_once, make_thread_key);

    JNIEnv *env = (JNIEnv*) pthread_getspecific(g_thread_key);
64 65 66 67 68 69 70 71 72
    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;
73 74 75
    }

    return -1;
Z
Zhang Rui 已提交
76 77
}

Z
Zhang Rui 已提交
78
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
Z
Zhang Rui 已提交
79
{
Z
Zhang Rui 已提交
80
    int retval;
Z
Zhang Rui 已提交
81 82 83 84 85 86 87
    JNIEnv* env = NULL;

    g_jvm = vm;
    if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        return -1;
    }

Z
Zhang Rui 已提交
88 89 90
    retval = sdl_audiotrack_global_init(env);
    JNI_CHECK_RET(retval == 0, env, NULL, NULL, -1);

Z
Zhang Rui 已提交
91 92 93
    return JNI_VERSION_1_4;
}

Z
Zhang Rui 已提交
94
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved)
Z
Zhang Rui 已提交
95 96
{
}