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 11 commits 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?


// Import Objectice-C part of the implementation when SwiftPM is used.
#if canImport(camera_avfoundation_objc)
import camera_avfoundation_objc
@testable import camera_avfoundation_objc
Copy link
Contributor

Choose a reason for hiding this comment

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

curious does @testable import have any effect on objc modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably not, TBH I haven't considered it. I will keep this one for consistency and we will be able to remove all of them shortly

var getMaximumAvailableZoomFactorStub: (() -> CGFloat)?
final class MockFLTCam: NSObject, FLTCam {
var setDartApiStub: ((FCPCameraEventApi?) -> Void)?
var setOnFrameAvailableStub: (((() -> Void)?) -> Void)?
Copy link
Contributor

Choose a reason for hiding this comment

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

are these just re-ordered?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes aside from some very minor renames e.g. lockCaptureStub -> lockCaptureOrientationStub because this method is finally is called properly in FLTCam

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

import Flutter
import ObjectiveC

class FLTImageStreamHandler: NSObject, FlutterStreamHandler {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: final, and remove FLT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is extended in some tests so I'm leaving it as non-final

@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

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