gstreamer_audio/
functions.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4use glib::translate::{from_glib_full, IntoGlibPtr, ToGlibPtr};
5
6#[doc(alias = "gst_audio_buffer_clip")]
7pub fn audio_buffer_clip(
8    buffer: gst::Buffer,
9    segment: &gst::Segment,
10    rate: u32,
11    bpf: u32,
12) -> Option<gst::Buffer> {
13    skip_assert_initialized!();
14
15    unsafe {
16        from_glib_full(ffi::gst_audio_buffer_clip(
17            buffer.into_glib_ptr(),
18            segment.to_glib_none().0,
19            rate as i32,
20            bpf as i32,
21        ))
22    }
23}
24
25#[cfg(feature = "v1_16")]
26#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
27#[doc(alias = "gst_audio_buffer_truncate")]
28pub fn audio_buffer_truncate(
29    buffer: gst::Buffer,
30    bpf: u32,
31    trim: usize,
32    samples: Option<usize>,
33) -> gst::Buffer {
34    skip_assert_initialized!();
35
36    unsafe {
37        from_glib_full(ffi::gst_audio_buffer_truncate(
38            buffer.into_glib_ptr(),
39            bpf as i32,
40            trim,
41            samples.unwrap_or(usize::MAX),
42        ))
43    }
44}
45
46pub fn audio_make_raw_caps(
47    formats: &[crate::AudioFormat],
48    layout: crate::AudioLayout,
49) -> crate::AudioCapsBuilder<gst::caps::NoFeature> {
50    skip_assert_initialized!();
51
52    let formats = formats.iter().copied().map(|f| match f {
53        crate::AudioFormat::Encoded => panic!("Invalid encoded format"),
54        crate::AudioFormat::Unknown => panic!("Invalid unknown format"),
55        _ => f,
56    });
57
58    let builder = crate::AudioCapsBuilder::new().format_list(formats);
59
60    match layout {
61        crate::AudioLayout::Interleaved => builder.field("layout", "interleaved"),
62        crate::AudioLayout::NonInterleaved => builder.field("layout", "non-interleaved"),
63        crate::AudioLayout::__Unknown(_) => builder,
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn audio_caps() {
73        gst::init().unwrap();
74
75        let caps = audio_make_raw_caps(
76            &[crate::AudioFormat::S16be, crate::AudioFormat::S16le],
77            crate::AudioLayout::Interleaved,
78        )
79        .build();
80        assert_eq!(caps.to_string(), "audio/x-raw, rate=(int)[ 1, 2147483647 ], channels=(int)[ 1, 2147483647 ], layout=(string)interleaved, format=(string){ S16BE, S16LE }");
81
82        #[cfg(feature = "v1_18")]
83        {
84            use glib::translate::IntoGlib;
85
86            /* audio_make_raw_caps() is a re-implementation so ensure it returns the same caps as the C API */
87            let c_caps = unsafe {
88                let formats: Vec<ffi::GstAudioFormat> =
89                    [crate::AudioFormat::S16be, crate::AudioFormat::S16le]
90                        .iter()
91                        .map(|f| f.into_glib())
92                        .collect();
93                let caps = ffi::gst_audio_make_raw_caps(
94                    formats.as_ptr(),
95                    formats.len() as u32,
96                    ffi::GST_AUDIO_LAYOUT_INTERLEAVED,
97                );
98                gst::Caps::from_glib_full(caps)
99            };
100            assert_eq!(caps, c_caps);
101        }
102
103        let caps = audio_make_raw_caps(
104            &[crate::AudioFormat::S16be, crate::AudioFormat::S16le],
105            crate::AudioLayout::NonInterleaved,
106        )
107        .rate(16000)
108        .channels(2)
109        .build();
110        assert_eq!(
111            caps.to_string(),
112            "audio/x-raw, rate=(int)16000, channels=(int)2, layout=(string)non-interleaved, format=(string){ S16BE, S16LE }",
113        );
114    }
115
116    #[test]
117    #[should_panic(expected = "Invalid encoded format")]
118    fn audio_caps_encoded() {
119        gst::init().unwrap();
120        let _caps = audio_make_raw_caps(
121            &[crate::AudioFormat::Encoded],
122            crate::AudioLayout::Interleaved,
123        );
124    }
125
126    #[test]
127    #[should_panic(expected = "Invalid unknown format")]
128    fn audio_caps_unknown() {
129        gst::init().unwrap();
130        let _caps = audio_make_raw_caps(
131            &[crate::AudioFormat::Unknown],
132            crate::AudioLayout::Interleaved,
133        );
134    }
135}