From f03ee95bf0726a4bf00559b8658b8ac6162ec84e Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Wed, 8 Feb 2023 16:41:53 +0100 Subject: [PATCH] livesync: Extract audio_info_from_caps And adjust it slightly so it never panics. Part-of: --- utils/livesync/src/livesync/imp.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/utils/livesync/src/livesync/imp.rs b/utils/livesync/src/livesync/imp.rs index f21decba..9fc22257 100644 --- a/utils/livesync/src/livesync/imp.rs +++ b/utils/livesync/src/livesync/imp.rs @@ -27,6 +27,15 @@ static CAT: Lazy = Lazy::new(|| { ) }); +fn audio_info_from_caps( + caps: &gst::CapsRef, +) -> Result, glib::BoolError> { + caps.structure(0) + .map_or(false, |s| s.has_name("audio/x-raw")) + .then(|| gst_audio::AudioInfo::from_caps(caps)) + .transpose() +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum BufferLateness { OnTime, @@ -631,13 +640,7 @@ impl LiveSync { gst::EventView::Caps(c) => { let caps = c.caps_owned(); - let audio_info = match caps - .structure(0) - .unwrap() - .has_name("audio/x-raw") - .then(|| gst_audio::AudioInfo::from_caps(&caps)) - .transpose() - { + let audio_info = match audio_info_from_caps(&caps) { Ok(ai) => ai, Err(e) => { gst::error!(CAT, imp: self, "Failed to parse audio caps: {}", e); @@ -1092,11 +1095,7 @@ impl LiveSync { MutexGuard::unlocked(&mut state, || self.srcpad.push_event(event)); state.srcresult?; - state.out_audio_info = caps - .structure(0) - .unwrap() - .has_name("audio/x-raw") - .then(|| gst_audio::AudioInfo::from_caps(&caps).unwrap()); + state.out_audio_info = audio_info_from_caps(&caps).unwrap(); } if let Some(segment) = segment {