blob: e0c66d5fe1b39aa3331f493cd127463538704732 [file] [log] [blame]
henrikab2619892015-05-18 16:49:16 +02001/*
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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/utility/include/jvm_android.h"
12
henrikab2619892015-05-18 16:49:16 +020013#include <android/log.h>
14
kwiberg84be5112016-04-27 01:19:58 -070015#include <memory>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
henrika53e048d2018-01-12 14:35:07 +010018#include "rtc_base/logging.h"
Magnus Jedvert9185bde2017-12-28 14:12:05 +010019#include "rtc_base/platform_thread.h"
henrikab2619892015-05-18 16:49:16 +020020
henrikab2619892015-05-18 16:49:16 +020021namespace webrtc {
22
23JVM* g_jvm;
24
25// TODO(henrika): add more clases here if needed.
26struct {
27 const char* name;
28 jclass clazz;
29} loaded_classes[] = {
henrikab2619892015-05-18 16:49:16 +020030};
31
32// Android's FindClass() is trickier than usual because the app-specific
33// ClassLoader is not consulted when there is no app-specific frame on the
34// stack. Consequently, we only look up all classes once in native WebRTC.
35// http://developer.android.com/training/articles/perf-jni.html#faq_FindClass
36void LoadClasses(JNIEnv* jni) {
Harald Alvestrand97597c02021-11-04 12:01:23 +000037 RTC_LOG(LS_INFO) << "LoadClasses:";
henrikab2619892015-05-18 16:49:16 +020038 for (auto& c : loaded_classes) {
39 jclass localRef = FindClass(jni, c.name);
Harald Alvestrand97597c02021-11-04 12:01:23 +000040 RTC_LOG(LS_INFO) << "name: " << c.name;
henrikab2619892015-05-18 16:49:16 +020041 CHECK_EXCEPTION(jni) << "Error during FindClass: " << c.name;
henrikg91d6ede2015-09-17 00:24:34 -070042 RTC_CHECK(localRef) << c.name;
henrikab2619892015-05-18 16:49:16 +020043 jclass globalRef = reinterpret_cast<jclass>(jni->NewGlobalRef(localRef));
44 CHECK_EXCEPTION(jni) << "Error during NewGlobalRef: " << c.name;
henrikg91d6ede2015-09-17 00:24:34 -070045 RTC_CHECK(globalRef) << c.name;
henrikab2619892015-05-18 16:49:16 +020046 c.clazz = globalRef;
47 }
48}
49
50void FreeClassReferences(JNIEnv* jni) {
51 for (auto& c : loaded_classes) {
52 jni->DeleteGlobalRef(c.clazz);
53 c.clazz = nullptr;
54 }
55}
56
57jclass LookUpClass(const char* name) {
58 for (auto& c : loaded_classes) {
sophiechangf935bcc2015-07-07 01:10:14 -070059 if (strcmp(c.name, name) == 0)
henrikab2619892015-05-18 16:49:16 +020060 return c.clazz;
61 }
henrikg91d6ede2015-09-17 00:24:34 -070062 RTC_CHECK(false) << "Unable to find class in lookup table";
henrikab2619892015-05-18 16:49:16 +020063 return 0;
64}
65
Mirko Bonadei977c8202019-01-11 19:26:40 +010066// JvmThreadConnector implementation.
67JvmThreadConnector::JvmThreadConnector() : attached_(false) {
Harald Alvestrand97597c02021-11-04 12:01:23 +000068 RTC_LOG(LS_INFO) << "JvmThreadConnector::ctor";
henrikab2619892015-05-18 16:49:16 +020069 JavaVM* jvm = JVM::GetInstance()->jvm();
henrikg91d6ede2015-09-17 00:24:34 -070070 RTC_CHECK(jvm);
henrikab2619892015-05-18 16:49:16 +020071 JNIEnv* jni = GetEnv(jvm);
72 if (!jni) {
Harald Alvestrand97597c02021-11-04 12:01:23 +000073 RTC_LOG(LS_INFO) << "Attaching thread to JVM";
henrikab2619892015-05-18 16:49:16 +020074 JNIEnv* env = nullptr;
75 jint ret = jvm->AttachCurrentThread(&env, nullptr);
76 attached_ = (ret == JNI_OK);
77 }
78}
79
Mirko Bonadei977c8202019-01-11 19:26:40 +010080JvmThreadConnector::~JvmThreadConnector() {
Harald Alvestrand97597c02021-11-04 12:01:23 +000081 RTC_LOG(LS_INFO) << "JvmThreadConnector::dtor";
Sebastian Janssonc01367d2019-04-08 15:20:44 +020082 RTC_DCHECK(thread_checker_.IsCurrent());
henrikab2619892015-05-18 16:49:16 +020083 if (attached_) {
Harald Alvestrand97597c02021-11-04 12:01:23 +000084 RTC_LOG(LS_INFO) << "Detaching thread from JVM";
henrikab2619892015-05-18 16:49:16 +020085 jint res = JVM::GetInstance()->jvm()->DetachCurrentThread();
henrikg91d6ede2015-09-17 00:24:34 -070086 RTC_CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res;
henrikab2619892015-05-18 16:49:16 +020087 }
88}
89
90// GlobalRef implementation.
91GlobalRef::GlobalRef(JNIEnv* jni, jobject object)
92 : jni_(jni), j_object_(NewGlobalRef(jni, object)) {
Harald Alvestrand97597c02021-11-04 12:01:23 +000093 RTC_LOG(LS_INFO) << "GlobalRef::ctor";
henrikab2619892015-05-18 16:49:16 +020094}
95
96GlobalRef::~GlobalRef() {
Harald Alvestrand97597c02021-11-04 12:01:23 +000097 RTC_LOG(LS_INFO) << "GlobalRef::dtor";
henrikab2619892015-05-18 16:49:16 +020098 DeleteGlobalRef(jni_, j_object_);
99}
100
101jboolean GlobalRef::CallBooleanMethod(jmethodID methodID, ...) {
102 va_list args;
103 va_start(args, methodID);
henrikaee369e42015-05-25 10:11:27 +0200104 jboolean res = jni_->CallBooleanMethodV(j_object_, methodID, args);
henrikab2619892015-05-18 16:49:16 +0200105 CHECK_EXCEPTION(jni_) << "Error during CallBooleanMethod";
henrikaee369e42015-05-25 10:11:27 +0200106 va_end(args);
107 return res;
108}
109
110jint GlobalRef::CallIntMethod(jmethodID methodID, ...) {
111 va_list args;
112 va_start(args, methodID);
113 jint res = jni_->CallIntMethodV(j_object_, methodID, args);
114 CHECK_EXCEPTION(jni_) << "Error during CallIntMethod";
115 va_end(args);
henrikab2619892015-05-18 16:49:16 +0200116 return res;
117}
118
119void GlobalRef::CallVoidMethod(jmethodID methodID, ...) {
120 va_list args;
121 va_start(args, methodID);
henrikaee369e42015-05-25 10:11:27 +0200122 jni_->CallVoidMethodV(j_object_, methodID, args);
henrikab2619892015-05-18 16:49:16 +0200123 CHECK_EXCEPTION(jni_) << "Error during CallVoidMethod";
henrikaee369e42015-05-25 10:11:27 +0200124 va_end(args);
henrikab2619892015-05-18 16:49:16 +0200125}
126
127// NativeRegistration implementation.
128NativeRegistration::NativeRegistration(JNIEnv* jni, jclass clazz)
129 : JavaClass(jni, clazz), jni_(jni) {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000130 RTC_LOG(LS_INFO) << "NativeRegistration::ctor";
henrikab2619892015-05-18 16:49:16 +0200131}
132
133NativeRegistration::~NativeRegistration() {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000134 RTC_LOG(LS_INFO) << "NativeRegistration::dtor";
henrikab2619892015-05-18 16:49:16 +0200135 jni_->UnregisterNatives(j_class_);
136 CHECK_EXCEPTION(jni_) << "Error during UnregisterNatives";
137}
138
Yves Gerey665174f2018-06-19 15:03:05 +0200139std::unique_ptr<GlobalRef> NativeRegistration::NewObject(const char* name,
140 const char* signature,
141 ...) {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000142 RTC_LOG(LS_INFO) << "NativeRegistration::NewObject";
henrikab2619892015-05-18 16:49:16 +0200143 va_list args;
144 va_start(args, signature);
Yves Gerey665174f2018-06-19 15:03:05 +0200145 jobject obj = jni_->NewObjectV(
146 j_class_, GetMethodID(jni_, j_class_, name, signature), args);
henrikab2619892015-05-18 16:49:16 +0200147 CHECK_EXCEPTION(jni_) << "Error during NewObjectV";
148 va_end(args);
kwiberg84be5112016-04-27 01:19:58 -0700149 return std::unique_ptr<GlobalRef>(new GlobalRef(jni_, obj));
henrikab2619892015-05-18 16:49:16 +0200150}
151
152// JavaClass implementation.
Yves Gerey665174f2018-06-19 15:03:05 +0200153jmethodID JavaClass::GetMethodId(const char* name, const char* signature) {
henrikab2619892015-05-18 16:49:16 +0200154 return GetMethodID(jni_, j_class_, name, signature);
155}
156
Yves Gerey665174f2018-06-19 15:03:05 +0200157jmethodID JavaClass::GetStaticMethodId(const char* name,
158 const char* signature) {
henrikab2619892015-05-18 16:49:16 +0200159 return GetStaticMethodID(jni_, j_class_, name, signature);
160}
161
162jobject JavaClass::CallStaticObjectMethod(jmethodID methodID, ...) {
163 va_list args;
164 va_start(args, methodID);
henrika7fc3f152017-07-11 03:52:09 -0700165 jobject res = jni_->CallStaticObjectMethodV(j_class_, methodID, args);
henrikab2619892015-05-18 16:49:16 +0200166 CHECK_EXCEPTION(jni_) << "Error during CallStaticObjectMethod";
167 return res;
168}
169
henrika918b5542016-09-19 15:44:09 +0200170jint JavaClass::CallStaticIntMethod(jmethodID methodID, ...) {
171 va_list args;
172 va_start(args, methodID);
henrika7fc3f152017-07-11 03:52:09 -0700173 jint res = jni_->CallStaticIntMethodV(j_class_, methodID, args);
henrika918b5542016-09-19 15:44:09 +0200174 CHECK_EXCEPTION(jni_) << "Error during CallStaticIntMethod";
175 return res;
176}
177
henrikab2619892015-05-18 16:49:16 +0200178// JNIEnvironment implementation.
179JNIEnvironment::JNIEnvironment(JNIEnv* jni) : jni_(jni) {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000180 RTC_LOG(LS_INFO) << "JNIEnvironment::ctor";
henrikab2619892015-05-18 16:49:16 +0200181}
182
183JNIEnvironment::~JNIEnvironment() {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000184 RTC_LOG(LS_INFO) << "JNIEnvironment::dtor";
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200185 RTC_DCHECK(thread_checker_.IsCurrent());
henrikab2619892015-05-18 16:49:16 +0200186}
187
kwiberg84be5112016-04-27 01:19:58 -0700188std::unique_ptr<NativeRegistration> JNIEnvironment::RegisterNatives(
Yves Gerey665174f2018-06-19 15:03:05 +0200189 const char* name,
190 const JNINativeMethod* methods,
191 int num_methods) {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000192 RTC_LOG(LS_INFO) << "JNIEnvironment::RegisterNatives: " << name;
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200193 RTC_DCHECK(thread_checker_.IsCurrent());
henrikab2619892015-05-18 16:49:16 +0200194 jclass clazz = LookUpClass(name);
195 jni_->RegisterNatives(clazz, methods, num_methods);
196 CHECK_EXCEPTION(jni_) << "Error during RegisterNatives";
kwiberg84be5112016-04-27 01:19:58 -0700197 return std::unique_ptr<NativeRegistration>(
henrikab2619892015-05-18 16:49:16 +0200198 new NativeRegistration(jni_, clazz));
199}
200
201std::string JNIEnvironment::JavaToStdString(const jstring& j_string) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200202 RTC_DCHECK(thread_checker_.IsCurrent());
henrikab2619892015-05-18 16:49:16 +0200203 const char* jchars = jni_->GetStringUTFChars(j_string, nullptr);
204 CHECK_EXCEPTION(jni_);
205 const int size = jni_->GetStringUTFLength(j_string);
206 CHECK_EXCEPTION(jni_);
207 std::string ret(jchars, size);
208 jni_->ReleaseStringUTFChars(j_string, jchars);
209 CHECK_EXCEPTION(jni_);
210 return ret;
211}
212
213// static
sakald7fdb802017-05-26 01:51:53 -0700214void JVM::Initialize(JavaVM* jvm) {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000215 RTC_LOG(LS_INFO) << "JVM::Initialize";
henrikg91d6ede2015-09-17 00:24:34 -0700216 RTC_CHECK(!g_jvm);
sakald7fdb802017-05-26 01:51:53 -0700217 g_jvm = new JVM(jvm);
218}
219
220void JVM::Initialize(JavaVM* jvm, jobject context) {
221 Initialize(jvm);
222
223 // Pass in the context to the new ContextUtils class.
224 JNIEnv* jni = g_jvm->jni();
225 jclass context_utils = FindClass(jni, "org/webrtc/ContextUtils");
226 jmethodID initialize_method = jni->GetStaticMethodID(
227 context_utils, "initialize", "(Landroid/content/Context;)V");
228 jni->CallStaticVoidMethod(context_utils, initialize_method, context);
henrikab2619892015-05-18 16:49:16 +0200229}
230
231// static
232void JVM::Uninitialize() {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000233 RTC_LOG(LS_INFO) << "JVM::Uninitialize";
henrikg91d6ede2015-09-17 00:24:34 -0700234 RTC_DCHECK(g_jvm);
henrikab2619892015-05-18 16:49:16 +0200235 delete g_jvm;
236 g_jvm = nullptr;
237}
238
239// static
240JVM* JVM::GetInstance() {
henrikg91d6ede2015-09-17 00:24:34 -0700241 RTC_DCHECK(g_jvm);
henrikab2619892015-05-18 16:49:16 +0200242 return g_jvm;
243}
244
sakald7fdb802017-05-26 01:51:53 -0700245JVM::JVM(JavaVM* jvm) : jvm_(jvm) {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000246 RTC_LOG(LS_INFO) << "JVM::JVM";
henrikg91d6ede2015-09-17 00:24:34 -0700247 RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread.";
henrikab2619892015-05-18 16:49:16 +0200248 LoadClasses(jni());
249}
250
251JVM::~JVM() {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000252 RTC_LOG(LS_INFO) << "JVM::~JVM";
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200253 RTC_DCHECK(thread_checker_.IsCurrent());
henrikab2619892015-05-18 16:49:16 +0200254 FreeClassReferences(jni());
henrikab2619892015-05-18 16:49:16 +0200255}
256
kwiberg84be5112016-04-27 01:19:58 -0700257std::unique_ptr<JNIEnvironment> JVM::environment() {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000258 RTC_LOG(LS_INFO) << "JVM::environment";
henrika53e048d2018-01-12 14:35:07 +0100259 ;
henrikab2619892015-05-18 16:49:16 +0200260 // The JNIEnv is used for thread-local storage. For this reason, we cannot
261 // share a JNIEnv between threads. If a piece of code has no other way to get
262 // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the
263 // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread).
264 // See // http://developer.android.com/training/articles/perf-jni.html.
265 JNIEnv* jni = GetEnv(jvm_);
266 if (!jni) {
henrika53e048d2018-01-12 14:35:07 +0100267 RTC_LOG(LS_ERROR)
268 << "AttachCurrentThread() has not been called on this thread";
kwiberg84be5112016-04-27 01:19:58 -0700269 return std::unique_ptr<JNIEnvironment>();
henrikab2619892015-05-18 16:49:16 +0200270 }
kwiberg84be5112016-04-27 01:19:58 -0700271 return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni));
henrikab2619892015-05-18 16:49:16 +0200272}
273
274JavaClass JVM::GetClass(const char* name) {
Harald Alvestrand97597c02021-11-04 12:01:23 +0000275 RTC_LOG(LS_INFO) << "JVM::GetClass: " << name;
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200276 RTC_DCHECK(thread_checker_.IsCurrent());
henrikab2619892015-05-18 16:49:16 +0200277 return JavaClass(jni(), LookUpClass(name));
278}
279
280} // namespace webrtc