2022-02-26 15:16:35 +00:00
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
os::unix::prelude::{IntoRawFd, RawFd},
|
|
|
|
};
|
|
|
|
|
2024-02-03 08:13:51 +00:00
|
|
|
use glib::{prelude::*, translate::*};
|
2022-02-26 15:16:35 +00:00
|
|
|
use gst::{Memory, MemoryRef};
|
|
|
|
|
2023-05-04 05:55:48 +00:00
|
|
|
#[cfg(feature = "v1_16")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
|
2022-02-26 15:16:35 +00:00
|
|
|
use crate::FdMemoryFlags;
|
2024-06-02 08:48:53 +00:00
|
|
|
use crate::{ffi, DmaBufAllocator, FdMemory, FdMemoryRef};
|
2022-02-26 15:16:35 +00:00
|
|
|
|
|
|
|
gst::memory_object_wrapper!(
|
|
|
|
DmaBufMemory,
|
|
|
|
DmaBufMemoryRef,
|
|
|
|
gst::ffi::GstMemory,
|
|
|
|
|mem: &gst::MemoryRef| { unsafe { from_glib(ffi::gst_is_dmabuf_memory(mem.as_mut_ptr())) } },
|
|
|
|
FdMemory,
|
|
|
|
FdMemoryRef,
|
|
|
|
Memory,
|
|
|
|
MemoryRef
|
|
|
|
);
|
|
|
|
|
|
|
|
impl fmt::Debug for DmaBufMemory {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
DmaBufMemoryRef::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for DmaBufMemoryRef {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
MemoryRef::fmt(self, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DmaBufMemoryRef {
|
|
|
|
#[doc(alias = "gst_dmabuf_memory_get_fd")]
|
|
|
|
pub fn fd(&self) -> RawFd {
|
2022-12-25 10:47:02 +00:00
|
|
|
skip_assert_initialized!();
|
2022-02-26 15:16:35 +00:00
|
|
|
unsafe { ffi::gst_dmabuf_memory_get_fd(self.as_mut_ptr()) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DmaBufAllocator {
|
|
|
|
#[doc(alias = "gst_dmabuf_allocator_alloc")]
|
2022-10-18 11:16:04 +00:00
|
|
|
pub unsafe fn alloc<A: IntoRawFd>(
|
|
|
|
&self,
|
|
|
|
fd: A,
|
|
|
|
size: usize,
|
|
|
|
) -> Result<gst::Memory, glib::BoolError> {
|
2022-12-25 10:47:02 +00:00
|
|
|
skip_assert_initialized!();
|
2022-10-18 11:16:04 +00:00
|
|
|
Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc(
|
2022-02-26 15:16:35 +00:00
|
|
|
self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
|
|
|
|
fd.into_raw_fd(),
|
|
|
|
size,
|
|
|
|
))
|
2022-10-18 11:16:04 +00:00
|
|
|
.ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
|
2022-02-26 15:16:35 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 05:55:48 +00:00
|
|
|
#[cfg(feature = "v1_16")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
|
2022-02-26 15:16:35 +00:00
|
|
|
#[doc(alias = "gst_dmabuf_allocator_alloc_with_flags")]
|
|
|
|
pub unsafe fn alloc_with_flags(
|
|
|
|
&self,
|
|
|
|
fd: RawFd,
|
|
|
|
size: usize,
|
|
|
|
flags: FdMemoryFlags,
|
2022-10-18 11:16:04 +00:00
|
|
|
) -> Result<gst::Memory, glib::BoolError> {
|
2022-12-25 10:47:02 +00:00
|
|
|
skip_assert_initialized!();
|
2022-10-18 11:16:04 +00:00
|
|
|
Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc_with_flags(
|
2022-02-26 15:16:35 +00:00
|
|
|
self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
|
|
|
|
fd,
|
|
|
|
size,
|
|
|
|
flags.into_glib(),
|
|
|
|
))
|
2022-10-18 11:16:04 +00:00
|
|
|
.ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
|
2022-02-26 15:16:35 +00:00
|
|
|
}
|
|
|
|
}
|