Skip to main content

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    /// Creates a new context.
15    /// ## `context_type`
16    /// Context type
17    /// ## `persistent`
18    /// Persistent context
19    ///
20    /// # Returns
21    ///
22    /// The new context.
23    #[doc(alias = "gst_context_new")]
24    pub fn new(context_type: &str, persistent: bool) -> Self {
25        assert_initialized_main_thread!();
26        unsafe {
27            from_glib_full(ffi::gst_context_new(
28                context_type.to_glib_none().0,
29                persistent.into_glib(),
30            ))
31        }
32    }
33}
34
35impl ContextRef {
36    #[doc(alias = "get_context_type")]
37    #[doc(alias = "gst_context_get_context_type")]
38    pub fn context_type(&self) -> &str {
39        unsafe {
40            let raw = ffi::gst_context_get_context_type(self.as_mut_ptr());
41            CStr::from_ptr(raw).to_str().unwrap()
42        }
43    }
44
45    #[doc(alias = "gst_context_has_context_type")]
46    pub fn has_context_type(&self, context_type: &str) -> bool {
47        unsafe {
48            from_glib(ffi::gst_context_has_context_type(
49                self.as_mut_ptr(),
50                context_type.to_glib_none().0,
51            ))
52        }
53    }
54
55    #[doc(alias = "gst_context_is_persistent")]
56    pub fn is_persistent(&self) -> bool {
57        unsafe { from_glib(ffi::gst_context_is_persistent(self.as_mut_ptr())) }
58    }
59
60    #[doc(alias = "get_structure")]
61    #[doc(alias = "gst_context_get_structure")]
62    pub fn structure(&self) -> &StructureRef {
63        unsafe { StructureRef::from_glib_borrow(ffi::gst_context_get_structure(self.as_mut_ptr())) }
64    }
65
66    #[doc(alias = "get_mut_structure")]
67    pub fn structure_mut(&mut self) -> &mut StructureRef {
68        unsafe {
69            StructureRef::from_glib_borrow_mut(ffi::gst_context_writable_structure(
70                self.as_mut_ptr(),
71            ))
72        }
73    }
74
75    #[cfg(feature = "v1_28")]
76    #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
77    #[doc(alias = "gst_context_get_task_pool")]
78    pub fn task_pool(&self) -> Option<crate::TaskPool> {
79        assert_eq!(self.context_type(), TASK_POOL_CONTEXT_TYPE);
80
81        unsafe {
82            use std::ptr;
83
84            let mut pool = ptr::null_mut();
85            if from_glib(ffi::gst_context_get_task_pool(self.as_mut_ptr(), &mut pool)) {
86                Some(from_glib_full(pool))
87            } else {
88                None
89            }
90        }
91    }
92
93    #[cfg(feature = "v1_28")]
94    #[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
95    #[doc(alias = "gst_context_set_task_pool")]
96    pub fn set_task_pool<'a, T: glib::prelude::IsA<crate::TaskPool>>(
97        &self,
98        pool: impl Into<Option<&'a T>>,
99    ) {
100        unsafe {
101            ffi::gst_context_set_task_pool(
102                self.as_mut_ptr(),
103                pool.into().map(|d| d.as_ref()).to_glib_none().0,
104            );
105        }
106    }
107}
108
109/// The well-known context type for sharing a [`TaskPool`][crate::TaskPool] between elements
110/// in a pipeline.
111///
112/// Elements that support this context will post a `GST_MESSAGE_NEED_CONTEXT`
113/// message on the bus when they need a task pool. Applications can respond
114/// by setting the context on the element or the pipeline. Elements will not
115/// query neighbors for this context type as the task pool is optional and
116/// elements will fall back to their default behavior if no pool is provided.
117#[cfg(feature = "v1_28")]
118#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
119#[doc(alias = "GST_TASK_POOL_CONTEXT_TYPE")]
120pub static TASK_POOL_CONTEXT_TYPE: &glib::GStr =
121    unsafe { glib::GStr::from_utf8_with_nul_unchecked(ffi::GST_TASK_POOL_CONTEXT_TYPE) };
122
123impl fmt::Debug for Context {
124    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125        ContextRef::fmt(self, f)
126    }
127}
128
129impl fmt::Debug for ContextRef {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        f.debug_struct("Context")
132            .field("type", &self.context_type())
133            .field("structure", &self.structure())
134            .finish()
135    }
136}