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
47pub trait FdAllocatorExtManual: IsA<FdAllocator> + 'static {
48    #[doc(alias = "gst_fd_allocator_alloc")]
49    unsafe fn alloc_fd(
50        &self,
51        fd: RawFd,
52        size: usize,
53        flags: FdMemoryFlags,
54    ) -> Result<gst::Memory, glib::BoolError> {
55        skip_assert_initialized!();
56        Option::<_>::from_glib_full(ffi::gst_fd_allocator_alloc(
57            self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
58            fd,
59            size,
60            flags.into_glib(),
61        ))
62        .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
63    }
64
65    #[cfg(feature = "v1_28")]
66    #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
67    #[doc(alias = "gst_fd_allocator_alloc_full")]
68    unsafe fn alloc_fd_full(
69        allocator: &impl IsA<gst::Allocator>,
70        fd: RawFd,
71        maxsize: usize,
72        offset: usize,
73        size: usize,
74        flags: FdMemoryFlags,
75    ) -> Option<gst::Memory> {
76        assert_initialized_main_thread!();
77        unsafe {
78            from_glib_full(ffi::gst_fd_allocator_alloc_full(
79                allocator.as_ref().to_glib_none().0,
80                fd,
81                maxsize,
82                offset,
83                size,
84                flags.into_glib(),
85            ))
86        }
87    }
88}
89
90impl<O: IsA<FdAllocator>> FdAllocatorExtManual for O {}