Skip to main content

gstreamer/
functions.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ptr;
4
5use glib::translate::*;
6
7#[cfg(feature = "v1_18")]
8#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
9use crate::Tracer;
10
11// import only functions which do not have their own module as namespace
12#[cfg(feature = "v1_28")]
13pub use crate::auto::functions::call_async;
14pub use crate::auto::functions::{
15    main_executable_path, util_get_timestamp as get_timestamp, version, version_string,
16};
17use crate::ffi;
18
19#[doc(alias = "gst_calculate_linear_regression")]
20pub fn calculate_linear_regression(
21    xy: &[(u64, u64)],
22    temp: Option<&mut [(u64, u64)]>,
23) -> Option<(u64, u64, u64, u64, f64)> {
24    skip_assert_initialized!();
25    use std::mem;
26
27    unsafe {
28        assert_eq!(mem::size_of::<u64>() * 2, mem::size_of::<(u64, u64)>());
29        assert_eq!(mem::align_of::<u64>(), mem::align_of::<(u64, u64)>());
30        assert!(
31            temp.as_ref()
32                .map(|temp| temp.len())
33                .unwrap_or_else(|| xy.len())
34                >= xy.len()
35        );
36
37        let mut m_num = mem::MaybeUninit::uninit();
38        let mut m_denom = mem::MaybeUninit::uninit();
39        let mut b = mem::MaybeUninit::uninit();
40        let mut xbase = mem::MaybeUninit::uninit();
41        let mut r_squared = mem::MaybeUninit::uninit();
42
43        let res = from_glib(ffi::gst_calculate_linear_regression(
44            xy.as_ptr() as *const u64,
45            temp.map(|temp| temp.as_mut_ptr() as *mut u64)
46                .unwrap_or(ptr::null_mut()),
47            xy.len() as u32,
48            m_num.as_mut_ptr(),
49            m_denom.as_mut_ptr(),
50            b.as_mut_ptr(),
51            xbase.as_mut_ptr(),
52            r_squared.as_mut_ptr(),
53        ));
54        if res {
55            Some((
56                m_num.assume_init(),
57                m_denom.assume_init(),
58                b.assume_init(),
59                xbase.assume_init(),
60                r_squared.assume_init(),
61            ))
62        } else {
63            None
64        }
65    }
66}
67
68/// Get a list of all active tracer objects owned by the tracing framework for
69/// the entirety of the run-time of the process or till `gst_deinit()` is called.
70///
71/// # Returns
72///
73/// A `GList` of
74/// [`Tracer`][crate::Tracer] objects
75#[cfg(feature = "v1_18")]
76#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
77#[doc(alias = "gst_tracing_get_active_tracers")]
78pub fn active_tracers() -> glib::List<Tracer> {
79    assert_initialized_main_thread!();
80    unsafe { FromGlibPtrContainer::from_glib_full(ffi::gst_tracing_get_active_tracers()) }
81}
82
83#[cfg(feature = "v1_24")]
84#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
85#[doc(alias = "gst_util_filename_compare")]
86pub fn filename_compare(a: &std::path::Path, b: &std::path::Path) -> std::cmp::Ordering {
87    skip_assert_initialized!();
88    unsafe {
89        from_glib(ffi::gst_util_filename_compare(
90            a.to_glib_none().0,
91            b.to_glib_none().0,
92        ))
93    }
94}
95
96#[doc(alias = "gst_segtrap_is_enabled")]
97pub fn segtrap_is_enabled() -> bool {
98    skip_assert_initialized!();
99    unsafe { from_glib(ffi::gst_segtrap_is_enabled()) }
100}
101
102#[doc(alias = "gst_segtrap_set_enabled")]
103pub fn segtrap_set_enabled(enabled: bool) {
104    skip_assert_initialized!();
105
106    // Ensure this is called before GStreamer is initialized
107    if unsafe { ffi::gst_is_initialized() } == glib::ffi::GTRUE {
108        panic!("segtrap_set_enabled() must be called before gst::init()");
109    }
110
111    unsafe {
112        ffi::gst_segtrap_set_enabled(enabled.into_glib());
113    }
114}
115
116#[doc(alias = "gst_check_version")]
117pub fn check_version(major: u32, minor: u32, micro: u32) -> bool {
118    skip_assert_initialized!();
119
120    #[cfg(feature = "v1_28")]
121    {
122        crate::auto::functions::check_version(major, minor, micro)
123    }
124    #[cfg(not(feature = "v1_28"))]
125    {
126        let v = crate::auto::functions::version();
127        if v.0 != major {
128            return false;
129        }
130        if v.1 < minor {
131            return false;
132        }
133        if v.1 > minor {
134            return true;
135        }
136        if v.2 < micro {
137            return false;
138        }
139        true
140    }
141}
142
143#[cfg(test)]
144mod tests {
145    use super::*;
146
147    #[test]
148    fn test_calculate_linear_regression() {
149        crate::init().unwrap();
150
151        let values = [(0, 0), (1, 1), (2, 2), (3, 3)];
152
153        let (m_num, m_denom, b, xbase, _) = calculate_linear_regression(&values, None).unwrap();
154        assert_eq!((m_num, m_denom, b, xbase), (10, 10, 3, 3));
155
156        let mut temp = [(0, 0); 4];
157        let (m_num, m_denom, b, xbase, _) =
158            calculate_linear_regression(&values, Some(&mut temp)).unwrap();
159        assert_eq!((m_num, m_denom, b, xbase), (10, 10, 3, 3));
160    }
161
162    #[test]
163    fn test_segtrap_is_enabled() {
164        // Default should be enabled
165        assert!(segtrap_is_enabled());
166    }
167}