1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use std::{ffi::CStr, fmt}; |
4 | |
5 | use glib::translate::{from_glib, from_glib_full, IntoGlib, ToGlibPtr}; |
6 | |
7 | use crate::StructureRef; |
8 | |
9 | mini_object_wrapper!(Context, ContextRef, ffi::GstContext, || { |
10 | ffi::gst_context_get_type() |
11 | }); |
12 | |
13 | impl 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(ptr:ffi::gst_context_new( |
19 | context_type:context_type.to_glib_none().0, |
20 | persistent:persistent.into_glib(), |
21 | )) |
22 | } |
23 | } |
24 | } |
25 | |
26 | impl 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 | |
67 | impl fmt::Debug for Context { |
68 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
69 | ContextRef::fmt(self, f) |
70 | } |
71 | } |
72 | |
73 | impl fmt::Debug for ContextRef { |
74 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
75 | f&mut DebugStruct<'_, '_>.debug_struct("Context" ) |
76 | .field("type" , &self.context_type()) |
77 | .field(name:"structure" , &self.structure()) |
78 | .finish() |
79 | } |
80 | } |
81 | |