{Audio,Video}CapsBuilder: add for_encoding constructor

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1222>
This commit is contained in:
François Laignel 2023-02-20 09:33:01 +01:00 committed by GStreamer Marge Bot
parent 8b6bf18bb0
commit 58475f4a25
2 changed files with 133 additions and 36 deletions

View file

@ -11,7 +11,19 @@ pub struct AudioCapsBuilder<T> {
} }
impl AudioCapsBuilder<gst::caps::NoFeature> { impl AudioCapsBuilder<gst::caps::NoFeature> {
// rustdoc-stripper-ignore-next
/// Constructs an `AudioCapsBuilder` for the "audio/x-raw" encoding.
///
/// If left unchanged, the resulting `Caps` will be initialized with:
/// - "audio/x-raw" encoding.
/// - maximum rate range.
/// - maximum channels range.
/// - both interleaved and non-interleaved layouts.
/// - all available formats.
///
/// Use [`AudioCapsBuilder::for_encoding`] to specify another encoding.
pub fn new() -> Self { pub fn new() -> Self {
assert_initialized_main_thread!();
let builder = Caps::builder(glib::gstr!("audio/x-raw")); let builder = Caps::builder(glib::gstr!("audio/x-raw"));
let builder = AudioCapsBuilder { builder }; let builder = AudioCapsBuilder { builder };
builder builder
@ -21,10 +33,34 @@ impl AudioCapsBuilder<gst::caps::NoFeature> {
.format_list(AudioFormat::iter_raw()) .format_list(AudioFormat::iter_raw())
} }
// rustdoc-stripper-ignore-next
/// Constructs an `AudioCapsBuilder` for the "audio/x-raw" encoding
/// with interleaved layout.
///
/// If left unchanged, the resulting `Caps` will be initialized with:
/// - "audio/x-raw" encoding.
/// - maximum rate range.
/// - maximum channels range.
/// - interleaved layout.
/// - all available formats.
///
/// Use [`AudioCapsBuilder::for_encoding`] to specify another encoding.
pub fn new_interleaved() -> Self { pub fn new_interleaved() -> Self {
AudioCapsBuilder::new().layout(AudioLayout::Interleaved) AudioCapsBuilder::new().layout(AudioLayout::Interleaved)
} }
// rustdoc-stripper-ignore-next
/// Constructs an `AudioCapsBuilder` for the specified encoding.
///
/// The resulting `Caps` will use the `encoding` argument as name
/// and will not contain any additional fields unless explicitly added.
pub fn for_encoding(encoding: impl IntoGStr) -> Self {
assert_initialized_main_thread!();
AudioCapsBuilder {
builder: Caps::builder(encoding),
}
}
pub fn any_features(self) -> AudioCapsBuilder<gst::caps::HasFeatures> { pub fn any_features(self) -> AudioCapsBuilder<gst::caps::HasFeatures> {
AudioCapsBuilder { AudioCapsBuilder {
builder: self.builder.any_features(), builder: self.builder.any_features(),
@ -180,3 +216,22 @@ fn layout_str(layout: AudioLayout) -> &'static glib::GStr {
crate::AudioLayout::__Unknown(_) => glib::gstr!("unknown"), crate::AudioLayout::__Unknown(_) => glib::gstr!("unknown"),
} }
} }
#[cfg(test)]
mod tests {
use super::AudioCapsBuilder;
#[test]
fn default_encoding() {
gst::init().unwrap();
let caps = AudioCapsBuilder::new().build();
assert_eq!(caps.structure(0).unwrap().name(), "audio/x-raw");
}
#[test]
fn explicit_encoding() {
gst::init().unwrap();
let caps = AudioCapsBuilder::for_encoding("audio/mpeg").build();
assert_eq!(caps.structure(0).unwrap().name(), "audio/mpeg");
}
}

View file

@ -10,7 +10,18 @@ pub struct VideoCapsBuilder<T> {
} }
impl VideoCapsBuilder<gst::caps::NoFeature> { impl VideoCapsBuilder<gst::caps::NoFeature> {
// rustdoc-stripper-ignore-next
/// Constructs an `VideoCapsBuilder` for the "video/x-raw" encoding.
///
/// If left unchanged, the resulting `Caps` will be initialized with:
/// - "video/x-raw" encoding.
/// - all available formats.
/// - maximum width range.
/// - maximum height range.
///
/// Use [`VideoCapsBuilder::for_encoding`] to specify another encoding.
pub fn new() -> Self { pub fn new() -> Self {
assert_initialized_main_thread!();
let builder = Caps::builder(glib::gstr!("video/x-raw")); let builder = Caps::builder(glib::gstr!("video/x-raw"));
let builder = VideoCapsBuilder { builder }; let builder = VideoCapsBuilder { builder };
builder builder
@ -20,6 +31,18 @@ impl VideoCapsBuilder<gst::caps::NoFeature> {
.framerate_range(..) .framerate_range(..)
} }
// rustdoc-stripper-ignore-next
/// Constructs an `VideoCapsBuilder` for the specified encoding.
///
/// The resulting `Caps` will use the `encoding` argument as name
/// and will not contain any additional fields unless explicitly added.
pub fn for_encoding(encoding: impl IntoGStr) -> Self {
assert_initialized_main_thread!();
VideoCapsBuilder {
builder: Caps::builder(encoding),
}
}
pub fn any_features(self) -> VideoCapsBuilder<gst::caps::HasFeatures> { pub fn any_features(self) -> VideoCapsBuilder<gst::caps::HasFeatures> {
VideoCapsBuilder { VideoCapsBuilder {
builder: self.builder.any_features(), builder: self.builder.any_features(),
@ -283,41 +306,60 @@ fn next_fraction(fraction: gst::Fraction) -> gst::Fraction {
gst::Fraction::new(new_num, new_den) gst::Fraction::new(new_num, new_den)
} }
#[test] #[cfg(test)]
fn test_0_1_fraction() { mod tests {
gst::init().unwrap(); use super::{next_fraction, previous_fraction, VideoCapsBuilder};
let zero_over_one = gst::Fraction::new(0, 1);
let prev = previous_fraction(zero_over_one);
assert_eq!(prev.numer(), -1);
assert_eq!(prev.denom(), i32::MAX);
let next = next_fraction(zero_over_one);
assert_eq!(next.numer(), 1);
assert_eq!(next.denom(), i32::MAX);
}
#[test] #[test]
fn test_25_1() { fn default_encoding() {
gst::init().unwrap(); gst::init().unwrap();
let twentyfive = gst::Fraction::new(25, 1); let caps = VideoCapsBuilder::new().build();
let next = next_fraction(twentyfive); assert_eq!(caps.structure(0).unwrap().name(), "video/x-raw");
//25.000000011641532 }
assert_eq!(next.numer(), 2147483626);
assert_eq!(next.denom(), 85899345); #[test]
let prev = previous_fraction(twentyfive); fn explicit_encoding() {
//24.999999988358468 gst::init().unwrap();
assert_eq!(prev.numer(), 2147483624); let caps = VideoCapsBuilder::for_encoding("video/mpeg").build();
assert_eq!(prev.denom(), 85899345); assert_eq!(caps.structure(0).unwrap().name(), "video/mpeg");
} }
#[test]
fn test_1_25() { #[test]
gst::init().unwrap(); fn test_0_1_fraction() {
let twentyfive = gst::Fraction::new(1, 25); gst::init().unwrap();
let next = next_fraction(twentyfive); let zero_over_one = gst::Fraction::new(0, 1);
//0.040000000018626 let prev = previous_fraction(zero_over_one);
assert_eq!(next.numer(), 85899345); assert_eq!(prev.numer(), -1);
assert_eq!(next.denom(), 2147483624); assert_eq!(prev.denom(), i32::MAX);
let prev = previous_fraction(twentyfive); let next = next_fraction(zero_over_one);
//0.039999999981374 assert_eq!(next.numer(), 1);
assert_eq!(prev.numer(), 85899345); assert_eq!(next.denom(), i32::MAX);
assert_eq!(prev.denom(), 2147483626); }
#[test]
fn test_25_1() {
gst::init().unwrap();
let twentyfive = gst::Fraction::new(25, 1);
let next = next_fraction(twentyfive);
//25.000000011641532
assert_eq!(next.numer(), 2147483626);
assert_eq!(next.denom(), 85899345);
let prev = previous_fraction(twentyfive);
//24.999999988358468
assert_eq!(prev.numer(), 2147483624);
assert_eq!(prev.denom(), 85899345);
}
#[test]
fn test_1_25() {
gst::init().unwrap();
let twentyfive = gst::Fraction::new(1, 25);
let next = next_fraction(twentyfive);
//0.040000000018626
assert_eq!(next.numer(), 85899345);
assert_eq!(next.denom(), 2147483624);
let prev = previous_fraction(twentyfive);
//0.039999999981374
assert_eq!(prev.numer(), 85899345);
assert_eq!(prev.denom(), 2147483626);
}
} }