gstreamer_base/flow_combiner.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4use glib::{prelude::*, translate::*};
5
6glib::wrapper! {
7 /// s.
8 ///
9 /// Aside from reducing the user's code size, the main advantage of using this
10 /// helper struct is to follow the standard rules for [`gst::FlowReturn`][crate::gst::FlowReturn] combination.
11 /// These rules are:
12 ///
13 /// * [`gst::FlowReturn::Eos`][crate::gst::FlowReturn::Eos]: only if all returns are EOS too
14 /// * [`gst::FlowReturn::NotLinked`][crate::gst::FlowReturn::NotLinked]: only if all returns are NOT_LINKED too
15 /// * [`gst::FlowReturn::Error`][crate::gst::FlowReturn::Error] or below: if at least one returns an error return
16 /// * [`gst::FlowReturn::NotNegotiated`][crate::gst::FlowReturn::NotNegotiated]: if at least one returns a not-negotiated return
17 /// * [`gst::FlowReturn::Flushing`][crate::gst::FlowReturn::Flushing]: if at least one returns flushing
18 /// * [`gst::FlowReturn::Ok`][crate::gst::FlowReturn::Ok]: otherwise
19 ///
20 /// [`gst::FlowReturn::Error`][crate::gst::FlowReturn::Error] or below, GST_FLOW_NOT_NEGOTIATED and GST_FLOW_FLUSHING are
21 /// returned immediately from the [`update_flow()`][Self::update_flow()] function.
22 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
23 #[doc(alias = "GstFlowCombiner")]
24 pub struct FlowCombiner(Shared<ffi::GstFlowCombiner>);
25
26 match fn {
27 ref => |ptr| ffi::gst_flow_combiner_ref(ptr),
28 unref => |ptr| ffi::gst_flow_combiner_unref(ptr),
29 type_ => || ffi::gst_flow_combiner_get_type(),
30 }
31}
32
33impl FlowCombiner {
34 /// Creates a new [`FlowCombiner`][crate::FlowCombiner], use `gst_flow_combiner_free()` to free it.
35 ///
36 /// # Returns
37 ///
38 /// A new [`FlowCombiner`][crate::FlowCombiner]
39 #[doc(alias = "gst_flow_combiner_new")]
40 pub fn new() -> Self {
41 assert_initialized_main_thread!();
42 unsafe { from_glib_full(ffi::gst_flow_combiner_new()) }
43 }
44
45 /// Adds a new [`gst::Pad`][crate::gst::Pad] to the [`FlowCombiner`][crate::FlowCombiner].
46 /// ## `pad`
47 /// the [`gst::Pad`][crate::gst::Pad] that is being added
48 #[doc(alias = "gst_flow_combiner_add_pad")]
49 pub fn add_pad<P: IsA<gst::Pad>>(&self, pad: &P) {
50 unsafe {
51 ffi::gst_flow_combiner_add_pad(self.to_glib_none().0, pad.as_ref().to_glib_none().0);
52 }
53 }
54
55 /// Removes all pads from a [`FlowCombiner`][crate::FlowCombiner] and resets it to its initial state.
56 #[doc(alias = "gst_flow_combiner_clear")]
57 pub fn clear(&self) {
58 unsafe {
59 ffi::gst_flow_combiner_clear(self.to_glib_none().0);
60 }
61 }
62
63 /// Removes a [`gst::Pad`][crate::gst::Pad] from the [`FlowCombiner`][crate::FlowCombiner].
64 /// ## `pad`
65 /// the [`gst::Pad`][crate::gst::Pad] to remove
66 #[doc(alias = "gst_flow_combiner_remove_pad")]
67 pub fn remove_pad<P: IsA<gst::Pad>>(&self, pad: &P) {
68 unsafe {
69 ffi::gst_flow_combiner_remove_pad(self.to_glib_none().0, pad.as_ref().to_glib_none().0);
70 }
71 }
72
73 /// Reset flow combiner and all pads to their initial state without removing pads.
74 #[doc(alias = "gst_flow_combiner_reset")]
75 pub fn reset(&self) {
76 unsafe {
77 ffi::gst_flow_combiner_reset(self.to_glib_none().0);
78 }
79 }
80
81 /// Computes the combined flow return for the pads in it.
82 ///
83 /// The [`gst::FlowReturn`][crate::gst::FlowReturn] parameter should be the last flow return update for a pad
84 /// in this [`FlowCombiner`][crate::FlowCombiner]. It will use this value to be able to shortcut some
85 /// combinations and avoid looking over all pads again. e.g. The last combined
86 /// return is the same as the latest obtained [`gst::FlowReturn`][crate::gst::FlowReturn].
87 /// ## `fret`
88 /// the latest [`gst::FlowReturn`][crate::gst::FlowReturn] received for a pad in this [`FlowCombiner`][crate::FlowCombiner]
89 ///
90 /// # Returns
91 ///
92 /// The combined [`gst::FlowReturn`][crate::gst::FlowReturn]
93 #[doc(alias = "gst_flow_combiner_update_flow")]
94 pub fn update_flow<FRet: Into<gst::FlowReturn>>(
95 &self,
96 fret: FRet,
97 ) -> Result<gst::FlowSuccess, gst::FlowError> {
98 let fret: gst::FlowReturn = fret.into();
99 unsafe {
100 try_from_glib(ffi::gst_flow_combiner_update_flow(
101 self.to_glib_none().0,
102 fret.into_glib(),
103 ))
104 }
105 }
106
107 /// Sets the provided pad's last flow return to provided value and computes
108 /// the combined flow return for the pads in it.
109 ///
110 /// The [`gst::FlowReturn`][crate::gst::FlowReturn] parameter should be the last flow return update for a pad
111 /// in this [`FlowCombiner`][crate::FlowCombiner]. It will use this value to be able to shortcut some
112 /// combinations and avoid looking over all pads again. e.g. The last combined
113 /// return is the same as the latest obtained [`gst::FlowReturn`][crate::gst::FlowReturn].
114 /// ## `pad`
115 /// the [`gst::Pad`][crate::gst::Pad] whose [`gst::FlowReturn`][crate::gst::FlowReturn] to update
116 /// ## `fret`
117 /// the latest [`gst::FlowReturn`][crate::gst::FlowReturn] received for a pad in this [`FlowCombiner`][crate::FlowCombiner]
118 ///
119 /// # Returns
120 ///
121 /// The combined [`gst::FlowReturn`][crate::gst::FlowReturn]
122 #[doc(alias = "gst_flow_combiner_update_pad_flow")]
123 pub fn update_pad_flow<P: IsA<gst::Pad>, FRet: Into<gst::FlowReturn>>(
124 &self,
125 pad: &P,
126 fret: FRet,
127 ) -> Result<gst::FlowSuccess, gst::FlowError> {
128 let fret: gst::FlowReturn = fret.into();
129 unsafe {
130 try_from_glib(ffi::gst_flow_combiner_update_pad_flow(
131 self.to_glib_none().0,
132 pad.as_ref().to_glib_none().0,
133 fret.into_glib(),
134 ))
135 }
136 }
137}
138
139impl Default for FlowCombiner {
140 fn default() -> Self {
141 Self::new()
142 }
143}
144
145#[derive(Debug)]
146pub struct UniqueFlowCombiner(FlowCombiner);
147
148unsafe impl Sync for UniqueFlowCombiner {}
149unsafe impl Send for UniqueFlowCombiner {}
150
151impl UniqueFlowCombiner {
152 pub fn new() -> Self {
153 Self(FlowCombiner::new())
154 }
155
156 pub fn add_pad<P: IsA<gst::Pad>>(&mut self, pad: &P) {
157 self.0.add_pad(pad);
158 }
159
160 pub fn clear(&mut self) {
161 self.0.clear();
162 }
163
164 pub fn remove_pad<P: IsA<gst::Pad>>(&mut self, pad: &P) {
165 self.0.remove_pad(pad);
166 }
167
168 pub fn reset(&mut self) {
169 self.0.reset();
170 }
171
172 pub fn update_flow(
173 &mut self,
174 fret: Result<gst::FlowSuccess, gst::FlowError>,
175 ) -> Result<gst::FlowSuccess, gst::FlowError> {
176 self.0.update_flow(fret)
177 }
178
179 pub fn update_pad_flow<P: IsA<gst::Pad>>(
180 &mut self,
181 pad: &P,
182 fret: Result<gst::FlowSuccess, gst::FlowError>,
183 ) -> Result<gst::FlowSuccess, gst::FlowError> {
184 self.0.update_pad_flow(pad, fret)
185 }
186}
187
188impl Default for UniqueFlowCombiner {
189 fn default() -> Self {
190 Self::new()
191 }
192}