Skip to main content

moq_audio/
lib.rs

1//! Native audio capture, encoding, and decoding for Media over QUIC.
2//!
3//! Counterpart to [`moq-video`](https://crates.io/crates/moq-video) for audio
4//! tracks, and shaped the same way. Sits on top of [`moq_mux`] and [`hang`] and
5//! adds the missing piece for native callers: Rust-native Opus and uncompressed
6//! PCM codecs that turn raw samples into HANG audio tracks and back, plus AAC-LC
7//! on the way in, which is what the gateways (RTMP, SRT, HLS, gstreamer) publish.
8//!
9//! - `capture` describes an audio source (`capture::Config`) and grabs buffers
10//!   per platform: a microphone via cpal (CoreAudio / WASAPI / ALSA) everywhere,
11//!   or macOS system audio via ScreenCaptureKit. `capture::Source` picks between
12//!   them and `capture::devices` lists the inputs and hands back the ids it
13//!   takes. Requires the `capture` feature, so these names are unlinked here:
14//!   they don't exist in a default build.
15//! - [`encode`] encodes PCM and publishes it through `moq_mux::container`,
16//!   registering the rendition in the `hang` catalog. Two entry points:
17//!   - `encode::publish_capture` captures a microphone and publishes it
18//!     (turnkey). It encodes strictly on demand: the track and catalog are
19//!     advertised up front, but the device opens only while a subscriber is
20//!     listening and is released when the last one leaves.
21//!   - [`encode::Producer`] publishes PCM you hand it.
22//! - [`decode`] subscribes to an encoded track and decodes it back to PCM.
23//!   [`decode::Consumer`] is the mirror of [`encode::Producer`]. It reads AAC-LC
24//!   too, behind the default-on `aac` feature, since a broadcast that came in
25//!   through a gateway is AAC rather than one of the two codecs we encode.
26//! - `playback` plays decoded PCM out a speaker. `playback::Engine` owns the
27//!   output device and mixes the `playback::Sink`s registered with it, so one
28//!   device serves every track in a call. Requires the `playback` feature, so
29//!   these names are unlinked here too.
30//! - `aec` keeps the speaker out of the microphone, which is what a conference
31//!   on a laptop needs to not send itself back. `playback::Engine::canceller`
32//!   builds an `aec::Canceller` from the mix it is playing and
33//!   `capture::Config::aec` hands it to the microphone. Requires the `aec`
34//!   feature, which implies both of the above.
35//!
36//! [`Format`] mirrors WebCodecs `AudioData.format`; the helpers convert between
37//! any supported layout and the interleaved `f32` representation libopus
38//! expects. [`Frame`] is a thin owned buffer: a timestamp and a payload. PCM
39//! layout lives on the producer / consumer via [`encode::Input`] /
40//! [`decode::Config`], not on each frame, so callers can't drift between calls.
41
42#[cfg(feature = "aac")]
43mod aac;
44mod error;
45mod format;
46mod frame;
47mod opus;
48mod pcm;
49mod resample;
50
51#[cfg(feature = "aec")]
52pub mod aec;
53#[cfg(feature = "capture")]
54pub mod capture;
55pub mod decode;
56pub mod encode;
57
58#[cfg(feature = "playback")]
59pub mod playback;
60
61pub use error::Error;
62pub use format::Format;
63pub use frame::Frame;
64pub use resample::Resampler;