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§
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.
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.
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
stream/clear, which seeks or restarts the stream without ending it, afterEvent::StreamClear, - 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§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".