Skip to content

Commit

Permalink
Deprecated method "AVCaptureDevice.devices(for:" replaced to "AVCaptu…
Browse files Browse the repository at this point in the history
…reDevice.DiscoverySession".
  • Loading branch information
NikKovIos committed Jan 8, 2022
1 parent beed49d commit 7ba2661
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 51 deletions.
19 changes: 19 additions & 0 deletions Source/Helpers/Extensions/AVCaptureDevice+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,22 @@ extension AVCaptureDevice {
}
}
}

internal extension AVCaptureDevice {
class var audioCaptureDevice: AVCaptureDevice? {
let availableMicrophoneAudioDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone], mediaType: .audio, position: .unspecified).devices
return availableMicrophoneAudioDevices.first
}

/// Best available device for selected position.
class func deviceForPosition(_ p: AVCaptureDevice.Position) -> AVCaptureDevice? {
let devicesSession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTrueDepthCamera, .builtInDualCamera, .builtInWideAngleCamera], mediaType: .video, position: p)
let devices = devicesSession.devices
guard !devices.isEmpty else {
print("Don't have supported cameras for this position: \(p.rawValue)")
return nil
}

return devices.first
}
}
26 changes: 9 additions & 17 deletions Source/Helpers/Extensions/AVFoundation+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import AVFoundation

// MARK: - Global functions

func deviceForPosition(_ p: AVCaptureDevice.Position) -> AVCaptureDevice? {
for device in AVCaptureDevice.devices(for: AVMediaType.video) where device.position == p {
return device

internal func flippedDeviceInputForInput(_ input: AVCaptureDeviceInput) -> AVCaptureDeviceInput? {
let position: AVCaptureDevice.Position = (input.device.position == .front) ? .back : .front
guard let aDevice = AVCaptureDevice.deviceForPosition(position) else {
return nil
}
return nil
return try? AVCaptureDeviceInput(device: aDevice)
}

func thumbnailFromVideoPath(_ path: URL) -> UIImage {
internal func thumbnailFromVideoPath(_ path: URL) -> UIImage {
let asset = AVURLAsset(url: path, options: nil)
let gen = AVAssetImageGenerator(asset: asset)
gen.appliesPreferredTrackTransform = true
Expand All @@ -34,7 +36,7 @@ func thumbnailFromVideoPath(_ path: URL) -> UIImage {
return UIImage()
}

func setFocusPointOnDevice(device: AVCaptureDevice, point: CGPoint) {
internal func setFocusPointOnDevice(device: AVCaptureDevice, point: CGPoint) {
do {
try device.lockForConfiguration()
if device.isFocusModeSupported(AVCaptureDevice.FocusMode.autoFocus) {
Expand All @@ -51,7 +53,7 @@ func setFocusPointOnDevice(device: AVCaptureDevice, point: CGPoint) {
}
}

func setFocusPointOnCurrentDevice(_ point: CGPoint) {
internal func setFocusPointOnCurrentDevice(_ point: CGPoint) {
if let device = AVCaptureDevice.default(for: AVMediaType.video) {
do {
try device.lockForConfiguration()
Expand All @@ -69,13 +71,3 @@ func setFocusPointOnCurrentDevice(_ point: CGPoint) {
device.unlockForConfiguration()
}
}

func toggledPositionForDevice(_ device: AVCaptureDevice) -> AVCaptureDevice.Position {
return (device.position == .front) ? .back : .front
}

func flippedDeviceInputForInput(_ input: AVCaptureDeviceInput) -> AVCaptureDeviceInput? {
let p = toggledPositionForDevice(input.device)
let aDevice = deviceForPosition(p)
return try? AVCaptureDeviceInput(device: aDevice!)
}
2 changes: 1 addition & 1 deletion Source/Pages/Photo/YPPhotoCaptureHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private extension YPPhotoCaptureHelper {
session.beginConfiguration()
session.sessionPreset = .photo
let cameraPosition: AVCaptureDevice.Position = YPConfig.usesFrontCamera ? .front : .back
let aDevice = deviceForPosition(cameraPosition)
let aDevice = AVCaptureDevice.deviceForPosition(cameraPosition)
if let d = aDevice {
deviceInput = try? AVCaptureDeviceInput(device: d)
}
Expand Down
69 changes: 36 additions & 33 deletions Source/Pages/Video/YPVideoCaptureHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ class YPVideoCaptureHelper: NSObject {
// MARK: - Start Camera

public func startCamera(completion: @escaping (() -> Void)) {
if !session.isRunning {
sessionQueue.async { [weak self] in
// Re-apply session preset
self?.session.sessionPreset = .photo
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
switch status {
case .notDetermined, .restricted, .denied:
self?.session.stopRunning()
case .authorized:
self?.session.startRunning()
completion()
self?.tryToSetupPreview()
@unknown default:
ypLog("unknown default reached. Check code.")
}
guard !session.isRunning else {
print("Session is already running. Returning.")
return
}

sessionQueue.async { [weak self] in
// Re-apply session preset
self?.session.sessionPreset = .photo
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
switch status {
case .notDetermined, .restricted, .denied:
self?.session.stopRunning()
case .authorized:
self?.session.startRunning()
completion()
self?.tryToSetupPreview()
@unknown default:
ypLog("unknown default reached. Check code.")
}
}
}
Expand All @@ -92,14 +95,14 @@ class YPVideoCaptureHelper: NSObject {
}

// Re Add audio recording
for device in AVCaptureDevice.devices(for: .audio) {
if let audioInput = try? AVCaptureDeviceInput(device: device) {
if strongSelf.session.canAddInput(audioInput) {
strongSelf.session.addInput(audioInput)
}
}
if let audioDevice = AVCaptureDevice.audioCaptureDevice,
let audioInput = try? AVCaptureDeviceInput(device: audioDevice),
strongSelf.session.canAddInput(audioInput) {
strongSelf.session.addInput(audioInput)
}

strongSelf.session.commitConfiguration()

DispatchQueue.main.async {
completion()
}
Expand Down Expand Up @@ -151,10 +154,12 @@ class YPVideoCaptureHelper: NSObject {
// MARK: - Stop Camera

public func stopCamera() {
if session.isRunning {
sessionQueue.async { [weak self] in
self?.session.stopRunning()
}
guard session.isRunning else {
return
}

sessionQueue.async { [weak self] in
self?.session.stopRunning()
}
}

Expand Down Expand Up @@ -205,7 +210,7 @@ class YPVideoCaptureHelper: NSObject {
private func setupCaptureSession() {
session.beginConfiguration()
let cameraPosition: AVCaptureDevice.Position = YPConfig.usesFrontCamera ? .front : .back
let aDevice = deviceForPosition(cameraPosition)
let aDevice = AVCaptureDevice.deviceForPosition(cameraPosition)

if let d = aDevice {
videoInput = try? AVCaptureDeviceInput(device: d)
Expand All @@ -217,14 +222,12 @@ class YPVideoCaptureHelper: NSObject {
}

// Add audio recording
for device in AVCaptureDevice.devices(for: .audio) {
if let audioInput = try? AVCaptureDeviceInput(device: device) {
if session.canAddInput(audioInput) {
session.addInput(audioInput)
}
}
if let audioDevice = AVCaptureDevice.audioCaptureDevice,
let audioInput = try? AVCaptureDeviceInput(device: audioDevice),
session.canAddInput(audioInput) {
session.addInput(audioInput)
}

let timeScale: Int32 = 30 // FPS
let maxDuration =
CMTimeMakeWithSeconds(self.videoRecordingTimeLimit, preferredTimescale: timeScale)
Expand Down

0 comments on commit 7ba2661

Please sign in to comment.