Skip to content

Commit 3724746

Browse files
virtustilusnodeful
authored andcommitted
fixes bitgapp#227 - crashes and no sound after device adding and removing
1 parent 56f2c8c commit 3724746

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

native/app/Source/Application.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,28 @@ class Application {
214214
Console.log("listChanged", list)
215215

216216
if list.added.count > 0 {
217-
let added = list.added[0]
218-
selectOutput(device: added)
217+
for added in list.added {
218+
if Output.isDeviceAllowed(added) {
219+
selectOutput(device: added)
220+
break
221+
}
222+
}
219223
} else if (list.removed.count > 0) {
220-
let removed = list.removed[0]
221-
if (removed.id != selectedDevice.id) {
222-
stopEngines()
224+
225+
var currentDeviceRemoved = false
226+
for removed in list.removed {
227+
if removed.id == selectedDevice.id {
228+
currentDeviceRemoved = true
229+
break
230+
}
231+
}
232+
233+
stopEngines()
234+
if (!currentDeviceRemoved) {
235+
try! AudioDeviceEvents.recreateEventEmitters([.isAliveChanged, .volumeChanged, .nominalSampleRateChanged])
236+
self.setupDriverDeviceEvents()
223237
Utilities.delay(500) {
224-
createAudioPipeline()
238+
createAudioPipeline()
225239
}
226240
}
227241
}

native/app/Source/Audio/Outputs/Output.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import EmitterKit
1313
import AVFoundation
1414

1515
class Output {
16+
static func isDeviceAllowed(_ device: AudioDevice) -> Bool {
17+
return device.transportType != nil && Constants.SUPPORTED_TRANSPORT_TYPES.contains(device.transportType!) && !device.isInputOnlyDevice()
18+
}
19+
1620
static var allowedDevices: [AudioDevice] {
1721
return AudioDevice.allOutputDevices()
1822
.filter({ device in
@@ -21,7 +25,7 @@ class Output {
2125
return false
2226
}
2327
}
24-
return device.transportType != nil && Constants.SUPPORTED_TRANSPORT_TYPES.contains(device.transportType!)
28+
return isDeviceAllowed(device)
2529
})
2630
}
2731

native/app/Source/Extensions/AudioDevice.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ extension AudioDevice {
6161
self.setMute(newValue, channel: 0, direction: .playback)
6262
} else {
6363
Console.log(self.channels(direction: .playback).intValue)
64-
for channel in 1...self.channels(direction: .playback).intValue {
65-
self.setMute(newValue, channel: UInt32(channel), direction: .playback)
64+
let channels = self.channels(direction: .playback).intValue
65+
if channels >= 1 {
66+
for channel in 1...self.channels(direction: .playback).intValue {
67+
self.setMute(newValue, channel: UInt32(channel), direction: .playback)
68+
}
6669
}
6770
}
6871
}

0 commit comments

Comments
 (0)