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