1use 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")]
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#[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}