2023-01-03 18:58:25 +00:00
|
|
|
use std::ops::{Bound::*, RangeBounds};
|
|
|
|
|
2022-06-22 17:10:23 +00:00
|
|
|
use gst::Caps;
|
2023-01-03 18:58:25 +00:00
|
|
|
|
2023-01-15 18:38:13 +00:00
|
|
|
use glib::IntoGStr;
|
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{AudioFormat, AudioLayout};
|
2022-06-22 17:10:23 +00:00
|
|
|
|
|
|
|
pub struct AudioCapsBuilder<T> {
|
|
|
|
builder: gst::caps::Builder<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioCapsBuilder<gst::caps::NoFeature> {
|
|
|
|
pub fn new() -> Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
let builder = Caps::builder(glib::gstr!("audio/x-raw"));
|
2022-08-09 11:48:16 +00:00
|
|
|
let builder = AudioCapsBuilder { builder };
|
|
|
|
builder
|
|
|
|
.rate_range(..)
|
|
|
|
.channels_range(..)
|
|
|
|
.layout_list([AudioLayout::Interleaved, AudioLayout::NonInterleaved])
|
|
|
|
.format_list(AudioFormat::iter_raw())
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 09:29:13 +00:00
|
|
|
pub fn new_interleaved() -> Self {
|
|
|
|
AudioCapsBuilder::new().layout(AudioLayout::Interleaved)
|
|
|
|
}
|
|
|
|
|
2022-06-22 17:10:23 +00:00
|
|
|
pub fn any_features(self) -> AudioCapsBuilder<gst::caps::HasFeatures> {
|
|
|
|
AudioCapsBuilder {
|
|
|
|
builder: self.builder.any_features(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 18:38:13 +00:00
|
|
|
pub fn features(
|
|
|
|
self,
|
|
|
|
features: impl IntoIterator<Item = impl IntoGStr>,
|
|
|
|
) -> AudioCapsBuilder<gst::caps::HasFeatures> {
|
2022-06-22 17:10:23 +00:00
|
|
|
AudioCapsBuilder {
|
|
|
|
builder: self.builder.features(features),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AudioCapsBuilder<gst::caps::NoFeature> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AudioCapsBuilder<T> {
|
|
|
|
pub fn format(self, format: AudioFormat) -> Self {
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self.builder.field(glib::gstr!("format"), format.to_str()),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_list(self, formats: impl IntoIterator<Item = AudioFormat>) -> Self {
|
|
|
|
Self {
|
|
|
|
builder: self.builder.field(
|
2023-01-15 18:38:13 +00:00
|
|
|
glib::gstr!("format"),
|
2022-06-22 17:10:23 +00:00
|
|
|
gst::List::new(formats.into_iter().map(|f| f.to_str())),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rate(self, rate: i32) -> Self {
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self.builder.field(glib::gstr!("rate"), rate),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rate_range(self, rates: impl RangeBounds<i32>) -> Self {
|
|
|
|
let (start, end) = range_bounds_i32_start_end(rates);
|
|
|
|
let gst_rates = gst::IntRange::<i32>::new(start, end);
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self.builder.field(glib::gstr!("rate"), gst_rates),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rate_list(self, rates: impl IntoIterator<Item = i32>) -> Self {
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self
|
|
|
|
.builder
|
|
|
|
.field(glib::gstr!("rate"), gst::List::new(rates)),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channels(self, channels: i32) -> Self {
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self.builder.field(glib::gstr!("channels"), channels),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channels_range(self, channels: impl RangeBounds<i32>) -> Self {
|
|
|
|
let (start, end) = range_bounds_i32_start_end(channels);
|
|
|
|
let gst_channels: gst::IntRange<i32> = gst::IntRange::new(start, end);
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self.builder.field(glib::gstr!("channels"), gst_channels),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channels_list(self, channels: impl IntoIterator<Item = i32>) -> Self {
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self
|
|
|
|
.builder
|
|
|
|
.field(glib::gstr!("channels"), gst::List::new(channels)),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn layout(self, layout: AudioLayout) -> Self {
|
|
|
|
Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
builder: self
|
|
|
|
.builder
|
|
|
|
.field(glib::gstr!("layout"), layout_str(layout)),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn layout_list(self, layouts: impl IntoIterator<Item = AudioLayout>) -> Self {
|
|
|
|
Self {
|
|
|
|
builder: self.builder.field(
|
2023-01-15 18:38:13 +00:00
|
|
|
glib::gstr!("layout"),
|
2022-06-22 17:10:23 +00:00
|
|
|
gst::List::new(layouts.into_iter().map(layout_str)),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:26:30 +00:00
|
|
|
pub fn channel_mask(self, channel_mask: u64) -> Self {
|
|
|
|
Self {
|
|
|
|
builder: self
|
|
|
|
.builder
|
|
|
|
.field("channel-mask", gst::Bitmask::new(channel_mask)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fallback_channel_mask(self) -> Self {
|
2023-01-15 18:38:13 +00:00
|
|
|
let channels = self.builder.structure().get::<i32>(glib::gstr!("channels"));
|
2022-10-14 13:26:30 +00:00
|
|
|
match channels {
|
|
|
|
Ok(channels) => Self {
|
|
|
|
builder: self.builder.field(
|
2023-01-15 18:38:13 +00:00
|
|
|
glib::gstr!("channel-mask"),
|
2022-10-14 13:26:30 +00:00
|
|
|
gst::Bitmask::new(crate::AudioChannelPosition::fallback_mask(channels as u32)),
|
|
|
|
),
|
|
|
|
},
|
2023-01-25 08:09:45 +00:00
|
|
|
Err(e) => panic!("{e:?}"),
|
2022-10-14 13:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:56:53 +00:00
|
|
|
pub fn field(self, name: &str, value: impl Into<glib::Value> + Send) -> Self {
|
2022-06-22 17:10:23 +00:00
|
|
|
Self {
|
|
|
|
builder: self.builder.field(name, value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 10:19:49 +00:00
|
|
|
#[must_use]
|
2022-06-22 17:10:23 +00:00
|
|
|
pub fn build(self) -> gst::Caps {
|
|
|
|
self.builder.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_bounds_i32_start_end(range: impl RangeBounds<i32>) -> (i32, i32) {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
let start = match range.start_bound() {
|
|
|
|
Unbounded => 1,
|
|
|
|
Excluded(n) => n + 1,
|
|
|
|
Included(n) => *n,
|
|
|
|
};
|
|
|
|
let end = match range.end_bound() {
|
|
|
|
Unbounded => i32::MAX,
|
|
|
|
Excluded(n) => n - 1,
|
|
|
|
Included(n) => *n,
|
|
|
|
};
|
|
|
|
(start, end)
|
|
|
|
}
|
|
|
|
|
2023-01-15 18:38:13 +00:00
|
|
|
fn layout_str(layout: AudioLayout) -> &'static glib::GStr {
|
2022-06-22 17:10:23 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
match layout {
|
2023-01-15 18:38:13 +00:00
|
|
|
crate::AudioLayout::Interleaved => glib::gstr!("interleaved"),
|
|
|
|
crate::AudioLayout::NonInterleaved => glib::gstr!("non-interleaved"),
|
|
|
|
crate::AudioLayout::__Unknown(_) => glib::gstr!("unknown"),
|
2022-06-22 17:10:23 +00:00
|
|
|
}
|
|
|
|
}
|