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