{Audio,Video}CapsBuilder: add for_encoding constructor

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1225>
This commit is contained in:
François Laignel 2023-02-20 09:33:01 +01:00 committed by Sebastian Dröge
parent eeefa80227
commit 2f41cb99d3
2 changed files with 133 additions and 36 deletions

View file

@ -11,7 +11,19 @@ pub struct AudioCapsBuilder<T> {
}
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 {
assert_initialized_main_thread!();
let builder = Caps::builder(glib::gstr!("audio/x-raw"));
let builder = AudioCapsBuilder { builder };
builder
@ -21,10 +33,34 @@ impl AudioCapsBuilder<gst::caps::NoFeature> {
.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 {
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> {
AudioCapsBuilder {
builder: self.builder.any_features(),
@ -180,3 +216,22 @@ fn layout_str(layout: AudioLayout) -> &'static glib::GStr {
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> {
// 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 {
assert_initialized_main_thread!();
let builder = Caps::builder(glib::gstr!("video/x-raw"));
let builder = VideoCapsBuilder { builder };
builder
@ -20,6 +31,18 @@ impl VideoCapsBuilder<gst::caps::NoFeature> {
.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> {
VideoCapsBuilder {
builder: self.builder.any_features(),
@ -283,41 +306,60 @@ fn next_fraction(fraction: gst::Fraction) -> gst::Fraction {
gst::Fraction::new(new_num, new_den)
}
#[test]
fn test_0_1_fraction() {
gst::init().unwrap();
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);
}
#[cfg(test)]
mod tests {
use super::{next_fraction, previous_fraction, VideoCapsBuilder};
#[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);
#[test]
fn default_encoding() {
gst::init().unwrap();
let caps = VideoCapsBuilder::new().build();
assert_eq!(caps.structure(0).unwrap().name(), "video/x-raw");
}
#[test]
fn explicit_encoding() {
gst::init().unwrap();
let caps = VideoCapsBuilder::for_encoding("video/mpeg").build();
assert_eq!(caps.structure(0).unwrap().name(), "video/mpeg");
}
#[test]
fn test_0_1_fraction() {
gst::init().unwrap();
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]
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);
}
}