mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-09-09 19:31:51 +02:00
Remove DispatchQueue as that did not fix Realm crashes
This commit is contained in:
@@ -18,6 +18,8 @@ enum PlayMethod:Int {
|
||||
}
|
||||
|
||||
class AudioPlayer: NSObject {
|
||||
private let queue = DispatchQueue(label: "ABSAudioPlayerQueue")
|
||||
|
||||
// enums and @objc are not compatible
|
||||
@objc dynamic var status: Int
|
||||
@objc dynamic var rate: Float
|
||||
@@ -141,7 +143,7 @@ class AudioPlayer: NSObject {
|
||||
// Rate will be different depending on playback speed, aim for 2 observations/sec
|
||||
let seconds = 0.5 * (self.rate > 0 ? self.rate : 1.0)
|
||||
let time = CMTime(seconds: Double(seconds), preferredTimescale: timeScale)
|
||||
self.timeObserverToken = self.audioPlayer.addPeriodicTimeObserver(forInterval: time, queue: PlayerProgress.queue) { [weak self] time in
|
||||
self.timeObserverToken = self.audioPlayer.addPeriodicTimeObserver(forInterval: time, queue: queue) { [weak self] time in
|
||||
let sleepTimeStopAt = self?.sleepTimeStopAt
|
||||
Task {
|
||||
// Let the player update the current playback positions
|
||||
@@ -205,7 +207,7 @@ class AudioPlayer: NSObject {
|
||||
|
||||
private func startPausedTimer() {
|
||||
guard self.pausedTimer == nil else { return }
|
||||
PlayerProgress.queue.async {
|
||||
self.queue.async {
|
||||
self.pausedTimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { timer in
|
||||
NSLog("PAUSE TIMER: Syncing from server")
|
||||
Task { await PlayerProgress.shared.syncFromServer() }
|
||||
@@ -398,7 +400,7 @@ class AudioPlayer: NSObject {
|
||||
var times = [NSValue]()
|
||||
times.append(NSValue(time: sleepTime))
|
||||
|
||||
sleepTimeToken = self.audioPlayer.addBoundaryTimeObserver(forTimes: times, queue: PlayerProgress.queue) { [weak self] in
|
||||
sleepTimeToken = self.audioPlayer.addBoundaryTimeObserver(forTimes: times, queue: queue) { [weak self] in
|
||||
NSLog("SLEEP TIMER: Pausing audio")
|
||||
self?.pause()
|
||||
self?.removeSleepTimer()
|
||||
|
||||
@@ -11,7 +11,6 @@ import RealmSwift
|
||||
|
||||
class PlayerProgress {
|
||||
public static let shared = PlayerProgress()
|
||||
public static let queue = DispatchQueue(label: "ABSPlayerProgressQueue")
|
||||
|
||||
private static let TIME_BETWEEN_SESSION_SYNC_IN_SECONDS = 10.0
|
||||
|
||||
@@ -46,7 +45,6 @@ class PlayerProgress {
|
||||
// MARK: - SYNC LOGIC
|
||||
|
||||
private func updateLocalSessionFromPlayer(currentTime: Double, includesPlayProgress: Bool) -> PlaybackSession? {
|
||||
PlayerProgress.queue.sync {
|
||||
guard let session = PlayerHandler.getPlaybackSession() else { return nil }
|
||||
guard !currentTime.isNaN else { return nil } // Prevent bad data on player stop
|
||||
|
||||
@@ -69,10 +67,8 @@ class PlayerProgress {
|
||||
|
||||
return session.freeze()
|
||||
}
|
||||
}
|
||||
|
||||
private func updateLocalMediaProgressFromLocalSession() {
|
||||
PlayerProgress.queue.sync {
|
||||
guard let session = PlayerHandler.getPlaybackSession() else { return }
|
||||
guard session.isLocal else { return }
|
||||
|
||||
@@ -90,7 +86,6 @@ class PlayerProgress {
|
||||
// Send the local progress back to front-end
|
||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil)
|
||||
}
|
||||
}
|
||||
|
||||
private func updateAllServerSessionFromLocalSession() async {
|
||||
await withTaskGroup(of: Void.self) { [self] group in
|
||||
@@ -105,8 +100,8 @@ class PlayerProgress {
|
||||
}
|
||||
|
||||
private func updateServerSessionFromLocalSession(_ session: PlaybackSession, rateLimitSync: Bool = false) async {
|
||||
PlayerProgress.queue.sync {
|
||||
var safeToSync = true
|
||||
|
||||
guard var session = session.thaw() else { return }
|
||||
|
||||
// We need to update and check the server time in a transaction for thread-safety
|
||||
@@ -129,9 +124,8 @@ class PlayerProgress {
|
||||
session.serverUpdatedAt = nowInMilliseconds
|
||||
}
|
||||
session = session.freeze()
|
||||
guard safeToSync else { return }
|
||||
}
|
||||
|
||||
guard safeToSync else { return }
|
||||
NSLog("Sending sessionId(\(session.id)) to server")
|
||||
|
||||
var success = false
|
||||
@@ -145,13 +139,11 @@ class PlayerProgress {
|
||||
|
||||
// Remove old sessions after they synced with the server
|
||||
if success && !session.isActiveSession {
|
||||
PlayerProgress.queue.sync {
|
||||
if let session = session.thaw() {
|
||||
session.delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func updateLocalSessionFromServerMediaProgress() async {
|
||||
NSLog("updateLocalSessionFromServerMediaProgress: Checking if local media progress was updated on server")
|
||||
@@ -183,7 +175,6 @@ class PlayerProgress {
|
||||
|
||||
// Update the session, if needed
|
||||
if serverIsNewerThanLocal && currentTimeIsDifferent {
|
||||
PlayerProgress.queue.sync {
|
||||
NSLog("updateLocalSessionFromServerMediaProgress: Server has newer time than local serverLastUpdate=\(serverLastUpdate) localLastUpdate=\(localLastUpdate)")
|
||||
guard let session = session.thaw() else { return }
|
||||
session.update {
|
||||
@@ -192,7 +183,6 @@ class PlayerProgress {
|
||||
}
|
||||
NSLog("updateLocalSessionFromServerMediaProgress: Updated session currentTime newCurrentTime=\(serverCurrentTime) previousCurrentTime=\(localCurrentTime)")
|
||||
PlayerHandler.seek(amount: session.currentTime)
|
||||
}
|
||||
} else {
|
||||
NSLog("updateLocalSessionFromServerMediaProgress: Local session does not need updating; local has latest progress")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user