From 23b08eb531aef7d2c69512622c46cf2dab965207 Mon Sep 17 00:00:00 2001 From: magjed Date: Tue, 5 Apr 2016 01:37:04 -0700 Subject: [PATCH] Android VideoCapture: Add null checks in stopCaptureOnCameraThread If stopCapture is called shortly after startCapture, and the first startCaptureOnCameraThread failed, but still hasn't retried 3 times, stopCaptureOnCameraThread will be called in a state where the camera is not initialized. This CL adds null checks in stopCaptureOnCameraThread to avoid crashes. BUG=b/27939867 Review URL: https://codereview.webrtc.org/1854103002 Cr-Commit-Position: refs/heads/master@{#12234} --- .../org/webrtc/VideoCapturerAndroid.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java b/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java index b8ab1a8c5e..93e623e4bd 100644 --- a/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java +++ b/webrtc/api/java/android/org/webrtc/VideoCapturerAndroid.java @@ -467,10 +467,8 @@ public class VideoCapturerAndroid implements error = e; } Logging.e(TAG, "startCapture failed", error); - if (camera != null) { - // Make sure the camera is released. - stopCaptureOnCameraThread(); - } + // Make sure the camera is released. + stopCaptureOnCameraThread(); synchronized (handlerLock) { // Remove all pending Runnables posted from |this|. cameraThreadHandler.removeCallbacksAndMessages(this /* token */); @@ -607,20 +605,28 @@ public class VideoCapturerAndroid implements private void stopCaptureOnCameraThread() { checkIsOnCameraThread(); Logging.d(TAG, "stopCaptureOnCameraThread"); + // Note that the camera might still not be started here if startCaptureOnCameraThread failed + // and we posted a retry. // Make sure onTextureFrameAvailable() is not called anymore. - surfaceHelper.stopListening(); + if (surfaceHelper != null) { + surfaceHelper.stopListening(); + } cameraThreadHandler.removeCallbacks(cameraObserver); cameraStatistics.getAndResetFrameCount(); Logging.d(TAG, "Stop preview."); - camera.stopPreview(); - camera.setPreviewCallbackWithBuffer(null); + if (camera != null) { + camera.stopPreview(); + camera.setPreviewCallbackWithBuffer(null); + } queuedBuffers.clear(); captureFormat = null; Logging.d(TAG, "Release camera."); - camera.release(); - camera = null; + if (camera != null) { + camera.release(); + camera = null; + } if (eventsHandler != null) { eventsHandler.onCameraClosed(); }