-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[camera_avfoundation] Wrappers swift migration - part 1 #10119
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
// 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) | ||
|
@@ -11,11 +11,10 @@ import camera_avfoundation | |
|
||
/// Mock implementation of `FLTCaptureVideoDataOutput` protocol which allows injecting a custom | ||
/// implementation. | ||
class MockCaptureVideoDataOutput: NSObject, FLTCaptureVideoDataOutput { | ||
|
||
class MockCaptureVideoDataOutput: NSObject, CaptureVideoDataOutput { | ||
var avOutput = AVCaptureVideoDataOutput() | ||
var alwaysDiscardsLateVideoFrames = false | ||
var videoSettings: [String: Any] = [:] | ||
var videoSettings: [String: Any]! = [:] | ||
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. why change here? |
||
|
||
var connectionWithMediaTypeStub: ((AVMediaType) -> FLTCaptureConnection?)? | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 CoreMedia | ||
import Foundation | ||
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. Foundation should already be auto imported ( |
||
import UIKit | ||
|
||
// Import Objective-C part of the implementation when SwiftPM is used. | ||
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. you probably mentioned it before, but could you document both SwiftPM and non-SwiftPM (cocoapod?) behavior in the comment? |
||
#if canImport(camera_avfoundation_objc) | ||
import camera_avfoundation_objc | ||
#endif | ||
|
||
/// Factory block returning an FLTCaptureDevice. | ||
/// Used in tests to inject a device into DefaultCamera. | ||
typealias CaptureDeviceFactory = (String) -> FLTCaptureDevice | ||
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. should this be "VdieoCaptureDeviceFactory"? (see below) |
||
|
||
typealias AudioCaptureDeviceFactory = () -> FLTCaptureDevice | ||
|
||
typealias CaptureSessionFactory = () -> FLTCaptureSession | ||
|
||
typealias AssetWriterFactory = (URL, AVFileType, inout NSError?) -> FLTAssetWriter? | ||
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. can you add label so it's more readable? You may have to put an underscore there |
||
|
||
typealias InputPixelBufferAdaptorFactory = (FLTAssetWriterInput, [String: Any]?) -> | ||
FLTAssetWriterInputPixelBufferAdaptor | ||
|
||
/// Determines the video dimensions (width and height) for a given capture device format. | ||
/// Used in tests to mock CMVideoFormatDescriptionGetDimensions. | ||
typealias VideoDimensionsForFormat = (FLTCaptureDeviceFormat) -> CMVideoDimensions | ||
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. Nit: from this naming it's hard to tell it's a typealias for a closure. It sounds like a method name but its first letter is uppercase. |
||
|
||
/// A configuration object that centralizes dependencies for `DefaultCamera`. | ||
class FLTCamConfiguration { | ||
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. should we call it |
||
var mediaSettings: FCPPlatformMediaSettings | ||
var mediaSettingsWrapper: FLTCamMediaSettingsAVWrapper | ||
var captureSessionQueue: DispatchQueue | ||
var videoCaptureSession: FLTCaptureSession | ||
var audioCaptureSession: FLTCaptureSession | ||
var captureDeviceFactory: CaptureDeviceFactory | ||
let audioCaptureDeviceFactory: AudioCaptureDeviceFactory | ||
let captureDeviceInputFactory: FLTCaptureDeviceInputFactory | ||
var assetWriterFactory: AssetWriterFactory | ||
var inputPixelBufferAdaptorFactory: InputPixelBufferAdaptorFactory | ||
var videoDimensionsForFormat: VideoDimensionsForFormat | ||
var deviceOrientationProvider: FLTDeviceOrientationProviding | ||
let initialCameraName: String | ||
var orientation: UIDeviceOrientation | ||
|
||
init( | ||
mediaSettings: FCPPlatformMediaSettings, | ||
mediaSettingsWrapper: FLTCamMediaSettingsAVWrapper, | ||
captureDeviceFactory: @escaping CaptureDeviceFactory, | ||
audioCaptureDeviceFactory: @escaping AudioCaptureDeviceFactory, | ||
captureSessionFactory: @escaping CaptureSessionFactory, | ||
captureSessionQueue: DispatchQueue, | ||
captureDeviceInputFactory: FLTCaptureDeviceInputFactory, | ||
initialCameraName: String | ||
) { | ||
self.mediaSettings = mediaSettings | ||
self.mediaSettingsWrapper = mediaSettingsWrapper | ||
self.captureDeviceFactory = captureDeviceFactory | ||
self.audioCaptureDeviceFactory = audioCaptureDeviceFactory | ||
self.captureSessionQueue = captureSessionQueue | ||
self.videoCaptureSession = captureSessionFactory() | ||
self.audioCaptureSession = captureSessionFactory() | ||
self.captureDeviceInputFactory = captureDeviceInputFactory | ||
self.initialCameraName = initialCameraName | ||
self.orientation = UIDevice.current.orientation | ||
self.deviceOrientationProvider = FLTDefaultDeviceOrientationProvider() | ||
|
||
self.videoDimensionsForFormat = { format in | ||
return CMVideoFormatDescriptionGetDimensions(format.formatDescription) | ||
} | ||
|
||
self.assetWriterFactory = { url, fileType, error in | ||
return FLTDefaultAssetWriter(url: url, fileType: fileType, error: &error) | ||
} | ||
|
||
self.inputPixelBufferAdaptorFactory = { assetWriterInput, sourcePixelBufferAttributes in | ||
let adaptor = AVAssetWriterInputPixelBufferAdaptor( | ||
assetWriterInput: assetWriterInput.input, | ||
sourcePixelBufferAttributes: sourcePixelBufferAttributes | ||
) | ||
return FLTDefaultAssetWriterInputPixelBufferAdaptor(adaptor: adaptor) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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 | ||
|
||
// Import Objective-C part of the implementation when SwiftPM is used. | ||
#if canImport(camera_avfoundation_objc) | ||
import camera_avfoundation_objc | ||
#endif | ||
|
||
/// A protocol which is a direct passthrough to `AVCaptureOutput`. It exists to allow mocking | ||
/// `AVCaptureOutput` in tests. | ||
protocol CaptureOutput { | ||
/// Returns a connection with the specified media type, or nil if no such connection exists. | ||
func connection(withMediaType mediaType: AVMediaType) -> FLTCaptureConnection? | ||
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.
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. oh is it an apple api? if so, maybe |
||
} | ||
|
||
/// A protocol which is a direct passthrough to `AVCaptureVideoDataOutput`. It exists to allow | ||
/// mocking `AVCaptureVideoDataOutput` in tests. | ||
protocol CaptureVideoDataOutput: CaptureOutput { | ||
/// The underlying instance of `AVCaptureVideoDataOutput`. | ||
var avOutput: AVCaptureVideoDataOutput { get } | ||
|
||
/// Corresponds to the `alwaysDiscardsLateVideoFrames` property of `AVCaptureVideoDataOutput` | ||
var alwaysDiscardsLateVideoFrames: Bool { get set } | ||
|
||
/// Corresponds to the `videoSettings` property of `AVCaptureVideoDataOutput` | ||
var videoSettings: [String: Any]! { get set } | ||
|
||
/// Corresponds to the `setSampleBufferDelegate` method of `AVCaptureVideoDataOutput` | ||
func setSampleBufferDelegate( | ||
_ sampleBufferDelegate: AVCaptureVideoDataOutputSampleBufferDelegate?, | ||
queue sampleBufferCallbackQueue: DispatchQueue? | ||
) | ||
} | ||
|
||
extension AVCaptureVideoDataOutput: CaptureVideoDataOutput { | ||
var avOutput: AVCaptureVideoDataOutput { | ||
return self | ||
} | ||
|
||
func connection(withMediaType mediaType: AVMediaType) -> FLTCaptureConnection? { | ||
guard let connection = connection(with: mediaType) else { return nil } | ||
return FLTDefaultCaptureConnection(connection: connection) | ||
} | ||
} | ||
|
||
/// A protocol which is a direct passthrough to `AVCapturePhotoOutput`. It exists to allow mocking | ||
/// `AVCapturePhotoOutput` in tests. | ||
protocol CapturePhotoOutput: CaptureOutput { | ||
/// The underlying instance of `AVCapturePhotoOutput`. | ||
var avOutput: AVCapturePhotoOutput { get } | ||
|
||
/// Corresponds to the `availablePhotoCodecTypes` property of `AVCapturePhotoOutput` | ||
var availablePhotoCodecTypes: [AVVideoCodecType] { get } | ||
|
||
/// Corresponds to the `isHighResolutionCaptureEnabled` property of `AVCapturePhotoOutput` | ||
var isHighResolutionCaptureEnabled: Bool { get set } | ||
|
||
/// Corresponds to the `supportedFlashModes` property of `AVCapturePhotoOutput` | ||
var supportedFlashModes: [AVCaptureDevice.FlashMode] { get } | ||
|
||
/// Corresponds to the `capturePhotoWithSettings` method of `AVCapturePhotoOutput` | ||
func capturePhoto(with settings: AVCapturePhotoSettings, delegate: AVCapturePhotoCaptureDelegate) | ||
} | ||
|
||
/// Make AVCapturePhotoOutput conform to FLTCapturePhotoOutput protocol directly | ||
extension AVCapturePhotoOutput: CapturePhotoOutput { | ||
var avOutput: AVCapturePhotoOutput { | ||
return self | ||
} | ||
|
||
func connection(withMediaType mediaType: AVMediaType) -> FLTCaptureConnection? { | ||
guard let connection = connection(with: mediaType) else { return nil } | ||
return FLTDefaultCaptureConnection(connection: connection) | ||
} | ||
} |
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.
nit: just
.auto