-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathCameraDevice.swift
338 lines (299 loc) · 13.4 KB
/
CameraDevice.swift
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
import Foundation
import AVFoundation
import SwiftSignalKit
import TelegramCore
private let defaultFPS: Double = 30.0
final class CameraDevice {
var position: Camera.Position = .back
deinit {
if let videoDevice = self.videoDevice {
self.unsubscribeFromChanges(videoDevice)
}
}
public private(set) var videoDevice: AVCaptureDevice? = nil {
didSet {
if let previousVideoDevice = oldValue {
self.unsubscribeFromChanges(previousVideoDevice)
}
self.videoDevicePromise.set(.single(self.videoDevice))
if let videoDevice = self.videoDevice {
self.subscribeForChanges(videoDevice)
}
}
}
private var videoDevicePromise = Promise<AVCaptureDevice?>()
public private(set) var audioDevice: AVCaptureDevice? = nil
func configure(for session: CameraSession, position: Camera.Position, dual: Bool, switchAudio: Bool) {
self.position = position
var selectedDevice: AVCaptureDevice?
if #available(iOS 13.0, *), position != .front && !dual {
if let device = AVCaptureDevice.default(.builtInTripleCamera, for: .video, position: position) {
selectedDevice = device
} else if let device = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: position) {
selectedDevice = device
} else if let device = AVCaptureDevice.default(.builtInDualWideCamera, for: .video, position: position) {
selectedDevice = device
} else if let device = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInTelephotoCamera], mediaType: .video, position: position).devices.first {
selectedDevice = device
}
} else {
if selectedDevice == nil {
selectedDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .builtInTelephotoCamera], mediaType: .video, position: position).devices.first
}
}
if selectedDevice == nil, #available(iOS 13.0, *) {
let allDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera, .builtInTripleCamera, .builtInTelephotoCamera, .builtInDualWideCamera, .builtInTrueDepthCamera, .builtInWideAngleCamera, .builtInUltraWideCamera], mediaType: .video, position: position).devices
Logger.shared.log("Camera", "No device selected, availabled devices: \(allDevices)")
}
self.videoDevice = selectedDevice
self.videoDevicePromise.set(.single(selectedDevice))
if switchAudio {
self.audioDevice = AVCaptureDevice.default(for: .audio)
}
}
func configureDeviceFormat(maxDimensions: CMVideoDimensions, maxFramerate: Double) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
var maxWidth: Int32 = 0
var maxHeight: Int32 = 0
var hasSecondaryZoomLevels = false
var candidates: [AVCaptureDevice.Format] = []
var photoCandidates: [AVCaptureDevice.Format] = []
outer: for format in device.formats {
if format.mediaType != .video || format.value(forKey: "isPhotoFormat") as? Bool == true {
continue
}
let dimensions = CMVideoFormatDescriptionGetDimensions(format.formatDescription)
if dimensions.width >= maxWidth && dimensions.width <= maxDimensions.width && dimensions.height >= maxHeight && dimensions.height <= maxDimensions.height {
if dimensions.width > maxWidth {
hasSecondaryZoomLevels = false
candidates.removeAll()
}
let subtype = CMFormatDescriptionGetMediaSubType(format.formatDescription)
if subtype == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange {
for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate > 60 {
continue outer
}
}
maxWidth = dimensions.width
maxHeight = dimensions.height
if #available(iOS 16.0, *), !format.secondaryNativeResolutionZoomFactors.isEmpty {
hasSecondaryZoomLevels = true
candidates.append(format)
if format.isHighPhotoQualitySupported {
photoCandidates.append(format)
}
} else if !hasSecondaryZoomLevels {
candidates.append(format)
if #available(iOS 15.0, *), format.isHighPhotoQualitySupported {
photoCandidates.append(format)
}
}
}
}
}
if !candidates.isEmpty {
var bestFormat: AVCaptureDevice.Format?
photoOuter: for format in photoCandidates {
for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate > maxFramerate {
continue photoOuter
}
bestFormat = format
}
}
if bestFormat == nil {
outer: for format in candidates {
for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate > maxFramerate {
continue outer
}
bestFormat = format
}
}
}
if bestFormat == nil {
bestFormat = candidates.last
}
device.activeFormat = bestFormat!
Logger.shared.log("Camera", "Selected format:")
Logger.shared.log("Camera", bestFormat!.description)
} else {
Logger.shared.log("Camera", "No format selected")
}
Logger.shared.log("Camera", "Available formats:")
for format in device.formats {
Logger.shared.log("Camera", format.description)
}
if let targetFPS = device.actualFPS(maxFramerate) {
device.activeVideoMinFrameDuration = targetFPS.duration
device.activeVideoMaxFrameDuration = targetFPS.duration
}
if device.isLowLightBoostSupported {
device.automaticallyEnablesLowLightBoostWhenAvailable = true
}
if device.isExposureModeSupported(.continuousAutoExposure) {
device.exposureMode = .continuousAutoExposure
}
}
}
func transaction(_ device: AVCaptureDevice, update: (AVCaptureDevice) -> Void) {
if let _ = try? device.lockForConfiguration() {
update(device)
device.unlockForConfiguration()
}
}
private func subscribeForChanges(_ device: AVCaptureDevice) {
NotificationCenter.default.addObserver(self, selector: #selector(self.subjectAreaChanged), name: Notification.Name.AVCaptureDeviceSubjectAreaDidChange, object: device)
}
private func unsubscribeFromChanges(_ device: AVCaptureDevice) {
NotificationCenter.default.removeObserver(self, name: Notification.Name.AVCaptureDeviceSubjectAreaDidChange, object: device)
}
@objc private func subjectAreaChanged() {
self.setFocusPoint(CGPoint(x: 0.5, y: 0.5), focusMode: .continuousAutoFocus, exposureMode: .continuousAutoExposure, monitorSubjectAreaChange: false)
}
var fps: Double = defaultFPS {
didSet {
guard let device = self.videoDevice, let targetFPS = device.actualFPS(Double(self.fps)) else {
return
}
self.fps = targetFPS.fps
self.transaction(device) { device in
device.activeVideoMinFrameDuration = targetFPS.duration
device.activeVideoMaxFrameDuration = targetFPS.duration
}
}
}
var isTorchAvailable: Signal<Bool, NoError> {
return self.videoDevicePromise.get()
|> mapToSignal { device -> Signal<Bool, NoError> in
return Signal { subscriber in
guard let device else {
return EmptyDisposable
}
subscriber.putNext(device.isFlashAvailable)
let observer = device.observe(\.isFlashAvailable, options: [.new], changeHandler: { device, _ in
subscriber.putNext(device.isFlashAvailable)
})
return ActionDisposable {
observer.invalidate()
}
}
|> distinctUntilChanged
}
}
var isAdjustingFocus: Signal<Bool, NoError> {
return self.videoDevicePromise.get()
|> mapToSignal { device -> Signal<Bool, NoError> in
return Signal { subscriber in
guard let device else {
return EmptyDisposable
}
subscriber.putNext(device.isAdjustingFocus)
let observer = device.observe(\.isAdjustingFocus, options: [.new], changeHandler: { device, _ in
subscriber.putNext(device.isAdjustingFocus)
})
return ActionDisposable {
observer.invalidate()
}
}
|> distinctUntilChanged
}
}
func setFocusPoint(_ point: CGPoint, focusMode: Camera.FocusMode, exposureMode: Camera.ExposureMode, monitorSubjectAreaChange: Bool) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
if device.isExposurePointOfInterestSupported && device.isExposureModeSupported(exposureMode) {
device.exposurePointOfInterest = point
device.exposureMode = exposureMode
}
if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(focusMode) {
device.focusPointOfInterest = point
device.focusMode = focusMode
}
device.isSubjectAreaChangeMonitoringEnabled = monitorSubjectAreaChange
if abs(device.exposureTargetBias) > 0.0 {
device.setExposureTargetBias(0.0)
}
}
}
func setExposureTargetBias(_ bias: Float) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
let extremum = (bias >= 0) ? device.maxExposureTargetBias : device.minExposureTargetBias;
let value = abs(bias) * extremum * 0.85
device.setExposureTargetBias(value, completionHandler: nil)
}
}
func setTorchActive(_ active: Bool) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
let torchMode: AVCaptureDevice.TorchMode = active ? .on : .off
if device.isTorchModeSupported(torchMode) {
device.torchMode = active ? .on : .off
}
}
}
func setTorchMode(_ flashMode: AVCaptureDevice.FlashMode) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
let torchMode: AVCaptureDevice.TorchMode
switch flashMode {
case .on:
torchMode = .on
case .off:
torchMode = .off
case .auto:
torchMode = .auto
@unknown default:
torchMode = .off
}
if device.isTorchModeSupported(torchMode) {
device.torchMode = torchMode
}
}
}
func setZoomLevel(_ zoomLevel: CGFloat) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
device.videoZoomFactor = max(device.neutralZoomFactor, min(10.0, device.neutralZoomFactor + zoomLevel))
}
}
func setZoomDelta(_ zoomDelta: CGFloat) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
device.videoZoomFactor = max(1.0, min(10.0, device.videoZoomFactor * zoomDelta))
}
}
func rampZoom(_ zoomLevel: CGFloat, rate: CGFloat) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
device.ramp(toVideoZoomFactor: zoomLevel, withRate: Float(rate))
}
}
func resetZoom(neutral: Bool = true) {
guard let device = self.videoDevice else {
return
}
self.transaction(device) { device in
device.videoZoomFactor = neutral ? device.neutralZoomFactor : device.minAvailableVideoZoomFactor
}
}
}