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}