gstreamer_base/
base_src.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{mem, ptr};
4
5use glib::{prelude::*, translate::*};
6use gst::prelude::*;
7
8use crate::{ffi, BaseSrc};
9
10mod sealed {
11    pub trait Sealed {}
12    impl<T: super::IsA<super::BaseSrc>> Sealed for T {}
13}
14
15pub trait BaseSrcExtManual: sealed::Sealed + IsA<BaseSrc> + 'static {
16    /// Lets [`BaseSrc`][crate::BaseSrc] sub-classes to know the memory `allocator`
17    /// used by the base class and its `params`.
18    ///
19    /// Unref the `allocator` after usage.
20    ///
21    /// # Returns
22    ///
23    ///
24    /// ## `allocator`
25    /// the [`gst::Allocator`][crate::gst::Allocator]
26    /// used
27    ///
28    /// ## `params`
29    /// the [`gst::AllocationParams`][crate::gst::AllocationParams] of `allocator`
30    #[doc(alias = "get_allocator")]
31    #[doc(alias = "gst_base_src_get_allocator")]
32    fn allocator(&self) -> (Option<gst::Allocator>, gst::AllocationParams) {
33        unsafe {
34            let mut allocator = ptr::null_mut();
35            let mut params = mem::MaybeUninit::uninit();
36            ffi::gst_base_src_get_allocator(
37                self.as_ref().to_glib_none().0,
38                &mut allocator,
39                params.as_mut_ptr(),
40            );
41            (from_glib_full(allocator), params.assume_init().into())
42        }
43    }
44
45    #[doc(alias = "get_segment")]
46    fn segment(&self) -> gst::Segment {
47        unsafe {
48            let src: &ffi::GstBaseSrc = &*(self.as_ptr() as *const _);
49            let srcpad = self.src_pad();
50            let _guard = srcpad.stream_lock();
51            let _guard = self.as_ref().object_lock();
52            from_glib_none(&src.segment as *const _)
53        }
54    }
55
56    /// Query the source for the latency parameters. `live` will be [`true`] when `self` is
57    /// configured as a live source. `min_latency` and `max_latency` will be set
58    /// to the difference between the running time and the timestamp of the first
59    /// buffer.
60    ///
61    /// This function is mostly used by subclasses.
62    ///
63    /// # Returns
64    ///
65    /// [`true`] if the query succeeded.
66    ///
67    /// ## `live`
68    /// if the source is live
69    ///
70    /// ## `min_latency`
71    /// the min latency of the source
72    ///
73    /// ## `max_latency`
74    /// the max latency of the source
75    #[doc(alias = "gst_base_src_query_latency")]
76    fn query_latency(
77        &self,
78    ) -> Result<(bool, Option<gst::ClockTime>, Option<gst::ClockTime>), glib::BoolError> {
79        unsafe {
80            let mut live = mem::MaybeUninit::uninit();
81            let mut min_latency = mem::MaybeUninit::uninit();
82            let mut max_latency = mem::MaybeUninit::uninit();
83            let ret = from_glib(ffi::gst_base_src_query_latency(
84                self.as_ref().to_glib_none().0,
85                live.as_mut_ptr(),
86                min_latency.as_mut_ptr(),
87                max_latency.as_mut_ptr(),
88            ));
89            let live = live.assume_init();
90            let min_latency = min_latency.assume_init();
91            let max_latency = max_latency.assume_init();
92            if ret {
93                Ok((
94                    from_glib(live),
95                    from_glib(min_latency),
96                    from_glib(max_latency),
97                ))
98            } else {
99                Err(glib::bool_error!("Failed to query latency"))
100            }
101        }
102    }
103
104    fn src_pad(&self) -> &gst::Pad {
105        unsafe {
106            let elt = &*(self.as_ptr() as *const ffi::GstBaseSrc);
107            &*(&elt.srcpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
108        }
109    }
110}
111
112impl<O: IsA<BaseSrc>> BaseSrcExtManual for O {}