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};
8
9pub struct BaseParseFrame<'a>(
10 ptr::NonNull<ffi::GstBaseParseFrame>,
11 PhantomData<&'a BaseParse>,
12);
13
14unsafe impl<'a> Send for BaseParseFrame<'a> {}
15unsafe impl<'a> Sync for BaseParseFrame<'a> {}
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: u32) => i32::try_from(b).expect(msg:"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: i32 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<*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<'a> fmt::Debug for BaseParseFrame<'a> {
66 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67 let mut b: DebugStruct<'_, '_> = f.debug_struct(name:"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(name:"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 debug_assert!(!frame.is_null());
84 Self(ptr::NonNull::new_unchecked(frame), PhantomData)
85 }
86
87 #[doc(alias = "get_buffer")]
88 #[inline]
89 pub fn buffer(&self) -> Option<&gst::BufferRef> {
90 unsafe {
91 let ptr = (*self.to_glib_none().0).buffer;
92 if ptr.is_null() {
93 None
94 } else {
95 Some(gst::BufferRef::from_ptr(ptr))
96 }
97 }
98 }
99
100 #[doc(alias = "get_buffer_mut")]
101 pub fn buffer_mut(&mut self) -> Option<&mut gst::BufferRef> {
102 unsafe {
103 let ptr = (*self.to_glib_none().0).buffer;
104 if ptr.is_null() {
105 None
106 } else {
107 let writable: bool = from_glib(gst::ffi::gst_mini_object_is_writable(
108 ptr as *const gst::ffi::GstMiniObject,
109 ));
110 assert!(writable);
111
112 Some(gst::BufferRef::from_mut_ptr(ptr))
113 }
114 }
115 }
116
117 #[doc(alias = "get_output_buffer")]
118 #[inline]
119 pub fn output_buffer(&self) -> Option<&gst::BufferRef> {
120 unsafe {
121 let ptr = (*self.to_glib_none().0).out_buffer;
122 if ptr.is_null() {
123 None
124 } else {
125 Some(gst::BufferRef::from_ptr(ptr))
126 }
127 }
128 }
129
130 #[doc(alias = "get_output_buffer_mut")]
131 pub fn output_buffer_mut(&mut self) -> Option<&mut gst::BufferRef> {
132 unsafe {
133 let ptr = (*self.to_glib_none().0).out_buffer;
134 if ptr.is_null() {
135 None
136 } else {
137 let writable: bool = from_glib(gst::ffi::gst_mini_object_is_writable(
138 ptr as *const gst::ffi::GstMiniObject,
139 ));
140 assert!(writable);
141
142 Some(gst::BufferRef::from_mut_ptr(ptr))
143 }
144 }
145 }
146
147 pub fn set_output_buffer(&mut self, output_buffer: gst::Buffer) {
148 unsafe {
149 assert!(output_buffer.is_writable());
150 let prev = (*self.to_glib_none().0).out_buffer;
151
152 if !prev.is_null() {
153 gst::ffi::gst_mini_object_unref(prev as *mut gst::ffi::GstMiniObject);
154 }
155
156 (*self.to_glib_none().0).out_buffer = output_buffer.into_glib_ptr();
157 }
158 }
159
160 #[doc(alias = "get_flags")]
161 #[inline]
162 pub fn flags(&self) -> BaseParseFrameFlags {
163 let flags = unsafe { (*self.to_glib_none().0).flags };
164 BaseParseFrameFlags::from_bits_truncate(flags)
165 }
166
167 #[inline]
168 pub fn set_flags(&mut self, flags: BaseParseFrameFlags) {
169 unsafe { (*self.to_glib_none().0).flags |= flags.bits() }
170 }
171
172 #[inline]
173 pub fn unset_flags(&mut self, flags: BaseParseFrameFlags) {
174 unsafe { (*self.to_glib_none().0).flags &= !flags.bits() }
175 }
176
177 #[doc(alias = "get_offset")]
178 #[inline]
179 pub fn offset(&self) -> u64 {
180 unsafe { (*self.to_glib_none().0).offset }
181 }
182
183 #[doc(alias = "get_overhead")]
184 #[inline]
185 pub fn overhead(&self) -> Overhead {
186 unsafe { from_glib((*self.to_glib_none().0).overhead) }
187 }
188
189 #[inline]
190 pub fn set_overhead(&mut self, overhead: Overhead) {
191 unsafe {
192 (*self.to_glib_none().0).overhead = overhead.into_glib();
193 }
194 }
195}
196