Skip to content

fix: Don't restart a video that is still loading in libMPV - #1187

Open
TheBinaryLoop wants to merge 2 commits into
DonutWare:developfrom
TheBinaryLoop:fix/mpv-dont-restart-loading-video
Open

TheBinaryLoop wants to merge 2 commits into
DonutWare:developfrom
TheBinaryLoop:fix/mpv-dont-restart-loading-video

Conversation

@TheBinaryLoop

Copy link
Copy Markdown

Pull Request Description

LibMPV.loadVideo re-opens the video when mpv hasn't reported a duration within 5 seconds, and keeps doing so for up to a minute. It does this even when the first load is still in progress, so every load that needs more than 5 seconds gets aborted and started over.

This PR makes the retry check whether mpv is idle (idle-active) before re-opening:

  • idle: the previous load failed or ended, so it retries exactly as before.
  • not idle: the load is still running, so the timer is reset and the load is left alone. A load that really hangs is ended by mpv's own network-timeout (60 s), after which the player is idle and the retry takes over as before.

The one minute limit is unchanged, and so is the behaviour for streams that fail to open.

Issue Being Fixed

Starting a transcoded stream can take more than a minute with the libMPV player. For HLS, mpv only reports a duration once the first segment has arrived. When the server has to start a transcode first, that segment alone often takes a few seconds, and with a reverse proxy in front of the server or a server on another continent the playlist requests add a few more. Once the total goes past 5 seconds, every attempt is cancelled shortly before it would have succeeded. Playback only starts after the retry window closes and the last attempt is allowed to finish.

On the server side this looks like the master playlist being requested again every ~5 seconds, with the first segment requested and dropped each time, for about a minute.

Screenshots / Recordings

None, but here is how I tested it. I ran the libMPV wrapper in a Linux desktop build against a local HLS server that holds the first segment back for 8 seconds, then counted the requests:

before after
times the video was opened in 20 s 4 (every ~5.2 s) 1
duration reported never within 20 s after 8.1 s

I also checked that media_kit returns "yes" for idle-active before anything is loaded, and "no" during the slow load. With mpv on its own, idle-active becomes true right after a failed load (HTTP 404), which is the case that still gets retried.

Tested On

  • Android
  • Android TV
  • iOS
  • Linux
  • Windows
  • macOS
  • Web

Checklist

  • If a new package was added, did you ensure it works for all supported platforms? Is the package well maintained
  • Check that any changes are related to the issue at hand.

The load retry reopened the video whenever mpv had not reported a
duration within 5 seconds, even while the original load was still in
progress. Loads that legitimately take longer than that, such as a
server-side transcode that needs a few seconds to produce its first
segment or a server reached over a high-latency connection, were
aborted and restarted every 5 seconds until the one minute retry
window ran out, so playback only started after more than a minute.

Only reopen when mpv is idle, i.e. when the previous load actually
failed or ended. A load that is still running is left alone; if it
hangs, mpv's network-timeout ends it, the player goes idle and the
retry picks it up as before.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant