gstreamer_base/auto/adapter.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
4// DO NOT EDIT
5
6use crate::ffi;
7use glib::translate::*;
8
9glib::wrapper! {
10 /// ret == GST_FLOW_OK) {
11 /// const guint8 *data = gst_adapter_map (adapter, 512);
12 /// // use flowreturn as an error value
13 /// ret = my_library_foo (data);
14 /// gst_adapter_unmap (adapter);
15 /// gst_adapter_flush (adapter, 512);
16 /// }
17 /// return ret;
18 /// }
19 /// ]|
20 ///
21 /// For another example, a simple element inside GStreamer that uses [`Adapter`][crate::Adapter]
22 /// is the libvisual element.
23 ///
24 /// An element using [`Adapter`][crate::Adapter] in its sink pad chain function should ensure that
25 /// when the FLUSH_STOP event is received, that any queued data is cleared using
26 /// [`clear()`][Self::clear()]. Data should also be cleared or processed on EOS and
27 /// when changing state from [`gst::State::Paused`][crate::gst::State::Paused] to [`gst::State::Ready`][crate::gst::State::Ready].
28 ///
29 /// Also check the GST_BUFFER_FLAG_DISCONT flag on the buffer. Some elements might
30 /// need to clear the adapter after a discontinuity.
31 ///
32 /// The adapter will keep track of the timestamps of the buffers
33 /// that were pushed. The last seen timestamp before the current position
34 /// can be queried with [`prev_pts()`][Self::prev_pts()]. This function can
35 /// optionally return the number of bytes between the start of the buffer that
36 /// carried the timestamp and the current adapter position. The distance is
37 /// useful when dealing with, for example, raw audio samples because it allows
38 /// you to calculate the timestamp of the current adapter position by using the
39 /// last seen timestamp and the amount of bytes since. Additionally, the
40 /// [`prev_pts_at_offset()`][Self::prev_pts_at_offset()] can be used to determine the last
41 /// seen timestamp at a particular offset in the adapter.
42 ///
43 /// The adapter will also keep track of the offset of the buffers
44 /// (`GST_BUFFER_OFFSET`) that were pushed. The last seen offset before the
45 /// current position can be queried with [`prev_offset()`][Self::prev_offset()]. This function
46 /// can optionally return the number of bytes between the start of the buffer
47 /// that carried the offset and the current adapter position.
48 ///
49 /// Additionally the adapter also keeps track of the PTS, DTS and buffer offset
50 /// at the last discontinuity, which can be retrieved with
51 /// [`pts_at_discont()`][Self::pts_at_discont()], [`dts_at_discont()`][Self::dts_at_discont()] and
52 /// [`offset_at_discont()`][Self::offset_at_discont()]. The number of bytes that were consumed
53 /// since then can be queried with [`distance_from_discont()`][Self::distance_from_discont()].
54 ///
55 /// A last thing to note is that while [`Adapter`][crate::Adapter] is pretty optimized,
56 /// merging buffers still might be an operation that requires a ``malloc()`` and
57 /// ``memcpy()`` operation, and these operations are not the fastest. Because of
58 /// this, some functions like [`available_fast()`][Self::available_fast()] are provided to help
59 /// speed up such cases should you want to. To avoid repeated memory allocations,
60 /// `gst_adapter_copy()` can be used to copy data into a (statically allocated)
61 /// user provided buffer.
62 ///
63 /// [`Adapter`][crate::Adapter] is not MT safe. All operations on an adapter must be serialized by
64 /// the caller. This is not normally a problem, however, as the normal use case
65 /// of [`Adapter`][crate::Adapter] is inside one pad's chain function, in which case access is
66 /// serialized via the pad's STREAM_LOCK.
67 ///
68 /// Note that [`push()`][Self::push()] takes ownership of the buffer passed. Use
69 /// `gst_buffer_ref()` before pushing it into the adapter if you still want to
70 /// access the buffer later. The adapter will never modify the data in the
71 /// buffer pushed in it.
72 ///
73 /// # Implements
74 ///
75 /// [`trait@glib::ObjectExt`]
76 #[doc(alias = "GstAdapter")]
77 pub struct Adapter(Object<ffi::GstAdapter, ffi::GstAdapterClass>);
78
79 match fn {
80 type_ => || ffi::gst_adapter_get_type(),
81 }
82}
83
84impl Adapter {
85 /// Creates a new [`Adapter`][crate::Adapter]. Free with `g_object_unref()`.
86 ///
87 /// # Returns
88 ///
89 /// a new [`Adapter`][crate::Adapter]
90 #[doc(alias = "gst_adapter_new")]
91 pub fn new() -> Adapter {
92 assert_initialized_main_thread!();
93 unsafe { from_glib_full(ffi::gst_adapter_new()) }
94 }
95
96 /// Gets the maximum amount of bytes available, that is it returns the maximum
97 /// value that can be supplied to `gst_adapter_map()` without that function
98 /// returning [`None`].
99 ///
100 /// Calling `gst_adapter_map()` with the amount of bytes returned by this function
101 /// may require expensive operations (like copying the data into a temporary
102 /// buffer) in some cases.
103 ///
104 /// # Returns
105 ///
106 /// number of bytes available in `self`
107 #[doc(alias = "gst_adapter_available")]
108 pub fn available(&self) -> usize {
109 unsafe { ffi::gst_adapter_available(self.to_glib_none().0) }
110 }
111
112 /// Gets the maximum number of bytes that can be retrieved in a single map
113 /// operation without merging buffers.
114 ///
115 /// Calling `gst_adapter_map()` with the amount of bytes returned by this function
116 /// will never require any expensive operations (like copying the data into a
117 /// temporary buffer).
118 ///
119 /// # Returns
120 ///
121 /// number of bytes that are available in `self` without expensive
122 /// operations
123 #[doc(alias = "gst_adapter_available_fast")]
124 pub fn available_fast(&self) -> usize {
125 unsafe { ffi::gst_adapter_available_fast(self.to_glib_none().0) }
126 }
127
128 /// Removes all buffers from `self`.
129 #[doc(alias = "gst_adapter_clear")]
130 pub fn clear(&self) {
131 unsafe {
132 ffi::gst_adapter_clear(self.to_glib_none().0);
133 }
134 }
135
136 /// Get the distance in bytes since the last buffer with the
137 /// [`gst::BufferFlags::DISCONT`][crate::gst::BufferFlags::DISCONT] flag.
138 ///
139 /// The distance will be reset to 0 for all buffers with
140 /// [`gst::BufferFlags::DISCONT`][crate::gst::BufferFlags::DISCONT] on them, and then calculated for all other
141 /// following buffers based on their size.
142 ///
143 /// # Returns
144 ///
145 /// The offset. Can be `GST_BUFFER_OFFSET_NONE`.
146 #[doc(alias = "gst_adapter_distance_from_discont")]
147 pub fn distance_from_discont(&self) -> u64 {
148 unsafe { ffi::gst_adapter_distance_from_discont(self.to_glib_none().0) }
149 }
150
151 /// Get the DTS that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
152 /// flag, or GST_CLOCK_TIME_NONE.
153 ///
154 /// # Returns
155 ///
156 /// The DTS at the last discont or GST_CLOCK_TIME_NONE.
157 #[doc(alias = "gst_adapter_dts_at_discont")]
158 pub fn dts_at_discont(&self) -> Option<gst::ClockTime> {
159 unsafe { from_glib(ffi::gst_adapter_dts_at_discont(self.to_glib_none().0)) }
160 }
161
162 /// Get the offset that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
163 /// flag, or GST_BUFFER_OFFSET_NONE.
164 ///
165 /// # Returns
166 ///
167 /// The offset at the last discont or GST_BUFFER_OFFSET_NONE.
168 #[doc(alias = "gst_adapter_offset_at_discont")]
169 pub fn offset_at_discont(&self) -> u64 {
170 unsafe { ffi::gst_adapter_offset_at_discont(self.to_glib_none().0) }
171 }
172
173 /// Get the dts that was before the current byte in the adapter. When
174 /// `distance` is given, the amount of bytes between the dts and the current
175 /// position is returned.
176 ///
177 /// The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
178 /// the adapter is first created or when it is cleared. This also means that before
179 /// the first byte with a dts is added to the adapter, the dts
180 /// and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
181 ///
182 /// # Returns
183 ///
184 /// The previously seen dts.
185 ///
186 /// ## `distance`
187 /// pointer to location for distance, or [`None`]
188 #[doc(alias = "gst_adapter_prev_dts")]
189 pub fn prev_dts(&self) -> (Option<gst::ClockTime>, u64) {
190 unsafe {
191 let mut distance = std::mem::MaybeUninit::uninit();
192 let ret = from_glib(ffi::gst_adapter_prev_dts(
193 self.to_glib_none().0,
194 distance.as_mut_ptr(),
195 ));
196 (ret, distance.assume_init())
197 }
198 }
199
200 /// Get the dts that was before the byte at offset `offset` in the adapter. When
201 /// `distance` is given, the amount of bytes between the dts and the current
202 /// position is returned.
203 ///
204 /// The dts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
205 /// the adapter is first created or when it is cleared. This also means that before
206 /// the first byte with a dts is added to the adapter, the dts
207 /// and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
208 /// ## `offset`
209 /// the offset in the adapter at which to get timestamp
210 ///
211 /// # Returns
212 ///
213 /// The previously seen dts at given offset.
214 ///
215 /// ## `distance`
216 /// pointer to location for distance, or [`None`]
217 #[doc(alias = "gst_adapter_prev_dts_at_offset")]
218 pub fn prev_dts_at_offset(&self, offset: usize) -> (Option<gst::ClockTime>, u64) {
219 unsafe {
220 let mut distance = std::mem::MaybeUninit::uninit();
221 let ret = from_glib(ffi::gst_adapter_prev_dts_at_offset(
222 self.to_glib_none().0,
223 offset,
224 distance.as_mut_ptr(),
225 ));
226 (ret, distance.assume_init())
227 }
228 }
229
230 /// Get the offset that was before the current byte in the adapter. When
231 /// `distance` is given, the amount of bytes between the offset and the current
232 /// position is returned.
233 ///
234 /// The offset is reset to GST_BUFFER_OFFSET_NONE and the distance is set to 0
235 /// when the adapter is first created or when it is cleared. This also means that
236 /// before the first byte with an offset is added to the adapter, the offset
237 /// and distance returned are GST_BUFFER_OFFSET_NONE and 0 respectively.
238 ///
239 /// # Returns
240 ///
241 /// The previous seen offset.
242 ///
243 /// ## `distance`
244 /// pointer to a location for distance, or [`None`]
245 #[doc(alias = "gst_adapter_prev_offset")]
246 pub fn prev_offset(&self) -> (u64, u64) {
247 unsafe {
248 let mut distance = std::mem::MaybeUninit::uninit();
249 let ret = ffi::gst_adapter_prev_offset(self.to_glib_none().0, distance.as_mut_ptr());
250 (ret, distance.assume_init())
251 }
252 }
253
254 /// Get the pts that was before the current byte in the adapter. When
255 /// `distance` is given, the amount of bytes between the pts and the current
256 /// position is returned.
257 ///
258 /// The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
259 /// the adapter is first created or when it is cleared. This also means that before
260 /// the first byte with a pts is added to the adapter, the pts
261 /// and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
262 ///
263 /// # Returns
264 ///
265 /// The previously seen pts.
266 ///
267 /// ## `distance`
268 /// pointer to location for distance, or [`None`]
269 #[doc(alias = "gst_adapter_prev_pts")]
270 pub fn prev_pts(&self) -> (Option<gst::ClockTime>, u64) {
271 unsafe {
272 let mut distance = std::mem::MaybeUninit::uninit();
273 let ret = from_glib(ffi::gst_adapter_prev_pts(
274 self.to_glib_none().0,
275 distance.as_mut_ptr(),
276 ));
277 (ret, distance.assume_init())
278 }
279 }
280
281 /// Get the pts that was before the byte at offset `offset` in the adapter. When
282 /// `distance` is given, the amount of bytes between the pts and the current
283 /// position is returned.
284 ///
285 /// The pts is reset to GST_CLOCK_TIME_NONE and the distance is set to 0 when
286 /// the adapter is first created or when it is cleared. This also means that before
287 /// the first byte with a pts is added to the adapter, the pts
288 /// and distance returned are GST_CLOCK_TIME_NONE and 0 respectively.
289 /// ## `offset`
290 /// the offset in the adapter at which to get timestamp
291 ///
292 /// # Returns
293 ///
294 /// The previously seen pts at given offset.
295 ///
296 /// ## `distance`
297 /// pointer to location for distance, or [`None`]
298 #[doc(alias = "gst_adapter_prev_pts_at_offset")]
299 pub fn prev_pts_at_offset(&self, offset: usize) -> (Option<gst::ClockTime>, u64) {
300 unsafe {
301 let mut distance = std::mem::MaybeUninit::uninit();
302 let ret = from_glib(ffi::gst_adapter_prev_pts_at_offset(
303 self.to_glib_none().0,
304 offset,
305 distance.as_mut_ptr(),
306 ));
307 (ret, distance.assume_init())
308 }
309 }
310
311 /// Get the PTS that was on the last buffer with the GST_BUFFER_FLAG_DISCONT
312 /// flag, or GST_CLOCK_TIME_NONE.
313 ///
314 /// # Returns
315 ///
316 /// The PTS at the last discont or GST_CLOCK_TIME_NONE.
317 #[doc(alias = "gst_adapter_pts_at_discont")]
318 pub fn pts_at_discont(&self) -> Option<gst::ClockTime> {
319 unsafe { from_glib(ffi::gst_adapter_pts_at_discont(self.to_glib_none().0)) }
320 }
321}
322
323impl Default for Adapter {
324 fn default() -> Self {
325 Self::new()
326 }
327}