blob: 0206d15274461cf6ea8de7b4e9e13a83ca9ec676 [file] [log] [blame]
fischman@webrtc.org4e65e072013-10-03 18:23:13 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/utility/include/helpers_android.h"
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000012
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000013#include <android/log.h>
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000014#include <assert.h>
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000015#include <pthread.h>
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000016#include <stddef.h>
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000017#include <unistd.h>
18
Magnus Jedvert9185bde2017-12-28 14:12:05 +010019#include "rtc_base/checks.h"
20#include "rtc_base/platform_thread.h"
21
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000022#define TAG "HelpersAndroid"
23#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000024
25namespace webrtc {
26
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000027JNIEnv* GetEnv(JavaVM* jvm) {
28 void* env = NULL;
29 jint status = jvm->GetEnv(&env, JNI_VERSION_1_6);
henrikg91d6ede2015-09-17 00:24:34 -070030 RTC_CHECK(((env != NULL) && (status == JNI_OK)) ||
31 ((env == NULL) && (status == JNI_EDETACHED)))
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000032 << "Unexpected GetEnv return: " << status << ":" << env;
33 return reinterpret_cast<JNIEnv*>(env);
34}
35
henrikab2619892015-05-18 16:49:16 +020036// 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.
39jlong 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);
henrikg91d6ede2015-09-17 00:24:34 -070046 RTC_DCHECK(reinterpret_cast<void*>(ret) == ptr);
henrikab2619892015-05-18 16:49:16 +020047 return ret;
48}
49
Yves Gerey665174f2018-06-19 15:03:05 +020050jmethodID GetMethodID(JNIEnv* jni,
51 jclass c,
52 const char* name,
53 const char* signature) {
henrikab2619892015-05-18 16:49:16 +020054 jmethodID m = jni->GetMethodID(c, name, signature);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000055 CHECK_EXCEPTION(jni) << "Error during GetMethodID: " << name << ", "
56 << signature;
henrikg91d6ede2015-09-17 00:24:34 -070057 RTC_CHECK(m) << name << ", " << signature;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000058 return m;
59}
60
Yves Gerey665174f2018-06-19 15:03:05 +020061jmethodID GetStaticMethodID(JNIEnv* jni,
62 jclass c,
63 const char* name,
64 const char* signature) {
henrikab2619892015-05-18 16:49:16 +020065 jmethodID m = jni->GetStaticMethodID(c, name, signature);
66 CHECK_EXCEPTION(jni) << "Error during GetStaticMethodID: " << name << ", "
67 << signature;
henrikg91d6ede2015-09-17 00:24:34 -070068 RTC_CHECK(m) << name << ", " << signature;
henrikab2619892015-05-18 16:49:16 +020069 return m;
70}
71
72jclass FindClass(JNIEnv* jni, const char* name) {
73 jclass c = jni->FindClass(name);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000074 CHECK_EXCEPTION(jni) << "Error during FindClass: " << name;
henrikg91d6ede2015-09-17 00:24:34 -070075 RTC_CHECK(c) << name;
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000076 return c;
77}
78
79jobject NewGlobalRef(JNIEnv* jni, jobject o) {
80 jobject ret = jni->NewGlobalRef(o);
81 CHECK_EXCEPTION(jni) << "Error during NewGlobalRef";
henrikg91d6ede2015-09-17 00:24:34 -070082 RTC_CHECK(ret);
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000083 return ret;
84}
85
86void DeleteGlobalRef(JNIEnv* jni, jobject o) {
87 jni->DeleteGlobalRef(o);
88 CHECK_EXCEPTION(jni) << "Error during DeleteGlobalRef";
89}
90
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000091AttachThreadScoped::AttachThreadScoped(JavaVM* jvm)
92 : attached_(false), jvm_(jvm), env_(NULL) {
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000093 env_ = GetEnv(jvm);
94 if (!env_) {
95 // Adding debug log here so we can track down potential leaks and figure
96 // out why we sometimes see "Native thread exiting without having called
97 // DetachCurrentThread" in logcat outputs.
Magnus Jedvert9185bde2017-12-28 14:12:05 +010098 ALOGD("Attaching thread to JVM[tid=%d]", rtc::CurrentThreadId());
henrika@webrtc.org62f6e752015-02-11 08:38:35 +000099 jint res = jvm->AttachCurrentThread(&env_, NULL);
100 attached_ = (res == JNI_OK);
henrikg91d6ede2015-09-17 00:24:34 -0700101 RTC_CHECK(attached_) << "AttachCurrentThread failed: " << res;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +0000102 }
103}
104
105AttachThreadScoped::~AttachThreadScoped() {
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000106 if (attached_) {
Magnus Jedvert9185bde2017-12-28 14:12:05 +0100107 ALOGD("Detaching thread from JVM[tid=%d]", rtc::CurrentThreadId());
henrika@webrtc.org62f6e752015-02-11 08:38:35 +0000108 jint res = jvm_->DetachCurrentThread();
henrikg91d6ede2015-09-17 00:24:34 -0700109 RTC_CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res;
110 RTC_CHECK(!GetEnv(jvm_));
fischman@webrtc.org4e65e072013-10-03 18:23:13 +0000111 }
112}
113
Yves Gerey665174f2018-06-19 15:03:05 +0200114JNIEnv* AttachThreadScoped::env() {
115 return env_;
116}
fischman@webrtc.org4e65e072013-10-03 18:23:13 +0000117
118} // namespace webrtc