Skip to main content

Crate syncspin_client

Crate syncspin_client 

Source
Expand description

Client side of Sendspin.

This crate owns the session: it connects to a server, runs the Noise handshake, keeps a clock estimate and feeds audio to a sink the host provides. It stays device agnostic - the application that owns the speakers implements Sink and decides what playout means.

A connection always ends in Noise: the client refuses a server that does not complete the handshake and never falls back to cleartext. A successful attempt reports Event::Connected and then one event per change; an attempt that ends reports Event::Disconnected with the delay before the next one, and the crate reconnects on its own with the same sink and identity.

connect validates the configuration and spawns the connection; the first attempt and every retry run in that task and reach the caller through the EventStream. Only an invalid configuration or an address this build cannot use returns an error - an unreachable server is reported as Event::Disconnected. The ClientHandle waits for the connection to end and hands out a ControlHandle for transport commands, the volume level and mute state. The level follows ClientConfig::volume_policy - see clamp_volume_with, and clamp_volume for the default - and the value the server receives is the applied one. ClientConfig::volume and ClientConfig::muted are the level and mute state the first report carries, so a host passes the values it restored instead of letting the server see a baseline the output is not at. ControlHandle::report_volume is the other direction: a level the host reads from its own output is recorded and sent unchanged, because the policy limits requests rather than the level the device is at.

ClientConfig::device_info describes the device the client runs on and is announced in client/hello; a host without an answer passes None and the server sees only the player name.

Pairing records and the long-lived pairing PSK belong to the host: it implements storage::Storage and the client keeps what it restores, so a paired server is recognised across restarts. The crate does no file I/O of its own; storage::FileStorage is an optional JSON backend behind the storage-file feature for a host that wants one.

With a store the client offers the Pairing PSK method, the one method the protocol makes mandatory, and runs it when a server admits an attempt: it draws the long-term PSK, sends client/pair-init and client/pair-finalize, and writes the record after server/pair-finalize. Event::Paired reports the record and Event::PairingFailed an attempt that ended without one, always with the reason pair/abort carries. Without a store it offers no pairing method at all: a guest connection is unchanged, and a server that picks pairing anyway is answered with pair/abort reason method_not_supported. A pairing attempt is bounded by ClientConfig::pairing_timeout, DEFAULT_PAIRING_TIMEOUT by default.

With the pairing-code feature and a code in ClientConfig::static_pairing_code, the client also offers the Static Pairing Code method. Every attempt is gated by an operator gesture: ControlHandle::open_pairing_window opens a window on the live connection, the client answers a pairing activation with client/pair-pending while none is open, and the fifth failed key confirmation closes the window. The round runs CPace over X25519 with SHA-512 and delivers the new long-term PSK sealed under its output; that layer has not had an external review.

With the same feature and at least one entry in each of ClientConfig::code_formats and ClientConfig::code_out_channels, the client offers the Dynamic Pairing Code method instead of relying on a fixed secret. It draws a nonce per attempt, locks it with commit_B in client/pair-init, and derives the per-session code once the server’s nonce_A arrives: six digits, or a version-1 pairing token for a QR code. Event::PairingCode carries the code and its round; the code stays the same across the rounds of one attempt. A failed key confirmation asks the server for another round, and the twentieth failed round ends the attempt. After the limit the client holds dynamic attempts back - answering them with client/pair-pending - until ControlHandle::release_holdback is called, the deliberate operator action the spec requires.

A pairing record can also be dropped from the server’s side. server/unpair removes it, reports Event::Unpaired and closes the connection after client/goodbye with reason unpaired; a server that announces a psk_id the client does not hold is reported once per connection as Event::PairingRequired with reason PairingRequiredReason::CredentialMismatch, so a host can offer pairing again instead of the lost record going unnoticed. A deliberate close - ClientHandle::close or dropping the handle - sends client/goodbye with its reason, so the server can tell a restart from a shutdown and does not treat the close as a dropped link.

ControlHandle::pairing_token hands the operator the version-0 SP: token while a store is configured; it is a credential, so a host renders it only when the operator needs it.

With the discovery feature the client can also take the other half of connection.md: instead of dialling a server it advertises _sendspin._tcp and accepts the server that connects. Inbound names the host’s Discovery backend, the address to listen on and the path to serve; advertise publishes the record - the bound port and the path and name TXT records - and then serves one server at a time through the same session as a dialled connection. An upgrade that carries an Origin header is refused, as the spec requires, and the Noise handshake is unchanged because the server initiates it in both directions. A client uses one of the two methods at a time: while ClientConfig::inbound is set, connect refuses to dial. A second server that arrives while one is admitted is rejected with client/goodbye reason concurrent_attempt and closed, which is the spec’s admission rule for a client that caps itself at one connection. The mdns feature carries the mdns-sd implementation of Discovery; without it the trait is the seam, so a host can supply its own backend and a fake drives the tests.

A host that renders the stream sees its lifecycle as events: Event::StreamStart for a new format, Event::StreamEnd when the server pauses or removes the player role, and Event::StreamClear for a seek, plus Event::Track for the now-playing metadata with its position, duration and playback speed. The end and clear events arrive before the sink is flushed, so the host can mute, stop or restart its output first.

The connection keeps running until the audio sink fails, which the host has to repair, or until the handle or the event stream is dropped.

Modules§

storage
Where pairing records live.

Structs§

AudioFormat
The audio format a stream announces.
Client
Namespaced entry point, for callers that prefer a type to the free function.
ClientConfig
Everything the client needs to open a session.
ClientHandle
A live connection.
ControlHandle
Playback controls for a live connection.
DeviceInfo
What the client announces about the device it runs on.
DiscoveredServer
One server a browser reported.
EventStream
The receiving end of a session’s Event stream.
Identity
A long-lived X25519 keypair that identifies one side of a connection.
Inbound
The server-initiated half of the connection mode.
MdnsDiscovery
The mDNS implementation of Discovery.
PlaybackState
Playback state as the server describes it.
ServiceAdvertisement
One service to publish over mDNS.
StoredPsk
A pre-shared key the host stores for a known server.
VolumePolicy
Limits the session applies to a volume request.

Enums§

AudioFrame
One audio frame handed to a Sink.
AudioMode
How the client wants the stream delivered.
CodeFormat
Emission format of a dynamic pairing code.
CodeOutChannel
Channel a dynamic pairing code reaches the operator through.
Codec
Audio codec of a stream.
Event
Something that happened on the connection.
GoodbyeReason
Why a client says goodbye to a server.
PairingFailure
Why a pairing attempt ended without a record.
PairingRequiredReason
Why a server needs a pairing this client cannot satisfy with what it holds.
PskCategory
Category of a pre-shared key, as the server announces it in Noise message 1.
StreamEndReason
Why a player stream ended.

Constants§

CLIENT_SERVICE_TYPE
Service type a client publishes when it waits for a server to connect.
DEFAULT_INBOUND_ADDR
Address the inbound listener binds unless the host narrows it.
DEFAULT_INBOUND_PORT
Port connection.md recommends for a client’s Sendspin endpoint.
DEFAULT_MAX_BACKOFF
Longest delay between reconnect attempts a configuration starts with.
DEFAULT_PAIRING_TIMEOUT
How long one pairing attempt may run, measured from client/pair-init.
DEFAULT_PAIRING_WINDOW
How long an operator-opened pairing window admits attempts.
DEFAULT_SENDSPIN_PATH
WebSocket endpoint connection.md recommends for both directions.
INITIAL_BACKOFF
Delay before the first reconnect attempt, and the value the backoff resets to.
MAX_PAIRING_PENDING_TEXT_LEN
Longest client/pair-pending message the protocol allows, in bytes of UTF-8.
MAX_PLAYER_NAME_LEN
Longest player name the protocol allows, in bytes of UTF-8.
NORMAL_PLAYBACK_SPEED
Playback speed the protocol defines as normal, in per-mille.
SERVER_SERVICE_TYPE
Service type a server publishes for client-initiated connections.
STATIC_PAIRING_CODE_LEN
Number of digits in a static pairing code.

Traits§

Discovery
How a service is published and how remote ones are found.
Sink
Audio output owned by the host application.

Functions§

advertise
Advertises this client and serves the servers that connect to it.
clamp_volume
Returns the volume level to apply for requested while current is in force.
clamp_volume_with
Returns the volume level to apply for requested while current is in force, using policy.
connect
Connects to a server and returns its handle and event stream.
connect_with_psks
Connects with pairing keys the host stores for known servers.
discover_servers
Returns the servers discovery currently reports.
next_backoff
Returns the delay to wait before the next connection attempt.
version
Returns the version of this crate as recorded in its manifest.