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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::borrow::{Borrow, BorrowMut, ToOwned};
use std::ffi::CStr;
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::ptr;
use std::str;

use glib;
use glib::translate::{
    from_glib, from_glib_full, from_glib_none, FromGlibPtrFull, FromGlibPtrNone, GlibPtrDefault,
    Stash, StashMut, ToGlibPtr, ToGlibPtrMut,
};
use glib_sys::gpointer;
use gobject_sys;
use gst_sys;

pub struct CapsFeatures(ptr::NonNull<CapsFeaturesRef>, PhantomData<CapsFeaturesRef>);
unsafe impl Send for CapsFeatures {}
unsafe impl Sync for CapsFeatures {}

impl CapsFeatures {
    pub fn new(features: &[&str]) -> Self {
        let mut f = Self::new_empty();

        for feature in features {
            f.add(feature);
        }

        f
    }

    pub fn new_empty() -> Self {
        assert_initialized_main_thread!();
        unsafe {
            CapsFeatures(
                ptr::NonNull::new_unchecked(
                    gst_sys::gst_caps_features_new_empty() as *mut CapsFeaturesRef
                ),
                PhantomData,
            )
        }
    }

    pub fn new_any() -> Self {
        assert_initialized_main_thread!();
        unsafe {
            CapsFeatures(
                ptr::NonNull::new_unchecked(
                    gst_sys::gst_caps_features_new_any() as *mut CapsFeaturesRef
                ),
                PhantomData,
            )
        }
    }

    pub fn from_string(value: &str) -> Option<Self> {
        assert_initialized_main_thread!();
        unsafe {
            let ptr = gst_sys::gst_caps_features_from_string(value.to_glib_none().0);
            if ptr.is_null() {
                return None;
            }

            Some(CapsFeatures(
                ptr::NonNull::new_unchecked(ptr as *mut CapsFeaturesRef),
                PhantomData,
            ))
        }
    }

    pub unsafe fn into_ptr(self) -> *mut gst_sys::GstCapsFeatures {
        let ptr = self.0.as_ptr() as *mut CapsFeaturesRef as *mut gst_sys::GstCapsFeatures;
        mem::forget(self);

        ptr
    }
}

impl Deref for CapsFeatures {
    type Target = CapsFeaturesRef;

    fn deref(&self) -> &CapsFeaturesRef {
        unsafe { self.0.as_ref() }
    }
}

impl DerefMut for CapsFeatures {
    fn deref_mut(&mut self) -> &mut CapsFeaturesRef {
        unsafe { self.0.as_mut() }
    }
}

impl AsRef<CapsFeaturesRef> for CapsFeatures {
    fn as_ref(&self) -> &CapsFeaturesRef {
        self.deref()
    }
}

impl AsMut<CapsFeaturesRef> for CapsFeatures {
    fn as_mut(&mut self) -> &mut CapsFeaturesRef {
        self.deref_mut()
    }
}

impl Clone for CapsFeatures {
    fn clone(&self) -> Self {
        unsafe {
            let ptr = gst_sys::gst_caps_features_copy(&self.0.as_ref().0) as *mut CapsFeaturesRef;
            assert!(!ptr.is_null());
            CapsFeatures(ptr::NonNull::new_unchecked(ptr), PhantomData)
        }
    }
}

impl Drop for CapsFeatures {
    fn drop(&mut self) {
        unsafe { gst_sys::gst_caps_features_free(&mut self.0.as_mut().0) }
    }
}

impl fmt::Debug for CapsFeatures {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("CapsFeatures")
            .field(&self.to_string())
            .finish()
    }
}

impl fmt::Display for CapsFeatures {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Need to make sure to not call ToString::to_string() here, which
        // we have because of the Display impl. We need CapsFeaturesRef::to_string()
        f.write_str(&CapsFeaturesRef::to_string(self.as_ref()))
    }
}

impl str::FromStr for CapsFeatures {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, ()> {
        skip_assert_initialized!();
        CapsFeatures::from_string(s).ok_or(())
    }
}

impl Borrow<CapsFeaturesRef> for CapsFeatures {
    fn borrow(&self) -> &CapsFeaturesRef {
        unsafe { self.0.as_ref() }
    }
}

impl BorrowMut<CapsFeaturesRef> for CapsFeatures {
    fn borrow_mut(&mut self) -> &mut CapsFeaturesRef {
        unsafe { self.0.as_mut() }
    }
}

impl glib::types::StaticType for CapsFeatures {
    fn static_type() -> glib::types::Type {
        unsafe { from_glib(gst_sys::gst_caps_features_get_type()) }
    }
}

impl<'a> ToGlibPtr<'a, *const gst_sys::GstCapsFeatures> for CapsFeatures {
    type Storage = &'a Self;

    fn to_glib_none(&'a self) -> Stash<'a, *const gst_sys::GstCapsFeatures, Self> {
        unsafe { Stash(&self.0.as_ref().0, self) }
    }

    fn to_glib_full(&self) -> *const gst_sys::GstCapsFeatures {
        unsafe { gst_sys::gst_caps_features_copy(&self.0.as_ref().0) }
    }
}

impl<'a> ToGlibPtr<'a, *mut gst_sys::GstCapsFeatures> for CapsFeatures {
    type Storage = &'a Self;

    fn to_glib_none(&'a self) -> Stash<'a, *mut gst_sys::GstCapsFeatures, Self> {
        unsafe { Stash(&self.0.as_ref().0 as *const _ as *mut _, self) }
    }

    fn to_glib_full(&self) -> *mut gst_sys::GstCapsFeatures {
        unsafe { gst_sys::gst_caps_features_copy(&self.0.as_ref().0) }
    }
}

impl<'a> ToGlibPtrMut<'a, *mut gst_sys::GstCapsFeatures> for CapsFeatures {
    type Storage = &'a mut Self;

    fn to_glib_none_mut(&'a mut self) -> StashMut<*mut gst_sys::GstCapsFeatures, Self> {
        unsafe { StashMut(&mut self.0.as_mut().0, self) }
    }
}

impl FromGlibPtrNone<*const gst_sys::GstCapsFeatures> for CapsFeatures {
    unsafe fn from_glib_none(ptr: *const gst_sys::GstCapsFeatures) -> Self {
        assert!(!ptr.is_null());
        let ptr = gst_sys::gst_caps_features_copy(ptr);
        assert!(!ptr.is_null());
        CapsFeatures(
            ptr::NonNull::new_unchecked(ptr as *mut CapsFeaturesRef),
            PhantomData,
        )
    }
}

impl FromGlibPtrNone<*mut gst_sys::GstCapsFeatures> for CapsFeatures {
    unsafe fn from_glib_none(ptr: *mut gst_sys::GstCapsFeatures) -> Self {
        assert!(!ptr.is_null());
        let ptr = gst_sys::gst_caps_features_copy(ptr);
        assert!(!ptr.is_null());
        CapsFeatures(
            ptr::NonNull::new_unchecked(ptr as *mut CapsFeaturesRef),
            PhantomData,
        )
    }
}

impl FromGlibPtrFull<*const gst_sys::GstCapsFeatures> for CapsFeatures {
    unsafe fn from_glib_full(ptr: *const gst_sys::GstCapsFeatures) -> Self {
        assert!(!ptr.is_null());
        CapsFeatures(
            ptr::NonNull::new_unchecked(ptr as *mut CapsFeaturesRef),
            PhantomData,
        )
    }
}

impl FromGlibPtrFull<*mut gst_sys::GstCapsFeatures> for CapsFeatures {
    unsafe fn from_glib_full(ptr: *mut gst_sys::GstCapsFeatures) -> Self {
        assert!(!ptr.is_null());
        CapsFeatures(
            ptr::NonNull::new_unchecked(ptr as *mut CapsFeaturesRef),
            PhantomData,
        )
    }
}

impl<'a> glib::value::FromValueOptional<'a> for CapsFeatures {
    unsafe fn from_value_optional(v: &'a glib::Value) -> Option<Self> {
        let ptr = gobject_sys::g_value_get_boxed(v.to_glib_none().0);
        assert!(!ptr.is_null());
        from_glib_none(ptr as *const gst_sys::GstCapsFeatures)
    }
}

impl glib::value::SetValue for CapsFeatures {
    unsafe fn set_value(v: &mut glib::Value, s: &Self) {
        gobject_sys::g_value_set_boxed(v.to_glib_none_mut().0, s.0.as_ptr() as gpointer);
    }
}

impl glib::value::SetValueOptional for CapsFeatures {
    unsafe fn set_value_optional(v: &mut glib::Value, s: Option<&Self>) {
        if let Some(s) = s {
            gobject_sys::g_value_set_boxed(v.to_glib_none_mut().0, s.as_ptr() as gpointer);
        } else {
            gobject_sys::g_value_set_boxed(v.to_glib_none_mut().0, ptr::null_mut());
        }
    }
}

impl GlibPtrDefault for CapsFeatures {
    type GlibType = *mut gst_sys::GstCapsFeatures;
}

#[repr(C)]
pub struct CapsFeaturesRef(gst_sys::GstCapsFeatures);

impl CapsFeaturesRef {
    pub unsafe fn from_glib_borrow<'a>(
        ptr: *const gst_sys::GstCapsFeatures,
    ) -> &'a CapsFeaturesRef {
        assert!(!ptr.is_null());

        &*(ptr as *mut CapsFeaturesRef)
    }

    pub unsafe fn from_glib_borrow_mut<'a>(
        ptr: *mut gst_sys::GstCapsFeatures,
    ) -> &'a mut CapsFeaturesRef {
        assert!(!ptr.is_null());

        &mut *(ptr as *mut CapsFeaturesRef)
    }

    pub unsafe fn as_ptr(&self) -> *const gst_sys::GstCapsFeatures {
        self as *const Self as *const gst_sys::GstCapsFeatures
    }

    pub unsafe fn as_mut_ptr(&self) -> *mut gst_sys::GstCapsFeatures {
        self as *const Self as *mut gst_sys::GstCapsFeatures
    }

    pub fn to_string(&self) -> String {
        unsafe { from_glib_full(gst_sys::gst_caps_features_to_string(self.as_ptr())) }
    }

    pub fn is_empty(&self) -> bool {
        self.get_size() == 0 && !self.is_any()
    }

    pub fn is_any(&self) -> bool {
        unsafe { from_glib(gst_sys::gst_caps_features_is_any(self.as_ptr())) }
    }

    pub fn contains(&self, feature: &str) -> bool {
        unsafe {
            from_glib(gst_sys::gst_caps_features_contains(
                self.as_ptr(),
                feature.to_glib_none().0,
            ))
        }
    }

    pub fn get_size(&self) -> u32 {
        unsafe { gst_sys::gst_caps_features_get_size(self.as_ptr()) }
    }

    pub fn get_nth(&self, idx: u32) -> Option<&str> {
        if idx >= self.get_size() {
            return None;
        }

        unsafe {
            let feature = gst_sys::gst_caps_features_get_nth(self.as_ptr(), idx);
            if feature.is_null() {
                return None;
            }

            Some(CStr::from_ptr(feature).to_str().unwrap())
        }
    }

    pub fn add(&mut self, feature: &str) {
        unsafe { gst_sys::gst_caps_features_add(self.as_mut_ptr(), feature.to_glib_none().0) }
    }

    pub fn remove(&mut self, feature: &str) {
        unsafe { gst_sys::gst_caps_features_remove(self.as_mut_ptr(), feature.to_glib_none().0) }
    }

    pub fn iter(&self) -> Iter {
        Iter::new(self)
    }

    // This is not an equivalence relation with regards to ANY. Everything is equal to ANY
    pub fn is_equal(&self, other: &CapsFeaturesRef) -> bool {
        unsafe {
            from_glib(gst_sys::gst_caps_features_is_equal(
                self.as_ptr(),
                other.as_ptr(),
            ))
        }
    }
}

#[derive(Debug)]
pub struct Iter<'a> {
    caps_features: &'a CapsFeaturesRef,
    idx: u32,
    n_features: u32,
}

impl<'a> Iter<'a> {
    fn new(caps_features: &'a CapsFeaturesRef) -> Iter<'a> {
        skip_assert_initialized!();
        let n_features = caps_features.get_size();

        Iter {
            caps_features,
            idx: 0,
            n_features,
        }
    }
}

impl<'a> Iterator for Iter<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx >= self.n_features {
            return None;
        }

        unsafe {
            let feature = gst_sys::gst_caps_features_get_nth(self.caps_features.as_ptr(), self.idx);
            if feature.is_null() {
                return None;
            }

            self.idx += 1;

            Some(CStr::from_ptr(feature).to_str().unwrap())
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.idx == self.n_features {
            return (0, Some(0));
        }

        let remaining = (self.n_features - self.idx) as usize;

        (remaining, Some(remaining))
    }
}

impl<'a> DoubleEndedIterator for Iter<'a> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.idx == self.n_features {
            return None;
        }

        self.n_features -= 1;

        unsafe {
            let feature =
                gst_sys::gst_caps_features_get_nth(self.caps_features.as_ptr(), self.n_features);
            if feature.is_null() {
                return None;
            }

            Some(CStr::from_ptr(feature).to_str().unwrap())
        }
    }
}

impl<'a> ExactSizeIterator for Iter<'a> {}

impl fmt::Debug for CapsFeaturesRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("CapsFeatures")
            .field(&self.to_string())
            .finish()
    }
}

impl fmt::Display for CapsFeaturesRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.to_string())
    }
}

impl ToOwned for CapsFeaturesRef {
    type Owned = CapsFeatures;

    fn to_owned(&self) -> CapsFeatures {
        unsafe {
            from_glib_full(gst_sys::gst_caps_features_copy(self.as_ptr() as *const _) as *mut _)
        }
    }
}

unsafe impl Sync for CapsFeaturesRef {}
unsafe impl Send for CapsFeaturesRef {}

lazy_static! {
    pub static ref CAPS_FEATURE_MEMORY_SYSTEM_MEMORY: &'static str = unsafe {
        CStr::from_ptr(gst_sys::GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY)
            .to_str()
            .unwrap()
    };
    pub static ref CAPS_FEATURES_MEMORY_SYSTEM_MEMORY: CapsFeatures =
        CapsFeatures::new(&[*CAPS_FEATURE_MEMORY_SYSTEM_MEMORY]);
}