Skip to content

Commit

Permalink
Add some logs to allow debugging of voice messages in rageshake
Browse files Browse the repository at this point in the history
  • Loading branch information
nimau committed Oct 26, 2023
1 parent 8a5986f commit 28bd438
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
9 changes: 5 additions & 4 deletions ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -959,20 +959,21 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
case .failure(let error):
switch error {
case .audioRecorderError(.recordPermissionNotGranted):
MXLog.info("permission to record audio has not been granted.")
state.bindings.confirmationAlertInfo = .init(id: .init(),
title: L10n.dialogPermissionMicrophoneTitleIos(InfoPlistReader.main.bundleDisplayName),
message: L10n.dialogPermissionMicrophoneDescriptionIos,
primaryButton: .init(title: L10n.commonSettings, action: { [weak self] in self?.openSystemSettings() }),
secondaryButton: .init(title: L10n.actionNotNow, role: .cancel, action: nil))
default:
MXLog.error("failed to start voice message recording: \(error)")
MXLog.error("failed to start voice message recording. \(error)")
}
}
}

private func stopRecordingVoiceMessage() async {
if case .failure(let error) = await voiceMessageRecorder.stopRecording() {
MXLog.error("failed to stop the recording", context: error)
MXLog.error("failed to stop the recording. \(error)")
return
}

Expand Down Expand Up @@ -1012,15 +1013,15 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
case .success:
await deleteCurrentVoiceMessage()
case .failure(let error):
MXLog.error("failed to send the voice message", context: error)
MXLog.error("failed to send the voice message. \(error)")
actionsSubject.send(.composer(action: .setMode(mode: .previewVoiceMessage(state: audioPlayerState, waveform: .url(recordingURL), isUploading: false))))
displayError(.alert(L10n.errorFailedUploadingVoiceMessage))
}
}

private func startPlayingRecordedVoiceMessage() async {
if case .failure(let error) = await voiceMessageRecorder.startPlayback() {
MXLog.error("failed to play recorded voice message", context: error)
MXLog.error("failed to play recorded voice message. \(error)")
}
}

Expand Down
21 changes: 14 additions & 7 deletions ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,19 @@ class AudioRecorder: NSObject, AudioRecorderProtocol, AVAudioRecorderDelegate {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
let url = URL.temporaryDirectory.appendingPathComponent("voice-message-\(recordID.identifier).m4a")
audioRecorder = try AVAudioRecorder(url: url, settings: settings)
audioRecorder?.delegate = self
audioRecorder?.isMeteringEnabled = true
audioRecorder?.record()
completion(.success(()))
let audioRecorder = try AVAudioRecorder(url: url, settings: settings)
audioRecorder.delegate = self
audioRecorder.isMeteringEnabled = true
if audioRecorder.record() {
self.audioRecorder = audioRecorder
completion(.success(()))
} else {
MXLog.error("audio recording failed to start")
completion(.failure(.recordingFailed))
}
} catch {
MXLog.error("audio recording failed: \(error)")
completion(.failure(.genericError))
MXLog.error("audio recording failed to start. \(error)")
completion(.failure(.internalError(error: error)))
}
}
}
Expand Down Expand Up @@ -187,12 +192,14 @@ class AudioRecorder: NSObject, AudioRecorderProtocol, AVAudioRecorderDelegate {
if success {
actionsSubject.send(.didStopRecording)
} else {
MXLog.error("audio recorder did finish recording with an error.")
actionsSubject.send(.didFailWithError(error: AudioRecorderError.genericError))
}
}

func audioRecorderEncodeErrorDidOccur(_ recorder: AVAudioRecorder, error: Error?) {
try? AVAudioSession.sharedInstance().setActive(false)
MXLog.error("audio recorder encode error did occur. \(error?.localizedDescription ?? "")")
actionsSubject.send(.didFailWithError(error: error ?? AudioRecorderError.genericError))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ extension AudioRecordingIdentifier {

enum AudioRecorderError: Error {
case genericError
case internalError(error: Error)
case recordPermissionNotGranted
case recordingFailed
case recordingCancelled
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class VoiceMessageCache: VoiceMessageCacheProtocol {
do {
try cacheFile(source: fileURL, destination: url, move: move)
} catch {
MXLog.error("Failed storing file in cache", context: error)
MXLog.error("Failed storing file in cache. \(error)")
return .failure(.failedStoringFileInCache)
}
return .success(url)
Expand All @@ -46,7 +46,7 @@ class VoiceMessageCache: VoiceMessageCacheProtocol {
do {
try FileManager.default.removeItem(at: temporaryFilesFolderURL)
} catch {
MXLog.error("Failed clearing cached disk files", context: error)
MXLog.error("Failed clearing cached disk files. \(error)")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class VoiceMessageRecorder: VoiceMessageRecorderProtocol {
// linearly normalized to [0, 1] (1 -> -50 dB)
waveformData = samples.map { UInt16(max(0, (1 - $0) * 1024)) }
} catch {
MXLog.error("Waveform analysis failed: \(error)")
MXLog.error("Waveform analysis failed. \(error)")
}
return .success(waveformData)
}
Expand All @@ -154,7 +154,7 @@ class VoiceMessageRecorder: VoiceMessageRecorderProtocol {
do {
size = try UInt64(FileManager.default.sizeForItem(at: oggFile))
} catch {
MXLog.error("Failed to get the recording file size", context: error)
MXLog.error("Failed to get the recording file size. \(error)")
return .failure(.failedSendingVoiceMessage)
}
let audioInfo = AudioInfo(duration: recordingDuration, size: size, mimetype: "audio/ogg")
Expand All @@ -170,7 +170,7 @@ class VoiceMessageRecorder: VoiceMessageRecorderProtocol {
try? FileManager.default.removeItem(at: oggFile)

if case .failure(let error) = result {
MXLog.error("Failed to send the voice message.", context: error)
MXLog.error("Failed to send the voice message. \(error)")
return .failure(.failedSendingVoiceMessage)
}

Expand Down

0 comments on commit 28bd438

Please sign in to comment.