react-native-vision-camera/package/ios/CameraDevicesManager.swift
Marc Rousavy 977b859e46
feat: New JS API for useCameraDevice and useCameraFormat and much faster getAvailableCameraDevices() (#1784)
* Update podfile

* Update useCameraFormat.ts

* Update API

* Delete FormatFilter.md

* Format CameraViewManager.m ObjC style

* Make `getAvailableCameraDevices` synchronous/blocking

* Create some docs

* fix: Fix HardwareLevel types

* fix: Use new device/format API

* Use 60 FPS format as an example

* Replace `Camera.getAvailableCameraDevices` with new `CameraDevices` API/Module

* Fix Lint

* KTLint options

* Use continuation indent of 8

* Use 2 spaces for indent

* Update .editorconfig

* Format code

* Update .editorconfig

* Format more

* Update VideoStabilizationMode.kt

* fix: Expose `CameraDevicesManager` to ObjC

* Update CameraPage.tsx

* fix: `requiresMainQueueSetup() -> false`

* Always prefer higher resolution

* Update CameraDevicesManager.swift

* Update CameraPage.tsx

* Also filter pixelFormat

* fix: Add AVFoundation import
2023-09-21 11:20:33 +02:00

84 lines
2.7 KiB
Swift

//
// CameraDevicesManager.swift
// VisionCamera
//
// Created by Marc Rousavy on 19.09.23.
// Copyright © 2023 mrousavy. All rights reserved.
//
import AVFoundation
import Foundation
@objc(CameraDevicesManager)
class CameraDevicesManager: RCTEventEmitter {
private let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: getAllDeviceTypes(),
mediaType: .video,
position: .unspecified)
private var observer: NSKeyValueObservation?
private let devicesChangedEventName = "CameraDevicesChanged"
override init() {
super.init()
observer = discoverySession.observe(\.devices) { _, _ in
self.sendEvent(withName: self.devicesChangedEventName, body: self.getDevicesJson())
}
}
override func invalidate() {
observer?.invalidate()
}
override func supportedEvents() -> [String]! {
return [devicesChangedEventName]
}
override class func requiresMainQueueSetup() -> Bool {
return false
}
override func constantsToExport() -> [AnyHashable: Any]! {
return [
"availableCameraDevices": getDevicesJson(),
]
}
private func getDevicesJson() -> [[String: Any]] {
return discoverySession.devices.map {
return [
"id": $0.uniqueID,
"devices": $0.physicalDevices.map(\.deviceType.descriptor),
"position": $0.position.descriptor,
"name": $0.localizedName,
"hasFlash": $0.hasFlash,
"hasTorch": $0.hasTorch,
"minZoom": $0.minAvailableVideoZoomFactor,
"neutralZoom": $0.neutralZoomFactor,
"maxZoom": $0.maxAvailableVideoZoomFactor,
"isMultiCam": $0.isMultiCam,
"supportsDepthCapture": false, // TODO: supportsDepthCapture
"supportsRawCapture": false, // TODO: supportsRawCapture
"supportsLowLightBoost": $0.isLowLightBoostSupported,
"supportsFocus": $0.isFocusPointOfInterestSupported,
"hardwareLevel": "full",
"sensorOrientation": "portrait", // TODO: Sensor Orientation?
"formats": $0.formats.map { format -> [String: Any] in
format.toDictionary()
},
]
}
}
private static func getAllDeviceTypes() -> [AVCaptureDevice.DeviceType] {
var deviceTypes: [AVCaptureDevice.DeviceType] = []
if #available(iOS 13.0, *) {
deviceTypes.append(.builtInTripleCamera)
deviceTypes.append(.builtInDualWideCamera)
deviceTypes.append(.builtInUltraWideCamera)
}
deviceTypes.append(.builtInDualCamera)
deviceTypes.append(.builtInWideAngleCamera)
deviceTypes.append(.builtInTelephotoCamera)
return deviceTypes
}
}