From 53d712402f713d9bdbb0fa928442a19ffa14d5d3 Mon Sep 17 00:00:00 2001 From: Gian-Carlo Pascutto Date: Tue, 28 Jul 2015 08:55:06 +0200 Subject: [PATCH] Bug 1186657. r=jesup,nchen --- .../org/webrtc/videoengine/VideoCaptureAndroid.java | 11 ++++++++++- .../video_capture/android/video_capture_android.cc | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/media/webrtc/trunk/webrtc/modules/video_capture/android/java/src/org/webrtc/videoengine/VideoCaptureAndroid.java b/media/webrtc/trunk/webrtc/modules/video_capture/android/java/src/org/webrtc/videoengine/VideoCaptureAndroid.java index 2c67e743d19..c7729c4794a 100644 --- a/media/webrtc/trunk/webrtc/modules/video_capture/android/java/src/org/webrtc/videoengine/VideoCaptureAndroid.java +++ b/media/webrtc/trunk/webrtc/modules/video_capture/android/java/src/org/webrtc/videoengine/VideoCaptureAndroid.java @@ -56,7 +56,7 @@ public class VideoCaptureAndroid implements PreviewCallback, Callback, AppStateL private Handler cameraThreadHandler; private Context context; private final int id; - private final long native_capturer; // |VideoCaptureAndroid*| in C++. + private volatile long native_capturer; // |VideoCaptureAndroid*| in C++. private SurfaceTexture cameraSurfaceTexture; private int[] cameraGlTextures = null; @@ -373,6 +373,15 @@ public class VideoCaptureAndroid implements PreviewCallback, Callback, AppStateL return status; } + @WebRTCJNITarget + private void unlinkCapturer() { + // stopCapture might fail. That might leave the callbacks dangling, so make + // sure those don't call into dead code. + // Note that onPreviewCameraFrame isn't synchronized, so there's no point in + // synchronizing us either. ProvideCameraFrame has to do the null check. + native_capturer = 0; + } + private void stopCaptureOnCameraThread( Exchanger result) { if (camera == null) { diff --git a/media/webrtc/trunk/webrtc/modules/video_capture/android/video_capture_android.cc b/media/webrtc/trunk/webrtc/modules/video_capture/android/video_capture_android.cc index 4df1efb19da..ad784c7cea9 100644 --- a/media/webrtc/trunk/webrtc/modules/video_capture/android/video_capture_android.cc +++ b/media/webrtc/trunk/webrtc/modules/video_capture/android/video_capture_android.cc @@ -41,6 +41,8 @@ void JNICALL ProvideCameraFrame( jint rotation, jlong timeStamp, jlong context) { + if (!context) + return; webrtc::videocapturemodule::VideoCaptureAndroid* captureModule = reinterpret_cast( context); @@ -167,7 +169,14 @@ VideoCaptureAndroid::~VideoCaptureAndroid() { if (_captureStarted) StopCapture(); AttachThreadScoped ats(g_jvm); - ats.env()->DeleteGlobalRef(_jCapturer); + JNIEnv* env = ats.env(); + + // Avoid callbacks into ourself even if the above stopCapture fails. + jmethodID j_unlink = + env->GetMethodID(g_java_capturer_class, "unlinkCapturer", "()V"); + env->CallVoidMethod(_jCapturer, j_unlink); + + env->DeleteGlobalRef(_jCapturer); } int32_t VideoCaptureAndroid::StartCapture( @@ -216,6 +225,7 @@ int32_t VideoCaptureAndroid::StopCapture() { // onIncomingFrame() call. _apiCs.Leave(); + // try to stop the capturer. jmethodID j_stop = env->GetMethodID(g_java_capturer_class, "stopCapture", "()Z"); return env->CallBooleanMethod(_jCapturer, j_stop) ? 0 : -1;