Skip to main content

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