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

Conversation

@renefloor
Copy link
Contributor

@renefloor renefloor commented Jun 1, 2021

iOS supports 3 focus modes:

  • ContinuousAutoFocus
  • AutoFocus
  • Fixed

Flutter has

  • Auto
  • Locked

The second (AutoFocus) focuses once and then switches to Fixed.
Previously when setting the mode to 'Auto' the iOS code did check if ContinuousAutoFocus was supported and otherwise set AutoFocus. Setting locked always let iOS set mode to AutoFocus. However, not all devices support AutoFocus, for example the front camera of iPad (5th generation) does not. The FocusMode is always Locked.

My proposed solution is to just not set the FocusMode if it is not supported as that is the same behaviour already done when ContinuousAutoFocus is not supported. I can also throw a PlatformException so the caller in the dart side knows that the mode is not supported. However, I don't know if you would also give a warning if ContinuousAutoFocus is not supported and you switch to AutoFocus instead.

I also need to added a test project as there was none yet, this test project contains a failing test to check the CI.

The change is basically this:
from:

- (void)applyFocusMode:(FocusMode)focusMode onDevice:(AVCaptureDevice *)captureDevice {
  [captureDevice lockForConfiguration:nil];
  switch (focusMode) {
    case FocusModeLocked:
      [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
      break;
    case FocusModeAuto:
      if ([captureDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
        [captureDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
      } else {
        [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
      }
      break;
  }
  [captureDevice unlockForConfiguration];
}

to:

- (void)applyFocusMode:(FocusMode)focusMode onDevice:(AVCaptureDevice *)captureDevice {
  [captureDevice lockForConfiguration:nil];
  switch (focusMode) {
    case FocusModeLocked:
      if ([captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
        [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
      }
      break;
    case FocusModeAuto:
      if ([captureDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
        [captureDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
      } else if ([captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
        [captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
      }
      break;
  }
  [captureDevice unlockForConfiguration];
}

Fixes flutter/flutter#76498

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides. (Note that unlike the flutter/flutter repo, the flutter/plugins repo does use dart format. See plugin_tool format)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Copy link
Contributor

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

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

Looks good in general, I have two nits that would be good to correct.

renefloor and others added 2 commits June 2, 2021 13:06
Co-authored-by: Maurits van Beusekom <maurits@vnbskm.nl>
@renefloor renefloor requested a review from mvanbeusekom June 2, 2021 11:07
Copy link
Contributor

@mvanbeusekom mvanbeusekom left a comment

Choose a reason for hiding this comment

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

LGTM!

@mvanbeusekom mvanbeusekom merged commit aaea6b6 into flutter:master Jun 2, 2021
@mvanbeusekom mvanbeusekom deleted the bugfix/76498 branch June 2, 2021 14:30
@stuartmorgan-g
Copy link
Contributor

stuartmorgan-g commented Jun 2, 2021

I also need to added a test project as there was none yet, this test project contains a failing test to check the CI.

Is https://github.com/flutter/plugins/blob/master/packages/camera/camera/ios/Tests/CameraPluginTests.m not being compiled then?

(We have an iOS unit test location mismatch that crept in at some point; I've raised it with jmagman to decide on a canonical location and standardize the repo.)

@renefloor
Copy link
Contributor Author

renefloor commented Jun 2, 2021

Is https://github.com/flutter/plugins/blob/master/packages/camera/camera/ios/Tests/CameraPluginTests.m not being compiled then?

Let me check with the cli tools in the plugin repo if it was actually running, but at least it was not visible in the xcode project which makes editing the file way harder.

Edit: I just verified with the following branches:
https://github.com/Baseflow/flutter-plugins/tree/test/failing-before
https://github.com/Baseflow/flutter-plugins/tree/test/failing-after

Both have this commit: Baseflow@fe2e132

And I ran the test doing:

dart run ./script/tool/lib/src/main.dart xctest --plugins=camera

The before has the commit before this PR was merged and 'All XCTests have passed', the after has failing tests.

PS: The PR body had to be updated, because the faulty test was fixed in b0858f6

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jun 2, 2021
fotiDim pushed a commit to fotiDim/plugins that referenced this pull request Sep 13, 2021
* Check if chosen focus mode is supported

* adding unit tests

* Added tests for camera focus

* Fail 1 test on purpose

* formatting

* Fix copyright notion

* Fix test run

* Add documentation and changelog

* Update packages/camera/camera/CHANGELOG.md

Co-authored-by: Maurits van Beusekom <maurits@vnbskm.nl>

* Improve documentation

Co-authored-by: Maurits van Beusekom <maurits@vnbskm.nl>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

3 participants