From 0180d13d4ac825ab1b106effded6856b06d8e6b3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sat, 13 Aug 2022 16:11:07 +0200 Subject: [PATCH] Improve AVPlayer performance Fix updating aspect ratio Fix #170 --- Model/Player/Backends/AVPlayerBackend.swift | 33 +++++++++++++++------ Model/Player/Backends/MPVBackend.swift | 2 +- Model/Player/Backends/PlayerBackend.swift | 6 ---- Model/Player/PlayerModel.swift | 10 +++++++ 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Model/Player/Backends/AVPlayerBackend.swift b/Model/Player/Backends/AVPlayerBackend.swift index 1570d256..cd4e1993 100644 --- a/Model/Player/Backends/AVPlayerBackend.swift +++ b/Model/Player/Backends/AVPlayerBackend.swift @@ -70,6 +70,8 @@ final class AVPlayerBackend: PlayerBackend { private var timeObserverThrottle = Throttle(interval: 2) + private var controlsUpdates = false + init(model: PlayerModel, controls: PlayerControlsModel?, playerTime: PlayerTimeModel?) { self.model = model self.controls = controls @@ -314,8 +316,6 @@ final class AVPlayerBackend: PlayerBackend { return } - self.updatePlayerAspectRatio() - if !preservingTime, let segment = self.model.sponsorBlock.segments.first, segment.start < 3, @@ -454,7 +454,9 @@ final class AVPlayerBackend: PlayerBackend { switch playerItem.status { case .readyToPlay: - if self.isAutoplaying(playerItem) { + if self.model.activeBackend == .appleAVPlayer, + self.isAutoplaying(playerItem) { + self.model.updateAspectRatio() self.model.play() } case .failed: @@ -506,8 +508,10 @@ final class AVPlayerBackend: PlayerBackend { return } - self.playerTime.duration = self.playerItemDuration ?? .zero - self.playerTime.currentTime = self.currentTime ?? .zero + if self.controlsUpdates { + self.playerTime.duration = self.playerItemDuration ?? .zero + self.playerTime.currentTime = self.currentTime ?? .zero + } #if !os(tvOS) self.model.updateNowPlayingInfo() @@ -548,8 +552,12 @@ final class AVPlayerBackend: PlayerBackend { return } - DispatchQueue.main.async { - self.controls.isPlaying = player.timeControlStatus == .playing + let isPlaying = player.timeControlStatus == .playing + + if self.controls.isPlaying != isPlaying { + DispatchQueue.main.async { + self.controls.isPlaying = player.timeControlStatus == .playing + } } if player.timeControlStatus != .waitingToPlayAtSpecifiedRate { @@ -590,8 +598,15 @@ final class AVPlayerBackend: PlayerBackend { } func updateControls() {} - func startControlsUpdates() {} - func stopControlsUpdates() {} + + func startControlsUpdates() { + controlsUpdates = true + } + + func stopControlsUpdates() { + controlsUpdates = false + } + func setNeedsDrawing(_: Bool) {} func setSize(_: Double, _: Double) {} func setNeedsNetworkStateUpdates(_: Bool) {} diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index e605c1cf..57fad010 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -45,7 +45,7 @@ final class MPVBackend: PlayerBackend { networkStateTimer.start() if isPlaying { - self.updatePlayerAspectRatio() + self.model.updateAspectRatio() startClientUpdates() } else { stopControlsUpdates() diff --git a/Model/Player/Backends/PlayerBackend.swift b/Model/Player/Backends/PlayerBackend.swift index 37747b9d..ae8f17d0 100644 --- a/Model/Player/Backends/PlayerBackend.swift +++ b/Model/Player/Backends/PlayerBackend.swift @@ -95,10 +95,4 @@ extension PlayerBackend { model.advanceToItem(item) } } - - func updatePlayerAspectRatio() { - DispatchQueue.main.async { - self.model.aspectRatio = aspectRatio - } - } } diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 4a195f44..f412b3ff 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -871,4 +871,14 @@ final class PlayerModel: ObservableObject { mpvBackend.setVideoToAuto() } + + + func updateAspectRatio() { + guard aspectRatio != backend.aspectRatio else { return } + + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + self.aspectRatio = self.backend.aspectRatio + } + } }