-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathCameraController.swift
More file actions
631 lines (518 loc) · 23.6 KB
/
CameraController.swift
File metadata and controls
631 lines (518 loc) · 23.6 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//
// CameraController.swift
// Plugin
//
// Created by Ariel Hernandez Musa on 7/14/19.
// Copyright © 2019 Max Lynch. All rights reserved.
//
import AVFoundation
import UIKit
class CameraController: NSObject {
var captureSession: AVCaptureSession?
var currentCameraPosition: CameraPosition?
var frontCamera: AVCaptureDevice?
var frontCameraInput: AVCaptureDeviceInput?
var videoOutput: AVCaptureMovieFileOutput?
var photoOutput: AVCapturePhotoOutput?
var rearCamera: AVCaptureDevice?
var rearCameraInput: AVCaptureDeviceInput?
var previewLayer: AVCaptureVideoPreviewLayer?
var flashMode = AVCaptureDevice.FlashMode.off
var photoCaptureCompletionBlock: ((UIImage?, Error?) -> Void)?
var videoCaptureCompletionBlock: ((URL?, Error?) -> Void)?
var sampleBufferCaptureCompletionBlock: ((UIImage?, Error?) -> Void)?
var highResolutionOutput: Bool = false
var audioDevice: AVCaptureDevice?
var audioInput: AVCaptureDeviceInput?
var zoomFactor: CGFloat = 1.0
}
extension CameraController {
func prepare(cameraPosition: String, disableAudio: Bool, completionHandler: @escaping (Error?) -> Void) {
func createCaptureSession() {
self.captureSession = AVCaptureSession()
self.captureSession?.beginConfiguration()
}
func configureCaptureDevices() throws {
let session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
let cameras = session.devices.compactMap { $0 }
guard !cameras.isEmpty else { throw CameraControllerError.noCamerasAvailable }
for camera in cameras {
if camera.position == .front {
self.frontCamera = camera
}
if camera.position == .back {
self.rearCamera = camera
try camera.lockForConfiguration()
camera.focusMode = .continuousAutoFocus
camera.unlockForConfiguration()
}
}
if disableAudio == false {
self.audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
}
}
func configureDeviceInputs() throws {
guard let captureSession = self.captureSession else { throw CameraControllerError.captureSessionIsMissing }
if cameraPosition == "rear" {
if let rearCamera = self.rearCamera {
self.rearCameraInput = try AVCaptureDeviceInput(device: rearCamera)
if captureSession.canAddInput(self.rearCameraInput!) { captureSession.addInput(self.rearCameraInput!) }
self.currentCameraPosition = .rear
}
} else if cameraPosition == "front" {
if let frontCamera = self.frontCamera {
self.frontCameraInput = try AVCaptureDeviceInput(device: frontCamera)
if captureSession.canAddInput(self.frontCameraInput!) { captureSession.addInput(self.frontCameraInput!) } else { throw CameraControllerError.inputsAreInvalid }
self.currentCameraPosition = .front
}
} else { throw CameraControllerError.noCamerasAvailable }
// Add audio input
if disableAudio == false {
if let audioDevice = self.audioDevice {
self.audioInput = try AVCaptureDeviceInput(device: audioDevice)
if captureSession.canAddInput(self.audioInput!) {
captureSession.addInput(self.audioInput!)
} else {
throw CameraControllerError.inputsAreInvalid
}
}
}
}
func configurePhotoOutput() throws {
guard let captureSession = self.captureSession else { throw CameraControllerError.captureSessionIsMissing }
self.photoOutput = AVCapturePhotoOutput()
self.photoOutput!.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
self.photoOutput?.isHighResolutionCaptureEnabled = self.highResolutionOutput
if captureSession.canAddOutput(self.photoOutput!) { captureSession.addOutput(self.photoOutput!) }
}
func configureVideoOutput() throws {
guard let captureSession = self.captureSession else { throw CameraControllerError.captureSessionIsMissing }
self.videoOutput = AVCaptureMovieFileOutput()
if captureSession.canAddOutput(self.videoOutput!) {
captureSession.addOutput(self.videoOutput!)
} else {
throw CameraControllerError.invalidOperation
}
}
DispatchQueue(label: "prepare").async {
do {
createCaptureSession()
try configureCaptureDevices()
try configureDeviceInputs()
try configurePhotoOutput()
try configureVideoOutput()
self.captureSession?.commitConfiguration()
self.captureSession?.startRunning()
} catch {
DispatchQueue.main.async {
completionHandler(error)
}
return
}
DispatchQueue.main.async {
completionHandler(nil)
}
}
}
func resume(completionHandler: @escaping (Error?) -> Void) {
guard let captureSession = self.captureSession else {
completionHandler(CameraControllerError.captureSessionIsMissing)
return
}
DispatchQueue(label: "prepare").async {
if(!captureSession.isRunning){
captureSession.startRunning()
}
DispatchQueue.main.async {
completionHandler(nil)
}
}
}
func displayPreview(on view: UIView) throws {
guard let captureSession = self.captureSession, captureSession.isRunning else { throw CameraControllerError.captureSessionIsMissing }
self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
view.layer.insertSublayer(self.previewLayer!, at: 0)
self.previewLayer?.frame = view.frame
updateVideoOrientation()
}
func setupGestures(target: UIView, enableZoom: Bool) {
setupTapGesture(target: target, selector: #selector(handleTap(_:)), delegate: self)
if enableZoom {
setupPinchGesture(target: target, selector: #selector(handlePinch(_:)), delegate: self)
}
}
func setupTapGesture(target: UIView, selector: Selector, delegate: UIGestureRecognizerDelegate?) {
let tapGesture = UITapGestureRecognizer(target: self, action: selector)
tapGesture.delegate = delegate
target.addGestureRecognizer(tapGesture)
}
func setupPinchGesture(target: UIView, selector: Selector, delegate: UIGestureRecognizerDelegate?) {
let pinchGesture = UIPinchGestureRecognizer(target: self, action: selector)
pinchGesture.delegate = delegate
target.addGestureRecognizer(pinchGesture)
}
func updateVideoOrientation() {
assert(Thread.isMainThread) // UIApplication.statusBarOrientation requires the main thread.
let videoOrientation: AVCaptureVideoOrientation
switch UIApplication.shared.statusBarOrientation {
case .portrait:
videoOrientation = .portrait
case .landscapeLeft:
videoOrientation = .landscapeLeft
case .landscapeRight:
videoOrientation = .landscapeRight
case .portraitUpsideDown:
videoOrientation = .portraitUpsideDown
case .unknown:
fallthrough
@unknown default:
videoOrientation = .portrait
}
previewLayer?.connection?.videoOrientation = videoOrientation
//Orientation is not supported for video connections
//videoOutput?.connections.forEach { $0.videoOrientation = videoOrientation }
photoOutput?.connections.forEach { $0.videoOrientation = videoOrientation }
}
func switchCameras() throws {
guard let currentCameraPosition = currentCameraPosition, let captureSession = self.captureSession, captureSession.isRunning else { throw CameraControllerError.captureSessionIsMissing }
captureSession.beginConfiguration()
func switchToFrontCamera() throws {
guard let rearCameraInput = self.rearCameraInput, captureSession.inputs.contains(rearCameraInput),
let frontCamera = self.frontCamera else { throw CameraControllerError.invalidOperation }
self.frontCameraInput = try AVCaptureDeviceInput(device: frontCamera)
captureSession.removeInput(rearCameraInput)
if captureSession.canAddInput(self.frontCameraInput!) {
captureSession.addInput(self.frontCameraInput!)
self.currentCameraPosition = .front
} else {
throw CameraControllerError.invalidOperation
}
}
func switchToRearCamera() throws {
guard let frontCameraInput = self.frontCameraInput, captureSession.inputs.contains(frontCameraInput),
let rearCamera = self.rearCamera else { throw CameraControllerError.invalidOperation }
self.rearCameraInput = try AVCaptureDeviceInput(device: rearCamera)
captureSession.removeInput(frontCameraInput)
if captureSession.canAddInput(self.rearCameraInput!) {
captureSession.addInput(self.rearCameraInput!)
self.currentCameraPosition = .rear
} else { throw CameraControllerError.invalidOperation }
}
switch currentCameraPosition {
case .front:
try switchToRearCamera()
case .rear:
try switchToFrontCamera()
}
captureSession.commitConfiguration()
}
func captureImage(completion: @escaping (UIImage?, Error?) -> Void) {
guard let captureSession = captureSession, captureSession.isRunning else { completion(nil, CameraControllerError.captureSessionIsMissing); return }
let settings = AVCapturePhotoSettings()
settings.flashMode = self.flashMode
settings.isHighResolutionPhotoEnabled = self.highResolutionOutput
self.photoOutput?.capturePhoto(with: settings, delegate: self)
self.photoCaptureCompletionBlock = completion
}
func captureSample(completion: @escaping (UIImage?, Error?) -> Void) {
guard let captureSession = captureSession,
captureSession.isRunning else {
completion(nil, CameraControllerError.captureSessionIsMissing)
return
}
self.sampleBufferCaptureCompletionBlock = completion
}
func getSupportedFlashModes() throws -> [String] {
var currentCamera: AVCaptureDevice?
switch currentCameraPosition {
case .front:
currentCamera = self.frontCamera!
case .rear:
currentCamera = self.rearCamera!
default: break
}
guard
let device = currentCamera
else {
throw CameraControllerError.noCamerasAvailable
}
var supportedFlashModesAsStrings: [String] = []
if device.hasFlash {
guard let supportedFlashModes: [AVCaptureDevice.FlashMode] = self.photoOutput?.supportedFlashModes else {
throw CameraControllerError.noCamerasAvailable
}
for flashMode in supportedFlashModes {
var flashModeValue: String?
switch flashMode {
case AVCaptureDevice.FlashMode.off:
flashModeValue = "off"
case AVCaptureDevice.FlashMode.on:
flashModeValue = "on"
case AVCaptureDevice.FlashMode.auto:
flashModeValue = "auto"
default: break
}
if flashModeValue != nil {
supportedFlashModesAsStrings.append(flashModeValue!)
}
}
}
if device.hasTorch {
supportedFlashModesAsStrings.append("torch")
}
return supportedFlashModesAsStrings
}
func setFlashMode(flashMode: AVCaptureDevice.FlashMode) throws {
var currentCamera: AVCaptureDevice?
switch currentCameraPosition {
case .front:
currentCamera = self.frontCamera!
case .rear:
currentCamera = self.rearCamera!
default: break
}
guard let device = currentCamera else {
throw CameraControllerError.noCamerasAvailable
}
guard let supportedFlashModes: [AVCaptureDevice.FlashMode] = self.photoOutput?.supportedFlashModes else {
throw CameraControllerError.invalidOperation
}
if supportedFlashModes.contains(flashMode) {
do {
try device.lockForConfiguration()
if device.hasTorch && device.isTorchAvailable && device.torchMode == AVCaptureDevice.TorchMode.on {
device.torchMode = AVCaptureDevice.TorchMode.off
}
self.flashMode = flashMode
let photoSettings = AVCapturePhotoSettings()
photoSettings.flashMode = flashMode
self.photoOutput?.photoSettingsForSceneMonitoring = photoSettings
device.unlockForConfiguration()
} catch {
throw CameraControllerError.invalidOperation
}
} else {
throw CameraControllerError.invalidOperation
}
}
func setTorchMode() throws {
var currentCamera: AVCaptureDevice?
switch currentCameraPosition {
case .front:
currentCamera = self.frontCamera!
case .rear:
currentCamera = self.rearCamera!
default: break
}
guard
let device = currentCamera,
device.hasTorch,
device.isTorchAvailable
else {
throw CameraControllerError.invalidOperation
}
do {
try device.lockForConfiguration()
if device.isTorchModeSupported(AVCaptureDevice.TorchMode.on) {
device.torchMode = AVCaptureDevice.TorchMode.on
} else if device.isTorchModeSupported(AVCaptureDevice.TorchMode.auto) {
device.torchMode = AVCaptureDevice.TorchMode.auto
} else {
device.torchMode = AVCaptureDevice.TorchMode.off
}
device.unlockForConfiguration()
} catch {
throw CameraControllerError.invalidOperation
}
}
func captureVideo(mirror: Bool = false, completion: @escaping (Error?) -> Void) {
guard let captureSession = self.captureSession, captureSession.isRunning else {
completion(CameraControllerError.captureSessionIsMissing)
return
}
let path = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let identifier = UUID()
let randomIdentifier = identifier.uuidString.replacingOccurrences(of: "-", with: "")
let finalIdentifier = String(randomIdentifier.prefix(8))
let fileName="cpcp_video_"+finalIdentifier+".mp4"
let fileUrl = path.appendingPathComponent(fileName)
try? FileManager.default.removeItem(at: fileUrl)
if mirror {
if let connection = videoOutput?.connection(with: AVMediaType.video), connection.isVideoOrientationSupported {
connection.isVideoMirrored = true
} else {
completion(CameraControllerError.invalidOperation)
return
}
}
videoOutput?.movieFragmentInterval = CMTime.invalid
videoOutput?.startRecording(to: fileUrl, recordingDelegate: self)
completion(nil)
}
func stopRecording(completion: @escaping (URL?, Error?) -> Void) {
guard let captureSession = self.captureSession, captureSession.isRunning else {
completion(nil, CameraControllerError.captureSessionIsMissing)
return
}
self.videoCaptureCompletionBlock = completion
self.videoOutput?.stopRecording()
}
}
extension CameraController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc
func handleTap(_ tap: UITapGestureRecognizer) {
guard let device = self.currentCameraPosition == .rear ? rearCamera : frontCamera else { return }
let point = tap.location(in: tap.view)
let devicePoint = self.previewLayer?.captureDevicePointConverted(fromLayerPoint: point)
do {
try device.lockForConfiguration()
defer { device.unlockForConfiguration() }
let focusMode = AVCaptureDevice.FocusMode.autoFocus
if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(focusMode) {
device.focusPointOfInterest = CGPoint(x: CGFloat(devicePoint?.x ?? 0), y: CGFloat(devicePoint?.y ?? 0))
device.focusMode = focusMode
}
let exposureMode = AVCaptureDevice.ExposureMode.autoExpose
if device.isExposurePointOfInterestSupported && device.isExposureModeSupported(exposureMode) {
device.exposurePointOfInterest = CGPoint(x: CGFloat(devicePoint?.x ?? 0), y: CGFloat(devicePoint?.y ?? 0))
device.exposureMode = exposureMode
}
} catch {
debugPrint(error)
}
}
@objc
private func handlePinch(_ pinch: UIPinchGestureRecognizer) {
guard let device = self.currentCameraPosition == .rear ? rearCamera : frontCamera else { return }
func minMaxZoom(_ factor: CGFloat) -> CGFloat { return max(1.0, min(factor, device.activeFormat.videoMaxZoomFactor)) }
func update(scale factor: CGFloat) {
do {
try device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.videoZoomFactor = factor
} catch {
debugPrint(error)
}
}
switch pinch.state {
case .began: fallthrough
case .changed:
let newScaleFactor = minMaxZoom(pinch.scale * zoomFactor)
update(scale: newScaleFactor)
case .ended:
zoomFactor = device.videoZoomFactor
default: break
}
}
}
extension CameraController: AVCapturePhotoCaptureDelegate {
public func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Swift.Error?) {
if let error = error { self.photoCaptureCompletionBlock?(nil, error) } else if let buffer = photoSampleBuffer, let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: buffer, previewPhotoSampleBuffer: nil),
let image = UIImage(data: data) {
self.photoCaptureCompletionBlock?(image.fixedOrientation(), nil)
} else {
self.photoCaptureCompletionBlock?(nil, CameraControllerError.unknown)
}
}
}
enum CameraControllerError: Swift.Error {
case captureSessionAlreadyRunning
case captureSessionIsMissing
case inputsAreInvalid
case invalidOperation
case noCamerasAvailable
case unknown
}
public enum CameraPosition {
case front
case rear
}
extension CameraControllerError: LocalizedError {
public var errorDescription: String? {
switch self {
case .captureSessionAlreadyRunning:
return NSLocalizedString("Capture Session is Already Running", comment: "Capture Session Already Running")
case .captureSessionIsMissing:
return NSLocalizedString("Capture Session is Missing", comment: "Capture Session Missing")
case .inputsAreInvalid:
return NSLocalizedString("Inputs Are Invalid", comment: "Inputs Are Invalid")
case .invalidOperation:
return NSLocalizedString("Invalid Operation", comment: "invalid Operation")
case .noCamerasAvailable:
return NSLocalizedString("Failed to access device camera(s)", comment: "No Cameras Available")
case .unknown:
return NSLocalizedString("Unknown", comment: "Unknown")
}
}
}
extension UIImage {
func fixedOrientation() -> UIImage? {
guard imageOrientation != UIImage.Orientation.up else {
// This is default orientation, don't need to do anything
return self.copy() as? UIImage
}
guard let cgImage = self.cgImage else {
// CGImage is not available
return nil
}
guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
return nil // Not able to create CGContext
}
var transform: CGAffineTransform = CGAffineTransform.identity
switch imageOrientation {
case .down, .downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: CGFloat.pi)
print("down")
break
case .left, .leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.rotated(by: CGFloat.pi / 2.0)
print("left")
break
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0, y: size.height)
transform = transform.rotated(by: CGFloat.pi / -2.0)
print("right")
break
case .up, .upMirrored:
break
}
// Flip image one more time if needed to, this is to prevent flipped image
switch imageOrientation {
case .upMirrored, .downMirrored:
transform.translatedBy(x: size.width, y: 0)
transform.scaledBy(x: -1, y: 1)
break
case .leftMirrored, .rightMirrored:
transform.translatedBy(x: size.height, y: 0)
transform.scaledBy(x: -1, y: 1)
case .up, .down, .left, .right:
break
}
ctx.concatenate(transform)
switch imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
default:
ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
break
}
guard let newCGImage = ctx.makeImage() else { return nil }
return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
}
}
extension CameraController: AVCaptureFileOutputRecordingDelegate {
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if error == nil {
self.videoCaptureCompletionBlock?(outputFileURL, nil)
} else {
self.videoCaptureCompletionBlock?(nil, error)
}
}
}