gstreamer_audio/
audio_encoder.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::{prelude::*, translate::*};
6
7use crate::{ffi, AudioEncoder};
8
9mod sealed {
10    pub trait Sealed {}
11    impl<T: super::IsA<super::AudioEncoder>> Sealed for T {}
12}
13
14pub trait AudioEncoderExtManual: sealed::Sealed + IsA<AudioEncoder> + 'static {
15    /// Negotiate with downstream elements to currently configured [`gst::Caps`][crate::gst::Caps].
16    /// Unmark GST_PAD_FLAG_NEED_RECONFIGURE in any case. But mark it again if
17    /// negotiate fails.
18    ///
19    /// # Returns
20    ///
21    /// [`true`] if the negotiation succeeded, else [`false`].
22    #[doc(alias = "gst_audio_encoder_negotiate")]
23    fn negotiate(&self) -> Result<(), gst::FlowError> {
24        unsafe {
25            let ret = from_glib(ffi::gst_audio_encoder_negotiate(
26                self.as_ref().to_glib_none().0,
27            ));
28            if ret {
29                Ok(())
30            } else {
31                Err(gst::FlowError::NotNegotiated)
32            }
33        }
34    }
35
36    /// Configure output caps on the srcpad of `self`.
37    /// ## `caps`
38    /// [`gst::Caps`][crate::gst::Caps]
39    ///
40    /// # Returns
41    ///
42    /// [`true`] on success.
43    #[doc(alias = "gst_audio_encoder_set_output_format")]
44    fn set_output_format(&self, caps: &gst::Caps) -> Result<(), gst::FlowError> {
45        unsafe {
46            let ret = from_glib(ffi::gst_audio_encoder_set_output_format(
47                self.as_ref().to_glib_none().0,
48                caps.to_glib_none().0,
49            ));
50            if ret {
51                Ok(())
52            } else {
53                Err(gst::FlowError::NotNegotiated)
54            }
55        }
56    }
57
58    /// Lets [`AudioEncoder`][crate::AudioEncoder] sub-classes to know the memory `allocator`
59    /// used by the base class and its `params`.
60    ///
61    /// Unref the `allocator` after use it.
62    ///
63    /// # Returns
64    ///
65    ///
66    /// ## `allocator`
67    /// the [`gst::Allocator`][crate::gst::Allocator]
68    /// used
69    ///
70    /// ## `params`
71    /// the
72    /// [`gst::AllocationParams`][crate::gst::AllocationParams] of `allocator`
73    #[doc(alias = "get_allocator")]
74    #[doc(alias = "gst_audio_encoder_get_allocator")]
75    fn allocator(&self) -> (Option<gst::Allocator>, gst::AllocationParams) {
76        unsafe {
77            let mut allocator = ptr::null_mut();
78            let mut params = mem::MaybeUninit::uninit();
79            ffi::gst_audio_encoder_get_allocator(
80                self.as_ref().to_glib_none().0,
81                &mut allocator,
82                params.as_mut_ptr(),
83            );
84            (from_glib_full(allocator), params.assume_init().into())
85        }
86    }
87
88    /// Set the codec headers to be sent downstream whenever requested.
89    /// ## `headers`
90    /// a list of
91    ///  [`gst::Buffer`][crate::gst::Buffer] containing the codec header
92    #[doc(alias = "gst_audio_encoder_set_headers")]
93    fn set_headers(&self, headers: impl IntoIterator<Item = gst::Buffer>) {
94        unsafe {
95            ffi::gst_audio_encoder_set_headers(
96                self.as_ref().to_glib_none().0,
97                headers
98                    .into_iter()
99                    .collect::<glib::List<_>>()
100                    .into_glib_ptr(),
101            );
102        }
103    }
104
105    fn sink_pad(&self) -> &gst::Pad {
106        unsafe {
107            let elt = &*(self.as_ptr() as *const ffi::GstAudioEncoder);
108            &*(&elt.sinkpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
109        }
110    }
111
112    fn src_pad(&self) -> &gst::Pad {
113        unsafe {
114            let elt = &*(self.as_ptr() as *const ffi::GstAudioEncoder);
115            &*(&elt.srcpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
116        }
117    }
118
119    fn input_segment(&self) -> gst::Segment {
120        unsafe {
121            let ptr: &ffi::GstAudioDecoder = &*(self.as_ptr() as *const _);
122            glib::ffi::g_rec_mutex_lock(mut_override(&ptr.stream_lock));
123            let segment = ptr.input_segment;
124            glib::ffi::g_rec_mutex_unlock(mut_override(&ptr.stream_lock));
125            from_glib_none(&segment as *const gst::ffi::GstSegment)
126        }
127    }
128
129    fn output_segment(&self) -> gst::Segment {
130        unsafe {
131            let ptr: &ffi::GstAudioDecoder = &*(self.as_ptr() as *const _);
132            glib::ffi::g_rec_mutex_lock(mut_override(&ptr.stream_lock));
133            let segment = ptr.output_segment;
134            glib::ffi::g_rec_mutex_unlock(mut_override(&ptr.stream_lock));
135            from_glib_none(&segment as *const gst::ffi::GstSegment)
136        }
137    }
138}
139
140impl<O: IsA<AudioEncoder>> AudioEncoderExtManual for O {}