1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use std::mem; |
4 | |
5 | use glib::translate::*; |
6 | use gst::subclass::prelude::*; |
7 | |
8 | use crate::{prelude::*, BaseParse, BaseParseFrame}; |
9 | |
10 | pub trait BaseParseImpl: BaseParseImplExt + ElementImpl { |
11 | fn start(&self) -> Result<(), gst::ErrorMessage> { |
12 | self.parent_start() |
13 | } |
14 | |
15 | fn stop(&self) -> Result<(), gst::ErrorMessage> { |
16 | self.parent_stop() |
17 | } |
18 | |
19 | fn set_sink_caps(&self, caps: &gst::Caps) -> Result<(), gst::LoggableError> { |
20 | self.parent_set_sink_caps(caps) |
21 | } |
22 | |
23 | fn handle_frame( |
24 | &self, |
25 | frame: BaseParseFrame, |
26 | ) -> Result<(gst::FlowSuccess, u32), gst::FlowError> { |
27 | self.parent_handle_frame(frame) |
28 | } |
29 | |
30 | fn convert( |
31 | &self, |
32 | src_val: impl gst::format::FormattedValue, |
33 | dest_format: gst::Format, |
34 | ) -> Option<gst::GenericFormattedValue> { |
35 | self.parent_convert(src_val, dest_format) |
36 | } |
37 | } |
38 | |
39 | mod sealed { |
40 | pub trait Sealed {} |
41 | impl<T: super::BaseParseImplExt> Sealed for T {} |
42 | } |
43 | |
44 | pub trait BaseParseImplExt: sealed::Sealed + ObjectSubclass { |
45 | fn parent_start(&self) -> Result<(), gst::ErrorMessage> { |
46 | unsafe { |
47 | let data = Self::type_data(); |
48 | let parent_class = data.as_ref().parent_class() as *mut ffi::GstBaseParseClass; |
49 | (*parent_class) |
50 | .start |
51 | .map(|f| { |
52 | if from_glib(f(self |
53 | .obj() |
54 | .unsafe_cast_ref::<BaseParse>() |
55 | .to_glib_none() |
56 | .0)) |
57 | { |
58 | Ok(()) |
59 | } else { |
60 | Err(gst::error_msg!( |
61 | gst::CoreError::StateChange, |
62 | ["Parent function `start` failed" ] |
63 | )) |
64 | } |
65 | }) |
66 | .unwrap_or(Ok(())) |
67 | } |
68 | } |
69 | |
70 | fn parent_stop(&self) -> Result<(), gst::ErrorMessage> { |
71 | unsafe { |
72 | let data = Self::type_data(); |
73 | let parent_class = data.as_ref().parent_class() as *mut ffi::GstBaseParseClass; |
74 | (*parent_class) |
75 | .stop |
76 | .map(|f| { |
77 | if from_glib(f(self |
78 | .obj() |
79 | .unsafe_cast_ref::<BaseParse>() |
80 | .to_glib_none() |
81 | .0)) |
82 | { |
83 | Ok(()) |
84 | } else { |
85 | Err(gst::error_msg!( |
86 | gst::CoreError::StateChange, |
87 | ["Parent function `stop` failed" ] |
88 | )) |
89 | } |
90 | }) |
91 | .unwrap_or(Ok(())) |
92 | } |
93 | } |
94 | |
95 | fn parent_set_sink_caps(&self, caps: &gst::Caps) -> Result<(), gst::LoggableError> { |
96 | unsafe { |
97 | let data = Self::type_data(); |
98 | let parent_class = data.as_ref().parent_class() as *mut ffi::GstBaseParseClass; |
99 | (*parent_class) |
100 | .set_sink_caps |
101 | .map(|f| { |
102 | gst::result_from_gboolean!( |
103 | f( |
104 | self.obj().unsafe_cast_ref::<BaseParse>().to_glib_none().0, |
105 | caps.to_glib_none().0, |
106 | ), |
107 | gst::CAT_RUST, |
108 | "Parent function `set_sink_caps` failed" , |
109 | ) |
110 | }) |
111 | .unwrap_or(Ok(())) |
112 | } |
113 | } |
114 | |
115 | fn parent_handle_frame( |
116 | &self, |
117 | frame: BaseParseFrame, |
118 | ) -> Result<(gst::FlowSuccess, u32), gst::FlowError> { |
119 | unsafe { |
120 | let data = Self::type_data(); |
121 | let parent_class = data.as_ref().parent_class() as *mut ffi::GstBaseParseClass; |
122 | let mut skipsize = 0; |
123 | (*parent_class) |
124 | .handle_frame |
125 | .map(|f| { |
126 | let res = try_from_glib(f( |
127 | self.obj().unsafe_cast_ref::<BaseParse>().to_glib_none().0, |
128 | frame.to_glib_none().0, |
129 | &mut skipsize, |
130 | )); |
131 | (res.unwrap(), skipsize as u32) |
132 | }) |
133 | .ok_or(gst::FlowError::Error) |
134 | } |
135 | } |
136 | |
137 | fn parent_convert( |
138 | &self, |
139 | src_val: impl gst::format::FormattedValue, |
140 | dest_format: gst::Format, |
141 | ) -> Option<gst::GenericFormattedValue> { |
142 | unsafe { |
143 | let data = Self::type_data(); |
144 | let parent_class = data.as_ref().parent_class() as *mut ffi::GstBaseParseClass; |
145 | let res = (*parent_class).convert.map(|f| { |
146 | let mut dest_val = mem::MaybeUninit::uninit(); |
147 | |
148 | let res = from_glib(f( |
149 | self.obj().unsafe_cast_ref::<BaseParse>().to_glib_none().0, |
150 | src_val.format().into_glib(), |
151 | src_val.into_raw_value(), |
152 | dest_format.into_glib(), |
153 | dest_val.as_mut_ptr(), |
154 | )); |
155 | (res, dest_val) |
156 | }); |
157 | |
158 | match res { |
159 | Some((true, dest_val)) => Some(gst::GenericFormattedValue::new( |
160 | dest_format, |
161 | dest_val.assume_init(), |
162 | )), |
163 | _ => None, |
164 | } |
165 | } |
166 | } |
167 | } |
168 | |
169 | impl<T: BaseParseImpl> BaseParseImplExt for T {} |
170 | |
171 | unsafe impl<T: BaseParseImpl> IsSubclassable<T> for BaseParse { |
172 | fn class_init(klass: &mut glib::Class<Self>) { |
173 | Self::parent_class_init::<T>(class:klass); |
174 | let klass: &mut GstBaseParseClass = klass.as_mut(); |
175 | klass.start = Some(base_parse_start::<T>); |
176 | klass.stop = Some(base_parse_stop::<T>); |
177 | klass.set_sink_caps = Some(base_parse_set_sink_caps::<T>); |
178 | klass.handle_frame = Some(base_parse_handle_frame::<T>); |
179 | klass.convert = Some(base_parse_convert::<T>); |
180 | } |
181 | } |
182 | |
183 | unsafe extern "C" fn base_parse_start<T: BaseParseImpl>( |
184 | ptr: *mut ffi::GstBaseParse, |
185 | ) -> glib::ffi::gboolean { |
186 | let instance: &::Instance = &*(ptr as *mut T::Instance); |
187 | let imp: &T = instance.imp(); |
188 | |
189 | gstbool::panic_to_error!(imp, false, { |
190 | match imp.start() { |
191 | Ok(()) => true, |
192 | Err(err) => { |
193 | imp.post_error_message(err); |
194 | false |
195 | } |
196 | } |
197 | }) |
198 | .into_glib() |
199 | } |
200 | |
201 | unsafe extern "C" fn base_parse_stop<T: BaseParseImpl>( |
202 | ptr: *mut ffi::GstBaseParse, |
203 | ) -> glib::ffi::gboolean { |
204 | let instance: &::Instance = &*(ptr as *mut T::Instance); |
205 | let imp: &T = instance.imp(); |
206 | |
207 | gstbool::panic_to_error!(imp, false, { |
208 | match imp.stop() { |
209 | Ok(()) => true, |
210 | Err(err) => { |
211 | imp.post_error_message(err); |
212 | false |
213 | } |
214 | } |
215 | }) |
216 | .into_glib() |
217 | } |
218 | |
219 | unsafe extern "C" fn base_parse_set_sink_caps<T: BaseParseImpl>( |
220 | ptr: *mut ffi::GstBaseParse, |
221 | caps: *mut gst::ffi::GstCaps, |
222 | ) -> glib::ffi::gboolean { |
223 | let instance: &::Instance = &*(ptr as *mut T::Instance); |
224 | let imp: &T = instance.imp(); |
225 | let caps: Borrowed<gst::Caps> = from_glib_borrow(ptr:caps); |
226 | |
227 | gstbool::panic_to_error!(imp, false, { |
228 | match imp.set_sink_caps(&caps) { |
229 | Ok(()) => true, |
230 | Err(err) => { |
231 | err.log_with_imp(imp); |
232 | false |
233 | } |
234 | } |
235 | }) |
236 | .into_glib() |
237 | } |
238 | |
239 | unsafe extern "C" fn base_parse_handle_frame<T: BaseParseImpl>( |
240 | ptr: *mut ffi::GstBaseParse, |
241 | frame: *mut ffi::GstBaseParseFrame, |
242 | skipsize: *mut i32, |
243 | ) -> gst::ffi::GstFlowReturn { |
244 | let instance: &::Instance = &*(ptr as *mut T::Instance); |
245 | let imp: &T = instance.imp(); |
246 | let instance: BorrowedObject<'_, ::Type> = imp.obj(); |
247 | let instance: &BaseParse = instance.unsafe_cast_ref::<BaseParse>(); |
248 | let wrap_frame: BaseParseFrame<'_> = BaseParseFrame::new(frame, _parse:instance); |
249 | |
250 | let res: Result<(FlowSuccess, u32), …> = gst::panic_to_error!(imp, Err(gst::FlowError::Error), { |
251 | imp.handle_frame(wrap_frame) |
252 | }); |
253 | |
254 | matchFlowReturn res { |
255 | Ok((flow: FlowSuccess, skip: u32)) => { |
256 | *skipsize = i32::try_from(skip).expect(msg:"skip is higher than i32::MAX" ); |
257 | gst::FlowReturn::from_ok(flow) |
258 | } |
259 | Err(flow: FlowError) => gst::FlowReturn::from_error(flow), |
260 | } |
261 | .into_glib() |
262 | } |
263 | |
264 | unsafe extern "C" fn base_parse_convert<T: BaseParseImpl>( |
265 | ptr: *mut ffi::GstBaseParse, |
266 | source_format: gst::ffi::GstFormat, |
267 | source_value: i64, |
268 | dest_format: gst::ffi::GstFormat, |
269 | dest_value: *mut i64, |
270 | ) -> glib::ffi::gboolean { |
271 | let instance: &::Instance = &*(ptr as *mut T::Instance); |
272 | let imp: &T = instance.imp(); |
273 | let source: GenericFormattedValue = gst::GenericFormattedValue::new(format:from_glib(val:source_format), source_value); |
274 | |
275 | let res: Option = gst::panic_to_error!(imp, None, { imp.convert(source, from_glib(dest_format)) }); |
276 | |
277 | matchbool res { |
278 | Some(dest: GenericFormattedValue) => { |
279 | *dest_value = dest.into_raw_value(); |
280 | true |
281 | } |
282 | _ => false, |
283 | } |
284 | .into_glib() |
285 | } |
286 | |