gstreamer_rtsp_server/
rtsp_context.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{marker::PhantomData, ptr};
4
5use glib::{prelude::*, translate::*};
6use gst_rtsp::{rtsp_message::RTSPMessage, RTSPUrl};
7
8use crate::{ffi, RTSPClient, RTSPSession, RTSPToken};
9
10#[derive(Debug, PartialEq, Eq)]
11#[doc(alias = "GstRTSPContext")]
12#[repr(transparent)]
13pub struct RTSPContext(ptr::NonNull<ffi::GstRTSPContext>);
14
15impl RTSPContext {
16    #[inline]
17    pub fn with_current_context<F: FnOnce(&RTSPContext) -> T, T>(func: F) -> Option<T> {
18        unsafe {
19            let ptr = ffi::gst_rtsp_context_get_current();
20            if ptr.is_null() {
21                return None;
22            }
23
24            let ctx = RTSPContext(ptr::NonNull::new_unchecked(ptr));
25            Some(func(&ctx))
26        }
27    }
28
29    #[inline]
30    pub fn uri(&self) -> Option<&RTSPUrl> {
31        unsafe {
32            let ptr = self.0.as_ptr();
33            if (*ptr).uri.is_null() {
34                None
35            } else {
36                let uri = RTSPUrl::from_glib_ptr_borrow(&(*ptr).uri);
37                Some(uri)
38            }
39        }
40    }
41
42    #[inline]
43    pub fn client(&self) -> Option<&RTSPClient> {
44        unsafe {
45            let ptr = self.0.as_ptr();
46            if (*ptr).client.is_null() {
47                None
48            } else {
49                let client = RTSPClient::from_glib_ptr_borrow(&(*ptr).client);
50                Some(client)
51            }
52        }
53    }
54
55    #[inline]
56    pub fn request(&self) -> Option<&RTSPMessage> {
57        unsafe {
58            let ptr = self.0.as_ptr();
59            if (*ptr).request.is_null() {
60                None
61            } else {
62                let msg = RTSPMessage::from_glib_ptr_borrow(&(*ptr).request);
63                Some(msg)
64            }
65        }
66    }
67
68    #[inline]
69    pub fn response(&self) -> Option<&RTSPMessage> {
70        unsafe {
71            let ptr = self.0.as_ptr();
72            if (*ptr).response.is_null() {
73                None
74            } else {
75                let msg = RTSPMessage::from_glib_ptr_borrow(&(*ptr).response);
76                Some(msg)
77            }
78        }
79    }
80
81    #[inline]
82    pub fn session(&self) -> Option<&RTSPSession> {
83        unsafe {
84            let ptr = self.0.as_ptr();
85            if (*ptr).session.is_null() {
86                None
87            } else {
88                let sess = RTSPSession::from_glib_ptr_borrow(&(*ptr).session);
89                Some(sess)
90            }
91        }
92    }
93
94    #[inline]
95    pub fn token(&self) -> Option<RTSPToken> {
96        unsafe {
97            let ptr = self.0.as_ptr();
98            if (*ptr).token.is_null() {
99                None
100            } else {
101                let token = RTSPToken::from_glib_none((*ptr).token as *const ffi::GstRTSPToken);
102                Some(token)
103            }
104        }
105    }
106
107    #[cfg(feature = "v1_22")]
108    #[doc(alias = "gst_rtsp_context_set_token")]
109    pub fn set_token(&self, token: RTSPToken) {
110        unsafe {
111            ffi::gst_rtsp_context_set_token(self.0.as_ptr(), token.into_glib_ptr());
112        }
113    }
114
115    // TODO: Add additional getters for all the contained fields as needed
116}
117
118#[doc(hidden)]
119impl FromGlibPtrBorrow<*mut ffi::GstRTSPContext> for RTSPContext {
120    #[inline]
121    unsafe fn from_glib_borrow(ptr: *mut ffi::GstRTSPContext) -> Borrowed<Self> {
122        debug_assert!(!ptr.is_null());
123        Borrowed::new(RTSPContext(ptr::NonNull::new_unchecked(ptr)))
124    }
125}
126
127#[doc(hidden)]
128impl<'a> ToGlibPtr<'a, *mut ffi::GstRTSPContext> for RTSPContext {
129    type Storage = PhantomData<&'a RTSPContext>;
130
131    #[inline]
132    fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GstRTSPContext, Self> {
133        Stash(self.0.as_ptr(), PhantomData)
134    }
135
136    fn to_glib_full(&self) -> *mut ffi::GstRTSPContext {
137        unimplemented!()
138    }
139}