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 a347474437
commit e609e90165
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 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
}
if self.controlsUpdates {
self.playerTime.duration = self.playerItemDuration ?? .zero
self.playerTime.currentTime = self.currentTime ?? .zero
}
#if !os(tvOS)
self.model.updateNowPlayingInfo()
@ -548,9 +552,13 @@ final class AVPlayerBackend: PlayerBackend {
return
}
let isPlaying = player.timeControlStatus == .playing
if self.controls.isPlaying != isPlaying {
DispatchQueue.main.async {
self.controls.isPlaying = player.timeControlStatus == .playing
}
}
if player.timeControlStatus != .waitingToPlayAtSpecifiedRate {
if let controller = self.model.pipController {
@ -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) {}

View File

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

View File

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

View File

@ -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
}
}
}