Casting and network devices
Spotifast finds playback targets on the local network and makes them part of the Spotify Connect device picker. It supports normal Spotify Connect receivers, Chromecast-style devices, and Sonos players while keeping account hand-off and control on the correct path for each device. See Music playback and devices for the wider playback model and Player control and audio output for local and remote control.
Device discovery
Network discovery supplies the device picker with targets that Spotify's account device list does not yet show. The discovery system listens for two mDNS service types, then checks normal Connect receivers before it offers them to the user.
Receiver discovery
src/zeroconf.rs defines a receiver as a name, network address, service endpoint, and optional Spotify device identity. Spotify Connect receivers advertise _spotify-connect._tcp; Google Cast devices advertise _googlecast._tcp. The system keeps these sources separate because a Connect receiver can answer an HTTP information request, while a Cast device must first be started through Cast v2.
The discovery pass runs on the local network and updates the picker as Cast results arrive. It prefers a usable address, removes stale advertisements, and avoids showing the same speaker twice when it advertises both services. A Connect receiver becomes ready after it reports a stable device identity. A Cast receiver gets a temporary identity from its advertised name until activation.
References: src/zeroconf.rs• L61-103 src/zeroconf.rs• L164-197 src/zeroconf.rs• L200-334
Receiver activation
Activation hands the signed-in playback account to a receiver that is visible on the LAN but is not yet in Spotify's device list. The picker starts this work only after the user selects the receiver, so discovery itself does not send account data.
The process follows this path:
flowchart LR
A[mDNS advertisement] --> B[Resolve receiver identity]
B --> C[Show network target]
C --> D{User selects target}
D --> E[Connect receiver sign-in]
D --> F[Cast receiver start]
E --> G[Spotify Connect device]
F --> G
For a normal Connect receiver, the system reads device information and sends an encrypted account hand-off to the receiver. For a Cast receiver, the system starts Spotify on the device and obtains a device token from the active playback session. Both paths end with an ordinary Spotify Connect device that can receive playback.
References: src/zeroconf.rs• L336-355 src/zeroconf.rs• L357-418 src/zeroconf.rs• L421-467 src/backend.rs• L4215-4295
Chromecast-style casting
Chromecast-style casting adds Spotify to Google Cast devices that do not run Spotify until a sender starts it. The Cast flow uses the device's local Cast service for setup, then returns control to the normal Spotify Connect path.
Cast activation
src/cast.rs is the Cast activation boundary. It connects to the advertised Cast endpoint, launches Spotify's Cast receiver app, asks that app for its client identity, exchanges the session token for a device token, and sends that token to Spotify on the Cast device. After this hand-off, the device uses the name-derived Spotify Connect identity and normal transfer logic takes over.
The activation has one bounded operation and retries a device refusal once. It answers Cast heartbeat messages while it waits, so a slow device or token exchange does not create an unbounded wait.
References: src/cast.rs• L1-21 src/cast.rs• L66-105 src/cast.rs• L107-158 src/cast.rs• L192-257
Cast device trust
Cast setup must prove that the local endpoint is a genuine Google Cast device before it receives an account token. The device signs a fresh challenge and the connection certificate, and the client checks that proof against the bundled Google Cast roots. The token is not written to disk or placed in logs.
The trust material is part of the product data rather than a user setting. Certificate fixtures cover current and older Cast devices, including first-generation Chromecast behavior, so changes to the trust flow can be checked without a live device.
References: src/cast.rs• L38-62 src/cast.rs• L133-188 src/cast.rs• L401-545 src/cast.rs• L891-912 assets/…/cast-root-ca.der assets/…/eureka-root-ca.der
Sonos control
Sonos support covers a gap between local discovery and Spotify's public device API. A Sonos player can be absent while idle and can reject Web API player commands even when it is playing, so Spotifast discovers it from its local Spotify endpoint and controls it through Spotify Connect state.
Sonos discovery and presentation
src/sonos.rs identifies a Sonos player by the ZeroConf endpoint at port 1400 with the /spotifyzc path. The controller remembers resolved Sonos identities and current addresses. The device picker adds a Sonos row when Spotify's normal device list does not contain it.
The picker treats the network row as a real playback target, not as a separate local player. This lets a user select an idle Sonos before Spotify reports it through the account API.
References: src/sonos.rs• L1-15 src/sonos.rs• L31-87 src/sonos.rs• L165-185 src/…/devices.rs• L454-480
Sonos Connect control
Restricted-device commands use the signed-in session's Connect state service instead of the Web API. The controller tracks restricted identities and the device that the latest playback poll says is active. This matters because Spotify can report a Sonos without an id, or return an older poll after playback has moved.
The control path covers transfer, play, pause, seek, shuffle, repeat, volume, and queue writes. It preserves the order of queue batches and reports the accepted prefix if a later queue write fails.
References: src/sonos.rs• L89-163 src/sonos.rs• L187-214 src/sonos.rs• L263-369 src/backend.rs• L6134-6199
Device history
Device history improves the picker order without storing network details or credentials. src/device_history.rs stores recent output preference keys for one verified Spotify account and uses them to rank the combined list of account devices and newly discovered receivers.
Output ranking
The picker places the active output first. The remaining rows use account-scoped selection frequency, then recency, then stable display ties. History keeps a window of 32 choices, so old preferences age out instead of growing without bound.
Cast devices use an endpoint-scoped preference key rather than only their name-derived transfer id. This keeps two nearby Cast endpoints from sharing one history entry, while normal Connect devices and Sonos players use their stable device identity.
References: src/device_history.rs• L1-45 src/…/devices.rs• L14-70 src/zeroconf.rs• L77-89 src/settings.rs• L1851-1865
Account boundaries
A history entry is valid only for the account that created it. A new account clears the old ranking window, and signing out clears the in-memory session history. Empty account or device keys are ignored, so discovery failures do not create misleading preferences.
References: src/device_history.rs• L17-43 src/app.rs• L3416-3451
Diagnostics and integration assets
The repository includes small probes for checking network discovery and activation outside the main picker. It also includes the Cast icon, trust roots, and protocol fixtures that support the device integration boundary.
Network probes
The probes isolate the main device paths:
- examples/discovery_probe.rs compares live Cast result updates with final batch discovery.
- examples/connect_probe.rs browses Spotify Connect receivers, reads their information, and can hand an account to one.
- examples/cast_probe.rs starts Spotify on a Cast device and signs it in through a connected playback session.
These tools help separate a LAN, receiver, or account problem from a device-picker problem. They require access to the same local network and stored playback credentials used by the corresponding production flow.
References: examples/discovery_probe.rs• L1-45 examples/connect_probe.rs• L1-75 examples/cast_probe.rs• L1-41
Integration assets
Cast trust roots are loaded from assets/cast/, while assets/…/cast.svg supplies the Cast marker in the device picker. src/testdata/cast/ contains certificate chains, connection certificates, keys, and signatures used to model current and legacy Cast authentication.
References: src/cast.rs• L38-50 src/cast.rs• L891-912 assets/…/cast.svg• L1-16
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
_googlecast._tcpdiscovery separate from HTTP receiver resolution; a Cast row must not trigger a device-start operation during scanning. - Keep Cast preference keys distinct from Cast transfer ids, or nearby endpoints can share or lose picker history.
- Keep Sonos identity updates aligned with playback-poll ordering, or commands can reach a stale device after playback moves.
- Keep Cast trust roots and legacy certificate fixtures aligned when changing Cast authentication support.
- Keep receiver activation tied to the current playback account, or a stale account can be handed to a LAN device.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A Cast receiver is sent through the HTTP probe | It is dropped or waits for a user selection that has not happened | Skip HTTP resolution for Cast rows and activate them only after selection |
| A Cast endpoint fails device authentication | No account token is requested or sent | Verify the device proof against the bundled Cast roots first |
| A Sonos is absent from Spotify's device list | The user cannot select or control an idle Sonos | Add resolved ZeroConf Sonos rows and route restricted commands over Connect state |
| An older Sonos playback poll wins over a newer one | Commands can target the wrong restricted device | Ignore older poll results and match the device named when the poll was sent |
| The signed-in account changes during activation | Credentials can be handed to the wrong receiver | Check the credential lease before device information and before the final hand-off |
Key files
| File | Why to read it |
|---|---|
| src/zeroconf.rs | Discovery, receiver identity, resolution, duplicate handling, and Connect activation |
| src/cast.rs | Cast app launch, token hand-off, device proof, and Cast protocol behavior |
| src/sonos.rs | Sonos recognition, identity tracking, restricted-device control, and queue writes |
| src/backend.rs | Production orchestration for discovery, activation, and Connect fallback control |
| src/…/devices.rs | The device picker and the way discovered rows join account devices |
| src/device_history.rs | Account-scoped output ranking and its bounded history window |
| examples/discovery_probe.rs | Live versus batch discovery diagnosis |
| examples/connect_probe.rs | Spotify Connect receiver discovery and account activation diagnosis |
| examples/cast_probe.rs | Direct Cast activation diagnosis |
assets/cast/cast-root-ca.der | Current Cast trust root used during device proof |
assets/cast/eureka-root-ca.der | Legacy Cast trust root used for older devices |
| assets/…/cast.svg | Cast marker shown in the device picker |
src/testdata/cast/ | Local fixtures for Cast certificate and signature behavior |