gstreamer_rtsp_server/
rtsp_token.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use crate::ffi;
6use glib::{translate::*, SendValue};
7use gst::IdStr;
8
9gst::mini_object_wrapper!(RTSPToken, RTSPTokenRef, ffi::GstRTSPToken, || {
10    ffi::gst_rtsp_token_get_type()
11});
12
13impl RTSPToken {
14    #[doc(alias = "gst_rtsp_token_new_empty")]
15    pub fn new_empty() -> Self {
16        assert_initialized_main_thread!();
17        unsafe { from_glib_full(ffi::gst_rtsp_token_new_empty()) }
18    }
19
20    #[doc(alias = "gst_rtsp_token_new")]
21    pub fn builder() -> Builder {
22        skip_assert_initialized!();
23        Builder::new()
24    }
25
26    #[allow(clippy::should_implement_trait)]
27    pub fn from_iter<S: IntoGStr>(iter: impl IntoIterator<Item = (S, SendValue)>) -> RTSPToken {
28        skip_assert_initialized!();
29        let mut token = RTSPToken::new_empty();
30
31        let s = token.get_mut().unwrap().structure_mut();
32        iter.into_iter().for_each(|(f, v)| s.set_value(f, v));
33
34        token
35    }
36}
37
38impl RTSPTokenRef {
39    #[doc(alias = "get_string")]
40    #[doc(alias = "gst_rtsp_token_get_string")]
41    pub fn string(&self, field: &str) -> Option<String> {
42        unsafe {
43            from_glib_none(ffi::gst_rtsp_token_get_string(
44                self.as_mut_ptr(),
45                field.to_glib_none().0,
46            ))
47        }
48    }
49
50    #[doc(alias = "get_structure")]
51    #[doc(alias = "gst_rtsp_token_get_structure")]
52    pub fn structure(&self) -> Option<gst::Structure> {
53        unsafe { from_glib_none(ffi::gst_rtsp_token_get_structure(self.as_mut_ptr())) }
54    }
55
56    #[doc(alias = "gst_rtsp_token_is_allowed")]
57    pub fn is_allowed(&self, field: &str) -> bool {
58        unsafe {
59            from_glib(ffi::gst_rtsp_token_is_allowed(
60                self.as_mut_ptr(),
61                field.to_glib_none().0,
62            ))
63        }
64    }
65
66    #[doc(alias = "get_mut_structure")]
67    pub fn structure_mut(&mut self) -> &mut gst::StructureRef {
68        unsafe {
69            let structure = ffi::gst_rtsp_token_writable_structure(self.as_mut_ptr());
70            gst::StructureRef::from_glib_borrow_mut(structure)
71        }
72    }
73}
74
75impl fmt::Debug for RTSPToken {
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        RTSPTokenRef::fmt(self, f)
78    }
79}
80
81impl fmt::Debug for RTSPTokenRef {
82    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83        f.debug_struct("RTSPToken")
84            .field("structure", &self.structure())
85            .finish()
86    }
87}
88
89#[derive(Debug)]
90#[must_use = "The builder must be built to be used"]
91pub struct Builder {
92    token: RTSPToken,
93}
94
95impl Builder {
96    fn new() -> Self {
97        skip_assert_initialized!();
98        Builder {
99            token: RTSPToken::new_empty(),
100        }
101    }
102
103    pub fn field(mut self, name: impl IntoGStr, value: impl Into<glib::Value> + Send) -> Self {
104        self.token
105            .get_mut()
106            .unwrap()
107            .structure_mut()
108            .set(name, value);
109        self
110    }
111
112    pub fn field_with_static(
113        mut self,
114        name: impl AsRef<glib::GStr> + 'static,
115        value: impl Into<glib::Value> + Send,
116    ) -> Self {
117        self.token
118            .get_mut()
119            .unwrap()
120            .structure_mut()
121            .set_with_static(name, value);
122        self
123    }
124
125    pub fn field_with_id(
126        mut self,
127        name: impl AsRef<IdStr>,
128        value: impl Into<glib::Value> + Send,
129    ) -> Self {
130        self.token
131            .get_mut()
132            .unwrap()
133            .structure_mut()
134            .set_with_id(name, value);
135        self
136    }
137
138    gst::impl_builder_gvalue_extra_setters!(field);
139
140    #[must_use = "Building the structure without using it has no effect"]
141    pub fn build(self) -> RTSPToken {
142        self.token
143    }
144}