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