Skip to main content

AudioPlayback

Struct AudioPlayback 

Source
pub struct AudioPlayback {
    pub(crate) shared: Arc<PbShared>,
}
Expand description

A mic-uplink playback, symmetric to AudioCapture: the same lifecycle protocol, but a PulseAudio playback stream instead of a record stream, and a bounded drop-oldest queue fed by write / write_red instead of a sink. The caller never holds the stream — only the playback thread touches it — so a close-versus-inflight-write use-after-free is structurally impossible here. Dropping the handle stops the playback.

Fields§

§shared: Arc<PbShared>

Implementations§

Source§

impl AudioPlayback

Source

pub fn new() -> Self

An idle playback; nothing runs until start.

Source

pub(crate) fn inner(&self) -> &Arc<Inner> ⓘ

Source

pub(crate) fn decode_packet( &self, packet: &[u8], decode: impl FnOnce(&mut OpusPlaybackDecoder, &[u8], &PlayQueue), ) -> Result<(), Error>

Run decode on this run’s Opus decoder with packet. The shared body of write and write_red.

Gated on worker_alive: it fails with Error::NotRunning once no playback thread services the queue (start failure, stop, or a PA outage the session loop could not reconnect through), so the caller’s reopen-on-error path engages instead of the audio being swallowed silently. A reconnect in progress stays “alive” and keeps queueing. The decode holds the decoder’s lock for one packet and nothing else.

Source

pub fn start(&self, settings: &AudioPlaybackSettings) -> Result<(), Error>

Start (or restart) mic playback into the virtual sink. The playback mirror of AudioCapture::start.

Same shape as capture: a re-entrant start from the playback thread just undoes a nested self-stop; the settings are validated (AudioPlaybackSettings::validate); the worker is spawned (the stop/clear ordering lives in spawn_worker); the handle is registered for stop_all; and the ~2 s await_start handshake returns a FAILED start as Error::Start (with last_error) after tearing down only the thread THIS call spawned (identity-checked, sparing a concurrent winner), while a start still in its retry ladder returns Ok and is watched through state / last_error. Before spawning, it applies this run’s byte bound + frame alignment to the queue (dropping any stale audio) and creates the Opus decoder up front, since the mic uplink is always Opus and write / write_red decode packets to PCM on the writer’s thread for this same run.

Source

pub fn write(&self, packet: &[u8]) -> Result<(), Error>

Push one Opus mic packet for playback. The steady-state hot path.

See decode_packet for the liveness gate. A bad packet is dropped rather than corrupting the stream, and the call never blocks on PA (drop-oldest happens inside PlayQueue::push).

Source

pub fn write_red(&self, packet: &[u8], primary_ts: i64) -> Result<(), Error>

Play one RFC 2198 RED mic frame from the WebRTC/UDP uplink, recovering across any packet loss on the way in. The lossy-transport counterpart of write.

The payload is de-framed, loss-recovered, and decoded by decode_red_into_queue (see there for why RED exists). primary_ts is the packet’s monotonic RTP timestamp; the redundant blocks carry offsets back from it. Gated on worker_alive exactly like write (see decode_packet).

Source

pub fn stop(&self)

Stop mic playback, joining the playback thread.

A re-entrant stop from the playback thread records a self-stop only (it cannot self-join) and never clobbers an external stop already in effect. Otherwise it joins; stop returns only once the thread is joined and the sink is released.

Source

pub fn is_running(&self) -> bool

True while a playback worker is connected and running with no stop pending; false while still starting and after a failure — see state to tell those apart.

Source

pub fn state(&self) -> State

Lifecycle phase, the playback mirror of AudioCapture::state.

Source

pub fn last_error(&self) -> Option<String>

Why the last run failed, or None while no run has failed since the last start.

Trait Implementations§

Source§

impl Default for AudioPlayback

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for AudioPlayback

Source§

fn drop(&mut self)

Best-effort stop; symmetric to AudioCapture::drop.

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§

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.