Skip to content

[camera_avfoundation] Implementation swift migration - part 4 #9219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

RobertOdrowaz
Copy link
Contributor

Migrates camera implementation as part of flutter/flutter#119109

This PR migrates the main FLTCam class to Swift. I know it's large, but I don't see any reasonable way to split it into smaller parts (MockFLTCam clean up could be separated but it won't make the review simpler in any meaningful way).

Since the goal of the PR is migration to Swift the code is mostly translated close to verbatim including the known issues in setDescriptionWhileRecording method (flutter/flutter#162376 (comment))

There are quite a few cases of error as NSError casting because I didn't want to introduce a breaking change so I had do keep the error message structure exactly the same as in ObjC. The errors in ObjC implementation were very closely related to NSError. I don't see a better way to handle it than casting Error back to NSError but I'm open to suggestions.

Pre-Review Checklist

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

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

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

not a complete review. is this the last remaining work?

camera?.close()
camera = newCamera

FLTEnsureToRunOnMainQueue { [weak self] in
guard let strongSelf = self else { return }
completion(NSNumber(value: strongSelf.registry.register(newCamera)), nil)
}
} catch let error as NSError {
Copy link
Contributor

Choose a reason for hiding this comment

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

Was the error originated from our own code or from AVFoundation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both:

  1. AVCaptureDeviceInput deviceInputWithDevice (AVFoundation)
  2. AVCaptureDevice lockForConfiguration (AVFoundation)
  3. FLTDefaultCam.setCaptureSessionPreset (our own code)

IMO the whole package deserves a clean up of errors in the future because there is absolutely no consistency in the error format

@RobertOdrowaz
Copy link
Contributor Author

This is the main part but it's not the last. There will be a ~3 more PRs I think. ~2 with migration of all the protocols which is a formality and one with the switch to a Swift based pigeon which will include some more significant changes

@RobertOdrowaz RobertOdrowaz requested a review from hellohuanlin May 9, 2025 05:19
Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

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

Sorry it's really really hard for me to review this PR due to its size and complexity of the logic. Is it possible for you to have separate PRs that break out smaller bits first (e.g. stateless helpers)?

static func createTestCamera(_ configuration: FLTCamConfiguration) -> FLTCam {
return FLTCam(configuration: configuration, error: nil)
static func createTestCamera(_ configuration: FLTCamConfiguration) -> DefaultCamera {
let camera = try? DefaultCamera(configuration: configuration)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can you do a XCTUnwrap instead of !?

Copy link
Contributor Author

@RobertOdrowaz RobertOdrowaz May 15, 2025

Choose a reason for hiding this comment

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

XCTUnwrap is marked with throws so it leaves me in the same position as the DefaultCamera init

get {
return super.dartAPI
preconditionFailure("Attempted to access unimplemented property: dartAPI")
Copy link
Contributor

Choose a reason for hiding this comment

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

why this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

same q for other proeprties

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This mock used to extend the FLTCam class now it's implementing the Camera protocol so there is no super implementation to fall back to


// Add the audio input
if mediaSettings.enableAudio {
var acl = AudioChannelLayout()
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: audioChannelLayout

@RobertOdrowaz RobertOdrowaz force-pushed the feature/camera-implementation-swift-migration-part4 branch from 917fe7e to d49c640 Compare May 15, 2025 16:09
@RobertOdrowaz RobertOdrowaz marked this pull request as draft May 15, 2025 16:12
@RobertOdrowaz
Copy link
Contributor Author

@hellohuanlin I've extracted whatever was possible to part 3.5. I will rebase this one on main once 3.5 is merged

auto-submit bot pushed a commit that referenced this pull request May 28, 2025
Migrates camera implementation as part of flutter/flutter#119109

This PR consists of everything that was possible to extract from the [part 4](#9219) which includes:
* Adds `audioCaptureDeviceFactory` to `FLTCamConfiguration`.
* Renames the `lockCaptureOrientation` method of Objective-C type `FLTCam` when exported to Swift.
* Renames arguments of the `captureOutput` method of Objective-C type `FLTCam` when exported to Swift.
* Changes the `connection` argument type of the `captureOutput` method of the of `FLTCam` class to `AVCaptureConnection`.
* Makes `minimum/maximumAvailableZoomFactor` and `minimum/maximumExposureOffset` fields of `FLTCam` readonly.
* Remove `@testable` from `camera_avfoundation_objc` imports

## Pre-Review Checklist

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
@RobertOdrowaz RobertOdrowaz force-pushed the feature/camera-implementation-swift-migration-part4 branch 2 times, most recently from e9350c1 to 3ba84c1 Compare May 28, 2025 15:29
@RobertOdrowaz RobertOdrowaz force-pushed the feature/camera-implementation-swift-migration-part4 branch from 3ba84c1 to 52f0941 Compare May 28, 2025 15:39
@RobertOdrowaz RobertOdrowaz marked this pull request as ready for review May 28, 2025 15:58
@RobertOdrowaz
Copy link
Contributor Author

It's still big but at least a bit smaller now

details: error.domain)
}

private static func createConnection(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you try to further reduce the size of PR again? For example, static members can be moved out since it doesn't depend on camera states. For non-static members, some stateless logic can be factored out, and others can be reorganized into extensions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we can meaningfully reduce the size of this PR without a significant refactor, which I don't have a budget for. What I think we can do instead is try to migrate this class in smaller parts. Keep a "base" implementation in ObjC, subclass it in Swift, and move method implementation in smaller chunks from the "base" ObjC class to the Swift subclass. Let me know what you think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants