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<'a>() -> PipelineBuilder<'a> {
49        assert_initialized_main_thread!();
50        PipelineBuilder {
51            builder: crate::Object::builder(),
52        }
53    }
54}
55
56pub trait GstPipelineExtManual: IsA<Pipeline> + 'static {
57    fn set_pipeline_flags(&self, flags: PipelineFlags) {
58        unsafe {
59            let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
60            let _guard = self.as_ref().object_lock();
61            (*ptr).flags |= flags.into_glib();
62        }
63    }
64
65    fn unset_pipeline_flags(&self, flags: PipelineFlags) {
66        unsafe {
67            let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
68            let _guard = self.as_ref().object_lock();
69            (*ptr).flags &= !flags.into_glib();
70        }
71    }
72
73    #[doc(alias = "get_pipeline_flags")]
74    fn pipeline_flags(&self) -> PipelineFlags {
75        unsafe {
76            let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
77            let _guard = self.as_ref().object_lock();
78            from_glib((*ptr).flags)
79        }
80    }
81}
82
83impl<O: IsA<Pipeline>> GstPipelineExtManual for O {}
84
85impl Default for Pipeline {
86    fn default() -> Self {
87        glib::object::Object::new()
88    }
89}
90
91// rustdoc-stripper-ignore-next
92/// A [builder-pattern] type to construct [`Pipeline`] objects.
93///
94/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
95#[must_use = "The builder must be built to be used"]
96pub struct PipelineBuilder<'a> {
97    builder: crate::gobject::GObjectBuilder<'a, Pipeline>,
98}
99
100impl<'a> PipelineBuilder<'a> {
101    // rustdoc-stripper-ignore-next
102    /// Build the [`Pipeline`].
103    ///
104    /// # Panics
105    ///
106    /// This panics if the [`Pipeline`] doesn't have all the given properties or
107    /// property values of the wrong type are provided.
108    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
109    pub fn build(self) -> Pipeline {
110        self.builder.build().unwrap()
111    }
112
113    pub fn auto_flush_bus(self, auto_flush_bus: bool) -> Self {
114        Self {
115            builder: self.builder.property("auto-flush-bus", auto_flush_bus),
116        }
117    }
118
119    pub fn auto_flush_bus_if_some(self, auto_flush_bus: Option<bool>) -> Self {
120        if let Some(auto_flush_bus) = auto_flush_bus {
121            self.auto_flush_bus(auto_flush_bus)
122        } else {
123            self
124        }
125    }
126
127    pub fn delay(self, delay: u64) -> Self {
128        Self {
129            builder: self.builder.property("delay", delay),
130        }
131    }
132
133    pub fn delay_if(self, delay: u64, predicate: bool) -> Self {
134        if predicate {
135            self.delay(delay)
136        } else {
137            self
138        }
139    }
140
141    pub fn delay_if_some(self, delay: Option<u64>) -> Self {
142        if let Some(delay) = delay {
143            self.delay(delay)
144        } else {
145            self
146        }
147    }
148
149    pub fn latency(self, latency: impl Into<Option<crate::ClockTime>>) -> Self {
150        if let Some(latency) = latency.into() {
151            Self {
152                builder: self.builder.property("latency", latency),
153            }
154        } else {
155            self
156        }
157    }
158
159    pub fn latency_if(self, latency: impl Into<Option<crate::ClockTime>>, predicate: bool) -> Self {
160        if predicate {
161            self.latency(latency)
162        } else {
163            self
164        }
165    }
166
167    pub fn latency_if_some(self, latency: Option<crate::ClockTime>) -> Self {
168        if let Some(latency) = latency {
169            self.latency(latency)
170        } else {
171            self
172        }
173    }
174
175    pub fn async_handling(self, async_handling: bool) -> Self {
176        Self {
177            builder: self.builder.property("async-handling", async_handling),
178        }
179    }
180
181    pub fn async_handling_if_some(self, async_handling: Option<bool>) -> Self {
182        if let Some(async_handling) = async_handling {
183            self.async_handling(async_handling)
184        } else {
185            self
186        }
187    }
188
189    pub fn message_forward(self, message_forward: bool) -> Self {
190        Self {
191            builder: self.builder.property("message-forward", message_forward),
192        }
193    }
194
195    pub fn message_forward_if_some(self, message_forward: Option<bool>) -> Self {
196        if let Some(message_forward) = message_forward {
197            self.message_forward(message_forward)
198        } else {
199            self
200        }
201    }
202
203    // rustdoc-stripper-ignore-next
204    /// Sets property `name` to the given value `value`.
205    ///
206    /// Overrides any default or previously defined value for `name`.
207    #[inline]
208    pub fn property(self, name: &'a str, value: impl Into<glib::Value> + 'a) -> Self {
209        Self {
210            builder: self.builder.property(name, value),
211        }
212    }
213
214    // rustdoc-stripper-ignore-next
215    /// Sets property `name` to the given string value `value`.
216    #[inline]
217    pub fn property_from_str(self, name: &'a str, value: &'a str) -> Self {
218        Self {
219            builder: self.builder.property_from_str(name, value),
220        }
221    }
222
223    impl_builder_gvalue_extra_setters!(property_and_name);
224}