pub struct Sink { /* private fields */ }Expand description
One stream of PCM being played, mixed with every other sink on the device.
Write decoded samples with write, then finish
to play the tail or drop the sink to stop immediately.
Writes are cheap and never block on the device: they remix to the device’s
layout and hand samples to a ring buffer that the audio thread drains on its
own clock, resampling to the device rate on the way.
Implementations§
Source§impl Sink
impl Sink
Sourcepub fn finish(self) -> Drain ⓘ
pub fn finish(self) -> Drain ⓘ
Play every queued sample before leaving the mix.
Await the returned handle to wait for the device; dropping it stops immediately. A device that is unavailable keeps the drain pending. The wait includes staged stretch output and up to one resampler block of trailing silence. A finished sink bypasses emergency refilling.
Sourcepub fn write(&mut self, samples: &[u8]) -> Result<Write, Error>
pub fn write(&mut self, samples: &[u8]) -> Result<Write, Error>
Play samples, in the layout this sink was built with.
Samples play back to back in write order. Nothing is scheduled against a
clock here, so pace writes with buffered: it reports
how far ahead of the speaker you are, which is the anchor an A/V sync
clock steers video by.
Received audio is time-stretched toward the current latency, or toward
the clock due anchors it to. A writer that outruns that
correction eventually overflows and drops the excess; one that leaves no
received audio underruns and plays silence. Both are logged and neither
is an error, since the next write can recover.
The returned Write counts input sample frames accepted and dropped;
dropped live audio should be observed for telemetry, not retried.
Sourcepub fn silence(&mut self)
pub fn silence(&mut self)
Declare that the audio written so far is followed by a silence its publisher announced, such as a push-to-talk mute.
Once that audio has played, the sink plays comfort noise at the level of
the quietest 20 ms it played, at most -60 dBFS, rather than running dry,
and the Observer counts no gap. That lasts until the
utterance resume starts plays. Writes before then, such
as a codec’s final frames, continue the audio before the silence.
A player calls it at the decoder’s Read::End,
the marker a publisher’s discontinuity
sends. The sink plays on; finish is what ends it.
Sourcepub fn buffered(&self) -> Duration
pub fn buffered(&self) -> Duration
How much audio is queued between the last write and the
speaker, the device’s own delay included.
The pacing signal for A/V sync: the sample sounding right now was written
at roughly last_timestamp - buffered(), so a video clock can steer
against it. It settles near Input::latency once playback is running,
climbs when the writer runs ahead, and falls toward the device’s delay
when it falls behind.
Sourcepub fn output_latency(&self) -> Option<Duration>
pub fn output_latency(&self) -> Option<Duration>
How long the device takes to sound what it reads: the delay its host
reports plus one device period. None until the device has called back.
A player holding a playout delay counts this in it, since the sink places
audio on due’s clock where it is heard.
Sourcepub fn set_latency(&mut self, latency: Duration) -> Result<(), Error>
pub fn set_latency(&mut self, latency: Duration) -> Result<(), Error>
Set the playout target, converging by stretching received PCM.
This is the buffer the sink keeps against jitter, counting the device’s
own delay: it slows down while what it holds runs short of it, and speeds up past it only to catch up
with the clock due anchors it to.
Must be non-zero, at most [moq_mux::container::DELAY_MAX], and fit in the current
ring. Each device open or rebuild allocates the then-current target plus
three seconds, less a 30 ms processing window reserved as headroom.
Does not resize or reset the device.
Sourcepub fn due(&mut self, at: Instant, stretch: Duration)
pub fn due(&mut self, at: Instant, stretch: Duration)
Anchor the audio written so far to the caller’s clock: its end is due to
sound at at, as heard after the device’s own delay.
The sink then plays onto that clock rather than at its latency alone:
behind it, it speeds up, and ahead of it, it slows down, at a rate
proportional to how far it is off, up to 10%. Audio about to run out
slows down too, falling behind the clock by at most stretch. Audio
trailing the clock by more than stretch skips onto it, and audio
leading it by as much waits in silence for it, however the clock moved.
A zero stretch turns the time stretch off: written audio plays
unaltered, and a sink more than a window off the clock skips onto it or
pads silence until the clock reaches it. Call it after each write and
whenever the clock moves; until the first call, or after the device
changes, the latency alone is the clock.