2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2017-08-08 22:29:49 +00:00
|
|
|
|
2021-04-27 15:15:46 +00:00
|
|
|
use glib::translate::{
|
|
|
|
from_glib, from_glib_full, from_glib_none, IntoGlib, ToGlibPtr, ToGlibPtrMut,
|
|
|
|
};
|
2018-04-01 08:30:03 +00:00
|
|
|
use gst::prelude::*;
|
2017-08-08 22:29:49 +00:00
|
|
|
|
2018-06-08 07:19:06 +00:00
|
|
|
use std::fmt;
|
2017-08-08 22:29:49 +00:00
|
|
|
use std::mem;
|
|
|
|
use std::ptr;
|
|
|
|
|
2021-06-01 13:15:59 +00:00
|
|
|
#[doc(alias = "GstAudioInfo")]
|
2020-11-21 18:17:20 +00:00
|
|
|
pub struct AudioInfo(ffi::GstAudioInfo, [crate::AudioChannelPosition; 64]);
|
2017-08-08 22:29:49 +00:00
|
|
|
|
2018-06-08 07:19:06 +00:00
|
|
|
impl fmt::Debug for AudioInfo {
|
2020-11-28 11:33:46 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-06-08 07:19:06 +00:00
|
|
|
f.debug_struct("AudioInfo")
|
2019-07-03 12:32:43 +00:00
|
|
|
.field("format-info", &self.format_info())
|
2018-06-08 07:19:06 +00:00
|
|
|
.field("rate", &self.rate())
|
|
|
|
.field("channels", &self.channels())
|
|
|
|
.field("positions", &self.positions())
|
|
|
|
.field("flags", &self.flags())
|
|
|
|
.field("layout", &self.layout())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 15:43:29 +00:00
|
|
|
#[derive(Debug)]
|
2021-12-03 23:17:29 +00:00
|
|
|
#[must_use = "The builder must be built to be used"]
|
2017-08-08 22:29:49 +00:00
|
|
|
pub struct AudioInfoBuilder<'a> {
|
2020-11-21 18:17:20 +00:00
|
|
|
format: crate::AudioFormat,
|
2017-08-08 22:29:49 +00:00
|
|
|
rate: u32,
|
|
|
|
channels: u32,
|
2020-11-21 18:17:20 +00:00
|
|
|
positions: Option<&'a [crate::AudioChannelPosition]>,
|
|
|
|
flags: Option<crate::AudioFlags>,
|
|
|
|
layout: Option<crate::AudioLayout>,
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AudioInfoBuilder<'a> {
|
2021-12-03 23:17:29 +00:00
|
|
|
#[must_use = "The built AudioInfo must be used"]
|
2019-12-14 14:10:06 +00:00
|
|
|
pub fn build(self) -> Result<AudioInfo, glib::error::BoolError> {
|
2017-08-08 22:29:49 +00:00
|
|
|
unsafe {
|
2019-07-11 12:34:28 +00:00
|
|
|
let mut info = mem::MaybeUninit::uninit();
|
2017-08-10 11:39:57 +00:00
|
|
|
|
|
|
|
let positions = if let Some(p) = self.positions {
|
|
|
|
if p.len() != self.channels as usize || p.len() > 64 {
|
2020-12-17 22:38:06 +00:00
|
|
|
return Err(glib::bool_error!("Invalid positions length"));
|
2017-08-10 11:39:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let positions: [ffi::GstAudioChannelPosition; 64] = array_init::array_init(|i| {
|
|
|
|
if i >= self.channels as usize {
|
|
|
|
ffi::GST_AUDIO_CHANNEL_POSITION_INVALID
|
|
|
|
} else {
|
2021-04-27 15:15:46 +00:00
|
|
|
p[i].into_glib()
|
2020-11-21 18:17:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let valid: bool = from_glib(ffi::gst_audio_check_valid_channel_positions(
|
|
|
|
positions.as_ptr() as *mut _,
|
|
|
|
self.channels as i32,
|
2021-04-27 15:15:46 +00:00
|
|
|
true.into_glib(),
|
2020-11-21 18:17:20 +00:00
|
|
|
));
|
2017-08-10 11:39:57 +00:00
|
|
|
if !valid {
|
2020-12-17 22:38:06 +00:00
|
|
|
return Err(glib::bool_error!("channel positions are invalid"));
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
2017-08-10 11:39:57 +00:00
|
|
|
|
|
|
|
Some(positions)
|
|
|
|
} else {
|
|
|
|
None
|
2017-08-08 22:29:49 +00:00
|
|
|
};
|
|
|
|
|
2017-08-10 11:39:57 +00:00
|
|
|
let positions_ptr = positions
|
|
|
|
.as_ref()
|
|
|
|
.map(|p| p.as_ptr())
|
|
|
|
.unwrap_or(ptr::null());
|
2017-08-08 22:29:49 +00:00
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
ffi::gst_audio_info_set_format(
|
2019-07-11 12:34:28 +00:00
|
|
|
info.as_mut_ptr(),
|
2021-04-27 15:15:46 +00:00
|
|
|
self.format.into_glib(),
|
2017-08-08 22:29:49 +00:00
|
|
|
self.rate as i32,
|
|
|
|
self.channels as i32,
|
2018-03-15 08:46:49 +00:00
|
|
|
positions_ptr as *mut _,
|
2017-08-08 22:29:49 +00:00
|
|
|
);
|
|
|
|
|
2019-07-11 12:34:28 +00:00
|
|
|
let mut info = info.assume_init();
|
|
|
|
|
2017-08-08 22:29:49 +00:00
|
|
|
if info.finfo.is_null() || info.rate <= 0 || info.channels <= 0 {
|
2020-12-17 22:38:06 +00:00
|
|
|
return Err(glib::bool_error!("Failed to build AudioInfo"));
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(flags) = self.flags {
|
2021-04-27 15:15:46 +00:00
|
|
|
info.flags = flags.into_glib();
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(layout) = self.layout {
|
2021-04-27 15:15:46 +00:00
|
|
|
info.layout = layout.into_glib();
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:12:00 +00:00
|
|
|
let positions = array_init::array_init(|i| from_glib(info.position[i]));
|
2019-12-14 14:10:06 +00:00
|
|
|
Ok(AudioInfo(info, positions))
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn positions(self, positions: &'a [crate::AudioChannelPosition]) -> AudioInfoBuilder<'a> {
|
2017-08-08 22:29:49 +00:00
|
|
|
Self {
|
|
|
|
positions: Some(positions),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn flags(self, flags: crate::AudioFlags) -> Self {
|
2017-08-08 22:29:49 +00:00
|
|
|
Self {
|
|
|
|
flags: Some(flags),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn layout(self, layout: crate::AudioLayout) -> Self {
|
2017-08-08 22:29:49 +00:00
|
|
|
Self {
|
|
|
|
layout: Some(layout),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioInfo {
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn builder<'a>(
|
|
|
|
format: crate::AudioFormat,
|
|
|
|
rate: u32,
|
|
|
|
channels: u32,
|
|
|
|
) -> AudioInfoBuilder<'a> {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-08-08 22:29:49 +00:00
|
|
|
AudioInfoBuilder {
|
2018-07-20 07:21:06 +00:00
|
|
|
format,
|
|
|
|
rate,
|
|
|
|
channels,
|
2017-08-08 22:29:49 +00:00
|
|
|
positions: None,
|
|
|
|
flags: None,
|
|
|
|
layout: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 11:14:09 +00:00
|
|
|
pub fn is_valid(&self) -> bool {
|
|
|
|
!self.0.finfo.is_null() && self.0.channels > 0 && self.0.rate > 0 && self.0.bpf > 0
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_audio_info_from_caps")]
|
2021-04-29 20:49:26 +00:00
|
|
|
pub fn from_caps(caps: &gst::CapsRef) -> Result<Self, glib::error::BoolError> {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
2017-08-08 22:29:49 +00:00
|
|
|
unsafe {
|
2019-07-11 12:34:28 +00:00
|
|
|
let mut info = mem::MaybeUninit::uninit();
|
2020-11-21 18:17:20 +00:00
|
|
|
if from_glib(ffi::gst_audio_info_from_caps(
|
2019-07-11 12:34:28 +00:00
|
|
|
info.as_mut_ptr(),
|
2019-03-19 07:58:20 +00:00
|
|
|
caps.as_ptr(),
|
|
|
|
)) {
|
2019-07-11 12:34:28 +00:00
|
|
|
let info = info.assume_init();
|
2019-07-24 11:12:00 +00:00
|
|
|
let positions = array_init::array_init(|i| from_glib(info.position[i]));
|
2021-04-29 20:49:26 +00:00
|
|
|
Ok(Self(info, positions))
|
2017-08-08 22:29:49 +00:00
|
|
|
} else {
|
2020-12-17 22:38:06 +00:00
|
|
|
Err(glib::bool_error!("Failed to create AudioInfo from caps"))
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_audio_info_to_caps")]
|
2019-12-14 14:10:06 +00:00
|
|
|
pub fn to_caps(&self) -> Result<gst::Caps, glib::error::BoolError> {
|
|
|
|
unsafe {
|
2020-11-21 18:17:20 +00:00
|
|
|
let result = from_glib_full(ffi::gst_audio_info_to_caps(&self.0));
|
2019-12-14 14:10:06 +00:00
|
|
|
match result {
|
|
|
|
Some(c) => Ok(c),
|
2020-12-17 22:38:06 +00:00
|
|
|
None => Err(glib::bool_error!("Failed to create caps from AudioInfo")),
|
2019-12-14 14:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_audio_info_convert")]
|
2017-12-09 16:20:21 +00:00
|
|
|
pub fn convert<V: Into<gst::GenericFormattedValue>, U: gst::SpecificFormattedValue>(
|
2017-08-08 22:29:49 +00:00
|
|
|
&self,
|
2017-12-09 16:20:21 +00:00
|
|
|
src_val: V,
|
|
|
|
) -> Option<U> {
|
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
|
|
|
let src_val = src_val.into();
|
|
|
|
unsafe {
|
2019-07-11 12:34:28 +00:00
|
|
|
let mut dest_val = mem::MaybeUninit::uninit();
|
2020-11-21 18:17:20 +00:00
|
|
|
if from_glib(ffi::gst_audio_info_convert(
|
2017-12-09 16:20:21 +00:00
|
|
|
&self.0,
|
2021-04-27 15:15:46 +00:00
|
|
|
src_val.format().into_glib(),
|
2021-04-28 22:29:13 +00:00
|
|
|
src_val.into_raw_value(),
|
2021-04-27 15:15:46 +00:00
|
|
|
U::default_format().into_glib(),
|
2019-07-11 12:34:28 +00:00
|
|
|
dest_val.as_mut_ptr(),
|
2017-12-09 16:20:21 +00:00
|
|
|
)) {
|
2021-04-20 10:24:17 +00:00
|
|
|
Some(U::from_raw(U::default_format(), dest_val.assume_init()))
|
2017-12-09 16:20:21 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn convert_generic<V: Into<gst::GenericFormattedValue>>(
|
|
|
|
&self,
|
|
|
|
src_val: V,
|
2017-08-08 22:29:49 +00:00
|
|
|
dest_fmt: gst::Format,
|
2017-12-09 16:20:21 +00:00
|
|
|
) -> Option<gst::GenericFormattedValue> {
|
2017-08-30 11:39:09 +00:00
|
|
|
assert_initialized_main_thread!();
|
|
|
|
|
2017-12-09 16:20:21 +00:00
|
|
|
let src_val = src_val.into();
|
2017-08-08 22:29:49 +00:00
|
|
|
unsafe {
|
2019-07-11 12:34:28 +00:00
|
|
|
let mut dest_val = mem::MaybeUninit::uninit();
|
2020-11-21 18:17:20 +00:00
|
|
|
if from_glib(ffi::gst_audio_info_convert(
|
2017-08-08 22:29:49 +00:00
|
|
|
&self.0,
|
2021-04-27 15:15:46 +00:00
|
|
|
src_val.format().into_glib(),
|
2021-04-28 22:29:13 +00:00
|
|
|
src_val.into_raw_value(),
|
2021-04-27 15:15:46 +00:00
|
|
|
dest_fmt.into_glib(),
|
2019-07-11 12:34:28 +00:00
|
|
|
dest_val.as_mut_ptr(),
|
2017-08-08 22:29:49 +00:00
|
|
|
)) {
|
2019-07-11 12:34:28 +00:00
|
|
|
Some(gst::GenericFormattedValue::new(
|
|
|
|
dest_fmt,
|
|
|
|
dest_val.assume_init(),
|
|
|
|
))
|
2017-08-08 22:29:49 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn format(&self) -> crate::AudioFormat {
|
2020-06-07 11:14:09 +00:00
|
|
|
if self.0.finfo.is_null() {
|
2020-11-21 18:17:20 +00:00
|
|
|
return crate::AudioFormat::Unknown;
|
2020-06-07 11:14:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 22:29:49 +00:00
|
|
|
unsafe { from_glib((*self.0.finfo).format) }
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn format_info(&self) -> crate::AudioFormatInfo {
|
|
|
|
crate::AudioFormatInfo::from_format(self.format())
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn layout(&self) -> crate::AudioLayout {
|
2020-12-08 14:07:12 +00:00
|
|
|
unsafe { from_glib(self.0.layout) }
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn flags(&self) -> crate::AudioFlags {
|
2020-12-08 14:07:12 +00:00
|
|
|
unsafe { from_glib(self.0.flags) }
|
2017-08-09 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 22:29:49 +00:00
|
|
|
pub fn rate(&self) -> u32 {
|
|
|
|
self.0.rate as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channels(&self) -> u32 {
|
|
|
|
self.0.channels as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bpf(&self) -> u32 {
|
|
|
|
self.0.bpf as u32
|
|
|
|
}
|
|
|
|
|
2017-08-09 22:39:55 +00:00
|
|
|
pub fn bps(&self) -> u32 {
|
|
|
|
(self.format_info().depth() as u32) >> 3
|
|
|
|
}
|
|
|
|
|
2017-08-10 21:41:36 +00:00
|
|
|
pub fn depth(&self) -> u32 {
|
2017-08-09 22:39:55 +00:00
|
|
|
self.format_info().depth()
|
|
|
|
}
|
|
|
|
|
2017-08-10 21:41:36 +00:00
|
|
|
pub fn width(&self) -> u32 {
|
2017-08-09 22:39:55 +00:00
|
|
|
self.format_info().width()
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn endianness(&self) -> crate::AudioEndianness {
|
2017-08-09 22:39:55 +00:00
|
|
|
self.format_info().endianness()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_big_endian(&self) -> bool {
|
|
|
|
self.format_info().is_big_endian()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_little_endian(&self) -> bool {
|
|
|
|
self.format_info().is_little_endian()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_float(&self) -> bool {
|
|
|
|
self.format_info().is_float()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_integer(&self) -> bool {
|
|
|
|
self.format_info().is_integer()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_signed(&self) -> bool {
|
|
|
|
self.format_info().is_signed()
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn positions(&self) -> Option<&[crate::AudioChannelPosition]> {
|
2017-08-10 11:39:57 +00:00
|
|
|
if self.0.channels > 64 || self.is_unpositioned() {
|
|
|
|
return None;
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 11:39:57 +00:00
|
|
|
Some(&self.1[0..(self.0.channels as usize)])
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
2017-08-09 22:39:55 +00:00
|
|
|
|
|
|
|
pub fn is_unpositioned(&self) -> bool {
|
2020-11-21 18:17:20 +00:00
|
|
|
self.flags().contains(crate::AudioFlags::UNPOSITIONED)
|
2017-08-09 22:39:55 +00:00
|
|
|
}
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for AudioInfo {
|
|
|
|
fn clone(&self) -> Self {
|
2021-04-29 20:49:26 +00:00
|
|
|
unsafe { Self(ptr::read(&self.0), self.1) }
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for AudioInfo {
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_audio_info_is_equal")]
|
2017-08-08 22:29:49 +00:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
2020-11-21 18:17:20 +00:00
|
|
|
unsafe { from_glib(ffi::gst_audio_info_is_equal(&self.0, &other.0)) }
|
2017-08-08 22:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for AudioInfo {}
|
|
|
|
|
2017-08-15 14:02:49 +00:00
|
|
|
unsafe impl Send for AudioInfo {}
|
2018-09-22 07:56:25 +00:00
|
|
|
unsafe impl Sync for AudioInfo {}
|
2017-08-15 14:02:49 +00:00
|
|
|
|
2017-08-09 22:13:47 +00:00
|
|
|
impl glib::types::StaticType for AudioInfo {
|
|
|
|
fn static_type() -> glib::types::Type {
|
2020-11-21 18:17:20 +00:00
|
|
|
unsafe { glib::translate::from_glib(ffi::gst_audio_info_get_type()) }
|
2017-08-09 22:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 07:19:02 +00:00
|
|
|
impl glib::value::ValueType for AudioInfo {
|
|
|
|
type Type = Self;
|
|
|
|
}
|
|
|
|
|
2017-08-09 22:13:47 +00:00
|
|
|
#[doc(hidden)]
|
2021-04-20 07:19:02 +00:00
|
|
|
unsafe impl<'a> glib::value::FromValue<'a> for AudioInfo {
|
|
|
|
type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;
|
|
|
|
|
|
|
|
unsafe fn from_value(value: &'a glib::Value) -> Self {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
from_glib_none(
|
|
|
|
glib::gobject_ffi::g_value_get_boxed(value.to_glib_none().0) as *mut ffi::GstAudioInfo
|
|
|
|
)
|
2017-08-09 22:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2021-04-20 07:19:02 +00:00
|
|
|
impl glib::value::ToValue for AudioInfo {
|
|
|
|
fn to_value(&self) -> glib::Value {
|
2021-04-25 16:46:18 +00:00
|
|
|
let mut value = glib::Value::for_value_type::<Self>();
|
2021-04-20 07:19:02 +00:00
|
|
|
unsafe {
|
|
|
|
glib::gobject_ffi::g_value_set_boxed(
|
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
self.to_glib_none().0 as *mut _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value_type(&self) -> glib::Type {
|
|
|
|
Self::static_type()
|
2017-08-09 22:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2021-04-20 07:19:02 +00:00
|
|
|
impl glib::value::ToValueOptional for AudioInfo {
|
|
|
|
fn to_value_optional(s: Option<&Self>) -> glib::Value {
|
|
|
|
skip_assert_initialized!();
|
2021-04-25 16:46:18 +00:00
|
|
|
let mut value = glib::Value::for_value_type::<Self>();
|
2021-04-20 07:19:02 +00:00
|
|
|
unsafe {
|
|
|
|
glib::gobject_ffi::g_value_set_boxed(
|
|
|
|
value.to_glib_none_mut().0,
|
|
|
|
s.to_glib_none().0 as *mut _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value
|
2017-08-09 22:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl glib::translate::Uninitialized for AudioInfo {
|
|
|
|
unsafe fn uninitialized() -> Self {
|
|
|
|
mem::zeroed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl glib::translate::GlibPtrDefault for AudioInfo {
|
2020-11-21 18:17:20 +00:00
|
|
|
type GlibType = *mut ffi::GstAudioInfo;
|
2017-08-09 22:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 18:17:20 +00:00
|
|
|
impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstAudioInfo> for AudioInfo {
|
2021-04-29 20:49:26 +00:00
|
|
|
type Storage = &'a Self;
|
2017-08-09 22:13:47 +00:00
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstAudioInfo, Self> {
|
2017-08-09 22:13:47 +00:00
|
|
|
glib::translate::Stash(&self.0, self)
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
fn to_glib_full(&self) -> *const ffi::GstAudioInfo {
|
2017-08-09 22:13:47 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 18:17:20 +00:00
|
|
|
impl glib::translate::FromGlibPtrNone<*mut ffi::GstAudioInfo> for AudioInfo {
|
2017-08-09 22:13:47 +00:00
|
|
|
#[inline]
|
2020-11-21 18:17:20 +00:00
|
|
|
unsafe fn from_glib_none(ptr: *mut ffi::GstAudioInfo) -> Self {
|
2021-04-29 20:49:26 +00:00
|
|
|
Self(
|
2017-08-10 11:39:57 +00:00
|
|
|
ptr::read(ptr),
|
2019-07-24 11:12:00 +00:00
|
|
|
array_init::array_init(|i| from_glib((*ptr).position[i])),
|
2017-08-10 11:39:57 +00:00
|
|
|
)
|
2017-08-09 22:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2020-11-21 18:17:20 +00:00
|
|
|
impl glib::translate::FromGlibPtrFull<*mut ffi::GstAudioInfo> for AudioInfo {
|
2017-08-09 22:13:47 +00:00
|
|
|
#[inline]
|
2020-11-21 18:17:20 +00:00
|
|
|
unsafe fn from_glib_full(ptr: *mut ffi::GstAudioInfo) -> Self {
|
2017-08-09 22:13:47 +00:00
|
|
|
let info = from_glib_none(ptr);
|
2020-11-21 18:17:20 +00:00
|
|
|
glib::ffi::g_free(ptr as *mut _);
|
2017-08-09 22:13:47 +00:00
|
|
|
info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 22:29:49 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = AudioInfo::builder(crate::AudioFormat::S16le, 48000, 2)
|
2017-08-08 22:29:49 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-11-21 18:17:20 +00:00
|
|
|
assert_eq!(info.format(), crate::AudioFormat::S16le);
|
2017-08-08 22:29:49 +00:00
|
|
|
assert_eq!(info.rate(), 48000);
|
|
|
|
assert_eq!(info.channels(), 2);
|
|
|
|
assert_eq!(
|
2017-08-10 11:39:57 +00:00
|
|
|
&info.positions().unwrap(),
|
2017-08-08 22:29:49 +00:00
|
|
|
&[
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioChannelPosition::FrontLeft,
|
|
|
|
crate::AudioChannelPosition::FrontRight,
|
2017-08-08 22:29:49 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let positions = [
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioChannelPosition::RearLeft,
|
|
|
|
crate::AudioChannelPosition::RearRight,
|
2017-08-08 22:29:49 +00:00
|
|
|
];
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = AudioInfo::builder(crate::AudioFormat::S16le, 48000, 2)
|
2017-08-08 22:29:49 +00:00
|
|
|
.positions(&positions)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-11-21 18:17:20 +00:00
|
|
|
assert_eq!(info.format(), crate::AudioFormat::S16le);
|
2017-08-08 22:29:49 +00:00
|
|
|
assert_eq!(info.rate(), 48000);
|
|
|
|
assert_eq!(info.channels(), 2);
|
|
|
|
assert_eq!(
|
2017-08-10 11:39:57 +00:00
|
|
|
&info.positions().unwrap(),
|
2017-08-08 22:29:49 +00:00
|
|
|
&[
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioChannelPosition::RearLeft,
|
|
|
|
crate::AudioChannelPosition::RearRight,
|
2017-08-08 22:29:49 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_to_caps() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2021-10-24 15:57:15 +00:00
|
|
|
let caps = gst::Caps::builder("audio/x-raw")
|
|
|
|
.field("format", "S16LE")
|
|
|
|
.field("rate", 48000)
|
|
|
|
.field("channels", 2)
|
|
|
|
.field("layout", "interleaved")
|
|
|
|
.field("channel-mask", gst::Bitmask::new(0x3))
|
|
|
|
.build();
|
2017-08-08 22:29:49 +00:00
|
|
|
let info = AudioInfo::from_caps(&caps).unwrap();
|
2020-11-21 18:17:20 +00:00
|
|
|
assert_eq!(info.format(), crate::AudioFormat::S16le);
|
2017-08-08 22:29:49 +00:00
|
|
|
assert_eq!(info.rate(), 48000);
|
|
|
|
assert_eq!(info.channels(), 2);
|
|
|
|
assert_eq!(
|
2017-08-10 11:39:57 +00:00
|
|
|
&info.positions().unwrap(),
|
2017-08-08 22:29:49 +00:00
|
|
|
&[
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioChannelPosition::FrontLeft,
|
|
|
|
crate::AudioChannelPosition::FrontRight,
|
2017-08-08 22:29:49 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let caps2 = info.to_caps().unwrap();
|
|
|
|
assert_eq!(caps, caps2);
|
|
|
|
|
|
|
|
let info2 = AudioInfo::from_caps(&caps2).unwrap();
|
|
|
|
assert!(info == info2);
|
|
|
|
}
|
|
|
|
}
|