gstreamer/
plugin_feature.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{
4    prelude::*,
5    translate::{from_glib, FromGlibPtrFull, IntoGlib, ToGlibPtr},
6};
7
8use crate::{ffi, PluginFeature, Rank};
9
10pub trait PluginFeatureExtManual: IsA<PluginFeature> + 'static {
11    /// Gets the rank of a plugin feature.
12    ///
13    /// # Returns
14    ///
15    /// The rank of the feature
16    #[doc(alias = "get_rank")]
17    #[doc(alias = "gst_plugin_feature_get_rank")]
18    fn rank(&self) -> Rank {
19        unsafe {
20            let rank = ffi::gst_plugin_feature_get_rank(self.as_ref().to_glib_none().0);
21            from_glib(rank as i32)
22        }
23    }
24
25    /// Specifies a rank for a plugin feature, so that autoplugging uses
26    /// the most appropriate feature.
27    /// ## `rank`
28    /// rank value - higher number means more priority rank
29    #[doc(alias = "gst_plugin_feature_set_rank")]
30    fn set_rank(&self, rank: Rank) {
31        unsafe {
32            ffi::gst_plugin_feature_set_rank(
33                self.as_ref().to_glib_none().0,
34                rank.into_glib() as u32,
35            );
36        }
37    }
38
39    /// Loads the plugin containing `self` if it's not already loaded. `self` is
40    /// unaffected; use the return value instead.
41    ///
42    /// Normally this function is used like this:
43    ///
44    ///
45    /// **⚠️ The following code is in C ⚠️**
46    ///
47    /// ```C
48    /// GstPluginFeature *loaded_feature;
49    ///
50    /// loaded_feature = gst_plugin_feature_load (feature);
51    /// // presumably, we're no longer interested in the potentially-unloaded feature
52    /// gst_object_unref (feature);
53    /// feature = loaded_feature;
54    /// ```
55    ///
56    /// # Returns
57    ///
58    /// a reference to the loaded
59    /// feature, or [`None`] on error
60    #[doc(alias = "gst_plugin_feature_load")]
61    fn load(&self) -> Result<Self, glib::BoolError> {
62        unsafe {
63            let loaded = Option::<PluginFeature>::from_glib_full(ffi::gst_plugin_feature_load(
64                self.as_ref().to_glib_none().0,
65            ))
66            .ok_or_else(|| glib::bool_error!("Failed to load plugin feature"))?;
67            Ok(loaded.unsafe_cast())
68        }
69    }
70}
71
72impl<O: IsA<PluginFeature>> PluginFeatureExtManual for O {}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_load() {
80        crate::init().unwrap();
81
82        let factory = crate::ElementFactory::find("identity").unwrap();
83        let loaded = factory.load().unwrap();
84        assert_eq!(factory.type_(), loaded.type_());
85        let _element = loaded.create().build().unwrap();
86    }
87}