Skip to main content

gstreamer_base/
base_parse_frame.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{fmt, marker::PhantomData, ptr};
4
5use glib::translate::*;
6
7use crate::{BaseParse, BaseParseFrameFlags, ffi};
8
9pub struct BaseParseFrame<'a>(
10    ptr::NonNull<ffi::GstBaseParseFrame>,
11    PhantomData<&'a BaseParse>,
12);
13
14unsafe impl Send for BaseParseFrame<'_> {}
15unsafe impl Sync for BaseParseFrame<'_> {}
16
17#[derive(Debug)]
18pub enum Overhead {
19    None,
20    Frame,
21    Bytes(u32),
22}
23
24#[doc(hidden)]
25impl IntoGlib for Overhead {
26    type GlibType = i32;
27
28    #[inline]
29    fn into_glib(self) -> i32 {
30        match self {
31            Self::None => 0,
32            Self::Frame => -1,
33            Self::Bytes(b) => i32::try_from(b).expect("overhead is higher than i32::MAX"),
34        }
35    }
36}
37
38impl FromGlib<i32> for Overhead {
39    #[inline]
40    unsafe fn from_glib(val: i32) -> Self {
41        skip_assert_initialized!();
42        match val {
43            0 => Self::None,
44            1 => Self::Frame,
45            b if b > 0 => Self::Bytes(val as u32),
46            _ => panic!("overheader is lower than -1"),
47        }
48    }
49}
50
51#[doc(hidden)]
52impl<'a> ::glib::translate::ToGlibPtr<'a, *mut ffi::GstBaseParseFrame> for BaseParseFrame<'a> {
53    type Storage = PhantomData<&'a Self>;
54
55    #[inline]
56    fn to_glib_none(&'a self) -> ::glib::translate::Stash<'a, *mut ffi::GstBaseParseFrame, Self> {
57        Stash(self.0.as_ptr(), PhantomData)
58    }
59
60    fn to_glib_full(&self) -> *mut ffi::GstBaseParseFrame {
61        unimplemented!()
62    }
63}
64
65impl fmt::Debug for BaseParseFrame<'_> {
66    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67        let mut b = f.debug_struct("BaseParseFrame");
68
69        b.field("buffer", &self.buffer())
70            .field("output_buffer", &self.output_buffer())
71            .field("flags", &self.flags())
72            .field("offset", &self.offset())
73            .field("overhead", &self.overhead());
74
75        b.finish()
76    }
77}
78
79impl<'a> BaseParseFrame<'a> {
80    #[inline]
81    pub(crate) unsafe fn new(frame: *mut ffi::GstBaseParseFrame, _parse: &'a BaseParse) -> Self {
82        skip_assert_initialized!();
83
84        unsafe {
85            debug_assert!(!frame.is_null());
86            Self(ptr::NonNull::new_unchecked(frame), PhantomData)
87        }
88    }
89
90    #[doc(alias = "get_buffer")]
91    #[inline]
92    pub fn buffer(&self) -> Option<&gst::BufferRef> {
93        unsafe {
94            let ptr = (*self.to_glib_none().0).buffer;
95            if ptr.is_null() {
96                None
97            } else {
98                Some(gst::BufferRef::from_ptr(ptr))
99            }
100        }
101    }
102
103    #[doc(alias = "get_buffer_mut")]
104    pub fn buffer_mut(&mut self) -> Option<&mut gst::BufferRef> {
105        unsafe {
106            let ptr = (*self.to_glib_none().0).buffer;
107            if ptr.is_null() {
108                None
109            } else {
110                let writable: bool = from_glib(gst::ffi::gst_mini_object_is_writable(
111                    ptr as *const gst::ffi::GstMiniObject,
112                ));
113                assert!(writable);
114
115                Some(gst::BufferRef::from_mut_ptr(ptr))
116            }
117        }
118    }
119
120    #[doc(alias = "get_output_buffer")]
121    #[inline]
122    pub fn output_buffer(&self) -> Option<&gst::BufferRef> {
123        unsafe {
124            let ptr = (*self.to_glib_none().0).out_buffer;
125            if ptr.is_null() {
126                None
127            } else {
128                Some(gst::BufferRef::from_ptr(ptr))
129            }
130        }
131    }
132
133    #[doc(alias = "get_output_buffer_mut")]
134    pub fn output_buffer_mut(&mut self) -> Option<&mut gst::BufferRef> {
135        unsafe {
136            let ptr = (*self.to_glib_none().0).out_buffer;
137            if ptr.is_null() {
138                None
139            } else {
140                let writable: bool = from_glib(gst::ffi::gst_mini_object_is_writable(
141                    ptr as *const gst::ffi::GstMiniObject,
142                ));
143                assert!(writable);
144
145                Some(gst::BufferRef::from_mut_ptr(ptr))
146            }
147        }
148    }
149
150    pub fn set_output_buffer(&mut self, output_buffer: gst::Buffer) {
151        unsafe {
152            assert!(output_buffer.is_writable());
153            let prev = (*self.to_glib_none().0).out_buffer;
154
155            if !prev.is_null() {
156                gst::ffi::gst_mini_object_unref(prev as *mut gst::ffi::GstMiniObject);
157            }
158
159            (*self.to_glib_none().0).out_buffer = output_buffer.into_glib_ptr();
160        }
161    }
162
163    #[doc(alias = "get_flags")]
164    #[inline]
165    pub fn flags(&self) -> BaseParseFrameFlags {
166        let flags = unsafe { (*self.to_glib_none().0).flags };
167        BaseParseFrameFlags::from_bits_truncate(flags)
168    }
169
170    #[inline]
171    pub fn set_flags(&mut self, flags: BaseParseFrameFlags) {
172        unsafe { (*self.to_glib_none().0).flags |= flags.bits() }
173    }
174
175    #[inline]
176    pub fn unset_flags(&mut self, flags: BaseParseFrameFlags) {
177        unsafe { (*self.to_glib_none().0).flags &= !flags.bits() }
178    }
179
180    #[doc(alias = "get_offset")]
181    #[inline]
182    pub fn offset(&self) -> u64 {
183        unsafe { (*self.to_glib_none().0).offset }
184    }
185
186    #[doc(alias = "get_overhead")]
187    #[inline]
188    pub fn overhead(&self) -> Overhead {
189        unsafe { from_glib((*self.to_glib_none().0).overhead) }
190    }
191
192    #[inline]
193    pub fn set_overhead(&mut self, overhead: Overhead) {
194        unsafe {
195            (*self.to_glib_none().0).overhead = overhead.into_glib();
196        }
197    }
198}