Skip to content

Commit

Permalink
✨ add support for bluetooth devices
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardodino committed Apr 1, 2023
1 parent eb630a4 commit 177700e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Sources/AudioInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public extension Notification.Name {

final class AudioInput {
private var callback: (Bool) -> Void
private var timer: Timer?
private var devices: Set<AudioDevice> {
didSet {
let added = devices.subtracting(oldValue)
Expand All @@ -18,10 +19,23 @@ final class AudioInput {
}
}
}
private var hasBluetooth: Bool {
didSet {
guard hasBluetooth != oldValue else { return }

timer?.invalidate()
guard hasBluetooth == true else { return }

timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
NotificationCenter.default.post(name: .deviceIsRunningSomewhereDidChange, object: nil)
}
}
}

init(_ callback: @escaping (Bool) -> Void) {
self.devices = Set()
self.callback = callback
self.hasBluetooth = false
}

private func updateDeviceList() {
Expand All @@ -30,11 +44,12 @@ final class AudioInput {
guard devices != self.devices else { return }

self.devices = devices
self.hasBluetooth = devices.contains(where: \.isBluetooh)
}

private lazy var listener = Debouncer(delay: 0.5) { [weak self] in
guard let self else { return }
self.callback(self.devices.isRunningSomewhere() ?? false)
self.callback(self.isRunningSomewhere)
}

func startListener() {
Expand All @@ -49,3 +64,16 @@ final class AudioInput {
}
}
}

extension AudioInput {
private func hasOrangeDot() -> Bool {
let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[CFString: Any]]
return (windows ?? []).contains { $0[kCGWindowName] as? String == "StatusIndicator" }
}

private var isRunningSomewhere: Bool {
// FB12081267: bluetooth input devices always report that isRunningSomewhere == false
if hasBluetooth { return hasOrangeDot() }
return devices.isRunningSomewhere() ?? false
}
}
17 changes: 17 additions & 0 deletions Sources/SFBAudioEngine/AudioDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public class AudioDevice: AudioObject {

// MARK: - Audio Device Base Properties

extension AudioDevice {
/// Returns the transport type
/// - remark: This corresponds to the property `kAudioDevicePropertyTransportType`
public func transportType() throws -> UInt32 {
return try getProperty(PropertyAddress(kAudioDevicePropertyTransportType), type: UInt32.self)
}
}

// MARK: - Audio Device Properties

extension AudioDevice {
Expand Down Expand Up @@ -109,6 +117,15 @@ extension AudioDevice {
guard let inputs else { return nil }
return Set(inputs)
}

var isBluetooh: Bool {
guard let transport = try? transportType() else { return false }
switch(transport){
case(kAudioDeviceTransportTypeBluetooth): return true
case(kAudioDeviceTransportTypeBluetoothLE): return true
default: return false
}
}
}

extension Set where Element == AudioDevice {
Expand Down

0 comments on commit 177700e

Please sign in to comment.