1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Take a look at the license at the top of the repository in the LICENSE file.

use std::fmt;
#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
use std::ptr;
#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
use std::slice;

use glib::translate::{from_glib, IntoGlib};
#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
use glib::translate::{from_glib_none, ToGlibPtr};
use gst::prelude::*;

use crate::ffi;

/// Extra buffer metadata describing how much audio has to be clipped from
/// the start or end of a buffer. This is used for compressed formats, where
/// the first frame usually has some additional samples due to encoder and
/// decoder delays, and the last frame usually has some additional samples to
/// be able to fill the complete last frame.
///
/// This is used to ensure that decoded data in the end has the same amount of
/// samples, and multiply decoded streams can be gaplessly concatenated.
///
/// Note: If clipping of the start is done by adjusting the segment, this meta
/// has to be dropped from buffers as otherwise clipping could happen twice.
#[repr(transparent)]
#[doc(alias = "GstAudioClippingMeta")]
pub struct AudioClippingMeta(ffi::GstAudioClippingMeta);

unsafe impl Send for AudioClippingMeta {}
unsafe impl Sync for AudioClippingMeta {}

impl AudioClippingMeta {
    #[doc(alias = "gst_buffer_add_audio_clipping_meta")]
    pub fn add<V: gst::format::FormattedValue>(
        buffer: &mut gst::BufferRef,
        start: V,
        end: V,
    ) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
        skip_assert_initialized!();
        assert_eq!(start.format(), end.format());
        unsafe {
            let meta = ffi::gst_buffer_add_audio_clipping_meta(
                buffer.as_mut_ptr(),
                start.format().into_glib(),
                start.into_raw_value() as u64,
                end.into_raw_value() as u64,
            );

            Self::from_mut_ptr(buffer, meta)
        }
    }

    #[doc(alias = "get_start")]
    #[inline]
    pub fn start(&self) -> gst::GenericFormattedValue {
        unsafe { gst::GenericFormattedValue::new(from_glib(self.0.format), self.0.start as i64) }
    }

    #[doc(alias = "get_end")]
    #[inline]
    pub fn end(&self) -> gst::GenericFormattedValue {
        unsafe { gst::GenericFormattedValue::new(from_glib(self.0.format), self.0.end as i64) }
    }
}

unsafe impl MetaAPI for AudioClippingMeta {
    type GstType = ffi::GstAudioClippingMeta;

    #[doc(alias = "gst_audio_clipping_meta_api_get_type")]
    #[inline]
    fn meta_api() -> glib::Type {
        unsafe { from_glib(ffi::gst_audio_clipping_meta_api_get_type()) }
    }
}

impl fmt::Debug for AudioClippingMeta {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("AudioClippingMeta")
            .field("start", &self.start())
            .field("end", &self.end())
            .finish()
    }
}

/// `GstAudioDownmixMeta` defines an audio downmix matrix to be send along with
/// audio buffers. These functions in this module help to create and attach the
/// meta as well as extracting it.
#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
#[repr(transparent)]
#[doc(alias = "GstAudioMeta")]
pub struct AudioMeta(ffi::GstAudioMeta);

#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
unsafe impl Send for AudioMeta {}
#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
unsafe impl Sync for AudioMeta {}

#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
impl AudioMeta {
    #[doc(alias = "gst_buffer_add_audio_meta")]
    pub fn add<'a>(
        buffer: &'a mut gst::BufferRef,
        info: &crate::AudioInfo,
        samples: usize,
        offsets: &[usize],
    ) -> Result<gst::MetaRefMut<'a, Self, gst::meta::Standalone>, glib::BoolError> {
        skip_assert_initialized!();

        if !info.is_valid() {
            return Err(glib::bool_error!("Invalid audio info"));
        }

        if info.rate() == 0
            || info.channels() == 0
            || info.format() == crate::AudioFormat::Unknown
            || info.format() == crate::AudioFormat::Encoded
        {
            return Err(glib::bool_error!("Unsupported audio format {:?}", info));
        }

        if !offsets.is_empty() && info.layout() != crate::AudioLayout::NonInterleaved {
            return Err(glib::bool_error!(
                "Channel offsets only supported for non-interleaved audio"
            ));
        }

        if !offsets.is_empty() && offsets.len() != info.channels() as usize {
            return Err(glib::bool_error!(
                "Number of channel offsets different than number of channels ({} != {})",
                offsets.len(),
                info.channels()
            ));
        }

        if info.layout() == crate::AudioLayout::NonInterleaved {
            let plane_size = samples * (info.width() / 8) as usize;
            let max_offset = if offsets.is_empty() {
                plane_size * (info.channels() - 1) as usize
            } else {
                let mut max_offset = None;

                for (i, offset) in offsets.iter().copied().enumerate() {
                    if let Some(current_max_offset) = max_offset {
                        max_offset = Some(std::cmp::max(current_max_offset, offset));
                    } else {
                        max_offset = Some(offset);
                    }

                    for (j, other_offset) in offsets.iter().copied().enumerate() {
                        if i != j
                            && !(other_offset + plane_size <= offset
                                || offset + plane_size <= other_offset)
                        {
                            return Err(glib::bool_error!("Overlapping audio channel offsets: offset {} for channel {} and offset {} for channel {} with a plane size of {}", offset, i, other_offset, j, plane_size));
                        }
                    }
                }

                max_offset.unwrap()
            };

            if max_offset + plane_size > buffer.size() {
                return Err(glib::bool_error!("Audio channel offsets out of bounds: max offset {} with plane size {} and buffer size {}", max_offset, plane_size, buffer.size()));
            }
        }

        unsafe {
            let meta = ffi::gst_buffer_add_audio_meta(
                buffer.as_mut_ptr(),
                info.to_glib_none().0,
                samples,
                if offsets.is_empty() {
                    ptr::null_mut()
                } else {
                    offsets.as_ptr() as *mut _
                },
            );

            if meta.is_null() {
                return Err(glib::bool_error!("Failed to add audio meta"));
            }

            Ok(Self::from_mut_ptr(buffer, meta))
        }
    }

    #[doc(alias = "get_info")]
    #[inline]
    pub fn info(&self) -> crate::AudioInfo {
        unsafe { from_glib_none(&self.0.info as *const _) }
    }

    #[doc(alias = "get_samples")]
    #[inline]
    pub fn samples(&self) -> usize {
        self.0.samples
    }

    #[doc(alias = "get_offsets")]
    #[inline]
    pub fn offsets(&self) -> &[usize] {
        if self.0.offsets.is_null() || self.0.info.channels < 1 {
            return &[];
        }

        unsafe { slice::from_raw_parts(self.0.offsets, self.0.info.channels as usize) }
    }
}

#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
unsafe impl MetaAPI for AudioMeta {
    type GstType = ffi::GstAudioMeta;

    #[doc(alias = "gst_audio_meta_api_get_type")]
    #[inline]
    fn meta_api() -> glib::Type {
        unsafe { from_glib(ffi::gst_audio_meta_api_get_type()) }
    }
}

#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
impl fmt::Debug for AudioMeta {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("AudioMeta")
            .field("info", &self.info())
            .field("samples", &self.samples())
            .field("offsets", &self.offsets())
            .finish()
    }
}

/// Meta containing Audio Level Indication: https://tools.ietf.org/html/rfc6464
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
#[repr(transparent)]
#[doc(alias = "GstAudioLevelMeta")]
pub struct AudioLevelMeta(ffi::GstAudioLevelMeta);

#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
unsafe impl Send for AudioLevelMeta {}
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
unsafe impl Sync for AudioLevelMeta {}

#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
impl AudioLevelMeta {
    #[doc(alias = "gst_buffer_add_audio_level_meta")]
    pub fn add(
        buffer: &mut gst::BufferRef,
        level: u8,
        voice_activity: bool,
    ) -> gst::MetaRefMut<Self, gst::meta::Standalone> {
        skip_assert_initialized!();
        unsafe {
            let meta = ffi::gst_buffer_add_audio_level_meta(
                buffer.as_mut_ptr(),
                level,
                voice_activity.into_glib(),
            );

            Self::from_mut_ptr(buffer, meta)
        }
    }

    #[doc(alias = "get_level")]
    #[inline]
    pub fn level(&self) -> u8 {
        self.0.level
    }

    #[doc(alias = "get_voice_activity")]
    #[inline]
    pub fn voice_activity(&self) -> bool {
        unsafe { from_glib(self.0.voice_activity) }
    }
}

#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
unsafe impl MetaAPI for AudioLevelMeta {
    type GstType = ffi::GstAudioLevelMeta;

    #[doc(alias = "gst_audio_level_meta_api_get_type")]
    #[inline]
    fn meta_api() -> glib::Type {
        unsafe { from_glib(ffi::gst_audio_level_meta_api_get_type()) }
    }
}

#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
impl fmt::Debug for AudioLevelMeta {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("AudioLevelMeta")
            .field("level", &self.level())
            .field("voice_activity", &self.voice_activity())
            .finish()
    }
}

pub mod tags {
    gst::impl_meta_tag!(Audio, crate::ffi::GST_META_TAG_AUDIO_STR);
    gst::impl_meta_tag!(Channels, crate::ffi::GST_META_TAG_AUDIO_CHANNELS_STR);
    gst::impl_meta_tag!(Rate, crate::ffi::GST_META_TAG_AUDIO_RATE_STR);
    #[cfg(feature = "v1_24")]
    gst::impl_meta_tag!(
        DSDPlaneOffsets,
        crate::ffi::GST_META_TAG_DSD_PLANE_OFFSETS_STR
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add_get_audio_clipping_meta() {
        use gst::prelude::*;

        gst::init().unwrap();

        let mut buffer = gst::Buffer::with_size(1024).unwrap();

        let start = 1.default_format();
        let stop = 2.default_format();

        {
            let cmeta = AudioClippingMeta::add(buffer.get_mut().unwrap(), start, stop);
            assert_eq!(cmeta.start().try_into(), Ok(Some(start)));
            assert_eq!(cmeta.end().try_into(), Ok(Some(stop)));
        }

        {
            let cmeta = buffer.meta::<AudioClippingMeta>().unwrap();
            assert_eq!(cmeta.start().try_into(), Ok(Some(start)));
            assert_eq!(cmeta.end().try_into(), Ok(Some(stop)));

            assert!(cmeta.has_tag::<tags::Audio>());
        }
    }

    #[cfg(feature = "v1_20")]
    #[test]
    fn test_add_get_audio_level_meta() {
        gst::init().unwrap();

        let mut buffer = gst::Buffer::with_size(1024).unwrap();

        {
            let cmeta = AudioLevelMeta::add(buffer.get_mut().unwrap(), 10, true);
            assert_eq!(cmeta.level(), 10);
            assert!(cmeta.voice_activity());
        }

        {
            let cmeta = buffer.meta::<AudioLevelMeta>().unwrap();
            assert_eq!(cmeta.level(), 10);
            assert!(cmeta.voice_activity());
        }
    }
}