Compare commits

..

1 Commits

Author SHA1 Message Date
Toni Förster
c52b4e1007 tweaked A/V-sync for MPV
Signed-off-by: Toni Förster <toni.foerster@gmail.com>
2024-09-24 20:24:48 +02:00
4 changed files with 25 additions and 67 deletions

View File

@@ -79,14 +79,21 @@ final class MPVClient: ObservableObject {
checkError(mpv_set_option_string(mpv, "user-agent", UserAgentManager.shared.userAgent))
checkError(mpv_set_option_string(mpv, "initial-audio-sync", Defaults[.mpvInitialAudioSync] ? "yes" : "no"))
// A/V-SYNC //
// Enable VSYNC needed for `video-sync`
checkError(mpv_set_option_string(mpv, "opengl-swapinterval", "1"))
checkError(mpv_set_option_string(mpv, "video-sync", "display-resample"))
checkError(mpv_set_option_string(mpv, "interpolation", "yes"))
checkError(mpv_set_option_string(mpv, "tscale", "mitchell"))
checkError(mpv_set_option_string(mpv, "tscale-window", "blackman"))
checkError(mpv_set_option_string(mpv, "vd-lavc-framedrop", "nonref"))
checkError(mpv_set_option_string(mpv, "vd-lavc-framedrop", "no"))
checkError(mpv_set_option_string(mpv, "display-fps-override", "\(String(getScreenRefreshRate()))"))
checkError(mpv_set_option_string(mpv, "video-sync-max-factor", "1.1"))
checkError(mpv_set_option_string(mpv, "video-sync-max-video-change", "10"))
checkError(mpv_set_option_string(mpv, "audio-buffer", "0.2"))
checkError(mpv_set_option_string(mpv, "audio-wait-open", "no"))
checkError(mpv_set_option_string(mpv, "force-window", "yes"))
// CPU //

View File

@@ -269,6 +269,15 @@ final class PlayerModel: ObservableObject {
}
#endif
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
guard let self else { return }
if !self.musicMode, self.activeBackend == .mpv {
self.mpvBackend.setVideoToAuto()
self.mpvBackend.controls.resetTimer()
}
}
presentingPlayer = true
#if os(macOS)
@@ -290,6 +299,9 @@ final class PlayerModel: ObservableObject {
DispatchQueue.main.async { [weak self] in
Delay.by(0.3) {
self?.exitFullScreen(showControls: false)
if self?.activeBackend == .mpv, !(self?.musicMode ?? false) {
self?.mpvBackend.setVideoToNo()
}
}
}
@@ -1013,10 +1025,9 @@ final class PlayerModel: ObservableObject {
#else
func handleEnterForeground() {
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
guard let self = self else { return }
guard let self else { return }
if !self.musicMode, self.activeBackend == .mpv {
self.mpvBackend.addVideoTrackFromStream()
if !self.musicMode, self.activeBackend == .mpv, presentingPlayer {
self.mpvBackend.setVideoToAuto()
self.mpvBackend.controls.resetTimer()
} else if !self.musicMode, self.activeBackend == .appleAVPlayer {
@@ -1047,7 +1058,7 @@ final class PlayerModel: ObservableObject {
pause()
} else if !playingInPictureInPicture, activeBackend == .appleAVPlayer {
avPlayerBackend.removePlayerFromLayer()
} else if activeBackend == .mpv, !musicMode {
} else if activeBackend == .mpv, !musicMode, presentingPlayer {
mpvBackend.setVideoToNo()
}
}

View File

@@ -4,7 +4,7 @@
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.ZYMM2HKXY2.yattee.app.url</string>
<string>group.78Z5H3M6RJ.stream.yattee.app.urlbookmarks</string>
</array>
</dict>
</plist>

View File

@@ -11,7 +11,6 @@ final class MPVOGLView: GLKView {
var mpvGL: UnsafeMutableRawPointer?
var queue = DispatchQueue(label: "stream.yattee.opengl", qos: .userInteractive)
var needsDrawing = true
private var dirtyRegion: CGRect?
override init(frame: CGRect) {
guard let context = EAGLContext(api: .openGLES2) else {
@@ -86,7 +85,6 @@ final class MPVOGLView: GLKView {
@objc private func updateFrame() {
// Trigger the drawing process if needed
if needsDrawing {
markRegionAsDirty(bounds)
setNeedsDisplay()
}
}
@@ -102,60 +100,16 @@ final class MPVOGLView: GLKView {
glClear(UInt32(GL_COLOR_BUFFER_BIT))
}
// Function to set a dirty region when a part of the screen changes
func markRegionAsDirty(_ region: CGRect) {
if dirtyRegion == nil {
dirtyRegion = region
} else {
// Expand the dirty region to include the new region
dirtyRegion = dirtyRegion!.union(region)
}
}
// Logic to decide if only part of the screen needs updating
private func needsPartialUpdate() -> Bool {
// Check if there is a defined dirty region that needs updating
if let dirtyRegion, !dirtyRegion.isEmpty {
// Set up glScissor based on dirtyRegion coordinates
glScissor(GLint(dirtyRegion.origin.x), GLint(dirtyRegion.origin.y), GLsizei(dirtyRegion.width), GLsizei(dirtyRegion.height))
return true
}
return false
}
// Call this function when you know the entire screen needs updating
private func clearDirtyRegion() {
dirtyRegion = nil
}
override func draw(_: CGRect) {
guard needsDrawing, let mpvGL else { return }
// Ensure the correct context is set
guard EAGLContext.setCurrent(context) else {
logger.error("Failed to set current OpenGL context.")
return
}
// Bind the default framebuffer
glGetIntegerv(UInt32(GL_FRAMEBUFFER_BINDING), &defaultFBO!)
// Ensure the framebuffer is valid
guard defaultFBO != nil && defaultFBO! != 0 else {
logger.error("Invalid framebuffer ID.")
return
}
// Get the current viewport dimensions
var dims: [GLint] = [0, 0, 0, 0]
glGetIntegerv(GLenum(GL_VIEWPORT), &dims)
// Check if we need partial updates
if needsPartialUpdate() {
logger.info("Performing partial update with scissor test.")
glEnable(GLenum(GL_SCISSOR_TEST))
}
// Set up the OpenGL FBO data
var data = mpv_opengl_fbo(
fbo: Int32(defaultFBO!),
@@ -175,23 +129,9 @@ final class MPVOGLView: GLKView {
mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: flipPtr),
mpv_render_param()
]
// Call the render function and check for errors
let result = mpv_render_context_render(OpaquePointer(mpvGL), &params)
if result < 0 {
logger.error("mpv_render_context_render() failed with error code: \(result)")
} else {
logger.info("mpv_render_context_render() called successfully.")
}
mpv_render_context_render(OpaquePointer(mpvGL), &params)
}
}
// Disable the scissor test after rendering if it was enabled
if needsPartialUpdate() {
glDisable(GLenum(GL_SCISSOR_TEST))
}
// Clear dirty region after drawing
clearDirtyRegion()
}
}