gstreamer_base/functions.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::mem;
4
5pub use crate::auto::functions::*;
6use crate::ffi;
7use glib::{prelude::*, translate::*};
8
9/// Tries to find what type of data is contained in the given `data`, the
10/// assumption being that the data represents the beginning of the stream or
11/// file.
12///
13/// All available typefinders will be called on the data in order of rank. If
14/// a typefinding function returns a probability of [`gst::TypeFindProbability::Maximum`][crate::gst::TypeFindProbability::Maximum],
15/// typefinding is stopped immediately and the found caps will be returned
16/// right away. Otherwise, all available typefind functions will the tried,
17/// and the caps with the highest probability will be returned, or [`None`] if
18/// the content of `data` could not be identified.
19///
20/// Free-function: gst_caps_unref
21/// ## `obj`
22/// object doing the typefinding, or [`None`] (used for logging)
23/// ## `data`
24/// * a pointer with data to typefind
25///
26/// # Returns
27///
28/// the [`gst::Caps`][crate::gst::Caps] corresponding to the data,
29/// or [`None`] if no type could be found. The caller should free the caps
30/// returned with `gst_caps_unref()`.
31///
32/// ## `prob`
33/// location to store the probability of the found
34/// caps, or [`None`]
35#[doc(alias = "gst_type_find_helper_for_data")]
36pub fn type_find_helper_for_data(
37 obj: Option<&impl IsA<gst::Object>>,
38 data: impl AsRef<[u8]>,
39) -> Result<(gst::Caps, gst::TypeFindProbability), glib::error::BoolError> {
40 assert_initialized_main_thread!();
41 unsafe {
42 let mut prob = mem::MaybeUninit::uninit();
43 let data = data.as_ref();
44 let (ptr, len) = (data.as_ptr(), data.len());
45 let ret = ffi::gst_type_find_helper_for_data(
46 obj.map(|p| p.as_ref()).to_glib_none().0,
47 mut_override(ptr),
48 len,
49 prob.as_mut_ptr(),
50 );
51 if ret.is_null() {
52 Err(glib::bool_error!("No type could be found"))
53 } else {
54 Ok((from_glib_full(ret), from_glib(prob.assume_init())))
55 }
56 }
57}
58
59/// Tries to find what type of data is contained in the given `data`, the
60/// assumption being that the data represents the beginning of the stream or
61/// file.
62///
63/// All available typefinders will be called on the data in order of rank. If
64/// a typefinding function returns a probability of [`gst::TypeFindProbability::Maximum`][crate::gst::TypeFindProbability::Maximum],
65/// typefinding is stopped immediately and the found caps will be returned
66/// right away. Otherwise, all available typefind functions will the tried,
67/// and the caps with the highest probability will be returned, or [`None`] if
68/// the content of `data` could not be identified.
69///
70/// When `extension` is not [`None`], this function will first try the typefind
71/// functions for the given extension, which might speed up the typefinding
72/// in many cases.
73///
74/// Free-function: gst_caps_unref
75/// ## `obj`
76/// object doing the typefinding, or [`None`] (used for logging)
77/// ## `data`
78/// * a pointer with data to typefind
79/// ## `extension`
80/// extension of the media, or [`None`]
81///
82/// # Returns
83///
84/// the [`gst::Caps`][crate::gst::Caps] corresponding to the data,
85/// or [`None`] if no type could be found. The caller should free the caps
86/// returned with `gst_caps_unref()`.
87///
88/// ## `prob`
89/// location to store the probability of the found
90/// caps, or [`None`]
91#[cfg(feature = "v1_16")]
92#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
93#[doc(alias = "gst_type_find_helper_for_data_with_extension")]
94pub fn type_find_helper_for_data_with_extension(
95 obj: Option<&impl IsA<gst::Object>>,
96 data: impl AsRef<[u8]>,
97 extension: Option<&str>,
98) -> Result<(gst::Caps, gst::TypeFindProbability), glib::error::BoolError> {
99 assert_initialized_main_thread!();
100 unsafe {
101 let mut prob = mem::MaybeUninit::uninit();
102 let data = data.as_ref();
103 let (ptr, len) = (data.as_ptr(), data.len());
104 let ret = ffi::gst_type_find_helper_for_data_with_extension(
105 obj.map(|p| p.as_ref()).to_glib_none().0,
106 mut_override(ptr),
107 len,
108 extension.to_glib_none().0,
109 prob.as_mut_ptr(),
110 );
111 if ret.is_null() {
112 Err(glib::bool_error!("No type could be found"))
113 } else {
114 Ok((from_glib_full(ret), from_glib(prob.assume_init())))
115 }
116 }
117}
118
119/// Tries to find what type of data is contained in the given [`gst::Buffer`][crate::gst::Buffer], the
120/// assumption being that the buffer represents the beginning of the stream or
121/// file.
122///
123/// All available typefinders will be called on the data in order of rank. If
124/// a typefinding function returns a probability of [`gst::TypeFindProbability::Maximum`][crate::gst::TypeFindProbability::Maximum],
125/// typefinding is stopped immediately and the found caps will be returned
126/// right away. Otherwise, all available typefind functions will the tried,
127/// and the caps with the highest probability will be returned, or [`None`] if
128/// the content of the buffer could not be identified.
129///
130/// Free-function: gst_caps_unref
131/// ## `obj`
132/// object doing the typefinding, or [`None`] (used for logging)
133/// ## `buf`
134/// a [`gst::Buffer`][crate::gst::Buffer] with data to typefind
135///
136/// # Returns
137///
138/// the [`gst::Caps`][crate::gst::Caps] corresponding to the data,
139/// or [`None`] if no type could be found. The caller should free the caps
140/// returned with `gst_caps_unref()`.
141///
142/// ## `prob`
143/// location to store the probability of the found
144/// caps, or [`None`]
145#[doc(alias = "gst_type_find_helper_for_buffer")]
146pub fn type_find_helper_for_buffer<P: IsA<gst::Object>>(
147 obj: Option<&P>,
148 buf: &gst::BufferRef,
149) -> Result<(gst::Caps, gst::TypeFindProbability), glib::error::BoolError> {
150 skip_assert_initialized!();
151 unsafe {
152 let mut prob = mem::MaybeUninit::uninit();
153 let ret = ffi::gst_type_find_helper_for_buffer(
154 obj.map(|p| p.as_ref()).to_glib_none().0,
155 mut_override(buf.as_ptr()),
156 prob.as_mut_ptr(),
157 );
158 if ret.is_null() {
159 Err(glib::bool_error!("No type could be found"))
160 } else {
161 Ok((from_glib_full(ret), from_glib(prob.assume_init())))
162 }
163 }
164}
165
166/// Tries to find what type of data is contained in the given [`gst::Buffer`][crate::gst::Buffer], the
167/// assumption being that the buffer represents the beginning of the stream or
168/// file.
169///
170/// All available typefinders will be called on the data in order of rank. If
171/// a typefinding function returns a probability of [`gst::TypeFindProbability::Maximum`][crate::gst::TypeFindProbability::Maximum],
172/// typefinding is stopped immediately and the found caps will be returned
173/// right away. Otherwise, all available typefind functions will the tried,
174/// and the caps with the highest probability will be returned, or [`None`] if
175/// the content of the buffer could not be identified.
176///
177/// When `extension` is not [`None`], this function will first try the typefind
178/// functions for the given extension, which might speed up the typefinding
179/// in many cases.
180///
181/// Free-function: gst_caps_unref
182/// ## `obj`
183/// object doing the typefinding, or [`None`] (used for logging)
184/// ## `buf`
185/// a [`gst::Buffer`][crate::gst::Buffer] with data to typefind
186/// ## `extension`
187/// extension of the media, or [`None`]
188///
189/// # Returns
190///
191/// the [`gst::Caps`][crate::gst::Caps] corresponding to the data,
192/// or [`None`] if no type could be found. The caller should free the caps
193/// returned with `gst_caps_unref()`.
194///
195/// ## `prob`
196/// location to store the probability of the found
197/// caps, or [`None`]
198#[cfg(feature = "v1_16")]
199#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
200#[doc(alias = "gst_type_find_helper_for_buffer_with_extension")]
201pub fn type_find_helper_for_buffer_with_extension<P: IsA<gst::Object>>(
202 obj: Option<&P>,
203 buf: &gst::BufferRef,
204 extension: Option<&str>,
205) -> Result<(gst::Caps, gst::TypeFindProbability), glib::error::BoolError> {
206 skip_assert_initialized!();
207 unsafe {
208 let mut prob = mem::MaybeUninit::uninit();
209 let ret = ffi::gst_type_find_helper_for_buffer_with_extension(
210 obj.map(|p| p.as_ref()).to_glib_none().0,
211 mut_override(buf.as_ptr()),
212 extension.to_glib_none().0,
213 prob.as_mut_ptr(),
214 );
215 if ret.is_null() {
216 Err(glib::bool_error!("No type could be found"))
217 } else {
218 Ok((from_glib_full(ret), from_glib(prob.assume_init())))
219 }
220 }
221}
222
223/// Tries to find if type of media contained in the given [`gst::Buffer`][crate::gst::Buffer], matches
224/// `caps` specified, assumption being that the buffer represents the beginning
225/// of the stream or file.
226///
227/// Tries to find what type of data is contained in the given `data`, the
228/// assumption being that the data represents the beginning of the stream or
229/// file.
230///
231/// Only the typefinder matching the given caps will be called, if found. The
232/// caps with the highest probability will be returned, or [`None`] if the content
233/// of the `data` could not be identified.
234///
235/// Free-function: gst_caps_unref
236/// ## `obj`
237/// object doing the typefinding, or [`None`] (used for logging)
238/// ## `buf`
239/// a [`gst::Buffer`][crate::gst::Buffer] with data to typefind
240/// ## `caps`
241/// caps of the media
242///
243/// # Returns
244///
245/// the [`gst::Caps`][crate::gst::Caps] corresponding to the data,
246/// or [`None`] if no type could be found. The caller should free the caps
247/// returned with `gst_caps_unref()`.
248///
249/// ## `prob`
250/// location to store the probability of the found
251/// caps, or [`None`]
252#[cfg(feature = "v1_22")]
253#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
254#[doc(alias = "gst_type_find_helper_for_buffer_with_caps")]
255pub fn type_find_helper_for_buffer_with_caps(
256 obj: Option<&impl IsA<gst::Object>>,
257 buf: &gst::BufferRef,
258 caps: &gst::CapsRef,
259) -> (Option<gst::Caps>, gst::TypeFindProbability) {
260 skip_assert_initialized!();
261 unsafe {
262 let mut prob = mem::MaybeUninit::uninit();
263 let ret = from_glib_full(ffi::gst_type_find_helper_for_buffer_with_caps(
264 obj.map(|p| p.as_ref()).to_glib_none().0,
265 mut_override(buf.as_ptr()),
266 mut_override(caps.as_ptr()),
267 prob.as_mut_ptr(),
268 ));
269 (ret, from_glib(prob.assume_init()))
270 }
271}
272
273/// Tries to find if type of media contained in the given `data`, matches the
274/// `caps` specified, assumption being that the data represents the beginning
275/// of the stream or file.
276///
277/// Only the typefinder matching the given caps will be called, if found. The
278/// caps with the highest probability will be returned, or [`None`] if the content
279/// of the `data` could not be identified.
280///
281/// Free-function: gst_caps_unref
282/// ## `obj`
283/// object doing the typefinding, or [`None`] (used for logging)
284/// ## `data`
285/// a pointer with data to typefind
286/// ## `caps`
287/// caps of the media
288///
289/// # Returns
290///
291/// the [`gst::Caps`][crate::gst::Caps] corresponding to the data,
292/// or [`None`] if no type could be found. The caller should free the caps
293/// returned with `gst_caps_unref()`.
294///
295/// ## `prob`
296/// location to store the probability of the found
297/// caps, or [`None`]
298#[cfg(feature = "v1_22")]
299#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
300#[doc(alias = "gst_type_find_helper_for_data_with_caps")]
301pub fn type_find_helper_for_data_with_caps(
302 obj: Option<&impl IsA<gst::Object>>,
303 data: &[u8],
304 caps: &gst::CapsRef,
305) -> (Option<gst::Caps>, gst::TypeFindProbability) {
306 skip_assert_initialized!();
307 let size = data.len() as _;
308 unsafe {
309 let mut prob = mem::MaybeUninit::uninit();
310 let ret = from_glib_full(ffi::gst_type_find_helper_for_data_with_caps(
311 obj.map(|p| p.as_ref()).to_glib_none().0,
312 data.to_glib_none().0,
313 size,
314 mut_override(caps.as_ptr()),
315 prob.as_mut_ptr(),
316 ));
317 (ret, from_glib(prob.assume_init()))
318 }
319}
320
321/// Tries to find the best `GstTypeFindFactory` associated with `caps`.
322///
323/// The typefinder that can handle `caps` will be returned.
324///
325/// Free-function: g_list_free
326/// ## `obj`
327/// object doing the typefinding, or [`None`] (used for logging)
328/// ## `caps`
329/// caps of the media
330///
331/// # Returns
332///
333/// the list of `GstTypeFindFactory`
334/// corresponding to `caps`, or [`None`] if no typefinder could be
335/// found. Caller should free the returned list with `g_list_free()`
336/// and list elements with `gst_object_unref()`.
337#[cfg(feature = "v1_22")]
338#[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
339#[doc(alias = "gst_type_find_list_factories_for_caps")]
340pub fn type_find_list_factories_for_caps(
341 obj: Option<&impl IsA<gst::Object>>,
342 caps: &gst::CapsRef,
343) -> glib::List<gst::TypeFindFactory> {
344 skip_assert_initialized!();
345 unsafe {
346 glib::collections::List::from_glib_full(ffi::gst_type_find_list_factories_for_caps(
347 obj.map(|p| p.as_ref()).to_glib_none().0,
348 mut_override(caps.as_ptr()),
349 ))
350 }
351}