fix: play the correct default subtitle/audio track with multiple defaults - #1044
Open
aviadlevy wants to merge 2 commits into
Open
fix: play the correct default subtitle/audio track with multiple defaults#1044aviadlevy wants to merge 2 commits into
aviadlevy wants to merge 2 commits into
Conversation
At playback start setSubtitleTrack/setAudioTrack ran before media_kit finished parsing the file's tracks (open() returns before the track-list observer fires), so the positional remap resolved to null and the selection was silently skipped. mpv then kept its own default pick, which is wrong when a file has multiple default tracks. Wait for the wanted embedded track to appear on the tracks stream before mapping. The wait only fires when the track is not yet present, is race-free, and is capped at 5s, so already-loaded and external cases are unchanged. Fixes DonutWare#1031
PartyDonut
reviewed
Aug 9, 2026
PartyDonut
requested changes
Aug 9, 2026
The onTimeout callback in _awaitTrack ran up to 5 seconds after the await suspended, and dereferenced _player with a bang. By then the player can be null (dispose() nulls it, and the audio-queue path swaps it), so the timeout path threw a TypeError instead of returning the current tracks — turning the graceful-degradation branch into the one that breaks track selection. Hoists _player into a local that flow-promotes to non-nullable, so both the stream subscription and the timeout callback capture a non-null instance and neither needs an assertion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Description
At playback start,
setSubtitleTrack/setAudioTrack(inLibMPV) ran beforemedia_kithad finished parsing the file's tracks —open()returns before the mpvtrack-listobserver fires. Both wrappers map the wanted Jellyfin stream to a player track by list position, so while the track list is still empty the lookup resolves tonulland the selection is silently skipped. mpv then keeps the track it auto-selected on load — its first default-flagged track — which is wrong whenever a file has multiple default tracks (e.g. Jellyfin's smart subtitle selection picks the 2nd default).This is exactly why the in-player UI showed the correct track while a different one actually played, and why re-selecting the same track fixed it (by then the track list had been parsed).
Fix
lib/wrappers/players/lib_mpv.dart: before mapping the wanted stream to a player track, wait for that track to appear onplayer.stream.tracks. The wait:play: true), only the track application waits, and only until mpv finishes parsing (tens of ms);firstWheresubscribes synchronously before any track-list observer callback can run;Applied to both subtitle and audio selection. The
libMDKwrapper has the same latent race but is intentionally left out of scope to keep the diff minimal.AI assistance
Root-cause investigation and the patch were done with Claude Code. The diagnosis — traced through the vendored DonutWare
media_kitfork to confirmopen()returns before track parsing and thatTracks.subtitledefaults to[auto, no]— the fix design, and on-device validation were human-reviewed and -owned.Issue Being Fixed
Resolves #1031 — when a file has multiple subtitle tracks marked as default, the UI selects the correct one but playback uses the first default track instead.
Screenshots / Recordings
Tested On
LibMPVcode path as Android; not separately re-tested but covered by the same fix.Checklist