Skip to content

Commit

Permalink
Incorporate experimental streaming duration fixes
Browse files Browse the repository at this point in the history
Changes from PR tanhakabir#155
tanhakabir/SwiftAudioPlayer@master...HKdAlex:SwiftAudioPlayer:master

Also update lines 123-128 of AudioStreamEngine to avoid duration being
overridden by predictedStreamDuration once stream has completed
  • Loading branch information
micahjon authored and Pietro Rea committed Dec 10, 2022
1 parent 97a9bf6 commit 294ddee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
22 changes: 18 additions & 4 deletions Source/Engine/AudioStreamEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ class AudioStreamEngine: AudioEngine {
let s = predictedStreamDurationDebounceHelper
if d/DEBOUNCING_BUFFER_TIME != s/DEBOUNCING_BUFFER_TIME {
predictedStreamDurationDebounceHelper = predictedStreamDuration
duration = predictedStreamDuration
if AudioDataManager.shared.currentStreamFinished {
duration = AudioDataManager.shared.currentStreamFinishedWithDuration
} else {
duration = predictedStreamDuration
}
}
}
}
Expand Down Expand Up @@ -245,7 +249,12 @@ 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() {
Expand All @@ -268,8 +277,13 @@ class AudioStreamEngine: AudioEngine {

private func updateDuration() {
if let d = converter.pollPredictedDuration() {
self.predictedStreamDuration = d
}
self.predictedStreamDuration = d
if AudioDataManager.shared.currentStreamFinished {
self.predictedStreamDuration = AudioDataManager.shared.currentStreamFinishedWithDuration
} else {
self.predictedStreamDuration = d
}
}
}


Expand Down
17 changes: 16 additions & 1 deletion Source/Model/AudioDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import Foundation

protocol AudioDataManagable {
var currentStreamFinished: Bool { get }
var currentStreamFinishedWithDuration: Duration { get }
var numberOfQueued: Int { get }
var numberOfActive: Int { get }

Expand All @@ -38,6 +40,7 @@ protocol AudioDataManagable {
func setDownloadDirectory(_ dir: FileManager.SearchPathDirectory)

func clear()
func updateDuration(d: Duration)

//Director pattern
func attach(callback: @escaping (_ id: ID, _ progress: Double)->())
Expand All @@ -55,8 +58,13 @@ protocol AudioDataManagable {
}

class AudioDataManager: AudioDataManagable {
var currentStreamFinishedWithDuration: Duration = 0

var allowCellular: Bool = true
var downloadDirectory: FileManager.SearchPathDirectory = .documentDirectory

public var currentStreamFinished = false
public var totalStreamedDuration = 0

static let shared: AudioDataManagable = AudioDataManager()

Expand Down Expand Up @@ -95,6 +103,10 @@ class AudioDataManager: AudioDataManagable {
progressCallback: streamProgressListener,
doneCallback: streamDoneListener)
}

func updateDuration(d: Duration) {
currentStreamFinishedWithDuration = d
}

func clear() {
streamingCallbacks = []
Expand Down Expand Up @@ -125,6 +137,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))
Expand Down Expand Up @@ -154,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
Expand Down Expand Up @@ -230,7 +245,7 @@ extension AudioDataManager {
if error != nil {
return false
}

currentStreamFinished = true
downloadWorker.resumeAllActive()
return false
}
Expand Down

0 comments on commit 294ddee

Please sign in to comment.