Skip to content

Commit 0ce8793

Browse files
committed
Migrate FLTCaptureDeviceFormat class to Swift
1 parent 380b68d commit 0ce8793

File tree

14 files changed

+63
-146
lines changed

14 files changed

+63
-146
lines changed

packages/camera/camera_avfoundation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 0.9.22+6
22

3-
* Migrates `FLTCaptureConnection` class to Swift.
3+
* Migrates `FLTCaptureConnection` and `FLTCaptureDeviceFormat` classes to Swift.
44

55
## 0.9.22+5
66

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ final class CameraSettingsTests: XCTestCase {
203203
configuration.mediaSettings = settings
204204
let camera = CameraTestUtils.createTestCamera(configuration)
205205

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ enum CameraTestUtils {
3939
let frameRateRangeMock1 = MockFrameRateRange.init(minFrameRate: 3, maxFrameRate: 30)
4040

4141
let captureDeviceFormatMock1 = MockCaptureDeviceFormat()
42-
captureDeviceFormatMock1.videoSupportedFrameRateRanges = [frameRateRangeMock1]
42+
captureDeviceFormatMock1.flutterVideoSupportedFrameRateRanges = [frameRateRangeMock1]
4343

4444
let frameRateRangeMock2 = MockFrameRateRange.init(minFrameRate: 3, maxFrameRate: 60)
4545

4646
let captureDeviceFormatMock2 = MockCaptureDeviceFormat()
47-
captureDeviceFormatMock2.videoSupportedFrameRateRanges = [frameRateRangeMock2]
47+
captureDeviceFormatMock2.flutterVideoSupportedFrameRateRanges = [frameRateRangeMock2]
4848

4949
let captureDeviceMock = MockCaptureDevice()
5050
captureDeviceMock.flutterFormats = [captureDeviceFormatMock1, captureDeviceFormatMock2]
5151

52-
var currentFormat: FLTCaptureDeviceFormat = captureDeviceFormatMock1
52+
var currentFormat: CaptureDeviceFormat = captureDeviceFormatMock1
5353

5454
captureDeviceMock.activeFormatStub = { currentFormat }
5555
captureDeviceMock.setActiveFormatStub = { format in

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
/// A mock implementation of `FLTCaptureDevice` that allows mocking the class
1313
/// properties.
1414
class MockCaptureDevice: NSObject, CaptureDevice {
15-
var activeFormatStub: (() -> FLTCaptureDeviceFormat)?
16-
var setActiveFormatStub: ((FLTCaptureDeviceFormat) -> Void)?
15+
var activeFormatStub: (() -> CaptureDeviceFormat)?
16+
var setActiveFormatStub: ((CaptureDeviceFormat) -> Void)?
1717
var getTorchModeStub: (() -> AVCaptureDevice.TorchMode)?
1818
var setTorchModeStub: ((AVCaptureDevice.TorchMode) -> Void)?
1919
var isFocusModeSupportedStub: ((AVCaptureDevice.FocusMode) -> Bool)?
@@ -34,7 +34,7 @@ class MockCaptureDevice: NSObject, CaptureDevice {
3434
var position = AVCaptureDevice.Position.unspecified
3535
var deviceType = AVCaptureDevice.DeviceType.builtInWideAngleCamera
3636

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

46-
var flutterFormats: [FLTCaptureDeviceFormat] = []
46+
var flutterFormats: [CaptureDeviceFormat] = []
4747
var hasFlash = false
4848
var hasTorch = false
4949
var isTorchAvailable = false

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
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)
99
import camera_avfoundation_objc
1010
#endif
1111

12-
/// A mock implementation of `FLTCaptureDeviceFormat` that allows mocking the class
12+
/// A mock implementation of `CaptureDeviceFormat` that allows mocking the class
1313
/// properties.
14-
final class MockCaptureDeviceFormat: NSObject, FLTCaptureDeviceFormat {
14+
final class MockCaptureDeviceFormat: NSObject, CaptureDeviceFormat {
1515

1616
/// The format associated with the capture device.
17-
var format: AVCaptureDevice.Format {
18-
preconditionFailure("Attempted to access unimplemented property: format")
17+
var avFormat: AVCaptureDevice.Format {
18+
preconditionFailure("Attempted to access unimplemented property: avFormat")
1919
}
2020

2121
var _formatDescription: CMVideoFormatDescription?
@@ -26,7 +26,7 @@ final class MockCaptureDeviceFormat: NSObject, FLTCaptureDeviceFormat {
2626
}
2727

2828
/// The array of frame rate ranges supported by the video format.
29-
var videoSupportedFrameRateRanges: [FLTFrameRateRange] = []
29+
var flutterVideoSupportedFrameRateRanges: [FrameRateRange] = []
3030

3131
override init() {
3232
super.init()

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
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)
99
import camera_avfoundation_objc
1010
#endif
1111

12-
/// A mock implementation of `FLTFrameRateRange` that allows mocking the class properties.
13-
final class MockFrameRateRange: NSObject, FLTFrameRateRange {
14-
var minFrameRate: Float
15-
var maxFrameRate: Float
12+
/// A mock implementation of `FrameRateRange` that allows mocking the class properties.
13+
final class MockFrameRateRange: NSObject, FrameRateRange {
14+
var minFrameRate: Float64
15+
var maxFrameRate: Float64
1616

1717
/// Initializes a `MockFrameRateRange` with the given frame rate range.
18-
init(minFrameRate: Float, maxFrameRate: Float) {
18+
init(minFrameRate: Float64, maxFrameRate: Float64) {
1919
self.minFrameRate = minFrameRate
2020
self.maxFrameRate = maxFrameRate
2121
}

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/CaptureDevice.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ protocol CaptureDevice: NSObjectProtocol {
2727
var deviceType: AVCaptureDevice.DeviceType { get }
2828

2929
// Format/Configuration
30-
var flutterActiveFormat: FLTCaptureDeviceFormat { get set }
31-
var flutterFormats: [FLTCaptureDeviceFormat] { get }
30+
var flutterActiveFormat: CaptureDeviceFormat { get set }
31+
var flutterFormats: [CaptureDeviceFormat] { get }
3232

3333
// Flash/Torch
3434
var hasFlash: Bool { get }
@@ -91,14 +91,12 @@ protocol CaptureDeviceInputFactory: NSObjectProtocol {
9191
extension AVCaptureDevice: CaptureDevice {
9292
var avDevice: AVCaptureDevice { self }
9393

94-
var flutterActiveFormat: FLTCaptureDeviceFormat {
95-
get { FLTDefaultCaptureDeviceFormat.init(format: activeFormat) }
96-
set { activeFormat = newValue.format }
94+
var flutterActiveFormat: CaptureDeviceFormat {
95+
get { activeFormat }
96+
set { activeFormat = newValue.avFormat }
9797
}
9898

99-
var flutterFormats: [FLTCaptureDeviceFormat] {
100-
return self.formats.map { FLTDefaultCaptureDeviceFormat.init(format: $0) }
101-
}
99+
var flutterFormats: [CaptureDeviceFormat] { formats }
102100
}
103101

104102
extension AVCaptureInput: CaptureInput {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import AVFoundation
6+
7+
/// A protocol which is a direct passthrough to `AVFrameRateRange`. It exists to allow replacing
8+
/// `AVFrameRateRange` in tests as it has no public initializer.
9+
protocol FrameRateRange: NSObjectProtocol {
10+
var minFrameRate: Float64 { get }
11+
var maxFrameRate: Float64 { get }
12+
}
13+
14+
/// A protocol which is a direct passthrough to `AVCaptureDeviceFormat`. It exists to allow
15+
/// replacing `AVCaptureDeviceFormat` in tests as it has no public initializer.
16+
protocol CaptureDeviceFormat: NSObjectProtocol {
17+
/// The underlying `AVCaptureDeviceFormat` instance. This exists so that the format
18+
/// can be extracted when setting the active format on a device.
19+
var avFormat: AVCaptureDevice.Format { get }
20+
21+
var formatDescription: CMFormatDescription { get }
22+
var flutterVideoSupportedFrameRateRanges: [FrameRateRange] { get }
23+
}
24+
25+
extension AVFrameRateRange: FrameRateRange {}
26+
27+
extension AVCaptureDevice.Format: CaptureDeviceFormat {
28+
var avFormat: AVCaptureDevice.Format { self }
29+
30+
var flutterVideoSupportedFrameRateRanges: [FrameRateRange] { videoSupportedFrameRateRanges }
31+
}

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/CaptureOutput.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// found in the LICENSE file.
44

55
import AVFoundation
6-
import Foundation
76

87
// Import Objective-C part of the implementation when SwiftPM is used.
98
#if canImport(camera_avfoundation_objc)

packages/camera/camera_avfoundation/ios/camera_avfoundation/Sources/camera_avfoundation/DefaultCamera.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,11 @@ final class DefaultCamera: NSObject, Camera {
309309
/// Finds the highest available resolution in terms of pixel count for the given device.
310310
/// Preferred are formats with the same subtype as current activeFormat.
311311
private func highestResolutionFormat(forCaptureDevice captureDevice: CaptureDevice)
312-
-> FLTCaptureDeviceFormat?
312+
-> CaptureDeviceFormat?
313313
{
314314
let preferredSubType = CMFormatDescriptionGetMediaSubType(
315315
captureDevice.flutterActiveFormat.formatDescription)
316-
var bestFormat: FLTCaptureDeviceFormat? = nil
316+
var bestFormat: CaptureDeviceFormat? = nil
317317
var maxPixelCount: UInt = 0
318318
var isBestSubTypePreferred = false
319319

0 commit comments

Comments
 (0)