1 // Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 use std::ffi::CStr;
10 use std::fmt;
11 
12 use gst_sys;
13 
14 use glib;
15 use glib::translate::{from_glib, from_glib_full, ToGlib, ToGlibPtr};
16 
17 use miniobject::*;
18 use StructureRef;
19 
20 gst_define_mini_object_wrapper!(Context, ContextRef, gst_sys::GstContext, [Debug,], || {
21     gst_sys::gst_context_get_type()
22 });
23 
24 impl Context {
new(context_type: &str, persistent: bool) -> Self25     pub fn new(context_type: &str, persistent: bool) -> Self {
26         assert_initialized_main_thread!();
27         unsafe {
28             from_glib_full(gst_sys::gst_context_new(
29                 context_type.to_glib_none().0,
30                 persistent.to_glib(),
31             ))
32         }
33     }
34 }
35 
36 impl ContextRef {
get_context_type(&self) -> &str37     pub fn get_context_type(&self) -> &str {
38         unsafe {
39             let raw = gst_sys::gst_context_get_context_type(self.as_mut_ptr());
40             CStr::from_ptr(raw).to_str().unwrap()
41         }
42     }
43 
has_context_type(&self, context_type: &str) -> bool44     pub fn has_context_type(&self, context_type: &str) -> bool {
45         unsafe {
46             from_glib(gst_sys::gst_context_has_context_type(
47                 self.as_mut_ptr(),
48                 context_type.to_glib_none().0,
49             ))
50         }
51     }
52 
is_persistent(&self) -> bool53     pub fn is_persistent(&self) -> bool {
54         unsafe { from_glib(gst_sys::gst_context_is_persistent(self.as_mut_ptr())) }
55     }
56 
get_structure(&self) -> &StructureRef57     pub fn get_structure(&self) -> &StructureRef {
58         unsafe {
59             StructureRef::from_glib_borrow(gst_sys::gst_context_get_structure(self.as_mut_ptr()))
60         }
61     }
62 
get_mut_structure(&mut self) -> &mut StructureRef63     pub fn get_mut_structure(&mut self) -> &mut StructureRef {
64         unsafe {
65             StructureRef::from_glib_borrow_mut(gst_sys::gst_context_writable_structure(
66                 self.as_mut_ptr(),
67             ))
68         }
69     }
70 }
71 
72 impl fmt::Debug for ContextRef {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result73     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74         f.debug_struct("Context")
75             .field("type", &self.get_context_type())
76             .field("structure", &self.get_structure())
77             .finish()
78     }
79 }
80