Don't crash when recording starts with an invalid input format

AudioRecorder.startRecording crashed AVAudioEngine
(IsFormatSampleRateAndChannelCountValid) when the input node reported an
invalid format - e.g. a zero sample rate when no input route is
available. Bail out with an error before connecting nodes or installing
the tap.
This commit is contained in:
Stefan Ceriu
2026-06-22 14:33:04 +03:00
parent 1e7a9a5ae5
commit 0a3944deaa
@@ -166,7 +166,15 @@ nonisolated class AudioRecorder: AudioRecorderProtocol, @unchecked Sendable {
let inputNode = audioEngine.inputNode
let inputFormat = inputNode.outputFormat(forBus: 0)
let hardwareSampleRate = audioEngine.inputNode.outputFormat(forBus: 0).sampleRate
let hardwareSampleRate = inputFormat.sampleRate
// The input format can be invalid (e.g. a zero sample rate) when no input route is
// available, which would crash AVAudioEngine when connecting nodes or installing the tap.
guard inputFormat.sampleRate > 0, inputFormat.channelCount > 0 else {
MXLog.error("Invalid input audio format, can't record: \(inputFormat)")
completion(.failure(.unsupportedAudioFormat))
return
}
// Define a recording audio format. Force the sample rate to 48000 to ensure OGGEncoder won't crash
guard let recordingFormat = AVAudioFormat(standardFormatWithSampleRate: 48000, channels: 1) else {