Add ChannelLayout::describe

Calls `av_channel_layout_describe` and returns a string description.
This commit is contained in:
Luke Street
2023-10-30 11:30:06 -04:00
parent 49b41b2840
commit da93bfbbd4
+30
View File
@@ -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<String, Error> {
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)),
}
}
}
}
}