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