diff --git a/CLAUDE.md b/CLAUDE.md index 4605a8b..ef4f8f4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -839,6 +839,11 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal 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. + pyatv picks AirPlay **2** for any receiver advertising its features (the + WiiM does) and the fake only exercises v1, so `config.json` takes + `airplay_protocol` ("auto"/"1"/"2") and `airplay_debug` (pyatv's logger + at DEBUG); every stream's start, first audio and outcome are logged at + INFO under `lintunes.airplay`, and the chosen protocol in the control log. ## Conventions & gotchas diff --git a/lintunes/__init__.py b/lintunes/__init__.py index 5c0a1bc..8c137bf 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.56.0" +__version__ = "0.56.1" diff --git a/lintunes/airplay/controller.py b/lintunes/airplay/controller.py index efa9cd6..0587fdb 100644 --- a/lintunes/airplay/controller.py +++ b/lintunes/airplay/controller.py @@ -6,11 +6,14 @@ streams, so a speaker that vanishes while paused is only noticed when the next stream can't reach it, and Player falls back then. """ +import logging + 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.airplay.support import is_available, protocol_choice +from lintunes.config import load_config from lintunes.eventlog import log_control from lintunes.inhibit import INHIBIT_LOGOUT, INHIBIT_SUSPEND, SleepInhibitor @@ -90,6 +93,7 @@ class AirPlayController(QObject): async def _connect(self, conf, device): import asyncio import pyatv + from pyatv.const import Protocol loop = asyncio.get_running_loop() if conf is None: # Not seen by this session's scans (or seen long ago): ask the @@ -99,8 +103,29 @@ class AirPlayController(QObject): if str(c.identifier) == device.uuid), None) if conf is None: raise ConnectionError(f"{device.name} didn't answer") + from pyatv.protocols.airplay.utils import get_protocol_version + from pyatv.settings import AirPlayVersion + from pyatv.storage.memory_storage import MemoryStorage + config = load_config() + choice = protocol_choice(config.get("airplay_protocol")) + if config.get("airplay_debug"): + # Every RTSP request and a line per second of audio, into the + # journal: what "connected but silent" needs to be diagnosed. + logging.getLogger("pyatv").setLevel(logging.DEBUG) + storage = MemoryStorage() + settings = await storage.get_settings(conf) + settings.protocols.raop.protocol_version = { + "1": AirPlayVersion.V1, "2": AirPlayVersion.V2, + }.get(choice, AirPlayVersion.Auto) + raop = conf.get_service(Protocol.RAOP) + if raop is not None: + used = get_protocol_version( + raop, settings.protocols.raop.protocol_version) + log_control("airplay", "protocol", + f"{device.name}: {used.name} (airplay_protocol={choice})") return await asyncio.wait_for( - pyatv.connect(raop_only(conf), loop), CONNECT_TIMEOUT_S) + pyatv.connect(raop_only(conf), loop, storage=storage), + CONNECT_TIMEOUT_S) def _on_connect_result(self, result: dict, attempt: int): if attempt != self._attempt: diff --git a/lintunes/airplay/sink.py b/lintunes/airplay/sink.py index 367aef8..c45dc91 100644 --- a/lintunes/airplay/sink.py +++ b/lintunes/airplay/sink.py @@ -12,6 +12,7 @@ keeps in its PcmRequest. """ import asyncio +import logging from contextlib import suppress from PyQt6.QtCore import QBuffer, QIODevice, QTimer, Qt, pyqtSignal @@ -30,7 +31,10 @@ 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 +ART_MAX_PX = 600 + +# Each stream's life in the journal: started, first audio, how it ended. +log = logging.getLogger("lintunes.airplay") # receivers show a small square; a 3000 px scan is waste def jpeg_artwork(data: bytes | None) -> bytes | None: @@ -86,6 +90,7 @@ class AirPlaySink(PlaybackSink): self._track = None self._metadata: dict = {} + self._first_logged = False self._request: PcmRequest | None = None self._generation = 0 self._playing = False @@ -209,6 +214,9 @@ class AirPlaySink(PlaybackSink): request = PcmRequest(self._track.location, start_ms, self._duration_ms) self._request = request + self._first_logged = False + log.info("stream %d: %s from %d ms", self._generation, + self._track.name or self._track.location, start_ms) self._set_position(start_ms) self._set_playing(True) self._poll.start() @@ -233,9 +241,16 @@ class AirPlaySink(PlaybackSink): def _on_poll(self): request = self._request if request is not None: + if request.first_read_at is not None and not self._first_logged: + self._first_logged = True + log.info("stream %d: first audio sent %.1f s after play " + "(%d Hz)", self._generation, + request.first_read_at - request.created_at, + request.sample_rate) self._set_position(self._heard(request)) def _on_stream_ended(self, generation: int, outcome: str, detail: str): + log.info("stream %d ended: %s %s", generation, outcome, detail) if generation != self._generation or self._shutdown_done: return self._request = None diff --git a/lintunes/airplay/stream.py b/lintunes/airplay/stream.py index 99c5132..ad5d554 100644 --- a/lintunes/airplay/stream.py +++ b/lintunes/airplay/stream.py @@ -31,6 +31,7 @@ class PcmRequest: self.duration_ms = max(0, int(duration_ms)) self.sample_rate = 44100 self.frames_read = 0 + self.created_at = time.monotonic() 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 diff --git a/lintunes/airplay/support.py b/lintunes/airplay/support.py index 99f3ea1..f776f0b 100644 --- a/lintunes/airplay/support.py +++ b/lintunes/airplay/support.py @@ -110,6 +110,14 @@ def heard_ms(start_ms: int, frames_read: int, elapsed_ms: float | None, return start_ms + int(played) +def protocol_choice(value) -> str: + """config.json's `airplay_protocol`: "auto" (pyatv picks from the + device's features), "1" or "2". Machine-local on purpose — it's about + which speakers this machine meets — and anything unrecognized is auto.""" + value = str(value or "auto").strip().lower().lstrip("v") + return value if value in ("1", "2") else "auto" + + _SAMPLE_FORMATS = {2: "s16be", 4: "s32be"} diff --git a/tests/test_round87.py b/tests/test_round87.py index ecb4cfe..12cb8d5 100644 --- a/tests/test_round87.py +++ b/tests/test_round87.py @@ -489,3 +489,11 @@ class TestIndicator: assert vis.toolTip() == "Playing on tiat.place — click to stop" vis.mousePressEvent(None) airplay.disconnect.assert_called_once() + + +def test_protocol_choice_defaults_to_auto(): + assert support.protocol_choice(None) == "auto" + assert support.protocol_choice("1") == "1" + assert support.protocol_choice("v1") == "1" + assert support.protocol_choice(2) == "2" + assert support.protocol_choice("three") == "auto"