2016-12-30 11:11:30 +00:00
|
|
|
// Copyright (C) 2016 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
// Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
use libc::c_char;
|
|
|
|
use std::os::raw::c_void;
|
|
|
|
use std::ffi::CString;
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::fmt;
|
|
|
|
|
2017-01-13 17:16:12 +00:00
|
|
|
use value::*;
|
2016-12-30 11:11:30 +00:00
|
|
|
|
|
|
|
pub struct Caps(*mut c_void);
|
|
|
|
|
|
|
|
impl Caps {
|
2016-12-30 17:02:31 +00:00
|
|
|
pub fn new_empty() -> Self {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_caps_new_empty() -> *mut c_void;
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:46:59 +00:00
|
|
|
Caps(unsafe { gst_caps_new_empty() })
|
2016-12-30 17:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_any() -> Self {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_caps_new_any() -> *mut c_void;
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:46:59 +00:00
|
|
|
Caps(unsafe { gst_caps_new_any() })
|
2016-12-30 17:02:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 10:52:41 +00:00
|
|
|
pub fn new_simple(name: &str, values: &[(&str, &Value)]) -> Self {
|
2016-12-30 11:11:30 +00:00
|
|
|
extern "C" {
|
|
|
|
fn gst_caps_append_structure(caps: *mut c_void, structure: *mut c_void);
|
|
|
|
fn gst_structure_new_empty(name: *const c_char) -> *mut c_void;
|
|
|
|
}
|
|
|
|
|
2017-01-05 22:46:59 +00:00
|
|
|
let mut caps = Caps::new_empty();
|
2016-12-30 11:11:30 +00:00
|
|
|
|
|
|
|
let name_cstr = CString::new(name).unwrap();
|
|
|
|
let structure = unsafe { gst_structure_new_empty(name_cstr.as_ptr()) };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
gst_caps_append_structure(caps.0, structure);
|
|
|
|
}
|
|
|
|
|
|
|
|
caps.set_simple(values);
|
|
|
|
|
|
|
|
caps
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:05:04 +00:00
|
|
|
pub fn from_string(value: &str) -> Option<Self> {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_caps_from_string(value: *const c_char) -> *mut c_void;
|
|
|
|
}
|
|
|
|
|
|
|
|
let value_cstr = CString::new(value).unwrap();
|
|
|
|
|
|
|
|
let caps_ptr = unsafe { gst_caps_from_string(value_cstr.as_ptr()) };
|
|
|
|
|
|
|
|
if caps_ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Caps(caps_ptr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:55:10 +00:00
|
|
|
pub fn set_simple(&mut self, values: &[(&str, &Value)]) {
|
2016-12-30 11:11:30 +00:00
|
|
|
extern "C" {
|
|
|
|
fn gst_caps_set_value(caps: *mut c_void, name: *const c_char, value: *const GValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
for value in values {
|
|
|
|
let name_cstr = CString::new(value.0).unwrap();
|
2017-01-13 10:52:41 +00:00
|
|
|
let mut gvalue = value.1.to_gvalue();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
gst_caps_set_value(self.0, name_cstr.as_ptr(), &mut gvalue as *mut GValue);
|
2016-12-30 11:11:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(&self) -> String {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_caps_to_string(caps: *mut c_void) -> *mut c_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { CStr::from_ptr(gst_caps_to_string(self.0)).to_string_lossy().into_owned() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn as_ptr(&self) -> *const c_void {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_writable(self: Caps) -> Caps {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_mini_object_make_writable(obj: *mut c_void) -> *mut c_void;
|
|
|
|
}
|
|
|
|
|
|
|
|
let raw = unsafe { gst_mini_object_make_writable(self.0) };
|
|
|
|
|
|
|
|
Caps(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn copy(&self) -> Caps {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_mini_object_copy(obj: *const c_void) -> *mut c_void;
|
|
|
|
}
|
|
|
|
unsafe { Caps(gst_mini_object_copy(self.0)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for Caps {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_mini_object_ref(mini_object: *mut c_void) -> *mut c_void;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { Caps(gst_mini_object_ref(self.0)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 10:52:41 +00:00
|
|
|
impl Drop for Caps {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_mini_object_unref(mini_object: *mut c_void);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { gst_mini_object_unref(self.0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 11:11:30 +00:00
|
|
|
impl fmt::Debug for Caps {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.write_str(&self.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2017-01-13 17:16:12 +00:00
|
|
|
use value::*;
|
2016-12-30 11:11:30 +00:00
|
|
|
use std::ptr;
|
|
|
|
use std::os::raw::c_void;
|
|
|
|
|
|
|
|
fn init() {
|
|
|
|
extern "C" {
|
|
|
|
fn gst_init(argc: *mut c_void, argv: *mut c_void);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
gst_init(ptr::null_mut(), ptr::null_mut());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
init();
|
|
|
|
|
|
|
|
let caps = Caps::new_simple("foo/bar",
|
2017-01-13 10:52:41 +00:00
|
|
|
&[("int", &Value::Int(12)),
|
|
|
|
("bool", &Value::Bool(true)),
|
|
|
|
("string", &Value::String("bla".into())),
|
|
|
|
("fraction", &Value::Fraction(1, 2)),
|
|
|
|
("array",
|
|
|
|
&Value::Array(vec![Value::Int(1), Value::Int(2)]))]);
|
2016-12-30 11:11:30 +00:00
|
|
|
assert_eq!(caps.to_string(),
|
|
|
|
"foo/bar, int=(int)12, bool=(boolean)true, string=(string)bla, \
|
2017-01-13 10:52:41 +00:00
|
|
|
fraction=(fraction)1/2, array=(int)< 1, 2 >");
|
2016-12-30 11:11:30 +00:00
|
|
|
}
|
|
|
|
}
|