Call java SurfaceTextureHelper.dispose from the corresponding C++ destructor.

This makes it clearer that the C++ SurfaceTextureHelper owns its associated java object it.

In addition, arrange so that the SurfaceTextureHelper.stopListening
method (in java) can be called from any thread.

BUG=

Review-Url: https://codereview.webrtc.org/1988043002
Cr-Commit-Position: refs/heads/master@{#12941}
This commit is contained in:
nisse
2016-05-27 00:27:59 -07:00
committed by Commit bot
parent 756f395cbf
commit a44e72c44f
7 changed files with 60 additions and 53 deletions

View File

@ -63,7 +63,7 @@ class SurfaceTextureHelper {
// http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/graphics/SurfaceTexture.java#195.
// Therefore, in order to control the callback thread on API lvl < 21, the SurfaceTextureHelper
// is constructed on the |handler| thread.
return ThreadUtils.invokeUninterruptibly(handler, new Callable<SurfaceTextureHelper>() {
return ThreadUtils.invokeAtFrontUninterruptibly(handler, new Callable<SurfaceTextureHelper>() {
@Override
public SurfaceTextureHelper call() {
try {
@ -373,17 +373,18 @@ class SurfaceTextureHelper {
/**
* Stop listening. The listener set in startListening() is guaranteded to not receive any more
* onTextureFrameAvailable() callbacks after this function returns. This function must be called
* on the getHandler() thread.
* onTextureFrameAvailable() callbacks after this function returns.
*/
public void stopListening() {
if (handler.getLooper().getThread() != Thread.currentThread()) {
throw new IllegalStateException("Wrong thread.");
}
Logging.d(TAG, "stopListening()");
handler.removeCallbacks(setListenerRunnable);
this.listener = null;
this.pendingListener = null;
ThreadUtils.invokeAtFrontUninterruptibly(handler, new Runnable() {
@Override
public void run() {
listener = null;
pendingListener = null;
}
});
}
/**
@ -431,24 +432,15 @@ class SurfaceTextureHelper {
*/
public void dispose() {
Logging.d(TAG, "dispose()");
if (handler.getLooper().getThread() == Thread.currentThread()) {
isQuitting = true;
if (!isTextureInUse) {
release();
}
return;
}
final CountDownLatch barrier = new CountDownLatch(1);
handler.postAtFrontOfQueue(new Runnable() {
@Override public void run() {
ThreadUtils.invokeAtFrontUninterruptibly(handler, new Runnable() {
@Override
public void run() {
isQuitting = true;
barrier.countDown();
if (!isTextureInUse) {
release();
}
}
});
ThreadUtils.awaitUninterruptibly(barrier);
}
public void textureToYUV(ByteBuffer buf,

View File

@ -53,13 +53,6 @@ AndroidVideoCapturerJni::~AndroidVideoCapturerJni() {
*j_video_capturer_,
GetMethodID(jni(), *j_video_capturer_class_, "dispose", "()V"));
CHECK_EXCEPTION(jni()) << "error during VideoCapturer.dispose()";
if (surface_texture_helper_) {
jni()->CallVoidMethod(
surface_texture_helper_->GetJavaSurfaceTextureHelper(),
GetMethodID(jni(), FindClass(jni(), "org/webrtc/SurfaceTextureHelper"),
"dispose", "()V"));
}
CHECK_EXCEPTION(jni()) << "error during SurfaceTextureHelper.dispose()";
}
void AndroidVideoCapturerJni::Start(int width, int height, int framerate,

View File

@ -14,6 +14,7 @@
#include "webrtc/api/java/jni/classreferenceholder.h"
#include "webrtc/base/bind.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
namespace webrtc_jni {
@ -48,6 +49,14 @@ SurfaceTextureHelper::SurfaceTextureHelper(JNIEnv* jni,
}
SurfaceTextureHelper::~SurfaceTextureHelper() {
LOG(LS_INFO) << "SurfaceTextureHelper dtor";
JNIEnv* jni = AttachCurrentThreadIfNeeded();
jni->CallVoidMethod(
*j_surface_texture_helper_,
GetMethodID(jni, FindClass(jni, "org/webrtc/SurfaceTextureHelper"),
"dispose", "()V"));
CHECK_EXCEPTION(jni) << "error during SurfaceTextureHelper.dispose()";
}
jobject SurfaceTextureHelper::GetJavaSurfaceTextureHelper() const {

View File

@ -30,6 +30,8 @@ namespace webrtc_jni {
// SurfaceTextureHelper is reference counted to make sure that it is not
// destroyed while a VideoFrameBuffer is in use.
// This class is the C++ counterpart of the java class SurfaceTextureHelper.
// It owns the corresponding java object, and calls the java dispose
// method when destroyed.
// Usage:
// 1. Create an instance of this class.
// 2. Get the Java SurfaceTextureHelper with GetJavaSurfaceTextureHelper().

View File

@ -547,10 +547,10 @@ public class MediaCodecVideoDecoder {
}
public void release() {
// SurfaceTextureHelper.dispose() will block until any onTextureFrameAvailable() in
// progress is done. Therefore, the call to dispose() must be outside any synchronized
// SurfaceTextureHelper.stopListening() will block until any onTextureFrameAvailable() in
// progress is done. Therefore, the call must be outside any synchronized
// statement that is also used in the onTextureFrameAvailable() above to avoid deadlocks.
surfaceTextureHelper.dispose();
surfaceTextureHelper.stopListening();
synchronized (newFrameLock) {
if (renderedBuffer != null) {
surfaceTextureHelper.returnTextureFrame();