Skip to main content

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::{DmaBufAllocator, FdMemory, FdMemoryRef, ffi};
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
45pub trait DmaBufAllocatorExtManual: IsA<DmaBufAllocator> + 'static {
46    #[doc(alias = "gst_dmabuf_allocator_alloc")]
47    unsafe fn alloc_dmabuf<A: IntoRawFd>(
48        &self,
49        fd: A,
50        size: usize,
51    ) -> Result<gst::Memory, glib::BoolError> {
52        unsafe {
53            skip_assert_initialized!();
54            Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc(
55                self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
56                fd.into_raw_fd(),
57                size,
58            ))
59            .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
60        }
61    }
62
63    #[cfg(feature = "v1_16")]
64    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
65    #[doc(alias = "gst_dmabuf_allocator_alloc_with_flags")]
66    unsafe fn alloc_dmabuf_with_flags(
67        &self,
68        fd: RawFd,
69        size: usize,
70        flags: FdMemoryFlags,
71    ) -> Result<gst::Memory, glib::BoolError> {
72        unsafe {
73            skip_assert_initialized!();
74            Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc_with_flags(
75                self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
76                fd,
77                size,
78                flags.into_glib(),
79            ))
80            .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
81        }
82    }
83}
84
85impl<O: IsA<DmaBufAllocator>> DmaBufAllocatorExtManual for O {}