From da93bfbbd43bd4b783e2788cf58209b7a56f044d Mon Sep 17 00:00:00 2001 From: Luke Street Date: Mon, 30 Oct 2023 11:03:10 -0400 Subject: [PATCH] Add ChannelLayout::describe Calls `av_channel_layout_describe` and returns a string description. --- src/util/channel_layout.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/util/channel_layout.rs b/src/util/channel_layout.rs index 402d0b7..d375f56 100644 --- a/src/util/channel_layout.rs +++ b/src/util/channel_layout.rs @@ -1,5 +1,7 @@ use ffi::*; use libc::c_ulonglong; +use std::mem; +use Error; bitflags! { pub struct ChannelLayout: c_ulonglong { @@ -72,4 +74,32 @@ impl ChannelLayout { ChannelLayout::from_bits_truncate(av_get_default_channel_layout(number) as c_ulonglong) } } + + pub fn describe(&self) -> Result { + unsafe { + let mut av_channel_layout = mem::zeroed(); + match av_channel_layout_from_mask(&mut av_channel_layout, self.bits) { + v if v >= 0 => {} + e => return Err(Error::from(e)), + } + + let mut buf = vec![0u8; 64]; + loop { + match av_channel_layout_describe( + &av_channel_layout, + buf.as_mut_ptr() as *mut i8, + buf.len(), + ) { + v if v as usize > buf.len() => { + buf.resize(v as usize + 1, 0); + } + v if v >= 0 => { + buf.truncate(v as usize); + return Ok(String::from_utf8_unchecked(buf)); + } + e => return Err(Error::from(e)), + } + } + } + } }