1use crate::{ffi, Caps, Element, Object, Structure};
7use glib::{
8 object::ObjectType as _,
9 prelude::*,
10 signal::{connect_raw, SignalHandlerId},
11 translate::*,
12};
13use std::boxed::Box as Box_;
14
15glib::wrapper! {
16 #[doc(alias = "GstDevice")]
17 pub struct Device(Object<ffi::GstDevice, ffi::GstDeviceClass>) @extends Object;
18
19 match fn {
20 type_ => || ffi::gst_device_get_type(),
21 }
22}
23
24impl Device {
25 pub const NONE: Option<&'static Device> = None;
26}
27
28unsafe impl Send for Device {}
29unsafe impl Sync for Device {}
30
31pub trait DeviceExt: IsA<Device> + 'static {
32 #[doc(alias = "gst_device_create_element")]
33 fn create_element(&self, name: Option<&str>) -> Result<Element, glib::BoolError> {
34 unsafe {
35 Option::<_>::from_glib_none(ffi::gst_device_create_element(
36 self.as_ref().to_glib_none().0,
37 name.to_glib_none().0,
38 ))
39 .ok_or_else(|| glib::bool_error!("Failed to create element for device"))
40 }
41 }
42
43 #[doc(alias = "gst_device_get_caps")]
44 #[doc(alias = "get_caps")]
45 fn caps(&self) -> Option<Caps> {
46 unsafe { from_glib_full(ffi::gst_device_get_caps(self.as_ref().to_glib_none().0)) }
47 }
48
49 #[doc(alias = "gst_device_get_device_class")]
50 #[doc(alias = "get_device_class")]
51 #[doc(alias = "device-class")]
52 fn device_class(&self) -> glib::GString {
53 unsafe {
54 from_glib_full(ffi::gst_device_get_device_class(
55 self.as_ref().to_glib_none().0,
56 ))
57 }
58 }
59
60 #[doc(alias = "gst_device_get_display_name")]
61 #[doc(alias = "get_display_name")]
62 #[doc(alias = "display-name")]
63 fn display_name(&self) -> glib::GString {
64 unsafe {
65 from_glib_full(ffi::gst_device_get_display_name(
66 self.as_ref().to_glib_none().0,
67 ))
68 }
69 }
70
71 #[doc(alias = "gst_device_get_properties")]
72 #[doc(alias = "get_properties")]
73 fn properties(&self) -> Option<Structure> {
74 unsafe {
75 from_glib_full(ffi::gst_device_get_properties(
76 self.as_ref().to_glib_none().0,
77 ))
78 }
79 }
80
81 #[doc(alias = "gst_device_has_classes")]
82 fn has_classes(&self, classes: &str) -> bool {
83 unsafe {
84 from_glib(ffi::gst_device_has_classes(
85 self.as_ref().to_glib_none().0,
86 classes.to_glib_none().0,
87 ))
88 }
89 }
90
91 #[doc(alias = "gst_device_has_classesv")]
92 fn has_classesv(&self, classes: &[&str]) -> bool {
93 unsafe {
94 from_glib(ffi::gst_device_has_classesv(
95 self.as_ref().to_glib_none().0,
96 classes.to_glib_none().0,
97 ))
98 }
99 }
100
101 #[doc(alias = "gst_device_reconfigure_element")]
102 fn reconfigure_element(
103 &self,
104 element: &impl IsA<Element>,
105 ) -> Result<(), glib::error::BoolError> {
106 unsafe {
107 glib::result_from_gboolean!(
108 ffi::gst_device_reconfigure_element(
109 self.as_ref().to_glib_none().0,
110 element.as_ref().to_glib_none().0
111 ),
112 "Failed to reconfigure the element to use this device"
113 )
114 }
115 }
116
117 #[doc(alias = "removed")]
118 fn connect_removed<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
119 unsafe extern "C" fn removed_trampoline<
120 P: IsA<Device>,
121 F: Fn(&P) + Send + Sync + 'static,
122 >(
123 this: *mut ffi::GstDevice,
124 f: glib::ffi::gpointer,
125 ) {
126 let f: &F = &*(f as *const F);
127 f(Device::from_glib_borrow(this).unsafe_cast_ref())
128 }
129 unsafe {
130 let f: Box_<F> = Box_::new(f);
131 connect_raw(
132 self.as_ptr() as *mut _,
133 c"removed".as_ptr() as *const _,
134 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
135 removed_trampoline::<Self, F> as *const (),
136 )),
137 Box_::into_raw(f),
138 )
139 }
140 }
141}
142
143impl<O: IsA<Device>> DeviceExt for O {}