2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2020-06-07 11:53:38 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use std::{fmt, marker::PhantomData, mem, ops, ptr, slice};
|
2020-06-07 11:53:38 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use glib::translate::{from_glib, Borrowed, FromGlibPtrNone, ToGlibPtr};
|
2020-06-07 11:53:38 +00:00
|
|
|
|
|
|
|
pub enum Readable {}
|
|
|
|
pub enum Writable {}
|
|
|
|
|
|
|
|
pub struct AudioBuffer<T> {
|
|
|
|
// Has to be boxed because it contains self-references
|
2020-11-21 18:17:20 +00:00
|
|
|
audio_buffer: Box<ffi::GstAudioBuffer>,
|
2020-06-07 11:53:38 +00:00
|
|
|
buffer: Option<gst::Buffer>,
|
2020-11-21 18:17:20 +00:00
|
|
|
info: crate::AudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T> Send for AudioBuffer<T> {}
|
|
|
|
unsafe impl<T> Sync for AudioBuffer<T> {}
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for AudioBuffer<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("AudioBuffer")
|
|
|
|
.field("audio_buffer", &self.audio_buffer)
|
|
|
|
.field("buffer", &self.buffer)
|
|
|
|
.field("info", &self.info)
|
|
|
|
.field("phantom", &self.phantom)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AudioBuffer<T> {
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn info(&self) -> &crate::AudioInfo {
|
2020-06-07 11:53:38 +00:00
|
|
|
&self.info
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_buffer(mut self) -> gst::Buffer {
|
|
|
|
self.buffer.take().unwrap()
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn format(&self) -> crate::AudioFormat {
|
2020-06-07 11:53:38 +00:00
|
|
|
self.info().format()
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn format_info(&self) -> crate::AudioFormatInfo {
|
2020-06-07 11:53:38 +00:00
|
|
|
self.info().format_info()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channels(&self) -> u32 {
|
|
|
|
self.info().channels()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rate(&self) -> u32 {
|
|
|
|
self.info().rate()
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn layout(&self) -> crate::AudioLayout {
|
2020-06-07 11:53:38 +00:00
|
|
|
self.info().layout()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn width(&self) -> u32 {
|
|
|
|
self.info().width()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn depth(&self) -> u32 {
|
|
|
|
self.info().depth()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sample_stride(&self) -> u32 {
|
|
|
|
self.info().width() / 8
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bps(&self) -> u32 {
|
|
|
|
self.info().bps()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bpf(&self) -> u32 {
|
|
|
|
self.info().bpf()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn n_samples(&self) -> usize {
|
|
|
|
self.audio_buffer.n_samples
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn n_planes(&self) -> u32 {
|
|
|
|
self.audio_buffer.n_planes as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plane_size(&self) -> usize {
|
2022-12-13 09:28:00 +00:00
|
|
|
(self.n_samples() * self.sample_stride() as usize * self.channels() as usize)
|
2020-06-07 11:53:38 +00:00
|
|
|
/ self.n_planes() as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer(&self) -> &gst::BufferRef {
|
|
|
|
unsafe { gst::BufferRef::from_ptr(self.audio_buffer.buffer) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plane_data(&self, plane: u32) -> Result<&[u8], glib::BoolError> {
|
|
|
|
if plane >= self.n_planes() {
|
2020-12-17 22:38:06 +00:00
|
|
|
return Err(glib::bool_error!(
|
2020-11-21 18:17:20 +00:00
|
|
|
"Plane index higher than number of planes"
|
|
|
|
));
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
Ok(slice::from_raw_parts(
|
2020-07-26 15:21:06 +00:00
|
|
|
(*self.audio_buffer.planes.add(plane as usize)) as *const u8,
|
2020-06-07 11:53:38 +00:00
|
|
|
self.plane_size(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_audio_buffer_ref(&self) -> AudioBufferRef<&gst::BufferRef> {
|
|
|
|
let info = self.info.clone();
|
|
|
|
AudioBufferRef {
|
|
|
|
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&*self.audio_buffer)),
|
|
|
|
buffer: Some(self.buffer()),
|
|
|
|
info,
|
|
|
|
unmap: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn as_ptr(&self) -> *const ffi::GstAudioBuffer {
|
2020-06-07 11:53:38 +00:00
|
|
|
&*self.audio_buffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for AudioBuffer<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2020-11-21 18:17:20 +00:00
|
|
|
ffi::gst_audio_buffer_unmap(&mut *self.audio_buffer);
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioBuffer<Readable> {
|
|
|
|
pub fn from_buffer_readable(
|
|
|
|
buffer: gst::Buffer,
|
2020-11-21 18:17:20 +00:00
|
|
|
info: &crate::AudioInfo,
|
2021-04-29 20:49:26 +00:00
|
|
|
) -> Result<Self, gst::Buffer> {
|
2020-06-07 11:53:38 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
|
|
|
assert!(info.is_valid());
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut audio_buffer = Box::new(mem::MaybeUninit::zeroed().assume_init());
|
2020-11-21 18:17:20 +00:00
|
|
|
let res: bool = from_glib(ffi::gst_audio_buffer_map(
|
2020-06-07 11:53:38 +00:00
|
|
|
&mut *audio_buffer,
|
|
|
|
info.to_glib_none().0 as *mut _,
|
|
|
|
buffer.to_glib_none().0,
|
2020-11-21 18:17:20 +00:00
|
|
|
gst::ffi::GST_MAP_READ,
|
2020-06-07 11:53:38 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if !res {
|
|
|
|
Err(buffer)
|
|
|
|
} else {
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::from_glib_none(
|
|
|
|
&audio_buffer.info as *const _ as *mut ffi::GstAudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
);
|
2021-04-29 20:49:26 +00:00
|
|
|
Ok(Self {
|
2020-06-07 11:53:38 +00:00
|
|
|
audio_buffer,
|
|
|
|
buffer: Some(buffer),
|
|
|
|
info,
|
|
|
|
phantom: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioBuffer<Writable> {
|
|
|
|
pub fn from_buffer_writable(
|
|
|
|
buffer: gst::Buffer,
|
2020-11-21 18:17:20 +00:00
|
|
|
info: &crate::AudioInfo,
|
2021-04-29 20:49:26 +00:00
|
|
|
) -> Result<Self, gst::Buffer> {
|
2020-06-07 11:53:38 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
|
|
|
assert!(info.is_valid());
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut audio_buffer = Box::new(mem::MaybeUninit::zeroed().assume_init());
|
2020-11-21 18:17:20 +00:00
|
|
|
let res: bool = from_glib(ffi::gst_audio_buffer_map(
|
2020-06-07 11:53:38 +00:00
|
|
|
&mut *audio_buffer,
|
|
|
|
info.to_glib_none().0 as *mut _,
|
|
|
|
buffer.to_glib_none().0,
|
2020-11-21 18:17:20 +00:00
|
|
|
gst::ffi::GST_MAP_READ | gst::ffi::GST_MAP_WRITE,
|
2020-06-07 11:53:38 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if !res {
|
|
|
|
Err(buffer)
|
|
|
|
} else {
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::from_glib_none(
|
|
|
|
&audio_buffer.info as *const _ as *mut ffi::GstAudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
);
|
2021-04-29 20:49:26 +00:00
|
|
|
Ok(Self {
|
2020-06-07 11:53:38 +00:00
|
|
|
audio_buffer,
|
|
|
|
buffer: Some(buffer),
|
|
|
|
info,
|
|
|
|
phantom: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer_mut(&mut self) -> &mut gst::BufferRef {
|
|
|
|
unsafe { gst::BufferRef::from_mut_ptr(self.audio_buffer.buffer) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plane_data_mut(&mut self, plane: u32) -> Result<&mut [u8], glib::BoolError> {
|
|
|
|
if plane >= self.n_planes() {
|
2020-12-17 22:38:06 +00:00
|
|
|
return Err(glib::bool_error!(
|
2020-11-21 18:17:20 +00:00
|
|
|
"Plane index higher than number of planes"
|
|
|
|
));
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
Ok(slice::from_raw_parts_mut(
|
2020-07-26 15:21:06 +00:00
|
|
|
(*self.audio_buffer.planes.add(plane as usize)) as *mut u8,
|
2020-06-07 11:53:38 +00:00
|
|
|
self.plane_size(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_mut_audio_buffer_ref(&mut self) -> AudioBufferRef<&mut gst::BufferRef> {
|
|
|
|
let info = self.info.clone();
|
|
|
|
AudioBufferRef {
|
|
|
|
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::from(&mut *self.audio_buffer)),
|
|
|
|
buffer: Some(self.buffer_mut()),
|
|
|
|
info,
|
|
|
|
unmap: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut ffi::GstAudioBuffer {
|
2020-06-07 11:53:38 +00:00
|
|
|
&mut *self.audio_buffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum AudioBufferPtr {
|
2020-11-21 18:17:20 +00:00
|
|
|
Owned(Box<ffi::GstAudioBuffer>),
|
|
|
|
Borrowed(ptr::NonNull<ffi::GstAudioBuffer>),
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Deref for AudioBufferPtr {
|
2020-11-21 18:17:20 +00:00
|
|
|
type Target = ffi::GstAudioBuffer;
|
2020-06-07 11:53:38 +00:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
match self {
|
2022-08-09 13:20:08 +00:00
|
|
|
Self::Owned(ref b) => b,
|
2021-04-29 20:49:26 +00:00
|
|
|
Self::Borrowed(ref b) => unsafe { b.as_ref() },
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::DerefMut for AudioBufferPtr {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
match self {
|
2021-04-29 20:49:26 +00:00
|
|
|
Self::Owned(ref mut b) => &mut *b,
|
|
|
|
Self::Borrowed(ref mut b) => unsafe { b.as_mut() },
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AudioBufferRef<T> {
|
|
|
|
// Has to be boxed because it contains self-references
|
|
|
|
audio_buffer: AudioBufferPtr,
|
|
|
|
buffer: Option<T>,
|
2020-11-21 18:17:20 +00:00
|
|
|
info: crate::AudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
unmap: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> AudioBufferRef<T> {
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn info(&self) -> &crate::AudioInfo {
|
2020-06-07 11:53:38 +00:00
|
|
|
&self.info
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn format(&self) -> crate::AudioFormat {
|
2020-06-07 11:53:38 +00:00
|
|
|
self.info().format()
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn format_info(&self) -> crate::AudioFormatInfo {
|
2020-06-07 11:53:38 +00:00
|
|
|
self.info().format_info()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn channels(&self) -> u32 {
|
|
|
|
self.info().channels()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rate(&self) -> u32 {
|
|
|
|
self.info().rate()
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn layout(&self) -> crate::AudioLayout {
|
2020-06-07 11:53:38 +00:00
|
|
|
self.info().layout()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn width(&self) -> u32 {
|
|
|
|
self.info().width()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn depth(&self) -> u32 {
|
|
|
|
self.info().depth()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sample_stride(&self) -> u32 {
|
|
|
|
self.info().width() / 8
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bps(&self) -> u32 {
|
|
|
|
self.info().bps()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bpf(&self) -> u32 {
|
|
|
|
self.info().bpf()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn n_samples(&self) -> usize {
|
|
|
|
self.audio_buffer.n_samples
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn n_planes(&self) -> u32 {
|
|
|
|
self.audio_buffer.n_planes as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plane_size(&self) -> usize {
|
2022-12-13 09:28:00 +00:00
|
|
|
(self.n_samples() * self.sample_stride() as usize * self.channels() as usize)
|
2020-06-07 11:53:38 +00:00
|
|
|
/ self.n_planes() as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plane_data(&self, plane: u32) -> Result<&[u8], glib::BoolError> {
|
|
|
|
if plane >= self.n_planes() {
|
2020-12-17 22:38:06 +00:00
|
|
|
return Err(glib::bool_error!(
|
2020-11-21 18:17:20 +00:00
|
|
|
"Plane index higher than number of planes"
|
|
|
|
));
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 10:21:13 +00:00
|
|
|
if self.plane_size() == 0 {
|
|
|
|
return Ok(&[]);
|
|
|
|
}
|
|
|
|
|
2020-06-07 11:53:38 +00:00
|
|
|
unsafe {
|
|
|
|
Ok(slice::from_raw_parts(
|
2020-07-26 15:21:06 +00:00
|
|
|
(*self.audio_buffer.planes.add(plane as usize)) as *const u8,
|
2020-06-07 11:53:38 +00:00
|
|
|
self.plane_size(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn as_ptr(&self) -> *const ffi::GstAudioBuffer {
|
2020-06-07 11:53:38 +00:00
|
|
|
&*self.audio_buffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AudioBufferRef<&'a gst::BufferRef> {
|
2020-11-21 18:17:20 +00:00
|
|
|
pub unsafe fn from_glib_borrow(audio_buffer: *const ffi::GstAudioBuffer) -> Borrowed<Self> {
|
2020-06-07 11:53:38 +00:00
|
|
|
assert!(!audio_buffer.is_null());
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::from_glib_none(
|
|
|
|
&(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
);
|
|
|
|
let buffer = gst::BufferRef::from_ptr((*audio_buffer).buffer);
|
2021-04-29 20:49:26 +00:00
|
|
|
Borrowed::new(Self {
|
2020-06-07 11:53:38 +00:00
|
|
|
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked(
|
|
|
|
audio_buffer as *mut _,
|
|
|
|
)),
|
|
|
|
buffer: Some(buffer),
|
|
|
|
info,
|
|
|
|
unmap: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_buffer_ref_readable<'b>(
|
|
|
|
buffer: &'a gst::BufferRef,
|
2020-11-21 18:17:20 +00:00
|
|
|
info: &'b crate::AudioInfo,
|
2021-04-29 20:49:26 +00:00
|
|
|
) -> Result<Self, glib::BoolError> {
|
2020-06-07 11:53:38 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
|
|
|
assert!(info.is_valid());
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut audio_buffer = Box::new(mem::MaybeUninit::zeroed().assume_init());
|
2020-11-21 18:17:20 +00:00
|
|
|
let res: bool = from_glib(ffi::gst_audio_buffer_map(
|
2020-06-07 11:53:38 +00:00
|
|
|
&mut *audio_buffer,
|
|
|
|
info.to_glib_none().0 as *mut _,
|
|
|
|
buffer.as_mut_ptr(),
|
2020-11-21 18:17:20 +00:00
|
|
|
gst::ffi::GST_MAP_READ,
|
2020-06-07 11:53:38 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if !res {
|
2020-12-17 22:38:06 +00:00
|
|
|
Err(glib::bool_error!("Failed to map AudioBuffer"))
|
2020-06-07 11:53:38 +00:00
|
|
|
} else {
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::from_glib_none(
|
|
|
|
&audio_buffer.info as *const _ as *mut ffi::GstAudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
);
|
2021-04-29 20:49:26 +00:00
|
|
|
Ok(Self {
|
2020-06-07 11:53:38 +00:00
|
|
|
audio_buffer: AudioBufferPtr::Owned(audio_buffer),
|
|
|
|
buffer: Some(buffer),
|
|
|
|
info,
|
|
|
|
unmap: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer(&self) -> &gst::BufferRef {
|
|
|
|
self.buffer.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AudioBufferRef<&'a mut gst::BufferRef> {
|
2020-11-21 18:17:20 +00:00
|
|
|
pub unsafe fn from_glib_borrow_mut(audio_buffer: *mut ffi::GstAudioBuffer) -> Borrowed<Self> {
|
2020-06-07 11:53:38 +00:00
|
|
|
assert!(!audio_buffer.is_null());
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::from_glib_none(
|
|
|
|
&(*audio_buffer).info as *const _ as *mut ffi::GstAudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
);
|
|
|
|
let buffer = gst::BufferRef::from_mut_ptr((*audio_buffer).buffer);
|
2021-04-29 20:49:26 +00:00
|
|
|
Borrowed::new(Self {
|
2020-06-07 11:53:38 +00:00
|
|
|
audio_buffer: AudioBufferPtr::Borrowed(ptr::NonNull::new_unchecked(audio_buffer)),
|
|
|
|
buffer: Some(buffer),
|
|
|
|
info,
|
|
|
|
unmap: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_buffer_ref_writable<'b>(
|
|
|
|
buffer: &'a mut gst::BufferRef,
|
2020-11-21 18:17:20 +00:00
|
|
|
info: &'b crate::AudioInfo,
|
2021-04-29 20:49:26 +00:00
|
|
|
) -> Result<Self, glib::BoolError> {
|
2020-06-07 11:53:38 +00:00
|
|
|
skip_assert_initialized!();
|
|
|
|
|
|
|
|
assert!(info.is_valid());
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut audio_buffer = Box::new(mem::MaybeUninit::zeroed().assume_init());
|
2020-11-21 18:17:20 +00:00
|
|
|
let res: bool = from_glib(ffi::gst_audio_buffer_map(
|
2020-06-07 11:53:38 +00:00
|
|
|
&mut *audio_buffer,
|
|
|
|
info.to_glib_none().0 as *mut _,
|
|
|
|
buffer.as_mut_ptr(),
|
2020-11-21 18:17:20 +00:00
|
|
|
gst::ffi::GST_MAP_READ | gst::ffi::GST_MAP_WRITE,
|
2020-06-07 11:53:38 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
if !res {
|
2020-12-17 22:38:06 +00:00
|
|
|
Err(glib::bool_error!("Failed to map AudioBuffer"))
|
2020-06-07 11:53:38 +00:00
|
|
|
} else {
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::from_glib_none(
|
|
|
|
&audio_buffer.info as *const _ as *mut ffi::GstAudioInfo,
|
2020-06-07 11:53:38 +00:00
|
|
|
);
|
2021-04-29 20:49:26 +00:00
|
|
|
Ok(Self {
|
2020-06-07 11:53:38 +00:00
|
|
|
audio_buffer: AudioBufferPtr::Owned(audio_buffer),
|
|
|
|
buffer: Some(buffer),
|
|
|
|
info,
|
|
|
|
unmap: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer_mut(&mut self) -> &mut gst::BufferRef {
|
|
|
|
self.buffer.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn plane_data_mut(&mut self, plane: u32) -> Result<&mut [u8], glib::BoolError> {
|
|
|
|
if plane >= self.n_planes() {
|
2020-12-17 22:38:06 +00:00
|
|
|
return Err(glib::bool_error!(
|
2020-11-21 18:17:20 +00:00
|
|
|
"Plane index higher than number of planes"
|
|
|
|
));
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 10:21:13 +00:00
|
|
|
if self.plane_size() == 0 {
|
|
|
|
return Ok(&mut []);
|
|
|
|
}
|
|
|
|
|
2020-06-07 11:53:38 +00:00
|
|
|
unsafe {
|
|
|
|
Ok(slice::from_raw_parts_mut(
|
2020-07-26 15:21:06 +00:00
|
|
|
(*self.audio_buffer.planes.add(plane as usize)) as *mut u8,
|
2020-06-07 11:53:38 +00:00
|
|
|
self.plane_size(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut ffi::GstAudioBuffer {
|
2020-06-07 11:53:38 +00:00
|
|
|
&mut *self.audio_buffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ops::Deref for AudioBufferRef<&'a mut gst::BufferRef> {
|
|
|
|
type Target = AudioBufferRef<&'a gst::BufferRef>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2021-04-29 20:49:26 +00:00
|
|
|
unsafe { &*(self as *const Self as *const Self::Target) }
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T> Send for AudioBufferRef<T> {}
|
|
|
|
unsafe impl<T> Sync for AudioBufferRef<T> {}
|
|
|
|
|
|
|
|
impl<T> Drop for AudioBufferRef<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
if self.unmap {
|
2020-11-21 18:17:20 +00:00
|
|
|
ffi::gst_audio_buffer_unmap(&mut *self.audio_buffer);
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_read() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let buffer = gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
let buffer = AudioBuffer::from_buffer_readable(buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
{
|
|
|
|
let buffer = buffer.as_audio_buffer_ref();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_read_planar() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
|
|
|
.layout(crate::AudioLayout::NonInterleaved)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let mut buffer =
|
|
|
|
gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioMeta::add(buffer, &info, 48000, &[]).unwrap();
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
let buffer = AudioBuffer::from_buffer_readable(buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
{
|
|
|
|
let buffer = buffer.as_audio_buffer_ref();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_write() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let buffer = gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
let mut buffer = AudioBuffer::from_buffer_writable(buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut buffer = buffer.as_mut_audio_buffer_ref();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_write_planar() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
|
|
|
.layout(crate::AudioLayout::NonInterleaved)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let mut buffer =
|
|
|
|
gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioMeta::add(buffer, &info, 48000, &[]).unwrap();
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
let mut buffer = AudioBuffer::from_buffer_writable(buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut buffer = buffer.as_mut_audio_buffer_ref();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_ref_read() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let buffer = gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
let buffer = AudioBufferRef::from_buffer_ref_readable(&buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_ref_read_planar() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
|
|
|
.layout(crate::AudioLayout::NonInterleaved)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let mut buffer =
|
|
|
|
gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioMeta::add(buffer, &info, 48000, &[]).unwrap();
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
let buffer = AudioBufferRef::from_buffer_ref_readable(&buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
assert!(buffer.plane_data(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_ref_write() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let mut buffer =
|
|
|
|
gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
|
|
|
let mut buffer = AudioBufferRef::from_buffer_ref_writable(buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_err());
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_map_ref_write_planar() {
|
|
|
|
gst::init().unwrap();
|
|
|
|
|
2020-11-21 18:17:20 +00:00
|
|
|
let info = crate::AudioInfo::builder(crate::AUDIO_FORMAT_S16, 48000, 2)
|
|
|
|
.layout(crate::AudioLayout::NonInterleaved)
|
2020-06-07 11:53:38 +00:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let mut buffer =
|
|
|
|
gst::Buffer::with_size(info.rate() as usize * info.bpf() as usize).unwrap();
|
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-11-21 18:17:20 +00:00
|
|
|
crate::AudioMeta::add(buffer, &info, 48000, &[]).unwrap();
|
2020-06-07 11:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
|
|
|
let mut buffer = AudioBufferRef::from_buffer_ref_writable(buffer, &info).unwrap();
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
|
|
|
|
assert!(buffer.plane_data_mut(0).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(0).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.plane_data_mut(1).is_ok());
|
|
|
|
assert_eq!(buffer.plane_data_mut(1).unwrap().len(), 2 * 48000);
|
|
|
|
assert!(buffer.info() == &info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|