Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voice Broadcast - BugFix - send the last chunk #7002

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {
audioEngine.stop()
audioEngine.inputNode.removeTap(onBus: audioNodeBus)

resetValues()

voiceBroadcastService?.stopVoiceBroadcast(success: { [weak self] _ in
MXLog.debug("[VoiceBroadcastRecorderService] Stopped")

Expand All @@ -82,12 +80,18 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {

// Send current chunk
if self.chunkFile != nil {
self.sendChunkFile(at: self.chunkFile.url, sequence: self.chunkFileNumber)
self.sendChunkFile(at: self.chunkFile.url, sequence: self.chunkFileNumber){
self.tearDownVoiceBroadcastService()
}
} else {
self.tearDownVoiceBroadcastService()
}

self.session.tearDownVoiceBroadcastService()
}, failure: { error in
MXLog.error("[VoiceBroadcastRecorderService] Failed to stop voice broadcast", context: error)
// Discard the service on VoiceBroadcastService error. We keep the service in case of other error type
if error as? VoiceBroadcastServiceError != nil {
self.tearDownVoiceBroadcastService()
}
})
}

Expand Down Expand Up @@ -127,6 +131,12 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {
chunkFileNumber = 0
}

/// Release the service
private func tearDownVoiceBroadcastService() {
resetValues()
session.tearDownVoiceBroadcastService()
}

/// Write audio buffer to chunk file.
private func writeBuffer(_ buffer: AVAudioPCMBuffer) {
let sampleRate = buffer.format.sampleRate
Expand Down Expand Up @@ -176,9 +186,11 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {
}

/// Send chunk file to the server.
private func sendChunkFile(at url: URL, sequence: Int) {
guard let voiceBroadcastService = voiceBroadcastService else {
private func sendChunkFile(at url: URL, sequence: Int, completion: @escaping () -> Void = {}) {
phloux marked this conversation as resolved.
Show resolved Hide resolved
guard voiceBroadcastService != nil else {
// FIXME: Manage error
MXLog.debug("[VoiceBroadcastRecorderService] sendChunkFile: service is not available")
completion()
return
}

Expand All @@ -202,7 +214,10 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {
}

convertAACToM4A(at: url) { [weak self] convertedUrl in
guard let self = self else { return }
guard let self = self else {
completion()
return
}

// Delete the source file.
self.deleteRecording(at: url)
Expand All @@ -215,11 +230,13 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {
sequence: UInt(sequence)) { eventId in
MXLog.debug("[VoiceBroadcastRecorderService] Send voice broadcast chunk with success.")
self.deleteRecording(at: convertedUrl)
completion()
} failure: { error in
MXLog.error("[VoiceBroadcastRecorderService] Failed to send voice broadcast chunk.", context: error)
// Do not delete the file to be sent if request failed, the retry flow will need it
// There's no manual mechanism to clean it up afterwards but the tmp folder
// they live in will eventually be deleted by the system
completion()
}
}
}
Expand Down