macOS has no D-Bus/MPRIS: the audio engine is what the OS media UI talks to. mpv's Cocoa backend registers with MPRemoteCommandCenter and MPNowPlayingInfoCenter, so it already publishes the live ICY title and receives the media keys. Add a platform-neutral media session (mediasession.py) so the app keeps a single interface: MprisService on D-Bus platforms, NowPlayingSession on macOS. Map mpv's NEXT/PREV/PLAY/PLAYONLY/PAUSEONLY/STOP keys onto script-messages and read them back as client-message IPC events, so the hardware media keys and Control Center drive the TUI (station changes, play/pause/stop) instead of fighting it. --no-mpris (now also --no-media) disables the media keys too. macOS-only mpv options are gated on sys.platform so the Linux command line is unchanged; mpv aborts on unknown flags.
57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import queue
|
|
import sys
|
|
|
|
from somafm_tui import mediasession
|
|
from somafm_tui.mediasession import (
|
|
MediaSession,
|
|
NowPlayingSession,
|
|
create_media_session,
|
|
)
|
|
|
|
|
|
def test_linux_uses_mpris() -> None:
|
|
from somafm_tui.mpris import MprisService
|
|
|
|
session = create_media_session(queue.Queue(), enabled=False, platform="linux")
|
|
assert isinstance(session, MprisService)
|
|
assert session.label == "mpris"
|
|
|
|
|
|
def test_macos_uses_the_system_now_playing_session() -> None:
|
|
session = create_media_session(
|
|
queue.Queue(), enabled=True, backend="mpv", platform="darwin"
|
|
)
|
|
assert isinstance(session, NowPlayingSession)
|
|
assert session.label == "nowplaying"
|
|
assert session.status_label == "on"
|
|
|
|
|
|
def test_macos_without_mpv_reports_off() -> None:
|
|
session = create_media_session(
|
|
queue.Queue(), enabled=True, backend="ffplay", platform="darwin"
|
|
)
|
|
assert session.status_label == "off"
|
|
|
|
|
|
def test_disabled_session_is_inert_but_keeps_state() -> None:
|
|
actions: queue.Queue[str] = queue.Queue()
|
|
session = MediaSession(actions, enabled=False)
|
|
session.start()
|
|
session.set_status("Playing")
|
|
session.set_volume(1.7)
|
|
session.set_track(station="Groove Salad", title="T", artist="A", album="B")
|
|
session.stop()
|
|
assert session.status_label == "off"
|
|
assert session._status == "Playing"
|
|
assert session._volume == 1.0
|
|
assert session._metadata["album"] == "B"
|
|
assert actions.empty()
|
|
|
|
|
|
def test_platform_detection_is_available_for_tests() -> None:
|
|
# The factory must never import D-Bus code on a platform without it.
|
|
assert "linux" in sys.platform or sys.platform == "darwin"
|
|
assert mediasession.media_actions_queue().empty()
|