Skip to content

Commit

Permalink
Voice Broadcast - BugFix - send the last chunk (#7002)
Browse files Browse the repository at this point in the history
* Voice Broadcast - BugFix - send the last chunk with the right sequence number

- we reset now and teardown the service only after the last chunk is sent
  • Loading branch information
giomfo authored Oct 28, 2022
1 parent 64793d2 commit 58042e7
Showing 1 changed file with 25 additions and 8 deletions.
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: (() -> Void)? = nil) {
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

0 comments on commit 58042e7

Please sign in to comment.