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    pub sample_format: ffmpeg::format::Sample,
16    pub sample_rate: u32,
17    pub channels: u16,
18}
19
20impl AudioFormat {
21    pub fn new(sample_format: ffmpeg::format::Sample, sample_rate: u32, channels: u16) -> Self {
22        Self {
23            sample_format,
24            sample_rate,
25            channels,
26        }
27    }
28
29    pub fn channels(self) -> u16 {
30        self.channels
31    }
32
33    pub fn channel_layout(self) -> ffmpeg::ChannelLayout {
34        ffmpeg::ChannelLayout::default(i32::from(self.channels))
35    }
36}