v0.59.0: LinTunes' own AirPlay 1 sender for third-party speakers
pyatv's handshakes play nothing on a WiiM; Music Assistant's cliairplay plays on it through libraop's RAOP client. airplay/raop.py reproduces that client's behaviour on the wire: one RTSP session with auth-setup, an AppleLossless ANNOUNCE and RECORD carrying RTP-Info, OPTIONS keepalives, a 2 s buffer, NTP-locked timestamps, a start that sends the first two seconds at once, the timing responder and resends. Non-Apple speakers stream through it; pyatv only finds them. Verified against shairport-sync (decoded tone, flush on pause/seek, volume, metadata). Round 88's pyatv compat hooks are gone.
This commit is contained in:
@@ -855,24 +855,34 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal
|
||||
session (picked for any receiver advertising AirPlay 2 features) gets 200
|
||||
OK for everything, timing included, and stays silent; its AirPlay 1 runs a
|
||||
transient pairing and then sends the ANNOUNCE unencrypted, and the WiiM
|
||||
hangs up. Third-party speakers take what cliraop/owntones send, so
|
||||
`stream.install_shim` also hooks pyatv for hosts marked by `set_compat`:
|
||||
auth-setup whatever the model, no pairing, and ALAC — `AppleLossless` in
|
||||
the SDP, each packet an uncompressed ALAC frame (`support.alac_verbatim`,
|
||||
byte-identical to ffmpeg's encoder on incompressible input). The handshake
|
||||
was verified silently (all-zero audio) against tiat.place. `use_compat`:
|
||||
config.json's `airplay_protocol` "auto" = compat for everything but
|
||||
Apple's own hardware (a HomePod is AirPlay 2 only), "1" = compat
|
||||
everywhere, "2" = pyatv's AirPlay 2. `airplay_debug` puts 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.
|
||||
hangs up. Music Assistant's `cliairplay` plays on it through libraop's
|
||||
RAOP client (its "RAOP-compat" route), so since Round 90 **`raop.py` is
|
||||
LinTunes' own RAOP sender** reproducing that client's behaviour, and
|
||||
pyatv only discovers those speakers: one RTSP session per connection
|
||||
(auth-setup when `et` lists 4, ANNOUNCE AppleLossless, SETUP, RECORD with
|
||||
`RTP-Info`, no FLUSH before audio; OPTIONS keepalive every 25 s; FLUSH +
|
||||
restart for pause/seek/next), the iTunes User-Agent, a **2 s** buffer
|
||||
(pyatv's is 1.5 s; cliairplay's notes say LinkPlay starves on shallow
|
||||
ones), RTP timestamps on the NTP clock, a start 200 ms out so the first
|
||||
~2 s go out at once, the timing responder, resends from a 512-packet
|
||||
backlog. `NativeDevice` is the slice of a pyatv device `AirPlaySink`
|
||||
drives, so the sink is shared. Audio is **uncompressed** ALAC
|
||||
(`support.alac_verbatim`) where libraop compresses: ffmpeg's encoder only
|
||||
writes 4096-sample frames (RAOP needs 352), and Rice coding in Python
|
||||
would cost the GUI's interpreter a share of a core. Not yet confirmed on
|
||||
the WiiM. `use_compat`: config.json's `airplay_protocol` "auto" = the
|
||||
native sender for everything but Apple's own hardware (a HomePod is
|
||||
AirPlay 2 only), "1" = native everywhere, "2" = pyatv's AirPlay 2;
|
||||
`airplay_latency_ms` overrides the 2 s. `airplay_debug` puts pyatv's and
|
||||
`lintunes.airplay.raop`'s loggers at DEBUG (every RTSP request); every
|
||||
stream's start, first audio and outcome are logged at INFO under
|
||||
`lintunes.airplay`, and the chosen sender in the control log.
|
||||
**Testing without a speaker** (Round 89): shairport-sync in Docker
|
||||
(`mikebrady/shairport-sync:5.5.2-classic`, its own dbus + avahi inside,
|
||||
on the default bridge so nothing is advertised on the LAN, `-o stdout`
|
||||
to a file) is a real third-party receiver whose decoded PCM can be
|
||||
compared with what was sent — silent and bit-checkable. LinTunes' stream
|
||||
decodes there; the WiiM still plays nothing from pyatv, though
|
||||
Music Assistant's `cliairplay` binary plays on it. pyatv also died on
|
||||
compared with what was sent — silent and bit-checkable (its output file
|
||||
also holds the silence it writes while idle). pyatv also died on
|
||||
shairport's `/info` (not a plist), so the shim treats an unreadable
|
||||
`/info` as none.
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
"""LinTunes — iTunes-style music library manager and player for Linux."""
|
||||
|
||||
__version__ = "0.58.1"
|
||||
__version__ = "0.59.0"
|
||||
|
||||
@@ -112,23 +112,35 @@ class AirPlayController(QObject):
|
||||
# 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)
|
||||
logging.getLogger("lintunes.airplay.raop").setLevel(logging.DEBUG)
|
||||
storage = MemoryStorage()
|
||||
settings = await storage.get_settings(conf)
|
||||
raop = conf.get_service(Protocol.RAOP)
|
||||
model = raop.properties.get("am", "") if raop is not None else ""
|
||||
compat = use_compat(choice, model)
|
||||
from lintunes.airplay.stream import install_shim, set_compat
|
||||
from lintunes.airplay.stream import install_shim
|
||||
install_shim()
|
||||
set_compat(str(conf.address), compat)
|
||||
if compat and raop is not None:
|
||||
# LinTunes' own RAOP sender (raop.py): what third-party speakers
|
||||
# play. pyatv only found the device.
|
||||
from lintunes.airplay.raop import (
|
||||
DEFAULT_LATENCY_MS, NativeDevice, RaopClient)
|
||||
latency = config.get("airplay_latency_ms") or DEFAULT_LATENCY_MS
|
||||
log_control("airplay", "protocol",
|
||||
f"{device.name} ({model}): native RAOP, "
|
||||
f"{latency} ms buffer (airplay_protocol={choice})")
|
||||
native = NativeDevice(RaopClient(
|
||||
str(conf.address), raop.port, dict(raop.properties),
|
||||
latency_ms=int(latency)))
|
||||
await asyncio.wait_for(native.connect(), CONNECT_TIMEOUT_S)
|
||||
return native
|
||||
settings.protocols.raop.protocol_version = (
|
||||
AirPlayVersion.V1 if compat
|
||||
else {"2": AirPlayVersion.V2}.get(choice, AirPlayVersion.Auto))
|
||||
{"2": AirPlayVersion.V2}.get(choice, AirPlayVersion.Auto))
|
||||
if raop is not None:
|
||||
used = get_protocol_version(
|
||||
raop, settings.protocols.raop.protocol_version)
|
||||
log_control("airplay", "protocol",
|
||||
f"{device.name} ({model}): {used.name}"
|
||||
f"{' compat' if compat else ''} "
|
||||
f"{device.name} ({model}): pyatv {used.name} "
|
||||
f"(airplay_protocol={choice})")
|
||||
return await asyncio.wait_for(
|
||||
pyatv.connect(raop_only(conf), loop, storage=storage),
|
||||
|
||||
@@ -0,0 +1,636 @@
|
||||
"""LinTunes' own AirPlay 1 (RAOP) sender, for speakers pyatv can't play on.
|
||||
|
||||
pyatv's handshakes reach a WiiM (Linkplay) speaker and play nothing (Round
|
||||
88); Music Assistant's cliairplay plays on it through libraop's RAOP client
|
||||
(its "RAOP-compat" route: AirPlay 1 plus auth-setup, NTP timing). This is that
|
||||
client's *behaviour* on the wire, written from the protocol rather than
|
||||
translated, request for request:
|
||||
|
||||
- one RTSP session for the whole connection — ANNOUNCE (ALAC), SETUP,
|
||||
RECORD carrying `RTP-Info`, no FLUSH before the first packet — kept alive
|
||||
with OPTIONS every 25 s; songs, pauses and seeks are FLUSH + restart;
|
||||
- auth-setup whenever the speaker's `et` lists 4, whatever its model;
|
||||
- the iTunes User-Agent plus Client-Instance / DACP-ID / Active-Remote;
|
||||
- a **2 s** buffer (`latency_frames` 88200, plus the 11025 every receiver
|
||||
adds) against pyatv's 1.5 s — LinkPlay speakers go silent with a shallow
|
||||
one — and RTP timestamps on the NTP clock itself, so the sync packet says
|
||||
"frame `ts - latency` plays at NTP(`ts`)";
|
||||
- a start `START_LEAD_MS` out: the first packets are already late when they
|
||||
leave, so the first ~2 s go out at once and the speaker begins with a full
|
||||
buffer;
|
||||
- the NTP timing responder, resends from a 512-packet backlog, and a sync
|
||||
packet every second.
|
||||
|
||||
One deliberate difference: audio is *uncompressed* ALAC (`alac_verbatim`).
|
||||
libraop compresses with Apple's encoder; ffmpeg's encoder only writes
|
||||
4096-sample frames (RAOP needs 352), and adaptive Rice coding in Python
|
||||
would cost a share of a core on the GUI's interpreter. cliairplay's own
|
||||
`--raw` is the same uncompressed form.
|
||||
|
||||
Pure asyncio, run on the AirPlay `AsyncLoop`; `NativeDevice` gives
|
||||
`AirPlaySink` the slice of a pyatv device it uses.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
import socket
|
||||
import struct
|
||||
import time
|
||||
|
||||
from lintunes.airplay.support import alac_verbatim
|
||||
|
||||
log = logging.getLogger("lintunes.airplay.raop")
|
||||
|
||||
FRAMES_PER_PACKET = 352
|
||||
SAMPLE_RATE = 44100
|
||||
RAOP_LATENCY_MIN = 11025 # what every receiver adds to the sync's
|
||||
DEFAULT_LATENCY_MS = 2000
|
||||
START_LEAD_MS = 200 # libraop's minimum RAOP start lead
|
||||
USER_AGENT = "iTunes/7.6.2 (Windows; N;)"
|
||||
KEEPALIVE_S = 25.0
|
||||
SYNC_INTERVAL_S = 1.0
|
||||
BACKLOG = 512
|
||||
RTSP_TIMEOUT_S = 10.0
|
||||
|
||||
VOLUME_MIN_DB = -30.0
|
||||
VOLUME_MUTE_DB = -144.0
|
||||
|
||||
|
||||
class RaopError(ConnectionError):
|
||||
"""The speaker refused or dropped the session."""
|
||||
|
||||
|
||||
# ---- clocks ----
|
||||
# NTP here is a 32.32 fixed-point second count (on the Unix epoch, as
|
||||
# libraop's gettimeofday-based clock is: only differences reach the speaker).
|
||||
# A timestamp ("ts") is that clock counted in frames.
|
||||
|
||||
def ntp_now() -> int:
|
||||
ns = time.time_ns()
|
||||
seconds, rest = divmod(ns, 1_000_000_000)
|
||||
return (seconds << 32) | ((rest << 32) // 1_000_000_000)
|
||||
|
||||
|
||||
def ntp2ts(ntp: int, rate: int = SAMPLE_RATE) -> int:
|
||||
return ((ntp >> 16) * rate) >> 16
|
||||
|
||||
|
||||
def ts2ntp(ts: int, rate: int = SAMPLE_RATE) -> int:
|
||||
return ((ts << 16) // rate) << 16
|
||||
|
||||
|
||||
def ms2ts(ms: float, rate: int = SAMPLE_RATE) -> int:
|
||||
return int(ms * rate // 1000)
|
||||
|
||||
|
||||
def percent_to_db(percent: float) -> float:
|
||||
"""pyatv's / libraop's volume: 0–100 linear onto −30..0 dB, 0 muted."""
|
||||
percent = max(0.0, min(100.0, float(percent)))
|
||||
if percent <= 0:
|
||||
return VOLUME_MUTE_DB
|
||||
return VOLUME_MIN_DB + (-VOLUME_MIN_DB) * percent / 100.0
|
||||
|
||||
|
||||
# ---- packets ----
|
||||
|
||||
def audio_packet(first: bool, seq: int, ts: int, ssrc: int,
|
||||
payload: bytes) -> bytes:
|
||||
return struct.pack(">BBHII", 0x80, 0xE0 if first else 0x60,
|
||||
seq & 0xFFFF, ts & 0xFFFFFFFF, ssrc) + payload
|
||||
|
||||
|
||||
def sync_packet(first: bool, head_ts: int, latency_frames: int) -> bytes:
|
||||
""""Frame `head - latency` plays at NTP(head)"; seq is always 7."""
|
||||
ntp = ts2ntp(head_ts)
|
||||
return struct.pack(">BBHIIII", 0x90 if first else 0x80, 0xD4, 7,
|
||||
(head_ts - latency_frames) & 0xFFFFFFFF,
|
||||
(ntp >> 32) & 0xFFFFFFFF, ntp & 0xFFFFFFFF,
|
||||
head_ts & 0xFFFFFFFF)
|
||||
|
||||
|
||||
def timing_reply(request: bytes, now_ntp: int) -> bytes | None:
|
||||
"""Answer a timing request (type 0x52): its send time becomes our
|
||||
reference, and we received and sent it 'now'."""
|
||||
if len(request) < 32:
|
||||
return None
|
||||
send_time = request[24:32]
|
||||
now = struct.pack(">II", (now_ntp >> 32) & 0xFFFFFFFF,
|
||||
now_ntp & 0xFFFFFFFF)
|
||||
return (bytes([request[0], 0x53 | 0x80]) + request[2:4] + b"\0\0\0\0"
|
||||
+ send_time + now + now)
|
||||
|
||||
|
||||
def parse_resend(data: bytes) -> tuple[int, int] | None:
|
||||
"""A resend request (type 0x55): (first lost seq, count)."""
|
||||
if len(data) != 8 or data[1] & 0x7F != 0x55:
|
||||
return None
|
||||
seq, count = struct.unpack(">HH", data[4:8])
|
||||
return seq, count
|
||||
|
||||
|
||||
def resend_packet(packet: bytes) -> bytes:
|
||||
return b"\x80\xd6\x00\x01" + packet
|
||||
|
||||
|
||||
def daap_item(title: str, artist: str, album: str) -> bytes:
|
||||
"""Now-playing text as an `mlit` listing, libraop's layout."""
|
||||
def tag(code: bytes, data: bytes) -> bytes:
|
||||
return code + struct.pack(">I", len(data)) + data
|
||||
body = tag(b"mikd", b"\x02")
|
||||
for code, value in ((b"minm", title), (b"asar", artist),
|
||||
(b"asal", album)):
|
||||
if value:
|
||||
body += tag(code, value.encode("utf-8"))
|
||||
return tag(b"mlit", body)
|
||||
|
||||
|
||||
def announce_sdp(sid: str, local_ip: str, peer_ip: str,
|
||||
rate: int = SAMPLE_RATE) -> str:
|
||||
return ("v=0\r\n"
|
||||
f"o=iTunes {sid} 0 IN IP4 {local_ip}\r\n"
|
||||
"s=iTunes\r\n"
|
||||
f"c=IN IP4 {peer_ip}\r\n"
|
||||
"t=0 0\r\n"
|
||||
"m=audio 0 RTP/AVP 96\r\n"
|
||||
"a=rtpmap:96 AppleLossless\r\n"
|
||||
f"a=fmtp:96 {FRAMES_PER_PACKET} 0 16 40 10 14 2 255 0 0 {rate}\r\n")
|
||||
|
||||
|
||||
def parse_transport(value: str) -> dict[str, int]:
|
||||
ports = {}
|
||||
for token in (value or "").split(";"):
|
||||
key, _, number = token.partition("=")
|
||||
if key.strip() in ("server_port", "control_port", "timing_port"):
|
||||
with contextlib.suppress(ValueError):
|
||||
ports[key.strip()] = int(number)
|
||||
return ports
|
||||
|
||||
|
||||
def metadata_caps(md: str) -> set[str]:
|
||||
caps = set()
|
||||
for digit, name in (("0", "text"), ("1", "artwork"), ("2", "progress")):
|
||||
if digit in (md or ""):
|
||||
caps.add(name)
|
||||
return caps
|
||||
|
||||
|
||||
def auth_setup_body() -> bytes:
|
||||
"""0x01 (unencrypted) and a fresh X25519 public key, as libraop sends."""
|
||||
from cryptography.hazmat.primitives.asymmetric.x25519 import (
|
||||
X25519PrivateKey)
|
||||
from cryptography.hazmat.primitives.serialization import (
|
||||
Encoding, PublicFormat)
|
||||
key = X25519PrivateKey.generate().public_key()
|
||||
return b"\x01" + key.public_bytes(Encoding.Raw, PublicFormat.Raw)
|
||||
|
||||
|
||||
# ---- RTSP ----
|
||||
|
||||
class _Rtsp:
|
||||
"""Requests in libraop's header order, one at a time."""
|
||||
|
||||
def __init__(self, user_agent: str, extra: list[tuple[str, str]]):
|
||||
self.user_agent = user_agent
|
||||
self.extra = extra
|
||||
self.url = ""
|
||||
self.session: str | None = None
|
||||
self.local_ip = ""
|
||||
self._cseq = 0
|
||||
self._reader = None
|
||||
self._writer = None
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def open(self, host: str, port: int, sid: str):
|
||||
self._reader, self._writer = await asyncio.wait_for(
|
||||
asyncio.open_connection(host, port), RTSP_TIMEOUT_S)
|
||||
self.local_ip = self._writer.get_extra_info("sockname")[0]
|
||||
self.url = f"rtsp://{host}/{sid}"
|
||||
|
||||
def closed(self) -> bool:
|
||||
return (self._writer is None or self._writer.is_closing()
|
||||
or self._reader.at_eof())
|
||||
|
||||
async def request(self, method: str, headers=(), body: bytes = b"",
|
||||
content_type: str | None = None, url: str | None = None,
|
||||
must_succeed: bool = True):
|
||||
async with self._lock:
|
||||
if self.closed():
|
||||
raise RaopError("the speaker closed the connection")
|
||||
self._cseq += 1
|
||||
lines = [f"{method} {url or self.url} RTSP/1.0"]
|
||||
lines += [f"{key}: {value}" for key, value in headers]
|
||||
if content_type and body:
|
||||
lines += [f"Content-Type: {content_type}",
|
||||
f"Content-Length: {len(body)}"]
|
||||
lines += [f"CSeq: {self._cseq}", f"User-Agent: {self.user_agent}"]
|
||||
lines += [f"{key}: {value}" for key, value in self.extra]
|
||||
if self.session is not None:
|
||||
lines.append(f"Session: {self.session}")
|
||||
message = ("\r\n".join(lines) + "\r\n\r\n").encode() + body
|
||||
log.debug("> %s", message[:600])
|
||||
self._writer.write(message)
|
||||
try:
|
||||
await self._writer.drain()
|
||||
code, reply_headers, reply_body = await asyncio.wait_for(
|
||||
self._response(), RTSP_TIMEOUT_S)
|
||||
except (OSError, asyncio.IncompleteReadError,
|
||||
asyncio.TimeoutError) as e:
|
||||
raise RaopError(f"{method} got no answer ({e or type(e).__name__})")
|
||||
log.debug("< %s %s %s", method, code, reply_headers)
|
||||
if must_succeed and code != 200:
|
||||
raise RaopError(f"the speaker refused {method} ({code})")
|
||||
return code, reply_headers, reply_body
|
||||
|
||||
async def _response(self):
|
||||
status = (await self._reader.readuntil(b"\r\n")).decode(
|
||||
"latin-1").split()
|
||||
code = int(status[1]) if len(status) > 1 and status[1].isdigit() else 0
|
||||
headers = {}
|
||||
while True:
|
||||
line = (await self._reader.readuntil(b"\r\n")).decode("latin-1")
|
||||
if line in ("\r\n", "\n"):
|
||||
break
|
||||
key, _, value = line.partition(":")
|
||||
headers[key.strip().lower()] = value.strip()
|
||||
length = int(headers.get("content-length", "0") or 0)
|
||||
body = await self._reader.readexactly(length) if length else b""
|
||||
return code, headers, body
|
||||
|
||||
def close(self):
|
||||
if self._writer is not None:
|
||||
self._writer.close()
|
||||
self._writer = None
|
||||
|
||||
|
||||
# ---- UDP ----
|
||||
|
||||
class _Udp(asyncio.DatagramProtocol):
|
||||
def __init__(self, on_datagram=None):
|
||||
self.transport = None
|
||||
self._on_datagram = on_datagram
|
||||
|
||||
def connection_made(self, transport):
|
||||
self.transport = transport
|
||||
|
||||
def datagram_received(self, data, addr):
|
||||
if self._on_datagram is not None:
|
||||
self._on_datagram(data, addr)
|
||||
|
||||
def error_received(self, exc):
|
||||
log.debug("udp error: %s", exc)
|
||||
|
||||
@property
|
||||
def port(self) -> int:
|
||||
return self.transport.get_extra_info("sockname")[1]
|
||||
|
||||
def close(self):
|
||||
if self.transport is not None:
|
||||
self.transport.close()
|
||||
self.transport = None
|
||||
|
||||
|
||||
async def _udp(local_ip: str, on_datagram=None) -> _Udp:
|
||||
loop = asyncio.get_running_loop()
|
||||
_, protocol = await loop.create_datagram_endpoint(
|
||||
lambda: _Udp(on_datagram), local_addr=(local_ip, 0),
|
||||
family=socket.AF_INET)
|
||||
return protocol
|
||||
|
||||
|
||||
# ---- the session ----
|
||||
|
||||
class RaopClient:
|
||||
"""One speaker: connect once, then play stretch after stretch.
|
||||
|
||||
State mirrors libraop: DOWN → FLUSHED (connected, nothing playing) →
|
||||
STREAMING; `flush()` goes back to FLUSHED.
|
||||
"""
|
||||
|
||||
def __init__(self, host: str, port: int, properties: dict,
|
||||
latency_ms: int = DEFAULT_LATENCY_MS):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.properties = dict(properties or {})
|
||||
self.latency_frames = max(ms2ts(latency_ms), RAOP_LATENCY_MIN)
|
||||
self.caps = metadata_caps(self.properties.get("md", ""))
|
||||
self.state = "down"
|
||||
self.volume_db: float | None = None
|
||||
self.head_ts = 0
|
||||
self.seq = secrets.randbelow(1 << 16)
|
||||
self.ssrc = int.from_bytes(os.urandom(4), "big")
|
||||
self._first = False
|
||||
self._backlog: dict[int, bytes] = {}
|
||||
self._rtsp: _Rtsp | None = None
|
||||
self._timing: _Udp | None = None
|
||||
self._control: _Udp | None = None
|
||||
self._audio: _Udp | None = None
|
||||
self._ports: dict[str, int] = {}
|
||||
self._tasks: list[asyncio.Task] = []
|
||||
self._last_keepalive = 0.0
|
||||
|
||||
# -- connecting --
|
||||
|
||||
@property
|
||||
def latency(self) -> int:
|
||||
"""Frames from a packet's timestamp to its being heard."""
|
||||
return self.latency_frames + RAOP_LATENCY_MIN
|
||||
|
||||
def connected(self) -> bool:
|
||||
return self.state != "down" and self._rtsp is not None \
|
||||
and not self._rtsp.closed()
|
||||
|
||||
async def connect(self):
|
||||
await self._teardown_quietly()
|
||||
sid = f"{secrets.randbelow(10**10):010d}"
|
||||
extra = [("Client-Instance", secrets.token_hex(8)),
|
||||
("DACP-ID", secrets.token_hex(8).upper()),
|
||||
("Active-Remote", str(secrets.randbelow(1 << 32)))]
|
||||
model = self.properties.get("am", "")
|
||||
agent = ("AirPlay/999.0.0" if "AudioAccessory" in model
|
||||
or "AppleTV" in model else USER_AGENT)
|
||||
rtsp = _Rtsp(agent, extra)
|
||||
self._rtsp = rtsp
|
||||
try:
|
||||
await rtsp.open(self.host, self.port, sid)
|
||||
except (OSError, asyncio.TimeoutError) as e:
|
||||
raise RaopError(f"couldn't reach the speaker ({e})")
|
||||
try:
|
||||
if "4" in self.properties.get("et", ""):
|
||||
# libraop carries on whatever the answer.
|
||||
await rtsp.request("POST", body=auth_setup_body(),
|
||||
content_type="application/octet-stream",
|
||||
url="/auth-setup", must_succeed=False)
|
||||
local = rtsp.local_ip
|
||||
# Timing before ANNOUNCE: some receivers query it straight away.
|
||||
self._timing = await _udp(local, self._on_timing)
|
||||
await rtsp.request(
|
||||
"ANNOUNCE", content_type="application/sdp",
|
||||
body=announce_sdp(sid, local, self.host).encode())
|
||||
self._control = await _udp(local, self._on_control)
|
||||
self._audio = await _udp(local)
|
||||
transport = ("RTP/AVP/UDP;unicast;interleaved=0-1;mode=record;"
|
||||
f"control_port={self._control.port};"
|
||||
f"timing_port={self._timing.port}")
|
||||
_, headers, _ = await rtsp.request(
|
||||
"SETUP", headers=[("Transport", transport)])
|
||||
rtsp.session = (headers.get("session") or "").strip() or None
|
||||
self._ports = parse_transport(headers.get("transport", ""))
|
||||
if (rtsp.session is None or not self._ports.get("server_port")
|
||||
or not self._ports.get("control_port")):
|
||||
raise RaopError("the speaker's SETUP answer had no ports")
|
||||
_, headers, _ = await rtsp.request("RECORD", headers=[
|
||||
("Range", "npt=0-"),
|
||||
("RTP-Info", f"seq={(self.seq + 1) & 0xFFFF};"
|
||||
f"rtptime={ntp2ts(ntp_now()) & 0xFFFFFFFF}")])
|
||||
with contextlib.suppress(ValueError):
|
||||
self.latency_frames = max(
|
||||
int(headers.get("audio-latency", "0")), self.latency_frames)
|
||||
self.state = "flushed"
|
||||
self._last_keepalive = time.monotonic()
|
||||
self._tasks = [asyncio.ensure_future(self._control_loop())]
|
||||
if self.volume_db is not None and self.volume_db > VOLUME_MUTE_DB:
|
||||
await self._send_volume()
|
||||
log.info("connected to %s:%d (latency %d frames)", self.host,
|
||||
self.port, self.latency)
|
||||
except BaseException:
|
||||
await self._teardown_quietly()
|
||||
raise
|
||||
|
||||
async def ensure_connected(self):
|
||||
if not self.connected():
|
||||
await self.connect()
|
||||
|
||||
# -- playing --
|
||||
|
||||
def start(self, start_ntp: int | None = None):
|
||||
"""Begin a stretch: its first frame is heard at `start_ntp` (default
|
||||
START_LEAD_MS from now). Mirrors raopcl_start_at + accept_frames."""
|
||||
if start_ntp is None:
|
||||
start_ntp = ntp_now() + ts2ntp(ms2ts(START_LEAD_MS))
|
||||
self.head_ts = ntp2ts(start_ntp) - self.latency
|
||||
self._first = True
|
||||
self.state = "streaming"
|
||||
self._send_sync(first=True)
|
||||
|
||||
def ready_for(self) -> float:
|
||||
"""Seconds until the next packet is due (≤ 0: send it now)."""
|
||||
due = self.head_ts + FRAMES_PER_PACKET
|
||||
return (due - ntp2ts(ntp_now())) / SAMPLE_RATE
|
||||
|
||||
def send(self, pcm: bytes):
|
||||
"""One packet of 352 frames of s16be stereo PCM."""
|
||||
if len(pcm) < FRAMES_PER_PACKET * 4:
|
||||
pcm = pcm + bytes(FRAMES_PER_PACKET * 4 - len(pcm))
|
||||
self.seq = (self.seq + 1) & 0xFFFF
|
||||
packet = audio_packet(self._first, self.seq, self.head_ts, self.ssrc,
|
||||
alac_verbatim(pcm))
|
||||
self._first = False
|
||||
self._backlog[self.seq % BACKLOG] = (self.seq, packet)
|
||||
self.head_ts += FRAMES_PER_PACKET
|
||||
if self._audio is not None and self._audio.transport is not None:
|
||||
self._audio.transport.sendto(
|
||||
packet, (self.host, self._ports["server_port"]))
|
||||
|
||||
def heard_at(self, ts: int) -> float:
|
||||
"""time.time() at which frame `ts` comes out of the speaker."""
|
||||
return (ts + self.latency) / SAMPLE_RATE
|
||||
|
||||
async def flush(self):
|
||||
"""Stop what the speaker has queued (pause, seek, next song)."""
|
||||
if self.state != "streaming":
|
||||
return
|
||||
self.state = "flushed"
|
||||
await self._rtsp.request("FLUSH", headers=[(
|
||||
"RTP-Info", f"seq={(self.seq + 1) & 0xFFFF};"
|
||||
f"rtptime={(self.head_ts + 1) & 0xFFFFFFFF}")],
|
||||
must_succeed=False)
|
||||
|
||||
async def set_volume(self, db: float):
|
||||
self.volume_db = db
|
||||
if self.connected():
|
||||
await self._send_volume()
|
||||
|
||||
async def _send_volume(self):
|
||||
await self._rtsp.request(
|
||||
"SET_PARAMETER", content_type="text/parameters",
|
||||
body=f"volume: {self.volume_db:f}\r\n".encode())
|
||||
|
||||
async def set_metadata(self, title="", artist="", album="",
|
||||
artwork: bytes | None = None,
|
||||
elapsed_ms: int = 0, duration_ms: int = 0):
|
||||
"""Now playing, as far as the speaker said it takes each part
|
||||
(`md`). Best effort: a refusal is never fatal (libraop's rule)."""
|
||||
rtptime = (self.head_ts + self.latency_frames) & 0xFFFFFFFF
|
||||
info = [("RTP-Info", f"rtptime={rtptime}")]
|
||||
if "text" in self.caps:
|
||||
await self._rtsp.request(
|
||||
"SET_PARAMETER", headers=info,
|
||||
content_type="application/x-dmap-tagged",
|
||||
body=daap_item(title, artist, album), must_succeed=False)
|
||||
if "artwork" in self.caps and artwork:
|
||||
await self._rtsp.request(
|
||||
"SET_PARAMETER", headers=info, content_type="image/jpeg",
|
||||
body=artwork, must_succeed=False)
|
||||
if "progress" in self.caps and self.state == "streaming":
|
||||
now = ntp2ts(ntp_now())
|
||||
started = now - ms2ts(elapsed_ms)
|
||||
end = started + ms2ts(duration_ms) if duration_ms else now
|
||||
await self._rtsp.request(
|
||||
"SET_PARAMETER", content_type="text/parameters",
|
||||
body=(f"progress: {started & 0xFFFFFFFF}/{now & 0xFFFFFFFF}/"
|
||||
f"{end & 0xFFFFFFFF}\r\n").encode(),
|
||||
must_succeed=False)
|
||||
|
||||
# -- the background --
|
||||
|
||||
def _send_sync(self, first: bool = False):
|
||||
if self.state != "streaming" or self._control is None \
|
||||
or self._control.transport is None:
|
||||
return
|
||||
self._control.transport.sendto(
|
||||
sync_packet(first, self.head_ts, self.latency_frames),
|
||||
(self.host, self._ports["control_port"]))
|
||||
|
||||
def _on_timing(self, data: bytes, addr):
|
||||
reply = timing_reply(data, ntp_now())
|
||||
if reply is not None and self._timing is not None \
|
||||
and self._timing.transport is not None:
|
||||
self._timing.transport.sendto(reply, addr)
|
||||
|
||||
def _on_control(self, data: bytes, addr):
|
||||
lost = parse_resend(data)
|
||||
if lost is None or self._control is None:
|
||||
return
|
||||
first, count = lost
|
||||
for i in range(count):
|
||||
seq = (first + i) & 0xFFFF
|
||||
entry = self._backlog.get(seq % BACKLOG)
|
||||
if entry is not None and entry[0] == seq \
|
||||
and self._control.transport is not None:
|
||||
self._control.transport.sendto(
|
||||
resend_packet(entry[1]),
|
||||
(self.host, self._ports["control_port"]))
|
||||
|
||||
async def _control_loop(self):
|
||||
while self.state != "down":
|
||||
await asyncio.sleep(SYNC_INTERVAL_S)
|
||||
if self.state == "down":
|
||||
break
|
||||
self._send_sync()
|
||||
if time.monotonic() - self._last_keepalive >= KEEPALIVE_S:
|
||||
self._last_keepalive = time.monotonic()
|
||||
try:
|
||||
await self._rtsp.request("OPTIONS", url="*",
|
||||
must_succeed=False)
|
||||
except RaopError as e:
|
||||
log.info("keepalive failed: %s", e)
|
||||
self.state = "down"
|
||||
|
||||
# -- closing --
|
||||
|
||||
async def close(self):
|
||||
"""FLUSH and TEARDOWN, as libraop's disconnect does."""
|
||||
if self.connected():
|
||||
with contextlib.suppress(RaopError):
|
||||
await self._rtsp.request("FLUSH", headers=[(
|
||||
"RTP-Info", f"seq={(self.seq + 1) & 0xFFFF};"
|
||||
f"rtptime={(self.head_ts + 1) & 0xFFFFFFFF}")],
|
||||
must_succeed=False)
|
||||
with contextlib.suppress(RaopError):
|
||||
await self._rtsp.request("TEARDOWN", must_succeed=False)
|
||||
await self._teardown_quietly()
|
||||
|
||||
async def _teardown_quietly(self):
|
||||
self.state = "down"
|
||||
tasks, self._tasks = self._tasks, []
|
||||
for task in tasks:
|
||||
task.cancel()
|
||||
for task in tasks:
|
||||
with contextlib.suppress(BaseException):
|
||||
await task
|
||||
for udp in (self._timing, self._control, self._audio):
|
||||
if udp is not None:
|
||||
udp.close()
|
||||
self._timing = self._control = self._audio = None
|
||||
if self._rtsp is not None:
|
||||
self._rtsp.close()
|
||||
self._rtsp = None
|
||||
self._backlog.clear()
|
||||
|
||||
|
||||
# ---- what AirPlaySink drives ----
|
||||
|
||||
class NativeDevice:
|
||||
"""The pieces of a pyatv device `AirPlaySink` uses — `stream.stream_file`,
|
||||
`audio.set_volume`, `close()` — over a RaopClient."""
|
||||
|
||||
def __init__(self, client: RaopClient):
|
||||
self.client = client
|
||||
self.stream = self
|
||||
self.audio = self
|
||||
|
||||
async def connect(self):
|
||||
await self.client.connect()
|
||||
|
||||
async def stream_file(self, request, metadata=None):
|
||||
"""Play one stretch of a song: ffmpeg's PCM, paced packet by packet,
|
||||
then wait until the last of it has been heard. A cancel (pause,
|
||||
seek, next) flushes what the speaker has queued."""
|
||||
from lintunes.airplay.stream import _ffmpeg_source
|
||||
client = self.client
|
||||
request.latency_ms = START_LEAD_MS
|
||||
await client.ensure_connected()
|
||||
if client.state == "streaming":
|
||||
await client.flush()
|
||||
source = await _ffmpeg_source().open(request, SAMPLE_RATE, 2, 2)
|
||||
try:
|
||||
client.start()
|
||||
meta_task = asyncio.ensure_future(self._metadata(request, metadata))
|
||||
last_ts = None
|
||||
while True:
|
||||
pcm = await source.readframes(FRAMES_PER_PACKET)
|
||||
if not pcm:
|
||||
break
|
||||
wait = client.ready_for()
|
||||
if wait > 0:
|
||||
await asyncio.sleep(wait)
|
||||
if not client.connected():
|
||||
return # dropped: the request isn't exhausted
|
||||
last_ts = client.head_ts
|
||||
client.send(pcm)
|
||||
meta_task.cancel()
|
||||
if last_ts is not None:
|
||||
# Until the end has been heard, as pyatv's padding waits.
|
||||
end = client.heard_at(last_ts + FRAMES_PER_PACKET)
|
||||
while time.time() < end and client.connected():
|
||||
await asyncio.sleep(min(0.25, end - time.time()))
|
||||
except asyncio.CancelledError:
|
||||
with contextlib.suppress(Exception):
|
||||
await asyncio.wait_for(client.flush(), 2.0)
|
||||
raise
|
||||
finally:
|
||||
await source.close()
|
||||
|
||||
async def _metadata(self, request, metadata):
|
||||
if metadata is None:
|
||||
return
|
||||
try:
|
||||
await self.client.set_metadata(
|
||||
getattr(metadata, "title", "") or "",
|
||||
getattr(metadata, "artist", "") or "",
|
||||
getattr(metadata, "album", "") or "",
|
||||
getattr(metadata, "artwork", None),
|
||||
elapsed_ms=request.start_ms,
|
||||
duration_ms=int((getattr(metadata, "duration", 0) or 0)
|
||||
* 1000))
|
||||
except RaopError as e:
|
||||
log.info("metadata: %s", e)
|
||||
|
||||
async def set_volume(self, percent: float):
|
||||
await self.client.set_volume(percent_to_db(percent))
|
||||
|
||||
def close(self):
|
||||
return [self.client.close()]
|
||||
@@ -236,7 +236,8 @@ class AirPlaySink(PlaybackSink):
|
||||
|
||||
def _heard(self, request: PcmRequest) -> int:
|
||||
return heard_ms(request.start_ms, request.frames_read,
|
||||
request.elapsed_ms(), request.sample_rate)
|
||||
request.elapsed_ms(), request.sample_rate,
|
||||
request.latency_ms)
|
||||
|
||||
def _on_poll(self):
|
||||
request = self._request
|
||||
|
||||
@@ -11,17 +11,14 @@ 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.
|
||||
|
||||
`install_shim` also hooks pyatv's AirPlay 1 for the *compatibility* handshake
|
||||
(`support.use_compat`), but only for hosts `set_compat` named: auth-setup
|
||||
even though the receiver isn't an AirPort, no transient pairing (a WiiM drops
|
||||
the connection when the ANNOUNCE after it arrives unencrypted), and ALAC
|
||||
instead of L16 — uncompressed frames, what cliraop sends.
|
||||
The compatibility sender (`raop.py`, Round 90) reads its PCM through the
|
||||
same FfmpegSource; it replaced Round 88's hooks into pyatv's AirPlay 1.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from lintunes.airplay.support import alac_verbatim, ffmpeg_command
|
||||
from lintunes.airplay.support import ffmpeg_command
|
||||
|
||||
|
||||
class PcmRequest:
|
||||
@@ -41,6 +38,8 @@ class PcmRequest:
|
||||
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
|
||||
# From the first frame read to its being heard; None = pyatv's.
|
||||
self.latency_ms: float | None = None
|
||||
self._process = None
|
||||
|
||||
def elapsed_ms(self) -> float | None:
|
||||
@@ -151,79 +150,9 @@ def _ffmpeg_source():
|
||||
return FfmpegSource
|
||||
|
||||
|
||||
_compat_hosts: set[str] = set()
|
||||
|
||||
|
||||
def set_compat(host: str, on: bool):
|
||||
"""Stream to `host` with the compatibility handshake, or pyatv's own."""
|
||||
if on:
|
||||
_compat_hosts.add(host)
|
||||
else:
|
||||
_compat_hosts.discard(host)
|
||||
|
||||
|
||||
def _is_compat(connection) -> bool:
|
||||
return getattr(connection, "remote_ip", None) in _compat_hosts
|
||||
|
||||
|
||||
class _NoPairing:
|
||||
"""pair_verify's stand-in for a compat host: verifies nothing."""
|
||||
|
||||
def __init__(self, credentials, connection):
|
||||
pass
|
||||
|
||||
async def verify_credentials(self) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
_COMPAT_ANNOUNCE = (
|
||||
"v=0\r\n"
|
||||
"o=iTunes {session_id} 0 IN IP4 {local_ip}\r\n"
|
||||
"s=iTunes\r\n"
|
||||
"c=IN IP4 {remote_ip}\r\n"
|
||||
"t=0 0\r\n"
|
||||
"m=audio 0 RTP/AVP 96\r\n"
|
||||
"a=rtpmap:96 AppleLossless\r\n"
|
||||
"a=fmtp:96 {frames} 0 16 40 10 14 2 255 0 0 {sample_rate}\r\n"
|
||||
)
|
||||
|
||||
|
||||
def _alac_ok(context) -> bool:
|
||||
return context.bytes_per_channel == 2 and context.channels == 2
|
||||
|
||||
|
||||
def _install_compat():
|
||||
from pyatv.protocols.raop import stream_client
|
||||
from pyatv.protocols.raop.protocols import airplayv1
|
||||
def _install_info_fix():
|
||||
from pyatv.support import rtsp
|
||||
|
||||
real_pair = airplayv1.pair_verify
|
||||
airplayv1.pair_verify = lambda credentials, connection: (
|
||||
_NoPairing if _is_compat(connection) else real_pair)(
|
||||
credentials, connection)
|
||||
|
||||
real_auth = stream_client.StreamClient._requires_auth_setup
|
||||
stream_client.StreamClient._requires_auth_setup = property(
|
||||
lambda self: _is_compat(self.rtsp.connection)
|
||||
or real_auth.fget(self))
|
||||
|
||||
real_announce = rtsp.RtspSession.announce
|
||||
|
||||
async def announce(self, bytes_per_channel, channels, sample_rate,
|
||||
password):
|
||||
if (not _is_compat(self.connection) or password is not None
|
||||
or bytes_per_channel != 2 or channels != 2):
|
||||
return await real_announce(self, bytes_per_channel, channels,
|
||||
sample_rate, password)
|
||||
body = _COMPAT_ANNOUNCE.format(
|
||||
session_id=self.session_id, local_ip=self.connection.local_ip,
|
||||
remote_ip=self.connection.remote_ip,
|
||||
frames=rtsp.FRAMES_PER_PACKET, sample_rate=sample_rate)
|
||||
return await self.exchange("ANNOUNCE", content_type="application/sdp",
|
||||
body=body)
|
||||
|
||||
rtsp.RtspSession.announce = announce
|
||||
|
||||
# GET /info is optional, and pyatv treats an unparseable answer as fatal:
|
||||
# shairport-sync's classic build answers it with a body that isn't a
|
||||
# plist, so every stream failed before it began.
|
||||
@@ -237,15 +166,6 @@ def _install_compat():
|
||||
|
||||
rtsp.RtspSession.info = info
|
||||
|
||||
real_send = airplayv1.AirPlayV1.send_audio_packet
|
||||
|
||||
async def send_audio_packet(self, transport, rtp_header, audio):
|
||||
if _is_compat(self.rtsp.connection) and _alac_ok(self.context):
|
||||
audio = alac_verbatim(audio)
|
||||
return await real_send(self, transport, rtp_header, audio)
|
||||
|
||||
airplayv1.AirPlayV1.send_audio_packet = send_audio_packet
|
||||
|
||||
|
||||
def install_shim():
|
||||
"""Route a PcmRequest through `stream_file` to an FfmpegSource.
|
||||
@@ -266,4 +186,4 @@ def install_shim():
|
||||
|
||||
open_source._lintunes = True
|
||||
raop.open_source = open_source
|
||||
_install_compat()
|
||||
_install_info_fix()
|
||||
|
||||
@@ -94,19 +94,21 @@ def level_to_percent(linear: float) -> float:
|
||||
|
||||
|
||||
def heard_ms(start_ms: int, frames_read: int, elapsed_ms: float | None,
|
||||
sample_rate: int) -> int:
|
||||
sample_rate: int, lag_ms: float | None = None) -> 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.
|
||||
sends its trailing silence. `lag_ms` replaces pyatv's latency for a
|
||||
sender whose first packet is heard sooner after it is read (raop.py).
|
||||
"""
|
||||
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)))
|
||||
lag = latency_ms(sample_rate) if lag_ms is None else lag_ms
|
||||
played = max(0.0, min(read_ms, elapsed_ms - lag))
|
||||
return start_ms + int(played)
|
||||
|
||||
|
||||
@@ -124,14 +126,15 @@ _APPLE_MODELS = ("AudioAccessory", "AppleTV", "AirPort", "Mac", "iMac")
|
||||
|
||||
|
||||
def use_compat(choice: str, model: str) -> bool:
|
||||
"""Stream with the AirPlay 1 compatibility handshake (stream.py)?
|
||||
"""Stream with LinTunes' own RAOP sender (raop.py) rather than pyatv?
|
||||
|
||||
pyatv picks AirPlay 2 for any receiver advertising its features, and a
|
||||
WiiM (Linkplay) takes that whole session — RECORD, volume, timing — and
|
||||
plays nothing; its own AirPlay 1 refuses pyatv's transient pairing. What
|
||||
third-party speakers accept is the handshake cliraop/owntones send:
|
||||
auth-setup, no pairing, ALAC. So "auto" means that for everything that
|
||||
isn't Apple's, "1" means it everywhere, and "2" is pyatv's AirPlay 2.
|
||||
third-party speakers accept is libraop's AirPlay 1 (cliairplay's
|
||||
RAOP-compat route), which raop.py reproduces. So "auto" means that for
|
||||
everything that isn't Apple's, "1" means it everywhere, and "2" is
|
||||
pyatv's AirPlay 2.
|
||||
"""
|
||||
if choice == "1":
|
||||
return True
|
||||
|
||||
+5
-64
@@ -1,10 +1,7 @@
|
||||
"""Round 88: a repeat button, the streaming tile's click, and AirPlay's
|
||||
compatibility handshake for third-party speakers (a WiiM played nothing)."""
|
||||
compatibility mode for third-party speakers (a WiiM played nothing). Round 90
|
||||
replaced its pyatv hooks with raop.py; those tests are in test_round90."""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import random
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
@@ -169,71 +166,15 @@ class TestAlacVerbatim:
|
||||
assert frame[2] >> 1 == 0b0000001
|
||||
|
||||
|
||||
class _Conn:
|
||||
def __init__(self, ip):
|
||||
self.remote_ip = ip
|
||||
self.local_ip = "10.0.0.2"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def shim():
|
||||
stream.install_shim()
|
||||
yield
|
||||
stream._compat_hosts.clear()
|
||||
|
||||
|
||||
class TestCompatHooks:
|
||||
def test_announce_says_apple_lossless_only_for_compat_hosts(self, shim):
|
||||
from pyatv.support import rtsp
|
||||
sent = []
|
||||
|
||||
async def exchange(self, method, **kw):
|
||||
sent.append(kw["body"])
|
||||
return SimpleNamespace(code=200, headers={})
|
||||
|
||||
session = rtsp.RtspSession(_Conn("10.0.0.9"))
|
||||
session.exchange = exchange.__get__(session)
|
||||
stream.set_compat("10.0.0.9", True)
|
||||
asyncio.run(session.announce(2, 2, 44100, None))
|
||||
stream.set_compat("10.0.0.9", False)
|
||||
asyncio.run(session.announce(2, 2, 44100, None))
|
||||
assert "a=rtpmap:96 AppleLossless" in sent[0]
|
||||
assert "a=fmtp:96 352 0 16 40 10 14 2 255 0 0 44100" in sent[0]
|
||||
assert "L16/44100/2" in sent[1]
|
||||
|
||||
def test_no_pairing_and_auth_setup_for_compat_hosts(self, shim):
|
||||
from pyatv.protocols.raop import stream_client
|
||||
from pyatv.protocols.raop.protocols import airplayv1
|
||||
from pyatv.protocols.raop.parsers import EncryptionType
|
||||
stream.set_compat("10.0.0.9", True)
|
||||
assert isinstance(airplayv1.pair_verify(None, _Conn("10.0.0.9")),
|
||||
stream._NoPairing)
|
||||
client = SimpleNamespace(rtsp=SimpleNamespace(
|
||||
connection=_Conn("10.0.0.9")), _properties={"am": "WiiM"},
|
||||
_encryption_types=EncryptionType.Unencrypted)
|
||||
assert stream_client.StreamClient._requires_auth_setup.fget(client)
|
||||
client.rtsp.connection = _Conn("10.0.0.10")
|
||||
assert not stream_client.StreamClient._requires_auth_setup.fget(client)
|
||||
|
||||
def test_audio_is_wrapped_as_alac_for_compat_hosts(self, shim):
|
||||
from pyatv.protocols.raop.protocols import airplayv1
|
||||
sent = []
|
||||
proto = SimpleNamespace(
|
||||
rtsp=SimpleNamespace(connection=_Conn("10.0.0.9")),
|
||||
context=SimpleNamespace(bytes_per_channel=2, channels=2, rtpseq=7))
|
||||
transport = SimpleNamespace(sendto=sent.append)
|
||||
pcm = os.urandom(1408)
|
||||
stream.set_compat("10.0.0.9", True)
|
||||
asyncio.run(airplayv1.AirPlayV1.send_audio_packet(
|
||||
proto, transport, b"H" * 12, pcm))
|
||||
stream.set_compat("10.0.0.9", False)
|
||||
asyncio.run(airplayv1.AirPlayV1.send_audio_packet(
|
||||
proto, transport, b"H" * 12, pcm))
|
||||
assert sent[0] == b"H" * 12 + alac_verbatim(pcm)
|
||||
assert sent[1] == b"H" * 12 + pcm
|
||||
|
||||
class TestShim:
|
||||
def test_install_is_idempotent(self, shim):
|
||||
from pyatv.support import rtsp
|
||||
announce = rtsp.RtspSession.announce
|
||||
info = rtsp.RtspSession.info
|
||||
stream.install_shim()
|
||||
assert rtsp.RtspSession.announce is announce
|
||||
assert rtsp.RtspSession.info is info
|
||||
|
||||
+199
-1
@@ -1,5 +1,11 @@
|
||||
"""Round 90: the track list's headings run over the scrollbar's column."""
|
||||
"""Round 90: the track list's headings run over the scrollbar's column, and
|
||||
LinTunes' own RAOP sender (airplay/raop.py) for third-party speakers."""
|
||||
|
||||
import asyncio
|
||||
import struct
|
||||
|
||||
from lintunes.airplay import raop
|
||||
from lintunes.airplay.support import alac_verbatim, heard_ms
|
||||
from lintunes.gui.track_table import TrackTableView
|
||||
from tests.test_round29 import _local_track
|
||||
|
||||
@@ -17,3 +23,195 @@ def test_headings_span_the_scrollbar_and_dont_scroll_sideways(qapp, tmp_path):
|
||||
assert bar.geometry().top() >= header.height() # starts under them
|
||||
assert view.horizontalScrollBar().maximum() == 0
|
||||
view.close()
|
||||
|
||||
|
||||
# ---- LinTunes' own RAOP sender ----
|
||||
|
||||
|
||||
class TestClocks:
|
||||
def test_ts_and_ntp_round_trip(self):
|
||||
ntp = raop.ntp_now()
|
||||
ts = raop.ntp2ts(ntp)
|
||||
assert abs(raop.ts2ntp(ts) - ntp) < (1 << 32) // 44100 * 2
|
||||
|
||||
def test_volume_matches_pyatv(self):
|
||||
assert raop.percent_to_db(0) == -144.0
|
||||
assert raop.percent_to_db(100) == 0.0
|
||||
assert raop.percent_to_db(50) == -15.0
|
||||
|
||||
def test_two_second_buffer_plus_the_receivers_own(self):
|
||||
client = raop.RaopClient("10.0.0.9", 7000, {})
|
||||
assert client.latency_frames == 88200
|
||||
assert client.latency == 88200 + 11025
|
||||
|
||||
|
||||
class TestPackets:
|
||||
def test_sync_says_frame_ts_minus_latency_plays_at_ntp_ts(self):
|
||||
packet = raop.sync_packet(True, 1_000_000, 88200)
|
||||
first, kind, seq, minus, sec, frac, ts = struct.unpack(">BBHIIII",
|
||||
packet)
|
||||
assert (first, kind, seq) == (0x90, 0xD4, 7)
|
||||
assert minus == 1_000_000 - 88200 and ts == 1_000_000
|
||||
assert (sec << 32 | frac) == raop.ts2ntp(1_000_000)
|
||||
assert raop.sync_packet(False, 5, 1)[0] == 0x80
|
||||
|
||||
def test_audio_packet_marks_only_the_first(self):
|
||||
assert raop.audio_packet(True, 1, 2, 3, b"x")[:2] == b"\x80\xe0"
|
||||
assert raop.audio_packet(False, 1, 2, 3, b"x")[:2] == b"\x80\x60"
|
||||
assert struct.unpack(">HII", raop.audio_packet(
|
||||
False, 0x1_0005, 2**32 + 9, 3, b"")[2:]) == (5, 9, 3)
|
||||
|
||||
def test_timing_reply_echoes_the_send_time(self):
|
||||
request = b"\x80\xd2\x00\x07" + bytes(4) + bytes(16) + b"ABCDEFGH"
|
||||
reply = raop.timing_reply(request, 0x0102030405060708)
|
||||
assert reply[:4] == b"\x80\xd3\x00\x07"
|
||||
assert reply[8:16] == b"ABCDEFGH"
|
||||
assert reply[16:24] == reply[24:32] == bytes(range(1, 9))
|
||||
assert raop.timing_reply(b"short", 1) is None
|
||||
|
||||
def test_resend(self):
|
||||
assert raop.parse_resend(b"\x80\xd5\x00\x01\x00\x0a\x00\x03") == (10, 3)
|
||||
assert raop.parse_resend(b"\x80\xd4\x00\x01\x00\x0a\x00\x03") is None
|
||||
assert raop.resend_packet(b"P") == b"\x80\xd6\x00\x01P"
|
||||
|
||||
def test_daap_item(self):
|
||||
item = raop.daap_item("Song", "", "Album")
|
||||
assert item[:4] == b"mlit"
|
||||
assert struct.unpack(">I", item[4:8])[0] == len(item) - 8
|
||||
assert b"minm\x00\x00\x00\x04Song" in item
|
||||
assert b"asal\x00\x00\x00\x05Album" in item and b"asar" not in item
|
||||
|
||||
def test_sdp_and_transport(self):
|
||||
sdp = raop.announce_sdp("0123456789", "10.0.0.2", "10.0.0.9")
|
||||
assert "a=rtpmap:96 AppleLossless" in sdp
|
||||
assert "a=fmtp:96 352 0 16 40 10 14 2 255 0 0 44100" in sdp
|
||||
assert raop.parse_transport(
|
||||
"RTP/AVP/UDP;unicast;mode=record;server_port=6001;"
|
||||
"control_port=6002;timing_port=6003") == {
|
||||
"server_port": 6001, "control_port": 6002, "timing_port": 6003}
|
||||
|
||||
def test_metadata_caps(self):
|
||||
assert raop.metadata_caps("0,1,2") == {"text", "artwork", "progress"}
|
||||
assert raop.metadata_caps("") == set()
|
||||
|
||||
|
||||
def test_heard_uses_the_senders_own_lag():
|
||||
assert heard_ms(0, 88200, 1000.0, 44100, lag_ms=200) == 800
|
||||
assert heard_ms(0, 88200, 1000.0, 44100) == 0 # pyatv's 1.5 s
|
||||
|
||||
|
||||
class _FakeSpeaker:
|
||||
"""An RTSP + UDP receiver on 127.0.0.1 that records what arrives."""
|
||||
|
||||
def __init__(self):
|
||||
self.requests = []
|
||||
self.audio = []
|
||||
self.control = []
|
||||
|
||||
async def start(self):
|
||||
loop = asyncio.get_running_loop()
|
||||
self.server = await asyncio.start_server(self._client, "127.0.0.1", 0)
|
||||
self.port = self.server.sockets[0].getsockname()[1]
|
||||
speaker = self
|
||||
|
||||
class Sink(asyncio.DatagramProtocol):
|
||||
def __init__(self, into):
|
||||
self.into = into
|
||||
|
||||
def datagram_received(self, data, addr):
|
||||
self.into.append(data)
|
||||
|
||||
self.audio_t, _ = await loop.create_datagram_endpoint(
|
||||
lambda: Sink(speaker.audio), local_addr=("127.0.0.1", 0))
|
||||
self.control_t, _ = await loop.create_datagram_endpoint(
|
||||
lambda: Sink(speaker.control), local_addr=("127.0.0.1", 0))
|
||||
|
||||
async def _client(self, reader, writer):
|
||||
while True:
|
||||
try:
|
||||
head = await reader.readuntil(b"\r\n\r\n")
|
||||
except asyncio.IncompleteReadError:
|
||||
return
|
||||
lines = head.decode().split("\r\n")
|
||||
headers = dict(line.split(": ", 1) for line in lines[1:] if line)
|
||||
body = await reader.readexactly(
|
||||
int(headers.get("Content-Length", 0)))
|
||||
method, url = lines[0].split()[:2]
|
||||
self.requests.append((method, url, headers, body))
|
||||
extra = ""
|
||||
if method == "SETUP":
|
||||
extra = ("Session: 1\r\nTransport: RTP/AVP/UDP;unicast;"
|
||||
f"server_port={self.audio_t.get_extra_info('sockname')[1]};"
|
||||
f"control_port={self.control_t.get_extra_info('sockname')[1]};"
|
||||
"timing_port=1\r\n")
|
||||
writer.write(f"RTSP/1.0 200 OK\r\nCSeq: {headers['CSeq']}\r\n"
|
||||
f"{extra}\r\n".encode())
|
||||
await writer.drain()
|
||||
|
||||
def methods(self):
|
||||
return [(m, u) if m == "POST" else m for m, u, _, _ in self.requests]
|
||||
|
||||
def close(self):
|
||||
self.server.close()
|
||||
self.audio_t.close()
|
||||
self.control_t.close()
|
||||
|
||||
|
||||
def _run(coro):
|
||||
return asyncio.run(asyncio.wait_for(coro, 20))
|
||||
|
||||
|
||||
def test_the_session_is_libraops():
|
||||
async def go():
|
||||
speaker = _FakeSpeaker()
|
||||
await speaker.start()
|
||||
client = raop.RaopClient("127.0.0.1", speaker.port,
|
||||
{"et": "0,4", "md": "0"})
|
||||
await client.set_volume(-15.0)
|
||||
await client.connect()
|
||||
client.start()
|
||||
pcm = bytes(range(256)) * 5 + bytes(128) # one packet
|
||||
for _ in range(3):
|
||||
client.send(pcm)
|
||||
await client.set_metadata("Song", "Artist", "Album")
|
||||
await asyncio.sleep(0.2)
|
||||
await client.flush()
|
||||
await client.close()
|
||||
speaker.close()
|
||||
return speaker, pcm
|
||||
|
||||
speaker, pcm = _run(go())
|
||||
assert speaker.methods()[:6] == [
|
||||
("POST", "/auth-setup"), "ANNOUNCE", "SETUP", "RECORD",
|
||||
"SET_PARAMETER", "SET_PARAMETER"]
|
||||
by_method = {m: (h, b) for m, _, h, b in speaker.requests}
|
||||
assert len(by_method["POST"][1]) == 33 and by_method["POST"][1][0] == 1
|
||||
assert b"AppleLossless" in by_method["ANNOUNCE"][1]
|
||||
record = by_method["RECORD"][0]
|
||||
assert record["Range"] == "npt=0-" and "rtptime=" in record["RTP-Info"]
|
||||
assert "FLUSH" not in speaker.methods()[:5] # nothing before audio
|
||||
assert all(h["User-Agent"] == raop.USER_AGENT and "DACP-ID" in h
|
||||
for _, _, h, _ in speaker.requests)
|
||||
volume = [b for m, _, h, b in speaker.requests
|
||||
if h.get("Content-Type") == "text/parameters"]
|
||||
assert volume[0] == b"volume: -15.000000\r\n"
|
||||
# The audio: three packets, the first marked, each one ALAC frame.
|
||||
assert [p[1] for p in speaker.audio] == [0xE0, 0x60, 0x60]
|
||||
assert speaker.audio[0][12:] == alac_verbatim(pcm)
|
||||
seqs = [struct.unpack(">H", p[2:4])[0] for p in speaker.audio]
|
||||
assert seqs == [(seqs[0] + i) & 0xFFFF for i in range(3)]
|
||||
stamps = [struct.unpack(">I", p[4:8])[0] for p in speaker.audio]
|
||||
assert [b - a for a, b in zip(stamps, stamps[1:])] == [352, 352]
|
||||
# …and the first sync went out before them, flagged first.
|
||||
assert speaker.control and speaker.control[0][:2] == b"\x90\xd4"
|
||||
|
||||
|
||||
def test_a_late_start_sends_the_first_two_seconds_at_once():
|
||||
client = raop.RaopClient("10.0.0.9", 7000, {})
|
||||
client.state = "flushed"
|
||||
client.start()
|
||||
# The first packet is already ~2 s late: no waiting until the buffer
|
||||
# is full, then real time.
|
||||
assert client.ready_for() < -1.9
|
||||
client.head_ts += raop.ms2ts(2100)
|
||||
assert client.ready_for() > 0
|
||||
|
||||
Reference in New Issue
Block a user