gst_plugin/
plugin.rs

1// Copyright (C) 2016-2017 Sebastian Dröge <sebastian@centricular.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#[macro_export]
10macro_rules! plugin_define(
11    ($name:expr, $description:expr, $plugin_init:ident,
12     $version:expr, $license:expr, $source:expr,
13     $package:expr, $origin:expr, $release_datetime:expr) => {
14        pub mod plugin_desc {
15            use $crate::glib::translate::{from_glib_borrow, ToGlib};
16
17            // Not using c_char here because it requires the libc crate
18            #[allow(non_camel_case_types)]
19            type c_char = i8;
20
21            #[repr(C)]
22            pub struct GstPluginDesc($crate::gst_ffi::GstPluginDesc);
23            unsafe impl Sync for GstPluginDesc {}
24
25            #[no_mangle]
26            #[allow(non_upper_case_globals)]
27            pub static gst_plugin_desc: GstPluginDesc = GstPluginDesc($crate::gst_ffi::GstPluginDesc {
28                major_version: 1,
29                minor_version: 8,
30                name: $name as *const u8 as *const c_char,
31                description: $description as *const u8 as *const c_char,
32                plugin_init: Some(plugin_init_trampoline),
33                version: $version as *const u8 as *const c_char,
34                license: $license as *const u8 as *const c_char,
35                source: $source as *const u8 as *const c_char,
36                package: $package as *const u8 as *const c_char,
37                origin: $origin as *const u8 as *const c_char,
38                release_datetime: $release_datetime as *const u8 as *const c_char,
39                _gst_reserved: [0 as $crate::glib_ffi::gpointer; 4],
40            });
41
42            unsafe extern "C" fn plugin_init_trampoline(plugin: *mut $crate::gst_ffi::GstPlugin) -> $crate::glib_ffi::gboolean {
43                use std::panic::{self, AssertUnwindSafe};
44
45                let result = panic::catch_unwind(AssertUnwindSafe(|| super::$plugin_init(&from_glib_borrow(plugin)).to_glib()));
46                match result {
47                    Ok(result) => result,
48                    Err(err) => {
49                        let cat = $crate::gst::DebugCategory::get("GST_PLUGIN_LOADING").unwrap();
50                        if let Some(cause) = err.downcast_ref::<&str>() {
51                            gst_error!(cat, "Failed to initialize plugin due to panic: {}", cause);
52                        } else if let Some(cause) = err.downcast_ref::<String>() {
53                            gst_error!(cat, "Failed to initialize plugin due to panic: {}", cause);
54                        } else {
55                            gst_error!(cat, "Failed to initialize plugin due to panic");
56                        }
57
58                        $crate::glib_ffi::GFALSE
59                    }
60                }
61            }
62        }
63    };
64);