gstreamer_rtsp_server/
rtsp_auth.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{boxed::Box as Box_, mem::transmute};
4
5use glib::{
6    prelude::*,
7    signal::{connect_raw, SignalHandlerId},
8    translate::*,
9};
10
11use crate::{ffi, RTSPAuth, RTSPToken};
12
13mod sealed {
14    pub trait Sealed {}
15    impl<T: super::IsA<super::RTSPAuth>> Sealed for T {}
16}
17
18pub trait RTSPAuthExtManual: sealed::Sealed + IsA<RTSPAuth> + 'static {
19    /// Set the default [`RTSPToken`][crate::RTSPToken] to `token` in `self`. The default token will
20    /// be used for unauthenticated users.
21    /// ## `token`
22    /// a [`RTSPToken`][crate::RTSPToken]
23    #[doc(alias = "gst_rtsp_auth_set_default_token")]
24    fn set_default_token(&self, mut token: Option<&mut RTSPToken>) {
25        unsafe {
26            ffi::gst_rtsp_auth_set_default_token(
27                self.as_ref().to_glib_none().0,
28                token.to_glib_none_mut().0,
29            );
30        }
31    }
32
33    fn connect_accept_certificate<
34        F: Fn(
35                &Self,
36                &gio::TlsConnection,
37                &gio::TlsCertificate,
38                gio::TlsCertificateFlags,
39            ) -> Result<(), gst::LoggableError>
40            + Send
41            + Sync
42            + 'static,
43    >(
44        &self,
45        f: F,
46    ) -> SignalHandlerId {
47        unsafe {
48            let f: Box_<F> = Box_::new(f);
49            connect_raw(
50                self.as_ptr() as *mut _,
51                b"accept-certificate\0".as_ptr() as *const _,
52                Some(transmute::<*const (), unsafe extern "C" fn()>(
53                    accept_certificate_trampoline::<Self, F> as *const (),
54                )),
55                Box_::into_raw(f),
56            )
57        }
58    }
59}
60
61impl<O: IsA<RTSPAuth>> RTSPAuthExtManual for O {}
62
63unsafe extern "C" fn accept_certificate_trampoline<
64    P,
65    F: Fn(
66            &P,
67            &gio::TlsConnection,
68            &gio::TlsCertificate,
69            gio::TlsCertificateFlags,
70        ) -> Result<(), gst::LoggableError>
71        + Send
72        + Sync
73        + 'static,
74>(
75    this: *mut ffi::GstRTSPAuth,
76    connection: *mut gio::ffi::GTlsConnection,
77    peer_cert: *mut gio::ffi::GTlsCertificate,
78    errors: gio::ffi::GTlsCertificateFlags,
79    f: glib::ffi::gpointer,
80) -> glib::ffi::gboolean
81where
82    P: IsA<RTSPAuth>,
83{
84    let f: &F = &*(f as *const F);
85    match f(
86        RTSPAuth::from_glib_borrow(this).unsafe_cast_ref(),
87        &from_glib_borrow(connection),
88        &from_glib_borrow(peer_cert),
89        from_glib(errors),
90    ) {
91        Ok(()) => true,
92        Err(err) => {
93            err.log();
94            false
95        }
96    }
97    .into_glib()
98}