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

[camera] Added timeout to Android pre-capture sequence #3558

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change, is it to fix another issue?

Copy link
Contributor Author

@mvanbeusekom mvanbeusekom Feb 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a warning when running flutter analyze on the project. This is a mock implementation of the CameraPlatform class which got updated during the null-safety migration. During the migration we missed updating this value.

Since flutter analyze was complaining I decided to update it with this PR.

}) =>
mockPlatformException
? throw PlatformException(code: 'foo', message: 'bar')
Expand Down