Skip to content

Commit 4385dea

Browse files
[camera_avfoundation] Wrappers swift migration - part 3 (#10293)
Migrates camera wrappers as part of flutter/flutter#119109 This PR migrates wrappers to Swift: * `FLTCaptureDevice` * `FLTCaptureSession` * `FLTFormatUtils` In line with Swift conventions, the `FLT` prefixes are removed. The `Default` class implementations are replaced with protocol conformance on base `AV` classes. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^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.
1 parent a6ce940 commit 4385dea

26 files changed

+363
-688
lines changed

packages/camera/camera_avfoundation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.22+5
2+
3+
* Migrates `FLTCaptureDevice`, `FLTCaptureSession`, and `FLTFormatUtils` classes to Swift.
4+
15
## 0.9.22+4
26

37
* Migrates `FLTCameraDeviceDiscovering` and `FLTDeviceOrientationProviding` classes to Swift.

packages/camera/camera_avfoundation/example/ios/RunnerTests/CameraSessionPresetsTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ final class CameraSessionPresetsTests: XCTestCase {
2828
}
2929
let captureFormatMock = MockCaptureDeviceFormat()
3030
let captureDeviceMock = MockCaptureDevice()
31-
captureDeviceMock.formats = [captureFormatMock]
32-
captureDeviceMock.activeFormat = captureFormatMock
31+
captureDeviceMock.flutterFormats = [captureFormatMock]
32+
captureDeviceMock.flutterActiveFormat = captureFormatMock
3333
captureDeviceMock.lockForConfigurationStub = {
3434
lockForConfigurationExpectation.fulfill()
3535
}

packages/camera/camera_avfoundation/example/ios/RunnerTests/CameraSettingsTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ private final class TestMediaSettingsAVWrapper: FLTCamMediaSettingsAVWrapper {
3939
videoSettingsExpectation = test.expectation(description: "videoSettingsExpectation")
4040
}
4141

42-
override func lockDevice(_ captureDevice: FLTCaptureDevice) throws {
42+
override func lockDevice(_ captureDevice: CaptureDevice) throws {
4343
lockExpectation.fulfill()
4444
}
4545

46-
override func unlockDevice(_ captureDevice: FLTCaptureDevice) {
46+
override func unlockDevice(_ captureDevice: CaptureDevice) {
4747
unlockExpectation.fulfill()
4848
}
4949

50-
override func beginConfiguration(for videoCaptureSession: FLTCaptureSession) {
50+
override func beginConfiguration(for videoCaptureSession: CaptureSession) {
5151
beginConfigurationExpectation.fulfill()
5252
}
5353

54-
override func commitConfiguration(for videoCaptureSession: FLTCaptureSession) {
54+
override func commitConfiguration(for videoCaptureSession: CaptureSession) {
5555
commitConfigurationExpectation.fulfill()
5656
}
5757

58-
override func setMinFrameDuration(_ duration: CMTime, on captureDevice: FLTCaptureDevice) {
58+
override func setMinFrameDuration(_ duration: CMTime, on captureDevice: CaptureDevice) {
5959
// FLTCam allows to set frame rate with 1/10 precision.
6060
let expectedDuration = CMTimeMake(value: 10, timescale: Int32(testFramesPerSecond * 10))
6161
if duration == expectedDuration {
6262
minFrameDurationExpectation.fulfill()
6363
}
6464
}
6565

66-
override func setMaxFrameDuration(_ duration: CMTime, on captureDevice: FLTCaptureDevice) {
66+
override func setMaxFrameDuration(_ duration: CMTime, on captureDevice: CaptureDevice) {
6767
// FLTCam allows to set frame rate with 1/10 precision.
6868
let expectedDuration = CMTimeMake(value: 10, timescale: Int32(testFramesPerSecond * 10))
6969
if duration == expectedDuration {
@@ -203,7 +203,7 @@ final class CameraSettingsTests: XCTestCase {
203203
configuration.mediaSettings = settings
204204
let camera = CameraTestUtils.createTestCamera(configuration)
205205

206-
let range = camera.captureDevice.activeFormat.videoSupportedFrameRateRanges[0]
206+
let range = camera.captureDevice.flutterActiveFormat.videoSupportedFrameRateRanges[0]
207207
XCTAssertLessThanOrEqual(range.minFrameRate, 60)
208208
XCTAssertGreaterThanOrEqual(range.maxFrameRate, 60)
209209
}

packages/camera/camera_avfoundation/example/ios/RunnerTests/CameraTestUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ enum CameraTestUtils {
4747
captureDeviceFormatMock2.videoSupportedFrameRateRanges = [frameRateRangeMock2]
4848

4949
let captureDeviceMock = MockCaptureDevice()
50-
captureDeviceMock.formats = [captureDeviceFormatMock1, captureDeviceFormatMock2]
50+
captureDeviceMock.flutterFormats = [captureDeviceFormatMock1, captureDeviceFormatMock2]
5151

5252
var currentFormat: FLTCaptureDeviceFormat = captureDeviceFormatMock1
5353

packages/camera/camera_avfoundation/example/ios/RunnerTests/Mocks/MockCameraDeviceDiscoverer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ final class MockCameraDeviceDiscoverer: NSObject, CameraDeviceDiscoverer {
1818
_ deviceTypes: [AVCaptureDevice.DeviceType],
1919
_ mediaType: AVMediaType,
2020
_ position: AVCaptureDevice.Position
21-
) -> [NSObject & FLTCaptureDevice]?
21+
) -> [NSObject & CaptureDevice]?
2222
)?
2323

2424
/// A stub that replaces the default implementation of
2525
/// `discoverySessionWithDeviceTypes:mediaType:position`.
2626
func discoverySession(
2727
withDeviceTypes deviceTypes: [AVCaptureDevice.DeviceType], mediaType: AVMediaType,
2828
position: AVCaptureDevice.Position
29-
) -> [FLTCaptureDevice] {
29+
) -> [CaptureDevice] {
3030
return discoverySessionStub?(deviceTypes, mediaType, position) ?? []
3131
}
3232
}

packages/camera/camera_avfoundation/example/ios/RunnerTests/Mocks/MockCaptureDevice.swift

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import camera_avfoundation
5+
@testable import camera_avfoundation
66

77
// Import Objective-C part of the implementation when SwiftPM is used.
88
#if canImport(camera_avfoundation_objc)
@@ -11,7 +11,7 @@ import camera_avfoundation
1111

1212
/// A mock implementation of `FLTCaptureDevice` that allows mocking the class
1313
/// properties.
14-
class MockCaptureDevice: NSObject, FLTCaptureDevice {
14+
class MockCaptureDevice: NSObject, CaptureDevice {
1515
var activeFormatStub: (() -> FLTCaptureDeviceFormat)?
1616
var setActiveFormatStub: ((FLTCaptureDeviceFormat) -> Void)?
1717
var getTorchModeStub: (() -> AVCaptureDevice.TorchMode)?
@@ -26,15 +26,15 @@ class MockCaptureDevice: NSObject, FLTCaptureDevice {
2626
var setVideoZoomFactorStub: ((CGFloat) -> Void)?
2727
var lockForConfigurationStub: (() throws -> Void)?
2828

29-
var device: AVCaptureDevice {
29+
var avDevice: AVCaptureDevice {
3030
preconditionFailure("Attempted to access unimplemented property: device")
3131
}
3232

3333
var uniqueID = ""
3434
var position = AVCaptureDevice.Position.unspecified
3535
var deviceType = AVCaptureDevice.DeviceType.builtInWideAngleCamera
3636

37-
var activeFormat: FLTCaptureDeviceFormat {
37+
var flutterActiveFormat: FLTCaptureDeviceFormat {
3838
get {
3939
activeFormatStub?() ?? MockCaptureDeviceFormat()
4040
}
@@ -43,7 +43,7 @@ class MockCaptureDevice: NSObject, FLTCaptureDevice {
4343
}
4444
}
4545

46-
var formats: [FLTCaptureDeviceFormat] = []
46+
var flutterFormats: [FLTCaptureDeviceFormat] = []
4747
var hasFlash = false
4848
var hasTorch = false
4949
var isTorchAvailable = false
@@ -78,20 +78,28 @@ class MockCaptureDevice: NSObject, FLTCaptureDevice {
7878
return isFocusModeSupportedStub?(mode) ?? false
7979
}
8080

81+
var focusMode: AVCaptureDevice.FocusMode {
82+
get { .autoFocus }
83+
set { setFocusModeStub?(newValue) }
84+
}
85+
8186
func setFocusMode(_ focusMode: AVCaptureDevice.FocusMode) {
8287
setFocusModeStub?(focusMode)
8388
}
8489

85-
func setFocusPointOfInterest(_ point: CGPoint) {
86-
setFocusPointOfInterestStub?(point)
90+
var focusPointOfInterest: CGPoint {
91+
get { CGPoint.zero }
92+
set { setFocusPointOfInterestStub?(newValue) }
8793
}
8894

89-
func setExposureMode(_ exposureMode: AVCaptureDevice.ExposureMode) {
90-
setExposureModeStub?(exposureMode)
95+
var exposureMode: AVCaptureDevice.ExposureMode {
96+
get { .autoExpose }
97+
set { setExposureModeStub?(newValue) }
9198
}
9299

93-
func setExposurePointOfInterest(_ point: CGPoint) {
94-
setExposurePointOfInterestStub?(point)
100+
var exposurePointOfInterest: CGPoint {
101+
get { CGPoint.zero }
102+
set { setExposurePointOfInterestStub?(newValue) }
95103
}
96104

97105
func setExposureTargetBias(_ bias: Float, completionHandler handler: ((CMTime) -> Void)? = nil) {
@@ -102,17 +110,11 @@ class MockCaptureDevice: NSObject, FLTCaptureDevice {
102110
return isExposureModeSupportedStub?(mode) ?? false
103111
}
104112

105-
func lensAperture() -> Float {
106-
return 0
107-
}
113+
var lensAperture: Float { 0 }
108114

109-
func exposureDuration() -> CMTime {
110-
return CMTime(value: 1, timescale: 1)
111-
}
115+
var exposureDuration: CMTime { CMTime(value: 1, timescale: 1) }
112116

113-
func iso() -> Float {
114-
return 0
115-
}
117+
var iso: Float { 0 }
116118

117119
func lockForConfiguration() throws {
118120
try lockForConfigurationStub?()

packages/camera/camera_avfoundation/example/ios/RunnerTests/Mocks/MockCaptureDeviceInputFactory.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import camera_avfoundation
5+
@testable import camera_avfoundation
66

77
// Import Objective-C part of the implementation when SwiftPM is used.
88
#if canImport(camera_avfoundation_objc)
@@ -11,8 +11,8 @@ import camera_avfoundation
1111

1212
///// A mocked implementation of FLTCaptureDeviceInputFactory which allows injecting a custom
1313
///// implementation.
14-
final class MockCaptureDeviceInputFactory: NSObject, FLTCaptureDeviceInputFactory {
15-
func deviceInput(with device: FLTCaptureDevice) throws -> FLTCaptureInput {
14+
final class MockCaptureDeviceInputFactory: NSObject, CaptureDeviceInputFactory {
15+
func deviceInput(with device: CaptureDevice) throws -> CaptureInput {
1616
return MockCaptureInput()
1717
}
1818
}

packages/camera/camera_avfoundation/example/ios/RunnerTests/Mocks/MockCaptureInput.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import camera_avfoundation
5+
@testable import camera_avfoundation
66

77
// Import Objective-C part of the implementation when SwiftPM is used.
88
#if canImport(camera_avfoundation_objc)
@@ -11,8 +11,8 @@ import camera_avfoundation
1111

1212
/// A mocked implementation of FLTCaptureInput which allows injecting a custom
1313
/// implementation.
14-
final class MockCaptureInput: NSObject, FLTCaptureInput {
15-
var input: AVCaptureInput {
14+
final class MockCaptureInput: NSObject, CaptureInput {
15+
var avInput: AVCaptureInput {
1616
preconditionFailure("Attempted to access unimplemented property: input")
1717
}
1818

packages/camera/camera_avfoundation/example/ios/RunnerTests/Mocks/MockCaptureSession.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import camera_avfoundation
5+
@testable import camera_avfoundation
66

77
// Import Objective-C part of the implementation when SwiftPM is used.
88
#if canImport(camera_avfoundation_objc)
@@ -11,7 +11,7 @@ import camera_avfoundation
1111

1212
/// Mock implementation of `FLTCaptureSession` protocol which allows injecting a custom
1313
/// implementation.
14-
final class MockCaptureSession: NSObject, FLTCaptureSession {
14+
final class MockCaptureSession: NSObject, CaptureSession {
1515
var setSessionPresetStub: ((AVCaptureSession.Preset) -> Void)?
1616
var beginConfigurationStub: (() -> Void)?
1717
var commitConfigurationStub: (() -> Void)?
@@ -56,13 +56,13 @@ final class MockCaptureSession: NSObject, FLTCaptureSession {
5656
return canSetSessionPresetStub?(preset) ?? true
5757
}
5858

59-
func addInputWithNoConnections(_ input: FLTCaptureInput) {}
59+
func addInputWithNoConnections(_ input: CaptureInput) {}
6060

6161
func addOutputWithNoConnections(_ output: AVCaptureOutput) {}
6262

6363
func addConnection(_: AVCaptureConnection) {}
6464

65-
func addInput(_: FLTCaptureInput) {}
65+
func addInput(_: CaptureInput) {}
6666

6767
func addOutput(_ output: AVCaptureOutput) {
6868

@@ -71,11 +71,11 @@ final class MockCaptureSession: NSObject, FLTCaptureSession {
7171
}
7272
}
7373

74-
func removeInput(_: FLTCaptureInput) {}
74+
func removeInput(_: CaptureInput) {}
7575

7676
func removeOutput(_: AVCaptureOutput) {}
7777

78-
func canAddInput(_: FLTCaptureInput) -> Bool {
78+
func canAddInput(_: CaptureInput) -> Bool {
7979
return true
8080
}
8181

packages/camera/camera_avfoundation/example/ios/RunnerTests/SampleBufferTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ private class FakeMediaSettingsAVWrapper: FLTCamMediaSettingsAVWrapper {
1919
self.inputMock = inputMock
2020
}
2121

22-
override func lockDevice(_ captureDevice: FLTCaptureDevice) throws {
22+
override func lockDevice(_ captureDevice: CaptureDevice) throws {
2323
// No-op.
2424
}
2525

26-
override func unlockDevice(_ captureDevice: FLTCaptureDevice) {
26+
override func unlockDevice(_ captureDevice: CaptureDevice) {
2727
// No-op.
2828
}
2929

30-
override func beginConfiguration(for videoCaptureSession: FLTCaptureSession) {
30+
override func beginConfiguration(for videoCaptureSession: CaptureSession) {
3131
// No-op.
3232
}
3333

34-
override func commitConfiguration(for videoCaptureSession: FLTCaptureSession) {
34+
override func commitConfiguration(for videoCaptureSession: CaptureSession) {
3535
// No-op.
3636
}
3737

38-
override func setMinFrameDuration(_ duration: CMTime, on captureDevice: FLTCaptureDevice) {
38+
override func setMinFrameDuration(_ duration: CMTime, on captureDevice: CaptureDevice) {
3939
// No-op.
4040
}
4141

42-
override func setMaxFrameDuration(_ duration: CMTime, on captureDevice: FLTCaptureDevice) {
42+
override func setMaxFrameDuration(_ duration: CMTime, on captureDevice: CaptureDevice) {
4343
// No-op.
4444
}
4545

0 commit comments

Comments
 (0)