Skip to main content

media_pp/elements/
audio_format.rs

1use ffmpeg_next as ffmpeg;
2
3/// A complete uncompressed-audio format description.
4///
5/// Unlike a `(sample_rate, channels)` tuple, this also carries the sample
6/// representation, so it can be passed directly from a hardware endpoint
7/// such as `WasapiCaptureSource`/`WasapiRenderer` to an
8/// [`crate::elements::AudioResampler`]/[`crate::elements::SwAudioEncoder`]
9/// without guessing either one. Kept directly under `elements` rather than
10/// under any one of those (same reasoning as
11/// [`crate::elements::RtspTransport`]/[`crate::elements::VideoFormat`]'s own
12/// placement): it crosses source/filter/sink, not owned by any single one.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct AudioFormat {
15    /// In-memory representation and planar/packed layout of each sample.
16    pub sample_format: ffmpeg::format::Sample,
17    /// Number of samples per channel per second, in hertz.
18    pub sample_rate: u32,
19    /// Number of interleaved or planar audio channels.
20    pub channels: u16,
21}
22
23impl AudioFormat {
24    /// Creates an audio format from its sample representation, rate, and channel count.
25    pub fn new(sample_format: ffmpeg::format::Sample, sample_rate: u32, channels: u16) -> Self {
26        Self {
27            sample_format,
28            sample_rate,
29            channels,
30        }
31    }
32
33    /// Returns the configured channel count.
34    pub fn channels(self) -> u16 {
35        self.channels
36    }
37
38    /// Returns FFmpeg's default channel layout for [`Self::channels`].
39    pub fn channel_layout(self) -> ffmpeg::ChannelLayout {
40        ffmpeg::ChannelLayout::default(i32::from(self.channels))
41    }
42}