You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.9 KiB

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