AVAudioEngine - output format has 0 channels after changing device of auAudioUnit of the inputNode

I'm trying to change device of the inputNode of AVAudioEngine.

To do so, I'm calling setDeviceID on its auAudioUnit. Although this call doesn't fail, something wrong happens to the output busses. When I ask for its format, it shows a 0Hz and 0 channels format. It makes the app crash when I try to connect the node to the mainMixerNode.

Can anyone explain what's wrong with this code?

avEngine = AVAudioEngine()

print(avEngine.inputNode.auAudioUnit.inputBusses[0].format)
// <AVAudioFormat 0x1404b06e0:  2 ch,  44100 Hz, Float32, non-inter>
print(avEngine.inputNode.auAudioUnit.outputBusses[0].format)
// <AVAudioFormat 0x1404b0a60:  2 ch,  44100 Hz, Float32, inter>
        

// Now, let's change a device from headphone's mic to built-in mic.
try! avEngine.inputNode.auAudioUnit.setDeviceID(inputDevice.deviceID)
        

print(avEngine.inputNode.auAudioUnit.inputBusses[0].format)
// <AVAudioFormat 0x1404add50:  2 ch,  44100 Hz, Float32, non-inter>
print(avEngine.inputNode.auAudioUnit.outputBusses[0].format)
// <AVAudioFormat 0x1404adff0:  0 ch,      0 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved>
// !!!

        
// Interestingly, 'inputNode' shows a different format than `auAudioUnit`
print(avEngine.inputNode.inputFormat(forBus: 0))
// <AVAudioFormat 0x1404af480:  1 ch,  44100 Hz, Float32>
print(avEngine.inputNode.outputFormat(forBus: 0))
// <AVAudioFormat 0x1404ade30:  1 ch,  44100 Hz, Float32>

Edit: Further debugging revels another puzzling thing.

avEngine.inputNode.auAudioUnit == avEngine.outputNode.auAudioUnit // this is true ?!

inputNode and outputNode share the same AUAudioUnit. And its deviceID is by default set to the speakers. It's so confusing to me...why would inpudeNode's device be a speaker?

Is this for MacOS or iOS?

AVAudioEngine - output format has 0 channels after changing device of auAudioUnit of the inputNode
 
 
Q