gstreamer-rs/gstreamer/src/caps.rs

276 lines
6.8 KiB
Rust
Raw Normal View History

2017-07-10 21:02:08 +00:00
// Copyright (C) 2016-2017 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::fmt;
use std::str;
2017-07-10 21:02:08 +00:00
use miniobject::*;
use structure::*;
use glib;
use ffi;
2017-07-10 21:36:38 +00:00
use glib::translate::{from_glib, from_glib_none, from_glib_full, ToGlibPtr};
2017-07-10 21:02:08 +00:00
#[repr(C)]
pub struct CapsRef(ffi::GstCaps);
pub type Caps = GstRc<CapsRef>;
unsafe impl MiniObject for CapsRef {
type GstType = ffi::GstCaps;
}
2017-07-10 21:31:53 +00:00
impl GstRc<CapsRef> {
pub fn new_empty() -> Self {
assert_initialized_main_thread!();
2017-07-10 21:02:08 +00:00
unsafe { from_glib_full(ffi::gst_caps_new_empty()) }
}
2017-07-10 21:31:53 +00:00
pub fn new_any() -> Self {
assert_initialized_main_thread!();
2017-07-10 21:02:08 +00:00
unsafe { from_glib_full(ffi::gst_caps_new_any()) }
}
2017-07-10 21:31:53 +00:00
pub fn new_simple(name: &str, values: &[(&str, &glib::Value)]) -> Self {
assert_initialized_main_thread!();
let mut caps = Caps::new_empty();
2017-07-10 21:02:08 +00:00
2017-07-10 21:31:53 +00:00
let structure = Structure::new(name, values);
caps.get_mut().unwrap().append_structure(structure);
2017-07-10 21:02:08 +00:00
caps
}
2017-07-10 21:31:53 +00:00
pub fn from_string(value: &str) -> Option<Self> {
assert_initialized_main_thread!();
2017-07-10 21:02:08 +00:00
unsafe {
let caps_ptr = ffi::gst_caps_from_string(value.to_glib_none().0);
if caps_ptr.is_null() {
None
} else {
Some(from_glib_full(caps_ptr))
}
}
}
2017-07-10 21:31:53 +00:00
}
2017-07-10 21:02:08 +00:00
impl str::FromStr for Caps {
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
Caps::from_string(s).ok_or(())
}
}
2017-07-10 21:31:53 +00:00
impl CapsRef {
pub fn set_simple(&mut self, values: &[(&str, &glib::Value)]) {
2017-07-10 21:02:08 +00:00
for &(name, ref value) in values {
unsafe {
2017-07-10 21:33:24 +00:00
ffi::gst_caps_set_value(
self.as_mut_ptr(),
name.to_glib_none().0,
value.to_glib_none().0,
);
2017-07-10 21:02:08 +00:00
}
}
}
pub fn to_string(&self) -> String {
2017-07-10 21:33:24 +00:00
unsafe { from_glib_full(ffi::gst_caps_to_string(self.as_ptr())) }
2017-07-10 21:02:08 +00:00
}
pub fn get_structure(&self, idx: u32) -> Option<&StructureRef> {
unsafe {
let structure = ffi::gst_caps_get_structure(self.as_ptr(), idx);
if structure.is_null() {
return None;
}
Some(StructureRef::from_glib_borrow(
structure as *const ffi::GstStructure,
))
}
}
pub fn get_mut_structure<'a>(&'a mut self, idx: u32) -> Option<&'a mut StructureRef> {
unsafe {
let structure = ffi::gst_caps_get_structure(self.as_ptr(), idx);
if structure.is_null() {
return None;
}
Some(StructureRef::from_glib_borrow_mut(
structure as *mut ffi::GstStructure,
))
}
}
2017-07-10 21:31:53 +00:00
pub fn append_structure(&mut self, structure: Structure) {
2017-07-10 21:33:24 +00:00
unsafe { ffi::gst_caps_append_structure(self.as_mut_ptr(), structure.into_ptr()) }
2017-07-10 21:31:53 +00:00
}
2017-07-10 21:02:08 +00:00
pub fn get_size(&self) -> u32 {
unsafe { ffi::gst_caps_get_size(self.as_ptr()) }
}
pub fn iter(&self) -> Iter {
Iter::new(self)
}
//pub fn iter_mut(&mut self) -> IterMut {
// IterMut::new(self)
//}
// TODO: All kinds of caps operations
}
impl glib::types::StaticType for GstRc<CapsRef> {
fn static_type() -> glib::types::Type {
2017-07-10 21:33:24 +00:00
unsafe { from_glib(ffi::gst_caps_get_type()) }
2017-07-10 21:02:08 +00:00
}
}
macro_rules! define_iter(
($name:ident, $typ:ty, $styp:ty, $getter:ident) => {
pub struct $name<'a> {
caps: $typ,
idx: u32,
n_structures: u32,
}
impl<'a> $name<'a> {
pub fn new(caps: $typ) -> $name<'a> {
let n_structures = caps.get_size();
$name {
caps: caps,
idx: 0,
n_structures: n_structures,
}
}
}
impl<'a> Iterator for $name<'a> {
type Item = $styp;
fn next(&mut self) -> Option<Self::Item> {
if self.idx >= self.n_structures {
return None;
}
if let Some(s) = self.caps.$getter(self.idx) {
self.idx += 1;
Some(s)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
if self.idx == self.n_structures {
return (0, Some(0));
}
let remaining = (self.n_structures - self.idx) as usize;
(remaining, Some(remaining))
}
}
impl<'a> DoubleEndedIterator for $name<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.idx == self.n_structures {
return None;
}
self.n_structures -= 1;
if let Some(s) = self.caps.$getter(self.n_structures) {
Some(s)
} else {
None
}
}
}
impl<'a> ExactSizeIterator for $name<'a> {}
}
);
define_iter!(Iter, &'a CapsRef, &'a StructureRef, get_structure);
//define_iter!(IterMut, &'a mut CapsRef, &'a mut Structure, get_mut_structure);
impl fmt::Debug for CapsRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_string())
}
}
impl PartialEq for CapsRef {
fn eq(&self, other: &CapsRef) -> bool {
unsafe { from_glib(ffi::gst_caps_is_equal(self.as_ptr(), other.as_ptr())) }
}
}
impl Eq for CapsRef {}
impl ToOwned for CapsRef {
type Owned = GstRc<CapsRef>;
fn to_owned(&self) -> GstRc<CapsRef> {
unsafe { from_glib_none(self.as_ptr()) }
}
}
unsafe impl Sync for CapsRef {}
unsafe impl Send for CapsRef {}
#[cfg(test)]
mod tests {
use super::*;
2017-07-12 07:38:34 +00:00
use glib::ToValue;
2017-07-12 10:25:11 +00:00
use Fraction;
2017-07-10 21:02:08 +00:00
#[test]
fn test_simple() {
2017-07-12 07:32:39 +00:00
::init().unwrap();
2017-07-10 21:02:08 +00:00
let caps = Caps::new_simple(
2017-07-10 21:02:08 +00:00
"foo/bar",
&[
2017-07-12 07:32:39 +00:00
("int", &12.to_value()),
("bool", &true.to_value()),
("string", &"bla".to_value()),
2017-07-12 10:25:11 +00:00
("fraction", &Fraction::new(1, 2).to_value()),
2017-07-12 07:32:39 +00:00
//("array", vec![1.into(), 2.into()].into()),
2017-07-10 21:02:08 +00:00
],
);
assert_eq!(
caps.to_string(),
2017-07-12 10:25:11 +00:00
"foo/bar, int=(int)12, bool=(boolean)true, string=(string)bla, fraction=(fraction)1/2"
2017-07-12 07:32:39 +00:00
); //, \
2017-07-12 10:25:11 +00:00
// array=(int)< 1, 2 >"
2017-07-12 07:32:39 +00:00
//);
2017-07-10 21:02:08 +00:00
let s = caps.get_structure(0).unwrap();
assert_eq!(
s,
2017-07-12 07:32:39 +00:00
Structure::new(
2017-07-10 21:02:08 +00:00
"foo/bar",
&[
2017-07-12 07:32:39 +00:00
("int", &12.to_value()),
("bool", &true.to_value()),
("string", &"bla".to_value()),
("fraction", &Fraction::new(1, 2).to_value())
2017-07-12 07:32:39 +00:00
//("array", vec![1.into(), 2.into()].into()),
2017-07-10 21:02:08 +00:00
],
).as_ref()
);
}
}