gstreamer_validate/
action.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::CStr;
4
5use glib::prelude::*;
6use glib::translate::*;
7
8use crate::{ffi, ActionType, Scenario};
9
10gst::mini_object_wrapper!(Action, ActionRef, ffi::GstValidateAction, || {
11    ffi::gst_validate_action_get_type()
12});
13
14impl ActionRef {
15    pub fn structure(&self) -> Option<&gst::StructureRef> {
16        unsafe {
17            let action = &self.0 as *const ffi::GstValidateAction;
18
19            if (*action).structure.is_null() {
20                None
21            } else {
22                Some(gst::StructureRef::from_glib_borrow((*action).structure))
23            }
24        }
25    }
26
27    pub fn structure_mut(&mut self) -> Option<&mut gst::StructureRef> {
28        unsafe {
29            let action = &mut self.0 as *mut ffi::GstValidateAction;
30
31            if (*action).structure.is_null() {
32                None
33            } else {
34                Some(gst::StructureRef::from_glib_borrow_mut((*action).structure))
35            }
36        }
37    }
38
39    #[doc(alias = "gst_validate_action_get_scenario")]
40    pub fn scenario(&self) -> Option<Scenario> {
41        unsafe {
42            let scenario = ffi::gst_validate_action_get_scenario(self.as_mut_ptr());
43
44            from_glib_full(scenario)
45        }
46    }
47}
48
49impl Action {
50    pub(crate) unsafe fn from_glib_ptr_borrow_mut(
51        ptr: &mut *mut ffi::GstValidateAction,
52    ) -> &mut Self {
53        assert_initialized_main_thread!();
54
55        debug_assert_eq!((*(*ptr)).mini_object.refcount, 1);
56
57        debug_assert_eq!(
58            std::mem::size_of::<Action>(),
59            std::mem::size_of::<gst::glib::ffi::gpointer>()
60        );
61        debug_assert!(!ptr.is_null());
62
63        &mut *(ptr as *mut *mut ffi::GstValidateAction as *mut Action)
64    }
65
66    #[doc(alias = "gst_validate_action_new")]
67    pub fn new(
68        scenario: Option<&impl IsA<Scenario>>,
69        action_type: &ActionType,
70        structure: &gst::StructureRef,
71        add_to_lists: bool,
72    ) -> Action {
73        assert_initialized_main_thread!();
74        unsafe {
75            from_glib_full(ffi::gst_validate_action_new(
76                scenario.map(|p| p.as_ref()).to_glib_none().0,
77                action_type.to_glib_none().0,
78                structure.as_mut_ptr(),
79                add_to_lists.into_glib(),
80            ))
81        }
82    }
83
84    pub fn name(&self) -> &str {
85        unsafe {
86            let action: *mut ffi::GstValidateAction = self.to_glib_none().0;
87            CStr::from_ptr((*action).name).to_str().unwrap()
88        }
89    }
90
91    pub fn report_error(&self, error_message: &str) {
92        if let Some(scenario) = self.scenario() {
93            scenario.upcast_ref::<crate::Reporter>().report_action(
94                self,
95                glib::Quark::from_str("scenario::execution-error"),
96                error_message,
97            )
98        }
99    }
100
101    #[doc(alias = "gst_validate_execute_action")]
102    pub fn execute(self) -> Result<crate::ActionSuccess, crate::ActionError> {
103        unsafe {
104            let action: *mut ffi::GstValidateAction = self.into_glib_ptr();
105            let action_type = ffi::gst_validate_get_action_type((*action).type_);
106
107            let res = ffi::gst_validate_execute_action(action_type, action);
108
109            if let Some(v) = crate::ActionSuccess::from_value(res) {
110                Ok(v)
111            } else {
112                Err(crate::ActionError::from_value(res))
113            }
114        }
115    }
116
117    #[doc(alias = "gst_validate_action_set_done")]
118    pub fn set_done(self) {
119        unsafe {
120            ffi::gst_validate_action_set_done(self.into_glib_ptr());
121        }
122    }
123}
124
125impl std::fmt::Debug for Action {
126    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
127        f.debug_struct("Action")
128            .field("structure", &self.structure())
129            .field("name", &self.name())
130            .finish()
131    }
132}