Use glib GEnum derive macro instead of implementing this manually

This commit is contained in:
Sebastian Dröge 2021-02-23 12:40:28 +02:00
parent 0aef5152a7
commit bb1e1b1529
2 changed files with 5 additions and 94 deletions

View file

@ -9,7 +9,6 @@ edition = "2018"
[dependencies] [dependencies]
glib = "0.10" glib = "0.10"
gobject-sys = "0.10"
gst = { package = "gstreamer", version = "0.16", features = ["v1_12"] } gst = { package = "gstreamer", version = "0.16", features = ["v1_12"] }
gst-base = { package = "gstreamer-base", version = "0.16" } gst-base = { package = "gstreamer-base", version = "0.16" }
gst-audio = { package = "gstreamer-audio", version = "0.16" } gst-audio = { package = "gstreamer-audio", version = "0.16" }

View file

@ -16,11 +16,15 @@ use std::time;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy, glib::GEnum)]
#[repr(u32)] #[repr(u32)]
#[genum(type_name = "GstNdiTimestampMode")]
pub enum TimestampMode { pub enum TimestampMode {
#[genum(name = "Receive Time", nick = "receive-time")]
ReceiveTime = 0, ReceiveTime = 0,
#[genum(name = "NDI Timecode", nick = "timecode")]
Timecode = 1, Timecode = 1,
#[genum(name = "NDI Timestamp", nick = "timestamp")]
Timestamp = 2, Timestamp = 2,
} }
@ -50,98 +54,6 @@ static TIMECODE_CAPS: Lazy<gst::Caps> =
static TIMESTAMP_CAPS: Lazy<gst::Caps> = static TIMESTAMP_CAPS: Lazy<gst::Caps> =
Lazy::new(|| gst::Caps::new_simple("timestamp/x-ndi-timestamp", &[])); Lazy::new(|| gst::Caps::new_simple("timestamp/x-ndi-timestamp", &[]));
impl glib::translate::ToGlib for TimestampMode {
type GlibType = i32;
fn to_glib(&self) -> i32 {
*self as i32
}
}
impl glib::translate::FromGlib<i32> for TimestampMode {
fn from_glib(value: i32) -> Self {
match value {
0 => TimestampMode::ReceiveTime,
1 => TimestampMode::Timecode,
2 => TimestampMode::Timestamp,
_ => unreachable!(),
}
}
}
impl StaticType for TimestampMode {
fn static_type() -> glib::Type {
timestamp_mode_get_type()
}
}
impl<'a> glib::value::FromValueOptional<'a> for TimestampMode {
unsafe fn from_value_optional(value: &glib::Value) -> Option<Self> {
Some(glib::value::FromValue::from_value(value))
}
}
impl<'a> glib::value::FromValue<'a> for TimestampMode {
unsafe fn from_value(value: &glib::Value) -> Self {
use glib::translate::ToGlibPtr;
glib::translate::from_glib(gobject_sys::g_value_get_enum(value.to_glib_none().0))
}
}
impl glib::value::SetValue for TimestampMode {
unsafe fn set_value(value: &mut glib::Value, this: &Self) {
use glib::translate::{ToGlib, ToGlibPtrMut};
gobject_sys::g_value_set_enum(value.to_glib_none_mut().0, this.to_glib())
}
}
fn timestamp_mode_get_type() -> glib::Type {
use std::sync::Once;
static ONCE: Once = Once::new();
static mut TYPE: glib::Type = glib::Type::Invalid;
ONCE.call_once(|| {
use std::ffi;
use std::ptr;
static mut VALUES: [gobject_sys::GEnumValue; 4] = [
gobject_sys::GEnumValue {
value: TimestampMode::ReceiveTime as i32,
value_name: b"Receive Time\0" as *const _ as *const _,
value_nick: b"receive-time\0" as *const _ as *const _,
},
gobject_sys::GEnumValue {
value: TimestampMode::Timecode as i32,
value_name: b"NDI Timecode\0" as *const _ as *const _,
value_nick: b"timecode\0" as *const _ as *const _,
},
gobject_sys::GEnumValue {
value: TimestampMode::Timestamp as i32,
value_name: b"NDI Timestamp\0" as *const _ as *const _,
value_nick: b"timestamp\0" as *const _ as *const _,
},
gobject_sys::GEnumValue {
value: 0,
value_name: ptr::null(),
value_nick: ptr::null(),
},
];
let name = ffi::CString::new("GstNdiTimestampMode").unwrap();
unsafe {
let type_ = gobject_sys::g_enum_register_static(name.as_ptr(), VALUES.as_ptr());
TYPE = glib::translate::from_glib(type_);
}
});
unsafe {
assert_ne!(TYPE, glib::Type::Invalid);
TYPE
}
}
gst::gst_plugin_define!( gst::gst_plugin_define!(
ndi, ndi,
env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_DESCRIPTION"),