gstreamer/
pipeline.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{prelude::*, translate::*};
4
5use crate::{ffi, prelude::*, Pipeline, PipelineFlags};
6
7impl Pipeline {
8    // rustdoc-stripper-ignore-next
9    /// Creates a new [`Pipeline`] object with a default name.
10    ///
11    /// Use [`Pipeline::with_name()`] to create a [`Pipeline`] with a specific name.
12    /// Use [`Pipeline::builder()`] to get a [`PipelineBuilder`] and then define a specific name.
13    // rustdoc-stripper-ignore-next-stop
14    /// Create a new pipeline with the given name.
15    /// ## `name`
16    /// name of new pipeline
17    ///
18    /// # Returns
19    ///
20    /// newly created GstPipeline
21    ///
22    /// MT safe.
23    #[doc(alias = "gst_pipeline_new")]
24    pub fn new() -> Pipeline {
25        assert_initialized_main_thread!();
26        unsafe {
27            crate::Element::from_glib_none(ffi::gst_pipeline_new(std::ptr::null())).unsafe_cast()
28        }
29    }
30
31    // rustdoc-stripper-ignore-next
32    /// Creates a new [`Pipeline`] object with the specified name.
33    ///
34    /// Use [`Pipeline::builder()`] for additional configuration.
35    #[doc(alias = "gst_pipeline_new")]
36    pub fn with_name(name: &str) -> Pipeline {
37        assert_initialized_main_thread!();
38        unsafe {
39            crate::Element::from_glib_none(ffi::gst_pipeline_new(name.to_glib_none().0))
40                .unsafe_cast()
41        }
42    }
43
44    // rustdoc-stripper-ignore-next
45    /// Creates a new builder-pattern struct instance to construct [`Pipeline`] objects.
46    ///
47    /// This method returns an instance of [`PipelineBuilder`] which can be used to create [`Pipeline`] objects.
48    pub fn builder() -> PipelineBuilder {
49        PipelineBuilder::new()
50    }
51}
52
53mod sealed {
54    pub trait Sealed {}
55    impl<T: super::IsA<super::Pipeline>> Sealed for T {}
56}
57
58pub trait GstPipelineExtManual: sealed::Sealed + IsA<Pipeline> + 'static {
59    fn set_pipeline_flags(&self, flags: PipelineFlags) {
60        unsafe {
61            let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
62            let _guard = self.as_ref().object_lock();
63            (*ptr).flags |= flags.into_glib();
64        }
65    }
66
67    fn unset_pipeline_flags(&self, flags: PipelineFlags) {
68        unsafe {
69            let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
70            let _guard = self.as_ref().object_lock();
71            (*ptr).flags &= !flags.into_glib();
72        }
73    }
74
75    #[doc(alias = "get_pipeline_flags")]
76    fn pipeline_flags(&self) -> PipelineFlags {
77        unsafe {
78            let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
79            let _guard = self.as_ref().object_lock();
80            from_glib((*ptr).flags)
81        }
82    }
83}
84
85impl<O: IsA<Pipeline>> GstPipelineExtManual for O {}
86
87impl Default for Pipeline {
88    fn default() -> Self {
89        glib::object::Object::new()
90    }
91}
92
93// rustdoc-stripper-ignore-next
94/// A [builder-pattern] type to construct [`Pipeline`] objects.
95///
96/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
97#[must_use = "The builder must be built to be used"]
98pub struct PipelineBuilder {
99    builder: glib::object::ObjectBuilder<'static, Pipeline>,
100}
101
102impl PipelineBuilder {
103    fn new() -> Self {
104        Self {
105            builder: glib::Object::builder(),
106        }
107    }
108
109    // rustdoc-stripper-ignore-next
110    /// Build the [`Pipeline`].
111    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
112    pub fn build(self) -> Pipeline {
113        self.builder.build()
114    }
115
116    pub fn auto_flush_bus(self, auto_flush_bus: bool) -> Self {
117        Self {
118            builder: self.builder.property("auto-flush-bus", auto_flush_bus),
119        }
120    }
121
122    pub fn auto_flush_bus_if_some(self, auto_flush_bus: Option<bool>) -> Self {
123        if let Some(auto_flush_bus) = auto_flush_bus {
124            self.auto_flush_bus(auto_flush_bus)
125        } else {
126            self
127        }
128    }
129
130    pub fn delay(self, delay: u64) -> Self {
131        Self {
132            builder: self.builder.property("delay", delay),
133        }
134    }
135
136    pub fn delay_if(self, delay: u64, predicate: bool) -> Self {
137        if predicate {
138            self.delay(delay)
139        } else {
140            self
141        }
142    }
143
144    pub fn delay_if_some(self, delay: Option<u64>) -> Self {
145        if let Some(delay) = delay {
146            self.delay(delay)
147        } else {
148            self
149        }
150    }
151
152    pub fn latency(self, latency: impl Into<Option<crate::ClockTime>>) -> Self {
153        if let Some(latency) = latency.into() {
154            Self {
155                builder: self.builder.property("latency", latency),
156            }
157        } else {
158            self
159        }
160    }
161
162    pub fn latency_if(self, latency: impl Into<Option<crate::ClockTime>>, predicate: bool) -> Self {
163        if predicate {
164            self.latency(latency)
165        } else {
166            self
167        }
168    }
169
170    pub fn latency_if_some(self, latency: Option<crate::ClockTime>) -> Self {
171        if let Some(latency) = latency {
172            self.latency(latency)
173        } else {
174            self
175        }
176    }
177
178    pub fn async_handling(self, async_handling: bool) -> Self {
179        Self {
180            builder: self.builder.property("async-handling", async_handling),
181        }
182    }
183
184    pub fn async_handling_if_some(self, async_handling: Option<bool>) -> Self {
185        if let Some(async_handling) = async_handling {
186            self.async_handling(async_handling)
187        } else {
188            self
189        }
190    }
191
192    pub fn message_forward(self, message_forward: bool) -> Self {
193        Self {
194            builder: self.builder.property("message-forward", message_forward),
195        }
196    }
197
198    pub fn message_forward_if_some(self, message_forward: Option<bool>) -> Self {
199        if let Some(message_forward) = message_forward {
200            self.message_forward(message_forward)
201        } else {
202            self
203        }
204    }
205
206    pub fn name(self, name: impl Into<glib::GString>) -> Self {
207        Self {
208            builder: self.builder.property("name", name.into()),
209        }
210    }
211
212    pub fn name_if(self, name: impl Into<glib::GString>, predicate: bool) -> Self {
213        if predicate {
214            self.name(name)
215        } else {
216            self
217        }
218    }
219
220    pub fn name_if_some(self, name: Option<impl Into<glib::GString>>) -> Self {
221        if let Some(name) = name {
222            self.name(name)
223        } else {
224            self
225        }
226    }
227}