-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCameraSession.swift
More file actions
350 lines (311 loc) · 12.9 KB
/
CameraSession.swift
File metadata and controls
350 lines (311 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//
// CameraSession.swift
// VisionCamera
//
// Created by Marc Rousavy on 11.10.23.
// Copyright © 2023 mrousavy. All rights reserved.
//
import AVFoundation
import Foundation
/**
A fully-featured Camera Session supporting preview, video, photo, frame processing, and code scanning outputs.
All changes to the session have to be controlled via the `configure` function.
*/
final class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate {
// Configuration
private var isInitialized = false
var configuration: CameraConfiguration?
var currentConfigureCall: DispatchTime = .now()
// Capture Session
let captureSession = AVCaptureSession()
let audioCaptureSession = AVCaptureSession()
// Inputs & Outputs
var videoDeviceInput: AVCaptureDeviceInput?
var audioDeviceInput: AVCaptureDeviceInput?
var photoOutput: AVCapturePhotoOutput?
var videoOutput: AVCaptureVideoDataOutput?
var audioOutput: AVCaptureAudioDataOutput?
var codeScannerOutput: AVCaptureMetadataOutput?
// State
var metadataProvider = MetadataProvider()
var recordingSession: RecordingSession?
var didCancelRecording = false
var orientationManager = OrientationManager()
// Callbacks
weak var delegate: CameraSessionDelegate?
// Public accessors
var maxZoom: Double {
if let device = videoDeviceInput?.device {
return device.activeFormat.videoMaxZoomFactor
}
return 1.0
}
/**
Create a new instance of the `CameraSession`.
The `onError` callback is used for any runtime errors.
*/
override init() {
super.init()
NotificationCenter.default.addObserver(self,
selector: #selector(sessionRuntimeError),
name: .AVCaptureSessionRuntimeError,
object: captureSession)
NotificationCenter.default.addObserver(self,
selector: #selector(sessionRuntimeError),
name: .AVCaptureSessionRuntimeError,
object: audioCaptureSession)
NotificationCenter.default.addObserver(self,
selector: #selector(audioSessionInterrupted),
name: AVAudioSession.interruptionNotification,
object: AVAudioSession.sharedInstance)
}
private func initialize() {
if isInitialized {
return
}
orientationManager.delegate = self
isInitialized = true
}
deinit {
NotificationCenter.default.removeObserver(self,
name: .AVCaptureSessionRuntimeError,
object: captureSession)
NotificationCenter.default.removeObserver(self,
name: .AVCaptureSessionRuntimeError,
object: audioCaptureSession)
NotificationCenter.default.removeObserver(self,
name: AVAudioSession.interruptionNotification,
object: AVAudioSession.sharedInstance)
}
/**
Creates a PreviewView for the current Capture Session
*/
func createPreviewView(frame: CGRect) -> PreviewView {
return PreviewView(frame: frame, session: captureSession)
}
func onConfigureError(_ error: Error) {
if let error = error as? CameraError {
// It's a typed Error
delegate?.onError(error)
} else {
// It's any kind of unknown error
let cameraError = CameraError.unknown(message: error.localizedDescription)
delegate?.onError(cameraError)
}
}
/**
Update the session configuration.
Any changes in here will be re-configured only if required, and under a lock (in this case, the serial cameraQueue DispatchQueue).
The `configuration` object is a copy of the currently active configuration that can be modified by the caller in the lambda.
*/
func configure(_ lambda: @escaping (_ configuration: CameraConfiguration) throws -> Void) {
initialize()
VisionLogger.log(level: .info, message: "configure { ... }: Waiting for lock...")
let slowConfigurationWarning = DispatchWorkItem {
VisionLogger.log(level: .warning, message: "configure { ... }: is still running after 2 seconds.")
}
CameraQueues.cameraQueue.asyncAfter(deadline: .now() + .seconds(2), execute: slowConfigurationWarning)
let completionGroup = DispatchGroup()
completionGroup.enter()
completionGroup.notify(queue: CameraQueues.cameraQueue) {
slowConfigurationWarning.cancel()
VisionLogger.log(level: .info, message: "configure { ... }: completed.")
}
// Set up Camera (Video) Capture Session (on camera queue, acts like a lock)
CameraQueues.cameraQueue.async {
defer { completionGroup.leave() }
// Let caller configure a new configuration for the Camera.
let config = CameraConfiguration(copyOf: self.configuration)
do {
try lambda(config)
} catch CameraConfiguration.AbortThrow.abort {
// call has been aborted and changes shall be discarded
return
} catch {
// another error occured, possibly while trying to parse enums
self.onConfigureError(error)
return
}
let difference = CameraConfiguration.Difference(between: self.configuration, and: config)
VisionLogger.log(level: .info, message: "configure { ... }: Updating CameraSession Configuration... \(difference)")
do {
// If needed, configure the AVCaptureSession (inputs, outputs)
if difference.isSessionConfigurationDirty {
self.captureSession.beginConfiguration()
// 1. Update input device
if difference.inputChanged {
try self.configureDevice(configuration: config)
}
// 2. Update outputs
if difference.outputsChanged {
try self.configureOutputs(configuration: config)
}
// 3. Update Video Stabilization
if difference.videoStabilizationChanged {
self.configureVideoStabilization(configuration: config)
}
// 4. Update target output orientation
if difference.orientationChanged {
self.orientationManager.setTargetOutputOrientation(config.outputOrientation)
}
}
guard let device = self.videoDeviceInput?.device else {
throw CameraError.device(.noDevice)
}
// If needed, configure the AVCaptureDevice (format, zoom, low-light-boost, ..)
if difference.isDeviceConfigurationDirty {
try device.lockForConfiguration()
defer {
device.unlockForConfiguration()
}
// 5. Configure format
if difference.formatChanged {
try self.configureFormat(configuration: config, device: device)
}
// 6. After step 2. and 4., we also need to configure some output properties that depend on format.
// This needs to be done AFTER we updated the `format`, as this controls the supported properties.
if difference.outputsChanged || difference.formatChanged {
self.configureVideoOutputFormat(configuration: config)
self.configurePhotoOutputFormat(configuration: config)
}
// 7. Configure side-props (fps, lowLightBoost)
if difference.sidePropsChanged {
try self.configureSideProps(configuration: config, device: device)
}
// 8. Configure zoom
if difference.zoomChanged {
self.configureZoom(configuration: config, device: device)
}
// 9. Configure exposure bias
if difference.exposureChanged {
self.configureExposure(configuration: config, device: device)
}
}
if difference.isSessionConfigurationDirty {
// We commit the session config updates AFTER the device config,
// that way we can also batch those changes into one update instead of doing two updates.
self.captureSession.commitConfiguration()
}
// 10. Start or stop the session if needed
self.checkIsActive(configuration: config)
// 11. Enable or disable the Torch if needed (requires session to be running)
if difference.torchChanged {
try device.lockForConfiguration()
defer {
device.unlockForConfiguration()
}
try self.configureTorch(configuration: config, device: device)
}
// After configuring, set this to the new configuration.
self.configuration = config
} catch {
self.onConfigureError(error)
}
// Set up Audio Capture Session (on audio queue)
if difference.audioSessionChanged {
completionGroup.enter()
CameraQueues.audioQueue.async {
defer { completionGroup.leave() }
do {
// Lock Capture Session for configuration
VisionLogger.log(level: .info, message: "Beginning AudioSession configuration...")
self.audioCaptureSession.beginConfiguration()
try self.configureAudioSession(configuration: config)
// Unlock Capture Session again and submit configuration to Hardware
self.audioCaptureSession.commitConfiguration()
VisionLogger.log(level: .info, message: "Committed AudioSession configuration!")
} catch {
self.onConfigureError(error)
}
}
}
// Set up Location streaming (on location queue)
if difference.locationChanged {
completionGroup.enter()
CameraQueues.locationQueue.async {
defer { completionGroup.leave() }
do {
VisionLogger.log(level: .info, message: "Beginning Location Output configuration...")
try self.configureLocationOutput(configuration: config)
VisionLogger.log(level: .info, message: "Finished Location Output configuration!")
} catch {
self.onConfigureError(error)
}
}
}
}
}
/**
Starts or stops the CaptureSession if needed (`isActive`)
*/
private func checkIsActive(configuration: CameraConfiguration) {
if configuration.isActive == captureSession.isRunning {
return
}
// Start/Stop session
if configuration.isActive {
captureSession.startRunning()
delegate?.onCameraStarted()
} else {
captureSession.stopRunning()
delegate?.onCameraStopped()
}
}
final func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
switch captureOutput {
case is AVCaptureVideoDataOutput:
onVideoFrame(sampleBuffer: sampleBuffer, orientation: connection.orientation, isMirrored: connection.isVideoMirrored)
case is AVCaptureAudioDataOutput:
onAudioFrame(sampleBuffer: sampleBuffer)
default:
break
}
}
private final func onVideoFrame(sampleBuffer: CMSampleBuffer, orientation: Orientation, isMirrored: Bool) {
if let recordingSession {
do {
// Write the Video Buffer to the .mov/.mp4 file
try recordingSession.append(buffer: sampleBuffer, ofType: .video)
} catch let error as CameraError {
delegate?.onError(error)
} catch {
delegate?.onError(.capture(.unknown(message: error.localizedDescription)))
}
}
if let delegate {
// Call Frame Processor (delegate) for every Video Frame
delegate.onFrame(sampleBuffer: sampleBuffer, orientation: orientation, isMirrored: isMirrored)
}
}
private final func onAudioFrame(sampleBuffer: CMSampleBuffer) {
if let recordingSession {
do {
// Synchronize the Audio Buffer with the Video Session's time because it's two separate
// AVCaptureSessions, then write it to the .mov/.mp4 file
audioCaptureSession.synchronizeBuffer(sampleBuffer, toSession: captureSession)
try recordingSession.append(buffer: sampleBuffer, ofType: .audio)
} catch let error as CameraError {
delegate?.onError(error)
} catch {
delegate?.onError(.capture(.unknown(message: error.localizedDescription)))
}
}
}
// pragma MARK: Notifications
@objc
func sessionRuntimeError(notification: Notification) {
VisionLogger.log(level: .error, message: "Unexpected Camera Runtime Error occured!")
guard let error = notification.userInfo?[AVCaptureSessionErrorKey] as? AVError else {
return
}
// Notify consumer about runtime error
delegate?.onError(.unknown(message: error._nsError.description, cause: error._nsError))
let shouldRestart = configuration?.isActive == true
if shouldRestart {
// restart capture session after an error occured
CameraQueues.cameraQueue.async {
self.captureSession.startRunning()
}
}
}
}