1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30

Improve AVPlayer performance

Fix updating aspect ratio

Fix #170
This commit is contained in:
Arkadiusz Fal 2022-08-13 16:11:07 +02:00
parent f8d240ad86
commit 0180d13d4a
4 changed files with 35 additions and 16 deletions

View File

@ -70,6 +70,8 @@ final class AVPlayerBackend: PlayerBackend {
private var timeObserverThrottle = Throttle(interval: 2) private var timeObserverThrottle = Throttle(interval: 2)
private var controlsUpdates = false
init(model: PlayerModel, controls: PlayerControlsModel?, playerTime: PlayerTimeModel?) { init(model: PlayerModel, controls: PlayerControlsModel?, playerTime: PlayerTimeModel?) {
self.model = model self.model = model
self.controls = controls self.controls = controls
@ -314,8 +316,6 @@ final class AVPlayerBackend: PlayerBackend {
return return
} }
self.updatePlayerAspectRatio()
if !preservingTime, if !preservingTime,
let segment = self.model.sponsorBlock.segments.first, let segment = self.model.sponsorBlock.segments.first,
segment.start < 3, segment.start < 3,
@ -454,7 +454,9 @@ final class AVPlayerBackend: PlayerBackend {
switch playerItem.status { switch playerItem.status {
case .readyToPlay: case .readyToPlay:
if self.isAutoplaying(playerItem) { if self.model.activeBackend == .appleAVPlayer,
self.isAutoplaying(playerItem) {
self.model.updateAspectRatio()
self.model.play() self.model.play()
} }
case .failed: case .failed:
@ -506,8 +508,10 @@ final class AVPlayerBackend: PlayerBackend {
return return
} }
self.playerTime.duration = self.playerItemDuration ?? .zero if self.controlsUpdates {
self.playerTime.currentTime = self.currentTime ?? .zero self.playerTime.duration = self.playerItemDuration ?? .zero
self.playerTime.currentTime = self.currentTime ?? .zero
}
#if !os(tvOS) #if !os(tvOS)
self.model.updateNowPlayingInfo() self.model.updateNowPlayingInfo()
@ -548,8 +552,12 @@ final class AVPlayerBackend: PlayerBackend {
return return
} }
DispatchQueue.main.async { let isPlaying = player.timeControlStatus == .playing
self.controls.isPlaying = player.timeControlStatus == .playing
if self.controls.isPlaying != isPlaying {
DispatchQueue.main.async {
self.controls.isPlaying = player.timeControlStatus == .playing
}
} }
if player.timeControlStatus != .waitingToPlayAtSpecifiedRate { if player.timeControlStatus != .waitingToPlayAtSpecifiedRate {
@ -590,8 +598,15 @@ final class AVPlayerBackend: PlayerBackend {
} }
func updateControls() {} func updateControls() {}
func startControlsUpdates() {}
func stopControlsUpdates() {} func startControlsUpdates() {
controlsUpdates = true
}
func stopControlsUpdates() {
controlsUpdates = false
}
func setNeedsDrawing(_: Bool) {} func setNeedsDrawing(_: Bool) {}
func setSize(_: Double, _: Double) {} func setSize(_: Double, _: Double) {}
func setNeedsNetworkStateUpdates(_: Bool) {} func setNeedsNetworkStateUpdates(_: Bool) {}

View File

@ -45,7 +45,7 @@ final class MPVBackend: PlayerBackend {
networkStateTimer.start() networkStateTimer.start()
if isPlaying { if isPlaying {
self.updatePlayerAspectRatio() self.model.updateAspectRatio()
startClientUpdates() startClientUpdates()
} else { } else {
stopControlsUpdates() stopControlsUpdates()

View File

@ -95,10 +95,4 @@ extension PlayerBackend {
model.advanceToItem(item) model.advanceToItem(item)
} }
} }
func updatePlayerAspectRatio() {
DispatchQueue.main.async {
self.model.aspectRatio = aspectRatio
}
}
} }

View File

@ -871,4 +871,14 @@ final class PlayerModel: ObservableObject {
mpvBackend.setVideoToAuto() 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
}
}
} }