Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[camera] Added timeout to Android pre-capture sequence (#3558)
Browse files Browse the repository at this point in the history
* Added timeout for waiting pre-capture state Android

* Fix analysis warning and bumped version
  • Loading branch information
mvanbeusekom authored Feb 18, 2021
1 parent bf57107 commit 361567b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.0-nullsafety.1

* Added a timeout to the pre-capture sequence on Android to prevent crashes when the camera cannot get a focus.

## 0.8.0-nullsafety

* Migrated to null safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.util.Range;
import android.util.Rational;
Expand Down Expand Up @@ -73,6 +74,9 @@ interface ErrorCallback {
public class Camera {
private static final String TAG = "Camera";

/** Timeout for the pre-capture sequence. */
private static final long PRECAPTURE_TIMEOUT_MS = 1000;

private final SurfaceTextureEntry flutterTexture;
private final CameraManager cameraManager;
private final DeviceOrientationManager deviceOrientationListener;
Expand Down Expand Up @@ -105,6 +109,7 @@ public class Camera {
private boolean useAutoFocus = true;
private Range<Integer> fpsRange;
private PlatformChannel.DeviceOrientation lockedCaptureOrientation;
private long preCaptureStartTime;

private static final HashMap<String, Integer> supportedImageFormats;
// Current supported outputs
Expand Down Expand Up @@ -503,11 +508,16 @@ private void processCapture(CaptureResult result) {
|| aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED
|| aeState == CaptureRequest.CONTROL_AE_STATE_CONVERGED) {
pictureCaptureRequest.setState(State.waitingPreCaptureReady);
setPreCaptureStartTime();
}
break;
case waitingPreCaptureReady:
if (aeState == null || aeState != CaptureRequest.CONTROL_AE_STATE_PRECAPTURE) {
runPictureCapture();
} else {
if (hitPreCaptureTimeout()) {
unlockAutoFocus();
}
}
}
}
Expand Down Expand Up @@ -1142,6 +1152,20 @@ public void stopImageStream() throws CameraAccessException {
startPreview();
}

/** Sets the time the pre-capture sequence started. */
private void setPreCaptureStartTime() {
preCaptureStartTime = SystemClock.elapsedRealtime();
}

/**
* Check if the timeout for the pre-capture sequence has been reached.
*
* @return true if the timeout is reached; otherwise false is returned.
*/
private boolean hitPreCaptureTimeout() {
return (SystemClock.elapsedRealtime() - preCaptureStartTime) > PRECAPTURE_TIMEOUT_MS;
}

private void closeCaptureSession() {
if (cameraCaptureSession != null) {
cameraCaptureSession.close();
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera
description: A Flutter plugin for getting information about and controlling the
camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video,
and streaming image buffers to dart.
version: 0.8.0-nullsafety
version: 0.8.0-nullsafety.1
homepage: https://github.com/flutter/plugins/tree/master/packages/camera/camera

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera/test/camera_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ class MockCameraPlatform extends Mock
Future<int> createCamera(
CameraDescription description,
ResolutionPreset? resolutionPreset, {
bool enableAudio = true,
bool enableAudio = false,
}) =>
mockPlatformException
? throw PlatformException(code: 'foo', message: 'bar')
Expand Down

0 comments on commit 361567b

Please sign in to comment.