1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::CStr;
4
5use glib::{prelude::*, translate::*};
6
7use crate::{Caps, PadDirection, PadPresence, PadTemplate, StaticPadTemplate};
8
9impl PadTemplate {
10 #[doc(alias = "gst_pad_template_new_from_static_pad_template_with_gtype")]
11 pub fn from_static_pad_template_with_gtype(
12 pad_template: &StaticPadTemplate,
13 pad_type: glib::types::Type,
14 ) -> Result<PadTemplate, glib::BoolError> {
15 skip_assert_initialized!();
16 unsafe {
17 Option::<_>::from_glib_none(
18 ffi::gst_pad_template_new_from_static_pad_template_with_gtype(
19 mut_override(pad_template.to_glib_none().0),
20 pad_type.into_glib(),
21 ),
22 )
23 .ok_or_else(|| glib::bool_error!("Failed to create PadTemplate"))
24 }
25 }
26
27 #[doc(alias = "gst_pad_template_get_caps")]
28 #[doc(alias = "get_caps")]
29 pub fn caps(&self) -> &Caps {
30 unsafe {
31 let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
32 &*(&templ.caps as *const *mut ffi::GstCaps as *const Caps)
33 }
34 }
35
36 #[cfg(feature = "v1_18")]
37 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
38 #[doc(alias = "gst_pad_template_get_documentation_caps")]
39 #[doc(alias = "get_documentation_caps")]
40 pub fn documentation_caps(&self) -> &Caps {
41 unsafe {
42 let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
43 if !templ.ABI.abi.documentation_caps.is_null() {
44 &*(&templ.ABI.abi.documentation_caps as *const *mut ffi::GstCaps as *const Caps)
45 } else {
46 &*(&templ.caps as *const *mut ffi::GstCaps as *const Caps)
47 }
48 }
49 }
50
51 pub fn direction(&self) -> PadDirection {
52 unsafe {
53 let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
54 from_glib(templ.direction)
55 }
56 }
57
58 pub fn gtype(&self) -> glib::types::Type {
59 unsafe {
60 let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
61 from_glib(templ.ABI.abi.gtype)
62 }
63 }
64
65 #[doc(alias = "name-template")]
66 pub fn name_template(&self) -> &str {
67 unsafe {
68 let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
69 CStr::from_ptr(templ.name_template).to_str().unwrap()
70 }
71 }
72
73 pub fn presence(&self) -> PadPresence {
74 unsafe {
75 let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
76 from_glib(templ.presence)
77 }
78 }
79
80 pub fn builder<'a>(
81 name_template: &'a str,
82 direction: PadDirection,
83 presence: PadPresence,
84 caps: &'a Caps,
85 ) -> PadTemplateBuilder<'a> {
86 skip_assert_initialized!();
87
88 PadTemplateBuilder {
89 name_template,
90 direction,
91 presence,
92 caps,
93 gtype: None,
94 #[cfg(feature = "v1_18")]
95 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
96 documentation_caps: None,
97 }
98 }
99}
100
101#[must_use = "The builder must be built to be used"]
102#[derive(Debug)]
103pub struct PadTemplateBuilder<'a> {
104 name_template: &'a str,
105 direction: PadDirection,
106 presence: PadPresence,
107 caps: &'a Caps,
108 gtype: Option<glib::Type>,
109 #[cfg(feature = "v1_18")]
110 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
111 documentation_caps: Option<&'a Caps>,
112}
113
114impl<'a> PadTemplateBuilder<'a> {
115 pub fn gtype(self, gtype: glib::Type) -> Self {
116 PadTemplateBuilder {
117 gtype: Some(gtype),
118 ..self
119 }
120 }
121
122 #[cfg(feature = "v1_18")]
123 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
124 pub fn documentation_caps(self, documentation_caps: &'a Caps) -> Self {
125 PadTemplateBuilder {
126 documentation_caps: Some(documentation_caps),
127 ..self
128 }
129 }
130
131 pub fn build(self) -> Result<PadTemplate, glib::BoolError> {
132 let templ = if let Some(gtype) = self.gtype {
133 PadTemplate::with_gtype(
134 self.name_template,
135 self.direction,
136 self.presence,
137 self.caps,
138 gtype,
139 )?
140 } else {
141 PadTemplate::new(self.name_template, self.direction, self.presence, self.caps)?
142 };
143
144 #[cfg(feature = "v1_18")]
145 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
146 if let Some(documentation_caps) = self.documentation_caps {
147 unsafe {
148 ffi::gst_pad_template_set_documentation_caps(
149 templ.to_glib_none().0,
150 documentation_caps.to_glib_none().0,
151 );
152 }
153 }
154
155 Ok(templ)
156 }
157}
158