-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[camera_avfoundation] Implementation swift migration - part 5 #9397
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
RobertOdrowaz
wants to merge
4
commits into
flutter:main
Choose a base branch
from
leancodepl:feature/camera-implementation-swift-migration-part5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−223
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
98914d8
Migrate start, stop, close, and receivedImageStreamData to Swift
RobertOdrowaz 2e1fad1
Migrate reportInitializationState, setExposureMode, setExposureOffset…
RobertOdrowaz 8cfe0f2
Add missing comments to Camera protocol
RobertOdrowaz 1f66770
Remove overly verbose type in DefaultCamera
RobertOdrowaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,182 @@ final class DefaultCamera: FLTCam, Camera { | |
|
||
/// Maximum number of frames pending processing. | ||
/// To limit memory consumption, limit the number of frames pending processing. | ||
/// After some testing, 4 was determined to be the best maximuńm value. | ||
/// After some testing, 4 was determined to be the best maximum value. | ||
/// https://github.com/flutter/plugins/pull/4520#discussion_r766335637 | ||
private var maxStreamingPendingFramesCount = 4 | ||
|
||
private var exposureMode = FCPPlatformExposureMode.auto | ||
private var focusMode = FCPPlatformFocusMode.auto | ||
|
||
func reportInitializationState() { | ||
// Get all the state on the current thread, not the main thread. | ||
let state = FCPPlatformCameraState.make( | ||
withPreviewSize: FCPPlatformSize.make( | ||
withWidth: Double(previewSize.width), | ||
height: Double(previewSize.height) | ||
), | ||
exposureMode: exposureMode, | ||
focusMode: focusMode, | ||
exposurePointSupported: captureDevice.isExposurePointOfInterestSupported, | ||
focusPointSupported: captureDevice.isFocusPointOfInterestSupported | ||
) | ||
|
||
FLTEnsureToRunOnMainQueue { [weak self] in | ||
self?.dartAPI?.initialized(with: state) { _ in | ||
// Ignore any errors, as this is just an event broadcast. | ||
} | ||
} | ||
} | ||
|
||
func receivedImageStreamData() { | ||
streamingPendingFramesCount -= 1 | ||
} | ||
|
||
func start() { | ||
videoCaptureSession.startRunning() | ||
audioCaptureSession.startRunning() | ||
} | ||
|
||
func stop() { | ||
videoCaptureSession.stopRunning() | ||
audioCaptureSession.stopRunning() | ||
} | ||
|
||
func setExposureMode(_ mode: FCPPlatformExposureMode) { | ||
exposureMode = mode | ||
applyExposureMode() | ||
} | ||
|
||
private func applyExposureMode() { | ||
try? captureDevice.lockForConfiguration() | ||
switch exposureMode { | ||
case .locked: | ||
// AVCaptureExposureMode.autoExpose automatically adjusts the exposure one time, and then locks exposure for the device | ||
captureDevice.setExposureMode(.autoExpose) | ||
case .auto: | ||
if captureDevice.isExposureModeSupported(.continuousAutoExposure) { | ||
captureDevice.setExposureMode(.continuousAutoExposure) | ||
} else { | ||
captureDevice.setExposureMode(.autoExpose) | ||
} | ||
@unknown default: | ||
assertionFailure("Unknown exposure mode") | ||
} | ||
captureDevice.unlockForConfiguration() | ||
} | ||
|
||
func setExposureOffset(_ offset: Double) { | ||
try? captureDevice.lockForConfiguration() | ||
captureDevice.setExposureTargetBias(Float(offset), completionHandler: nil) | ||
captureDevice.unlockForConfiguration() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
} | ||
|
||
func setExposurePoint( | ||
_ point: FCPPlatformPoint?, withCompletion completion: @escaping (FlutterError?) -> Void | ||
) { | ||
guard captureDevice.isExposurePointOfInterestSupported else { | ||
completion( | ||
FlutterError( | ||
code: "setExposurePointFailed", | ||
message: "Device does not have exposure point capabilities", | ||
details: nil)) | ||
return | ||
} | ||
|
||
let orientation = UIDevice.current.orientation | ||
try? captureDevice.lockForConfiguration() | ||
// A nil point resets to the center. | ||
let exposurePoint = cgPoint( | ||
for: point ?? FCPPlatformPoint.makeWith(x: 0.5, y: 0.5), withOrientation: orientation) | ||
captureDevice.setExposurePointOfInterest(exposurePoint) | ||
captureDevice.unlockForConfiguration() | ||
// Retrigger auto exposure | ||
applyExposureMode() | ||
completion(nil) | ||
} | ||
|
||
func setFocusMode(_ mode: FCPPlatformFocusMode) { | ||
focusMode = mode | ||
applyFocusMode() | ||
} | ||
|
||
func setFocusPoint(_ point: FCPPlatformPoint?, completion: @escaping (FlutterError?) -> Void) { | ||
guard captureDevice.isFocusPointOfInterestSupported else { | ||
completion( | ||
FlutterError( | ||
code: "setFocusPointFailed", | ||
message: "Device does not have focus point capabilities", | ||
details: nil)) | ||
return | ||
} | ||
|
||
let orientation = deviceOrientationProvider.orientation() | ||
try? captureDevice.lockForConfiguration() | ||
// A nil point resets to the center. | ||
captureDevice.setFocusPointOfInterest( | ||
cgPoint( | ||
for: point ?? .makeWith(x: 0.5, y: 0.5), | ||
withOrientation: orientation) | ||
) | ||
captureDevice.unlockForConfiguration() | ||
// Retrigger auto focus | ||
applyFocusMode() | ||
completion(nil) | ||
} | ||
|
||
private func applyFocusMode() { | ||
applyFocusMode(focusMode, onDevice: captureDevice) | ||
} | ||
|
||
private func applyFocusMode( | ||
_ focusMode: FCPPlatformFocusMode, onDevice captureDevice: FLTCaptureDevice | ||
) { | ||
try? captureDevice.lockForConfiguration() | ||
switch focusMode { | ||
case .locked: | ||
// AVCaptureFocusMode.autoFocus automatically adjusts the focus one time, and then locks focus | ||
if captureDevice.isFocusModeSupported(.autoFocus) { | ||
captureDevice.setFocusMode(.autoFocus) | ||
} | ||
case .auto: | ||
if captureDevice.isFocusModeSupported(.continuousAutoFocus) { | ||
captureDevice.setFocusMode(.continuousAutoFocus) | ||
} else if captureDevice.isFocusModeSupported(.autoFocus) { | ||
captureDevice.setFocusMode(.autoFocus) | ||
} | ||
@unknown default: | ||
assertionFailure("Unknown focus mode") | ||
} | ||
captureDevice.unlockForConfiguration() | ||
} | ||
|
||
private func cgPoint( | ||
for point: FCPPlatformPoint, withOrientation orientation: UIDeviceOrientation | ||
) | ||
-> CGPoint | ||
{ | ||
var x = point.x | ||
var y = point.y | ||
switch orientation { | ||
case .portrait: // 90 ccw | ||
y = 1 - point.x | ||
x = point.y | ||
case .portraitUpsideDown: // 90 cw | ||
x = 1 - point.y | ||
y = point.x | ||
case .landscapeRight: // 180 | ||
x = 1 - point.x | ||
y = 1 - point.y | ||
case .landscapeLeft: | ||
// No rotation required | ||
break | ||
default: | ||
// No rotation required | ||
break | ||
} | ||
return CGPoint(x: x, y: y) | ||
} | ||
|
||
func captureOutput( | ||
_ output: AVCaptureOutput, | ||
didOutput sampleBuffer: CMSampleBuffer, | ||
|
@@ -241,6 +413,22 @@ final class DefaultCamera: FLTCam, Camera { | |
} | ||
} | ||
|
||
func close() { | ||
stop() | ||
for input in videoCaptureSession.inputs { | ||
videoCaptureSession.removeInput(FLTDefaultCaptureInput(input: input)) | ||
} | ||
for output in videoCaptureSession.outputs { | ||
videoCaptureSession.removeOutput(output) | ||
} | ||
for input in audioCaptureSession.inputs { | ||
audioCaptureSession.removeInput(FLTDefaultCaptureInput(input: input)) | ||
} | ||
for output in audioCaptureSession.outputs { | ||
audioCaptureSession.removeOutput(output) | ||
} | ||
} | ||
|
||
func copyPixelBuffer() -> Unmanaged<CVPixelBuffer>? { | ||
var pixelBuffer: CVPixelBuffer? | ||
pixelBufferSynchronizationQueue.sync { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we do
defer { captureDevice.unlockForConfiguration() }
in case we update the code and introduce an early exit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean an early exit after
lockForConfiguration
fails? If so I wouldn't add defer because I don't think we have tounlockForConfiguration
iflockForConfiguration
has failed (currently we discard the lock result in line with ObjC implementation)