From a5c799cf342440b00230d2c6def673de0e435eea Mon Sep 17 00:00:00 2001 From: Fabian Orccon Date: Sun, 4 Jun 2023 13:59:51 +0200 Subject: [PATCH] inspect: Print factory details of an element Example: $ gst-inspect-rs videotestsrc --- examples/Cargo.toml | 1 + examples/src/bin/gst-inspect.rs | 67 +++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index fd5eb5e86..3ed037340 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -35,6 +35,7 @@ derive_more = "0.99.5" futures = "0.3" byte-slice-cast = "1" cairo-rs = { git = "https://github.com/gtk-rs/gtk-rs-core", features=["use_glib"], optional = true } +clap = { version = "4.3.0", features = ["derive"] } pango = { git = "https://github.com/gtk-rs/gtk-rs-core", optional = true } pangocairo = { git = "https://github.com/gtk-rs/gtk-rs-core", optional = true } glutin = { version = "0.29", optional = true } diff --git a/examples/src/bin/gst-inspect.rs b/examples/src/bin/gst-inspect.rs index cf328b604..d981951d0 100644 --- a/examples/src/bin/gst-inspect.rs +++ b/examples/src/bin/gst-inspect.rs @@ -19,11 +19,14 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. use ansi_term::Color; +use clap::{Arg, Command}; use gst::prelude::*; const BRBLUE: Color = Color::RGB(97, 127, 166); const PLUGIN_NAME_COLOR: Color = BRBLUE; const ELEMENT_NAME_COLOR: Color = Color::Green; +const PROP_NAME_COLOR: Color = BRBLUE; +const HEADING_COLOR: Color = Color::Yellow; fn print_element_list() { let registry = gst::Registry::get(); @@ -47,8 +50,64 @@ fn print_element_list() { } } -fn main() { - gst::init().unwrap(); - - print_element_list(); +fn print_property(name: &str, value: &str) { + let formatted_name = format!("{:<25}", name); + println!(" {}{}", PROP_NAME_COLOR.paint(formatted_name), value); +} + +fn print_factory_details_info(factory: &gst::ElementFactory) { + let rank = factory.rank(); + println!("{}", HEADING_COLOR.paint("Factory details:")); + print_property("Rank", &format!("{} ({})", rank, i32::from(rank))); + print_property("Long name", factory.longname()); + print_property("Klass", factory.klass()); + print_property("Description", factory.description()); + print_property("Author", factory.author()); +} + +fn print_element_info(feature: &gst::PluginFeature) -> Result<(), String> { + let Ok(factory) = feature.load() else { + return Err(format!("element factory '{}' couldn't be loaded", feature.name())); + }; + let element_factory = factory.downcast_ref::().unwrap(); + let Ok(element) = element_factory.create_with_name(None) else { + return Err(format!( + "couldn't construct element '{}' for some reason", + feature.name() + )); + }; + + print_factory_details_info(element_factory); + + Ok(()) +} + +fn print_feature_info(feature_name: &str) -> Result<(), String> { + let registry = gst::Registry::get(); + + let feature = registry.find_feature(feature_name, gst::ElementFactory::static_type()); + let Some(feature) = feature else { + return Err(format!("No such element or plugin '{}'", feature_name)); + }; + + print_element_info(&feature) +} + +fn main() { + let matches = Command::new("prog") + .arg(Arg::new("ELEMENT-NAME | PLUGIN-NAME")) + .get_matches(); + let mut st: i32 = 0; + + gst::init().unwrap(); + if let Some(fname) = matches.get_one::("ELEMENT-NAME | PLUGIN-NAME") { + if let Err(e) = print_feature_info(fname) { + println!("{}", e); + st = -1; + } + } else { + print_element_list(); + } + + std::process::exit(st); }