1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use std::ptr; |
4 | |
5 | use glib::{prelude::*, translate::*}; |
6 | |
7 | #[cfg (feature = "v1_18" )] |
8 | #[cfg_attr (docsrs, doc(cfg(feature = "v1_18" )))] |
9 | use crate::Tracer; |
10 | use crate::{ |
11 | auto::functions::parse_bin_from_description, Bin, Element, Object, ParseContext, ParseFlags, |
12 | }; |
13 | |
14 | pub fn parse_bin_from_description_with_name( |
15 | bin_description: &str, |
16 | ghost_unlinked_pads: bool, |
17 | bin_name: &str, |
18 | ) -> Result<Bin, glib::Error> { |
19 | skip_assert_initialized!(); |
20 | let bin: Bin = parse_bin_from_description(bin_description, ghost_unlinked_pads)?; |
21 | if !bin_name.is_empty() { |
22 | let obj: Object = bin.clone().upcast::<Object>(); |
23 | unsafe { |
24 | ffi::gst_object_set_name(object:obj.to_glib_none().0, name:bin_name.to_glib_none().0); |
25 | } |
26 | } |
27 | Ok(bin) |
28 | } |
29 | |
30 | #[doc (alias = "gst_parse_bin_from_description_full" )] |
31 | pub fn parse_bin_from_description_full( |
32 | bin_description: &str, |
33 | ghost_unlinked_pads: bool, |
34 | mut context: Option<&mut ParseContext>, |
35 | flags: ParseFlags, |
36 | ) -> Result<Element, glib::Error> { |
37 | skip_assert_initialized!(); |
38 | unsafe { |
39 | let mut error: *mut GError = ptr::null_mut(); |
40 | let ret: *mut GstElement = ffi::gst_parse_bin_from_description_full( |
41 | bin_description:bin_description.to_glib_none().0, |
42 | ghost_unlinked_pads:ghost_unlinked_pads.into_glib(), |
43 | context:context.to_glib_none_mut().0, |
44 | flags:flags.into_glib(), |
45 | &mut error, |
46 | ); |
47 | if error.is_null() { |
48 | Ok(from_glib_none(ptr:ret)) |
49 | } else { |
50 | Err(from_glib_full(ptr:error)) |
51 | } |
52 | } |
53 | } |
54 | |
55 | pub fn parse_bin_from_description_with_name_full( |
56 | bin_description: &str, |
57 | ghost_unlinked_pads: bool, |
58 | bin_name: &str, |
59 | context: Option<&mut ParseContext>, |
60 | flags: ParseFlags, |
61 | ) -> Result<Element, glib::Error> { |
62 | skip_assert_initialized!(); |
63 | let bin: Element = |
64 | parse_bin_from_description_full(bin_description, ghost_unlinked_pads, context, flags)?; |
65 | if !bin_name.is_empty() { |
66 | let obj: Object = bin.clone().upcast::<Object>(); |
67 | unsafe { |
68 | ffi::gst_object_set_name(object:obj.to_glib_none().0, name:bin_name.to_glib_none().0); |
69 | } |
70 | } |
71 | Ok(bin) |
72 | } |
73 | |
74 | #[doc (alias = "gst_parse_launch_full" )] |
75 | pub fn parse_launch_full( |
76 | pipeline_description: &str, |
77 | mut context: Option<&mut ParseContext>, |
78 | flags: ParseFlags, |
79 | ) -> Result<Element, glib::Error> { |
80 | assert_initialized_main_thread!(); |
81 | unsafe { |
82 | let mut error: *mut GError = ptr::null_mut(); |
83 | let ret: *mut GstElement = ffi::gst_parse_launch_full( |
84 | pipeline_description:pipeline_description.to_glib_none().0, |
85 | context:context.to_glib_none_mut().0, |
86 | flags:flags.into_glib(), |
87 | &mut error, |
88 | ); |
89 | if error.is_null() { |
90 | Ok(from_glib_none(ptr:ret)) |
91 | } else { |
92 | Err(from_glib_full(ptr:error)) |
93 | } |
94 | } |
95 | } |
96 | |
97 | #[doc (alias = "gst_parse_launchv_full" )] |
98 | pub fn parse_launchv_full( |
99 | argv: &[&str], |
100 | mut context: Option<&mut ParseContext>, |
101 | flags: ParseFlags, |
102 | ) -> Result<Element, glib::Error> { |
103 | assert_initialized_main_thread!(); |
104 | unsafe { |
105 | let mut error: *mut GError = ptr::null_mut(); |
106 | let ret: *mut GstElement = ffi::gst_parse_launchv_full( |
107 | argv:argv.to_glib_none().0, |
108 | context:context.to_glib_none_mut().0, |
109 | flags:flags.into_glib(), |
110 | &mut error, |
111 | ); |
112 | if error.is_null() { |
113 | Ok(from_glib_none(ptr:ret)) |
114 | } else { |
115 | Err(from_glib_full(ptr:error)) |
116 | } |
117 | } |
118 | } |
119 | |
120 | #[doc (alias = "gst_calculate_linear_regression" )] |
121 | pub fn calculate_linear_regression( |
122 | xy: &[(u64, u64)], |
123 | temp: Option<&mut [(u64, u64)]>, |
124 | ) -> Option<(u64, u64, u64, u64, f64)> { |
125 | skip_assert_initialized!(); |
126 | use std::mem; |
127 | |
128 | unsafe { |
129 | assert_eq!(mem::size_of::<u64>() * 2, mem::size_of::<(u64, u64)>()); |
130 | assert_eq!(mem::align_of::<u64>(), mem::align_of::<(u64, u64)>()); |
131 | assert!( |
132 | temp.as_ref() |
133 | .map(|temp| temp.len()) |
134 | .unwrap_or_else(|| xy.len()) |
135 | >= xy.len() |
136 | ); |
137 | |
138 | let mut m_num = mem::MaybeUninit::uninit(); |
139 | let mut m_denom = mem::MaybeUninit::uninit(); |
140 | let mut b = mem::MaybeUninit::uninit(); |
141 | let mut xbase = mem::MaybeUninit::uninit(); |
142 | let mut r_squared = mem::MaybeUninit::uninit(); |
143 | |
144 | let res = from_glib(ffi::gst_calculate_linear_regression( |
145 | xy.as_ptr() as *const u64, |
146 | temp.map(|temp| temp.as_mut_ptr() as *mut u64) |
147 | .unwrap_or(ptr::null_mut()), |
148 | xy.len() as u32, |
149 | m_num.as_mut_ptr(), |
150 | m_denom.as_mut_ptr(), |
151 | b.as_mut_ptr(), |
152 | xbase.as_mut_ptr(), |
153 | r_squared.as_mut_ptr(), |
154 | )); |
155 | if res { |
156 | Some(( |
157 | m_num.assume_init(), |
158 | m_denom.assume_init(), |
159 | b.assume_init(), |
160 | xbase.assume_init(), |
161 | r_squared.assume_init(), |
162 | )) |
163 | } else { |
164 | None |
165 | } |
166 | } |
167 | } |
168 | |
169 | #[cfg (feature = "v1_18" )] |
170 | #[cfg_attr (docsrs, doc(cfg(feature = "v1_18" )))] |
171 | #[doc (alias = "gst_tracing_get_active_tracers" )] |
172 | pub fn active_tracers() -> glib::List<Tracer> { |
173 | assert_initialized_main_thread!(); |
174 | unsafe { FromGlibPtrContainer::from_glib_full(ffi::gst_tracing_get_active_tracers()) } |
175 | } |
176 | |
177 | #[cfg (test)] |
178 | mod tests { |
179 | use super::*; |
180 | use crate::prelude::*; |
181 | |
182 | #[test ] |
183 | fn test_calculate_linear_regression() { |
184 | crate::init().unwrap(); |
185 | |
186 | let values = [(0, 0), (1, 1), (2, 2), (3, 3)]; |
187 | |
188 | let (m_num, m_denom, b, xbase, _) = calculate_linear_regression(&values, None).unwrap(); |
189 | assert_eq!((m_num, m_denom, b, xbase), (10, 10, 3, 3)); |
190 | |
191 | let mut temp = [(0, 0); 4]; |
192 | let (m_num, m_denom, b, xbase, _) = |
193 | calculate_linear_regression(&values, Some(&mut temp)).unwrap(); |
194 | assert_eq!((m_num, m_denom, b, xbase), (10, 10, 3, 3)); |
195 | } |
196 | |
197 | #[test ] |
198 | fn test_parse_bin_from_description_with_name() { |
199 | crate::init().unwrap(); |
200 | |
201 | let bin = |
202 | parse_bin_from_description_with_name("fakesrc ! fakesink" , false, "all_fake" ).unwrap(); |
203 | let name = bin.name(); |
204 | assert_eq!(name, "all_fake" ); |
205 | |
206 | let bin = parse_bin_from_description_with_name("fakesrc ! fakesink" , false, "" ).unwrap(); |
207 | let name = bin.name(); |
208 | assert_ne!(name, "" ); |
209 | } |
210 | } |
211 | |