From 11ef8c4dfc2a8c2893cb134d1a6d75825dece727 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 3 Jul 2022 23:18:27 +0200 Subject: [PATCH] Fix handling EOF with MPV (#201) --- Model/Player/Backends/MPVBackend.swift | 17 +++-------------- Model/Player/Backends/MPVClient.swift | 4 ++++ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 8a151a97..6ec63131 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -30,13 +30,6 @@ final class MPVBackend: PlayerBackend { self.controls?.isLoadingVideo = self.isLoadingVideo self.setNeedsNetworkStateUpdates(true) - - if !self.isLoadingVideo { - DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in - self?.handleEOF = true - } - } - self.model?.objectWillChange.send() } }} @@ -77,7 +70,6 @@ final class MPVBackend: PlayerBackend { private var clientTimer: Repeater! private var networkStateTimer: Repeater! - private var handleEOF = false private var onFileLoaded: (() -> Void)? private var controlsUpdates = false @@ -157,7 +149,6 @@ final class MPVBackend: PlayerBackend { } func playStream(_ stream: Stream, of video: Video, preservingTime: Bool, upgrading _: Bool) { - handleEOF = false #if !os(macOS) if model.presentingPlayer { UIApplication.shared.isIdleTimerDisabled = true @@ -304,7 +295,6 @@ final class MPVBackend: PlayerBackend { } func closeItem() { - handleEOF = false client?.pause() client?.stop() } @@ -406,6 +396,7 @@ final class MPVBackend: PlayerBackend { onFileLoaded = nil case MPV_EVENT_PAUSE: + DispatchQueue.main.async { [weak self] in self?.handleEndOfFile() } isPlaying = false networkStateTimer.start() @@ -419,9 +410,7 @@ final class MPVBackend: PlayerBackend { isSeeking = true case MPV_EVENT_END_FILE: - DispatchQueue.main.async { [weak self] in - self?.handleEndOfFile() - } + DispatchQueue.main.async { [weak self] in self?.handleEndOfFile() } default: logger.info(.init(stringLiteral: "UNHANDLED event: \(String(cString: mpv_event_name(event.pointee.event_id)))")) @@ -429,7 +418,7 @@ final class MPVBackend: PlayerBackend { } func handleEndOfFile() { - guard handleEOF, !isLoadingVideo else { + guard client.eofReached else { return } diff --git a/Model/Player/Backends/MPVClient.swift b/Model/Player/Backends/MPVClient.swift index f3572b67..ab06b7cb 100644 --- a/Model/Player/Backends/MPVClient.swift +++ b/Model/Player/Backends/MPVClient.swift @@ -176,6 +176,10 @@ final class MPVClient: ObservableObject { mpv.isNil ? false : getFlag("paused-for-cache") } + var eofReached: Bool { + mpv.isNil ? false : getFlag("eof-reached") + } + func seek(relative time: CMTime, completionHandler: ((Bool) -> Void)? = nil) { guard !seeking else { logger.warning("ignoring seek, another in progress")