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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Take a look at the license at the top of the repository in the LICENSE file.

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

use glib::translate::{from_glib_full, from_glib_none, mut_override, ToGlibPtr};

use crate::Buffer;
use crate::BufferList;
use crate::BufferListRef;
use crate::BufferRef;
use crate::Caps;
use crate::CapsRef;
use crate::FormattedSegment;
use crate::FormattedValueIntrinsic;
use crate::Segment;
use crate::Structure;
use crate::StructureRef;

mini_object_wrapper!(Sample, SampleRef, ffi::GstSample, || {
    ffi::gst_sample_get_type()
});

#[derive(Debug, Clone)]
pub struct SampleBuilder<'a> {
    buffer: Option<&'a Buffer>,
    buffer_list: Option<&'a BufferList>,
    caps: Option<&'a Caps>,
    segment: Option<&'a Segment>,
    info: Option<Structure>,
}

impl<'a> SampleBuilder<'a> {
    pub fn buffer(self, buffer: &'a Buffer) -> Self {
        Self {
            buffer: Some(buffer),
            buffer_list: None,
            ..self
        }
    }

    pub fn buffer_list(self, buffer_list: &'a BufferList) -> Self {
        Self {
            buffer: None,
            buffer_list: Some(buffer_list),
            ..self
        }
    }

    pub fn caps(self, caps: &'a Caps) -> Self {
        Self {
            caps: Some(caps),
            ..self
        }
    }

    pub fn segment<F: FormattedValueIntrinsic>(self, segment: &'a FormattedSegment<F>) -> Self {
        Self {
            segment: Some(segment.upcast_ref()),
            ..self
        }
    }

    pub fn info(self, info: Structure) -> Self {
        Self {
            info: Some(info),
            ..self
        }
    }

    pub fn build(self) -> Sample {
        assert_initialized_main_thread!();

        unsafe {
            let info = self.info.map(|i| i.into_ptr()).unwrap_or(ptr::null_mut());

            let sample: Sample = from_glib_full(ffi::gst_sample_new(
                self.buffer.to_glib_none().0,
                self.caps.to_glib_none().0,
                self.segment.to_glib_none().0,
                mut_override(info),
            ));

            if let Some(buffer_list) = self.buffer_list {
                ffi::gst_sample_set_buffer_list(
                    sample.to_glib_none().0,
                    buffer_list.to_glib_none().0,
                );
            }

            sample
        }
    }
}

impl Sample {
    pub fn builder<'a>() -> SampleBuilder<'a> {
        SampleBuilder {
            buffer: None,
            buffer_list: None,
            caps: None,
            segment: None,
            info: None,
        }
    }
}

impl SampleRef {
    #[doc(alias = "get_buffer")]
    #[doc(alias = "gst_sample_get_buffer")]
    pub fn buffer(&self) -> Option<&BufferRef> {
        unsafe {
            let ptr = ffi::gst_sample_get_buffer(self.as_mut_ptr());
            if ptr.is_null() {
                None
            } else {
                Some(BufferRef::from_ptr(ptr))
            }
        }
    }

    #[doc(alias = "get_buffer_owned")]
    pub fn buffer_owned(&self) -> Option<Buffer> {
        unsafe { self.buffer().map(|buffer| from_glib_none(buffer.as_ptr())) }
    }

    #[doc(alias = "get_buffer_list")]
    #[doc(alias = "gst_sample_get_buffer_list")]
    pub fn buffer_list(&self) -> Option<&BufferListRef> {
        unsafe {
            let ptr = ffi::gst_sample_get_buffer_list(self.as_mut_ptr());
            if ptr.is_null() {
                None
            } else {
                Some(BufferListRef::from_ptr(ptr))
            }
        }
    }

    #[doc(alias = "get_buffer_list_owned")]
    pub fn buffer_list_owned(&self) -> Option<BufferList> {
        unsafe { self.buffer_list().map(|list| from_glib_none(list.as_ptr())) }
    }

    #[doc(alias = "get_caps")]
    #[doc(alias = "gst_sample_get_caps")]
    pub fn caps(&self) -> Option<&CapsRef> {
        unsafe {
            let ptr = ffi::gst_sample_get_caps(self.as_mut_ptr());
            if ptr.is_null() {
                None
            } else {
                Some(CapsRef::from_ptr(ptr))
            }
        }
    }

    #[doc(alias = "get_caps_owned")]
    pub fn caps_owned(&self) -> Option<Caps> {
        unsafe { self.caps().map(|caps| from_glib_none(caps.as_ptr())) }
    }

    #[doc(alias = "get_segment")]
    #[doc(alias = "gst_sample_get_segment")]
    pub fn segment(&self) -> Option<Segment> {
        unsafe { from_glib_none(ffi::gst_sample_get_segment(self.as_mut_ptr())) }
    }

    #[doc(alias = "get_info")]
    #[doc(alias = "gst_sample_get_info")]
    pub fn info(&self) -> Option<&StructureRef> {
        unsafe {
            let ptr = ffi::gst_sample_get_info(self.as_mut_ptr());
            if ptr.is_null() {
                None
            } else {
                Some(StructureRef::from_glib_borrow(ptr))
            }
        }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_sample_set_buffer")]
    pub fn set_buffer(&mut self, buffer: Option<&Buffer>) {
        unsafe { ffi::gst_sample_set_buffer(self.as_mut_ptr(), buffer.to_glib_none().0) }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_sample_set_buffer_list")]
    pub fn set_buffer_list(&mut self, buffer_list: Option<&BufferList>) {
        unsafe { ffi::gst_sample_set_buffer_list(self.as_mut_ptr(), buffer_list.to_glib_none().0) }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_sample_set_caps")]
    pub fn set_caps(&mut self, caps: Option<&Caps>) {
        unsafe { ffi::gst_sample_set_caps(self.as_mut_ptr(), caps.to_glib_none().0) }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_sample_set_segment")]
    pub fn set_segment(&mut self, segment: Option<&Segment>) {
        unsafe { ffi::gst_sample_set_segment(self.as_mut_ptr(), segment.to_glib_none().0) }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_sample_set_info")]
    pub fn set_info(&mut self, info: Option<Structure>) {
        unsafe {
            ffi::gst_sample_set_info(
                self.as_mut_ptr(),
                info.map(|i| i.into_ptr()).unwrap_or(ptr::null_mut()),
            );
        }
    }
}

impl fmt::Debug for Sample {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        SampleRef::fmt(self, f)
    }
}

impl fmt::Debug for SampleRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Sample")
            .field("buffer", &self.buffer())
            .field("caps", &self.caps())
            .field("segment", &self.segment())
            .field("info", &self.info())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_sample_new_with_info() {
        use crate::Sample;
        use crate::Structure;

        crate::init().unwrap();

        let info = Structure::builder("sample.info")
            .field("f3", &123i32)
            .build();
        let sample = Sample::builder().info(info).build();

        assert!(sample.info().is_some());
    }
}