Skip to main content

gstreamer_allocators/
fd_allocator.rs

1use std::{fmt, os::unix::prelude::RawFd};
2
3use glib::{prelude::*, translate::*};
4use gst::{Memory, MemoryRef};
5
6use crate::{FdAllocator, FdMemoryFlags, ffi};
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        unsafe {
56            skip_assert_initialized!();
57            Option::<_>::from_glib_full(ffi::gst_fd_allocator_alloc(
58                self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
59                fd,
60                size,
61                flags.into_glib(),
62            ))
63            .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
64        }
65    }
66
67    #[cfg(feature = "v1_28")]
68    #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
69    #[doc(alias = "gst_fd_allocator_alloc_full")]
70    unsafe fn alloc_fd_full(
71        allocator: &impl IsA<gst::Allocator>,
72        fd: RawFd,
73        maxsize: usize,
74        offset: usize,
75        size: usize,
76        flags: FdMemoryFlags,
77    ) -> Option<gst::Memory> {
78        assert_initialized_main_thread!();
79        unsafe {
80            from_glib_full(ffi::gst_fd_allocator_alloc_full(
81                allocator.as_ref().to_glib_none().0,
82                fd,
83                maxsize,
84                offset,
85                size,
86                flags.into_glib(),
87            ))
88        }
89    }
90}
91
92impl<O: IsA<FdAllocator>> FdAllocatorExtManual for O {}