Skip to main content

gstreamer/
parse.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::{prelude::*, translate::*};
6
7use crate::{Bin, Element, Object, ParseContext, ParseFlags, ffi};
8
9pub use crate::auto::functions::parse_bin_from_description as bin_from_description;
10pub use crate::auto::functions::parse_launch as launch;
11pub use crate::auto::functions::parse_launchv as launchv;
12#[cfg(feature = "v1_30")]
13#[cfg_attr(docsrs, doc(cfg(feature = "v1_30")))]
14pub use crate::auto::functions::parse_untrusted_report as untrusted_report;
15
16#[doc(alias = "gst_parse_bin_from_description_full")]
17pub fn bin_from_description_with_name(
18    bin_description: &str,
19    ghost_unlinked_pads: bool,
20    bin_name: &str,
21) -> Result<Bin, glib::Error> {
22    skip_assert_initialized!();
23    let bin = bin_from_description(bin_description, ghost_unlinked_pads)?;
24    if !bin_name.is_empty() {
25        let obj = bin.clone().upcast::<Object>();
26        unsafe {
27            ffi::gst_object_set_name(obj.to_glib_none().0, bin_name.to_glib_none().0);
28        }
29    }
30    Ok(bin)
31}
32
33#[doc(alias = "gst_parse_bin_from_description_full")]
34pub fn bin_from_description_full(
35    bin_description: &str,
36    ghost_unlinked_pads: bool,
37    mut context: Option<&mut ParseContext>,
38    flags: ParseFlags,
39) -> Result<Element, glib::Error> {
40    skip_assert_initialized!();
41    unsafe {
42        let mut error = ptr::null_mut();
43        let ret = ffi::gst_parse_bin_from_description_full(
44            bin_description.to_glib_none().0,
45            ghost_unlinked_pads.into_glib(),
46            context.to_glib_none_mut().0,
47            flags.into_glib(),
48            &mut error,
49        );
50        if error.is_null() {
51            Ok(from_glib_none(ret))
52        } else {
53            Err(from_glib_full(error))
54        }
55    }
56}
57
58#[doc(alias = "gst_parse_bin_from_description_full")]
59pub fn bin_from_description_with_name_full(
60    bin_description: &str,
61    ghost_unlinked_pads: bool,
62    bin_name: &str,
63    context: Option<&mut ParseContext>,
64    flags: ParseFlags,
65) -> Result<Element, glib::Error> {
66    skip_assert_initialized!();
67    let bin = bin_from_description_full(bin_description, ghost_unlinked_pads, context, flags)?;
68    if !bin_name.is_empty() {
69        let obj = bin.clone().upcast::<Object>();
70        unsafe {
71            ffi::gst_object_set_name(obj.to_glib_none().0, bin_name.to_glib_none().0);
72        }
73    }
74    Ok(bin)
75}
76
77#[doc(alias = "gst_parse_launch_full")]
78pub fn launch_full(
79    pipeline_description: &str,
80    mut context: Option<&mut ParseContext>,
81    flags: ParseFlags,
82) -> Result<Element, glib::Error> {
83    assert_initialized_main_thread!();
84    unsafe {
85        let mut error = ptr::null_mut();
86        let ret = ffi::gst_parse_launch_full(
87            pipeline_description.to_glib_none().0,
88            context.to_glib_none_mut().0,
89            flags.into_glib(),
90            &mut error,
91        );
92        if error.is_null() {
93            Ok(from_glib_none(ret))
94        } else {
95            Err(from_glib_full(error))
96        }
97    }
98}
99
100#[doc(alias = "gst_parse_launchv_full")]
101pub fn launchv_full(
102    argv: &[&str],
103    mut context: Option<&mut ParseContext>,
104    flags: ParseFlags,
105) -> Result<Element, glib::Error> {
106    assert_initialized_main_thread!();
107    unsafe {
108        let mut error = ptr::null_mut();
109        let ret = ffi::gst_parse_launchv_full(
110            argv.to_glib_none().0,
111            context.to_glib_none_mut().0,
112            flags.into_glib(),
113            &mut error,
114        );
115        if error.is_null() {
116            Ok(from_glib_none(ret))
117        } else {
118            Err(from_glib_full(error))
119        }
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126    use crate::prelude::*;
127
128    #[test]
129    fn test_parse_bin_from_description_with_name() {
130        crate::init().unwrap();
131
132        let bin = bin_from_description_with_name("fakesrc ! fakesink", false, "all_fake").unwrap();
133        let name = bin.name();
134        assert_eq!(name, "all_fake");
135
136        let bin = bin_from_description_with_name("fakesrc ! fakesink", false, "").unwrap();
137        let name = bin.name();
138        assert_ne!(name, "");
139    }
140}