Skip to main content

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::{Pipeline, PipelineFlags, ffi, prelude::*};
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 { self.delay(delay) } else { self }
135    }
136
137    pub fn delay_if_some(self, delay: Option<u64>) -> Self {
138        if let Some(delay) = delay {
139            self.delay(delay)
140        } else {
141            self
142        }
143    }
144
145    pub fn latency(self, latency: impl Into<Option<crate::ClockTime>>) -> Self {
146        if let Some(latency) = latency.into() {
147            Self {
148                builder: self.builder.property("latency", latency),
149            }
150        } else {
151            self
152        }
153    }
154
155    pub fn latency_if(self, latency: impl Into<Option<crate::ClockTime>>, predicate: bool) -> Self {
156        if predicate {
157            self.latency(latency)
158        } else {
159            self
160        }
161    }
162
163    pub fn latency_if_some(self, latency: Option<crate::ClockTime>) -> Self {
164        if let Some(latency) = latency {
165            self.latency(latency)
166        } else {
167            self
168        }
169    }
170
171    pub fn async_handling(self, async_handling: bool) -> Self {
172        Self {
173            builder: self.builder.property("async-handling", async_handling),
174        }
175    }
176
177    pub fn async_handling_if_some(self, async_handling: Option<bool>) -> Self {
178        if let Some(async_handling) = async_handling {
179            self.async_handling(async_handling)
180        } else {
181            self
182        }
183    }
184
185    pub fn message_forward(self, message_forward: bool) -> Self {
186        Self {
187            builder: self.builder.property("message-forward", message_forward),
188        }
189    }
190
191    pub fn message_forward_if_some(self, message_forward: Option<bool>) -> Self {
192        if let Some(message_forward) = message_forward {
193            self.message_forward(message_forward)
194        } else {
195            self
196        }
197    }
198
199    // rustdoc-stripper-ignore-next
200    /// Sets property `name` to the given value `value`.
201    ///
202    /// Overrides any default or previously defined value for `name`.
203    #[inline]
204    pub fn property(self, name: &'a str, value: impl Into<glib::Value> + 'a) -> Self {
205        Self {
206            builder: self.builder.property(name, value),
207        }
208    }
209
210    // rustdoc-stripper-ignore-next
211    /// Sets property `name` to the given string value `value`.
212    #[inline]
213    pub fn property_from_str(self, name: &'a str, value: &'a str) -> Self {
214        Self {
215            builder: self.builder.property_from_str(name, value),
216        }
217    }
218
219    impl_builder_gvalue_extra_setters!(property_and_name);
220}