Skip to main content

Sink

Trait Sink 

Source
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 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§

Source

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.

Source

fn write(&mut self, frame: AudioFrame<'_>, due: Instant) -> Result<()>

Writes one frame.

due is the local instant at which the first sample of the frame should reach the speakers. It is a playout deadline, not a wake-up call: a sink that buffers ahead compares it with its own clock, and a deadline that is already past means “play as soon as possible”. The client reports std::time::Instant::now while its clock estimate is not ready, so the deadline 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.

Source

fn flush(&mut self) -> Result<()>

Drops audio that is buffered but not audible yet.

Called:

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§

Source

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. due is the server timestamp mapped through the client’s clock estimate. 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.

Source

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.

None means the sink does not know, which the client treats as no additional delay.

Source

fn underruns(&self) -> u64

Reports how many times the output ran empty since it was opened.

This is diagnostic only; the client never changes its behaviour because of it.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§