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