gstreamer_allocators/
dma_buf_allocator.rs
1use std::{
2 fmt,
3 os::unix::prelude::{IntoRawFd, RawFd},
4};
5
6use glib::{prelude::*, translate::*};
7use gst::{Memory, MemoryRef};
8
9#[cfg(feature = "v1_16")]
10#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
11use crate::FdMemoryFlags;
12use crate::{ffi, DmaBufAllocator, FdMemory, FdMemoryRef};
13
14gst::memory_object_wrapper!(
15 DmaBufMemory,
16 DmaBufMemoryRef,
17 gst::ffi::GstMemory,
18 |mem: &gst::MemoryRef| { unsafe { from_glib(ffi::gst_is_dmabuf_memory(mem.as_mut_ptr())) } },
19 FdMemory,
20 FdMemoryRef,
21 Memory,
22 MemoryRef
23);
24
25impl fmt::Debug for DmaBufMemory {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 DmaBufMemoryRef::fmt(self, f)
28 }
29}
30
31impl fmt::Debug for DmaBufMemoryRef {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 MemoryRef::fmt(self, f)
34 }
35}
36
37impl DmaBufMemoryRef {
38 #[doc(alias = "gst_dmabuf_memory_get_fd")]
39 pub fn fd(&self) -> RawFd {
40 skip_assert_initialized!();
41 unsafe { ffi::gst_dmabuf_memory_get_fd(self.as_mut_ptr()) }
42 }
43}
44
45impl DmaBufAllocator {
46 #[doc(alias = "gst_dmabuf_allocator_alloc")]
60 pub unsafe fn alloc<A: IntoRawFd>(
61 &self,
62 fd: A,
63 size: usize,
64 ) -> Result<gst::Memory, glib::BoolError> {
65 skip_assert_initialized!();
66 Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc(
67 self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
68 fd.into_raw_fd(),
69 size,
70 ))
71 .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
72 }
73
74 #[cfg(feature = "v1_16")]
92 #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
93 #[doc(alias = "gst_dmabuf_allocator_alloc_with_flags")]
94 pub unsafe fn alloc_with_flags(
95 &self,
96 fd: RawFd,
97 size: usize,
98 flags: FdMemoryFlags,
99 ) -> Result<gst::Memory, glib::BoolError> {
100 skip_assert_initialized!();
101 Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc_with_flags(
102 self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
103 fd,
104 size,
105 flags.into_glib(),
106 ))
107 .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
108 }
109}