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