summaryrefslogtreecommitdiff
path: root/media/libmediaplayer2/JavaVMHelper.cpp
blob: 8d03ed0a10002248f1cfb1a0c9b896d4d355bc12 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "JavaVMHelper"

#include "mediaplayer2/JavaVMHelper.h"

#include <media/stagefright/foundation/ADebug.h>
#include <utils/threads.h>

#include <stdlib.h>

namespace android {

// static
std::atomic<JavaVM *> JavaVMHelper::sJavaVM(NULL);

/*
 * Makes the current thread visible to the VM.
 *
 * The JNIEnv pointer returned is only valid for the current thread, and
 * thus must be tucked into thread-local storage.
 */
static int javaAttachThread(const char* threadName, JNIEnv** pEnv) {
    JavaVMAttachArgs args;
    JavaVM* vm;
    jint result;

    vm = JavaVMHelper::getJavaVM();
    if (vm == NULL) {
        return JNI_ERR;
    }

    args.version = JNI_VERSION_1_4;
    args.name = (char*) threadName;
    args.group = NULL;

    result = vm->AttachCurrentThread(pEnv, (void*) &args);
    if (result != JNI_OK) {
        ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
    }

    return result;
}

/*
 * Detach the current thread from the set visible to the VM.
 */
static int javaDetachThread(void) {
    JavaVM* vm;
    jint result;

    vm = JavaVMHelper::getJavaVM();
    if (vm == NULL) {
        return JNI_ERR;
    }

    result = vm->DetachCurrentThread();
    if (result != JNI_OK) {
        ALOGE("ERROR: thread detach failed\n");
    }
    return result;
}

/*
 * When starting a native thread that will be visible from the VM, we
 * bounce through this to get the right attach/detach action.
 * Note that this function calls free(args)
 */
static int javaThreadShell(void* args) {
    void* start = ((void**)args)[0];
    void* userData = ((void **)args)[1];
    char* name = (char*) ((void **)args)[2];        // we own this storage
    free(args);
    JNIEnv* env;
    int result;

    /* hook us into the VM */
    if (javaAttachThread(name, &env) != JNI_OK) {
        return -1;
    }

    /* start the thread running */
    result = (*(android_thread_func_t)start)(userData);

    /* unhook us */
    javaDetachThread();
    free(name);

    return result;
}

/*
 * This is invoked from androidCreateThreadEtc() via the callback
 * set with androidSetCreateThreadFunc().
 *
 * We need to create the new thread in such a way that it gets hooked
 * into the VM before it really starts executing.
 */
static int javaCreateThreadEtc(
        android_thread_func_t entryFunction,
        void* userData,
        const char* threadName,
        int32_t threadPriority,
        size_t threadStackSize,
        android_thread_id_t* threadId) {
    void** args = (void**) malloc(3 * sizeof(void*));   // javaThreadShell must free
    int result;

    LOG_ALWAYS_FATAL_IF(threadName == nullptr, "threadName not provided to javaCreateThreadEtc");

    args[0] = (void*) entryFunction;
    args[1] = userData;
    args[2] = (void*) strdup(threadName);   // javaThreadShell must free

    result = androidCreateRawThreadEtc(javaThreadShell, args,
        threadName, threadPriority, threadStackSize, threadId);
    return result;
}

// static
JNIEnv *JavaVMHelper::getJNIEnv() {
    JNIEnv *env;
    JavaVM *vm = sJavaVM.load();
    CHECK(vm != NULL);

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

    return env;
}

//static
JavaVM *JavaVMHelper::getJavaVM() {
    return sJavaVM.load();
}

// static
void JavaVMHelper::setJavaVM(JavaVM *vm) {
    sJavaVM.store(vm);

    // Ensure that Thread(/*canCallJava*/ true) in libutils is attached to the VM.
    // This is supposed to be done by runtime, but when libutils is used with linker
    // namespace, CreateThreadFunc should be initialized separately within the namespace.
    androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
}

}  // namespace android