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