Move Value code into its own module

This commit is contained in:
Sebastian Dröge 2017-01-13 19:16:12 +02:00
parent 93f09c8a4a
commit ac9ba47403
4 changed files with 139 additions and 115 deletions

View file

@ -31,7 +31,7 @@ use gst_plugin::utils;
use gst_plugin::utils::Element;
use gst_plugin::log::*;
use gst_plugin::caps::Caps;
use gst_plugin::caps;
use gst_plugin::value;
use gst_plugin::bytes::*;
use slog::*;
@ -157,8 +157,8 @@ impl AudioFormat {
flavors::SoundFormat::MP3 |
flavors::SoundFormat::MP3_8KHZ => {
Some(Caps::new_simple("audio/mpeg",
&[("mpegversion", &caps::Value::Int(1)),
("layer", &caps::Value::Int(3))]))
&[("mpegversion", &value::Value::Int(1)),
("layer", &value::Value::Int(3))]))
}
flavors::SoundFormat::PCM_NE |
flavors::SoundFormat::PCM_LE => {
@ -167,9 +167,9 @@ impl AudioFormat {
// way to know what the endianness of the system creating the stream was
Some(Caps::new_simple("audio/x-raw",
&[("layout",
&caps::Value::String("interleaved".into())),
&value::Value::String("interleaved".into())),
("format",
&caps::Value::String(if self.width == 8 {
&value::Value::String(if self.width == 8 {
"U8".into()
} else {
"S16LE".into()
@ -180,7 +180,7 @@ impl AudioFormat {
}
flavors::SoundFormat::ADPCM => {
Some(Caps::new_simple("audio/x-adpcm",
&[("layout", &caps::Value::String("swf".into()))]))
&[("layout", &value::Value::String("swf".into()))]))
}
flavors::SoundFormat::NELLYMOSER_16KHZ_MONO |
flavors::SoundFormat::NELLYMOSER_8KHZ_MONO |
@ -190,10 +190,10 @@ impl AudioFormat {
flavors::SoundFormat::AAC => {
self.aac_sequence_header.as_ref().map(|header| {
Caps::new_simple("audio/mpeg",
&[("mpegversion", &caps::Value::Int(4)),
("framed", &caps::Value::Bool(true)),
("stream-format", &caps::Value::String("raw".into())),
("codec_data", &caps::Value::Buffer(header.clone()))])
&[("mpegversion", &value::Value::Int(4)),
("framed", &value::Value::Bool(true)),
("stream-format", &value::Value::String("raw".into())),
("codec_data", &value::Value::Buffer(header.clone()))])
})
}
flavors::SoundFormat::SPEEX => {
@ -231,8 +231,8 @@ impl AudioFormat {
}
Some(Caps::new_simple("audio/x-speex",
&[("streamheader",
&caps::Value::Array(vec![caps::Value::Buffer(header),
caps::Value::Buffer(comment)]))]))
&value::Value::Array(vec![value::Value::Buffer(header),
value::Value::Buffer(comment)]))]))
}
flavors::SoundFormat::DEVICE_SPECIFIC => {
// Nobody knows
@ -242,11 +242,11 @@ impl AudioFormat {
if self.rate != 0 {
caps.as_mut()
.map(|c| c.set_simple(&[("rate", &caps::Value::Int(self.rate as i32))]));
.map(|c| c.set_simple(&[("rate", &value::Value::Int(self.rate as i32))]));
}
if self.channels != 0 {
caps.as_mut()
.map(|c| c.set_simple(&[("channels", &caps::Value::Int(self.channels as i32))]));
.map(|c| c.set_simple(&[("channels", &value::Value::Int(self.channels as i32))]));
}
caps
@ -319,7 +319,7 @@ impl VideoFormat {
let mut caps = match self.format {
flavors::CodecId::SORENSON_H263 => {
Some(Caps::new_simple("video/x-flash-video",
&[("flvversion", &caps::Value::Int(1))]))
&[("flvversion", &value::Value::Int(1))]))
}
flavors::CodecId::SCREEN => Some(Caps::new_simple("video/x-flash-screen", &[])),
flavors::CodecId::VP6 => Some(Caps::new_simple("video/x-vp6-flash", &[])),
@ -328,15 +328,15 @@ impl VideoFormat {
flavors::CodecId::H264 => {
self.avc_sequence_header.as_ref().map(|header| {
Caps::new_simple("video/x-h264",
&[("stream-format", &caps::Value::String("avc".into())),
("codec_data", &caps::Value::Buffer(header.clone()))])
&[("stream-format", &value::Value::String("avc".into())),
("codec_data", &value::Value::Buffer(header.clone()))])
})
}
flavors::CodecId::H263 => Some(Caps::new_simple("video/x-h263", &[])),
flavors::CodecId::MPEG4Part2 => {
Some(Caps::new_simple("video/x-h263",
&[("mpegversion", &caps::Value::Int(4)),
("systemstream", &caps::Value::Bool(false))]))
&[("mpegversion", &value::Value::Int(4)),
("systemstream", &value::Value::Bool(false))]))
}
flavors::CodecId::JPEG => {
// Unused according to spec
@ -346,8 +346,8 @@ impl VideoFormat {
if let (Some(width), Some(height)) = (self.width, self.height) {
caps.as_mut().map(|c| {
c.set_simple(&[("width", &caps::Value::Int(width as i32)),
("height", &caps::Value::Int(height as i32))])
c.set_simple(&[("width", &value::Value::Int(width as i32)),
("height", &value::Value::Int(height as i32))])
});
}
@ -355,7 +355,7 @@ impl VideoFormat {
if par.0 != 0 && par.1 != 0 {
caps.as_mut().map(|c| {
c.set_simple(&[("pixel-aspect-ratio",
&caps::Value::Fraction(par.0 as i32, par.1 as i32))])
&value::Value::Fraction(par.0 as i32, par.1 as i32))])
});
}
}
@ -364,7 +364,7 @@ impl VideoFormat {
if fps.1 != 0 {
caps.as_mut().map(|c| {
c.set_simple(&[("framerate",
&caps::Value::Fraction(fps.0 as i32, fps.1 as i32))])
&value::Value::Fraction(fps.0 as i32, fps.1 as i32))])
});
}
}

View file

@ -19,103 +19,12 @@ use libc::c_char;
use std::os::raw::c_void;
use std::ffi::CString;
use std::ffi::CStr;
use std::mem;
use std::fmt;
use buffer::*;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Value {
Bool(bool),
Int(i32),
String(String),
Fraction(i32, i32),
Buffer(Buffer),
Array(Vec<Value>),
}
use value::*;
pub struct Caps(*mut c_void);
#[repr(C)]
struct GValue {
typ: usize,
data: [u64; 2],
}
// See gtype.h
const TYPE_BOOLEAN: usize = (5 << 2);
const TYPE_INT: usize = (6 << 2);
const TYPE_STRING: usize = (16 << 2);
impl Value {
fn to_gvalue(&self) -> GValue {
extern "C" {
fn g_value_init(value: *mut GValue, gtype: usize);
fn g_value_set_boolean(value: *mut GValue, value: i32);
fn g_value_set_int(value: *mut GValue, value: i32);
fn g_value_set_string(value: *mut GValue, value: *const c_char);
fn gst_value_set_fraction(value: *mut GValue, value_n: i32, value_d: i32);
fn gst_fraction_get_type() -> usize;
fn g_value_set_boxed(value: *mut GValue, boxed: *const c_void);
fn gst_buffer_get_type() -> usize;
fn gst_value_array_get_type() -> usize;
fn gst_value_array_append_and_take_value(value: *mut GValue, element: *mut GValue);
}
let mut gvalue: GValue = unsafe { mem::zeroed() };
match *self {
Value::Bool(v) => unsafe {
g_value_init(&mut gvalue as *mut GValue, TYPE_BOOLEAN);
g_value_set_boolean(&mut gvalue as *mut GValue, if v { 1 } else { 0 });
},
Value::Int(v) => unsafe {
g_value_init(&mut gvalue as *mut GValue, TYPE_INT);
g_value_set_int(&mut gvalue as *mut GValue, v);
},
Value::String(ref v) => unsafe {
let v_cstr = CString::new(String::from(v.clone())).unwrap();
g_value_init(&mut gvalue as *mut GValue, TYPE_STRING);
g_value_set_string(&mut gvalue as *mut GValue, v_cstr.as_ptr());
},
Value::Fraction(v_n, v_d) => unsafe {
g_value_init(&mut gvalue as *mut GValue, gst_fraction_get_type());
gst_value_set_fraction(&mut gvalue as *mut GValue, v_n, v_d);
},
Value::Buffer(ref buffer) => unsafe {
g_value_init(&mut gvalue as *mut GValue, gst_buffer_get_type());
g_value_set_boxed(&mut gvalue as *mut GValue, buffer.as_ptr());
},
Value::Array(ref array) => unsafe {
g_value_init(&mut gvalue as *mut GValue, gst_value_array_get_type());
for e in array {
let mut e_value = e.to_gvalue();
gst_value_array_append_and_take_value(&mut gvalue as *mut GValue,
&mut e_value as *mut GValue);
// Takes ownership, invalidate GValue
e_value.typ = 0;
}
},
}
gvalue
}
}
impl Drop for GValue {
fn drop(&mut self) {
extern "C" {
fn g_value_unset(value: *mut GValue);
}
if self.typ != 0 {
unsafe { g_value_unset(self as *mut GValue) }
}
}
}
impl Caps {
pub fn new_empty() -> Self {
extern "C" {
@ -243,6 +152,7 @@ impl fmt::Debug for Caps {
#[cfg(test)]
mod tests {
use super::*;
use value::*;
use std::ptr;
use std::os::raw::c_void;

View file

@ -38,5 +38,6 @@ pub mod source;
pub mod sink;
pub mod demuxer;
pub mod log;
pub mod value;
pub mod caps;
pub mod bytes;

113
gst-plugin/src/value.rs Normal file
View file

@ -0,0 +1,113 @@
// 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::mem;
use buffer::*;
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Value {
Bool(bool),
Int(i32),
String(String),
Fraction(i32, i32),
Buffer(Buffer),
Array(Vec<Value>),
}
#[repr(C)]
pub struct GValue {
typ: usize,
data: [u64; 2],
}
// See gtype.h
const TYPE_BOOLEAN: usize = (5 << 2);
const TYPE_INT: usize = (6 << 2);
const TYPE_STRING: usize = (16 << 2);
impl Value {
pub fn to_gvalue(&self) -> GValue {
extern "C" {
fn g_value_init(value: *mut GValue, gtype: usize);
fn g_value_set_boolean(value: *mut GValue, value: i32);
fn g_value_set_int(value: *mut GValue, value: i32);
fn g_value_set_string(value: *mut GValue, value: *const c_char);
fn gst_value_set_fraction(value: *mut GValue, value_n: i32, value_d: i32);
fn gst_fraction_get_type() -> usize;
fn g_value_set_boxed(value: *mut GValue, boxed: *const c_void);
fn gst_buffer_get_type() -> usize;
fn gst_value_array_get_type() -> usize;
fn gst_value_array_append_and_take_value(value: *mut GValue, element: *mut GValue);
}
let mut gvalue: GValue = unsafe { mem::zeroed() };
match *self {
Value::Bool(v) => unsafe {
g_value_init(&mut gvalue as *mut GValue, TYPE_BOOLEAN);
g_value_set_boolean(&mut gvalue as *mut GValue, if v { 1 } else { 0 });
},
Value::Int(v) => unsafe {
g_value_init(&mut gvalue as *mut GValue, TYPE_INT);
g_value_set_int(&mut gvalue as *mut GValue, v);
},
Value::String(ref v) => unsafe {
let v_cstr = CString::new(String::from(v.clone())).unwrap();
g_value_init(&mut gvalue as *mut GValue, TYPE_STRING);
g_value_set_string(&mut gvalue as *mut GValue, v_cstr.as_ptr());
},
Value::Fraction(v_n, v_d) => unsafe {
g_value_init(&mut gvalue as *mut GValue, gst_fraction_get_type());
gst_value_set_fraction(&mut gvalue as *mut GValue, v_n, v_d);
},
Value::Buffer(ref buffer) => unsafe {
g_value_init(&mut gvalue as *mut GValue, gst_buffer_get_type());
g_value_set_boxed(&mut gvalue as *mut GValue, buffer.as_ptr());
},
Value::Array(ref array) => unsafe {
g_value_init(&mut gvalue as *mut GValue, gst_value_array_get_type());
for e in array {
let mut e_value = e.to_gvalue();
gst_value_array_append_and_take_value(&mut gvalue as *mut GValue,
&mut e_value as *mut GValue);
// Takes ownership, invalidate GValue
e_value.typ = 0;
}
},
}
gvalue
}
}
impl Drop for GValue {
fn drop(&mut self) {
extern "C" {
fn g_value_unset(value: *mut GValue);
}
if self.typ != 0 {
unsafe { g_value_unset(self as *mut GValue) }
}
}
}