From ef3dbad49a2c46d4779f2c5a845ca49f69996a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Fri, 27 Sep 2019 14:04:56 +0200 Subject: [PATCH] New class ScopedJavaRefCounted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Intended to be used for holding on to references to the java EncodedImage and call its release method when no longer used by C++. Bug: webrtc:9378 Change-Id: I40d917c2bb4217419ef2d609e517566c8466a274 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/154740 Reviewed-by: Sami Kalliomäki Commit-Queue: Niels Moller Cr-Commit-Position: refs/heads/master@{#29347} --- sdk/android/BUILD.gn | 2 + sdk/android/api/org/webrtc/RefCounted.java | 2 +- sdk/android/src/jni/scoped_java_ref_counted.h | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 sdk/android/src/jni/scoped_java_ref_counted.h diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn index fec3ff1820..55091a27ff 100644 --- a/sdk/android/BUILD.gn +++ b/sdk/android/BUILD.gn @@ -544,6 +544,7 @@ if (current_os == "linux" || is_android) { "src/jni/pc/audio.h", "src/jni/pc/logging.cc", "src/jni/pc/video.h", + "src/jni/scoped_java_ref_counted.h", ] deps = [ @@ -1199,6 +1200,7 @@ if (current_os == "linux" || is_android) { sources = [ "api/org/webrtc/NetworkMonitor.java", "api/org/webrtc/NetworkMonitorAutoDetect.java", + "api/org/webrtc/RefCounted.java", "src/java/org/webrtc/Histogram.java", "src/java/org/webrtc/JniCommon.java", ] diff --git a/sdk/android/api/org/webrtc/RefCounted.java b/sdk/android/api/org/webrtc/RefCounted.java index 7741a445d9..f854f70b20 100644 --- a/sdk/android/api/org/webrtc/RefCounted.java +++ b/sdk/android/api/org/webrtc/RefCounted.java @@ -24,5 +24,5 @@ public interface RefCounted { * Decreases ref count by one. When the ref count reaches zero, resources related to the object * will be freed. */ - void release(); + @CalledByNative void release(); } diff --git a/sdk/android/src/jni/scoped_java_ref_counted.h b/sdk/android/src/jni/scoped_java_ref_counted.h new file mode 100644 index 0000000000..33cc6eba7d --- /dev/null +++ b/sdk/android/src/jni/scoped_java_ref_counted.h @@ -0,0 +1,54 @@ +/* + * Copyright 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#ifndef SDK_ANDROID_SRC_JNI_SCOPED_JAVA_REF_COUNTED_H_ +#define SDK_ANDROID_SRC_JNI_SCOPED_JAVA_REF_COUNTED_H_ + +#include "sdk/android/native_api/jni/scoped_java_ref.h" + +namespace webrtc { +namespace jni { + +// Holds a reference to a java object implementing the RefCounted interface, and +// calls its release() method from the destructor. +class ScopedJavaRefCounted { + public: + // Takes over the caller's reference. + static ScopedJavaRefCounted Adopt(JNIEnv* env, + const JavaRef& j_object) { + return ScopedJavaRefCounted(env, j_object); + } + + ScopedJavaRefCounted(ScopedJavaRefCounted&& other) = default; + + // TODO(nisse): Implement move assignment and copy operations when needed. + ScopedJavaRefCounted(const ScopedJavaRefCounted& other) = delete; + ScopedJavaRefCounted& operator=(const ScopedJavaRefCounted&) = delete; + + ~ScopedJavaRefCounted() { + if (!j_object_.is_null()) { + JNIEnv* jni = AttachCurrentThreadIfNeeded(); + Java_RefCounted_release(jni, j_object_); + CHECK_EXCEPTION(jni) + << "Unexpected java exception from ScopedJavaRefCounted.release()"; + } + } + + private: + // Adopts reference. + ScopedJavaRefCounted(JNIEnv* env, const JavaRef& j_object) + : j_object_(env, j_object) {} + + ScopedJavaGlobalRef j_object_; +}; + +} // namespace jni +} // namespace webrtc + +#endif // SDK_ANDROID_SRC_JNI_SCOPED_JAVA_REF_COUNTED_H_