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