Files
SpotiFLAC-Mobile/lib/services/playback_deck.dart
T
zarzet bcb1ee8b90 feat(desktop): play through media_kit with MPRIS and SMTC sessions
Windows and Linux now create a libmpv deck through media_kit; macOS,
Android and iOS keep audioplayers. Without libmpv the runtime falls back
to audioplayers.

- The equalizer becomes one libavfilter graph behind mpv's lavfi filter
  (input gain, peaking bands, balance, compressor, -0.5 dBFS limiter)
  after a planar float conversion. Changes set the af property again.
  Filters a libmpv build lacks are probed once and left out; the input
  gain then moves to mpv's volume. Filter failures are reported per player.
- Deck volume is scaled by the in-app output volume on mpv's cubic curve,
  position updates run on a timer, DSD sources throw when libmpv cannot
  open them, and exclusive output has a hook for a later setting.
- The in-app output volume persists across launches on every desktop OS.
- Previews go through the runtime's preview deck, so desktop previews
  follow the in-app volume.
- Linux publishes MPRIS over D-Bus and Windows uses SMTC through
  smtc_windows, both driven by the audio handler.
- The Linux CI job installs libmpv-dev.
2026-10-20 16:55:01 +07:00

114 lines
4.1 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:mornye/utils/desktop_platform.dart';
/// Transport state reported by a [PlaybackDeck].
enum PlaybackDeckState { stopped, playing, paused, completed, disposed }
/// The engine behind `PlaybackDependencies.createDeck`.
enum PlaybackEngine {
/// audioplayers: Android, iOS and macOS, where the forks carry the
/// equalizer tap and Android's USB output.
audioplayers,
/// libmpv through media_kit: Windows and Linux.
mediaKit,
}
/// Forces [playbackEngine], for tests that drive one engine on any host.
@visibleForTesting
PlaybackEngine? debugPlaybackEngineOverride;
PlaybackEngine get playbackEngine {
final override = debugPlaybackEngineOverride;
if (override != null) return override;
if (!isDesktopPlatform) return PlaybackEngine.audioplayers;
return switch (defaultTargetPlatform) {
TargetPlatform.windows || TargetPlatform.linux => PlaybackEngine.mediaKit,
_ => PlaybackEngine.audioplayers,
};
}
/// In-app output gain from 0 to 1 for platforms where the app must not change
/// the system volume (desktop). Every deck multiplies the gain it is given
/// (normalization, AutoMix fades) by this value. Mobile keeps it at 1.
final playbackOutputVolume = ValueNotifier<double>(1);
/// [playbackOutputVolume] as a stream: the current value, then each change.
Stream<double> playbackOutputVolumeChanges() =>
Stream<double>.multi((listener) {
void changed() => listener.add(playbackOutputVolume.value);
playbackOutputVolume.addListener(changed);
changed();
listener.onCancel = () => playbackOutputVolume.removeListener(changed);
});
/// One audio player owned by the music runtime. The main player and each
/// AutoMix deck are separate instances created through
/// `PlaybackDependencies.createDeck`.
///
/// Every implementation also:
/// - applies `playbackEqualizer` (`playback_equalizer.dart`) to its own output
/// and reports failures with `reportEqualizerError`, keyed by player id;
/// - multiplies [setVolume] by [playbackOutputVolume];
/// - delivers runtime failures as errors on [onPlayerStateChanged], and makes
/// [resume] throw when the current source failed to load.
abstract interface class PlaybackDeck {
PlaybackDeckState get state;
/// True while a native output bypasses the mixer (Android USB bit-perfect).
/// Volume, rate and the equalizer have no effect in that mode.
bool get isDirect;
/// True when a direct output lost its route and the source must be loaded
/// again before playback can continue.
bool get needsSourceReload;
Stream<PlaybackDeckState> get onPlayerStateChanged;
Stream<Duration> get onDurationChanged;
Stream<Duration> get onPositionChanged;
Stream<void> get onPlayerComplete;
/// Emits [onPositionChanged] every [interval] while playing. Updates must
/// not depend on rendered frames: AutoMix runs with the display asleep.
void startPositionUpdates(Duration interval);
/// Applies the platform's music output configuration (audio focus, usage,
/// keeping the source after completion). Called before each new source.
Future<void> configureOutput();
/// Linear gain from 0 to 1, before [playbackOutputVolume].
Future<void> setVolume(double volume);
Future<void> setPlaybackRate(double rate);
Future<void> setNetworkSource(String url);
/// Loads a local file. The output flags request Android's native USB and
/// exclusive paths; other decks ignore them, except that [requiresDsd]
/// throws when the deck cannot play DSD.
Future<void> setFileSource(
String path, {
bool preferBitPerfect = false,
bool directUsb = false,
bool allowDop = false,
bool requiresDsd = false,
bool allowFixedVolume = false,
bool dapExclusive = false,
});
/// Abandons a source that is still preparing, if any.
Future<void> cancelPreparation();
Future<void> resume();
Future<void> pause();
Future<void> seek(Duration position);
Future<void> stop();
Future<Duration?> getCurrentPosition();
Future<Duration?> getDuration();
/// Releases the native player. The deck is unusable afterwards.
Future<void> dispose();
}