From cd49659fae0e1af9c21a3a7d40f3b652ad91e64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 6 May 2022 16:40:01 +0300 Subject: [PATCH] gstreamer: Add a `PadTemplateBuilder` And only allow setting documentation caps via that. It's not actually allowed to update the documentation caps at random times as it is not thread-safe. --- gstreamer/Gir.toml | 5 ++ gstreamer/src/auto/pad_template.rs | 12 ----- gstreamer/src/lib.rs | 1 + gstreamer/src/pad_template.rs | 77 ++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 12 deletions(-) diff --git a/gstreamer/Gir.toml b/gstreamer/Gir.toml index bd10916e3..489e9e1e6 100644 --- a/gstreamer/Gir.toml +++ b/gstreamer/Gir.toml @@ -1801,6 +1801,11 @@ final_type = true # directly access the field manual = true + [[object.function]] + name = "set_documentation_caps" + # builder + manual = true + [[object.function]] name = "get_documentation_caps" # directly access the field diff --git a/gstreamer/src/auto/pad_template.rs b/gstreamer/src/auto/pad_template.rs index 9b6b781df..af74d6f55 100644 --- a/gstreamer/src/auto/pad_template.rs +++ b/gstreamer/src/auto/pad_template.rs @@ -74,18 +74,6 @@ impl PadTemplate { } } - #[cfg(any(feature = "v1_18", feature = "dox"))] - #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))] - #[doc(alias = "gst_pad_template_set_documentation_caps")] - pub fn set_documentation_caps(&self, caps: &Caps) { - unsafe { - ffi::gst_pad_template_set_documentation_caps( - self.to_glib_none().0, - caps.to_glib_full(), - ); - } - } - #[doc(alias = "pad-created")] pub fn connect_pad_created( &self, diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index 10bf2dd86..e85690fc6 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -240,6 +240,7 @@ mod buffer_pool; pub use crate::buffer_pool::{BufferPoolAcquireParams, BufferPoolConfig, BufferPoolConfigRef}; mod pad_template; +pub use pad_template::PadTemplateBuilder; pub mod param_spec; pub use crate::param_spec::ParamSpecArray; diff --git a/gstreamer/src/pad_template.rs b/gstreamer/src/pad_template.rs index 74b1e93e5..d6c74cc3a 100644 --- a/gstreamer/src/pad_template.rs +++ b/gstreamer/src/pad_template.rs @@ -79,4 +79,81 @@ impl PadTemplate { from_glib(templ.presence) } } + + pub fn builder<'a>( + name_template: &'a str, + direction: PadDirection, + presence: PadPresence, + caps: &'a Caps, + ) -> PadTemplateBuilder<'a> { + assert_initialized_main_thread!(); + + PadTemplateBuilder { + name_template, + direction, + presence, + caps, + gtype: None, + #[cfg(any(feature = "v1_18", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))] + documentation_caps: None, + } + } +} + +#[derive(Debug)] +pub struct PadTemplateBuilder<'a> { + name_template: &'a str, + direction: PadDirection, + presence: PadPresence, + caps: &'a Caps, + gtype: Option, + #[cfg(any(feature = "v1_18", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))] + documentation_caps: Option<&'a Caps>, +} + +impl<'a> PadTemplateBuilder<'a> { + pub fn gtype(self, gtype: glib::Type) -> Self { + PadTemplateBuilder { + gtype: Some(gtype), + ..self + } + } + + #[cfg(any(feature = "v1_18", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))] + pub fn documentation_caps(self, documentation_caps: &'a Caps) -> Self { + PadTemplateBuilder { + documentation_caps: Some(documentation_caps), + ..self + } + } + + pub fn build(self) -> Result { + let templ = if let Some(gtype) = self.gtype { + PadTemplate::with_gtype( + self.name_template, + self.direction, + self.presence, + self.caps, + gtype, + )? + } else { + PadTemplate::new(self.name_template, self.direction, self.presence, self.caps)? + }; + + #[cfg(any(feature = "v1_18", feature = "dox"))] + #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))] + if let Some(documentation_caps) = self.documentation_caps { + unsafe { + ffi::gst_pad_template_set_documentation_caps( + templ.to_glib_none().0, + documentation_caps.to_glib_none().0, + ); + } + } + + Ok(templ) + } }