gstreamer_allocators/
dma_buf_allocator.rs1use 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
45pub trait DmaBufAllocatorExtManual: IsA<DmaBufAllocator> + 'static {
46 #[doc(alias = "gst_dmabuf_allocator_alloc")]
47 unsafe fn alloc_dmabuf<A: IntoRawFd>(
48 &self,
49 fd: A,
50 size: usize,
51 ) -> Result<gst::Memory, glib::BoolError> {
52 skip_assert_initialized!();
53 Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc(
54 self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
55 fd.into_raw_fd(),
56 size,
57 ))
58 .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
59 }
60
61 #[cfg(feature = "v1_16")]
62 #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
63 #[doc(alias = "gst_dmabuf_allocator_alloc_with_flags")]
64 unsafe fn alloc_dmabuf_with_flags(
65 &self,
66 fd: RawFd,
67 size: usize,
68 flags: FdMemoryFlags,
69 ) -> Result<gst::Memory, glib::BoolError> {
70 skip_assert_initialized!();
71 Option::<_>::from_glib_full(ffi::gst_dmabuf_allocator_alloc_with_flags(
72 self.unsafe_cast_ref::<gst::Allocator>().to_glib_none().0,
73 fd,
74 size,
75 flags.into_glib(),
76 ))
77 .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
78 }
79}
80
81impl<O: IsA<DmaBufAllocator>> DmaBufAllocatorExtManual for O {}