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    /// Return a `GstMemory` that wraps a dmabuf file descriptor.
47    /// ## `allocator`
48    /// allocator to be used for this memory
49    /// ## `fd`
50    /// dmabuf file descriptor
51    /// ## `size`
52    /// memory size
53    ///
54    /// # Returns
55    ///
56    /// a GstMemory based on `allocator`.
57    /// When the buffer will be released dmabuf allocator will close the `fd`.
58    /// The memory is only mmapped on [`gst::Buffer::map()`][crate::gst::Buffer::map()] request.
59    #[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    /// Return a `GstMemory` that wraps a dmabuf file descriptor.
75    /// ## `allocator`
76    /// allocator to be used for this memory
77    /// ## `fd`
78    /// dmabuf file descriptor
79    /// ## `size`
80    /// memory size
81    /// ## `flags`
82    /// extra [`FdMemoryFlags`][crate::FdMemoryFlags]
83    ///
84    /// # Returns
85    ///
86    /// a GstMemory based on `allocator`.
87    ///
88    /// When the buffer will be released the allocator will close the `fd` unless
89    /// the [`FdMemoryFlags::DONT_CLOSE`][crate::FdMemoryFlags::DONT_CLOSE] flag is specified.
90    /// The memory is only mmapped on `gst_buffer_mmap()` request.
91    #[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}