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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Take a look at the license at the top of the repository in the LICENSE file.

use std::fmt;
use std::ptr;

use crate::ffi;
use glib::translate::*;
use gst::prelude::*;

#[repr(transparent)]
#[doc(alias = "GstRTPSourceMeta")]
pub struct RTPSourceMeta(ffi::GstRTPSourceMeta);

unsafe impl Send for RTPSourceMeta {}
unsafe impl Sync for RTPSourceMeta {}

impl RTPSourceMeta {
    #[doc(alias = "gst_buffer_add_rtp_source_meta")]
    pub fn add<'a>(
        buffer: &'a mut gst::BufferRef,
        ssrc: Option<u32>,
        csrc: &[u32],
    ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
        skip_assert_initialized!();
        unsafe {
            let meta = ffi::gst_buffer_add_rtp_source_meta(
                buffer.as_mut_ptr(),
                ssrc.as_ref()
                    .map(|ssrc: &u32| ssrc as *const u32)
                    .unwrap_or(ptr::null()),
                csrc.as_ptr(),
                csrc.len() as u32,
            );

            Self::from_mut_ptr(buffer, meta)
        }
    }

    #[inline]
    pub fn ssrc(&self) -> Option<u32> {
        unsafe {
            if from_glib(self.0.ssrc_valid) {
                Some(self.0.ssrc)
            } else {
                None
            }
        }
    }

    #[inline]
    pub fn csrc(&self) -> &[u32] {
        &self.0.csrc[0..self.0.csrc_count as usize]
    }

    #[doc(alias = "gst_rtp_source_meta_set_ssrc")]
    #[inline]
    pub fn set_ssrc(&mut self, ssrc: Option<u32>) {
        unsafe {
            ffi::gst_rtp_source_meta_set_ssrc(
                &mut self.0,
                mut_override(
                    ssrc.as_ref()
                        .map(|ssrc: &u32| ssrc as *const u32)
                        .unwrap_or(ptr::null()),
                ),
            );
        }
    }

    #[inline]
    #[doc(alias = "gst_rtp_source_meta_append_csrc")]
    pub fn append_csrc(&mut self, csrc: &[u32]) -> Result<(), glib::BoolError> {
        unsafe {
            let res: bool = from_glib(ffi::gst_rtp_source_meta_append_csrc(
                &mut self.0,
                csrc.as_ptr(),
                csrc.len() as u32,
            ));

            if res {
                Ok(())
            } else {
                Err(glib::bool_error!("Failed adding another CSRC"))
            }
        }
    }
}

unsafe impl MetaAPI for RTPSourceMeta {
    type GstType = ffi::GstRTPSourceMeta;

    #[doc(alias = "gst_rtp_source_meta_api_get_type")]
    #[inline]
    fn meta_api() -> glib::Type {
        unsafe { from_glib(ffi::gst_rtp_source_meta_api_get_type()) }
    }
}

impl fmt::Debug for RTPSourceMeta {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("RTPSourceMeta")
            .field("ssrc", &self.ssrc())
            .field("csrc", &self.csrc())
            .finish()
    }
}