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