gstreamer_sdp/
sdp_time.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{ffi::CStr, fmt, mem, os::raw::c_char, ptr};
4
5use crate::ffi;
6use glib::translate::*;
7
8#[repr(transparent)]
9#[doc(alias = "GstSDPTime")]
10pub struct SDPTime(pub(crate) ffi::GstSDPTime);
11
12unsafe impl Send for SDPTime {}
13unsafe impl Sync for SDPTime {}
14
15impl SDPTime {
16    pub fn new(start: &str, stop: &str, repeat: &[&str]) -> Self {
17        assert_initialized_main_thread!();
18        unsafe {
19            let mut time = mem::MaybeUninit::uninit();
20            ffi::gst_sdp_time_set(
21                time.as_mut_ptr(),
22                start.to_glib_none().0,
23                stop.to_glib_none().0,
24                repeat.to_glib_none().0,
25            );
26            SDPTime(time.assume_init())
27        }
28    }
29
30    pub fn start(&self) -> Option<&str> {
31        unsafe {
32            if self.0.start.is_null() {
33                None
34            } else {
35                Some(CStr::from_ptr(self.0.start).to_str().unwrap())
36            }
37        }
38    }
39
40    pub fn stop(&self) -> Option<&str> {
41        unsafe {
42            if self.0.stop.is_null() {
43                None
44            } else {
45                Some(CStr::from_ptr(self.0.stop).to_str().unwrap())
46            }
47        }
48    }
49
50    pub fn repeat(&self) -> Vec<&str> {
51        #[allow(clippy::cast_ptr_alignment)]
52        unsafe {
53            if self.0.repeat.is_null() || (*self.0.repeat).data.is_null() {
54                return vec![];
55            }
56
57            let arr = (*self.0.repeat).data as *const *const c_char;
58            let len = (*self.0.repeat).len as usize;
59            let mut vec = Vec::with_capacity(len);
60            for i in 0..len {
61                vec.push(CStr::from_ptr(*arr.add(i)).to_str().unwrap());
62            }
63            vec
64        }
65    }
66}
67
68impl Clone for SDPTime {
69    fn clone(&self) -> Self {
70        skip_assert_initialized!();
71        #[allow(clippy::cast_ptr_alignment)]
72        unsafe {
73            let mut time = mem::MaybeUninit::uninit();
74            ffi::gst_sdp_time_set(
75                time.as_mut_ptr(),
76                self.0.start,
77                self.0.stop,
78                if self.0.repeat.is_null() {
79                    ptr::null_mut()
80                } else {
81                    (*self.0.repeat).data as *mut *const c_char
82                },
83            );
84            SDPTime(time.assume_init())
85        }
86    }
87}
88
89impl Drop for SDPTime {
90    fn drop(&mut self) {
91        unsafe {
92            ffi::gst_sdp_time_clear(&mut self.0);
93        }
94    }
95}
96
97impl fmt::Debug for SDPTime {
98    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99        f.debug_struct("SDPTime")
100            .field("start", &self.start())
101            .field("stop", &self.stop())
102            .field("repeat", &self.repeat())
103            .finish()
104    }
105}