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 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 11 | #include "webrtc/base/checks.h" |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 12 | #include "webrtc/modules/utility/interface/helpers_android.h" |
| 13 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 14 | #include <android/log.h> |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 15 | #include <assert.h> |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 16 | #include <pthread.h> |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 17 | #include <stddef.h> |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
| 20 | #define TAG "HelpersAndroid" |
| 21 | #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 25 | JNIEnv* GetEnv(JavaVM* jvm) { |
| 26 | void* env = NULL; |
| 27 | jint status = jvm->GetEnv(&env, JNI_VERSION_1_6); |
| 28 | CHECK(((env != NULL) && (status == JNI_OK)) || |
| 29 | ((env == NULL) && (status == JNI_EDETACHED))) |
| 30 | << "Unexpected GetEnv return: " << status << ":" << env; |
| 31 | return reinterpret_cast<JNIEnv*>(env); |
| 32 | } |
| 33 | |
| 34 | jmethodID GetMethodID ( |
| 35 | JNIEnv* jni, jclass c, const std::string& name, const char* signature) { |
| 36 | jmethodID m = jni->GetMethodID(c, name.c_str(), signature); |
| 37 | CHECK_EXCEPTION(jni) << "Error during GetMethodID: " << name << ", " |
| 38 | << signature; |
| 39 | CHECK(m) << name << ", " << signature; |
| 40 | return m; |
| 41 | } |
| 42 | |
| 43 | jclass FindClass(JNIEnv* jni, const std::string& name) { |
| 44 | jclass c = jni->FindClass(name.c_str()); |
| 45 | CHECK_EXCEPTION(jni) << "Error during FindClass: " << name; |
| 46 | CHECK(c) << name; |
| 47 | return c; |
| 48 | } |
| 49 | |
| 50 | jobject NewGlobalRef(JNIEnv* jni, jobject o) { |
| 51 | jobject ret = jni->NewGlobalRef(o); |
| 52 | CHECK_EXCEPTION(jni) << "Error during NewGlobalRef"; |
| 53 | CHECK(ret); |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | void DeleteGlobalRef(JNIEnv* jni, jobject o) { |
| 58 | jni->DeleteGlobalRef(o); |
| 59 | CHECK_EXCEPTION(jni) << "Error during DeleteGlobalRef"; |
| 60 | } |
| 61 | |
| 62 | std::string GetThreadId() { |
| 63 | char buf[21]; // Big enough to hold a kuint64max plus terminating NULL. |
| 64 | int thread_id = gettid(); |
| 65 | CHECK_LT(snprintf(buf, sizeof(buf), "%i", thread_id), |
| 66 | static_cast<int>(sizeof(buf))) << "Thread id is bigger than uint64??"; |
| 67 | return std::string(buf); |
| 68 | } |
| 69 | |
| 70 | std::string GetThreadInfo() { |
| 71 | return "@[tid=" + GetThreadId() + "]"; |
| 72 | } |
| 73 | |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 74 | AttachThreadScoped::AttachThreadScoped(JavaVM* jvm) |
| 75 | : attached_(false), jvm_(jvm), env_(NULL) { |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 76 | env_ = GetEnv(jvm); |
| 77 | if (!env_) { |
| 78 | // Adding debug log here so we can track down potential leaks and figure |
| 79 | // out why we sometimes see "Native thread exiting without having called |
| 80 | // DetachCurrentThread" in logcat outputs. |
| 81 | ALOGD("Attaching thread to JVM%s", GetThreadInfo().c_str()); |
| 82 | jint res = jvm->AttachCurrentThread(&env_, NULL); |
| 83 | attached_ = (res == JNI_OK); |
| 84 | CHECK(attached_) << "AttachCurrentThread failed: " << res; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | AttachThreadScoped::~AttachThreadScoped() { |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 89 | if (attached_) { |
| 90 | ALOGD("Detaching thread from JVM%s", GetThreadInfo().c_str()); |
| 91 | jint res = jvm_->DetachCurrentThread(); |
| 92 | CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res; |
| 93 | CHECK(!GetEnv(jvm_)); |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | JNIEnv* AttachThreadScoped::env() { return env_; } |
| 98 | |
| 99 | } // namespace webrtc |