From 0a3944deaa5f0b5281c5bdca7dd64a1297002b95 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Mon, 22 Jun 2026 14:33:04 +0300 Subject: [PATCH] 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. --- .../Services/Audio/Recorder/AudioRecorder.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift b/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift index f3571bf23..96ce631a9 100644 --- a/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift +++ b/ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift @@ -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 {