From b4a3738b82d79706c26defaeb1e6c5cab7fffc0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 24 Oct 2021 18:57:15 +0300 Subject: [PATCH] Move more code to caps/structure builders Instead of the new() functions requiring slices. The code reads cleaner. --- examples/src/bin/appsink.rs | 17 +++++----- examples/src/bin/rtpfecclient.rs | 40 +++++++++++------------ examples/src/bin/rtpfecserver.rs | 2 +- examples/src/bin/subclass.rs | 15 ++++----- gstreamer-audio/src/audio_info.rs | 17 ++++------ gstreamer-pbutils/src/encoding_profile.rs | 26 ++++++++++----- gstreamer-video/src/video_info.rs | 23 ++++++------- gstreamer/src/buffer_pool.rs | 2 +- gstreamer/src/caps.rs | 2 +- gstreamer/src/promise.rs | 2 +- gstreamer/src/structure_serde.rs | 29 ++++++++-------- gstreamer/src/typefind.rs | 8 ++--- 12 files changed, 88 insertions(+), 95 deletions(-) diff --git a/examples/src/bin/appsink.rs b/examples/src/bin/appsink.rs index a425a853f..3ceb8ce46 100644 --- a/examples/src/bin/appsink.rs +++ b/examples/src/bin/appsink.rs @@ -56,15 +56,14 @@ fn create_pipeline() -> Result { // provide the format we request. // This can be set after linking the two objects, because format negotiation between // both elements will happen during pre-rolling of the pipeline. - appsink.set_caps(Some(&gst::Caps::new_simple( - "audio/x-raw", - &[ - ("format", &gst_audio::AUDIO_FORMAT_S16.to_str()), - ("layout", &"interleaved"), - ("channels", &(1i32)), - ("rate", &gst::IntRange::::new(1, i32::MAX)), - ], - ))); + appsink.set_caps(Some( + &gst::Caps::builder("audio/x-raw") + .field("format", gst_audio::AUDIO_FORMAT_S16.to_str()) + .field("layout", "interleaved") + .field("channels", (1i32)) + .field("rate", gst::IntRange::::new(1, i32::MAX)) + .build(), + )); // Getting data out of the appsink is done by setting callbacks on it. // The appsink will then call those handlers, as soon as data is available. diff --git a/examples/src/bin/rtpfecclient.rs b/examples/src/bin/rtpfecclient.rs index 78455f5a4..6a6476c3c 100644 --- a/examples/src/bin/rtpfecclient.rs +++ b/examples/src/bin/rtpfecclient.rs @@ -158,26 +158,20 @@ fn example_main() -> Result<(), Error> { .expect("rtpbin \"new-storage\" signal values[2]"); match pt { 100 => Some( - gst::Caps::new_simple( - "application/x-rtp", - &[ - ("media", &"video"), - ("clock-rate", &90000i32), - ("is-fec", &true), - ], - ) - .to_value(), + gst::Caps::builder("application/x-rtp") + .field("media", "video") + .field("clock-rate", 90000i32) + .field("is-fec", true) + .build() + .to_value(), ), 96 => Some( - gst::Caps::new_simple( - "application/x-rtp", - &[ - ("media", &"video"), - ("clock-rate", &90000i32), - ("encoding-name", &"VP8"), - ], - ) - .to_value(), + gst::Caps::builder("application/x-rtp") + .field("media", "video") + .field("clock-rate", 90000i32) + .field("encoding-name", "VP8") + .build() + .to_value(), ), _ => None, } @@ -229,10 +223,14 @@ fn example_main() -> Result<(), Error> { } }); - let rtp_caps = gst::Caps::new_simple("application/x-rtp", &[("clock-rate", &90000i32)]); + let rtp_caps = gst::Caps::builder("application/x-rtp") + .field("clock-rate", 90000i32) + .build(); - let video_caps = - gst::Caps::new_simple("video/x-raw", &[("width", &1920i32), ("height", &1080i32)]); + let video_caps = gst::Caps::builder("video/x-raw") + .field("width", 1920i32) + .field("height", 1080i32) + .build(); src.set_property("address", &"127.0.0.1")?; src.set_property("caps", &rtp_caps)?; diff --git a/examples/src/bin/rtpfecserver.rs b/examples/src/bin/rtpfecserver.rs index fecd4ed86..82c9d94d4 100644 --- a/examples/src/bin/rtpfecserver.rs +++ b/examples/src/bin/rtpfecserver.rs @@ -149,7 +149,7 @@ fn example_main() -> Result<(), Error> { }, ); - let video_caps = gst::Caps::new_simple("video/x-raw", &[]); + let video_caps = gst::Caps::builder("video/x-raw").build(); src.set_property_from_str("pattern", "ball")?; sink.set_property("host", &"127.0.0.1")?; diff --git a/examples/src/bin/subclass.rs b/examples/src/bin/subclass.rs index 4481c809d..c3c86ba60 100644 --- a/examples/src/bin/subclass.rs +++ b/examples/src/bin/subclass.rs @@ -88,15 +88,12 @@ mod fir_filter { // GStreamer about all possible pads that could exist for this type. // On both of pads we can only handle F32 mono at any sample rate. - let caps = gst::Caps::new_simple( - "audio/x-raw", - &[ - ("format", &gst_audio::AUDIO_FORMAT_F32.to_str()), - ("rate", &gst::IntRange::::new(1, i32::MAX)), - ("channels", &1i32), - ("layout", &"interleaved"), - ], - ); + let caps = gst::Caps::builder("audio/x-raw") + .field("format", gst_audio::AUDIO_FORMAT_F32.to_str()) + .field("rate", gst::IntRange::::new(1, i32::MAX)) + .field("channels", 1i32) + .field("layout", "interleaved") + .build(); vec![ // The src pad template must be named "src" for basetransform diff --git a/gstreamer-audio/src/audio_info.rs b/gstreamer-audio/src/audio_info.rs index c904a5dd2..f480096ea 100644 --- a/gstreamer-audio/src/audio_info.rs +++ b/gstreamer-audio/src/audio_info.rs @@ -470,16 +470,13 @@ mod tests { fn test_from_to_caps() { gst::init().unwrap(); - let caps = gst::Caps::new_simple( - "audio/x-raw", - &[ - ("format", &"S16LE"), - ("rate", &48000), - ("channels", &2), - ("layout", &"interleaved"), - ("channel-mask", &gst::Bitmask::new(0x3)), - ], - ); + let caps = gst::Caps::builder("audio/x-raw") + .field("format", "S16LE") + .field("rate", 48000) + .field("channels", 2) + .field("layout", "interleaved") + .field("channel-mask", gst::Bitmask::new(0x3)) + .build(); let info = AudioInfo::from_caps(&caps).unwrap(); assert_eq!(info.format(), crate::AudioFormat::S16le); assert_eq!(info.rate(), 48000); diff --git a/gstreamer-pbutils/src/encoding_profile.rs b/gstreamer-pbutils/src/encoding_profile.rs index 82a69b7b6..72f2e29da 100644 --- a/gstreamer-pbutils/src/encoding_profile.rs +++ b/gstreamer-pbutils/src/encoding_profile.rs @@ -538,9 +538,11 @@ mod tests { fn test_encoding_audio_profile_builder() { gst::init().unwrap(); - let caps = gst::Caps::new_simple("audio/x-raw", &[]); + let caps = gst::Caps::builder("audio/x-raw").build(); - let restriction = gst::Caps::new_simple("audio/x-raw", &[("format", &"S32LE")]); + let restriction = gst::Caps::builder("audio/x-raw") + .field("format", "S32LE") + .build(); let audio_profile = EncodingAudioProfile::builder(&caps) .name(AUDIO_PROFILE_NAME) @@ -566,7 +568,9 @@ mod tests { assert_eq!(audio_profile.allows_dynamic_output(), ALLOW_DYNAMIC_OUTPUT); assert_eq!(audio_profile.is_enabled(), ENABLED); - let restriction = gst::Caps::new_simple("audio/x-raw", &[("format", &"S32BE")]); + let restriction = gst::Caps::builder("audio/x-raw") + .field("format", "S32BE") + .build(); audio_profile.set_restriction(Some(&restriction)); assert_eq!(audio_profile.restriction().unwrap(), restriction); } @@ -575,9 +579,11 @@ mod tests { fn test_encoding_video_profile_builder() { gst::init().unwrap(); - let caps = gst::Caps::new_simple("video/x-raw", &[]); + let caps = gst::Caps::builder("video/x-raw").build(); - let restriction = gst::Caps::new_simple("video/x-raw", &[("format", &"RGBA")]); + let restriction = gst::Caps::builder("video/x-raw") + .field("format", "RGBA") + .build(); let video_profile = EncodingVideoProfile::builder(&caps) .name(VIDEO_PROFILE_NAME) @@ -610,7 +616,9 @@ mod tests { assert_eq!(video_profile.is_variableframerate(), VARIABLE_FRAMERATE); assert_eq!(video_profile.pass(), PASS); - let restriction = gst::Caps::new_simple("video/x-raw", &[("format", &"NV12")]); + let restriction = gst::Caps::builder("video/x-raw") + .field("format", "NV12") + .build(); video_profile.set_restriction(Some(&restriction)); assert_eq!(video_profile.restriction().unwrap(), restriction); } @@ -619,9 +627,9 @@ mod tests { fn test_encoding_container_profile_builder() { gst::init().unwrap(); - let container_caps = gst::Caps::new_simple("container/x-caps", &[]); - let video_caps = gst::Caps::new_simple("video/x-raw", &[]); - let audio_caps = gst::Caps::new_simple("audio/x-raw", &[]); + let container_caps = gst::Caps::builder("container/x-caps").build(); + let video_caps = gst::Caps::builder("video/x-raw").build(); + let audio_caps = gst::Caps::builder("audio/x-raw").build(); let video_profile = EncodingVideoProfile::builder(&video_caps) .name(VIDEO_PROFILE_NAME) diff --git a/gstreamer-video/src/video_info.rs b/gstreamer-video/src/video_info.rs index 5cbe52fc6..28339736e 100644 --- a/gstreamer-video/src/video_info.rs +++ b/gstreamer-video/src/video_info.rs @@ -1058,19 +1058,16 @@ mod tests { fn test_from_to_caps() { gst::init().unwrap(); - let caps = gst::Caps::new_simple( - "video/x-raw", - &[ - ("format", &"I420"), - ("width", &320), - ("height", &240), - ("framerate", &gst::Fraction::new(30, 1)), - ("pixel-aspect-ratio", &gst::Fraction::new(1, 1)), - ("interlace-mode", &"progressive"), - ("chroma-site", &"mpeg2"), - ("colorimetry", &"bt709"), - ], - ); + let caps = gst::Caps::builder("video/x-raw") + .field("format", "I420") + .field("width", 320) + .field("height", 240) + .field("framerate", gst::Fraction::new(30, 1)) + .field("pixel-aspect-ratio", gst::Fraction::new(1, 1)) + .field("interlace-mode", "progressive") + .field("chroma-site", "mpeg2") + .field("colorimetry", "bt709") + .build(); let info = VideoInfo::from_caps(&caps).unwrap(); assert_eq!(info.format(), crate::VideoFormat::I420); assert_eq!(info.width(), 320); diff --git a/gstreamer/src/buffer_pool.rs b/gstreamer/src/buffer_pool.rs index b9a30a328..230c141c7 100644 --- a/gstreamer/src/buffer_pool.rs +++ b/gstreamer/src/buffer_pool.rs @@ -373,7 +373,7 @@ mod tests { let pool = crate::BufferPool::new(); let mut config = pool.config(); - config.set_params(Some(&crate::Caps::new_simple("foo/bar", &[])), 1024, 0, 2); + config.set_params(Some(&crate::Caps::builder("foo/bar").build()), 1024, 0, 2); pool.set_config(config).unwrap(); pool.set_active(true).unwrap(); diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index 9fb3a109e..cdebe9df4 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -901,7 +901,7 @@ mod tests { fn test_display() { crate::init().unwrap(); - let caps = Caps::new_simple("foo/bar", &[]); + let caps = Caps::builder("foo/bar").build(); format!("{}", caps); } diff --git a/gstreamer/src/promise.rs b/gstreamer/src/promise.rs index c140565d4..b05fe3bf5 100644 --- a/gstreamer/src/promise.rs +++ b/gstreamer/src/promise.rs @@ -206,7 +206,7 @@ mod tests { }); thread::spawn(move || { - promise.reply(Some(crate::Structure::new("foo/bar", &[]))); + promise.reply(Some(crate::Structure::new_empty("foo/bar"))); }); let res = receiver.recv().unwrap(); diff --git a/gstreamer/src/structure_serde.rs b/gstreamer/src/structure_serde.rs index 092ba2e8d..62fcca5b2 100644 --- a/gstreamer/src/structure_serde.rs +++ b/gstreamer/src/structure_serde.rs @@ -231,22 +231,19 @@ mod tests { let s: Structure = ron::de::from_str(s_ron).unwrap(); assert_eq!( s.as_ref(), - Structure::new( - "test", - &[ - ("f1", &"abc"), - ("f2", &"bcd"), - ("f3", &123), - ("date", &Date::new_dmy(19, DateMonth::August, 2019).unwrap()), - ( - "date_time", - &DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap() - ), - ("fraction", &Fraction::new(1, 2)), - ("array", &Array::new(&[&1, &2])), - ], - ) - .as_ref() + Structure::builder("test") + .field("f1", "abc") + .field("f2", "bcd") + .field("f3", 123) + .field("date", Date::new_dmy(19, DateMonth::August, 2019).unwrap()) + .field( + "date_time", + DateTime::new(2f32, 2019, 8, 19, 13, 34, 42f64).unwrap() + ) + .field("fraction", Fraction::new(1, 2)) + .field("array", Array::new(&[&1, &2])) + .build() + .as_ref() ); } diff --git a/gstreamer/src/typefind.rs b/gstreamer/src/typefind.rs index d46fe5716..da7ffab83 100644 --- a/gstreamer/src/typefind.rs +++ b/gstreamer/src/typefind.rs @@ -268,7 +268,7 @@ mod tests { assert_eq!( typefind.caps, - Some(Caps::new_simple("application/xml", &[])) + Some(Caps::builder("application/xml").build()) ); assert_eq!(typefind.probability, Some(TypeFindProbability::Minimum)); } @@ -282,7 +282,7 @@ mod tests { "test_typefind", crate::Rank::Primary, None, - Some(&Caps::new_simple("test/test", &[])), + Some(&Caps::builder("test/test").build()), |typefind| { assert_eq!(typefind.length(), Some(8)); let mut found = false; @@ -295,7 +295,7 @@ mod tests { if found { typefind.suggest( TypeFindProbability::Likely, - &Caps::new_simple("test/test", &[]), + &Caps::builder("test/test").build(), ); } }, @@ -306,7 +306,7 @@ mod tests { let data = &data[..]; let (probability, caps) = SliceTypeFind::type_find(&data); - assert_eq!(caps, Some(Caps::new_simple("test/test", &[]))); + assert_eq!(caps, Some(Caps::builder("test/test").build())); assert_eq!(probability, TypeFindProbability::Likely); } }