Skip to main content

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::{ActionType, Scenario, ffi};
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        unsafe {
56            debug_assert_eq!((*(*ptr)).mini_object.refcount, 1);
57
58            debug_assert_eq!(
59                std::mem::size_of::<Action>(),
60                std::mem::size_of::<gst::glib::ffi::gpointer>()
61            );
62            debug_assert!(!ptr.is_null());
63
64            &mut *(ptr as *mut *mut ffi::GstValidateAction as *mut Action)
65        }
66    }
67
68    /// ## `scenario`
69    /// The scenario executing the action
70    /// ## `action_type`
71    /// The action type
72    /// ## `structure`
73    /// The structure containing the action arguments
74    /// ## `add_to_lists`
75    /// Weather the action should be added to the scenario action list
76    ///
77    /// # Returns
78    ///
79    /// A newly created [`Action`][crate::Action]
80    #[doc(alias = "gst_validate_action_new")]
81    pub fn new(
82        scenario: Option<&impl IsA<Scenario>>,
83        action_type: &ActionType,
84        structure: &gst::StructureRef,
85        add_to_lists: bool,
86    ) -> Action {
87        assert_initialized_main_thread!();
88        unsafe {
89            from_glib_full(ffi::gst_validate_action_new(
90                scenario.map(|p| p.as_ref()).to_glib_none().0,
91                action_type.to_glib_none().0,
92                structure.as_mut_ptr(),
93                add_to_lists.into_glib(),
94            ))
95        }
96    }
97
98    pub fn name(&self) -> &str {
99        unsafe {
100            let action: *mut ffi::GstValidateAction = self.to_glib_none().0;
101            CStr::from_ptr((*action).name).to_str().unwrap()
102        }
103    }
104
105    pub fn report_error(&self, error_message: &str) {
106        if let Some(scenario) = self.scenario() {
107            scenario.upcast_ref::<crate::Reporter>().report_action(
108                self,
109                glib::Quark::from_str("scenario::execution-error"),
110                error_message,
111            )
112        }
113    }
114
115    #[doc(alias = "gst_validate_execute_action")]
116    pub fn execute(self) -> Result<crate::ActionSuccess, crate::ActionError> {
117        unsafe {
118            let action: *mut ffi::GstValidateAction = self.into_glib_ptr();
119            let action_type = ffi::gst_validate_get_action_type((*action).type_);
120
121            let res = ffi::gst_validate_execute_action(action_type, action);
122
123            if let Some(v) = crate::ActionSuccess::from_value(res) {
124                Ok(v)
125            } else {
126                Err(crate::ActionError::from_value(res))
127            }
128        }
129    }
130
131    #[doc(alias = "gst_validate_action_set_done")]
132    pub fn set_done(self) {
133        unsafe {
134            ffi::gst_validate_action_set_done(self.into_glib_ptr());
135        }
136    }
137}
138
139impl std::fmt::Debug for Action {
140    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
141        f.debug_struct("Action")
142            .field("structure", &self.structure())
143            .field("name", &self.name())
144            .finish()
145    }
146}