Files
media-player-server/media_server/services/__init__.py
T
alexei.dolgolyov ddf4a6cb29
Lint & Test / test (push) Successful in 20s
Lint & Test / linux-smoke (push) Failing after 34s
feat: production-ready Linux & macOS support
- Add `linux` (dbus-python, PyGObject, python-xlib) and `macos`
  (pyobjc) extras to pyproject.toml with sys_platform markers; move
  cross-platform screen-brightness-control + monitorcontrol to base deps.
- build-dist-linux.sh: install `.[linux]`, pkg-config pre-flight for
  dbus-1/glib-2.0, emit a systemd unit with DBUS_SESSION_BUS_ADDRESS +
  XDG_RUNTIME_DIR + ReadWritePaths for ~/.config and ~/.cache so MPRIS
  works and audit-log / thumbnail writes aren't blocked by ProtectHome.
- New build-dist-macos.sh + per-user LaunchAgent installer producing
  MediaServer-vX.Y-macos-{arm64,x86_64}.tar.gz.
- Templated media-server.service updated to match the dist layout with
  proper session-bus env vars and a writable state-dir grant.
- install_linux.sh: drop dead requirements.txt path; install via
  `pip install ".[linux]"` and pre-create the writable state dirs.
- Cross-platform album artwork: abstract MediaController.get_album_art()
  with Linux (mpris:artUrl, file:// + http(s)://) and macOS (Spotify URL)
  impls; routes/media artwork endpoint now awaits the controller.
- LinuxMediaController connects to the session bus lazily — failure no
  longer crashes lifespan startup; MPRIS calls return idle until the bus
  is reachable. Logged once at INFO with a hint about
  `loginctl enable-linger`.
- Startup preflight on Linux warns if DBUS_SESSION_BUS_ADDRESS or
  XDG_RUNTIME_DIR is unset and informs the user when Wayland disables
  the foreground probe.
- /api/media/visualizer/status now reports a per-OS unavailable_reason.
- tray._confirm guarded against ctypes.windll on non-Windows.
- config.example.yaml: per-OS commented script examples; on_turn_off
  default is now a no-op echo (used to silently fail off Windows).
- README: replace stale `pip install -r requirements.txt` instructions
  with the new extras; add systemd lingering doc + troubleshooting
  section; add macOS LaunchAgent section.
- CI: new linux-smoke job (installs `.[linux]`, boots the server under
  dbus-run-session, asserts /api/health). Release workflow gains
  apt-deps step for the Linux build and a best-effort macOS build job.
2026-05-26 12:17:30 +03:00

112 lines
3.3 KiB
Python

"""Media controller services."""
import os
import platform
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .media_controller import MediaController
_controller_instance: "MediaController | None" = None
def _is_android() -> bool:
"""Check if running on Android (e.g., via Termux)."""
# Check for Android-specific paths and environment
android_indicators = [
Path("/system/build.prop").exists(),
Path("/data/data/com.termux").exists(),
"ANDROID_ROOT" in os.environ,
"TERMUX_VERSION" in os.environ,
]
return any(android_indicators)
def get_media_controller() -> "MediaController":
"""Get the platform-specific media controller instance.
Returns:
The media controller for the current platform
Raises:
RuntimeError: If the platform is not supported
"""
global _controller_instance
if _controller_instance is not None:
return _controller_instance
system = platform.system()
if system == "Windows":
from ..config import settings
from .windows_media import WindowsMediaController
_controller_instance = WindowsMediaController(audio_device=settings.audio_device)
elif system == "Linux":
# Check if running on Android
if _is_android():
from .android_media import AndroidMediaController
_controller_instance = AndroidMediaController()
else:
from .linux_media import LinuxMediaController
_controller_instance = LinuxMediaController()
elif system == "Darwin": # macOS
from .macos_media import MacOSMediaController
_controller_instance = MacOSMediaController()
else:
raise RuntimeError(f"Unsupported platform: {system}")
return _controller_instance
def get_current_album_art() -> bytes | None:
"""Get the current album art bytes (synchronous, Windows-cached path).
Windows pre-populates a module-level cache via the WinRT polling thread,
so this stays sync. For Linux/macOS the controller fetches on demand —
use ``get_current_album_art_async()`` from FastAPI handlers instead.
"""
system = platform.system()
if system == "Windows":
from .windows_media import get_current_album_art as _get_art
return _get_art()
return None
async def get_current_album_art_async() -> bytes | None:
"""Cross-platform album art fetch. Awaits the controller's impl.
Falls back to the sync Windows cache when running on Windows so we don't
pay an extra coroutine hop for the existing path.
"""
system = platform.system()
if system == "Windows":
return get_current_album_art()
controller = get_media_controller()
try:
return await controller.get_album_art()
except Exception: # noqa: BLE001 — art is best-effort; never break the route
return None
def get_audio_devices() -> list[dict[str, str]]:
"""Get list of available audio output devices (Windows only for now)."""
system = platform.system()
if system == "Windows":
from .windows_media import WindowsMediaController
return WindowsMediaController.get_audio_devices()
return []
__all__ = [
"get_media_controller",
"get_current_album_art",
"get_current_album_art_async",
"get_audio_devices",
]