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§
Implementations§
Source§impl AudioPlayback
impl AudioPlayback
pub(crate) fn inner(&self) -> &Arc<Inner> ⓘ
Sourcepub(crate) fn decode_packet(
&self,
packet: &[u8],
decode: impl FnOnce(&mut OpusPlaybackDecoder, &[u8], &PlayQueue),
) -> Result<(), Error>
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.
Sourcepub fn start(&self, settings: &AudioPlaybackSettings) -> Result<(), Error>
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.
Sourcepub fn write(&self, packet: &[u8]) -> Result<(), Error>
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).
Sourcepub fn write_red(&self, packet: &[u8], primary_ts: i64) -> Result<(), Error>
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).
Sourcepub fn stop(&self)
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.
Sourcepub fn is_running(&self) -> bool
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.
Sourcepub fn last_error(&self) -> Option<String>
pub fn last_error(&self) -> Option<String>
Why the last run failed, or None while no run has failed since the last start.