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.

284 lines
8.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015 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/jvm_android.h"
  11. #include <android/log.h>
  12. #include <memory>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/logging.h"
  15. #include "rtc_base/platform_thread.h"
  16. namespace webrtc {
  17. JVM* g_jvm;
  18. // TODO(henrika): add more clases here if needed.
  19. struct {
  20. const char* name;
  21. jclass clazz;
  22. } loaded_classes[] = {
  23. {"org/webrtc/voiceengine/BuildInfo", nullptr},
  24. {"org/webrtc/voiceengine/WebRtcAudioManager", nullptr},
  25. {"org/webrtc/voiceengine/WebRtcAudioRecord", nullptr},
  26. {"org/webrtc/voiceengine/WebRtcAudioTrack", nullptr},
  27. };
  28. // Android's FindClass() is trickier than usual because the app-specific
  29. // ClassLoader is not consulted when there is no app-specific frame on the
  30. // stack. Consequently, we only look up all classes once in native WebRTC.
  31. // http://developer.android.com/training/articles/perf-jni.html#faq_FindClass
  32. void LoadClasses(JNIEnv* jni) {
  33. RTC_LOG(INFO) << "LoadClasses:";
  34. for (auto& c : loaded_classes) {
  35. jclass localRef = FindClass(jni, c.name);
  36. RTC_LOG(INFO) << "name: " << c.name;
  37. CHECK_EXCEPTION(jni) << "Error during FindClass: " << c.name;
  38. RTC_CHECK(localRef) << c.name;
  39. jclass globalRef = reinterpret_cast<jclass>(jni->NewGlobalRef(localRef));
  40. CHECK_EXCEPTION(jni) << "Error during NewGlobalRef: " << c.name;
  41. RTC_CHECK(globalRef) << c.name;
  42. c.clazz = globalRef;
  43. }
  44. }
  45. void FreeClassReferences(JNIEnv* jni) {
  46. for (auto& c : loaded_classes) {
  47. jni->DeleteGlobalRef(c.clazz);
  48. c.clazz = nullptr;
  49. }
  50. }
  51. jclass LookUpClass(const char* name) {
  52. for (auto& c : loaded_classes) {
  53. if (strcmp(c.name, name) == 0)
  54. return c.clazz;
  55. }
  56. RTC_CHECK(false) << "Unable to find class in lookup table";
  57. return 0;
  58. }
  59. // JvmThreadConnector implementation.
  60. JvmThreadConnector::JvmThreadConnector() : attached_(false) {
  61. RTC_LOG(INFO) << "JvmThreadConnector::ctor";
  62. JavaVM* jvm = JVM::GetInstance()->jvm();
  63. RTC_CHECK(jvm);
  64. JNIEnv* jni = GetEnv(jvm);
  65. if (!jni) {
  66. RTC_LOG(INFO) << "Attaching thread to JVM";
  67. JNIEnv* env = nullptr;
  68. jint ret = jvm->AttachCurrentThread(&env, nullptr);
  69. attached_ = (ret == JNI_OK);
  70. }
  71. }
  72. JvmThreadConnector::~JvmThreadConnector() {
  73. RTC_LOG(INFO) << "JvmThreadConnector::dtor";
  74. RTC_DCHECK(thread_checker_.IsCurrent());
  75. if (attached_) {
  76. RTC_LOG(INFO) << "Detaching thread from JVM";
  77. jint res = JVM::GetInstance()->jvm()->DetachCurrentThread();
  78. RTC_CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res;
  79. }
  80. }
  81. // GlobalRef implementation.
  82. GlobalRef::GlobalRef(JNIEnv* jni, jobject object)
  83. : jni_(jni), j_object_(NewGlobalRef(jni, object)) {
  84. RTC_LOG(INFO) << "GlobalRef::ctor";
  85. }
  86. GlobalRef::~GlobalRef() {
  87. RTC_LOG(INFO) << "GlobalRef::dtor";
  88. DeleteGlobalRef(jni_, j_object_);
  89. }
  90. jboolean GlobalRef::CallBooleanMethod(jmethodID methodID, ...) {
  91. va_list args;
  92. va_start(args, methodID);
  93. jboolean res = jni_->CallBooleanMethodV(j_object_, methodID, args);
  94. CHECK_EXCEPTION(jni_) << "Error during CallBooleanMethod";
  95. va_end(args);
  96. return res;
  97. }
  98. jint GlobalRef::CallIntMethod(jmethodID methodID, ...) {
  99. va_list args;
  100. va_start(args, methodID);
  101. jint res = jni_->CallIntMethodV(j_object_, methodID, args);
  102. CHECK_EXCEPTION(jni_) << "Error during CallIntMethod";
  103. va_end(args);
  104. return res;
  105. }
  106. void GlobalRef::CallVoidMethod(jmethodID methodID, ...) {
  107. va_list args;
  108. va_start(args, methodID);
  109. jni_->CallVoidMethodV(j_object_, methodID, args);
  110. CHECK_EXCEPTION(jni_) << "Error during CallVoidMethod";
  111. va_end(args);
  112. }
  113. // NativeRegistration implementation.
  114. NativeRegistration::NativeRegistration(JNIEnv* jni, jclass clazz)
  115. : JavaClass(jni, clazz), jni_(jni) {
  116. RTC_LOG(INFO) << "NativeRegistration::ctor";
  117. }
  118. NativeRegistration::~NativeRegistration() {
  119. RTC_LOG(INFO) << "NativeRegistration::dtor";
  120. jni_->UnregisterNatives(j_class_);
  121. CHECK_EXCEPTION(jni_) << "Error during UnregisterNatives";
  122. }
  123. std::unique_ptr<GlobalRef> NativeRegistration::NewObject(const char* name,
  124. const char* signature,
  125. ...) {
  126. RTC_LOG(INFO) << "NativeRegistration::NewObject";
  127. va_list args;
  128. va_start(args, signature);
  129. jobject obj = jni_->NewObjectV(
  130. j_class_, GetMethodID(jni_, j_class_, name, signature), args);
  131. CHECK_EXCEPTION(jni_) << "Error during NewObjectV";
  132. va_end(args);
  133. return std::unique_ptr<GlobalRef>(new GlobalRef(jni_, obj));
  134. }
  135. // JavaClass implementation.
  136. jmethodID JavaClass::GetMethodId(const char* name, const char* signature) {
  137. return GetMethodID(jni_, j_class_, name, signature);
  138. }
  139. jmethodID JavaClass::GetStaticMethodId(const char* name,
  140. const char* signature) {
  141. return GetStaticMethodID(jni_, j_class_, name, signature);
  142. }
  143. jobject JavaClass::CallStaticObjectMethod(jmethodID methodID, ...) {
  144. va_list args;
  145. va_start(args, methodID);
  146. jobject res = jni_->CallStaticObjectMethodV(j_class_, methodID, args);
  147. CHECK_EXCEPTION(jni_) << "Error during CallStaticObjectMethod";
  148. return res;
  149. }
  150. jint JavaClass::CallStaticIntMethod(jmethodID methodID, ...) {
  151. va_list args;
  152. va_start(args, methodID);
  153. jint res = jni_->CallStaticIntMethodV(j_class_, methodID, args);
  154. CHECK_EXCEPTION(jni_) << "Error during CallStaticIntMethod";
  155. return res;
  156. }
  157. // JNIEnvironment implementation.
  158. JNIEnvironment::JNIEnvironment(JNIEnv* jni) : jni_(jni) {
  159. RTC_LOG(INFO) << "JNIEnvironment::ctor";
  160. }
  161. JNIEnvironment::~JNIEnvironment() {
  162. RTC_LOG(INFO) << "JNIEnvironment::dtor";
  163. RTC_DCHECK(thread_checker_.IsCurrent());
  164. }
  165. std::unique_ptr<NativeRegistration> JNIEnvironment::RegisterNatives(
  166. const char* name,
  167. const JNINativeMethod* methods,
  168. int num_methods) {
  169. RTC_LOG(INFO) << "JNIEnvironment::RegisterNatives: " << name;
  170. RTC_DCHECK(thread_checker_.IsCurrent());
  171. jclass clazz = LookUpClass(name);
  172. jni_->RegisterNatives(clazz, methods, num_methods);
  173. CHECK_EXCEPTION(jni_) << "Error during RegisterNatives";
  174. return std::unique_ptr<NativeRegistration>(
  175. new NativeRegistration(jni_, clazz));
  176. }
  177. std::string JNIEnvironment::JavaToStdString(const jstring& j_string) {
  178. RTC_DCHECK(thread_checker_.IsCurrent());
  179. const char* jchars = jni_->GetStringUTFChars(j_string, nullptr);
  180. CHECK_EXCEPTION(jni_);
  181. const int size = jni_->GetStringUTFLength(j_string);
  182. CHECK_EXCEPTION(jni_);
  183. std::string ret(jchars, size);
  184. jni_->ReleaseStringUTFChars(j_string, jchars);
  185. CHECK_EXCEPTION(jni_);
  186. return ret;
  187. }
  188. // static
  189. void JVM::Initialize(JavaVM* jvm) {
  190. RTC_LOG(INFO) << "JVM::Initialize";
  191. RTC_CHECK(!g_jvm);
  192. g_jvm = new JVM(jvm);
  193. }
  194. void JVM::Initialize(JavaVM* jvm, jobject context) {
  195. Initialize(jvm);
  196. // Pass in the context to the new ContextUtils class.
  197. JNIEnv* jni = g_jvm->jni();
  198. jclass context_utils = FindClass(jni, "org/webrtc/ContextUtils");
  199. jmethodID initialize_method = jni->GetStaticMethodID(
  200. context_utils, "initialize", "(Landroid/content/Context;)V");
  201. jni->CallStaticVoidMethod(context_utils, initialize_method, context);
  202. }
  203. // static
  204. void JVM::Uninitialize() {
  205. RTC_LOG(INFO) << "JVM::Uninitialize";
  206. RTC_DCHECK(g_jvm);
  207. delete g_jvm;
  208. g_jvm = nullptr;
  209. }
  210. // static
  211. JVM* JVM::GetInstance() {
  212. RTC_DCHECK(g_jvm);
  213. return g_jvm;
  214. }
  215. JVM::JVM(JavaVM* jvm) : jvm_(jvm) {
  216. RTC_LOG(INFO) << "JVM::JVM";
  217. RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread.";
  218. LoadClasses(jni());
  219. }
  220. JVM::~JVM() {
  221. RTC_LOG(INFO) << "JVM::~JVM";
  222. RTC_DCHECK(thread_checker_.IsCurrent());
  223. FreeClassReferences(jni());
  224. }
  225. std::unique_ptr<JNIEnvironment> JVM::environment() {
  226. RTC_LOG(INFO) << "JVM::environment";
  227. ;
  228. // The JNIEnv is used for thread-local storage. For this reason, we cannot
  229. // share a JNIEnv between threads. If a piece of code has no other way to get
  230. // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the
  231. // thread's JNIEnv. (Assuming it has one, if not, use AttachCurrentThread).
  232. // See // http://developer.android.com/training/articles/perf-jni.html.
  233. JNIEnv* jni = GetEnv(jvm_);
  234. if (!jni) {
  235. RTC_LOG(LS_ERROR)
  236. << "AttachCurrentThread() has not been called on this thread";
  237. return std::unique_ptr<JNIEnvironment>();
  238. }
  239. return std::unique_ptr<JNIEnvironment>(new JNIEnvironment(jni));
  240. }
  241. JavaClass JVM::GetClass(const char* name) {
  242. RTC_LOG(INFO) << "JVM::GetClass: " << name;
  243. RTC_DCHECK(thread_checker_.IsCurrent());
  244. return JavaClass(jni(), LookUpClass(name));
  245. }
  246. } // namespace webrtc