gstreamer/
context.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{ffi::CStr, fmt};
4
5use glib::translate::{IntoGlib, ToGlibPtr, from_glib, from_glib_full};
6
7use crate::{StructureRef, ffi};
8
9mini_object_wrapper!(Context, ContextRef, ffi::GstContext, || {
10    ffi::gst_context_get_type()
11});
12
13impl Context {
14    #[doc(alias = "gst_context_new")]
15    pub fn new(context_type: &str, persistent: bool) -> Self {
16        assert_initialized_main_thread!();
17        unsafe {
18            from_glib_full(ffi::gst_context_new(
19                context_type.to_glib_none().0,
20                persistent.into_glib(),
21            ))
22        }
23    }
24}
25
26impl ContextRef {
27    #[doc(alias = "get_context_type")]
28    #[doc(alias = "gst_context_get_context_type")]
29    pub fn context_type(&self) -> &str {
30        unsafe {
31            let raw = ffi::gst_context_get_context_type(self.as_mut_ptr());
32            CStr::from_ptr(raw).to_str().unwrap()
33        }
34    }
35
36    #[doc(alias = "gst_context_has_context_type")]
37    pub fn has_context_type(&self, context_type: &str) -> bool {
38        unsafe {
39            from_glib(ffi::gst_context_has_context_type(
40                self.as_mut_ptr(),
41                context_type.to_glib_none().0,
42            ))
43        }
44    }
45
46    #[doc(alias = "gst_context_is_persistent")]
47    pub fn is_persistent(&self) -> bool {
48        unsafe { from_glib(ffi::gst_context_is_persistent(self.as_mut_ptr())) }
49    }
50
51    #[doc(alias = "get_structure")]
52    #[doc(alias = "gst_context_get_structure")]
53    pub fn structure(&self) -> &StructureRef {
54        unsafe { StructureRef::from_glib_borrow(ffi::gst_context_get_structure(self.as_mut_ptr())) }
55    }
56
57    #[doc(alias = "get_mut_structure")]
58    pub fn structure_mut(&mut self) -> &mut StructureRef {
59        unsafe {
60            StructureRef::from_glib_borrow_mut(ffi::gst_context_writable_structure(
61                self.as_mut_ptr(),
62            ))
63        }
64    }
65
66    #[cfg(feature = "v1_28")]
67    #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
68    #[doc(alias = "gst_context_get_task_pool")]
69    pub fn task_pool(&self) -> Option<crate::TaskPool> {
70        assert_eq!(self.context_type(), TASK_POOL_CONTEXT_TYPE);
71
72        unsafe {
73            use std::ptr;
74
75            let mut pool = ptr::null_mut();
76            if from_glib(ffi::gst_context_get_task_pool(self.as_mut_ptr(), &mut pool)) {
77                Some(from_glib_full(pool))
78            } else {
79                None
80            }
81        }
82    }
83
84    #[cfg(feature = "v1_28")]
85    #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
86    #[doc(alias = "gst_context_set_task_pool")]
87    pub fn set_task_pool<'a, T: glib::prelude::IsA<crate::TaskPool>>(
88        &self,
89        pool: impl Into<Option<&'a T>>,
90    ) {
91        unsafe {
92            ffi::gst_context_set_task_pool(
93                self.as_mut_ptr(),
94                pool.into().map(|d| d.as_ref()).to_glib_none().0,
95            );
96        }
97    }
98}
99
100#[cfg(feature = "v1_28")]
101#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
102#[doc(alias = "GST_TASK_POOL_CONTEXT_TYPE")]
103pub static TASK_POOL_CONTEXT_TYPE: &glib::GStr =
104    unsafe { glib::GStr::from_utf8_with_nul_unchecked(ffi::GST_TASK_POOL_CONTEXT_TYPE) };
105
106impl fmt::Debug for Context {
107    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108        ContextRef::fmt(self, f)
109    }
110}
111
112impl fmt::Debug for ContextRef {
113    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114        f.debug_struct("Context")
115            .field("type", &self.context_type())
116            .field("structure", &self.structure())
117            .finish()
118    }
119}