From 2466fc9bc5c285fb7fdeab5ff8ad2015249c7b76 Mon Sep 17 00:00:00 2001 From: jacky Date: Mon, 13 Sep 2021 10:36:00 +0900 Subject: [PATCH 1/2] trying to fix wrong duration --- Source/Engine/AudioStreamEngine.swift | 15 +++++++++++++-- Source/Model/AudioDataManager.swift | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Source/Engine/AudioStreamEngine.swift b/Source/Engine/AudioStreamEngine.swift index 1214634..e7beda9 100644 --- a/Source/Engine/AudioStreamEngine.swift +++ b/Source/Engine/AudioStreamEngine.swift @@ -235,7 +235,14 @@ class AudioStreamEngine: AudioEngine { let range = converter.pollNetworkAudioAvailabilityRange() isPlayable = (numberOfBuffersScheduledInTotal >= MIN_BUFFERS_TO_BE_PLAYABLE && range.1 > 0) && predictedStreamDuration > 0 Log.debug("loaded \(range), numberOfBuffersScheduledInTotal: \(numberOfBuffersScheduledInTotal), isPlayable: \(isPlayable)") - bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, predictedDurationToLoad: predictedStreamDuration, isPlayable: isPlayable) + + if AudioDataManager.shared.currentStreamFinished { + AudioDataManager.shared.updateDuration(d: range.1); + bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, predictedDurationToLoad: range.1, isPlayable: isPlayable) + }else { + bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, predictedDurationToLoad: predictedStreamDuration, isPlayable: isPlayable) + } + } private func updateNeedle() { @@ -258,7 +265,11 @@ class AudioStreamEngine: AudioEngine { private func updateDuration() { if let d = converter.pollPredictedDuration() { - self.predictedStreamDuration = d + if AudioDataManager.shared.currentStreamFinished { + self.predictedStreamDuration = AudioDataManager.shared.currentStreamFinishedWithDuration + }else { + self.predictedStreamDuration = d + } } } diff --git a/Source/Model/AudioDataManager.swift b/Source/Model/AudioDataManager.swift index 6b5e3a1..45c7f20 100644 --- a/Source/Model/AudioDataManager.swift +++ b/Source/Model/AudioDataManager.swift @@ -26,6 +26,8 @@ import Foundation protocol AudioDataManagable { + var currentStreamFinished: Bool { get } + var currentStreamFinishedWithDuration: Duration { get } var numberOfQueued: Int { get } var numberOfActive: Int { get } @@ -35,6 +37,7 @@ protocol AudioDataManagable { func setAllowCellularDownloadPreference(_ preference: Bool) func clear() + func updateDuration(d: Duration) //Director pattern func attach(callback: @escaping (_ id: ID, _ progress: Double)->()) @@ -52,8 +55,13 @@ protocol AudioDataManagable { } class AudioDataManager: AudioDataManagable { + var currentStreamFinishedWithDuration: Duration = 0 + var allowCellular: Bool = true + public var currentStreamFinished = false + public var totalStreamedDuration = 0 + static let shared: AudioDataManagable = AudioDataManager() // When we're streaming we want to stagger the size of data push up from disk to prevent the phone from freezing. We push up data of this chunk size every couple milliseconds. @@ -92,6 +100,10 @@ class AudioDataManager: AudioDataManagable { doneCallback: streamDoneListener) } + func updateDuration(d: Duration) { + currentStreamFinishedWithDuration = d + } + func clear() { streamingCallbacks = [] } @@ -112,6 +124,7 @@ class AudioDataManager: AudioDataManagable { // MARK:- Streaming extension AudioDataManager { func startStream(withRemoteURL url: AudioURL, callback: @escaping (StreamProgressPTO) -> ()) { + currentStreamFinished = false if let data = FileStorage.Audio.read(url.key) { let dto = StreamProgressDTO.init(progress: 1.0, data: data, totalBytesExpected: Int64(data.count)) callback(StreamProgressPTO(dto: dto)) @@ -213,11 +226,12 @@ extension AudioDataManager { globalDownloadProgressCallback(id, 1.0) } + private func streamDoneListener(id: ID, error: Error?) -> Bool { if error != nil { return false } - + currentStreamFinished = true downloadWorker.resumeAllActive() return false } From d11555b2ca6b2b338b0563a33ca1d6877df274c6 Mon Sep 17 00:00:00 2001 From: jacky Date: Tue, 14 Sep 2021 12:04:19 +0900 Subject: [PATCH 2/2] reset stream finished flag when seek --- Source/Model/AudioDataManager.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Model/AudioDataManager.swift b/Source/Model/AudioDataManager.swift index 23b214d..a4b132b 100644 --- a/Source/Model/AudioDataManager.swift +++ b/Source/Model/AudioDataManager.swift @@ -167,10 +167,12 @@ extension AudioDataManager { streamWorker.resume(withId: url.key) } func seekStream(withRemoteURL url: AudioURL, toByteOffset offset: UInt64) { + currentStreamFinished = false streamWorker.seek(withId: url.key, withByteOffset: offset) } func deleteStream(withRemoteURL url: AudioURL) { + currentStreamFinished = false streamWorker.stop(withId: url.key) streamingCallbacks.removeAll { (cb: (ID, (StreamProgressPTO) -> ())) -> Bool in return cb.0 == url.key