From cff77a0dbfda6b19973005fd6bc3c30ff51139d8 Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:35:44 +0700 Subject: [PATCH] fix(desktop): keep the output open across a gapless song change The load hook of the queued song set gapless-audio back to no before mpv had decided whether that song keeps the open device, so the device was closed and reopened at every song change. The libmpv build used on Windows closes it without playing what is still queued, which drops the end of the previous song. The option now goes back to no once the queued song has started. --- lib/services/media_kit_playback_deck.dart | 30 +++++++++++++++++++++-- test/media_kit_playback_deck_test.dart | 9 +++++-- test/support/fake_mpv_engine.dart | 9 ++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/services/media_kit_playback_deck.dart b/lib/services/media_kit_playback_deck.dart index 9e895b5c7..8036aaa14 100644 --- a/lib/services/media_kit_playback_deck.dart +++ b/lib/services/media_kit_playback_deck.dart @@ -495,9 +495,12 @@ class MediaKitPlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck { // The on_load hook blocks event delivery. Use the synchronous native // property setter rather than media_kit's event-acknowledged volume API. await _engine.setProperty('volume', _mpvVolume.toString()); - // With no following entry, completion must wait for the audible tail. - await _engine.setProperty('gapless-audio', 'no'); if (_disposed || token != _token || revision != _nextRevision) return; + unawaited( + _drainOnceStarted(token, revision).catchError( + (Object error) => _log.w('Gapless drain setting: $error'), + ), + ); onStarted(); }); return !_disposed && token == _token && revision == _nextRevision; @@ -507,6 +510,29 @@ class MediaKitPlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck { } } + /// With no following entry, completion must wait for the audible tail, so + /// `gapless-audio` goes back to `no`. mpv decides whether the queued song + /// keeps the open device only when that song's first audio is ready, after + /// the load hook returns: `no` set inside the hook closes and reopens the + /// device at every song change. `seeking` reads `yes` until that decision + /// is made, and nothing while the file still loads. + Future _drainOnceStarted(int token, int revision) async { + for (var attempt = 0; attempt < _startPolls; attempt++) { + if (_disposed || token != _token || revision != _nextRevision) return; + if (await _engine.getProperty('seeking') == 'no') break; + await Future.delayed(_startPoll); + } + // A newer queued song or source owns the setting by now. + if (_disposed || token != _token || revision != _nextRevision) return; + await _engine.setProperty('gapless-audio', 'no'); + } + + static const _startPoll = Duration(milliseconds: 20); + + /// Ten seconds: a song that has not started by then loses only the + /// retained device, not its ending. + static const _startPolls = 500; + @override Future clearNextSource() async { _nextRevision++; diff --git a/test/media_kit_playback_deck_test.dart b/test/media_kit_playback_deck_test.dart index 4e7623cd7..9749139ee 100644 --- a/test/media_kit_playback_deck_test.dart +++ b/test/media_kit_playback_deck_test.dart @@ -98,8 +98,8 @@ void main() { engine.completedEvents.add(true); await Future.delayed(Duration.zero); expect(events, isEmpty); - await engine.advance(); - await Future.delayed(Duration.zero); + await engine.advance(started: false); + await _settle(); expect(events, ['next']); expect(deck.state, PlaybackDeckState.playing); expect(engine.volume, closeTo(100 * 0.7368063, 0.001)); @@ -107,6 +107,11 @@ void main() { engine.calls.where((call) => call.first == 'open'), hasLength(opens), ); + // mpv keeps the open device only if the option still reads `weak` + // when the queued song's first audio is ready. + expect(engine.properties['gapless-audio'], 'weak'); + engine.finishStart(); + await _settle(); expect(engine.properties['gapless-audio'], 'no'); engine.completedEvents.add(true); await Future.delayed(Duration.zero); diff --git a/test/support/fake_mpv_engine.dart b/test/support/fake_mpv_engine.dart index 6d8dbd95f..30f49891a 100644 --- a/test/support/fake_mpv_engine.dart +++ b/test/support/fake_mpv_engine.dart @@ -47,15 +47,22 @@ class FakeMpvEngine implements MpvEngine, MpvPlaylistEngine { queuedStart = null; } - Future advance() async { + /// Moves to the queued song. mpv reports `seeking` until the song's first + /// audio is ready; [started] false leaves it there for [finishStart]. + Future advance({bool started = true}) async { final start = queuedStart; queuedStart = null; queuedSource = null; position = Duration.zero; + properties.remove('seeking'); await start?.call(); + properties['seeking'] = 'yes'; durationEvents.add(openDuration!); + if (started) finishStart(); } + void finishStart() => properties['seeking'] = 'no'; + @override Stream get completed => completedEvents.stream; @override