Files
lintunes/lintunes/airplay/loop.py
T
trav c1aee48b8b v0.56.0: Connect to AirPlay — stream to AirPlay speakers via pyatv
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).
2026-10-15 16:01:08 -07:00

39 lines
1.4 KiB
Python

"""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)