Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/camera_avfoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.22+7

* Migrates `FLTCaptureConnection`, `FLTCaptureDeviceFormat` and `FLTAssetWriter` classes to Swift.

## 0.9.22+6

* Updates to Pigeon 26.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private final class TestMediaSettingsAVWrapper: FLTCamMediaSettingsAVWrapper {
}

override func assetWriterAudioInput(withOutputSettings outputSettings: [String: Any]?)
-> FLTAssetWriterInput
-> AssetWriterInput
{
if let bitrate = outputSettings?[AVEncoderBitRateKey] as? Int, bitrate == testAudioBitrate {
audioSettingsExpectation.fulfill()
Expand All @@ -81,7 +81,7 @@ private final class TestMediaSettingsAVWrapper: FLTCamMediaSettingsAVWrapper {
}

override func assetWriterVideoInput(withOutputSettings outputSettings: [String: Any]?)
-> FLTAssetWriterInput
-> AssetWriterInput
{
if let compressionProperties = outputSettings?[AVVideoCompressionPropertiesKey]
as? [String: Any],
Expand All @@ -101,7 +101,7 @@ private final class TestMediaSettingsAVWrapper: FLTCamMediaSettingsAVWrapper {
return MockAssetWriterInput()
}

override func addInput(_ writerInput: FLTAssetWriterInput, to writer: FLTAssetWriter) {
override func addInput(_ writerInput: AssetWriterInput, to writer: AssetWriter) {
// No-op.
}

Expand Down Expand Up @@ -203,7 +203,7 @@ final class CameraSettingsTests: XCTestCase {
configuration.mediaSettings = settings
let camera = CameraTestUtils.createTestCamera(configuration)

let range = camera.captureDevice.flutterActiveFormat.videoSupportedFrameRateRanges[0]
let range = camera.captureDevice.flutterActiveFormat.flutterVideoSupportedFrameRateRanges[0]
XCTAssertLessThanOrEqual(range.minFrameRate, 60)
XCTAssertGreaterThanOrEqual(range.maxFrameRate, 60)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ enum CameraTestUtils {
let frameRateRangeMock1 = MockFrameRateRange.init(minFrameRate: 3, maxFrameRate: 30)

let captureDeviceFormatMock1 = MockCaptureDeviceFormat()
captureDeviceFormatMock1.videoSupportedFrameRateRanges = [frameRateRangeMock1]
captureDeviceFormatMock1.flutterVideoSupportedFrameRateRanges = [frameRateRangeMock1]

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

let captureDeviceFormatMock2 = MockCaptureDeviceFormat()
captureDeviceFormatMock2.videoSupportedFrameRateRanges = [frameRateRangeMock2]
captureDeviceFormatMock2.flutterVideoSupportedFrameRateRanges = [frameRateRangeMock2]

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

var currentFormat: FLTCaptureDeviceFormat = captureDeviceFormatMock1
var currentFormat: CaptureDeviceFormat = captureDeviceFormatMock1

captureDeviceMock.activeFormatStub = { currentFormat }
captureDeviceMock.setActiveFormatStub = { format in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import camera_avfoundation
@testable import camera_avfoundation

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

/// Mock implementation of `FLTAssetWriter` protocol which allows injecting a custom
/// Mock implementation of `AssetWriter` protocol which allows injecting a custom
/// implementation.
final class MockAssetWriter: NSObject, FLTAssetWriter {
final class MockAssetWriter: NSObject, AssetWriter {
var statusStub: (() -> AVAssetWriter.Status)?
var startWritingStub: (() -> Bool)?
var finishWritingStub: ((() -> Void) -> Void)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import camera_avfoundation
@testable import camera_avfoundation

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

/// Mock implementation of `FLTAssetWriterInput` protocol which allows injecting a custom
/// Mock implementation of `AssetWriterInput` protocol which allows injecting a custom
/// implementation.
final class MockAssetWriterInput: NSObject, FLTAssetWriterInput {
final class MockAssetWriterInput: NSObject, AssetWriterInput {
var appendStub: ((CMSampleBuffer) -> Bool)?

var input: AVAssetWriterInput {
preconditionFailure("Attempted to access unimplemented property: input")
var avInput: AVAssetWriterInput {
preconditionFailure("Attempted to access unimplemented property: avInput")
}

var expectsMediaDataInRealTime = false

var readyForMoreMediaData = false
var isReadyForMoreMediaData = false

func append(_ sampleBuffer: CMSampleBuffer) -> Bool {
return appendStub?(sampleBuffer) ?? false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import camera_avfoundation
@testable import camera_avfoundation

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

/// Mock implementation of `FLTAssetWriterInputPixelBufferAdaptor` protocol which allows injecting a custom
/// Mock implementation of `AssetWriterInputPixelBufferAdaptor` protocol which allows injecting a custom
/// implementation.
final class MockAssetWriterInputPixelBufferAdaptor: NSObject, FLTAssetWriterInputPixelBufferAdaptor
{
final class MockAssetWriterInputPixelBufferAdaptor: NSObject, AssetWriterInputPixelBufferAdaptor {
var appendStub: ((CVPixelBuffer, CMTime) -> Bool)?

func append(_ pixelBuffer: CVPixelBuffer, withPresentationTime presentationTime: CMTime) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import camera_avfoundation
@testable import camera_avfoundation

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

/// A mock implementation of `FLTCaptureConnection` that allows injecting a custom implementation.
final class MockCaptureConnection: NSObject, FLTCaptureConnection {
/// A mock implementation of `CaptureConnection` that allows injecting a custom implementation.
final class MockCaptureConnection: NSObject, CaptureConnection {
var setVideoOrientationStub: ((AVCaptureVideoOrientation) -> Void)?

var connection: AVCaptureConnection {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
/// A mock implementation of `FLTCaptureDevice` that allows mocking the class
/// properties.
class MockCaptureDevice: NSObject, CaptureDevice {
var activeFormatStub: (() -> FLTCaptureDeviceFormat)?
var setActiveFormatStub: ((FLTCaptureDeviceFormat) -> Void)?
var activeFormatStub: (() -> CaptureDeviceFormat)?
var setActiveFormatStub: ((CaptureDeviceFormat) -> Void)?
var getTorchModeStub: (() -> AVCaptureDevice.TorchMode)?
var setTorchModeStub: ((AVCaptureDevice.TorchMode) -> Void)?
var isFocusModeSupportedStub: ((AVCaptureDevice.FocusMode) -> Bool)?
Expand All @@ -34,7 +34,7 @@ class MockCaptureDevice: NSObject, CaptureDevice {
var position = AVCaptureDevice.Position.unspecified
var deviceType = AVCaptureDevice.DeviceType.builtInWideAngleCamera

var flutterActiveFormat: FLTCaptureDeviceFormat {
var flutterActiveFormat: CaptureDeviceFormat {
get {
activeFormatStub?() ?? MockCaptureDeviceFormat()
}
Expand All @@ -43,7 +43,7 @@ class MockCaptureDevice: NSObject, CaptureDevice {
}
}

var flutterFormats: [FLTCaptureDeviceFormat] = []
var flutterFormats: [CaptureDeviceFormat] = []
var hasFlash = false
var hasTorch = false
var isTorchAvailable = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import camera_avfoundation
@testable import camera_avfoundation

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

/// A mock implementation of `FLTCaptureDeviceFormat` that allows mocking the class
/// A mock implementation of `CaptureDeviceFormat` that allows mocking the class
/// properties.
final class MockCaptureDeviceFormat: NSObject, FLTCaptureDeviceFormat {
final class MockCaptureDeviceFormat: NSObject, CaptureDeviceFormat {

/// The format associated with the capture device.
var format: AVCaptureDevice.Format {
preconditionFailure("Attempted to access unimplemented property: format")
var avFormat: AVCaptureDevice.Format {
preconditionFailure("Attempted to access unimplemented property: avFormat")
}

var _formatDescription: CMVideoFormatDescription?
Expand All @@ -26,7 +26,7 @@ final class MockCaptureDeviceFormat: NSObject, FLTCaptureDeviceFormat {
}

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

override init() {
super.init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ final class MockCapturePhotoOutput: NSObject, CapturePhotoOutput {
((_ settings: AVCapturePhotoSettings, _ delegate: AVCapturePhotoCaptureDelegate) -> Void)?

// Stub that is called when the corresponding public method is called.
var connectionWithMediaTypeStub: ((_ mediaType: AVMediaType) -> FLTCaptureConnection?)?
var connectionWithMediaTypeStub: ((_ mediaType: AVMediaType) -> CaptureConnection?)?

func capturePhoto(with settings: AVCapturePhotoSettings, delegate: AVCapturePhotoCaptureDelegate)
{
capturePhotoWithSettingsStub?(settings, delegate)
}

func connection(with mediaType: AVMediaType) -> FLTCaptureConnection? {
func connection(with mediaType: AVMediaType) -> CaptureConnection? {
return connectionWithMediaTypeStub?(mediaType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class MockCaptureVideoDataOutput: NSObject, CaptureVideoDataOutput {
var alwaysDiscardsLateVideoFrames = false
var videoSettings: [String: Any]! = [:]

var connectionWithMediaTypeStub: ((AVMediaType) -> FLTCaptureConnection?)?
var connectionWithMediaTypeStub: ((AVMediaType) -> CaptureConnection?)?

func connection(with mediaType: AVMediaType) -> FLTCaptureConnection? {
func connection(with mediaType: AVMediaType) -> CaptureConnection? {
return connectionWithMediaTypeStub?(mediaType)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import camera_avfoundation
@testable import camera_avfoundation

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

/// A mock implementation of `FLTFrameRateRange` that allows mocking the class properties.
final class MockFrameRateRange: NSObject, FLTFrameRateRange {
var minFrameRate: Float
var maxFrameRate: Float
/// A mock implementation of `FrameRateRange` that allows mocking the class properties.
final class MockFrameRateRange: NSObject, FrameRateRange {
var minFrameRate: Float64
var maxFrameRate: Float64

/// Initializes a `MockFrameRateRange` with the given frame rate range.
init(minFrameRate: Float, maxFrameRate: Float) {
init(minFrameRate: Float64, maxFrameRate: Float64) {
self.minFrameRate = minFrameRate
self.maxFrameRate = maxFrameRate
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ private class FakeMediaSettingsAVWrapper: FLTCamMediaSettingsAVWrapper {
}

override func assetWriterAudioInput(withOutputSettings outputSettings: [String: Any]?)
-> FLTAssetWriterInput
-> AssetWriterInput
{
return inputMock
}

override func assetWriterVideoInput(withOutputSettings outputSettings: [String: Any]?)
-> FLTAssetWriterInput
-> AssetWriterInput
{
return inputMock
}

override func addInput(_ writerInput: FLTAssetWriterInput, to writer: FLTAssetWriter) {
override func addInput(_ writerInput: AssetWriterInput, to writer: AssetWriter) {
// No-op.
}

Expand Down Expand Up @@ -175,7 +175,7 @@ final class CameraSampleBufferTests: XCTestCase {
writtenSamples.append("video")
return true
}
inputMock.readyForMoreMediaData = true
inputMock.isReadyForMoreMediaData = true
inputMock.appendStub = { buffer in
writtenSamples.append("audio")
return true
Expand Down Expand Up @@ -222,7 +222,7 @@ final class CameraSampleBufferTests: XCTestCase {
}

var audioAppended = false
inputMock.readyForMoreMediaData = true
inputMock.isReadyForMoreMediaData = true
inputMock.appendStub = { buffer in
let sampleTime = CMSampleBufferGetPresentationTimeStamp(buffer)
XCTAssert(CMTIME_IS_NUMERIC(sampleTime))
Expand Down Expand Up @@ -262,15 +262,15 @@ final class CameraSampleBufferTests: XCTestCase {

camera.startVideoRecording(completion: { error in }, messengerForStreaming: nil)

inputMock.readyForMoreMediaData = true
inputMock.isReadyForMoreMediaData = true
sampleAppended = false
camera.captureOutput(
camera.captureVideoOutput.avOutput,
didOutput: videoSample,
from: testVideoConnection)
XCTAssertTrue(sampleAppended, "Sample was not appended.")

inputMock.readyForMoreMediaData = false
inputMock.isReadyForMoreMediaData = false
sampleAppended = false
camera.captureOutput(
camera.captureVideoOutput.avOutput,
Expand Down Expand Up @@ -327,7 +327,7 @@ final class CameraSampleBufferTests: XCTestCase {
return true
}

inputMock.readyForMoreMediaData = true
inputMock.isReadyForMoreMediaData = true

camera.startVideoRecording(completion: { error in }, messengerForStreaming: nil)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import AVFoundation
import Foundation

/// A protocol that is a direct passthrough to `AVAssetWriter`. It is used to allow for mocking
/// `AVAssetWriter` in tests.
protocol AssetWriter: NSObjectProtocol {
var status: AVAssetWriter.Status { get }
var error: Error? { get }

func startWriting() -> Bool
func finishWriting(completionHandler handler: @escaping @Sendable () -> Void)
func startSession(atSourceTime startTime: CMTime)
func add(_ input: AVAssetWriterInput)
}

/// A protocol that is a direct passthrough to `AVAssetWriterInput`. It is used to allow for mocking
/// `AVAssetWriterInput` in tests.
protocol AssetWriterInput: NSObjectProtocol {
/// The underlying `AVAssetWriterInput` instance. This exists so that the input
/// can be extracted when adding to an AVAssetWriter.
var avInput: AVAssetWriterInput { get }

var expectsMediaDataInRealTime: Bool { get set }
var isReadyForMoreMediaData: Bool { get }

func append(_ sampleBuffer: CMSampleBuffer) -> Bool
}

/// A protocol that is a direct passthrough to `AVAssetWriterInputPixelBufferAdaptor`. It is used to
/// allow for mocking `AVAssetWriterInputPixelBufferAdaptor` in tests.
protocol AssetWriterInputPixelBufferAdaptor: NSObjectProtocol {
func append(_ pixelBuffer: CVPixelBuffer, withPresentationTime presentationTime: CMTime) -> Bool
}

extension AVAssetWriter: AssetWriter {}

extension AVAssetWriterInput: AssetWriterInput {
var avInput: AVAssetWriterInput { self }
}

extension AVAssetWriterInputPixelBufferAdaptor: AssetWriterInputPixelBufferAdaptor {}
Loading