2021-02-19 08:28:05 -07:00
|
|
|
//
|
|
|
|
// CameraView.swift
|
2021-06-21 14:42:46 -06:00
|
|
|
// mrousavy
|
2021-02-19 08:28:05 -07:00
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 09.11.20.
|
2021-06-01 05:07:57 -06:00
|
|
|
// Copyright © 2020 mrousavy. All rights reserved.
|
2021-02-19 08:28:05 -07:00
|
|
|
//
|
|
|
|
|
|
|
|
import AVFoundation
|
|
|
|
import Foundation
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
//
|
|
|
|
// TODOs for the CameraView which are currently too hard to implement either because of AVFoundation's limitations, or my brain capacity
|
|
|
|
//
|
|
|
|
// CameraView+RecordVideo
|
|
|
|
// TODO: Better startRecording()/stopRecording() (promise + callback, wait for TurboModules/JSI)
|
|
|
|
|
|
|
|
// CameraView+TakePhoto
|
|
|
|
// TODO: Photo HDR
|
|
|
|
|
2021-03-19 08:53:19 -06:00
|
|
|
private let propsThatRequireReconfiguration = ["cameraId",
|
|
|
|
"enableDepthData",
|
2021-06-10 05:49:34 -06:00
|
|
|
"enableHighQualityPhotos",
|
2021-03-19 08:53:19 -06:00
|
|
|
"enablePortraitEffectsMatteDelivery",
|
2021-06-07 05:08:40 -06:00
|
|
|
"photo",
|
2021-07-12 07:16:03 -06:00
|
|
|
"video",
|
2023-02-21 07:00:48 -07:00
|
|
|
"enableFrameProcessor",
|
2023-09-21 08:30:05 -06:00
|
|
|
"hdr",
|
2023-10-04 04:53:52 -06:00
|
|
|
"pixelFormat",
|
|
|
|
"codeScannerOptions"]
|
2021-03-19 08:53:19 -06:00
|
|
|
private let propsThatRequireDeviceReconfiguration = ["fps",
|
2023-08-21 04:50:14 -06:00
|
|
|
"lowLightBoost"]
|
2021-02-19 08:28:05 -07:00
|
|
|
|
2021-03-09 02:53:29 -07:00
|
|
|
// MARK: - CameraView
|
|
|
|
|
2021-03-31 07:43:29 -06:00
|
|
|
public final class CameraView: UIView {
|
|
|
|
// pragma MARK: React Properties
|
2021-02-19 08:28:05 -07:00
|
|
|
// props that require reconfiguring
|
|
|
|
@objc var cameraId: NSString?
|
|
|
|
@objc var enableDepthData = false
|
2021-06-10 05:49:34 -06:00
|
|
|
@objc var enableHighQualityPhotos: NSNumber? // nullable bool
|
2021-02-19 08:28:05 -07:00
|
|
|
@objc var enablePortraitEffectsMatteDelivery = false
|
2023-09-21 09:18:54 -06:00
|
|
|
@objc var enableBufferCompression = false
|
2021-06-07 05:08:40 -06:00
|
|
|
// use cases
|
|
|
|
@objc var photo: NSNumber? // nullable bool
|
|
|
|
@objc var video: NSNumber? // nullable bool
|
|
|
|
@objc var audio: NSNumber? // nullable bool
|
2021-07-12 07:16:03 -06:00
|
|
|
@objc var enableFrameProcessor = false
|
2023-10-04 04:53:52 -06:00
|
|
|
@objc var codeScannerOptions: NSDictionary?
|
2023-08-21 04:50:14 -06:00
|
|
|
@objc var pixelFormat: NSString?
|
2021-02-19 08:28:05 -07:00
|
|
|
// props that require format reconfiguring
|
|
|
|
@objc var format: NSDictionary?
|
|
|
|
@objc var fps: NSNumber?
|
|
|
|
@objc var hdr: NSNumber? // nullable bool
|
|
|
|
@objc var lowLightBoost: NSNumber? // nullable bool
|
2022-01-04 08:57:40 -07:00
|
|
|
@objc var orientation: NSString?
|
2021-02-19 08:28:05 -07:00
|
|
|
// other props
|
|
|
|
@objc var isActive = false
|
|
|
|
@objc var torch = "off"
|
2021-07-29 03:44:22 -06:00
|
|
|
@objc var zoom: NSNumber = 1.0 // in "factor"
|
2023-02-21 07:00:48 -07:00
|
|
|
@objc var enableFpsGraph = false
|
2021-06-03 07:42:02 -06:00
|
|
|
@objc var videoStabilizationMode: NSString?
|
2023-09-23 02:14:27 -06:00
|
|
|
@objc var resizeMode: NSString = "cover" {
|
|
|
|
didSet {
|
|
|
|
previewView.resizeMode = ResizeMode(fromTypeScriptUnion: resizeMode as String)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:28:05 -07:00
|
|
|
// events
|
|
|
|
@objc var onInitialized: RCTDirectEventBlock?
|
|
|
|
@objc var onError: RCTDirectEventBlock?
|
2021-10-11 10:27:23 -06:00
|
|
|
@objc var onViewReady: RCTDirectEventBlock?
|
2023-10-04 04:53:52 -06:00
|
|
|
@objc var onCodeScanned: RCTDirectEventBlock?
|
2021-03-31 07:43:29 -06:00
|
|
|
// zoom
|
|
|
|
@objc var enableZoomGesture = false {
|
|
|
|
didSet {
|
|
|
|
if enableZoomGesture {
|
|
|
|
addPinchGestureRecognizer()
|
|
|
|
} else {
|
|
|
|
removePinchGestureRecognizer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pragma MARK: Internal Properties
|
2023-09-01 04:58:32 -06:00
|
|
|
var isMounted = false
|
|
|
|
var isReady = false
|
2021-03-26 09:28:08 -06:00
|
|
|
// Capture Session
|
2023-09-01 04:58:32 -06:00
|
|
|
let captureSession = AVCaptureSession()
|
|
|
|
let audioCaptureSession = AVCaptureSession()
|
2023-07-20 07:30:04 -06:00
|
|
|
// Inputs & Outputs
|
2023-09-01 04:58:32 -06:00
|
|
|
var videoDeviceInput: AVCaptureDeviceInput?
|
|
|
|
var audioDeviceInput: AVCaptureDeviceInput?
|
|
|
|
var photoOutput: AVCapturePhotoOutput?
|
|
|
|
var videoOutput: AVCaptureVideoDataOutput?
|
|
|
|
var audioOutput: AVCaptureAudioDataOutput?
|
2023-07-20 07:30:04 -06:00
|
|
|
// CameraView+RecordView (+ Frame Processor)
|
2023-09-01 04:58:32 -06:00
|
|
|
var isRecording = false
|
|
|
|
var recordingSession: RecordingSession?
|
2023-07-20 07:30:04 -06:00
|
|
|
#if VISION_CAMERA_ENABLE_FRAME_PROCESSORS
|
|
|
|
@objc public var frameProcessor: FrameProcessor?
|
|
|
|
#endif
|
2021-02-19 08:28:05 -07:00
|
|
|
// CameraView+Zoom
|
2023-09-01 04:58:32 -06:00
|
|
|
var pinchGestureRecognizer: UIPinchGestureRecognizer?
|
|
|
|
var pinchScaleOffset: CGFloat = 1.0
|
2021-02-19 08:28:05 -07:00
|
|
|
|
2023-09-01 05:08:33 -06:00
|
|
|
var previewView: PreviewView
|
2023-02-21 07:00:48 -07:00
|
|
|
#if DEBUG
|
2023-09-01 04:58:32 -06:00
|
|
|
var fpsGraph: RCTFPSGraph?
|
2023-02-21 07:00:48 -07:00
|
|
|
#endif
|
|
|
|
|
2021-07-26 03:32:58 -06:00
|
|
|
/// Returns whether the AVCaptureSession is currently running (reflected by isActive)
|
2021-03-09 02:53:29 -07:00
|
|
|
var isRunning: Bool {
|
|
|
|
return captureSession.isRunning
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|
|
|
|
|
2021-03-31 07:43:29 -06:00
|
|
|
// pragma MARK: Setup
|
|
|
|
override public init(frame: CGRect) {
|
2023-09-01 07:07:16 -06:00
|
|
|
previewView = PreviewView(frame: frame, session: captureSession)
|
2021-03-31 07:43:29 -06:00
|
|
|
super.init(frame: frame)
|
|
|
|
|
2023-09-01 07:07:16 -06:00
|
|
|
addSubview(previewView)
|
|
|
|
|
2021-03-31 07:43:29 -06:00
|
|
|
NotificationCenter.default.addObserver(self,
|
|
|
|
selector: #selector(sessionRuntimeError),
|
|
|
|
name: .AVCaptureSessionRuntimeError,
|
|
|
|
object: captureSession)
|
|
|
|
NotificationCenter.default.addObserver(self,
|
2021-06-03 06:16:02 -06:00
|
|
|
selector: #selector(sessionRuntimeError),
|
|
|
|
name: .AVCaptureSessionRuntimeError,
|
|
|
|
object: audioCaptureSession)
|
2021-03-31 07:43:29 -06:00
|
|
|
NotificationCenter.default.addObserver(self,
|
|
|
|
selector: #selector(audioSessionInterrupted),
|
|
|
|
name: AVAudioSession.interruptionNotification,
|
|
|
|
object: AVAudioSession.sharedInstance)
|
|
|
|
}
|
|
|
|
|
2021-05-06 06:11:55 -06:00
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder _: NSCoder) {
|
|
|
|
fatalError("init(coder:) is not implemented.")
|
|
|
|
}
|
|
|
|
|
2021-03-31 07:43:29 -06:00
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self,
|
|
|
|
name: .AVCaptureSessionRuntimeError,
|
|
|
|
object: captureSession)
|
|
|
|
NotificationCenter.default.removeObserver(self,
|
2021-06-03 06:16:02 -06:00
|
|
|
name: .AVCaptureSessionRuntimeError,
|
|
|
|
object: audioCaptureSession)
|
2021-03-31 07:43:29 -06:00
|
|
|
NotificationCenter.default.removeObserver(self,
|
|
|
|
name: AVAudioSession.interruptionNotification,
|
|
|
|
object: AVAudioSession.sharedInstance)
|
|
|
|
}
|
|
|
|
|
2021-10-11 10:27:23 -06:00
|
|
|
override public func willMove(toSuperview newSuperview: UIView?) {
|
|
|
|
super.willMove(toSuperview: newSuperview)
|
2023-02-21 07:00:48 -07:00
|
|
|
|
|
|
|
if newSuperview != nil {
|
|
|
|
if !isMounted {
|
|
|
|
isMounted = true
|
2023-07-20 07:30:04 -06:00
|
|
|
onViewReady?(nil)
|
2021-10-11 10:27:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 07:00:48 -07:00
|
|
|
override public func layoutSubviews() {
|
2023-09-01 05:08:33 -06:00
|
|
|
previewView.frame = frame
|
|
|
|
previewView.bounds = bounds
|
2023-02-21 07:00:48 -07:00
|
|
|
}
|
|
|
|
|
2021-03-26 09:28:08 -06:00
|
|
|
// pragma MARK: Props updating
|
2021-03-31 07:43:29 -06:00
|
|
|
override public final func didSetProps(_ changedProps: [String]!) {
|
2021-03-26 09:28:08 -06:00
|
|
|
ReactLogger.log(level: .info, message: "Updating \(changedProps.count) prop(s)...")
|
|
|
|
let shouldReconfigure = changedProps.contains { propsThatRequireReconfiguration.contains($0) }
|
|
|
|
let shouldReconfigureFormat = shouldReconfigure || changedProps.contains("format")
|
|
|
|
let shouldReconfigureDevice = shouldReconfigureFormat || changedProps.contains { propsThatRequireDeviceReconfiguration.contains($0) }
|
2021-06-07 05:08:40 -06:00
|
|
|
let shouldReconfigureAudioSession = changedProps.contains("audio")
|
2021-03-26 09:28:08 -06:00
|
|
|
|
|
|
|
let willReconfigure = shouldReconfigure || shouldReconfigureFormat || shouldReconfigureDevice
|
|
|
|
|
|
|
|
let shouldCheckActive = willReconfigure || changedProps.contains("isActive") || captureSession.isRunning != isActive
|
|
|
|
let shouldUpdateTorch = willReconfigure || changedProps.contains("torch") || shouldCheckActive
|
|
|
|
let shouldUpdateZoom = willReconfigure || changedProps.contains("zoom") || shouldCheckActive
|
2021-06-03 07:42:02 -06:00
|
|
|
let shouldUpdateVideoStabilization = willReconfigure || changedProps.contains("videoStabilizationMode")
|
2023-02-21 07:00:48 -07:00
|
|
|
let shouldUpdateOrientation = willReconfigure || changedProps.contains("orientation")
|
|
|
|
|
|
|
|
if changedProps.contains("enableFpsGraph") {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.setupFpsGraph()
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 07:42:02 -06:00
|
|
|
|
|
|
|
if shouldReconfigure ||
|
2021-06-07 05:08:40 -06:00
|
|
|
shouldReconfigureAudioSession ||
|
2021-06-03 07:42:02 -06:00
|
|
|
shouldCheckActive ||
|
|
|
|
shouldUpdateTorch ||
|
|
|
|
shouldUpdateZoom ||
|
|
|
|
shouldReconfigureFormat ||
|
|
|
|
shouldReconfigureDevice ||
|
2022-01-04 08:57:40 -07:00
|
|
|
shouldUpdateVideoStabilization ||
|
|
|
|
shouldUpdateOrientation {
|
2023-07-20 07:30:04 -06:00
|
|
|
CameraQueues.cameraQueue.async {
|
|
|
|
// Video Configuration
|
2021-03-26 09:28:08 -06:00
|
|
|
if shouldReconfigure {
|
|
|
|
self.configureCaptureSession()
|
|
|
|
}
|
|
|
|
if shouldReconfigureFormat {
|
|
|
|
self.configureFormat()
|
|
|
|
}
|
|
|
|
if shouldReconfigureDevice {
|
|
|
|
self.configureDevice()
|
|
|
|
}
|
2021-06-03 07:42:02 -06:00
|
|
|
if shouldUpdateVideoStabilization, let videoStabilizationMode = self.videoStabilizationMode as String? {
|
|
|
|
self.captureSession.setVideoStabilizationMode(videoStabilizationMode)
|
|
|
|
}
|
2021-03-26 09:28:08 -06:00
|
|
|
|
|
|
|
if shouldUpdateZoom {
|
2021-07-29 03:44:22 -06:00
|
|
|
let zoomClamped = max(min(CGFloat(self.zoom.doubleValue), self.maxAvailableZoom), self.minAvailableZoom)
|
|
|
|
self.zoom(factor: zoomClamped, animated: false)
|
|
|
|
self.pinchScaleOffset = zoomClamped
|
2021-03-26 09:28:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if shouldCheckActive && self.captureSession.isRunning != self.isActive {
|
|
|
|
if self.isActive {
|
|
|
|
ReactLogger.log(level: .info, message: "Starting Session...")
|
|
|
|
self.captureSession.startRunning()
|
|
|
|
ReactLogger.log(level: .info, message: "Started Session!")
|
|
|
|
} else {
|
|
|
|
ReactLogger.log(level: .info, message: "Stopping Session...")
|
|
|
|
self.captureSession.stopRunning()
|
|
|
|
ReactLogger.log(level: .info, message: "Stopped Session!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 08:57:40 -07:00
|
|
|
if shouldUpdateOrientation {
|
|
|
|
self.updateOrientation()
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:28:08 -06:00
|
|
|
// This is a wack workaround, but if I immediately set torch mode after `startRunning()`, the session isn't quite ready yet and will ignore torch.
|
2021-06-03 07:41:43 -06:00
|
|
|
if shouldUpdateTorch {
|
2023-07-20 07:30:04 -06:00
|
|
|
CameraQueues.cameraQueue.asyncAfter(deadline: .now() + 0.1) {
|
2021-03-26 09:28:08 -06:00
|
|
|
self.setTorchMode(self.torch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 05:08:40 -06:00
|
|
|
|
|
|
|
// Audio Configuration
|
|
|
|
if shouldReconfigureAudioSession {
|
2023-07-20 07:30:04 -06:00
|
|
|
CameraQueues.audioQueue.async {
|
2021-06-07 05:08:40 -06:00
|
|
|
self.configureAudioSession()
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 09:28:08 -06:00
|
|
|
}
|
|
|
|
}
|
2023-09-01 07:07:16 -06:00
|
|
|
|
2023-09-01 05:08:33 -06:00
|
|
|
func setupFpsGraph() {
|
|
|
|
#if DEBUG
|
|
|
|
if enableFpsGraph {
|
|
|
|
if fpsGraph != nil { return }
|
|
|
|
fpsGraph = RCTFPSGraph(frame: CGRect(x: 10, y: 54, width: 75, height: 45), color: .red)
|
|
|
|
fpsGraph!.layer.zPosition = 9999.0
|
|
|
|
addSubview(fpsGraph!)
|
|
|
|
} else {
|
|
|
|
fpsGraph?.removeFromSuperview()
|
|
|
|
fpsGraph = nil
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-03-26 09:28:08 -06:00
|
|
|
|
2021-03-09 02:53:29 -07:00
|
|
|
// pragma MARK: Event Invokers
|
2023-09-01 04:58:32 -06:00
|
|
|
final func invokeOnError(_ error: CameraError, cause: NSError? = nil) {
|
2021-03-29 06:12:04 -06:00
|
|
|
ReactLogger.log(level: .error, message: "Invoking onError(): \(error.message)")
|
2021-12-10 01:44:54 -07:00
|
|
|
guard let onError = onError else { return }
|
2021-02-23 02:27:31 -07:00
|
|
|
|
2021-03-09 02:53:29 -07:00
|
|
|
var causeDictionary: [String: Any]?
|
|
|
|
if let cause = cause {
|
|
|
|
causeDictionary = [
|
|
|
|
"code": cause.code,
|
|
|
|
"domain": cause.domain,
|
2021-03-26 08:54:27 -06:00
|
|
|
"message": cause.description,
|
2021-03-09 02:53:29 -07:00
|
|
|
"details": cause.userInfo,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
onError([
|
|
|
|
"code": error.code,
|
|
|
|
"message": error.message,
|
|
|
|
"cause": causeDictionary ?? NSNull(),
|
|
|
|
])
|
|
|
|
}
|
2021-02-23 02:27:31 -07:00
|
|
|
|
2023-09-01 04:58:32 -06:00
|
|
|
final func invokeOnInitialized() {
|
2021-03-29 06:12:04 -06:00
|
|
|
ReactLogger.log(level: .info, message: "Camera initialized!")
|
2021-12-10 01:44:54 -07:00
|
|
|
guard let onInitialized = onInitialized else { return }
|
2021-03-09 02:53:29 -07:00
|
|
|
onInitialized([String: Any]())
|
|
|
|
}
|
2021-02-19 08:28:05 -07:00
|
|
|
}
|