Files
SpotiFLAC-Mobile/lib/services/system_volume_service.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

61 lines
2.2 KiB
Dart

import 'dart:async';
import 'package:mornye/services/playback_deck.dart';
import 'package:mornye/services/playback_output_volume.dart';
import 'package:mornye/utils/desktop_platform.dart';
import 'package:volume_controller/volume_controller.dart';
/// The volume plugin owns one listener. Share it between background playback
/// and volume sliders so opening or closing a player cannot replace it.
/// On desktop the stream follows [playbackOutputVolume] instead.
class SystemVolumeService {
SystemVolumeService({Stream<double>? source}) : _source = source;
static final SystemVolumeService instance = SystemVolumeService();
final Stream<double>? _source;
final Set<MultiStreamController<double>> _listeners = {};
StreamSubscription<double>? _subscription;
double? _latest;
Stream<double> get changes => Stream<double>.multi((listener) {
_listeners.add(listener);
if (_latest case final value?) listener.add(value);
if (_subscription == null) {
// Desktop never changes the system volume; it reports the in-app gain,
// restored from the previous launch.
if (_source == null && isDesktopPlatform) {
unawaited(PlaybackOutputVolumeStore.ensureRestored());
}
final source =
_source ?? (isDesktopPlatform ? playbackOutputVolumeChanges() : null);
_subscription = source == null
? VolumeController.instance.addListener(_publish)
: source.listen(_publish);
_subscription!.onError((Object error, StackTrace stack) {
for (final listener in _listeners.toList()) {
listener.addError(error, stack);
}
});
}
listener.onCancel = () {
_listeners.remove(listener);
if (_listeners.isEmpty) {
_latest = null;
// Cancel only our subscription; removeListener could cancel a newer
// plugin subscription while cancellation is still in flight.
if (_subscription != null) unawaited(_subscription!.cancel());
_subscription = null;
}
};
});
void _publish(double value) {
if (!value.isFinite) return;
_latest = value.clamp(0, 1);
for (final listener in _listeners.toList()) {
listener.add(_latest!);
}
}
}