From e91c7fb2d531d845d96e0811dc01040211389cb9 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Thu, 16 Jun 2022 02:03:15 +0200 Subject: [PATCH] Improve MPV performance --- Model/Player/Backends/MPVBackend.swift | 16 +++++++--- Model/Player/Backends/MPVClient.swift | 4 +-- Shared/Player/MPV/MPVOGLView.swift | 42 ++++++++++++-------------- Shared/Player/VideoPlayerView.swift | 1 - 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 154311d6..ee6131d9 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -273,9 +273,17 @@ final class MPVBackend: PlayerBackend { func updateControls() { DispatchQueue.main.async { [weak self] in - self?.logger.info("updating controls") - self?.controls.currentTime = self?.currentTime ?? .zero - self?.controls.duration = self?.playerItemDuration ?? .zero + guard let self = self else { + return + } + + guard self.controls.player.presentingPlayer else { + return + } + + self.logger.info("updating controls") + self.controls.currentTime = self.currentTime ?? .zero + self.controls.duration = self.playerItemDuration ?? .zero } } @@ -296,8 +304,6 @@ final class MPVBackend: PlayerBackend { private var handleSegmentsThrottle = Throttle(interval: 1) private func getClientUpdates() { - self.logger.info("getting client updates") - currentTime = client?.currentTime playerItemDuration = client?.duration diff --git a/Model/Player/Backends/MPVClient.swift b/Model/Player/Backends/MPVClient.swift index 2176182f..c350907f 100644 --- a/Model/Player/Backends/MPVClient.swift +++ b/Model/Player/Backends/MPVClient.swift @@ -324,8 +324,8 @@ final class MPVClient: ObservableObject { return } - DispatchQueue.main.async { - glView.setNeedsDisplay() + glView.queue.async { + glView.display() } } diff --git a/Shared/Player/MPV/MPVOGLView.swift b/Shared/Player/MPV/MPVOGLView.swift index f466e548..fccdd422 100644 --- a/Shared/Player/MPV/MPVOGLView.swift +++ b/Shared/Player/MPV/MPVOGLView.swift @@ -7,6 +7,7 @@ final class MPVOGLView: GLKView { private var defaultFBO: GLint? var mpvGL: UnsafeMutableRawPointer? + var queue = DispatchQueue(label: "stream.yattee.opengl", qos: .userInteractive) var needsDrawing = true override init(frame: CGRect) { @@ -18,14 +19,9 @@ final class MPVOGLView: GLKView { logger.info("frame size: \(frame.width) x \(frame.height)") super.init(frame: frame, context: context) - contentMode = .redraw EAGLContext.setCurrent(context) - drawableColorFormat = .RGBA8888 - drawableDepthFormat = .formatNone - drawableStencilFormat = .formatNone - defaultFBO = -1 isOpaque = false @@ -38,28 +34,30 @@ final class MPVOGLView: GLKView { } override func draw(_: CGRect) { + guard needsDrawing, let mpvGL = mpvGL else { + return + } + glGetIntegerv(UInt32(GL_FRAMEBUFFER_BINDING), &defaultFBO!) var dims: [GLint] = [0, 0, 0, 0] glGetIntegerv(GLenum(GL_VIEWPORT), &dims) - if mpvGL != nil { - var data = mpv_opengl_fbo( - fbo: Int32(defaultFBO!), - w: Int32(dims[2]), - h: Int32(dims[3]), - internal_format: 0 - ) - var flip: CInt = 1 - withUnsafeMutablePointer(to: &flip) { flip in - withUnsafeMutablePointer(to: &data) { data in - var params = [ - mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: data), - mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: flip), - mpv_render_param() - ] - mpv_render_context_render(OpaquePointer(mpvGL), ¶ms) - } + var data = mpv_opengl_fbo( + fbo: Int32(defaultFBO!), + w: Int32(dims[2]), + h: Int32(dims[3]), + internal_format: 0 + ) + var flip: CInt = 1 + withUnsafeMutablePointer(to: &flip) { flip in + withUnsafeMutablePointer(to: &data) { data in + var params = [ + mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: data), + mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: flip), + mpv_render_param() + ] + mpv_render_context_render(OpaquePointer(mpvGL), ¶ms) } } } diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 0ecc1f59..89f99158 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -156,7 +156,6 @@ struct VideoPlayerView: View { return // swiftlint:disable:this implicit_return } - player.backend.setNeedsDrawing(false) let drag = value.translation.height guard drag > 0 else {