2022-10-17 16:23:47 +00:00
|
|
|
//
|
|
|
|
// Copyright 2022 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import SwiftUI
|
2022-10-19 12:22:23 +00:00
|
|
|
import MatrixSDK
|
2022-10-17 16:23:47 +00:00
|
|
|
|
2022-10-19 08:01:36 +00:00
|
|
|
typealias VoiceBroadcastPlaybackViewModelType = StateStoreViewModel<VoiceBroadcastPlaybackViewState, VoiceBroadcastPlaybackViewAction>
|
2022-10-17 16:23:47 +00:00
|
|
|
|
2022-10-19 13:32:03 +00:00
|
|
|
class VoiceBroadcastPlaybackViewModel: VoiceBroadcastPlaybackViewModelType, VoiceBroadcastPlaybackViewModelProtocol {
|
2022-10-19 10:56:59 +00:00
|
|
|
|
2022-10-17 16:23:47 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
2022-10-19 10:56:59 +00:00
|
|
|
private var voiceBroadcastAggregator: VoiceBroadcastAggregator
|
|
|
|
private let mediaServiceProvider: VoiceMessageMediaServiceProvider
|
|
|
|
private let cacheManager: VoiceMessageAttachmentCacheManager
|
|
|
|
private var audioPlayer: VoiceMessageAudioPlayer?
|
2022-10-17 16:23:47 +00:00
|
|
|
|
2022-10-19 21:09:48 +00:00
|
|
|
private var voiceBroadcastChunkQueue: [VoiceBroadcastChunk] = []
|
|
|
|
|
2022-10-17 16:23:47 +00:00
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
2022-10-19 13:32:03 +00:00
|
|
|
init(details: VoiceBroadcastPlaybackDetails,
|
|
|
|
mediaServiceProvider: VoiceMessageMediaServiceProvider,
|
2022-10-19 10:56:59 +00:00
|
|
|
cacheManager: VoiceMessageAttachmentCacheManager,
|
|
|
|
voiceBroadcastAggregator: VoiceBroadcastAggregator) {
|
|
|
|
self.mediaServiceProvider = mediaServiceProvider
|
|
|
|
self.cacheManager = cacheManager
|
|
|
|
self.voiceBroadcastAggregator = voiceBroadcastAggregator
|
|
|
|
|
2022-10-19 13:32:03 +00:00
|
|
|
let viewState = VoiceBroadcastPlaybackViewState(details: details,
|
|
|
|
playbackState: .stopped,
|
|
|
|
bindings: VoiceBroadcastPlaybackViewStateBindings())
|
|
|
|
super.init(initialViewState: viewState)
|
2022-10-19 10:56:59 +00:00
|
|
|
|
|
|
|
self.voiceBroadcastAggregator.delegate = self
|
2022-10-17 16:23:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 07:26:00 +00:00
|
|
|
private func release() {
|
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] release")
|
|
|
|
if let audioPlayer = audioPlayer {
|
|
|
|
audioPlayer.deregisterDelegate(self)
|
|
|
|
self.audioPlayer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:23:47 +00:00
|
|
|
// MARK: - Public
|
|
|
|
|
2022-10-19 08:01:36 +00:00
|
|
|
override func process(viewAction: VoiceBroadcastPlaybackViewAction) {
|
2022-10-18 14:36:02 +00:00
|
|
|
switch viewAction {
|
|
|
|
case .play:
|
|
|
|
play()
|
|
|
|
case .pause:
|
|
|
|
pause()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 07:26:00 +00:00
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
2022-10-18 14:36:02 +00:00
|
|
|
/// Listen voice broadcast
|
|
|
|
private func play() {
|
2022-10-19 21:09:48 +00:00
|
|
|
if voiceBroadcastAggregator.isStarted == false {
|
|
|
|
// Start the streaming by fetching broadcast chunks
|
|
|
|
// The audio player will start the automatically playback on incoming chunks
|
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] play: Start streaming")
|
|
|
|
state.playbackState = .buffering
|
|
|
|
voiceBroadcastAggregator.start()
|
|
|
|
}
|
|
|
|
else if let audioPlayer = audioPlayer {
|
2022-10-20 07:26:00 +00:00
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] play: resume")
|
2022-10-19 21:09:48 +00:00
|
|
|
audioPlayer.play()
|
|
|
|
}
|
|
|
|
else {
|
2022-10-20 07:26:00 +00:00
|
|
|
let chunks = voiceBroadcastAggregator.voiceBroadcast.chunks
|
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] play: restart from the beginning: \(chunks.count) chunks")
|
|
|
|
|
|
|
|
// Reinject all the chunck we already have
|
|
|
|
voiceBroadcastChunkQueue.append(contentsOf: chunks)
|
|
|
|
processPendingVoiceBroadcastChunks()
|
2022-10-19 21:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop voice broadcast
|
|
|
|
private func pause() {
|
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] pause")
|
2022-10-19 12:22:23 +00:00
|
|
|
|
2022-10-19 21:09:48 +00:00
|
|
|
if let audioPlayer = audioPlayer, audioPlayer.isPlaying {
|
|
|
|
audioPlayer.pause()
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
2022-10-19 21:09:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 07:26:00 +00:00
|
|
|
private func stopIfVoiceBroadcastOver() {
|
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] stopIfVoiceBroadcastOver")
|
|
|
|
|
|
|
|
// TODO: Check if the broadcast is over before stopping everything
|
|
|
|
// If not, the player should not stopped. The view state must be move to buffering
|
|
|
|
stop()
|
|
|
|
}
|
2022-10-19 21:09:48 +00:00
|
|
|
|
2022-10-20 07:26:00 +00:00
|
|
|
private func stop() {
|
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] stop")
|
|
|
|
|
|
|
|
// Objects will be released on audioPlayerDidStopPlaying
|
|
|
|
audioPlayer?.stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Voice broadcast chunks playback
|
|
|
|
|
|
|
|
private func processPendingVoiceBroadcastChunks() {
|
|
|
|
reorderPendingVoiceBroadcastChunks()
|
|
|
|
processNextVoiceBroadcastChunk()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reorderPendingVoiceBroadcastChunks() {
|
|
|
|
// Make sure we download and process check in the right order
|
|
|
|
voiceBroadcastChunkQueue = voiceBroadcastChunkQueue.sorted(by: {$0.sequence < $1.sequence})
|
|
|
|
}
|
|
|
|
|
|
|
|
private func processNextVoiceBroadcastChunk() {
|
2022-10-19 12:22:23 +00:00
|
|
|
|
2022-10-19 21:09:48 +00:00
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] processNextVoiceBroadcastChunk: \(voiceBroadcastChunkQueue.count) chunks remaining")
|
|
|
|
|
|
|
|
guard voiceBroadcastChunkQueue.count > 0 else {
|
|
|
|
// We cached all chunks. Nothing more to do
|
2022-10-19 12:22:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-20 07:26:00 +00:00
|
|
|
// TODO: Control the download rate to avoid to download all chunk in mass
|
|
|
|
// We could synchronise it with the number of chunks in the player playlist (audioPlayer.playerItems)
|
|
|
|
|
2022-10-19 21:09:48 +00:00
|
|
|
let chunk = voiceBroadcastChunkQueue.removeFirst()
|
|
|
|
|
2022-10-19 15:28:08 +00:00
|
|
|
// numberOfSamples is for the equalizer view we do not support yet
|
2022-10-19 21:09:48 +00:00
|
|
|
cacheManager.loadAttachment(chunk.attachment, numberOfSamples: 1) { [weak self] result in
|
2022-10-19 12:22:23 +00:00
|
|
|
guard let self = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-20 07:26:00 +00:00
|
|
|
// TODO: Make sure there has no new incoming chunk that should be before this attachment
|
|
|
|
// Be careful that this new chunk is not older than the chunk being played by the audio player. Else
|
|
|
|
// we will get an unexecpted rewind.
|
|
|
|
|
2022-10-19 12:22:23 +00:00
|
|
|
switch result {
|
|
|
|
case .success(let result):
|
2022-10-19 21:09:48 +00:00
|
|
|
guard result.eventIdentifier == chunk.attachment.eventId else {
|
2022-10-19 12:22:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 21:09:48 +00:00
|
|
|
if self.audioPlayer == nil {
|
|
|
|
// Init and start the player on the first chunk
|
|
|
|
let audioPlayer = self.mediaServiceProvider.audioPlayerForIdentifier(result.eventIdentifier)
|
|
|
|
audioPlayer.registerDelegate(self)
|
|
|
|
|
|
|
|
audioPlayer.loadContentFromURL(result.url, displayName: chunk.attachment.originalFileName)
|
|
|
|
audioPlayer.play()
|
|
|
|
self.audioPlayer = audioPlayer
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Append the chunk to the current playlist
|
|
|
|
self.audioPlayer?.addContentFromURL(result.url)
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:22:23 +00:00
|
|
|
case .failure (let error):
|
2022-10-19 21:09:48 +00:00
|
|
|
MXLog.error("[VoiceBroadcastPlaybackViewModel] processVoiceBroadcastChunkQueue: loadAttachment error", context: error)
|
|
|
|
if self.voiceBroadcastChunkQueue.count == 0 {
|
|
|
|
// No more chunk to try. Go to error
|
|
|
|
self.state.playbackState = .error
|
|
|
|
}
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
2022-10-19 21:09:48 +00:00
|
|
|
|
|
|
|
self.processNextVoiceBroadcastChunk()
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
2022-10-17 16:23:47 +00:00
|
|
|
}
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - TODO: VoiceBroadcastAggregatorDelegate
|
2022-10-19 10:56:59 +00:00
|
|
|
extension VoiceBroadcastPlaybackViewModel: VoiceBroadcastAggregatorDelegate {
|
|
|
|
func voiceBroadcastAggregatorDidStartLoading(_ aggregator: VoiceBroadcastAggregator) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func voiceBroadcastAggregatorDidEndLoading(_ aggregator: VoiceBroadcastAggregator) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func voiceBroadcastAggregator(_ aggregator: VoiceBroadcastAggregator, didFailWithError: Error) {
|
2022-10-19 21:09:48 +00:00
|
|
|
MXLog.error("[VoiceBroadcastPlaybackViewModel] voiceBroadcastAggregator didFailWithError:", context: didFailWithError)
|
2022-10-19 10:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 15:28:08 +00:00
|
|
|
func voiceBroadcastAggregator(_ aggregator: VoiceBroadcastAggregator, didReceiveChunk: VoiceBroadcastChunk) {
|
2022-10-19 21:09:48 +00:00
|
|
|
voiceBroadcastChunkQueue.append(didReceiveChunk)
|
2022-10-19 15:28:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 10:56:59 +00:00
|
|
|
func voiceBroadcastAggregatorDidUpdateData(_ aggregator: VoiceBroadcastAggregator) {
|
2022-10-20 07:26:00 +00:00
|
|
|
processPendingVoiceBroadcastChunks()
|
2022-10-19 10:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-19 12:22:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
// MARK: - TODO: VoiceMessageAudioPlayerDelegate
|
|
|
|
extension VoiceBroadcastPlaybackViewModel: VoiceMessageAudioPlayerDelegate {
|
|
|
|
func audioPlayerDidFinishLoading(_ audioPlayer: VoiceMessageAudioPlayer) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func audioPlayerDidStartPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
2022-10-19 13:01:43 +00:00
|
|
|
state.playbackState = .playing
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func audioPlayerDidPausePlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
2022-10-19 13:01:43 +00:00
|
|
|
state.playbackState = .paused
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func audioPlayerDidStopPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
2022-10-20 07:26:00 +00:00
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] audioPlayerDidStopPlaying")
|
2022-10-19 13:01:43 +00:00
|
|
|
state.playbackState = .stopped
|
2022-10-20 07:26:00 +00:00
|
|
|
release()
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func audioPlayer(_ audioPlayer: VoiceMessageAudioPlayer, didFailWithError error: Error) {
|
2022-10-19 13:01:43 +00:00
|
|
|
state.playbackState = .error
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func audioPlayerDidFinishPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
|
2022-10-20 07:26:00 +00:00
|
|
|
MXLog.debug("[VoiceBroadcastPlaybackViewModel] audioPlayerDidFinishPlaying: \(audioPlayer.playerItems.count)")
|
|
|
|
stopIfVoiceBroadcastOver()
|
2022-10-19 12:22:23 +00:00
|
|
|
}
|
|
|
|
}
|