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 latency(&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:

  • on stream/start, after the previous stream and before Sink::open for the new one,
  • on stream/end, after Event::StreamEnd,
  • on stream/clear, which seeks or restarts the stream without ending it, after Event::StreamClear,
  • when the server removes the player@v1 role, after an Event::StreamEnd with StreamEndReason::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§

Source

fn latency(&self) -> Option<Duration>

Reports the output delay the sink adds beyond the audio port.

The client uses it when it reports its timing to the server. 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§