From c1aee48b8b3e1a2370e5448ea8fa0649162eaf49 Mon Sep 17 00:00:00 2001 From: trav Date: Thu, 15 Oct 2026 16:01:08 -0700 Subject: [PATCH] =?UTF-8?q?v0.56.0:=20Connect=20to=20AirPlay=20=E2=80=94?= =?UTF-8?q?=20stream=20to=20AirPlay=20speakers=20via=20pyatv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every song is decoded by the ffmpeg CLI and streamed over RAOP, so ALAC and AIFF play too (only FairPlay is refused). pyatv has no pause or seek for RAOP, so both restart the stream at the heard position. The picker lists every AirPlay device on the network, greyed with the reason when LinTunes can't stream to it (video-only TVs, PIN pairing). Casting and AirPlay are mutually exclusive; the visualizer tile shows the AirPlay glyph and a click there stops it. Needs: pip install -e . (adds pyatv). --- CLAUDE.md | 34 ++- lintunes/__init__.py | 2 +- lintunes/airplay/__init__.py | 17 ++ lintunes/airplay/controller.py | 162 +++++++++++ lintunes/airplay/discovery.py | 145 ++++++++++ lintunes/airplay/loop.py | 38 +++ lintunes/airplay/sink.py | 330 ++++++++++++++++++++++ lintunes/airplay/stream.py | 165 +++++++++++ lintunes/airplay/support.py | 135 +++++++++ lintunes/gui/cast_dialog.py | 57 +++- lintunes/gui/icons.py | 20 ++ lintunes/gui/main_window.py | 67 ++++- lintunes/gui/transport.py | 8 +- lintunes/gui/visualizer.py | 9 +- setup.py | 1 + tests/test_round87.py | 491 +++++++++++++++++++++++++++++++++ 16 files changed, 1647 insertions(+), 34 deletions(-) create mode 100644 lintunes/airplay/__init__.py create mode 100644 lintunes/airplay/controller.py create mode 100644 lintunes/airplay/discovery.py create mode 100644 lintunes/airplay/loop.py create mode 100644 lintunes/airplay/sink.py create mode 100644 lintunes/airplay/stream.py create mode 100644 lintunes/airplay/support.py create mode 100644 tests/test_round87.py diff --git a/CLAUDE.md b/CLAUDE.md index 7646e9e..4605a8b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -115,7 +115,7 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal finish/skip/stop/quit; the artist/album snapshot outlives a delete or retag. Under 5 s is noise and never written. Only while the synced pref `wrapped.track_history` is on: `Player.listen_ended` → `MainWindow` (adds - `src` desktop/cast and `ctx`, which it latches at `track_changed` because + `src` desktop/cast/airplay and `ctx`, which it latches at `track_changed` because `_now_playing_context` moves before the old listen closes) → `LibraryManager.record_listen`, flushed with the journal. The Rabbit writes the same lines (`History.java`) only when sync put `"history": true` in its @@ -808,6 +808,38 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal a 500 ms poll of `adjusted_current_time` (only trusted while PLAYING — it creeps while paused). +- **`lintunes/airplay/`** — AirPlay playback (Connections → Connect to + AirPlay…, Round 87), via **pyatv**'s RAOP streaming and the opposite model + to `cast/`: an AirPlay speaker is *sent* PCM in real time, so every song is + decoded by the `ffmpeg` CLI (`stream.py`) and pyatv packetizes it — ALAC and + AIFF play here, only FairPlay is refused. pyatv's own sources don't fit + (its file path is miniaudio, no AAC/ALAC; its pipe path reports duration 0 + and guesses the WAV header), so `stream.install_shim` patches the + `open_source` name in `pyatv.protocols.raop` to answer a `PcmRequest` with + an `FfmpegSource` at the negotiated rate. ffmpeg emits **big-endian** + (`s16be`): pyatv byteswaps its own little-endian samples, and the shim's + output is byte-identical to pyatv's `FileSource`. pyatv has **no pause or + seek** for RAOP (its pause *is* stop), so `AirPlaySink` runs one + `stream_file` per stretch of playback and pause/seek/resume start a new one + at `-ss`; each start bumps a generation, so a replaced stream can't report + "finished" and advance the queue. **A closed connection makes pyatv + *return*, not raise** — "the song ended" is only ffmpeg's EOF + (`outcome_of`), anything else is a lost speaker. Position is what's + *heard*: frames read minus the receiver's `22050 + rate` frames of latency + (~1.5 s, `support.heard_ms`). Volume: the slider's level through Qt's + log→linear curve, then that dB on pyatv's −30..0 scale + (`level_to_percent`), applied before each stream starts. Everything pyatv + runs on one `AsyncLoop` thread; results come back as Qt signals. + Discovery repeats `pyatv.scan` while the picker is open and lists *every* + AirPlay device, greyed with the reason when it can't be used (video-only + TVs/projectors have no RAOP; PIN pairing and passwords aren't built), in + `gui/cast_dialog.DevicePickerDialog`, which `ChromecastDialog` shares. + Connects go **RAOP-only** (`controller.raop_only`). Casting and AirPlay are + mutually exclusive (MainWindow disconnects the other first); the visualizer + tile shows the sink's `glyph`. Testing streams against **pyatv's own fake + receiver** (`tests/fake_device/raop.py` in its sdist, bound to 127.0.0.1) — + never a real speaker unless trav says so. + ## Conventions & gotchas - **The Rabbit R1's screen is not what the internet says.** Measured: diff --git a/lintunes/__init__.py b/lintunes/__init__.py index a5ed9f5..5c0a1bc 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.55.0" +__version__ = "0.56.0" diff --git a/lintunes/airplay/__init__.py b/lintunes/airplay/__init__.py new file mode 100644 index 0000000..348c198 --- /dev/null +++ b/lintunes/airplay/__init__.py @@ -0,0 +1,17 @@ +"""AirPlay playback: discovery, the streaming session, and the Player sink. + +The opposite model to Chromecast. A Chromecast fetches the original file over +HTTP and decodes it itself; an AirPlay speaker (RAOP) is *sent* PCM in real +time. So lintunes decodes every song with the ``ffmpeg`` CLI and pyatv +packetizes it. That makes every format the desktop plays castable here (ALAC +and AIFF included), at the cost of the stream being ours to keep running: a +suspended laptop is a silent speaker. + +pyatv has neither pause nor seek for RAOP (its "pause" is stop), so both are +"tear the stream down and start a new one at that position" — ffmpeg's +``-ss`` makes the restart cheap. + +Nothing here imports pyatv at module scope — like pychromecast, it is deferred +into the code that needs it, so lintunes still launches on a machine that has +pulled this round but not yet run ``pip install -e .``. +""" diff --git a/lintunes/airplay/controller.py b/lintunes/airplay/controller.py new file mode 100644 index 0000000..efa9cd6 --- /dev/null +++ b/lintunes/airplay/controller.py @@ -0,0 +1,162 @@ +"""Owns an AirPlay session on MainWindow's behalf (CastController's shape). + +Widget-free: it reports state as signals and MainWindow decides what to show. +"Connected" is mostly a promise here — RAOP holds no connection between +streams, so a speaker that vanishes while paused is only noticed when the +next stream can't reach it, and Player falls back then. +""" + +from PyQt6.QtCore import QObject, QTimer, pyqtSignal + +from lintunes.airplay.discovery import AirPlayDiscovery +from lintunes.airplay.loop import AsyncLoop +from lintunes.airplay.support import is_available +from lintunes.eventlog import log_control +from lintunes.inhibit import INHIBIT_LOGOUT, INHIBIT_SUSPEND, SleepInhibitor + +CONNECT_TIMEOUT_S = 10.0 + + +def raop_only(conf): + """Connect over RAOP alone. The AirPlay service on a speaker is the video/ + remote-control side; left enabled, pyatv may route volume through it + when no stream is running.""" + from pyatv.const import Protocol + for service in conf.services: + if service.protocol != Protocol.RAOP: + service.enabled = False + return conf + + +class AirPlayController(QObject): + connecting = pyqtSignal(object) # AirPlayDevice + connected = pyqtSignal(str) # device name + disconnected = pyqtSignal(str) # reason; "" when the user asked + failed = pyqtSignal(str) + + _connect_done = pyqtSignal(object, int) # {"atv"|"error"}, attempt + + def __init__(self, player, parent=None): + super().__init__(parent) + self._player = player + self._loop = AsyncLoop() + self._sink = None + self._discovery: AirPlayDiscovery | None = None + self._attempt = 0 + self._connect_done.connect(self._on_connect_result) + # This machine decodes every packet the speaker plays, so a suspend + # silences the room mid-song. + self._inhibitor = SleepInhibitor( + reason="Playing to an AirPlay speaker", + flags=INHIBIT_LOGOUT | INHIBIT_SUSPEND) + + # ---- state ---- + + def is_available(self) -> tuple[bool, str]: + return is_available() + + def is_connected(self) -> bool: + return self._sink is not None + + def device_name(self) -> str: + return self._sink.name if self._sink is not None else "" + + def discovery(self) -> AirPlayDiscovery: + if self._discovery is None: + self._discovery = AirPlayDiscovery(self._loop, self) + return self._discovery + + # ---- connecting ---- + + def connect_to(self, device): + if self._sink is not None: + self.disconnect() + self._attempt += 1 + attempt = self._attempt + self.connecting.emit(device) + log_control("airplay", "connect", f"{device.name} at {device.host}") + conf = self.discovery().config_for(device.uuid) + future = self._loop.submit(self._connect(conf, device)) + + def done(f): + try: + result = {"atv": f.result(), "name": device.name} + except Exception as e: # noqa: BLE001 — report, never crash + result = {"error": str(e) or type(e).__name__} + self._connect_done.emit(result, attempt) + + future.add_done_callback(done) + + async def _connect(self, conf, device): + import asyncio + import pyatv + loop = asyncio.get_running_loop() + if conf is None: + # Not seen by this session's scans (or seen long ago): ask the + # address directly. + found = await pyatv.scan(loop, hosts=[device.host], timeout=5) + conf = next((c for c in found + if str(c.identifier) == device.uuid), None) + if conf is None: + raise ConnectionError(f"{device.name} didn't answer") + return await asyncio.wait_for( + pyatv.connect(raop_only(conf), loop), CONNECT_TIMEOUT_S) + + def _on_connect_result(self, result: dict, attempt: int): + if attempt != self._attempt: + atv = result.get("atv") + if atv is not None: + self._loop.submit(self._close(atv)) + return + if "error" in result: + self.failed.emit(f"Couldn't connect: {result['error']}") + return + from lintunes.airplay.sink import AirPlaySink + self._sink = AirPlaySink(result["atv"], self._loop, result["name"], + self) + # Deferred a turn, as in CastController: Player's own fallback slot + # disconnects this signal mid-emission. + self._sink.unavailable.connect( + lambda reason: QTimer.singleShot( + 0, lambda: self._on_unavailable(reason))) + self._player.set_sink(self._sink) + self._inhibitor.inhibit() + self.connected.emit(self._sink.name) + + @staticmethod + async def _close(atv): + import asyncio + await asyncio.gather(*atv.close(), return_exceptions=True) + + # ---- disconnecting ---- + + def disconnect(self, reason: str = ""): + sink, self._sink = self._sink, None + if sink is None: + return + log_control("airplay", "disconnect", + f"{sink.name} reason={reason or 'user'}") + self._player.set_sink(None) + self._teardown(sink, reason) + + def _on_unavailable(self, reason: str): + sink, self._sink = self._sink, None + if sink is None: + return + log_control("airplay", "lost", reason) + self._teardown(sink, reason) + + def _teardown(self, sink, reason: str): + sink.shutdown() + self._inhibitor.release() + self.disconnected.emit(reason) + + def shutdown(self): + """Quitting: stop first so the swap back can't burst out of the + laptop's speakers (CastController.shutdown's rule).""" + if self._discovery is not None: + self._discovery.stop() + if self._sink is not None: + self._player.stop() + self.disconnect() + self._inhibitor.release() diff --git a/lintunes/airplay/discovery.py b/lintunes/airplay/discovery.py new file mode 100644 index 0000000..550794d --- /dev/null +++ b/lintunes/airplay/discovery.py @@ -0,0 +1,145 @@ +"""Finding AirPlay speakers on the local network. + +pyatv's `scan` is one-shot, so while the picker is open this repeats it and +reports differences — the same device_found / device_lost / failed shape as +CastDiscovery, which is what lets one picker dialog serve both. A device has +to miss two scans in a row to count as gone: one lost mDNS reply shouldn't +make a row blink out. + +Every device with AirPlay is listed, including ones lintunes can't stream to +(a TV that wants a PIN, a video-only projector) — greyed, with the reason — +so "why isn't my speaker there?" has an answer on screen. +""" + +import asyncio +import threading +from dataclasses import dataclass + +from PyQt6.QtCore import QObject, pyqtSignal + +from lintunes.airplay.support import classify + +SCAN_TIMEOUT_S = 3 +SCAN_PAUSE_S = 2.0 +MISSES_TO_LOSE = 2 + + +@dataclass(frozen=True) +class AirPlayDevice: + uuid: str # pyatv's identifier (the device id for RAOP) + name: str + model: str + host: str + connectable: bool = True + note: str = "" # why it isn't connectable + + +def to_device(conf) -> AirPlayDevice: + """Every pyatv config access in one place (discovery.py's _to_device + rule: a pyatv upgrade should be a fix here).""" + from pyatv.const import Protocol + raop = conf.get_service(Protocol.RAOP) + if raop is not None: + ok, note = classify(True, raop.pairing.name, raop.requires_password) + else: + ok, note = classify(False, "", False) + model = "" + info = getattr(conf, "device_info", None) + if info is not None: + model = getattr(info, "raw_model", "") or "" + if not model or model.startswith("Unknown"): + model = "" + if not model and raop is not None: + model = raop.properties.get("am", "") or "" + return AirPlayDevice(uuid=str(conf.identifier), name=conf.name or "AirPlay", + model=model, host=str(conf.address), + connectable=ok, note=note) + + +class AirPlayDiscovery(QObject): + """Scans for AirPlay devices while running, reporting changes.""" + + device_found = pyqtSignal(object) # AirPlayDevice + device_lost = pyqtSignal(object) # AirPlayDevice + failed = pyqtSignal(str) + + def __init__(self, loop, parent=None): + super().__init__(parent) + self._loop = loop + self._future = None + self._lock = threading.Lock() + self._devices: dict[str, AirPlayDevice] = {} + # pyatv configs by identifier, kept after stop(): the picker stops + # scanning as it closes, *before* the controller connects to the pick. + self._configs: dict[str, object] = {} + + def is_running(self) -> bool: + return self._future is not None + + def devices(self) -> list[AirPlayDevice]: + with self._lock: + return list(self._devices.values()) + + def config_for(self, uuid: str): + with self._lock: + return self._configs.get(uuid) + + def start(self): + if self._future is not None: + return + try: + import pyatv # noqa: F401 — imported here, not at module scope + except ImportError as e: + self.failed.emit(f"AirPlay support isn't installed ({e}).") + return + self._future = self._loop.submit(self._scan_forever()) + + def stop(self): + """Idempotent: safe from every dialog exit path.""" + future, self._future = self._future, None + if future is not None: + future.cancel() + with self._lock: + self._devices.clear() + + # ---- loop thread ---- + + async def _scan_forever(self): + import pyatv + from pyatv.const import Protocol + misses: dict[str, int] = {} + loop = asyncio.get_running_loop() + while True: + try: + confs = await pyatv.scan( + loop, timeout=SCAN_TIMEOUT_S, + protocol={Protocol.AirPlay, Protocol.RAOP}) + except asyncio.CancelledError: + raise + except Exception as e: # noqa: BLE001 — report, never crash + self.failed.emit(f"Couldn't search for AirPlay devices: {e}") + return + seen = set() + for conf in confs: + device = to_device(conf) + seen.add(device.uuid) + misses.pop(device.uuid, None) + with self._lock: + known = self._devices.get(device.uuid) + self._devices[device.uuid] = device + self._configs[device.uuid] = conf + if known != device: + if known is not None: + self.device_lost.emit(known) + self.device_found.emit(device) + with self._lock: + gone = [uuid for uuid in self._devices if uuid not in seen] + for uuid in gone: + misses[uuid] = misses.get(uuid, 0) + 1 + if misses[uuid] >= MISSES_TO_LOSE: + misses.pop(uuid) + with self._lock: + device = self._devices.pop(uuid, None) + if device is not None: + self.device_lost.emit(device) + await asyncio.sleep(SCAN_PAUSE_S) diff --git a/lintunes/airplay/loop.py b/lintunes/airplay/loop.py new file mode 100644 index 0000000..fd8a976 --- /dev/null +++ b/lintunes/airplay/loop.py @@ -0,0 +1,38 @@ +"""The one asyncio loop pyatv runs on, on a daemon thread of its own. + +pyatv is asyncio throughout; Qt owns the GUI thread's loop. Everything AirPlay +(discovery scans, connects, streams, volume) is submitted here and its results +come back as Qt signals, so no coroutine ever runs on the GUI thread and no +widget is touched from this one. +""" + +import asyncio +import threading + + +class AsyncLoop: + """Started on first use; lives as long as the process (it's one idle + thread when nothing is playing).""" + + def __init__(self): + self._loop: asyncio.AbstractEventLoop | None = None + self._lock = threading.Lock() + + def loop(self) -> asyncio.AbstractEventLoop: + with self._lock: + if self._loop is None: + loop = asyncio.new_event_loop() + threading.Thread(target=loop.run_forever, daemon=True, + name="airplay-loop").start() + self._loop = loop + return self._loop + + def submit(self, coro): + """Schedule `coro`; returns a concurrent.futures.Future. Coroutines + submitted in order start in that order.""" + return asyncio.run_coroutine_threadsafe(coro, self.loop()) + + def run(self, coro, timeout: float): + """Block for `coro`'s result — teardown only, where waiting a moment + for the speaker to stop is better than leaving it playing.""" + return self.submit(coro).result(timeout) diff --git a/lintunes/airplay/sink.py b/lintunes/airplay/sink.py new file mode 100644 index 0000000..367aef8 --- /dev/null +++ b/lintunes/airplay/sink.py @@ -0,0 +1,330 @@ +"""The Player sink that plays through an AirPlay speaker. + +One `stream_file` per stretch of continuous playback: a song from its start, +or from wherever a resume or a seek put it. Each start bumps a *generation*; +a stream's outcome only counts if it is still the current one, so the stream +a seek just replaced can't report "finished" and advance the queue. + +Threading follows cast/sink.py: every pyatv call runs on the AsyncLoop, every +outcome comes back through a private signal (queued onto the GUI thread), and +the position is computed on the GUI thread from the frame count the source +keeps in its PcmRequest. +""" + +import asyncio +from contextlib import suppress + +from PyQt6.QtCore import QBuffer, QIODevice, QTimer, Qt, pyqtSignal +from PyQt6.QtGui import QImage +from PyQt6.QtMultimedia import QAudio + +from lintunes import tagging +from lintunes.airplay.stream import PcmRequest, install_shim +from lintunes.airplay.support import ( + can_stream, heard_ms, level_to_percent, unstreamable_reason) +from lintunes.player import PlaybackSink + +# Outcomes a stream reports when it ends. +FINISHED = "finished" # ffmpeg reached the end of the song +DECODE_ERROR = "decode_error" # this song can't be read; Player skips it +DROPPED = "dropped" # the speaker closed on us mid-song +FAILED = "failed" # couldn't reach the speaker at all + +ART_MAX_PX = 600 # receivers show a small square; a 3000 px scan is waste + + +def jpeg_artwork(data: bytes | None) -> bytes | None: + """The cover as a JPEG of at most ART_MAX_PX: RAOP sends artwork as + image/jpeg whatever it really is. Best-effort, like every art path.""" + if not data: + return None + image = QImage() + if not image.loadFromData(data): + return None + if data[:3] == b"\xff\xd8\xff" and max(image.width(), + image.height()) <= ART_MAX_PX: + return data + if max(image.width(), image.height()) > ART_MAX_PX: + image = image.scaled(ART_MAX_PX, ART_MAX_PX, + Qt.AspectRatioMode.KeepAspectRatio, + Qt.TransformationMode.SmoothTransformation) + buffer = QBuffer() + buffer.open(QIODevice.OpenModeFlag.WriteOnly) + image.save(buffer, "JPEG", 90) + return bytes(buffer.data()) + + +def outcome_of(request: PcmRequest, error: Exception | None) -> str: + """Why a stream ended. A closed connection makes pyatv *return* rather + than raise, so "the song is over" is only ever ffmpeg's end of file.""" + if request.decode_error: + return DECODE_ERROR + if error is not None: + return FAILED if request.frames_read == 0 else DROPPED + return FINISHED if request.exhausted else DROPPED + + +class AirPlaySink(PlaybackSink): + """Streams decoded PCM to an AirPlay receiver.""" + + provides_pcm = False + glyph = "airplay" + listen_src = "airplay" + + POLL_MS = 250 + VOLUME_COALESCE_MS = 120 + STOP_TIMEOUT_S = 3.0 + + _stream_ended = pyqtSignal(int, str, str) # generation, outcome, detail + + def __init__(self, atv, loop, name: str, parent=None): + super().__init__(parent) + install_shim() + self._atv = atv + self._loop = loop + self.name = name or "AirPlay" + + self._track = None + self._metadata: dict = {} + self._request: PcmRequest | None = None + self._generation = 0 + self._playing = False + self._position_ms = 0 + self._duration_ms = 0 + self._percent = 0.0 + self._shutdown_done = False + self._dying = False + + # Loop-thread state: the running stream task, and a lock so a new + # stream never starts before the last one has torn down (pyatv + # refuses a second stream while one holds the session). + self._task: asyncio.Task | None = None + self._lock: asyncio.Lock | None = None + + self._stream_ended.connect(self._on_stream_ended) + + self._poll = QTimer(self) + self._poll.setInterval(self.POLL_MS) + self._poll.timeout.connect(self._on_poll) + + self._volume_timer = QTimer(self) + self._volume_timer.setSingleShot(True) + self._volume_timer.setInterval(self.VOLUME_COALESCE_MS) + self._volume_timer.timeout.connect(self._flush_volume) + + # ---- PlaybackSink ---- + + def can_play(self, track) -> bool: + return can_stream(track.location, track.kind) + + def unplayable_reason(self, track) -> str: + return unstreamable_reason(track.location, track.kind) + + def load(self, track, autoplay: bool, start_ms: int): + self._track = track + self._metadata = { + "title": track.name or "", + "artist": track.artist or "", + "album": track.album or "", + "duration": (track.total_time or 0) / 1000.0, + "artwork": self._artwork(track), + } + self._duration_ms = track.total_time or 0 + self.duration_changed.emit(self._duration_ms) + self._set_position(max(0, start_ms)) + if autoplay: + self._start_stream(self._position_ms) + else: + self._halt_stream() + self._set_playing(False) + + def _artwork(self, track): + try: + return jpeg_artwork(tagging.read_embedded_artwork(track.location)) + except Exception: # noqa: BLE001 — art is never worth a failure + return None + + def play(self): + if self._track is None or self._playing: + return + self._start_stream(self._position_ms) + + def pause(self): + # AirPlay has no pause: stop the stream where the listener is, and + # resume() starts a new one from there. + self._freeze_position() + self._halt_stream() + self._set_playing(False) + + def stop(self): + self._freeze_position() + self._halt_stream() + self._set_playing(False) + + def seek(self, position_ms: int): + target = max(0, position_ms) + if self._playing: + self._start_stream(target) + else: + self._set_position(target) + + def position_ms(self) -> int: + return self._position_ms + + def duration_ms(self) -> int: + return self._duration_ms + + def is_playing(self) -> bool: + return self._playing + + def set_volume(self, level: float): + level = max(0.0, min(1.0, float(level))) + # The same perceptual→linear curve LocalSink uses, then dB on the + # speaker: one slider position, one loudness, either output. + linear = QAudio.convertVolume( + level, QAudio.VolumeScale.LogarithmicVolumeScale, + QAudio.VolumeScale.LinearVolumeScale) + self._percent = level_to_percent(linear) + self._volume_timer.start() + + def shutdown(self): + """Stop the speaker and close the session. Waits a moment for the + teardown, so quitting doesn't leave the speaker playing what it had + buffered, and never longer: a vanished speaker can't hold up quit.""" + if self._shutdown_done: + return + self._shutdown_done = True + self._poll.stop() + self._volume_timer.stop() + self._generation += 1 + with suppress(Exception): + self._loop.run(self._close_all(), self.STOP_TIMEOUT_S) + + # ---- streams (GUI side) ---- + + def _start_stream(self, start_ms: int): + if self._shutdown_done or self._dying or self._track is None: + return + self._generation += 1 + request = PcmRequest(self._track.location, start_ms, + self._duration_ms) + self._request = request + self._set_position(start_ms) + self._set_playing(True) + self._poll.start() + self._loop.submit(self._restart(self._generation, request, + dict(self._metadata), self._percent)) + + def _halt_stream(self): + self._generation += 1 + self._request = None + self._poll.stop() + if not self._shutdown_done: + self._loop.submit(self._halt()) + + def _freeze_position(self): + if self._request is not None: + self._set_position(self._heard(self._request)) + + def _heard(self, request: PcmRequest) -> int: + return heard_ms(request.start_ms, request.frames_read, + request.elapsed_ms(), request.sample_rate) + + def _on_poll(self): + request = self._request + if request is not None: + self._set_position(self._heard(request)) + + def _on_stream_ended(self, generation: int, outcome: str, detail: str): + if generation != self._generation or self._shutdown_done: + return + self._request = None + self._poll.stop() + if outcome == FINISHED: + self._set_position(self._duration_ms or self._position_ms) + self._set_playing(False) + self.ended.emit() + elif outcome == DECODE_ERROR: + self._set_playing(False) + self.error.emit(f"it couldn't be decoded ({detail})") + elif outcome == DROPPED: + self._fail(f"{self.name} stopped playing — something else may " + "have taken it over.") + else: + self._fail(f"Lost {self.name} ({detail}).") + + def _fail(self, reason: str): + """Let Player fall back to the local output, still playing — the + cast sink's rule: what lintunes last asked for, not what the dying + session said.""" + if self._dying: + return + self._dying = True + self._poll.stop() + self.unavailable.emit(reason) + + def _flush_volume(self): + if not self._shutdown_done: + self._loop.submit(self._apply_volume(self._percent)) + + def _set_playing(self, playing: bool): + if playing != self._playing: + self._playing = playing + self.state_changed.emit(playing) + + def _set_position(self, position_ms: int): + self._position_ms = position_ms + self.position_changed.emit(position_ms) + + # ---- streams (loop thread) ---- + + def _loop_lock(self) -> asyncio.Lock: + if self._lock is None: + self._lock = asyncio.Lock() + return self._lock + + async def _restart(self, generation, request, metadata, percent): + async with self._loop_lock(): + await self._cancel_task() + if generation != self._generation: + return # superseded while the last one tore down + # Before the stream, so its first packet is already at this level: + # with no stream running pyatv only stores it for the next one. + await self._apply_volume(percent) + self._task = asyncio.ensure_future( + self._stream(generation, request, metadata)) + + async def _halt(self): + async with self._loop_lock(): + await self._cancel_task() + + async def _cancel_task(self): + task, self._task = self._task, None + if task is not None and not task.done(): + task.cancel() + with suppress(asyncio.CancelledError, Exception): + await task + + async def _stream(self, generation, request, metadata): + from pyatv.interface import MediaMetadata + error = None + try: + await self._atv.stream.stream_file( + request, metadata=MediaMetadata(**metadata)) + except asyncio.CancelledError: + request.kill() + raise + except Exception as e: # noqa: BLE001 — reported, never raised + error = e + finally: + request.kill() + self._stream_ended.emit(generation, outcome_of(request, error), + request.decode_error or str(error or "")) + + async def _apply_volume(self, percent: float): + with suppress(Exception): # a volume write is never fatal + await self._atv.audio.set_volume(percent) + + async def _close_all(self): + await self._halt() + await asyncio.gather(*self._atv.close(), return_exceptions=True) diff --git a/lintunes/airplay/stream.py b/lintunes/airplay/stream.py new file mode 100644 index 0000000..99c5132 --- /dev/null +++ b/lintunes/airplay/stream.py @@ -0,0 +1,165 @@ +"""The audio that goes to the speaker: ffmpeg's PCM, as a pyatv AudioSource. + +pyatv's `stream_file` takes a path (decoded whole into memory by miniaudio, +which can't read AAC or ALAC) or a pipe (whose duration it reports as 0 and +whose WAV header it skips by assuming 44 bytes). Neither fits, so +`install_shim` teaches pyatv's `open_source` one more input: a `PcmRequest`, +answered with an `FfmpegSource` built at the sample rate, channel count and +sample size the receiver actually negotiated. Every other input still goes to +pyatv's own code. + +A `PcmRequest` is also how the GUI thread watches the stream: the source counts +frames into it, and the sink turns that count into a position. +""" + +import asyncio +import time + +from lintunes.airplay.support import ffmpeg_command + + +class PcmRequest: + """One song from one position, plus what happened to it. + + Written from the loop thread, read from the GUI thread; every field is a + plain int/float/bool/str, so a read is never torn. + """ + + def __init__(self, path: str, start_ms: int, duration_ms: int): + self.path = path + self.start_ms = max(0, int(start_ms)) + self.duration_ms = max(0, int(duration_ms)) + self.sample_rate = 44100 + self.frames_read = 0 + self.first_read_at: float | None = None # time.monotonic() + self.exhausted = False # ffmpeg reached the end of the song + self.decode_error = "" # ffmpeg failed before producing any audio + self._process = None + + def elapsed_ms(self) -> float | None: + if self.first_read_at is None: + return None + return (time.monotonic() - self.first_read_at) * 1000.0 + + def kill(self): + process, self._process = self._process, None + if process is not None and process.returncode is None: + try: + process.kill() + except ProcessLookupError: + pass + + +_source_class = None + + +def _ffmpeg_source(): + """FfmpegSource subclasses pyatv's AudioSource, so it is defined on first + use rather than at import (see the package docstring).""" + global _source_class + if _source_class is not None: + return _source_class + from pyatv.interface import MediaMetadata + from pyatv.protocols.raop.audio_source import AudioSource + + class FfmpegSource(AudioSource): + def __init__(self, request, process, sample_rate, channels, + sample_size): + self._request = request + self._process = process + self._rate = sample_rate + self._channels = channels + self._size = sample_size + self._frame = channels * sample_size + + @classmethod + async def open(cls, request, sample_rate, channels, sample_size): + process = await asyncio.create_subprocess_exec( + *ffmpeg_command(request.path, request.start_ms, sample_rate, + channels, sample_size), + stdin=asyncio.subprocess.DEVNULL, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE) + request._process = process + request.sample_rate = sample_rate + return cls(request, process, sample_rate, channels, sample_size) + + async def readframes(self, nframes: int) -> bytes: + request = self._request + if request.exhausted or request.decode_error: + return self.NO_FRAMES + try: + data = await self._process.stdout.readexactly( + nframes * self._frame) + except asyncio.IncompleteReadError as e: + data = e.partial + await self._finish(bool(data)) + # A trailing partial frame would shift every sample after it. + data = data[:len(data) - len(data) % self._frame] + if data: + if request.first_read_at is None: + request.first_read_at = time.monotonic() + request.frames_read += len(data) // self._frame + return data + + async def _finish(self, got_data: bool): + """End of ffmpeg's output: the song's end, or a failure if it + never produced a frame (missing file, unreadable codec).""" + code = await self._process.wait() + request = self._request + if code != 0 and request.frames_read == 0 and not got_data: + stderr = await self._process.stderr.read() + lines = stderr.decode(errors="replace").strip().splitlines() + request.decode_error = (lines[-1] if lines + else f"ffmpeg exited with {code}") + else: + request.exhausted = True + + async def close(self) -> None: + self._request.kill() + + async def get_metadata(self): + return MediaMetadata() + + @property + def sample_rate(self) -> int: + return self._rate + + @property + def channels(self) -> int: + return self._channels + + @property + def sample_size(self) -> int: + return self._size + + @property + def duration(self) -> int: + # What's left from the start position: the receiver's progress + # bar runs over this stream, not over the whole song. + remaining = self._request.duration_ms - self._request.start_ms + return max(0, round(remaining / 1000)) + + _source_class = FfmpegSource + return FfmpegSource + + +def install_shim(): + """Route a PcmRequest through `stream_file` to an FfmpegSource. + + Patches the name the RAOP module looked up at import, which is what + `RaopStream.stream_file` calls. Idempotent. + """ + import pyatv.protocols.raop as raop + if getattr(raop.open_source, "_lintunes", False): + return + original = raop.open_source + + async def open_source(source, sample_rate, channels, sample_size): + if isinstance(source, PcmRequest): + return await _ffmpeg_source().open( + source, sample_rate, channels, sample_size) + return await original(source, sample_rate, channels, sample_size) + + open_source._lintunes = True + raop.open_source = open_source diff --git a/lintunes/airplay/support.py b/lintunes/airplay/support.py new file mode 100644 index 0000000..99f3ea1 --- /dev/null +++ b/lintunes/airplay/support.py @@ -0,0 +1,135 @@ +"""Pure helpers for AirPlay: availability, formats, volume, position. + +Free of pyatv and the network, so the fiddly arithmetic is unit-tested +offline (the cast/support.py pattern). +""" + +import math +import shutil +from pathlib import Path + +# pyatv's own buffering: RAOP receivers are told to play each packet +# ``22050 + sample_rate`` frames after it is sent (stream_client's +# StreamContext.latency), so what is heard trails what was sent by ~1.5 s. +def latency_ms(sample_rate: int) -> float: + return (22050 + sample_rate) / sample_rate * 1000.0 + + +# ffmpeg decodes everything the desktop plays; only FairPlay is out of reach. +_REFUSED_KINDS = {"Protected AAC audio file": "copy-protected AAC"} +_REFUSED_SUFFIXES = {".m4p": "copy-protected AAC"} + +# AirPlay's volume is dB: 0 is full, -30 is pyatv's floor (its 0–100 % maps +# linearly onto -30..0 dB), -144 is mute. +DBFS_FLOOR = -30.0 + + +def is_available() -> tuple[bool, str]: + """(True, "") when pyatv is importable and ffmpeg is on PATH. + + find_spec, not an import: the menu asks on every open, and pyatv pulls in + aiohttp, cryptography and protobuf. + """ + import importlib.util + try: + if importlib.util.find_spec("pyatv") is None: + return False, "pyatv isn't installed" + except (ImportError, ValueError): + return False, "pyatv isn't installed" + if shutil.which("ffmpeg") is None: + return False, "ffmpeg isn't installed (AirPlay decodes with it)" + return True, "" + + +def can_stream(location: str, kind: str = "") -> bool: + if not location: + return False + if kind in _REFUSED_KINDS: + return False + return Path(location).suffix.lower() not in _REFUSED_SUFFIXES + + +def unstreamable_reason(location: str, kind: str = "") -> str: + if not location: + return "it has no file" + if kind in _REFUSED_KINDS: + return f"AirPlay can't play {_REFUSED_KINDS[kind]}" + suffix = Path(location).suffix.lower() + return f"AirPlay can't play {_REFUSED_SUFFIXES.get(suffix, 'this file')}" + + +def classify(has_raop: bool, pairing: str, requires_password: bool + ) -> tuple[bool, str]: + """Can lintunes stream to this device? (yes, "") or (no, why not). + + `pairing` is the name of pyatv's PairingRequirement for the RAOP service. + Only receivers that take a stream from anyone are offered: pairing (a PIN + on a TV) and passwords aren't built yet. A device with AirPlay but no RAOP + is a video/screen-mirroring target with no audio-only endpoint. + """ + if not has_raop: + return False, "video only" + if requires_password: + return False, "needs a password" + if pairing == "Mandatory": + return False, "needs pairing" + if pairing in ("Unsupported", "Disabled"): + return False, "doesn't accept LinTunes" + return True, "" + + +def level_to_percent(linear: float) -> float: + """pyatv's volume percent for a *linear* amplitude 0..1. + + Player hands sinks a perceptual slider level; the sink converts that to + linear the way LocalSink does (Qt's log→linear curve), and this turns the + linear gain into the same dB on the speaker, so the slider means one + loudness whichever output is playing. Anything under pyatv's -30 dB floor + is 0 % (silent) — the bottom of Qt's curve is that quiet locally too. + """ + if linear <= 0: + return 0.0 + db = 20.0 * math.log10(min(1.0, linear)) + return max(0.0, min(100.0, (db - DBFS_FLOOR) / -DBFS_FLOOR * 100.0)) + + +def heard_ms(start_ms: int, frames_read: int, elapsed_ms: float | None, + sample_rate: int) -> int: + """Where the listener is in the song. + + `elapsed_ms` runs from the first frame handed to pyatv (None before + that). pyatv paces packets to real time, so elapsed minus the receiver's + latency is what has come out of the speaker — never more than what was + actually read, which is what holds the position at the end while pyatv + sends its trailing silence. + """ + if elapsed_ms is None or sample_rate <= 0: + return start_ms + read_ms = frames_read / sample_rate * 1000.0 + played = max(0.0, min(read_ms, elapsed_ms - latency_ms(sample_rate))) + return start_ms + int(played) + + +_SAMPLE_FORMATS = {2: "s16be", 4: "s32be"} + + +def ffmpeg_command(path: str, start_ms: int, sample_rate: int, channels: int, + sample_size: int) -> list[str]: + """Decode `path` from `start_ms` to raw PCM on stdout, in the shape the + receiver asked for. + + *Big*-endian: pyatv's own sources byteswap little-endian samples before + sending (audio_source._to_audio_samples), so asking ffmpeg for BE is the + same bytes on the wire. `-ss` before `-i` is an input seek, which is + sample-accurate for audio and instant even deep into a long file. + """ + fmt = _SAMPLE_FORMATS.get(sample_size) + if fmt is None: + raise ValueError(f"unsupported sample size {sample_size}") + command = ["ffmpeg", "-nostdin", "-hide_banner", "-loglevel", "error"] + if start_ms > 0: + command += ["-ss", f"{start_ms / 1000.0:.3f}"] + command += ["-i", path, "-map", "0:a:0", "-vn", + "-f", fmt, "-ac", str(channels), "-ar", str(sample_rate), + "pipe:1"] + return command diff --git a/lintunes/gui/cast_dialog.py b/lintunes/gui/cast_dialog.py index c58c94d..047fd79 100644 --- a/lintunes/gui/cast_dialog.py +++ b/lintunes/gui/cast_dialog.py @@ -1,6 +1,6 @@ -"""The "Connect to Chromecast…" picker. +"""The "Connect to Chromecast…" and "Connect to AirPlay…" pickers. -Searching starts when the dialog opens and stops when it closes, whichever way +One dialog over either discovery (they share a signal shape). Searching starts when the dialog opens and stops when it closes, whichever way it closes. Connecting is not awaited here — the dialog's job ends at "which device", and CastController reports the outcome on the status bar. """ @@ -17,22 +17,30 @@ from lintunes.gui.spinner import Spinner EMPTY_AFTER_MS = 12_000 -class ChromecastDialog(QDialog): - """Lists Chromecasts as they're discovered; returns the one picked.""" +class DevicePickerDialog(QDialog): + """Lists devices as they're discovered; returns the one picked. + + A device may carry `connectable=False` and a `note`: it is listed greyed + with the reason rather than hidden, so a speaker that can't be used + doesn't look like one that wasn't found. + """ + + noun = "device" # "Searching for s…" + title = "Connect" def __init__(self, discovery, parent=None): super().__init__(parent) self._discovery = discovery self._selected = None - self.setWindowTitle("Connect to Chromecast") + self.setWindowTitle(self.title) self.setMinimumWidth(340) layout = QVBoxLayout(self) header = QHBoxLayout() self._spinner = Spinner() header.addWidget(self._spinner) - self._status = QLabel("Searching for Chromecasts…") + self._status = QLabel(f"Searching for {self.noun}s…") header.addWidget(self._status, stretch=1) layout.addLayout(header) @@ -81,12 +89,19 @@ class ChromecastDialog(QDialog): label = device.name if device.model: label = f"{device.name} · {device.model}" + connectable = getattr(device, "connectable", True) + if not connectable: + label = f"{label} — {device.note}" item = QListWidgetItem(label) item.setData(Qt.ItemDataRole.UserRole, device) + if not connectable: + item.setFlags(item.flags() & ~(Qt.ItemFlag.ItemIsEnabled + | Qt.ItemFlag.ItemIsSelectable)) self._list.addItem(item) - self._status.setText("Select a Chromecast to connect to.") - if self._list.count() == 1: - self._list.setCurrentRow(0) + if connectable: + self._status.setText(f"Select a {self.noun} to connect to.") + if self._list.currentItem() is None: + self._list.setCurrentItem(item) def _on_lost(self, device): for row in range(self._list.count()): @@ -95,7 +110,7 @@ class ChromecastDialog(QDialog): self._list.takeItem(row) break if self._list.count() == 0: - self._status.setText("Searching for Chromecasts…") + self._status.setText(f"Searching for {self.noun}s…") def _on_failed(self, message: str): self._spinner.hide() @@ -106,16 +121,22 @@ class ChromecastDialog(QDialog): if self._list.count() == 0: # The spinner keeps turning: discovery is still running, and a # device that comes online in a minute will still appear. - self._status.setText("No Chromecasts found on this network yet.") + self._status.setText(f"No {self.noun}s found on this network yet.") + elif not any(self._list.item(row).flags() & Qt.ItemFlag.ItemIsEnabled + for row in range(self._list.count())): + self._status.setText( + f"No {self.noun}s LinTunes can play to found yet.") # ---- selection ---- def _on_selection_changed(self): - self._connect_button.setEnabled(self._list.currentItem() is not None) + item = self._list.currentItem() + self._connect_button.setEnabled( + item is not None and bool(item.flags() & Qt.ItemFlag.ItemIsEnabled)) def _on_connect(self): item = self._list.currentItem() - if item is None: + if item is None or not item.flags() & Qt.ItemFlag.ItemIsEnabled: return self._selected = item.data(Qt.ItemDataRole.UserRole) self.accept() @@ -127,3 +148,13 @@ class ChromecastDialog(QDialog): self._spinner.stop() self._discovery.stop() super().done(result) + + +class ChromecastDialog(DevicePickerDialog): + noun = "Chromecast" + title = "Connect to Chromecast" + + +class AirPlayDialog(DevicePickerDialog): + noun = "AirPlay speaker" + title = "Connect to AirPlay" diff --git a/lintunes/gui/icons.py b/lintunes/gui/icons.py index 69b14ce..86fda6b 100644 --- a/lintunes/gui/icons.py +++ b/lintunes/gui/icons.py @@ -102,6 +102,25 @@ def _cast(p, color): p.drawEllipse(QPointF(3.8, 15.2), 1.5, 1.5) +def _airplay(p, color): + """The AirPlay glyph: a screen whose bottom edge opens around a solid + triangle rising into it.""" + pen = QPen(color, 1.6) + pen.setCapStyle(Qt.PenCapStyle.RoundCap) + pen.setJoinStyle(Qt.PenJoinStyle.RoundJoin) + p.setPen(pen) + p.setBrush(Qt.BrushStyle.NoBrush) + p.drawPolyline(QPolygonF([ + QPointF(6.0, 14.0), QPointF(3.5, 14.0), QPointF(3.5, 3.5), + QPointF(16.5, 3.5), QPointF(16.5, 14.0), QPointF(14.0, 14.0), + ])) + p.setPen(Qt.PenStyle.NoPen) + p.setBrush(color) + p.drawPolygon(QPolygonF([ + QPointF(10.0, 11.0), QPointF(14.5, 17.0), QPointF(5.5, 17.0), + ])) + + _DRAWERS = { "play": _play, "pause": _pause, @@ -109,4 +128,5 @@ _DRAWERS = { "previous": _previous, "shuffle": _shuffle, "cast": _cast, + "airplay": _airplay, } diff --git a/lintunes/gui/main_window.py b/lintunes/gui/main_window.py index 6b4b997..8436100 100644 --- a/lintunes/gui/main_window.py +++ b/lintunes/gui/main_window.py @@ -15,6 +15,7 @@ from lintunes.andtunes import layout as andtunes_layout from lintunes.andtunes import install as andtunes_install from lintunes.andtunes import sync as andtunes_sync from lintunes.art_search import AlbumArtFetcher +from lintunes.airplay.controller import AirPlayController from lintunes.cast.controller import CastController from lintunes.eventlog import log_control from lintunes.config import load_config, save_config @@ -26,7 +27,7 @@ from lintunes.storage import listen_history from lintunes.importers import file_importer from lintunes.gui.album_art_dialog import AlbumArtDialog from lintunes.gui.art_ops import embed_artwork -from lintunes.gui.cast_dialog import ChromecastDialog +from lintunes.gui.cast_dialog import AirPlayDialog, ChromecastDialog from lintunes.gui.cassette_ui import CassetteUi from lintunes.gui import sounds from lintunes.gui.device_sync_dialog import ( @@ -120,6 +121,9 @@ class MainWindow(QMainWindow): # Owns the cast session, if any. Built before the transport bar, which # takes it to drive the little cast indicator under the volume slider. self._cast = CastController(self.player, self) + # The same for AirPlay. At most one of the two is ever connected: + # connecting either disconnects the other first. + self._airplay = AirPlayController(self.player, self) # Where the Rabbit is, found off the GUI thread: a wedged gvfs-MTP # mount blocks any listing of it forever (Round 71). Nothing probes # until the Connections menu opens. @@ -152,7 +156,7 @@ class MainWindow(QMainWindow): self.setCentralWidget(central) self._transport = TransportBar(self.player, manager, self._prefs, - cast=self._cast) + cast=self._cast, airplay=self._airplay) layout.addWidget(self._transport) splitter = QSplitter(Qt.Orientation.Horizontal) @@ -329,6 +333,15 @@ class MainWindow(QMainWindow): self._cast.disconnected.connect(self._on_cast_disconnected) self._cast.failed.connect( lambda msg: QMessageBox.warning(self, "Chromecast", msg)) + self._airplay.connecting.connect(self._on_cast_connecting) + self._airplay.connected.connect( + lambda name: self.statusBar().showMessage( + f"Playing on {name} (AirPlay)", 6000)) + self._airplay.disconnected.connect( + lambda reason: self.statusBar().showMessage( + reason or "Stopped AirPlay — playing here again", 8000)) + self._airplay.failed.connect( + lambda msg: QMessageBox.warning(self, "AirPlay", msg)) manager.library_reloaded.connect(self._on_library_reloaded) manager.conflict_resolved.connect(self._on_conflict_resolved) prefs.changed.connect(self._on_prefs_changed) @@ -492,6 +505,8 @@ class MainWindow(QMainWindow): self._cassette.build_menu(connections_menu) self._cast_action = self._add_action( connections_menu, "Connect to Chromecast…", "", self._toggle_cast) + self._airplay_action = self._add_action( + connections_menu, "Connect to AirPlay…", "", self._toggle_airplay) # Re-checked every time the menu opens. The device part is a # background probe; its answer updates the open menu when it lands. connections_menu.aboutToShow.connect(self._refresh_connection_actions) @@ -565,19 +580,22 @@ class MainWindow(QMainWindow): and not (self._install_worker is not None and self._install_worker.busy())) - if self._cast.is_connected(): - self._cast_action.setText( - f"Disconnect from {self._cast.device_name()}") - self._cast_action.setEnabled(True) - self._cast_action.setToolTip("") - return - self._cast_action.setText("Connect to Chromecast…") - available, why = self._cast.is_available() - self._cast_action.setEnabled(available) # The self-updater ships code but not dependencies, so a machine that - # has pulled this round hasn't necessarily installed pychromecast yet. - self._cast_action.setToolTip( - "" if available else f"{why} — run: pip install -e .") + # has pulled this round hasn't necessarily installed pychromecast (or + # pyatv) yet. + for action, controller, text in ( + (self._cast_action, self._cast, "Connect to Chromecast…"), + (self._airplay_action, self._airplay, "Connect to AirPlay…")): + if controller.is_connected(): + action.setText(f"Disconnect from {controller.device_name()}") + action.setEnabled(True) + action.setToolTip("") + continue + action.setText(text) + available, why = controller.is_available() + action.setEnabled(available) + action.setToolTip( + "" if available else f"{why} — run: pip install -e .") # ---- connections: casting ---- @@ -594,8 +612,25 @@ class MainWindow(QMainWindow): return dialog = ChromecastDialog(self._cast.discovery(), self) if dialog.exec() and dialog.selected_device is not None: + self._airplay.disconnect() self._cast.connect_to(dialog.selected_device) + def _toggle_airplay(self): + if self._airplay.is_connected(): + self._airplay.disconnect() + return + available, why = self._airplay.is_available() + if not available: + QMessageBox.information( + self, "AirPlay support not installed", + f"{why}.\n\nRun “pip install -e .” in the lintunes checkout " + "(and install ffmpeg if it's missing), then restart LinTunes.") + return + dialog = AirPlayDialog(self._airplay.discovery(), self) + if dialog.exec() and dialog.selected_device is not None: + self._cast.disconnect() + self._airplay.connect_to(dialog.selected_device) + def _on_cast_connecting(self, device): self.statusBar().showMessage(f"Connecting to {device.name}…", 8000) @@ -2270,7 +2305,8 @@ class MainWindow(QMainWindow): self._manager.record_listen(listen_history.entry_for( track, started=started or listen_history.local_now(), heard_ms=heard_ms, how=how, - src="desktop" if self.player.is_local else "cast", + src=("desktop" if self.player.is_local else + getattr(self.player.active_sink(), "listen_src", "cast")), ctx=ctx, shuffle=self.player.is_shuffle(), track_id=None if isinstance(key, str) else key)) @@ -2384,6 +2420,7 @@ class MainWindow(QMainWindow): # is fetching from. Skipped, the Chromecast sits buffering a dead URL. self._cassette.shutdown() self._cast.shutdown() + self._airplay.shutdown() self.player.shutdown() self.player.loudness.save() self._manager.flush() diff --git a/lintunes/gui/transport.py b/lintunes/gui/transport.py index dcd5afb..d126264 100644 --- a/lintunes/gui/transport.py +++ b/lintunes/gui/transport.py @@ -360,7 +360,8 @@ class TransportBar(QWidget): play_clicked = pyqtSignal() # MainWindow decides what "play" means - def __init__(self, player, manager, prefs, cast=None, parent=None): + def __init__(self, player, manager, prefs, cast=None, airplay=None, + parent=None): super().__init__(parent) self._player = player self._prefs = prefs @@ -543,8 +544,11 @@ class TransportBar(QWidget): if cast is not None: # While casting, the visualizer panel *is* the cast indicator — # there is no spectrum to show, so a click there stops casting. + # Only one of them is ever connected; the other's disconnect is + # a no-op. self._visualizer.cast_stop_requested.connect( - lambda: cast.disconnect()) + lambda: (cast.disconnect(), + airplay is not None and airplay.disconnect())) # ---- the header row ---- diff --git a/lintunes/gui/visualizer.py b/lintunes/gui/visualizer.py index fb28e50..09e192d 100644 --- a/lintunes/gui/visualizer.py +++ b/lintunes/gui/visualizer.py @@ -93,6 +93,7 @@ class VisualizerWidget(QWidget): # (a Chromecast), so there is no decoded PCM to draw. self._no_pcm = False self._device = "" + self._glyph = "cast" self.setFixedWidth(140) self.setMinimumHeight(36) @@ -142,9 +143,13 @@ class VisualizerWidget(QWidget): went, and clicking it stops casting.""" self._no_pcm = not getattr(sink, "provides_pcm", True) if self._no_pcm: + # A sink names its own glyph: "cast" for a Chromecast, "airplay" + # for an AirPlay speaker. + self._glyph = getattr(sink, "glyph", "cast") self._device = getattr(sink, "name", "") or "a Chromecast" self._clear() - self.setToolTip(f"Casting to {self._device} — click to stop") + verb = "Playing on" if self._glyph == "airplay" else "Casting to" + self.setToolTip(f"{verb} {self._device} — click to stop") self.setCursor(Qt.CursorShape.PointingHandCursor) else: self._device = "" @@ -505,7 +510,7 @@ class VisualizerWidget(QWidget): # Casting: the audio never passes through this machine, so show # where it went instead of pretending to analyze silence. glyph = transport_icon( - "cast", self.palette().highlight().color(), + self._glyph, self.palette().highlight().color(), size=ICON_SIZE).pixmap(ICON_SIZE, ICON_SIZE) painter.drawPixmap( (self.width() - ICON_SIZE) // 2, diff --git a/setup.py b/setup.py index dcc5c84..4701b83 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,7 @@ setup( "numpy>=1.24", "requests>=2.28", "pychromecast>=14.0.10", + "pyatv>=0.18.0", # AirPlay (RAOP) streaming; decodes via the ffmpeg CLI ], data_files=[ ("share/applications", ["packaging/lintunes.desktop"]), diff --git a/tests/test_round87.py b/tests/test_round87.py new file mode 100644 index 0000000..ecb4cfe --- /dev/null +++ b/tests/test_round87.py @@ -0,0 +1,491 @@ +"""Round 87: Connect to AirPlay. + +The sink is driven against a fake pyatv session on the real AsyncLoop thread; +the ffmpeg source decodes real files. Nothing here touches the network — +pyatv's scan and connect are never called. +""" + +import asyncio +import time +from types import SimpleNamespace +from unittest.mock import MagicMock, patch + +import pytest +from PyQt6.QtCore import QObject, pyqtSignal +from PyQt6.QtGui import QColor, QImage + +from lintunes import player as player_module +from lintunes.airplay import support +from lintunes.airplay.discovery import AirPlayDevice +from lintunes.airplay.loop import AsyncLoop +from lintunes.gui.cast_dialog import AirPlayDialog, ChromecastDialog +from lintunes.models.library import Library +from lintunes.player import Player + + +def _until(qapp, condition, timeout=3.0): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + qapp.processEvents() + if condition(): + return True + time.sleep(0.005) + qapp.processEvents() + return condition() + + +# ---- pure helpers ---- + +class TestSupport: + def test_only_receivers_that_take_anyone_are_connectable(self): + assert support.classify(True, "NotNeeded", False) == (True, "") + assert support.classify(True, "Optional", False) == (True, "") + assert support.classify(True, "Mandatory", False) == (False, + "needs pairing") + assert support.classify(True, "NotNeeded", True)[0] is False + assert support.classify(True, "Unsupported", False)[0] is False + # AirPlay without RAOP is a screen to mirror to, not a speaker. + assert support.classify(False, "", False) == (False, "video only") + + def test_volume_is_the_same_db_as_the_local_output(self): + assert support.level_to_percent(0.0) == 0.0 + assert support.level_to_percent(1.0) == 100.0 + # Half amplitude is -6 dB: 24/30 of the way up pyatv's -30..0 scale. + assert support.level_to_percent(0.5) == pytest.approx(79.93, abs=0.01) + # Below pyatv's floor is silence, never a negative percent. + assert support.level_to_percent(0.01) == 0.0 + + def test_position_is_what_was_heard_not_what_was_sent(self): + rate = 44100 + latency = support.latency_ms(rate) + assert latency == pytest.approx(1500, abs=1) + # Nothing read yet: still at the start position. + assert support.heard_ms(5000, 0, None, rate) == 5000 + # Inside the receiver's latency nothing has come out yet. + assert support.heard_ms(5000, rate, 1000, rate) == 5000 + assert support.heard_ms(0, 10 * rate, 3500, rate) == 2000 + # Capped at what was read: pyatv sends trailing silence after the + # song, and the position must not run past it. + assert support.heard_ms(0, 4 * rate, 9000, rate) == 4000 + + def test_ffmpeg_seeks_only_when_asked_and_sends_big_endian(self): + cmd = support.ffmpeg_command("/m/a.m4a", 0, 44100, 2, 2) + assert "-ss" not in cmd + assert cmd[cmd.index("-f") + 1] == "s16be" + assert cmd[-1] == "pipe:1" + cmd = support.ffmpeg_command("/m/a.m4a", 61_500, 44100, 2, 2) + # Before -i: an input seek, instant however deep into the file. + assert cmd.index("-ss") < cmd.index("-i") + assert cmd[cmd.index("-ss") + 1] == "61.500" + + def test_everything_but_fairplay_streams(self): + assert support.can_stream("/m/a.m4a", "Apple Lossless audio file") + assert support.can_stream("/m/a.aiff", "AIFF audio file") + assert not support.can_stream("/m/a.m4p", "") + assert not support.can_stream("/m/a.m4a", "Protected AAC audio file") + assert not support.can_stream("", "") + assert "copy-protected" in support.unstreamable_reason( + "/m/a.m4a", "Protected AAC audio file") + + +# ---- the ffmpeg source ---- + +def _drain(request, rate=44100): + from lintunes.airplay.stream import _ffmpeg_source + + async def run(): + source = await _ffmpeg_source().open(request, rate, 2, 2) + total = b"" + while True: + chunk = await source.readframes(352) + if not chunk: + break + total += chunk + await source.close() + return total + return asyncio.run(run()) + + +class TestFfmpegSource: + @pytest.fixture(autouse=True) + def _needs_pyatv(self): + pytest.importorskip("pyatv") + + def test_decodes_a_whole_song(self, m4a_file): + from lintunes.airplay.stream import PcmRequest + request = PcmRequest(str(m4a_file), 0, 1000) + data = _drain(request) + assert request.exhausted and not request.decode_error + assert request.frames_read == len(data) // 4 + # A 1 s AAC fixture, give or take the encoder's priming frames. + assert request.frames_read == pytest.approx(44100, abs=2048) + assert request.first_read_at is not None + + def test_starts_from_an_offset(self, flac_file): + from lintunes.airplay.stream import PcmRequest + request = PcmRequest(str(flac_file), 500, 1000) + _drain(request) + assert request.frames_read == pytest.approx(22050, abs=64) + + def test_an_unreadable_file_is_a_decode_error(self, tmp_path): + from lintunes.airplay.stream import PcmRequest + request = PcmRequest(str(tmp_path / "gone.mp3"), 0, 1000) + assert _drain(request) == b"" + assert request.decode_error + assert not request.exhausted + + def test_shim_routes_only_our_requests(self): + import pyatv.protocols.raop as raop + from lintunes.airplay.stream import install_shim + install_shim() + patched = raop.open_source + install_shim() + assert raop.open_source is patched # idempotent + assert patched._lintunes + + +# ---- the sink, against a fake pyatv session ---- + +class _FakeAtv: + def __init__(self): + self.log = [] + self.behavior = "finish" + self.closed = 0 + outer = self + + class Stream: + async def stream_file(self, request, metadata=None): + outer.log.append(("stream", request.start_ms, metadata.title)) + behavior = outer.behavior + if behavior == "finish": + request.frames_read = 4410 + request.exhausted = True + elif behavior == "hang": + try: + await asyncio.sleep(3600) + except asyncio.CancelledError: + outer.log.append(("cancelled", request.start_ms)) + raise + elif behavior == "decode": + request.decode_error = "Invalid data found" + elif behavior == "drop": + request.frames_read = 1000 + elif behavior == "unreachable": + raise ConnectionRefusedError("refused") + + class Audio: + async def set_volume(self, percent): + outer.log.append(("volume", round(percent, 1))) + + self.stream = Stream() + self.audio = Audio() + + def close(self): + self.closed += 1 + return set() + + def kinds(self): + return [entry[0] for entry in self.log] + + +def _track(location="/m/song.mp3", total_time=4000): + return SimpleNamespace(location=location, kind="MPEG audio file", + name="Song", artist="Artist", album="Album", + total_time=total_time) + + +@pytest.fixture +def loop(): + return AsyncLoop() + + +@pytest.fixture +def sink_and_atv(qapp, loop): + pytest.importorskip("pyatv") + from lintunes.airplay.sink import AirPlaySink + atv = _FakeAtv() + with patch("lintunes.airplay.sink.tagging.read_embedded_artwork", + return_value=None): + sink = AirPlaySink(atv, loop, "tiat.place") + events = [] + sink.ended.connect(lambda: events.append("ended")) + sink.error.connect(lambda m: events.append(("error", m))) + sink.unavailable.connect(lambda m: events.append(("unavailable", m))) + yield sink, atv, events + sink.shutdown() + + +class TestAirPlaySink: + def test_a_finished_song_ends_once(self, qapp, sink_and_atv): + sink, atv, events = sink_and_atv + sink.load(_track(), autoplay=True, start_ms=0) + assert sink.is_playing() + assert _until(qapp, lambda: events) + assert events == ["ended"] + assert not sink.is_playing() + assert sink.position_ms() == 4000 + assert ("stream", 0, "Song") in atv.log + + def test_volume_reaches_the_speaker_before_the_stream( + self, qapp, sink_and_atv): + sink, atv, events = sink_and_atv + sink.set_volume(1.0) + sink.load(_track(), autoplay=True, start_ms=0) + assert _until(qapp, lambda: "stream" in atv.kinds()) + assert atv.log[0] == ("volume", 100.0) + assert atv.kinds().index("volume") < atv.kinds().index("stream") + + def test_loading_paused_streams_nothing(self, qapp, sink_and_atv): + sink, atv, events = sink_and_atv + sink.load(_track(), autoplay=False, start_ms=1200) + _until(qapp, lambda: False, timeout=0.2) + assert "stream" not in atv.kinds() + assert sink.position_ms() == 1200 and not sink.is_playing() + + def test_a_seek_replaces_the_stream_and_the_old_one_cannot_end_it( + self, qapp, sink_and_atv): + sink, atv, events = sink_and_atv + atv.behavior = "hang" + sink.load(_track(), autoplay=True, start_ms=0) + assert _until(qapp, lambda: ("stream", 0, "Song") in atv.log) + sink.seek(3000) + assert sink.position_ms() == 3000 + assert _until(qapp, lambda: ("stream", 3000, "Song") in atv.log) + assert ("cancelled", 0) in atv.log + # The old stream was cancelled, never finished: no queue advance. + assert events == [] + assert sink.is_playing() + + def test_pause_stops_the_stream_and_resume_starts_where_it_was( + self, qapp, sink_and_atv): + sink, atv, events = sink_and_atv + atv.behavior = "hang" + sink.load(_track(), autoplay=True, start_ms=2000) + assert _until(qapp, lambda: ("stream", 2000, "Song") in atv.log) + sink.pause() + assert not sink.is_playing() + assert _until(qapp, lambda: ("cancelled", 2000) in atv.log) + paused_at = sink.position_ms() + sink.play() + assert _until(qapp, lambda: atv.kinds().count("stream") == 2) + assert atv.log[-1] == ("stream", paused_at, "Song") + assert events == [] + + def test_an_undecodable_song_is_skipped_not_a_lost_speaker( + self, qapp, sink_and_atv): + sink, atv, events = sink_and_atv + atv.behavior = "decode" + sink.load(_track(), autoplay=True, start_ms=0) + assert _until(qapp, lambda: events) + assert events[0][0] == "error" + assert "Invalid data" in events[0][1] + + @pytest.mark.parametrize("behavior, says", [ + ("drop", "something else"), # pyatv returned before ffmpeg's end + ("unreachable", "Lost"), + ]) + def test_a_lost_speaker_hands_back_to_this_machine( + self, qapp, sink_and_atv, behavior, says): + sink, atv, events = sink_and_atv + atv.behavior = behavior + sink.load(_track(), autoplay=True, start_ms=0) + assert _until(qapp, lambda: events) + kind, reason = events[0] + assert kind == "unavailable" and says in reason + # Still "playing": Player falls back to the local output carrying on. + assert sink.is_playing() + + def test_shutdown_closes_the_session(self, qapp, sink_and_atv): + sink, atv, events = sink_and_atv + sink.shutdown() + sink.shutdown() + assert atv.closed == 1 + + def test_artwork_is_sent_as_a_small_jpeg(self, qapp): + from lintunes.airplay.sink import ART_MAX_PX, jpeg_artwork + from PyQt6.QtCore import QBuffer, QIODevice + image = QImage(1200, 800, QImage.Format.Format_RGB32) + image.fill(QColor("red")) + buffer = QBuffer() + buffer.open(QIODevice.OpenModeFlag.WriteOnly) + image.save(buffer, "PNG") + out = jpeg_artwork(bytes(buffer.data())) + assert out[:3] == b"\xff\xd8\xff" + decoded = QImage() + decoded.loadFromData(out) + assert max(decoded.width(), decoded.height()) == ART_MAX_PX + assert jpeg_artwork(None) is None + assert jpeg_artwork(b"not an image") is None + + +# ---- the controller ---- + +def _player(qapp): + with patch.multiple(player_module, QMediaPlayer=MagicMock(), + QAudioOutput=MagicMock(), + QAudioBufferOutput=MagicMock(), + QMediaDevices=MagicMock()): + return Player(MagicMock(library=Library())) + + +class TestController: + def test_connect_hands_player_the_sink_and_disconnect_takes_it_back( + self, qapp): + pytest.importorskip("pyatv") + from lintunes.airplay.controller import AirPlayController + player = _player(qapp) + controller = AirPlayController(player) + controller._inhibitor = MagicMock() + seen = [] + controller.connected.connect(seen.append) + atv = _FakeAtv() + + controller._attempt = 1 + controller._on_connect_result({"atv": atv, "name": "tiat.place"}, 1) + assert controller.is_connected() + assert player.active_sink().name == "tiat.place" + assert player.active_sink().listen_src == "airplay" + assert seen == ["tiat.place"] + + controller.disconnect() + assert player.is_local + assert not controller.is_connected() + assert atv.closed == 1 + + def test_a_superseded_connect_is_closed_not_installed(self, qapp): + pytest.importorskip("pyatv") + from lintunes.airplay.controller import AirPlayController + player = _player(qapp) + controller = AirPlayController(player) + atv = _FakeAtv() + controller._attempt = 2 + controller._on_connect_result({"atv": atv, "name": "old pick"}, 1) + assert not controller.is_connected() + assert _until(qapp, lambda: atv.closed == 1) + + def test_raop_only_turns_the_other_services_off(self): + pytest.importorskip("pyatv") + from pyatv.const import Protocol + from lintunes.airplay.controller import raop_only + services = [SimpleNamespace(protocol=Protocol.AirPlay, enabled=True), + SimpleNamespace(protocol=Protocol.RAOP, enabled=True)] + raop_only(SimpleNamespace(services=services)) + assert [s.enabled for s in services] == [False, True] + + +# ---- discovery and the picker ---- + +class TestDiscovery: + def test_a_scan_result_becomes_a_device(self): + pytest.importorskip("pyatv") + from pyatv.const import Protocol + from lintunes.airplay.discovery import to_device + + def conf(raop, name, model): + return SimpleNamespace( + identifier="42:D9", name=name, address="192.0.2.7", + device_info=SimpleNamespace(raw_model=model), + get_service=lambda p: raop if p == Protocol.RAOP else None) + + speaker = to_device(conf(SimpleNamespace( + pairing=SimpleNamespace(name="NotNeeded"), requires_password=False, + properties={"am": "WiiM Mini Receiver"}), "Gallery", None)) + assert speaker == AirPlayDevice( + uuid="42:D9", name="Gallery", model="WiiM Mini Receiver", + host="192.0.2.7", connectable=True, note="") + + tv = to_device(conf(None, "Lobby TV", "7415X")) + assert not tv.connectable and tv.note == "video only" + assert tv.model == "7415X" + + +class _FakeDiscovery(QObject): + device_found = pyqtSignal(object) + device_lost = pyqtSignal(object) + failed = pyqtSignal(str) + + def __init__(self): + super().__init__() + self.stopped = 0 + + def devices(self): + return [] + + def start(self): + pass + + def stop(self): + self.stopped += 1 + + +def _ap(uuid, name, connectable=True, note=""): + return AirPlayDevice(uuid=uuid, name=name, model="", host="192.0.2.9", + connectable=connectable, note=note) + + +class TestPicker: + def test_unusable_devices_are_listed_greyed_with_the_reason(self, qapp): + discovery = _FakeDiscovery() + dialog = AirPlayDialog(discovery) + assert dialog.windowTitle() == "Connect to AirPlay" + assert "AirPlay speaker" in dialog._status.text() + + discovery.device_found.emit(_ap("tv", "Lobby TV", False, + "needs pairing")) + assert dialog._list.count() == 1 + assert "needs pairing" in dialog._list.item(0).text() + assert not dialog._connect_button.isEnabled() + dialog._on_connect() + assert dialog.selected_device is None + + dialog._on_empty_timeout() + assert "can play to" in dialog._status.text() + + discovery.device_found.emit(_ap("wiim", "tiat.place")) + assert dialog._connect_button.isEnabled() + dialog._on_connect() + assert dialog.selected_device.name == "tiat.place" + assert discovery.stopped == 1 + + def test_chromecast_picker_reads_as_before(self, qapp): + dialog = ChromecastDialog(_FakeDiscovery()) + assert dialog.windowTitle() == "Connect to Chromecast" + assert dialog._status.text() == "Searching for Chromecasts…" + dialog.reject() + + +# ---- the transport-bar indicator ---- + +class TestIndicator: + def test_the_visualizer_shows_airplay_and_a_click_disconnects_it( + self, qapp, tmp_path): + from lintunes.gui.transport import TransportBar + from lintunes.player import PlaybackSink + from lintunes.preferences import Preferences + + class Sink(PlaybackSink): + name = "tiat.place" + glyph = "airplay" + + def stop(self): + pass + + def set_volume(self, level): + pass + + def position_ms(self): + return 0 + + def is_playing(self): + return False + + player = _player(qapp) + cast, airplay = MagicMock(), MagicMock() + bar = TransportBar(player, MagicMock(), Preferences(tmp_path), + cast=cast, airplay=airplay) + player.set_sink(Sink()) + vis = bar._visualizer + assert vis._glyph == "airplay" + assert vis.toolTip() == "Playing on tiat.place — click to stop" + vis.mousePressEvent(None) + airplay.disconnect.assert_called_once()