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 | |
| 13 | #include "webrtc/modules/utility/interface/jvm_android.h" |
| 14 | |
| 15 | #include "webrtc/base/checks.h" |
| 16 | |
| 17 | #define TAG "JVM" |
| 18 | #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) |
| 19 | #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | JVM* g_jvm; |
| 24 | |
| 25 | // TODO(henrika): add more clases here if needed. |
| 26 | struct { |
| 27 | const char* name; |
| 28 | jclass clazz; |
| 29 | } loaded_classes[] = { |
| 30 | {"org/webrtc/voiceengine/WebRtcAudioManager", nullptr}, |
| 31 | {"org/webrtc/voiceengine/BuildInfo", nullptr}, |
| 32 | }; |
| 33 | |
| 34 | // Android's FindClass() is trickier than usual because the app-specific |
| 35 | // ClassLoader is not consulted when there is no app-specific frame on the |
| 36 | // stack. Consequently, we only look up all classes once in native WebRTC. |
| 37 | // http://developer.android.com/training/articles/perf-jni.html#faq_FindClass |
| 38 | void LoadClasses(JNIEnv* jni) { |
| 39 | for (auto& c : loaded_classes) { |
| 40 | jclass localRef = FindClass(jni, c.name); |
| 41 | CHECK_EXCEPTION(jni) << "Error during FindClass: " << c.name; |
| 42 | CHECK(localRef) << c.name; |
| 43 | jclass globalRef = reinterpret_cast<jclass>(jni->NewGlobalRef(localRef)); |
| 44 | CHECK_EXCEPTION(jni) << "Error during NewGlobalRef: " << c.name; |
| 45 | CHECK(globalRef) << c.name; |
| 46 | c.clazz = globalRef; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void FreeClassReferences(JNIEnv* jni) { |
| 51 | for (auto& c : loaded_classes) { |
| 52 | jni->DeleteGlobalRef(c.clazz); |
| 53 | c.clazz = nullptr; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | jclass LookUpClass(const char* name) { |
| 58 | for (auto& c : loaded_classes) { |
| 59 | if (c.name == name) |
| 60 | return c.clazz; |
| 61 | } |
| 62 | CHECK(false) << "Unable to find class in lookup table"; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | // AttachCurrentThreadIfNeeded implementation. |
| 67 | AttachCurrentThreadIfNeeded::AttachCurrentThreadIfNeeded() |
| 68 | : attached_(false) { |
| 69 | ALOGD("AttachCurrentThreadIfNeeded::ctor%s", GetThreadInfo().c_str()); |
| 70 | JavaVM* jvm = JVM::GetInstance()->jvm(); |
| 71 | CHECK(jvm); |
| 72 | JNIEnv* jni = GetEnv(jvm); |
| 73 | if (!jni) { |
| 74 | ALOGD("Attaching thread to JVM"); |
| 75 | JNIEnv* env = nullptr; |
| 76 | jint ret = jvm->AttachCurrentThread(&env, nullptr); |
| 77 | attached_ = (ret == JNI_OK); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | AttachCurrentThreadIfNeeded::~AttachCurrentThreadIfNeeded() { |
| 82 | ALOGD("AttachCurrentThreadIfNeeded::dtor%s", GetThreadInfo().c_str()); |
| 83 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 | if (attached_) { |
| 85 | ALOGD("Detaching thread from JVM"); |
| 86 | jint res = JVM::GetInstance()->jvm()->DetachCurrentThread(); |
| 87 | CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // GlobalRef implementation. |
| 92 | GlobalRef::GlobalRef(JNIEnv* jni, jobject object) |
| 93 | : jni_(jni), j_object_(NewGlobalRef(jni, object)) { |
| 94 | ALOGD("GlobalRef::ctor%s", GetThreadInfo().c_str()); |
| 95 | } |
| 96 | |
| 97 | GlobalRef::~GlobalRef() { |
| 98 | ALOGD("GlobalRef::dtor%s", GetThreadInfo().c_str()); |
| 99 | DeleteGlobalRef(jni_, j_object_); |
| 100 | } |
| 101 | |
| 102 | jboolean GlobalRef::CallBooleanMethod(jmethodID methodID, ...) { |
| 103 | va_list args; |
| 104 | va_start(args, methodID); |
| 105 | jboolean res = jni_->CallBooleanMethod(j_object_, methodID, args); |
| 106 | CHECK_EXCEPTION(jni_) << "Error during CallBooleanMethod"; |
| 107 | return res; |
| 108 | } |
| 109 | |
| 110 | void GlobalRef::CallVoidMethod(jmethodID methodID, ...) { |
| 111 | va_list args; |
| 112 | va_start(args, methodID); |
| 113 | jni_->CallVoidMethod(j_object_, methodID, args); |
| 114 | CHECK_EXCEPTION(jni_) << "Error during CallVoidMethod"; |
| 115 | } |
| 116 | |
| 117 | // NativeRegistration implementation. |
| 118 | NativeRegistration::NativeRegistration(JNIEnv* jni, jclass clazz) |
| 119 | : JavaClass(jni, clazz), jni_(jni) { |
| 120 | ALOGD("NativeRegistration::ctor%s", GetThreadInfo().c_str()); |
| 121 | } |
| 122 | |
| 123 | NativeRegistration::~NativeRegistration() { |
| 124 | ALOGD("NativeRegistration::dtor%s", GetThreadInfo().c_str()); |
| 125 | jni_->UnregisterNatives(j_class_); |
| 126 | CHECK_EXCEPTION(jni_) << "Error during UnregisterNatives"; |
| 127 | } |
| 128 | |
| 129 | rtc::scoped_ptr<GlobalRef> NativeRegistration::NewObject( |
| 130 | const char* name, const char* signature, ...) { |
| 131 | ALOGD("NativeRegistration::NewObject%s", GetThreadInfo().c_str()); |
| 132 | va_list args; |
| 133 | va_start(args, signature); |
| 134 | jobject obj = jni_->NewObjectV(j_class_, |
| 135 | GetMethodID(jni_, j_class_, name, signature), |
| 136 | args); |
| 137 | CHECK_EXCEPTION(jni_) << "Error during NewObjectV"; |
| 138 | va_end(args); |
| 139 | return rtc::scoped_ptr<GlobalRef>(new GlobalRef(jni_, obj)); |
| 140 | } |
| 141 | |
| 142 | // JavaClass implementation. |
| 143 | jmethodID JavaClass::GetMethodId( |
| 144 | const char* name, const char* signature) { |
| 145 | return GetMethodID(jni_, j_class_, name, signature); |
| 146 | } |
| 147 | |
| 148 | jmethodID JavaClass::GetStaticMethodId( |
| 149 | const char* name, const char* signature) { |
| 150 | return GetStaticMethodID(jni_, j_class_, name, signature); |
| 151 | } |
| 152 | |
| 153 | jobject JavaClass::CallStaticObjectMethod(jmethodID methodID, ...) { |
| 154 | va_list args; |
| 155 | va_start(args, methodID); |
| 156 | jobject res = jni_->CallStaticObjectMethod(j_class_, methodID, args); |
| 157 | CHECK_EXCEPTION(jni_) << "Error during CallStaticObjectMethod"; |
| 158 | return res; |
| 159 | } |
| 160 | |
| 161 | // JNIEnvironment implementation. |
| 162 | JNIEnvironment::JNIEnvironment(JNIEnv* jni) : jni_(jni) { |
| 163 | ALOGD("JNIEnvironment::ctor%s", GetThreadInfo().c_str()); |
| 164 | } |
| 165 | |
| 166 | JNIEnvironment::~JNIEnvironment() { |
| 167 | ALOGD("JNIEnvironment::dtor%s", GetThreadInfo().c_str()); |
| 168 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 169 | } |
| 170 | |
| 171 | rtc::scoped_ptr<NativeRegistration> JNIEnvironment::RegisterNatives( |
| 172 | const char* name, const JNINativeMethod *methods, int num_methods) { |
| 173 | ALOGD("JNIEnvironment::RegisterNatives(%s)", name); |
| 174 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 175 | jclass clazz = LookUpClass(name); |
| 176 | jni_->RegisterNatives(clazz, methods, num_methods); |
| 177 | CHECK_EXCEPTION(jni_) << "Error during RegisterNatives"; |
| 178 | return rtc::scoped_ptr<NativeRegistration>( |
| 179 | new NativeRegistration(jni_, clazz)); |
| 180 | } |
| 181 | |
| 182 | std::string JNIEnvironment::JavaToStdString(const jstring& j_string) { |
| 183 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 184 | const char* jchars = jni_->GetStringUTFChars(j_string, nullptr); |
| 185 | CHECK_EXCEPTION(jni_); |
| 186 | const int size = jni_->GetStringUTFLength(j_string); |
| 187 | CHECK_EXCEPTION(jni_); |
| 188 | std::string ret(jchars, size); |
| 189 | jni_->ReleaseStringUTFChars(j_string, jchars); |
| 190 | CHECK_EXCEPTION(jni_); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | // static |
| 195 | void JVM::Initialize(JavaVM* jvm, jobject context) { |
| 196 | ALOGD("JVM::Initialize%s", GetThreadInfo().c_str()); |
| 197 | CHECK(!g_jvm); |
| 198 | g_jvm = new JVM(jvm, context); |
| 199 | } |
| 200 | |
| 201 | // static |
| 202 | void JVM::Uninitialize() { |
| 203 | ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str()); |
| 204 | DCHECK(g_jvm); |
| 205 | delete g_jvm; |
| 206 | g_jvm = nullptr; |
| 207 | } |
| 208 | |
| 209 | // static |
| 210 | JVM* JVM::GetInstance() { |
| 211 | DCHECK(g_jvm); |
| 212 | return g_jvm; |
| 213 | } |
| 214 | |
| 215 | JVM::JVM(JavaVM* jvm, jobject context) |
| 216 | : jvm_(jvm) { |
| 217 | ALOGD("JVM::JVM%s", GetThreadInfo().c_str()); |
| 218 | CHECK(jni()) << "AttachCurrentThread() must be called on this thread."; |
| 219 | context_ = NewGlobalRef(jni(), context); |
| 220 | LoadClasses(jni()); |
| 221 | } |
| 222 | |
| 223 | JVM::~JVM() { |
| 224 | ALOGD("JVM::~JVM%s", GetThreadInfo().c_str()); |
| 225 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 226 | FreeClassReferences(jni()); |
| 227 | DeleteGlobalRef(jni(), context_); |
| 228 | } |
| 229 | |
| 230 | rtc::scoped_ptr<JNIEnvironment> JVM::environment() { |
| 231 | ALOGD("JVM::environment%s", GetThreadInfo().c_str()); |
| 232 | // The JNIEnv is used for thread-local storage. For this reason, we cannot |
| 233 | // share a JNIEnv between threads. If a piece of code has no other way to get |
| 234 | // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the |
| 235 | // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread). |
| 236 | // See // http://developer.android.com/training/articles/perf-jni.html. |
| 237 | JNIEnv* jni = GetEnv(jvm_); |
| 238 | if (!jni) { |
| 239 | ALOGE("AttachCurrentThread() has not been called on this thread."); |
| 240 | return rtc::scoped_ptr<JNIEnvironment>(); |
| 241 | } |
| 242 | return rtc::scoped_ptr<JNIEnvironment>(new JNIEnvironment(jni)); |
| 243 | } |
| 244 | |
| 245 | JavaClass JVM::GetClass(const char* name) { |
| 246 | ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str()); |
| 247 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 248 | return JavaClass(jni(), LookUpClass(name)); |
| 249 | } |
| 250 | |
| 251 | } // namespace webrtc |