henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <android/log.h> |
| 12 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/utility/include/jvm_android.h" |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 18 | #include "rtc_base/platform_thread.h" |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 19 | |
| 20 | #define TAG "JVM" |
| 21 | #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) |
| 22 | #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | JVM* g_jvm; |
| 27 | |
| 28 | // TODO(henrika): add more clases here if needed. |
| 29 | struct { |
| 30 | const char* name; |
| 31 | jclass clazz; |
| 32 | } loaded_classes[] = { |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 33 | {"org/webrtc/voiceengine/BuildInfo", nullptr}, |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 34 | {"org/webrtc/voiceengine/WebRtcAudioManager", nullptr}, |
| 35 | {"org/webrtc/voiceengine/WebRtcAudioRecord", nullptr}, |
| 36 | {"org/webrtc/voiceengine/WebRtcAudioTrack", nullptr}, |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // Android's FindClass() is trickier than usual because the app-specific |
| 40 | // ClassLoader is not consulted when there is no app-specific frame on the |
| 41 | // stack. Consequently, we only look up all classes once in native WebRTC. |
| 42 | // http://developer.android.com/training/articles/perf-jni.html#faq_FindClass |
| 43 | void LoadClasses(JNIEnv* jni) { |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 44 | ALOGD("LoadClasses"); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 45 | for (auto& c : loaded_classes) { |
| 46 | jclass localRef = FindClass(jni, c.name); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 47 | ALOGD("name: %s", c.name); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 48 | CHECK_EXCEPTION(jni) << "Error during FindClass: " << c.name; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 49 | RTC_CHECK(localRef) << c.name; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 50 | jclass globalRef = reinterpret_cast<jclass>(jni->NewGlobalRef(localRef)); |
| 51 | CHECK_EXCEPTION(jni) << "Error during NewGlobalRef: " << c.name; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 52 | RTC_CHECK(globalRef) << c.name; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 53 | c.clazz = globalRef; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void FreeClassReferences(JNIEnv* jni) { |
| 58 | for (auto& c : loaded_classes) { |
| 59 | jni->DeleteGlobalRef(c.clazz); |
| 60 | c.clazz = nullptr; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | jclass LookUpClass(const char* name) { |
| 65 | for (auto& c : loaded_classes) { |
sophiechang | f935bcc | 2015-07-07 01:10:14 -0700 | [diff] [blame] | 66 | if (strcmp(c.name, name) == 0) |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 67 | return c.clazz; |
| 68 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 69 | RTC_CHECK(false) << "Unable to find class in lookup table"; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | // AttachCurrentThreadIfNeeded implementation. |
| 74 | AttachCurrentThreadIfNeeded::AttachCurrentThreadIfNeeded() |
| 75 | : attached_(false) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 76 | ALOGD("AttachCurrentThreadIfNeeded::ctor[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 77 | JavaVM* jvm = JVM::GetInstance()->jvm(); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 78 | RTC_CHECK(jvm); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 79 | JNIEnv* jni = GetEnv(jvm); |
| 80 | if (!jni) { |
| 81 | ALOGD("Attaching thread to JVM"); |
| 82 | JNIEnv* env = nullptr; |
| 83 | jint ret = jvm->AttachCurrentThread(&env, nullptr); |
| 84 | attached_ = (ret == JNI_OK); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | AttachCurrentThreadIfNeeded::~AttachCurrentThreadIfNeeded() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 89 | ALOGD("AttachCurrentThreadIfNeeded::dtor[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 90 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 91 | if (attached_) { |
| 92 | ALOGD("Detaching thread from JVM"); |
| 93 | jint res = JVM::GetInstance()->jvm()->DetachCurrentThread(); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 94 | RTC_CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | // GlobalRef implementation. |
| 99 | GlobalRef::GlobalRef(JNIEnv* jni, jobject object) |
| 100 | : jni_(jni), j_object_(NewGlobalRef(jni, object)) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 101 | ALOGD("GlobalRef::ctor[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | GlobalRef::~GlobalRef() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 105 | ALOGD("GlobalRef::dtor[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 106 | DeleteGlobalRef(jni_, j_object_); |
| 107 | } |
| 108 | |
| 109 | jboolean GlobalRef::CallBooleanMethod(jmethodID methodID, ...) { |
| 110 | va_list args; |
| 111 | va_start(args, methodID); |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 112 | jboolean res = jni_->CallBooleanMethodV(j_object_, methodID, args); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 113 | CHECK_EXCEPTION(jni_) << "Error during CallBooleanMethod"; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 114 | va_end(args); |
| 115 | return res; |
| 116 | } |
| 117 | |
| 118 | jint GlobalRef::CallIntMethod(jmethodID methodID, ...) { |
| 119 | va_list args; |
| 120 | va_start(args, methodID); |
| 121 | jint res = jni_->CallIntMethodV(j_object_, methodID, args); |
| 122 | CHECK_EXCEPTION(jni_) << "Error during CallIntMethod"; |
| 123 | va_end(args); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 124 | return res; |
| 125 | } |
| 126 | |
| 127 | void GlobalRef::CallVoidMethod(jmethodID methodID, ...) { |
| 128 | va_list args; |
| 129 | va_start(args, methodID); |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 130 | jni_->CallVoidMethodV(j_object_, methodID, args); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 131 | CHECK_EXCEPTION(jni_) << "Error during CallVoidMethod"; |
henrika | ee369e4 | 2015-05-25 10:11:27 +0200 | [diff] [blame] | 132 | va_end(args); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // NativeRegistration implementation. |
| 136 | NativeRegistration::NativeRegistration(JNIEnv* jni, jclass clazz) |
| 137 | : JavaClass(jni, clazz), jni_(jni) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 138 | ALOGD("NativeRegistration::ctor[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | NativeRegistration::~NativeRegistration() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 142 | ALOGD("NativeRegistration::dtor[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 143 | jni_->UnregisterNatives(j_class_); |
| 144 | CHECK_EXCEPTION(jni_) << "Error during UnregisterNatives"; |
| 145 | } |
| 146 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 147 | std::unique_ptr<GlobalRef> NativeRegistration::NewObject( |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 148 | const char* name, const char* signature, ...) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 149 | ALOGD("NativeRegistration::NewObject[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 150 | va_list args; |
| 151 | va_start(args, signature); |
| 152 | jobject obj = jni_->NewObjectV(j_class_, |
| 153 | GetMethodID(jni_, j_class_, name, signature), |
| 154 | args); |
| 155 | CHECK_EXCEPTION(jni_) << "Error during NewObjectV"; |
| 156 | va_end(args); |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 157 | return std::unique_ptr<GlobalRef>(new GlobalRef(jni_, obj)); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // JavaClass implementation. |
| 161 | jmethodID JavaClass::GetMethodId( |
| 162 | const char* name, const char* signature) { |
| 163 | return GetMethodID(jni_, j_class_, name, signature); |
| 164 | } |
| 165 | |
| 166 | jmethodID JavaClass::GetStaticMethodId( |
| 167 | const char* name, const char* signature) { |
| 168 | return GetStaticMethodID(jni_, j_class_, name, signature); |
| 169 | } |
| 170 | |
| 171 | jobject JavaClass::CallStaticObjectMethod(jmethodID methodID, ...) { |
| 172 | va_list args; |
| 173 | va_start(args, methodID); |
henrika | 7fc3f15 | 2017-07-11 03:52:09 -0700 | [diff] [blame] | 174 | jobject res = jni_->CallStaticObjectMethodV(j_class_, methodID, args); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 175 | CHECK_EXCEPTION(jni_) << "Error during CallStaticObjectMethod"; |
| 176 | return res; |
| 177 | } |
| 178 | |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 179 | jint JavaClass::CallStaticIntMethod(jmethodID methodID, ...) { |
| 180 | va_list args; |
| 181 | va_start(args, methodID); |
henrika | 7fc3f15 | 2017-07-11 03:52:09 -0700 | [diff] [blame] | 182 | jint res = jni_->CallStaticIntMethodV(j_class_, methodID, args); |
henrika | 918b554 | 2016-09-19 15:44:09 +0200 | [diff] [blame] | 183 | CHECK_EXCEPTION(jni_) << "Error during CallStaticIntMethod"; |
| 184 | return res; |
| 185 | } |
| 186 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 187 | // JNIEnvironment implementation. |
| 188 | JNIEnvironment::JNIEnvironment(JNIEnv* jni) : jni_(jni) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 189 | ALOGD("JNIEnvironment::ctor[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | JNIEnvironment::~JNIEnvironment() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 193 | ALOGD("JNIEnvironment::dtor[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 194 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 195 | } |
| 196 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 197 | std::unique_ptr<NativeRegistration> JNIEnvironment::RegisterNatives( |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 198 | const char* name, const JNINativeMethod *methods, int num_methods) { |
| 199 | ALOGD("JNIEnvironment::RegisterNatives(%s)", name); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 200 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 201 | jclass clazz = LookUpClass(name); |
| 202 | jni_->RegisterNatives(clazz, methods, num_methods); |
| 203 | CHECK_EXCEPTION(jni_) << "Error during RegisterNatives"; |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 204 | return std::unique_ptr<NativeRegistration>( |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 205 | new NativeRegistration(jni_, clazz)); |
| 206 | } |
| 207 | |
| 208 | std::string JNIEnvironment::JavaToStdString(const jstring& j_string) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 209 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 210 | const char* jchars = jni_->GetStringUTFChars(j_string, nullptr); |
| 211 | CHECK_EXCEPTION(jni_); |
| 212 | const int size = jni_->GetStringUTFLength(j_string); |
| 213 | CHECK_EXCEPTION(jni_); |
| 214 | std::string ret(jchars, size); |
| 215 | jni_->ReleaseStringUTFChars(j_string, jchars); |
| 216 | CHECK_EXCEPTION(jni_); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | // static |
sakal | d7fdb80 | 2017-05-26 01:51:53 -0700 | [diff] [blame] | 221 | void JVM::Initialize(JavaVM* jvm) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 222 | ALOGD("JVM::Initialize[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 223 | RTC_CHECK(!g_jvm); |
sakal | d7fdb80 | 2017-05-26 01:51:53 -0700 | [diff] [blame] | 224 | g_jvm = new JVM(jvm); |
| 225 | } |
| 226 | |
| 227 | void JVM::Initialize(JavaVM* jvm, jobject context) { |
| 228 | Initialize(jvm); |
| 229 | |
| 230 | // Pass in the context to the new ContextUtils class. |
| 231 | JNIEnv* jni = g_jvm->jni(); |
| 232 | jclass context_utils = FindClass(jni, "org/webrtc/ContextUtils"); |
| 233 | jmethodID initialize_method = jni->GetStaticMethodID( |
| 234 | context_utils, "initialize", "(Landroid/content/Context;)V"); |
| 235 | jni->CallStaticVoidMethod(context_utils, initialize_method, context); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // static |
| 239 | void JVM::Uninitialize() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 240 | ALOGD("JVM::Uninitialize[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 241 | RTC_DCHECK(g_jvm); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 242 | delete g_jvm; |
| 243 | g_jvm = nullptr; |
| 244 | } |
| 245 | |
| 246 | // static |
| 247 | JVM* JVM::GetInstance() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 248 | RTC_DCHECK(g_jvm); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 249 | return g_jvm; |
| 250 | } |
| 251 | |
sakal | d7fdb80 | 2017-05-26 01:51:53 -0700 | [diff] [blame] | 252 | JVM::JVM(JavaVM* jvm) : jvm_(jvm) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 253 | ALOGD("JVM::JVM[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 254 | RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 255 | LoadClasses(jni()); |
| 256 | } |
| 257 | |
| 258 | JVM::~JVM() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 259 | ALOGD("JVM::~JVM[tid=%d]", rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 260 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 261 | FreeClassReferences(jni()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 262 | } |
| 263 | |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 264 | std::unique_ptr<JNIEnvironment> JVM::environment() { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 265 | ALOGD("JVM::environment[tid=%d]", rtc::CurrentThreadId()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 266 | // The JNIEnv is used for thread-local storage. For this reason, we cannot |
| 267 | // share a JNIEnv between threads. If a piece of code has no other way to get |
| 268 | // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the |
| 269 | // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). |
| 270 | // See // http://developer.android.com/training/articles/perf-jni.html. |
| 271 | JNIEnv* jni = GetEnv(jvm_); |
| 272 | if (!jni) { |
| 273 | ALOGE("AttachCurrentThread() has not been called on this thread."); |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 274 | return std::unique_ptr<JNIEnvironment>(); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 275 | } |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 276 | return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni)); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | JavaClass JVM::GetClass(const char* name) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 280 | ALOGD("JVM::GetClass(%s)[tid=%d]", name, rtc::CurrentThreadId()); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 281 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 282 | return JavaClass(jni(), LookUpClass(name)); |
| 283 | } |
| 284 | |
| 285 | } // namespace webrtc |