1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{prelude::*, subclass::prelude::*, translate::*};

use crate::{ffi, RTSPServer};

pub trait RTSPServerImpl: RTSPServerImplExt + ObjectImpl + Send + Sync {
    /// Create, configure a new GstRTSPClient
    ///  object that handles the new connection on `socket`. The default
    ///  implementation will create a GstRTSPClient and will configure the
    ///  mount-points, auth, session-pool and thread-pool on the client.
    fn create_client(&self) -> Option<crate::RTSPClient> {
        self.parent_create_client()
    }

    /// emitted when a new client connected.
    fn client_connected(&self, client: &crate::RTSPClient) {
        self.parent_client_connected(client);
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::RTSPServerImplExt> Sealed for T {}
}

pub trait RTSPServerImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_create_client(&self) -> Option<crate::RTSPClient>;

    fn parent_client_connected(&self, client: &crate::RTSPClient);
}

impl<T: RTSPServerImpl> RTSPServerImplExt for T {
    fn parent_create_client(&self) -> Option<crate::RTSPClient> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPServerClass;
            let f = (*parent_class)
                .create_client
                .expect("No `create_client` virtual method implementation in parent class");
            from_glib_full(f(self
                .obj()
                .unsafe_cast_ref::<RTSPServer>()
                .to_glib_none()
                .0))
        }
    }

    fn parent_client_connected(&self, client: &crate::RTSPClient) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPServerClass;
            if let Some(f) = (*parent_class).client_connected {
                f(
                    self.obj().unsafe_cast_ref::<RTSPServer>().to_glib_none().0,
                    client.to_glib_none().0,
                )
            }
        }
    }
}
unsafe impl<T: RTSPServerImpl> IsSubclassable<T> for RTSPServer {
    fn class_init(klass: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(klass);
        let klass = klass.as_mut();
        klass.create_client = Some(server_create_client::<T>);
        klass.client_connected = Some(server_client_connected::<T>);
    }
}

unsafe extern "C" fn server_create_client<T: RTSPServerImpl>(
    ptr: *mut ffi::GstRTSPServer,
) -> *mut ffi::GstRTSPClient {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.create_client().into_glib_ptr()
}

unsafe extern "C" fn server_client_connected<T: RTSPServerImpl>(
    ptr: *mut ffi::GstRTSPServer,
    client: *mut ffi::GstRTSPClient,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.client_connected(&from_glib_borrow(client));
}