gstreamer_audio/subclass/
audio_decoder.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{mem, ptr};
4
5use glib::translate::*;
6use gst::subclass::prelude::*;
7
8use crate::{ffi, prelude::*, AudioDecoder};
9
10pub trait AudioDecoderImpl: ElementImpl + ObjectSubclass<Type: IsA<AudioDecoder>> {
11    /// Optional.
12    ///  Called when the element changes to GST_STATE_READY.
13    ///  Allows opening external resources.
14    fn open(&self) -> Result<(), gst::ErrorMessage> {
15        self.parent_open()
16    }
17
18    /// Optional.
19    ///  Called when the element changes to GST_STATE_NULL.
20    ///  Allows closing external resources.
21    fn close(&self) -> Result<(), gst::ErrorMessage> {
22        self.parent_close()
23    }
24
25    /// Optional.
26    ///  Called when the element starts processing.
27    ///  Allows opening external resources.
28    fn start(&self) -> Result<(), gst::ErrorMessage> {
29        self.parent_start()
30    }
31
32    /// Optional.
33    ///  Called when the element stops processing.
34    ///  Allows closing external resources.
35    fn stop(&self) -> Result<(), gst::ErrorMessage> {
36        self.parent_stop()
37    }
38
39    /// Notifies subclass of incoming data format (caps).
40    fn set_format(&self, caps: &gst::Caps) -> Result<(), gst::LoggableError> {
41        self.parent_set_format(caps)
42    }
43
44    fn parse(&self, adapter: &gst_base::Adapter) -> Result<(u32, u32), gst::FlowError> {
45        self.parent_parse(adapter)
46    }
47
48    /// Provides input data (or NULL to clear any remaining data)
49    ///  to subclass. Input data ref management is performed by
50    ///  base class, subclass should not care or intervene,
51    ///  and input data is only valid until next call to base class,
52    ///  most notably a call to [`AudioDecoderExt::finish_frame()`][crate::prelude::AudioDecoderExt::finish_frame()].
53    fn handle_frame(
54        &self,
55        buffer: Option<&gst::Buffer>,
56    ) -> Result<gst::FlowSuccess, gst::FlowError> {
57        self.parent_handle_frame(buffer)
58    }
59
60    /// Optional.
61    ///  Called just prior to pushing (encoded data) buffer downstream.
62    ///  Subclass has full discretionary access to buffer,
63    ///  and a not OK flow return will abort downstream pushing.
64    fn pre_push(&self, buffer: gst::Buffer) -> Result<Option<gst::Buffer>, gst::FlowError> {
65        self.parent_pre_push(buffer)
66    }
67
68    /// Optional.
69    ///  Instructs subclass to clear any codec caches and discard
70    ///  any pending samples and not yet returned decoded data.
71    ///  `hard` indicates whether a FLUSH is being processed,
72    ///  or otherwise a DISCONT (or conceptually similar).
73    fn flush(&self, hard: bool) {
74        self.parent_flush(hard)
75    }
76
77    /// Negotiate with downstream elements to currently configured [`AudioInfo`][crate::AudioInfo].
78    /// Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
79    /// negotiate fails.
80    ///
81    /// # Returns
82    ///
83    /// [`true`] if the negotiation succeeded, else [`false`].
84    fn negotiate(&self) -> Result<(), gst::LoggableError> {
85        self.parent_negotiate()
86    }
87
88    fn caps(&self, filter: Option<&gst::Caps>) -> gst::Caps {
89        self.parent_caps(filter)
90    }
91
92    /// Optional.
93    ///  Event handler on the sink pad. Subclasses should chain up to
94    ///  the parent implementation to invoke the default handler.
95    fn sink_event(&self, event: gst::Event) -> bool {
96        self.parent_sink_event(event)
97    }
98
99    /// Optional.
100    ///  Query handler on the sink pad. This function should
101    ///  return TRUE if the query could be performed. Subclasses
102    ///  should chain up to the parent implementation to invoke the
103    ///  default handler. Since: 1.6
104    fn sink_query(&self, query: &mut gst::QueryRef) -> bool {
105        self.parent_sink_query(query)
106    }
107
108    /// Optional.
109    ///  Event handler on the src pad. Subclasses should chain up to
110    ///  the parent implementation to invoke the default handler.
111    fn src_event(&self, event: gst::Event) -> bool {
112        self.parent_src_event(event)
113    }
114
115    /// Optional.
116    ///  Query handler on the source pad. This function should
117    ///  return TRUE if the query could be performed. Subclasses
118    ///  should chain up to the parent implementation to invoke the
119    ///  default handler. Since: 1.6
120    fn src_query(&self, query: &mut gst::QueryRef) -> bool {
121        self.parent_src_query(query)
122    }
123
124    /// Optional.
125    ///  Propose buffer allocation parameters for upstream elements.
126    ///  Subclasses should chain up to the parent implementation to
127    ///  invoke the default handler.
128    fn propose_allocation(
129        &self,
130        query: &mut gst::query::Allocation,
131    ) -> Result<(), gst::LoggableError> {
132        self.parent_propose_allocation(query)
133    }
134
135    /// Optional.
136    ///  Setup the allocation parameters for allocating output
137    ///  buffers. The passed in query contains the result of the
138    ///  downstream allocation query.
139    ///  Subclasses should chain up to the parent implementation to
140    ///  invoke the default handler.
141    fn decide_allocation(
142        &self,
143        query: &mut gst::query::Allocation,
144    ) -> Result<(), gst::LoggableError> {
145        self.parent_decide_allocation(query)
146    }
147}
148
149pub trait AudioDecoderImplExt: AudioDecoderImpl {
150    fn parent_open(&self) -> Result<(), gst::ErrorMessage> {
151        unsafe {
152            let data = Self::type_data();
153            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
154            (*parent_class)
155                .open
156                .map(|f| {
157                    if from_glib(f(self
158                        .obj()
159                        .unsafe_cast_ref::<AudioDecoder>()
160                        .to_glib_none()
161                        .0))
162                    {
163                        Ok(())
164                    } else {
165                        Err(gst::error_msg!(
166                            gst::CoreError::StateChange,
167                            ["Parent function `open` failed"]
168                        ))
169                    }
170                })
171                .unwrap_or(Ok(()))
172        }
173    }
174
175    fn parent_close(&self) -> Result<(), gst::ErrorMessage> {
176        unsafe {
177            let data = Self::type_data();
178            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
179            (*parent_class)
180                .close
181                .map(|f| {
182                    if from_glib(f(self
183                        .obj()
184                        .unsafe_cast_ref::<AudioDecoder>()
185                        .to_glib_none()
186                        .0))
187                    {
188                        Ok(())
189                    } else {
190                        Err(gst::error_msg!(
191                            gst::CoreError::StateChange,
192                            ["Parent function `close` failed"]
193                        ))
194                    }
195                })
196                .unwrap_or(Ok(()))
197        }
198    }
199
200    fn parent_start(&self) -> Result<(), gst::ErrorMessage> {
201        unsafe {
202            let data = Self::type_data();
203            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
204            (*parent_class)
205                .start
206                .map(|f| {
207                    if from_glib(f(self
208                        .obj()
209                        .unsafe_cast_ref::<AudioDecoder>()
210                        .to_glib_none()
211                        .0))
212                    {
213                        Ok(())
214                    } else {
215                        Err(gst::error_msg!(
216                            gst::CoreError::StateChange,
217                            ["Parent function `start` failed"]
218                        ))
219                    }
220                })
221                .unwrap_or(Ok(()))
222        }
223    }
224
225    fn parent_stop(&self) -> Result<(), gst::ErrorMessage> {
226        unsafe {
227            let data = Self::type_data();
228            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
229            (*parent_class)
230                .stop
231                .map(|f| {
232                    if from_glib(f(self
233                        .obj()
234                        .unsafe_cast_ref::<AudioDecoder>()
235                        .to_glib_none()
236                        .0))
237                    {
238                        Ok(())
239                    } else {
240                        Err(gst::error_msg!(
241                            gst::CoreError::StateChange,
242                            ["Parent function `stop` failed"]
243                        ))
244                    }
245                })
246                .unwrap_or(Ok(()))
247        }
248    }
249
250    fn parent_set_format(&self, caps: &gst::Caps) -> Result<(), gst::LoggableError> {
251        unsafe {
252            let data = Self::type_data();
253            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
254            (*parent_class)
255                .set_format
256                .map(|f| {
257                    gst::result_from_gboolean!(
258                        f(
259                            self.obj()
260                                .unsafe_cast_ref::<AudioDecoder>()
261                                .to_glib_none()
262                                .0,
263                            caps.to_glib_none().0
264                        ),
265                        gst::CAT_RUST,
266                        "parent function `set_format` failed"
267                    )
268                })
269                .unwrap_or(Ok(()))
270        }
271    }
272
273    fn parent_parse(&self, adapter: &gst_base::Adapter) -> Result<(u32, u32), gst::FlowError> {
274        unsafe {
275            let data = Self::type_data();
276            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
277            (*parent_class)
278                .parse
279                .map(|f| {
280                    let mut offset = mem::MaybeUninit::uninit();
281                    let mut len = mem::MaybeUninit::uninit();
282                    gst::FlowSuccess::try_from_glib(f(
283                        self.obj()
284                            .unsafe_cast_ref::<AudioDecoder>()
285                            .to_glib_none()
286                            .0,
287                        adapter.to_glib_none().0,
288                        offset.as_mut_ptr(),
289                        len.as_mut_ptr(),
290                    ))
291                    .map(|_| {
292                        let offset = offset.assume_init();
293                        let len = len.assume_init();
294                        assert!(offset >= 0);
295                        assert!(len >= 0);
296                        (offset as u32, len as u32)
297                    })
298                })
299                .unwrap_or_else(|| Ok((0, adapter.available() as u32)))
300        }
301    }
302
303    fn parent_handle_frame(
304        &self,
305        buffer: Option<&gst::Buffer>,
306    ) -> Result<gst::FlowSuccess, gst::FlowError> {
307        unsafe {
308            let data = Self::type_data();
309            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
310            (*parent_class)
311                .handle_frame
312                .map(|f| {
313                    try_from_glib(f(
314                        self.obj()
315                            .unsafe_cast_ref::<AudioDecoder>()
316                            .to_glib_none()
317                            .0,
318                        buffer
319                            .map(|buffer| buffer.as_mut_ptr() as *mut *mut gst::ffi::GstBuffer)
320                            .unwrap_or(ptr::null_mut()),
321                    ))
322                })
323                .unwrap_or(Err(gst::FlowError::Error))
324        }
325    }
326
327    fn parent_pre_push(&self, buffer: gst::Buffer) -> Result<Option<gst::Buffer>, gst::FlowError> {
328        unsafe {
329            let data = Self::type_data();
330            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
331            if let Some(f) = (*parent_class).pre_push {
332                let mut buffer = buffer.into_glib_ptr();
333                gst::FlowSuccess::try_from_glib(f(
334                    self.obj()
335                        .unsafe_cast_ref::<AudioDecoder>()
336                        .to_glib_none()
337                        .0,
338                    &mut buffer,
339                ))
340                .map(|_| from_glib_full(buffer))
341            } else {
342                Ok(Some(buffer))
343            }
344        }
345    }
346
347    fn parent_flush(&self, hard: bool) {
348        unsafe {
349            let data = Self::type_data();
350            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
351            (*parent_class)
352                .flush
353                .map(|f| {
354                    f(
355                        self.obj()
356                            .unsafe_cast_ref::<AudioDecoder>()
357                            .to_glib_none()
358                            .0,
359                        hard.into_glib(),
360                    )
361                })
362                .unwrap_or(())
363        }
364    }
365
366    fn parent_negotiate(&self) -> Result<(), gst::LoggableError> {
367        unsafe {
368            let data = Self::type_data();
369            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
370            (*parent_class)
371                .negotiate
372                .map(|f| {
373                    gst::result_from_gboolean!(
374                        f(self
375                            .obj()
376                            .unsafe_cast_ref::<AudioDecoder>()
377                            .to_glib_none()
378                            .0),
379                        gst::CAT_RUST,
380                        "Parent function `negotiate` failed"
381                    )
382                })
383                .unwrap_or(Ok(()))
384        }
385    }
386
387    fn parent_caps(&self, filter: Option<&gst::Caps>) -> gst::Caps {
388        unsafe {
389            let data = Self::type_data();
390            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
391            (*parent_class)
392                .getcaps
393                .map(|f| {
394                    from_glib_full(f(
395                        self.obj()
396                            .unsafe_cast_ref::<AudioDecoder>()
397                            .to_glib_none()
398                            .0,
399                        filter.to_glib_none().0,
400                    ))
401                })
402                .unwrap_or_else(|| {
403                    self.obj()
404                        .unsafe_cast_ref::<AudioDecoder>()
405                        .proxy_getcaps(None, filter)
406                })
407        }
408    }
409
410    fn parent_sink_event(&self, event: gst::Event) -> bool {
411        unsafe {
412            let data = Self::type_data();
413            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
414            let f = (*parent_class)
415                .sink_event
416                .expect("Missing parent function `sink_event`");
417            from_glib(f(
418                self.obj()
419                    .unsafe_cast_ref::<AudioDecoder>()
420                    .to_glib_none()
421                    .0,
422                event.into_glib_ptr(),
423            ))
424        }
425    }
426
427    fn parent_sink_query(&self, query: &mut gst::QueryRef) -> bool {
428        unsafe {
429            let data = Self::type_data();
430            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
431            let f = (*parent_class)
432                .sink_query
433                .expect("Missing parent function `sink_query`");
434            from_glib(f(
435                self.obj()
436                    .unsafe_cast_ref::<AudioDecoder>()
437                    .to_glib_none()
438                    .0,
439                query.as_mut_ptr(),
440            ))
441        }
442    }
443
444    fn parent_src_event(&self, event: gst::Event) -> bool {
445        unsafe {
446            let data = Self::type_data();
447            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
448            let f = (*parent_class)
449                .src_event
450                .expect("Missing parent function `src_event`");
451            from_glib(f(
452                self.obj()
453                    .unsafe_cast_ref::<AudioDecoder>()
454                    .to_glib_none()
455                    .0,
456                event.into_glib_ptr(),
457            ))
458        }
459    }
460
461    fn parent_src_query(&self, query: &mut gst::QueryRef) -> bool {
462        unsafe {
463            let data = Self::type_data();
464            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
465            let f = (*parent_class)
466                .src_query
467                .expect("Missing parent function `src_query`");
468            from_glib(f(
469                self.obj()
470                    .unsafe_cast_ref::<AudioDecoder>()
471                    .to_glib_none()
472                    .0,
473                query.as_mut_ptr(),
474            ))
475        }
476    }
477
478    fn parent_propose_allocation(
479        &self,
480        query: &mut gst::query::Allocation,
481    ) -> Result<(), gst::LoggableError> {
482        unsafe {
483            let data = Self::type_data();
484            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
485            (*parent_class)
486                .propose_allocation
487                .map(|f| {
488                    gst::result_from_gboolean!(
489                        f(
490                            self.obj()
491                                .unsafe_cast_ref::<AudioDecoder>()
492                                .to_glib_none()
493                                .0,
494                            query.as_mut_ptr(),
495                        ),
496                        gst::CAT_RUST,
497                        "Parent function `propose_allocation` failed",
498                    )
499                })
500                .unwrap_or(Ok(()))
501        }
502    }
503
504    fn parent_decide_allocation(
505        &self,
506        query: &mut gst::query::Allocation,
507    ) -> Result<(), gst::LoggableError> {
508        unsafe {
509            let data = Self::type_data();
510            let parent_class = data.as_ref().parent_class() as *mut ffi::GstAudioDecoderClass;
511            (*parent_class)
512                .decide_allocation
513                .map(|f| {
514                    gst::result_from_gboolean!(
515                        f(
516                            self.obj()
517                                .unsafe_cast_ref::<AudioDecoder>()
518                                .to_glib_none()
519                                .0,
520                            query.as_mut_ptr(),
521                        ),
522                        gst::CAT_RUST,
523                        "Parent function `decide_allocation` failed",
524                    )
525                })
526                .unwrap_or(Ok(()))
527        }
528    }
529}
530
531impl<T: AudioDecoderImpl> AudioDecoderImplExt for T {}
532
533unsafe impl<T: AudioDecoderImpl> IsSubclassable<T> for AudioDecoder {
534    fn class_init(klass: &mut glib::Class<Self>) {
535        Self::parent_class_init::<T>(klass);
536        let klass = klass.as_mut();
537        klass.open = Some(audio_decoder_open::<T>);
538        klass.close = Some(audio_decoder_close::<T>);
539        klass.start = Some(audio_decoder_start::<T>);
540        klass.stop = Some(audio_decoder_stop::<T>);
541        klass.set_format = Some(audio_decoder_set_format::<T>);
542        klass.parse = Some(audio_decoder_parse::<T>);
543        klass.handle_frame = Some(audio_decoder_handle_frame::<T>);
544        klass.pre_push = Some(audio_decoder_pre_push::<T>);
545        klass.flush = Some(audio_decoder_flush::<T>);
546        klass.negotiate = Some(audio_decoder_negotiate::<T>);
547        klass.getcaps = Some(audio_decoder_getcaps::<T>);
548        klass.sink_event = Some(audio_decoder_sink_event::<T>);
549        klass.src_event = Some(audio_decoder_src_event::<T>);
550        klass.sink_query = Some(audio_decoder_sink_query::<T>);
551        klass.src_query = Some(audio_decoder_src_query::<T>);
552        klass.propose_allocation = Some(audio_decoder_propose_allocation::<T>);
553        klass.decide_allocation = Some(audio_decoder_decide_allocation::<T>);
554    }
555}
556
557unsafe extern "C" fn audio_decoder_open<T: AudioDecoderImpl>(
558    ptr: *mut ffi::GstAudioDecoder,
559) -> glib::ffi::gboolean {
560    let instance = &*(ptr as *mut T::Instance);
561    let imp = instance.imp();
562
563    gst::panic_to_error!(imp, false, {
564        match imp.open() {
565            Ok(()) => true,
566            Err(err) => {
567                imp.post_error_message(err);
568                false
569            }
570        }
571    })
572    .into_glib()
573}
574
575unsafe extern "C" fn audio_decoder_close<T: AudioDecoderImpl>(
576    ptr: *mut ffi::GstAudioDecoder,
577) -> glib::ffi::gboolean {
578    let instance = &*(ptr as *mut T::Instance);
579    let imp = instance.imp();
580
581    gst::panic_to_error!(imp, false, {
582        match imp.close() {
583            Ok(()) => true,
584            Err(err) => {
585                imp.post_error_message(err);
586                false
587            }
588        }
589    })
590    .into_glib()
591}
592
593unsafe extern "C" fn audio_decoder_start<T: AudioDecoderImpl>(
594    ptr: *mut ffi::GstAudioDecoder,
595) -> glib::ffi::gboolean {
596    let instance = &*(ptr as *mut T::Instance);
597    let imp = instance.imp();
598
599    gst::panic_to_error!(imp, false, {
600        match imp.start() {
601            Ok(()) => true,
602            Err(err) => {
603                imp.post_error_message(err);
604                false
605            }
606        }
607    })
608    .into_glib()
609}
610
611unsafe extern "C" fn audio_decoder_stop<T: AudioDecoderImpl>(
612    ptr: *mut ffi::GstAudioDecoder,
613) -> glib::ffi::gboolean {
614    let instance = &*(ptr as *mut T::Instance);
615    let imp = instance.imp();
616
617    gst::panic_to_error!(imp, false, {
618        match imp.stop() {
619            Ok(()) => true,
620            Err(err) => {
621                imp.post_error_message(err);
622                false
623            }
624        }
625    })
626    .into_glib()
627}
628
629unsafe extern "C" fn audio_decoder_set_format<T: AudioDecoderImpl>(
630    ptr: *mut ffi::GstAudioDecoder,
631    caps: *mut gst::ffi::GstCaps,
632) -> glib::ffi::gboolean {
633    let instance = &*(ptr as *mut T::Instance);
634    let imp = instance.imp();
635
636    gst::panic_to_error!(imp, false, {
637        match imp.set_format(&from_glib_borrow(caps)) {
638            Ok(()) => true,
639            Err(err) => {
640                err.log_with_imp(imp);
641                false
642            }
643        }
644    })
645    .into_glib()
646}
647
648unsafe extern "C" fn audio_decoder_parse<T: AudioDecoderImpl>(
649    ptr: *mut ffi::GstAudioDecoder,
650    adapter: *mut gst_base::ffi::GstAdapter,
651    offset: *mut i32,
652    len: *mut i32,
653) -> gst::ffi::GstFlowReturn {
654    let instance = &*(ptr as *mut T::Instance);
655    let imp = instance.imp();
656
657    gst::panic_to_error!(imp, gst::FlowReturn::Error, {
658        match imp.parse(&from_glib_borrow(adapter)) {
659            Ok((new_offset, new_len)) => {
660                assert!(new_offset <= i32::MAX as u32);
661                assert!(new_len <= i32::MAX as u32);
662                *offset = new_offset as i32;
663                *len = new_len as i32;
664                Ok(gst::FlowSuccess::Ok)
665            }
666            Err(err) => Err(err),
667        }
668        .into()
669    })
670    .into_glib()
671}
672
673unsafe extern "C" fn audio_decoder_handle_frame<T: AudioDecoderImpl>(
674    ptr: *mut ffi::GstAudioDecoder,
675    buffer: *mut *mut gst::ffi::GstBuffer,
676) -> gst::ffi::GstFlowReturn {
677    // FIXME: Misgenerated in gstreamer-audio-sys
678    let buffer = buffer as *mut gst::ffi::GstBuffer;
679    let instance = &*(ptr as *mut T::Instance);
680    let imp = instance.imp();
681
682    gst::panic_to_error!(imp, gst::FlowReturn::Error, {
683        imp.handle_frame(Option::<gst::Buffer>::from_glib_none(buffer).as_ref())
684            .into()
685    })
686    .into_glib()
687}
688
689unsafe extern "C" fn audio_decoder_pre_push<T: AudioDecoderImpl>(
690    ptr: *mut ffi::GstAudioDecoder,
691    buffer: *mut *mut gst::ffi::GstBuffer,
692) -> gst::ffi::GstFlowReturn {
693    let instance = &*(ptr as *mut T::Instance);
694    let imp = instance.imp();
695
696    gst::panic_to_error!(imp, gst::FlowReturn::Error, {
697        match imp.pre_push(from_glib_full(*buffer)) {
698            Ok(Some(new_buffer)) => {
699                *buffer = new_buffer.into_glib_ptr();
700                Ok(gst::FlowSuccess::Ok)
701            }
702            Ok(None) => {
703                *buffer = ptr::null_mut();
704                Ok(gst::FlowSuccess::Ok)
705            }
706            Err(err) => Err(err),
707        }
708        .into()
709    })
710    .into_glib()
711}
712
713unsafe extern "C" fn audio_decoder_flush<T: AudioDecoderImpl>(
714    ptr: *mut ffi::GstAudioDecoder,
715    hard: glib::ffi::gboolean,
716) {
717    let instance = &*(ptr as *mut T::Instance);
718    let imp = instance.imp();
719
720    gst::panic_to_error!(imp, (), { AudioDecoderImpl::flush(imp, from_glib(hard)) })
721}
722
723unsafe extern "C" fn audio_decoder_negotiate<T: AudioDecoderImpl>(
724    ptr: *mut ffi::GstAudioDecoder,
725) -> glib::ffi::gboolean {
726    let instance = &*(ptr as *mut T::Instance);
727    let imp = instance.imp();
728
729    gst::panic_to_error!(imp, false, {
730        match imp.negotiate() {
731            Ok(()) => true,
732            Err(err) => {
733                err.log_with_imp(imp);
734                false
735            }
736        }
737    })
738    .into_glib()
739}
740
741unsafe extern "C" fn audio_decoder_getcaps<T: AudioDecoderImpl>(
742    ptr: *mut ffi::GstAudioDecoder,
743    filter: *mut gst::ffi::GstCaps,
744) -> *mut gst::ffi::GstCaps {
745    let instance = &*(ptr as *mut T::Instance);
746    let imp = instance.imp();
747
748    gst::panic_to_error!(imp, gst::Caps::new_empty(), {
749        AudioDecoderImpl::caps(
750            imp,
751            Option::<gst::Caps>::from_glib_borrow(filter)
752                .as_ref()
753                .as_ref(),
754        )
755    })
756    .into_glib_ptr()
757}
758
759unsafe extern "C" fn audio_decoder_sink_event<T: AudioDecoderImpl>(
760    ptr: *mut ffi::GstAudioDecoder,
761    event: *mut gst::ffi::GstEvent,
762) -> glib::ffi::gboolean {
763    let instance = &*(ptr as *mut T::Instance);
764    let imp = instance.imp();
765
766    gst::panic_to_error!(imp, false, { imp.sink_event(from_glib_full(event)) }).into_glib()
767}
768
769unsafe extern "C" fn audio_decoder_sink_query<T: AudioDecoderImpl>(
770    ptr: *mut ffi::GstAudioDecoder,
771    query: *mut gst::ffi::GstQuery,
772) -> glib::ffi::gboolean {
773    let instance = &*(ptr as *mut T::Instance);
774    let imp = instance.imp();
775
776    gst::panic_to_error!(imp, false, {
777        imp.sink_query(gst::QueryRef::from_mut_ptr(query))
778    })
779    .into_glib()
780}
781
782unsafe extern "C" fn audio_decoder_src_event<T: AudioDecoderImpl>(
783    ptr: *mut ffi::GstAudioDecoder,
784    event: *mut gst::ffi::GstEvent,
785) -> glib::ffi::gboolean {
786    let instance = &*(ptr as *mut T::Instance);
787    let imp = instance.imp();
788
789    gst::panic_to_error!(imp, false, { imp.src_event(from_glib_full(event)) }).into_glib()
790}
791
792unsafe extern "C" fn audio_decoder_src_query<T: AudioDecoderImpl>(
793    ptr: *mut ffi::GstAudioDecoder,
794    query: *mut gst::ffi::GstQuery,
795) -> glib::ffi::gboolean {
796    let instance = &*(ptr as *mut T::Instance);
797    let imp = instance.imp();
798
799    gst::panic_to_error!(imp, false, {
800        imp.src_query(gst::QueryRef::from_mut_ptr(query))
801    })
802    .into_glib()
803}
804
805unsafe extern "C" fn audio_decoder_propose_allocation<T: AudioDecoderImpl>(
806    ptr: *mut ffi::GstAudioDecoder,
807    query: *mut gst::ffi::GstQuery,
808) -> glib::ffi::gboolean {
809    let instance = &*(ptr as *mut T::Instance);
810    let imp = instance.imp();
811    let query = match gst::QueryRef::from_mut_ptr(query).view_mut() {
812        gst::QueryViewMut::Allocation(allocation) => allocation,
813        _ => unreachable!(),
814    };
815
816    gst::panic_to_error!(imp, false, {
817        match imp.propose_allocation(query) {
818            Ok(()) => true,
819            Err(err) => {
820                err.log_with_imp(imp);
821                false
822            }
823        }
824    })
825    .into_glib()
826}
827
828unsafe extern "C" fn audio_decoder_decide_allocation<T: AudioDecoderImpl>(
829    ptr: *mut ffi::GstAudioDecoder,
830    query: *mut gst::ffi::GstQuery,
831) -> glib::ffi::gboolean {
832    let instance = &*(ptr as *mut T::Instance);
833    let imp = instance.imp();
834    let query = match gst::QueryRef::from_mut_ptr(query).view_mut() {
835        gst::QueryViewMut::Allocation(allocation) => allocation,
836        _ => unreachable!(),
837    };
838
839    gst::panic_to_error!(imp, false, {
840        match imp.decide_allocation(query) {
841            Ok(()) => true,
842            Err(err) => {
843                err.log_with_imp(imp);
844                false
845            }
846        }
847    })
848    .into_glib()
849}