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