diff --git a/gstreamer/Cargo.toml b/gstreamer/Cargo.toml index 51d0d79cf..a88e0e1bb 100644 --- a/gstreamer/Cargo.toml +++ b/gstreamer/Cargo.toml @@ -27,6 +27,7 @@ muldiv = "0.2" serde = { version = "1.0", optional = true } serde_bytes = { version = "0.11", optional = true } serde_derive = { version = "1.0", optional = true } +paste = "0.1" [build-dependencies.rustdoc-stripper] version = "0.1" diff --git a/gstreamer/src/subclass/mod.rs b/gstreamer/src/subclass/mod.rs index 892627575..c6343ae68 100644 --- a/gstreamer/src/subclass/mod.rs +++ b/gstreamer/src/subclass/mod.rs @@ -10,7 +10,15 @@ #[macro_use] pub mod error; + +#[cfg(any(feature = "v1_14"))] #[macro_use] +#[path = "plugin_1_14.rs"] +pub mod plugin; + +#[cfg(not(any(feature = "v1_14")))] +#[macro_use] +#[path = "plugin_1_12.rs"] pub mod plugin; pub mod bin; diff --git a/gstreamer/src/subclass/plugin.rs b/gstreamer/src/subclass/plugin_1_12.rs similarity index 91% rename from gstreamer/src/subclass/plugin.rs rename to gstreamer/src/subclass/plugin_1_12.rs index 2e584f192..89815e425 100644 --- a/gstreamer/src/subclass/plugin.rs +++ b/gstreamer/src/subclass/plugin_1_12.rs @@ -12,16 +12,12 @@ pub const MAJOR_VERSION: i32 = 1; pub const MINOR_VERSION: i32 = 8; #[cfg(all(feature = "v1_10", not(feature = "v1_12")))] pub const MINOR_VERSION: i32 = 10; -#[cfg(all(feature = "v1_12", not(feature = "v1_14")))] +#[cfg(feature = "v1_12")] pub const MINOR_VERSION: i32 = 12; -#[cfg(all(feature = "v1_14", not(feature = "v1_16")))] -pub const MINOR_VERSION: i32 = 14; -#[cfg(all(feature = "v1_16", not(feature = "v1_18")))] -pub const MINOR_VERSION: i32 = 16; #[macro_export] macro_rules! gst_plugin_define( - ($name:expr, $description:expr, $plugin_init:ident, + ($name:ident, $description:expr, $plugin_init:ident, $version:expr, $license:expr, $source:expr, $package:expr, $origin:expr, $release_datetime:expr) => { pub mod plugin_desc { @@ -36,7 +32,7 @@ macro_rules! gst_plugin_define( pub static gst_plugin_desc: GstPluginDesc = GstPluginDesc($crate::gst_sys::GstPluginDesc { major_version: $crate::subclass::plugin::MAJOR_VERSION, minor_version: $crate::subclass::plugin::MINOR_VERSION, - name: concat!($name, "\0") as *const str as *const _, + name: concat!(stringify!($name), "\0") as *const str as *const _, description: concat!($description, "\0") as *const str as *const _, plugin_init: Some(plugin_init_trampoline), version: concat!($version, "\0") as *const str as *const _, @@ -54,7 +50,7 @@ macro_rules! gst_plugin_define( $crate::gst_sys::gst_plugin_register_static( $crate::subclass::plugin::MAJOR_VERSION, $crate::subclass::plugin::MINOR_VERSION, - concat!($name, "\0") as *const str as *const _, + concat!(stringify!($name), "\0") as *const str as *const _, concat!($description, "\0") as *const str as _, Some(plugin_init_trampoline), concat!($version, "\0") as *const str as *const _, diff --git a/gstreamer/src/subclass/plugin_1_14.rs b/gstreamer/src/subclass/plugin_1_14.rs new file mode 100644 index 000000000..ee1c5cc53 --- /dev/null +++ b/gstreamer/src/subclass/plugin_1_14.rs @@ -0,0 +1,105 @@ +// Copyright (C) 2016-2018 Sebastian Dröge +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub const MAJOR_VERSION: i32 = 1; + +#[cfg(all(feature = "v1_14", not(feature = "v1_16")))] +pub const MINOR_VERSION: i32 = 14; +#[cfg(all(feature = "v1_16", not(feature = "v1_18")))] +pub const MINOR_VERSION: i32 = 16; + +#[macro_export] +macro_rules! gst_plugin_define( + ($name:ident, $description:expr, $plugin_init:ident, + $version:expr, $license:expr, $source:expr, + $package:expr, $origin:expr, $release_datetime:expr) => { + pub mod plugin_desc { + use $crate::glib::translate::{from_glib_borrow, ToGlib, from_glib}; + + #[repr(C)] + pub struct GstPluginDesc($crate::gst_sys::GstPluginDesc); + unsafe impl Sync for GstPluginDesc {} + + static GST_PLUGIN_DESC: GstPluginDesc = GstPluginDesc($crate::gst_sys::GstPluginDesc { + major_version: $crate::subclass::plugin::MAJOR_VERSION, + minor_version: $crate::subclass::plugin::MINOR_VERSION, + name: concat!(stringify!($name), "\0") as *const str as *const _, + description: concat!($description, "\0") as *const str as *const _, + plugin_init: Some(plugin_init_trampoline), + version: concat!($version, "\0") as *const str as *const _, + license: concat!($license, "\0") as *const str as *const _, + source: concat!($source, "\0") as *const str as *const _, + package: concat!($package, "\0") as *const str as *const _, + origin: concat!($origin, "\0") as *const str as *const _, + release_datetime: concat!($release_datetime, "\0") as *const str as *const _, + _gst_reserved: [0 as $crate::glib_sys::gpointer; 4], + }); + + pub fn plugin_register_static() -> Result<(), glib::BoolError> { + unsafe { + glib_result_from_gboolean!( + $crate::gst_sys::gst_plugin_register_static( + $crate::subclass::plugin::MAJOR_VERSION, + $crate::subclass::plugin::MINOR_VERSION, + concat!(stringify!($name), "\0") as *const str as *const _, + concat!($description, "\0") as *const str as _, + Some(plugin_init_trampoline), + concat!($version, "\0") as *const str as *const _, + concat!($license, "\0") as *const str as *const _, + concat!($source, "\0") as *const str as *const _, + concat!($package, "\0") as *const str as *const _, + concat!($origin, "\0") as *const str as *const _, + ), + "Failed to register the plugin" + ) + } + } + + paste::item! { + #[no_mangle] + pub unsafe extern "C" fn [] () { + let _ = plugin_register_static(); + } + + #[no_mangle] + pub unsafe extern "C" fn [] () -> *const $crate::gst_sys::GstPluginDesc { + &GST_PLUGIN_DESC.0 + } + } + + unsafe extern "C" fn plugin_init_trampoline(plugin: *mut $crate::gst_sys::GstPlugin) -> $crate::glib_sys::gboolean { + use std::panic::{self, AssertUnwindSafe}; + + let panic_result = panic::catch_unwind(AssertUnwindSafe(|| super::$plugin_init(&from_glib_borrow(plugin)))); + match panic_result { + Ok(register_result) => match register_result { + Ok(_) => $crate::glib_sys::GTRUE, + Err(err) => { + let cat = $crate::DebugCategory::get("GST_PLUGIN_LOADING").unwrap(); + gst_error!(cat, "Failed to register plugin: {}", err); + $crate::glib_sys::GFALSE + } + } + Err(err) => { + let cat = $crate::DebugCategory::get("GST_PLUGIN_LOADING").unwrap(); + if let Some(cause) = err.downcast_ref::<&str>() { + gst_error!(cat, "Failed to initialize plugin due to panic: {}", cause); + } else if let Some(cause) = err.downcast_ref::() { + gst_error!(cat, "Failed to initialize plugin due to panic: {}", cause); + } else { + gst_error!(cat, "Failed to initialize plugin due to panic"); + } + + $crate::glib_sys::GFALSE + } + } + } + } + pub use plugin_desc::plugin_register_static; + }; +);