From 4e9ee99c88e1bbe7dbcf69c041050610870e0525 Mon Sep 17 00:00:00 2001 From: Philippe Normand Date: Wed, 15 Jul 2020 19:24:18 +0100 Subject: [PATCH] functions: Add new parse_bin_from_description_with_name utility This is basically `parse_bin_from_description()` but additionally the returned bin has the passed name. It is sometimes convenient to name those bins so they can later be easily retrieved by name from the pipeline they belong to. --- gstreamer/src/functions.rs | 42 +++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/gstreamer/src/functions.rs b/gstreamer/src/functions.rs index 55b27a3ff..5b49de71c 100644 --- a/gstreamer/src/functions.rs +++ b/gstreamer/src/functions.rs @@ -6,15 +6,35 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use auto::functions::parse_bin_from_description; use glib; use glib::translate::*; +use glib::Cast; use gst_sys; use std::ptr; +use Bin; use Element; +use Object; use ParseContext; use ParseFlags; +pub fn parse_bin_from_description_with_name( + bin_description: &str, + ghost_unlinked_pads: bool, + bin_name: &str, +) -> Result { + assert_initialized_main_thread!(); + let bin = parse_bin_from_description(bin_description, ghost_unlinked_pads)?; + if !bin_name.is_empty() { + let obj = bin.clone().upcast::(); + unsafe { + gst_sys::gst_object_set_name(obj.to_glib_none().0, bin_name.to_glib_none().0); + } + } + Ok(bin) +} + pub fn parse_bin_from_description_full( bin_description: &str, ghost_unlinked_pads: bool, @@ -154,14 +174,12 @@ pub fn type_is_plugin_api(type_: glib::types::Type) -> Option<::PluginAPIFlags> #[cfg(test)] mod tests { + use super::*; + use prelude::*; + #[cfg(feature = "v1_12")] #[test] fn test_calculate_linear_regression() { - // Moved the module `use` inside test function because this is the only test for now - // and it is feature-gated, so it generates a warning when the feature is not selected. - // Can be moved out of the function when other tests are added. - use super::*; - ::init().unwrap(); let values = [(0, 0), (1, 1), (2, 2), (3, 3)]; @@ -174,4 +192,18 @@ mod tests { calculate_linear_regression(&values, Some(&mut temp)).unwrap(); assert_eq!((m_num, m_denom, b, xbase), (10, 10, 3, 3)); } + + #[test] + fn test_parse_bin_from_description_with_name() { + ::init().unwrap(); + + let bin = + parse_bin_from_description_with_name("fakesrc ! fakesink", false, "all_fake").unwrap(); + let name = bin.get_name(); + assert_eq!(name, "all_fake"); + + let bin = parse_bin_from_description_with_name("fakesrc ! fakesink", false, "").unwrap(); + let name = bin.get_name(); + assert_ne!(name, ""); + } }