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