gstreamer-audio/audio_info: Change functions from returning Option to Result

Partial work for:
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/issues/216
This commit is contained in:
Tony Jinwoo Ahn 2019-12-14 14:10:06 +00:00 committed by Sebastian Dröge
parent 90854eb047
commit c563fb6f44
2 changed files with 18 additions and 11 deletions

View file

@ -180,7 +180,8 @@ mod fir_filter {
_element: &gst_base::BaseTransform, _element: &gst_base::BaseTransform,
caps: &gst::Caps, caps: &gst::Caps,
) -> Option<usize> { ) -> Option<usize> {
gst_audio::AudioInfo::from_caps(caps).map(|info| info.bpf() as usize) let audio_info = gst_audio::AudioInfo::from_caps(caps).ok();
audio_info.map(|info| info.bpf() as usize)
} }
// Called when shutting down the element so we can release all stream-related state // Called when shutting down the element so we can release all stream-related state

View file

@ -49,13 +49,13 @@ pub struct AudioInfoBuilder<'a> {
} }
impl<'a> AudioInfoBuilder<'a> { impl<'a> AudioInfoBuilder<'a> {
pub fn build(self) -> Option<AudioInfo> { pub fn build(self) -> Result<AudioInfo, glib::error::BoolError> {
unsafe { unsafe {
let mut info = mem::MaybeUninit::uninit(); let mut info = mem::MaybeUninit::uninit();
let positions = if let Some(p) = self.positions { let positions = if let Some(p) = self.positions {
if p.len() != self.channels as usize || p.len() > 64 { if p.len() != self.channels as usize || p.len() > 64 {
return None; return Err(glib_bool_error!("Invalid positions length"));
} }
let positions: [gst_audio_sys::GstAudioChannelPosition; 64] = let positions: [gst_audio_sys::GstAudioChannelPosition; 64] =
@ -74,7 +74,7 @@ impl<'a> AudioInfoBuilder<'a> {
true.to_glib(), true.to_glib(),
)); ));
if !valid { if !valid {
return None; return Err(glib_bool_error!("channel positions are invalid"));
} }
Some(positions) Some(positions)
@ -98,7 +98,7 @@ impl<'a> AudioInfoBuilder<'a> {
let mut info = info.assume_init(); let mut info = info.assume_init();
if info.finfo.is_null() || info.rate <= 0 || info.channels <= 0 { if info.finfo.is_null() || info.rate <= 0 || info.channels <= 0 {
return None; return Err(glib_bool_error!("Failed to build AudioInfo"));
} }
if let Some(flags) = self.flags { if let Some(flags) = self.flags {
@ -110,7 +110,7 @@ impl<'a> AudioInfoBuilder<'a> {
} }
let positions = array_init::array_init(|i| from_glib(info.position[i])); let positions = array_init::array_init(|i| from_glib(info.position[i]));
Some(AudioInfo(info, positions)) Ok(AudioInfo(info, positions))
} }
} }
@ -151,7 +151,7 @@ impl AudioInfo {
} }
} }
pub fn from_caps(caps: &gst::CapsRef) -> Option<Self> { pub fn from_caps(caps: &gst::CapsRef) -> Result<AudioInfo, glib::error::BoolError> {
skip_assert_initialized!(); skip_assert_initialized!();
unsafe { unsafe {
@ -162,15 +162,21 @@ impl AudioInfo {
)) { )) {
let info = info.assume_init(); let info = info.assume_init();
let positions = array_init::array_init(|i| from_glib(info.position[i])); let positions = array_init::array_init(|i| from_glib(info.position[i]));
Some(AudioInfo(info, positions)) Ok(AudioInfo(info, positions))
} else { } else {
None Err(glib_bool_error!("Failed to create AudioInfo from caps"))
} }
} }
} }
pub fn to_caps(&self) -> Option<gst::Caps> { pub fn to_caps(&self) -> Result<gst::Caps, glib::error::BoolError> {
unsafe { from_glib_full(gst_audio_sys::gst_audio_info_to_caps(&self.0)) } unsafe {
let result = from_glib_full(gst_audio_sys::gst_audio_info_to_caps(&self.0));
match result {
Some(c) => Ok(c),
None => Err(glib_bool_error!("Failed to create caps from AudioInfo")),
}
}
} }
pub fn convert<V: Into<gst::GenericFormattedValue>, U: gst::SpecificFormattedValue>( pub fn convert<V: Into<gst::GenericFormattedValue>, U: gst::SpecificFormattedValue>(