Skip to content

Commit

Permalink
Open source
Browse files Browse the repository at this point in the history
  • Loading branch information
tattn committed Feb 11, 2023
1 parent a408ee4 commit 45eaa86
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
35 changes: 31 additions & 4 deletions app/xcode/Sources/VCamCamera/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,33 @@

import AVFoundation
import CoreMediaIO
import VCamEntity

public enum Camera {
private static var cachedDevices: [AVCaptureDevice] = [] {
didSet {
NotificationCenter.default.post(name: .deviceWasChanged, object: nil)
}
}

public static func configure() {
let updateCache = {
Camera.enableDalDevices()
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .externalUnknown], mediaType: nil, position: .unspecified)
cachedDevices = deviceDiscoverySession.devices.filter { $0.uniqueID != "vcam-device" }
}

updateCache()

NotificationCenter.default.addObserver(forName: .AVCaptureDeviceWasConnected, object: nil, queue: .main) { _ in
updateCache()
}

NotificationCenter.default.addObserver(forName: .AVCaptureDeviceWasDisconnected, object: nil, queue: .main) { _ in
updateCache()
}
}

public static var hasCamera: Bool {
defaultCaptureDevice != nil
}
Expand All @@ -30,13 +55,15 @@ public enum Camera {
}

public static func cameras(type: AVMediaType? = .video) -> [AVCaptureDevice] {
enableDalDevices()
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera, .externalUnknown], mediaType: type, position: .unspecified)
return deviceDiscoverySession.devices.filter { $0.uniqueID != "vcam-device" }
if let type {
return cachedDevices.filter { $0.hasMediaType(type) }
} else {
return cachedDevices
}
}

public static func camera(id: String?) -> AVCaptureDevice? {
cameras().first { $0.uniqueID == id }
cachedDevices.first { $0.uniqueID == id }
}

public static func searchHighestResolutionFormat(for device: AVCaptureDevice) -> (format: AVCaptureDevice.Format, resolution: CGSize)? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import Foundation
public extension Notification.Name {
static let reloadUI = Notification.Name("vcam.reloadUI")
static let showEmojiPicker = Notification.Name("vcam.showEmojiPicker")
static let deviceWasChanged = Notification.Name("vcam.deviceWasChanged")
}

0 comments on commit 45eaa86

Please sign in to comment.