fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/utility/include/helpers_android.h" |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 12 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 13 | #include <android/log.h> |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 14 | #include <pthread.h> |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 15 | #include <stddef.h> |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/platform_thread.h" |
| 20 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 21 | #define TAG "HelpersAndroid" |
| 22 | #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 26 | JNIEnv* GetEnv(JavaVM* jvm) { |
| 27 | void* env = NULL; |
| 28 | jint status = jvm->GetEnv(&env, JNI_VERSION_1_6); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 29 | RTC_CHECK(((env != NULL) && (status == JNI_OK)) || |
| 30 | ((env == NULL) && (status == JNI_EDETACHED))) |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 31 | << "Unexpected GetEnv return: " << status << ":" << env; |
| 32 | return reinterpret_cast<JNIEnv*>(env); |
| 33 | } |
| 34 | |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 35 | // Return a |jlong| that will correctly convert back to |ptr|. This is needed |
| 36 | // because the alternative (of silently passing a 32-bit pointer to a vararg |
| 37 | // function expecting a 64-bit param) picks up garbage in the high 32 bits. |
| 38 | jlong PointerTojlong(void* ptr) { |
| 39 | static_assert(sizeof(intptr_t) <= sizeof(jlong), |
| 40 | "Time to rethink the use of jlongs"); |
| 41 | // Going through intptr_t to be obvious about the definedness of the |
| 42 | // conversion from pointer to integral type. intptr_t to jlong is a standard |
| 43 | // widening by the static_assert above. |
| 44 | jlong ret = reinterpret_cast<intptr_t>(ptr); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 45 | RTC_DCHECK(reinterpret_cast<void*>(ret) == ptr); |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 46 | return ret; |
| 47 | } |
| 48 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | jmethodID GetMethodID(JNIEnv* jni, |
| 50 | jclass c, |
| 51 | const char* name, |
| 52 | const char* signature) { |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 53 | jmethodID m = jni->GetMethodID(c, name, signature); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 54 | CHECK_EXCEPTION(jni) << "Error during GetMethodID: " << name << ", " |
| 55 | << signature; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 56 | RTC_CHECK(m) << name << ", " << signature; |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 57 | return m; |
| 58 | } |
| 59 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 60 | jmethodID GetStaticMethodID(JNIEnv* jni, |
| 61 | jclass c, |
| 62 | const char* name, |
| 63 | const char* signature) { |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 64 | jmethodID m = jni->GetStaticMethodID(c, name, signature); |
| 65 | CHECK_EXCEPTION(jni) << "Error during GetStaticMethodID: " << name << ", " |
| 66 | << signature; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 67 | RTC_CHECK(m) << name << ", " << signature; |
henrika | b261989 | 2015-05-18 16:49:16 +0200 | [diff] [blame] | 68 | return m; |
| 69 | } |
| 70 | |
| 71 | jclass FindClass(JNIEnv* jni, const char* name) { |
| 72 | jclass c = jni->FindClass(name); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 73 | CHECK_EXCEPTION(jni) << "Error during FindClass: " << name; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 74 | RTC_CHECK(c) << name; |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 75 | return c; |
| 76 | } |
| 77 | |
| 78 | jobject NewGlobalRef(JNIEnv* jni, jobject o) { |
| 79 | jobject ret = jni->NewGlobalRef(o); |
| 80 | CHECK_EXCEPTION(jni) << "Error during NewGlobalRef"; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 81 | RTC_CHECK(ret); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | void DeleteGlobalRef(JNIEnv* jni, jobject o) { |
| 86 | jni->DeleteGlobalRef(o); |
| 87 | CHECK_EXCEPTION(jni) << "Error during DeleteGlobalRef"; |
| 88 | } |
| 89 | |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 90 | AttachThreadScoped::AttachThreadScoped(JavaVM* jvm) |
| 91 | : attached_(false), jvm_(jvm), env_(NULL) { |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 92 | env_ = GetEnv(jvm); |
| 93 | if (!env_) { |
| 94 | // Adding debug log here so we can track down potential leaks and figure |
| 95 | // out why we sometimes see "Native thread exiting without having called |
| 96 | // DetachCurrentThread" in logcat outputs. |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 97 | ALOGD("Attaching thread to JVM[tid=%d]", rtc::CurrentThreadId()); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 98 | jint res = jvm->AttachCurrentThread(&env_, NULL); |
| 99 | attached_ = (res == JNI_OK); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 100 | RTC_CHECK(attached_) << "AttachCurrentThread failed: " << res; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | AttachThreadScoped::~AttachThreadScoped() { |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 105 | if (attached_) { |
Magnus Jedvert | 9185bde | 2017-12-28 14:12:05 +0100 | [diff] [blame] | 106 | ALOGD("Detaching thread from JVM[tid=%d]", rtc::CurrentThreadId()); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 107 | jint res = jvm_->DetachCurrentThread(); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 108 | RTC_CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res; |
| 109 | RTC_CHECK(!GetEnv(jvm_)); |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 113 | JNIEnv* AttachThreadScoped::env() { |
| 114 | return env_; |
| 115 | } |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 116 | |
| 117 | } // namespace webrtc |