From 07f28db9f2a2fe8d95ce83d82a7daebb66b19ded Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Tue, 23 Aug 2022 23:29:50 +0200 Subject: [PATCH] Controls updates fixes --- Model/Player/Backends/AVPlayerBackend.swift | 10 +++++++++- Model/Player/Backends/MPVBackend.swift | 4 ++-- Model/Player/Backends/PlayerBackend.swift | 1 + Model/Player/PlayerControlsModel.swift | 17 +++++++++++------ Shared/Player/Controls/TimelineView.swift | 4 ++-- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Model/Player/Backends/AVPlayerBackend.swift b/Model/Player/Backends/AVPlayerBackend.swift index 36e1f87e..a58472b6 100644 --- a/Model/Player/Backends/AVPlayerBackend.swift +++ b/Model/Player/Backends/AVPlayerBackend.swift @@ -1,6 +1,7 @@ import AVFoundation import Defaults import Foundation +import Logging import MediaPlayer #if !os(macOS) import UIKit @@ -9,6 +10,8 @@ import MediaPlayer final class AVPlayerBackend: PlayerBackend { static let assetKeysToLoad = ["tracks", "playable", "duration"] + private var logger = Logger(label: "avplayer-backend") + var model: PlayerModel! var controls: PlayerControlsModel! var playerTime: PlayerTimeModel! @@ -69,7 +72,7 @@ final class AVPlayerBackend: PlayerBackend { private var timeObserverThrottle = Throttle(interval: 2) - private var controlsUpdates = false + internal var controlsUpdates = false init(model: PlayerModel, controls: PlayerControlsModel?, playerTime: PlayerTimeModel?) { self.model = model @@ -589,6 +592,11 @@ final class AVPlayerBackend: PlayerBackend { func updateControls() {} func startControlsUpdates() { + guard model.presentingPlayer, model.controls.presentingControls, !model.controls.presentingOverlays else { + logger.info("ignored controls update start") + return + } + logger.info("starting controls updates") controlsUpdates = true } diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 040485cb..3abf1e4f 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -89,7 +89,7 @@ final class MPVBackend: PlayerBackend { private var onFileLoaded: (() -> Void)? - private var controlsUpdates = false + internal var controlsUpdates = false private var timeObserverThrottle = Throttle(interval: 2) var tracks: Int { @@ -337,7 +337,7 @@ final class MPVBackend: PlayerBackend { return } - DispatchQueue.main.async { [weak self] in + DispatchQueue.main.async(qos: .userInteractive) { [weak self] in guard let self = self else { return } diff --git a/Model/Player/Backends/PlayerBackend.swift b/Model/Player/Backends/PlayerBackend.swift index ff52a757..a203d198 100644 --- a/Model/Player/Backends/PlayerBackend.swift +++ b/Model/Player/Backends/PlayerBackend.swift @@ -20,6 +20,7 @@ protocol PlayerBackend { var playerItemDuration: CMTime? { get } var aspectRatio: Double { get } + var controlsUpdates: Bool { get } func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream? func canPlay(_ stream: Stream) -> Bool diff --git a/Model/Player/PlayerControlsModel.swift b/Model/Player/PlayerControlsModel.swift index 48bbfc84..9413a3fe 100644 --- a/Model/Player/PlayerControlsModel.swift +++ b/Model/Player/PlayerControlsModel.swift @@ -39,14 +39,20 @@ final class PlayerControlsModel: ObservableObject { func handlePresentationChange() { guard let player = player else { return } if presentingControls { - player.backend.startControlsUpdates() - resetTimer() + DispatchQueue.main.async(qos: .userInteractive) { [weak self] in + player.backend.startControlsUpdates() + self?.resetTimer() + } } else { if !player.musicMode { - player.backend.stopControlsUpdates() - removeTimer() + DispatchQueue.main.async(qos: .userInteractive) { [weak self] in + player.backend.stopControlsUpdates() + self?.removeTimer() + } } else { - presentingControls = true + DispatchQueue.main.async(qos: .userInteractive) { [weak self] in + self?.presentingControls = true + } } } } @@ -116,7 +122,6 @@ final class PlayerControlsModel: ObservableObject { timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { _ in withAnimation(PlayerControls.animation) { [weak self] in self?.presentingControls = false - self?.player.backend.stopControlsUpdates() } } } diff --git a/Shared/Player/Controls/TimelineView.swift b/Shared/Player/Controls/TimelineView.swift index e9573851..39be8ab2 100644 --- a/Shared/Player/Controls/TimelineView.swift +++ b/Shared/Player/Controls/TimelineView.swift @@ -23,9 +23,9 @@ struct TimelineView: View { @State private var size = CGSize.zero @State private var tooltipSize = CGSize.zero @State private var dragging = false { didSet { - if dragging { + if dragging, player.backend.controlsUpdates { player.backend.stopControlsUpdates() - } else { + } else if !dragging, !player.backend.controlsUpdates { player.backend.startControlsUpdates() } }}