gstreamer_allocators/
fd_allocator.rs

1use std::{fmt, os::unix::prelude::RawFd};
2
3use glib::{prelude::*, translate::*};
4use gst::{Memory, MemoryRef};
5
6use crate::{ffi, FdAllocator, FdMemoryFlags};
7
8gst::memory_object_wrapper!(
9    FdMemory,
10    FdMemoryRef,
11    gst::ffi::GstMemory,
12    |mem: &gst::MemoryRef| { unsafe { from_glib(ffi::gst_is_fd_memory(mem.as_mut_ptr())) } },
13    Memory,
14    MemoryRef,
15);
16
17impl fmt::Debug for FdMemory {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        FdMemoryRef::fmt(self, f)
20    }
21}
22
23impl fmt::Debug for FdMemoryRef {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        f.debug_struct("FdMemory")
26            .field("ptr", &self.as_ptr())
27            .field("allocator", &self.allocator())
28            .field("parent", &self.parent())
29            .field("maxsize", &self.maxsize())
30            .field("align", &self.align())
31            .field("offset", &self.offset())
32            .field("size", &self.size())
33            .field("flags", &self.flags())
34            .field("fd", &self.fd())
35            .finish()
36    }
37}
38
39impl FdMemoryRef {
40    #[doc(alias = "gst_fd_memory_get_fd")]
41    pub fn fd(&self) -> RawFd {
42        skip_assert_initialized!();
43        unsafe { ffi::gst_fd_memory_get_fd(self.as_mut_ptr()) }
44    }
45}
46
47impl FdAllocator {
48    /// Return a `GstMemory` that wraps a generic file descriptor.
49    /// ## `allocator`
50    /// allocator to be used for this memory
51    /// ## `fd`
52    /// file descriptor
53    /// ## `size`
54    /// memory size
55    /// ## `flags`
56    /// extra [`FdMemoryFlags`][crate::FdMemoryFlags]
57    ///
58    /// # Returns
59    ///
60    /// a GstMemory based on `allocator`.
61    /// When the buffer will be released the allocator will close the `fd` unless
62    /// the [`FdMemoryFlags::DONT_CLOSE`][crate::FdMemoryFlags::DONT_CLOSE] flag is specified.
63    /// The memory is only mmapped on [`gst::Buffer::map()`][crate::gst::Buffer::map()] request.
64    #[doc(alias = "gst_fd_allocator_alloc")]
65    pub unsafe fn alloc(
66        &self,
67        fd: RawFd,
68        size: usize,
69        flags: FdMemoryFlags,
70    ) -> Result<gst::Memory, glib::BoolError> {
71        skip_assert_initialized!();
72        Option::<_>::from_glib_full(ffi::gst_fd_allocator_alloc(
73            self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
74            fd,
75            size,
76            flags.into_glib(),
77        ))
78        .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
79    }
80
81    /// Return a `GstMemory` that wraps a generic file descriptor.
82    /// ## `allocator`
83    /// allocator to be used for this memory
84    /// ## `fd`
85    /// file descriptor
86    /// ## `maxsize`
87    /// the total size of the memory represented by `fd`
88    /// ## `offset`
89    /// the offset of valid data in the memory
90    /// ## `size`
91    /// the size of valid data in the memory
92    /// ## `flags`
93    /// extra [`FdMemoryFlags`][crate::FdMemoryFlags]
94    ///
95    /// # Returns
96    ///
97    /// a GstMemory based on `allocator`.
98    /// When the buffer will be released the allocator will close the `fd` unless
99    /// the [`FdMemoryFlags::DONT_CLOSE`][crate::FdMemoryFlags::DONT_CLOSE] flag is specified.
100    /// The memory is only mmapped on [`gst::Buffer::map()`][crate::gst::Buffer::map()] request.
101    #[cfg(feature = "v1_28")]
102    #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
103    #[doc(alias = "gst_fd_allocator_alloc_full")]
104    pub unsafe fn alloc_full(
105        allocator: &impl IsA<gst::Allocator>,
106        fd: RawFd,
107        maxsize: usize,
108        offset: usize,
109        size: usize,
110        flags: FdMemoryFlags,
111    ) -> Option<gst::Memory> {
112        assert_initialized_main_thread!();
113        unsafe {
114            from_glib_full(ffi::gst_fd_allocator_alloc_full(
115                allocator.as_ref().to_glib_none().0,
116                fd,
117                maxsize,
118                offset,
119                size,
120                flags.into_glib(),
121            ))
122        }
123    }
124}