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