Skip to main content

Sink

Struct Sink 

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn resume(&mut self)

Start the next utterance after a declared silence with the next write.

It plays one latency after that write, behind whatever is still queued, with comfort noise filling the wait, the way a new sink’s first write plays behind its opening silence.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn input(&self) -> &Input

The PCM layout this sink was built with.

Source

pub fn control(&self) -> Control

This sink’s volume and level, as a handle a UI thread can hold while a decode task does the writing.

Trait Implementations§

Source§

impl Drop for Sink

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Sink

§

impl !UnwindSafe for Sink

§

impl Freeze for Sink

§

impl Send for Sink

§

impl Sync for Sink

§

impl Unpin for Sink

§

impl UnsafeUnpin for Sink

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more