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