pub trait Sink: Send + 'static {
// Required methods
fn open(&mut self, format: AudioFormat) -> Result<()>;
fn write(&mut self, frame: AudioFrame<'_>, due: Instant) -> Result<()>;
fn flush(&mut self) -> Result<()>;
// Provided methods
fn discard_from(&mut self, due: Instant) -> Result<()> { ... }
fn pre_port_delay(&self) -> Duration { ... }
fn output_delay(&self) -> Option<Duration> { ... }
fn underruns(&self) -> u64 { ... }
}Expand description
Audio output owned by the host application.
A sink is moved into the connection and reused across reconnects, so its output device may stay open for the whole lifetime of the connection rather than per session. Dropping it must stop playback: the client drops the sink when the connection ends.
Required Methods§
Sourcefn open(&mut self, format: AudioFormat) -> Result<()>
fn open(&mut self, format: AudioFormat) -> Result<()>
Prepares the output for format.
The client calls this once per announced stream, after flushing the previous one, and before it hands over the first chunk. The same sink sees the streams of every connection attempt, and a new attempt announces its own format, so an implementation must not assume that the format it last saw is still current. A format the sink cannot play is an error, which ends the connection: the client does not retry a sink that failed.
Sourcefn write(&mut self, frame: AudioFrame<'_>, due: Instant) -> Result<()>
fn write(&mut self, frame: AudioFrame<'_>, due: Instant) -> Result<()>
Writes one frame.
The client schedules playout and calls this at the frame’s wake time, so the sink writes it
now and must not schedule it again - otherwise a frame whose sink buffers internally is
delayed twice. The call is already early enough to fill the sink’s
Sink::pre_port_delay.
due is the local instant at which the first sample of the frame should reach the audio
port, with the sink’s Sink::output_delay already subtracted. The sink’s pre-port delay
is compensated by the instant of this call, not by this value. due is informational: a
sink that forwards the frame to its pipeline ignores it, one that buffers ahead may compare
it with its own clock, and a deadline that is already past means “write as soon as
possible”. The client reports std::time::Instant::now while its clock estimate is not
ready, so the value carries no guarantee in that state.
An error ends the connection: the host cannot recover a broken output from here, and the client does not retry a sink that failed.
Sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Drops audio that is buffered but not audible yet.
Called:
- on
stream/start, after the previous stream and beforeSink::openfor the new one, - on
stream/end, afterEvent::StreamEnd, - on a
stream/clearthat carries no timestamp, afterEvent::StreamClear; a clear that carries one is aSink::discard_frominstead, - when the server removes the
player@v1role, after anEvent::StreamEndwithStreamEndReason::RoleRemoved, and - after a connection attempt ends, before the client reconnects, so audio buffered for the session that ended cannot become audible on the next one.
It must succeed when no stream is open, so a sink can treat it as “discard everything, keep the output device”. An error ends the connection.
Provided Methods§
Sourcefn discard_from(&mut self, due: Instant) -> Result<()>
fn discard_from(&mut self, due: Instant) -> Result<()>
Drops the buffered audio that is due at or after due, keeping what plays earlier.
Called on a stream/clear that carries a from_timestamp, after
Event::StreamClear. The client already dropped its own queued
frames at or after that server timestamp; this call is for the audio the sink holds itself.
due is the server timestamp mapped through the client’s clock estimate, with the sink’s
Sink::output_delay subtracted: the local deadline the sink would have been given for that
server time. It is std::time::Instant::now while no estimate is ready, for a timestamp
that already lies in the past, and when the mapping would overflow — each of which drops
everything buffered and matches Sink::flush.
It must succeed when no stream is open or no queue exists: an empty queue discards nothing.
A clear can arrive before stream/start or after stream/end, and the client does not
withhold it.
The default drops everything, so a sink with no timestamped queue keeps the flush behaviour without implementing this. An error ends the connection.
Sourcefn pre_port_delay(&self) -> Duration
fn pre_port_delay(&self) -> Duration
Reports the delay between Sink::write and the audio port: decoder, pipeline, driver and
device buffers inside the sink. Not the post-port delay reported to the server; that is
Sink::output_delay.
The client calls Sink::write this much before the frame’s playout deadline, so the frame
reaches the port at due. A sink that forwards frames straight to its pipeline leaves this
at zero; one whose output buffers internally reports the buffer here so the client can fill
it. A value that belongs on the post-port side raises the lead the server sends ahead
instead of compensating it, because the server factors output_delay into that lead.
The client polls this when a stream opens, after every client/state report, after every
clock burst, once about half a second after an accepted set_output_delay, and when the host
calls ControlHandle::output_delay_applied; a change moves the timer, and because the
reported lead is at least the pre-port delay, a change that moves that lead in either
direction sends an updated required_lead_time_ms. The default is zero.
Sourcefn output_delay(&self) -> Option<Duration>
fn output_delay(&self) -> Option<Duration>
Reports the delay the output adds after the audio port: external speakers, amplifiers, a
chain after the DAC. Decoder, pipeline and device-internal buffer latency belong in the host’s
playout scheduling, not here. A conformant server factors this value into its send-ahead, so
reporting a buffer here raises the lead instead of compensating it; the server in this
workspace ignores the reported timing until docs/spec-deviations.md #17 closes.
The host restores the same value across restarts and reconnections: the protocol requires
output_delay_ms to persist, and this trait is the only place the client learns it. The
client polls it at the same points as Sink::pre_port_delay (stream open, after every
client/state report, after every clock burst, the fallback read after set_output_delay
and ControlHandle::output_delay_applied); a change re-maps the queued
deadlines and sends a new client/state, because output_delay_ms is a player field.
None means the sink does not know, which the client treats as no additional delay.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".