YAACC - UPNP Client and Server
Loading...
Searching...
No Matches
de.yaacc.multiroom.engine.ClientEngine Class Reference

The core of the Snapcast client: turns what the connection reports (settings, codec headers, chunks, offset updates, a lost connection) into writes to an AudioSink so that every sample is presented at the time the server wants it. More...

Collaboration diagram for de.yaacc.multiroom.engine.ClientEngine:
Collaboration graph

Public Member Functions

 ClientEngine (ClientEngineConfig config, AudioSink sink, DecoderFactory decoders, OffsetSource offsets, MicroClock clock)
 
void onServerSettings (StreamSettings settings)
 The settings are global: they apply to whatever plays, whenever they arrive (no pairing with a header).
 
void setManualLatencyMs (int milliseconds)
 Set by the user: positive plays earlier (like the latency of the server).
 
void setConnectionStats (ConnectionStats stats)
 The counters of the connection (the host hands them over before every status); used for the status and the trace.
 
void noteConnection (TraceKind kind, long a, long b, long c)
 A connection event (attempt, connected) for the trace; a new connection starts counting its chunks again.
 
void onOffsetUpdate (long offsetMicros, long jitterMicros)
 
void onCodecHeader (CodecHeader newHeader)
 
void onChunk (StreamChunk chunk)
 
void onConnectionLost (int reasonOrdinal, int ownLimitCode)
 As onConnectionLost(), and the reason (a LossReason ordinal and our own limit code) goes in the trace.
 
void onConnectionLost ()
 The connection to the server was lost: silence the output; a reconnect brings the header again.
 
void tick ()
 One cycle: releases due chunks, polls the output, writes what is due.
 
void stop ()
 Ends the session on request: the output and the decoder are freed.
 
void abort (EngineFailure reason)
 Ends the session with a failure that the caller found (a watchdog, an exception on the engine thread).
 
void onChunkLost ()
 A chunk was lost before it reached the engine (the host queue was full): a flac stream can not find the hole.
 
boolean isTerminal ()
 
EventTrace trace ()
 The event trace (thread safe; it outlives the engine's session in the last status).
 
EngineStatus status ()
 A snapshot; call it from the engine thread.
 

Detailed Description

The core of the Snapcast client: turns what the connection reports (settings, codec headers, chunks, offset updates, a lost connection) into writes to an AudioSink so that every sample is presented at the time the server wants it.

Everything it touches is an interface (AudioSink, DecoderFactory, OffsetSource, MicroClock), so it runs on the JVM with fakes. It is not thread safe: one thread (the engine thread of EngineHost) makes every call.

Time. The play time of the stream sample with index s (counted from the anchor, the timestamp of the first chunk after a start or a resync) is, on the client clock, anchor + s / rate + bufferMs - (server latency + manual latency) - offset. The output reports when it presents which frame (PlaybackTracker); from that the engine knows when the next frame it writes will be presented, and the difference to the play time is the drift (positive: late). Because the report is of the output's own pipeline, the latency of the output is already in it; only what the platform can not see (a Bluetooth speaker) needs the manual latency.

What it does with the drift. The sink is kept fed all the time (silence when there is no audio due), so the output never runs dry and always has a position. Before audio is first played (and after a gap, a step or a resync) it is aligned exactly: silence is written until the next frame would be presented on time, or frames are skipped when it would be late. While playing, a drift above the dead band is corrected by dropping or repeating single frames, at most one per correctionIntervalFrames (DriftController); the pitch never changes. A drift above the gradual limit is a step (the offset or the settings moved), also done exactly; playback late by more than the hard bound means the output holds old audio, so it is flushed.

Rules for a hostile server (decisions of Group 7 and 8): an identical codec header is ignored, a different one restarts the decoder at most maxDecoderRestartsPerMinute times a minute (more ends the session); the settings are global and not paired with a header (a coalesced value may overtake an earlier header); an epoch change of the clock empties the queues but does not flush or re-create the output; chunk timestamps are never trusted (saturating maths in the scheduler, a limit on how far a timeline may run from its anchor); buffers are sized from the validated rate, channels and bit depth only; a chunk with a trailing partial frame loses it; at most two channels are played; decoding failures drop the chunk and a run of them ends the session.

Flac. The decoded frames of a flac stream are placed one after the other by counting them (the output of a platform decoder does not line up with the chunks), starting at the timestamp of the first chunk. What the count can not see, the timestamps can: Snapcast stamps the chunks of a flac stream with a running clock that is advanced by the duration of the encoded frames and restarted after a stall of the source, so the stamp of a chunk minus the position of its first frame (the skew) is constant until a stall adds the stall to it. The position comes from the FLAC frame header at the start of the chunk (FlacFrameHeader: the frame number the server's encoder wrote, whatever the decoder holds back). FlacTimeGuard judges the skew of such positions and inserts the silence of a stall, or asks for the stream to be placed anew. When the headers can not be read (three chunks in a row) the guard does nothing, for the rest of the stream: the frames are placed by counting as before and a jump of the timestamps of 300 ms or more places the stream anew; nothing is ever filled from a count of decoded frames (a decoder that holds a block back would look like a stall). Not yet heard on a device against a real server (the model is Snapcast 0.35.0's source, not a capture).

Member Function Documentation

◆ stop()

void de.yaacc.multiroom.engine.ClientEngine.stop ( )

Ends the session on request: the output and the decoder are freed.

Safe to call twice.

◆ tick()

void de.yaacc.multiroom.engine.ClientEngine.tick ( )

One cycle: releases due chunks, polls the output, writes what is due.

Call about every tickMs.

Here is the call graph for this function:

The documentation for this class was generated from the following file: