fix(mpv): avoid blocking runtime calls

This commit is contained in:
edde746
2026-05-03 08:23:44 +02:00
parent f5901389e0
commit 5010833da9
12 changed files with 662 additions and 292 deletions
+12 -21
View File
@@ -50,36 +50,31 @@ class MpvPlayerCore: MpvPlayerCoreBase {
}
func switchToPipVO(layerPtr: UnsafeMutableRawPointer) -> Bool {
guard let mpv else { return false }
guard mpv != nil else { return false }
print("[MpvPlayerCore] Switching to pip VO for PiP")
metalLayer?.removeFromSuperlayer()
mpv_set_property_string(mpv, "vid", "no")
var pointer = Int64(Int(bitPattern: layerPtr))
mpv_set_property(mpv, "wid", MPV_FORMAT_INT64, &pointer)
mpv_set_property_string(mpv, "vo", "pip")
mpv_set_property_string(mpv, "vid", "auto")
setProperty("vid", value: "no")
setInt64PropertyAsync("wid", value: Int64(Int(bitPattern: layerPtr))) { _ in }
setProperty("vo", value: "pip")
setProperty("vid", value: "auto")
print("[MpvPlayerCore] Switched to pip VO successfully")
return true
}
func switchToGpuNextVO() -> Bool {
guard let mpv, let metalLayer else { return false }
guard mpv != nil, let metalLayer else { return false }
print("[MpvPlayerCore] Switching back to gpu-next VO")
mpv_set_property_string(mpv, "vid", "no")
var layer = metalLayer
mpv_set_property(mpv, "wid", MPV_FORMAT_INT64, &layer)
setProperty("vid", value: "no")
setInt64PropertyAsync("wid", value: Int64(Int(bitPattern: Unmanaged.passUnretained(metalLayer).toOpaque()))) { _ in
}
applyGpuNextOptions()
mpv_set_property_string(mpv, "vid", "auto")
setProperty("vid", value: "auto")
if metalLayer.superlayer == nil, let containerView {
containerView.layer.addSublayer(metalLayer)
@@ -179,9 +174,7 @@ class MpvPlayerCore: MpvPlayerCoreBase {
}
print("[MpvPlayerCore] Entering background - disabling video")
if mpv != nil {
mpv_set_option_string(mpv, "vid", "no")
}
setProperty("vid", value: "no")
}
@objc private func enterForeground() {
@@ -191,8 +184,6 @@ class MpvPlayerCore: MpvPlayerCoreBase {
}
print("[MpvPlayerCore] Entering foreground - enabling video")
if mpv != nil {
mpv_set_option_string(mpv, "vid", "auto")
}
setProperty("vid", value: "auto")
}
}
+20 -10
View File
@@ -230,7 +230,12 @@ class MpvPlayerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, MpvPluginS
stopPipTimebaseSync()
pipController?.flushLayer()
let restoredInlineVO = playerCore?.switchToGpuNextVO() ?? false
if pause { playerCore?.setProperty("pause", value: "yes") }
if pause {
playerCore?.setPropertyAsync("pause", value: "yes") { [weak self] _ in
self?.pipController?.invalidatePlaybackState()
self?.syncPipTimebase()
}
}
pendingInlineRestoreAfterPip = restoredInlineVO
if pendingInlineRestoreAfterPip {
if isSceneActive {
@@ -340,14 +345,18 @@ class MpvPlayerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, MpvPluginS
return
}
playerCore?.setProperty(name, value: value)
if name == "pause" {
pipController?.invalidatePlaybackState()
if playerCore?.isPipActive == true { syncPipTimebase() }
guard let core = playerCore else {
result(nil)
return
}
result(nil)
core.setPropertyAsync(name, value: value) { [weak self] _ in
if name == "pause" {
self?.pipController?.invalidatePlaybackState()
if core.isPipActive == true { self?.syncPipTimebase() }
}
result(nil)
}
}
// MARK: - Helpers
@@ -400,9 +409,10 @@ extension MpvPlayerPlugin: MpvPipDelegate {
}
func pipSetPlaying(_ playing: Bool) {
playerCore?.setProperty("pause", value: playing ? "no" : "yes")
pipController?.invalidatePlaybackState()
syncPipTimebase()
playerCore?.setPropertyAsync("pause", value: playing ? "no" : "yes") { [weak self] _ in
self?.pipController?.invalidatePlaybackState()
self?.syncPipTimebase()
}
}
func pipSkip(byInterval seconds: Double) {