From 1fef9d28cf7e38577c0684c3d2c769de6ff269da Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Sat, 25 May 2019 11:52:44 -0300 Subject: [PATCH] Fix #425 --- .../com/otaliastudios/cameraview/VideoRecorderTest.java | 2 +- .../main/java/com/otaliastudios/cameraview/Camera1.java | 8 ++++---- .../com/otaliastudios/cameraview/FullVideoRecorder.java | 9 +++++++-- .../otaliastudios/cameraview/SnapshotVideoRecorder.java | 2 ++ .../java/com/otaliastudios/cameraview/VideoRecorder.java | 6 ++++-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java index 2399f215..96bbb420 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java @@ -28,7 +28,7 @@ public class VideoRecorderTest extends BaseTest { }; recorder.start(); recorder.stop(); - Mockito.verify(listener, Mockito.times(1)).onVideoResult(result); + Mockito.verify(listener, Mockito.times(1)).onVideoResult(result, ); assertNull(recorder.mListener); assertNull(recorder.mResult); } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 1d54cde2..1d25a8c8 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -336,7 +336,7 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera return; } - LOG.e("Error inside the onError callback.", error); + LOG.e("Internal Camera1 error.", error); Exception runtime = new RuntimeException(CameraLogger.lastMessage); int reason; switch (error) { @@ -646,13 +646,13 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera // Video recording stuff. @Override - public void onVideoResult(@Nullable VideoResult result) { + public void onVideoResult(@Nullable VideoResult result, @Nullable Exception exception) { mVideoRecorder = null; if (result != null) { mCameraCallbacks.dispatchOnVideoTaken(result); } else { // Something went wrong, lock the camera again. - mCameraCallbacks.dispatchError(new CameraException(CameraException.REASON_VIDEO_FAILED)); + mCameraCallbacks.dispatchError(new CameraException(exception, CameraException.REASON_VIDEO_FAILED)); mCamera.lock(); } } @@ -689,7 +689,7 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera } catch (Exception e) { // If this failed, we are unlikely able to record the video. // Dispatch an error. - onVideoResult(null); + onVideoResult(null, e); return; } mVideoRecorder = new FullVideoRecorder(videoResult, Camera1.this, diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/FullVideoRecorder.java b/cameraview/src/main/java/com/otaliastudios/cameraview/FullVideoRecorder.java index 65359b34..4d26287d 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/FullVideoRecorder.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/FullVideoRecorder.java @@ -104,7 +104,9 @@ class FullVideoRecorder extends VideoRecorder { mMediaRecorder.prepare(); mMediaRecorder.start(); } catch (Exception e) { + LOG.w("stop:", "Error while starting media recorder.", e); mResult = null; + mError = e; stop(); } } @@ -115,9 +117,12 @@ class FullVideoRecorder extends VideoRecorder { try { mMediaRecorder.stop(); } catch (Exception e) { - // This can happen if stopVideo() is called right after takeVideo(). We don't care. + LOG.w("stop:", "Error while closing media recorder.", e); + // This can happen if stopVideo() is called right after takeVideo() (in which case we don't care) + // Or when prepare()/start() have failed for some reason and we are not allowed to call stop. + // Make sure we don't override the error if one exists already. mResult = null; - LOG.w("stop:", "Error while closing media recorder. Swallowing", e); + if (mError == null) mError = e; } mMediaRecorder.release(); if (mController != null) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotVideoRecorder.java b/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotVideoRecorder.java index 3168b5dd..bf54ab14 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotVideoRecorder.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/SnapshotVideoRecorder.java @@ -124,7 +124,9 @@ class SnapshotVideoRecorder extends VideoRecorder implements GlCameraPreview.Ren // If something failed, undo the result, since this is the mechanism // to notify Camera1 about this. if (e != null) { + LOG.e("Error onEncoderStop", e); mResult = null; + mError = e; } else { if (stopReason == MediaEncoderEngine.STOP_BY_MAX_DURATION) { mResult.endReason = VideoResult.REASON_MAX_DURATION_REACHED; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/VideoRecorder.java b/cameraview/src/main/java/com/otaliastudios/cameraview/VideoRecorder.java index 22d512b0..afd176c4 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/VideoRecorder.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/VideoRecorder.java @@ -12,6 +12,7 @@ abstract class VideoRecorder { /* tests */ VideoResult mResult; /* tests */ VideoResultListener mListener; + protected Exception mError; VideoRecorder(@NonNull VideoResult stub, @Nullable VideoResultListener listener) { mResult = stub; @@ -25,14 +26,15 @@ abstract class VideoRecorder { @SuppressWarnings("WeakerAccess") protected void dispatchResult() { if (mListener != null) { - mListener.onVideoResult(mResult); + mListener.onVideoResult(mResult, mError); mListener = null; mResult = null; + mError = null; } } interface VideoResultListener { - void onVideoResult(@Nullable VideoResult result); + void onVideoResult(@Nullable VideoResult result, @Nullable Exception exception); } }