gstreamer_sdp/
sdp_attribute.rs
1use std::{ffi::CStr, fmt, mem};
4
5use crate::ffi;
6use glib::translate::*;
7
8#[repr(transparent)]
9#[doc(alias = "GstSDPAttribute")]
10pub struct SDPAttribute(pub(crate) ffi::GstSDPAttribute);
11
12unsafe impl Send for SDPAttribute {}
13unsafe impl Sync for SDPAttribute {}
14
15impl SDPAttribute {
16 pub fn new(key: &str, value: Option<&str>) -> Self {
17 assert_initialized_main_thread!();
18 unsafe {
19 let mut attr = mem::MaybeUninit::uninit();
20 ffi::gst_sdp_attribute_set(
21 attr.as_mut_ptr(),
22 key.to_glib_none().0,
23 value.to_glib_none().0,
24 );
25 SDPAttribute(attr.assume_init())
26 }
27 }
28
29 pub fn key(&self) -> &str {
30 unsafe { CStr::from_ptr(self.0.key).to_str().unwrap() }
31 }
32
33 pub fn value(&self) -> Option<&str> {
34 unsafe {
35 let ptr = self.0.value;
36
37 if ptr.is_null() {
38 None
39 } else {
40 Some(CStr::from_ptr(ptr).to_str().unwrap())
41 }
42 }
43 }
44}
45
46impl Clone for SDPAttribute {
47 fn clone(&self) -> Self {
48 SDPAttribute::new(self.key(), self.value())
49 }
50}
51
52impl Drop for SDPAttribute {
53 fn drop(&mut self) {
54 unsafe {
55 ffi::gst_sdp_attribute_clear(&mut self.0);
56 }
57 }
58}
59
60impl fmt::Debug for SDPAttribute {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 f.debug_struct("SDPAttribute")
63 .field("key", &self.key())
64 .field("value", &self.value())
65 .finish()
66 }
67}