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#[cfg(feature = "v1_18")]
69#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
70#[doc(alias = "gst_tracing_get_active_tracers")]
71pub fn active_tracers() -> glib::List<Tracer> {
72    assert_initialized_main_thread!();
73    unsafe { FromGlibPtrContainer::from_glib_full(ffi::gst_tracing_get_active_tracers()) }
74}
75
76#[cfg(feature = "v1_24")]
77#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
78#[doc(alias = "gst_util_filename_compare")]
79pub fn filename_compare(a: &std::path::Path, b: &std::path::Path) -> std::cmp::Ordering {
80    skip_assert_initialized!();
81    unsafe {
82        from_glib(ffi::gst_util_filename_compare(
83            a.to_glib_none().0,
84            b.to_glib_none().0,
85        ))
86    }
87}
88
89#[doc(alias = "gst_segtrap_is_enabled")]
90pub fn segtrap_is_enabled() -> bool {
91    skip_assert_initialized!();
92    unsafe { from_glib(ffi::gst_segtrap_is_enabled()) }
93}
94
95#[doc(alias = "gst_segtrap_set_enabled")]
96pub fn segtrap_set_enabled(enabled: bool) {
97    skip_assert_initialized!();
98
99    // Ensure this is called before GStreamer is initialized
100    if unsafe { ffi::gst_is_initialized() } == glib::ffi::GTRUE {
101        panic!("segtrap_set_enabled() must be called before gst::init()");
102    }
103
104    unsafe {
105        ffi::gst_segtrap_set_enabled(enabled.into_glib());
106    }
107}
108
109#[doc(alias = "gst_check_version")]
110pub fn check_version(major: u32, minor: u32, micro: u32) -> bool {
111    skip_assert_initialized!();
112
113    #[cfg(feature = "v1_28")]
114    {
115        crate::auto::functions::check_version(major, minor, micro)
116    }
117    #[cfg(not(feature = "v1_28"))]
118    {
119        let v = crate::auto::functions::version();
120        if v.0 != major {
121            return false;
122        }
123        if v.1 < minor {
124            return false;
125        }
126        if v.1 > minor {
127            return true;
128        }
129        if v.2 < micro {
130            return false;
131        }
132        true
133    }
134}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139
140    #[test]
141    fn test_calculate_linear_regression() {
142        crate::init().unwrap();
143
144        let values = [(0, 0), (1, 1), (2, 2), (3, 3)];
145
146        let (m_num, m_denom, b, xbase, _) = calculate_linear_regression(&values, None).unwrap();
147        assert_eq!((m_num, m_denom, b, xbase), (10, 10, 3, 3));
148
149        let mut temp = [(0, 0); 4];
150        let (m_num, m_denom, b, xbase, _) =
151            calculate_linear_regression(&values, Some(&mut temp)).unwrap();
152        assert_eq!((m_num, m_denom, b, xbase), (10, 10, 3, 3));
153    }
154
155    #[test]
156    fn test_segtrap_is_enabled() {
157        // Default should be enabled
158        assert!(segtrap_is_enabled());
159    }
160}