1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use Object;
use Plugin;
use PluginFeature;
use ffi;
use glib;
use glib::object::IsA;
use glib::signal::SignalHandlerId;
use glib::signal::connect;
use glib::translate::*;
use glib_ffi;
use gobject_ffi;
use std;
use std::boxed::Box as Box_;
use std::mem;
use std::mem::transmute;
use std::ptr;

glib_wrapper! {
    /// One registry holds the metadata of a set of plugins.
    ///
    /// <emphasis role="bold">Design:`</emphasis>`
    ///
    /// The `Registry` object is a list of plugins and some functions for dealing
    /// with them. Each `Plugin` is matched 1-1 with a file on disk, and may or may
    /// not be loaded at a given time.
    ///
    /// The primary source, at all times, of plugin information is each plugin file
    /// itself. Thus, if an application wants information about a particular plugin,
    /// or wants to search for a feature that satisfies given criteria, the primary
    /// means of doing so is to load every plugin and look at the resulting
    /// information that is gathered in the default registry. Clearly, this is a time
    /// consuming process, so we cache information in the registry file. The format
    /// and location of the cache file is internal to gstreamer.
    ///
    /// On startup, plugins are searched for in the plugin search path. The following
    /// locations are checked in this order:
    ///
    /// * location from --gst-plugin-path commandline option.
    /// * the GST_PLUGIN_PATH environment variable.
    /// * the GST_PLUGIN_SYSTEM_PATH environment variable.
    /// * default locations (if GST_PLUGIN_SYSTEM_PATH is not set).
    ///  Those default locations are:
    ///  `$XDG_DATA_HOME/gstreamer-$GST_API_VERSION/plugins/`
    ///  and `$prefix/libs/gstreamer-$GST_API_VERSION/`.
    ///  [$XDG_DATA_HOME](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) defaults to
    ///  `$HOME/.local/share`.
    ///
    /// The registry cache file is loaded from
    /// `$XDG_CACHE_HOME/gstreamer-$GST_API_VERSION/registry-$ARCH.bin`
    /// (where $XDG_CACHE_HOME defaults to `$HOME/.cache`) or the file listed in the `GST_REGISTRY`
    /// env var. One reason to change the registry location is for testing.
    ///
    /// For each plugin that is found in the plugin search path, there could be 3
    /// possibilities for cached information:
    ///
    ///  * the cache may not contain information about a given file.
    ///  * the cache may have stale information.
    ///  * the cache may have current information.
    ///
    /// In the first two cases, the plugin is loaded and the cache updated. In
    /// addition to these cases, the cache may have entries for plugins that are not
    /// relevant to the current process. These are marked as not available to the
    /// current process. If the cache is updated for whatever reason, it is marked
    /// dirty.
    ///
    /// A dirty cache is written out at the end of initialization. Each entry is
    /// checked to make sure the information is minimally valid. If not, the entry is
    /// simply dropped.
    ///
    /// ## Implementation notes:
    ///
    /// The "cache" and "registry" are different concepts and can represent
    /// different sets of plugins. For various reasons, at init time, the cache is
    /// stored in the default registry, and plugins not relevant to the current
    /// process are marked with the `PluginFlags::Cached` bit. These plugins are
    /// removed at the end of initialization.
    ///
    /// # Implements
    ///
    /// [`GstObjectExt`](trait.GstObjectExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html)
    pub struct Registry(Object<ffi::GstRegistry, ffi::GstRegistryClass>): Object;

    match fn {
        get_type => || ffi::gst_registry_get_type(),
    }
}

impl Registry {
    /// Add the feature to the registry. The feature-added signal will be emitted.
    ///
    /// `feature`'s reference count will be incremented, and any floating
    /// reference will be removed (see `Object::ref_sink`)
    /// ## `feature`
    /// the feature to add
    ///
    /// # Returns
    ///
    /// `true` on success.
    ///
    /// MT safe.
    pub fn add_feature<P: IsA<PluginFeature>>(&self, feature: &P) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::error::BoolError::from_glib(ffi::gst_registry_add_feature(self.to_glib_none().0, feature.to_glib_none().0), "Failed to add feature")
        }
    }

    /// Add the plugin to the registry. The plugin-added signal will be emitted.
    ///
    /// `plugin`'s reference count will be incremented, and any floating
    /// reference will be removed (see `Object::ref_sink`)
    /// ## `plugin`
    /// the plugin to add
    ///
    /// # Returns
    ///
    /// `true` on success.
    ///
    /// MT safe.
    pub fn add_plugin(&self, plugin: &Plugin) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::error::BoolError::from_glib(ffi::gst_registry_add_plugin(self.to_glib_none().0, plugin.to_glib_none().0), "Failed to add plugin")
        }
    }

    /// Checks whether a plugin feature by the given name exists in
    /// `self` and whether its version is at least the
    /// version required.
    /// ## `feature_name`
    /// the name of the feature (e.g. "oggdemux")
    /// ## `min_major`
    /// the minimum major version number
    /// ## `min_minor`
    /// the minimum minor version number
    /// ## `min_micro`
    /// the minimum micro version number
    ///
    /// # Returns
    ///
    /// `true` if the feature could be found and the version is
    /// the same as the required version or newer, and `false` otherwise.
    pub fn check_feature_version(&self, feature_name: &str, min_major: u32, min_minor: u32, min_micro: u32) -> bool {
        unsafe {
            from_glib(ffi::gst_registry_check_feature_version(self.to_glib_none().0, feature_name.to_glib_none().0, min_major, min_minor, min_micro))
        }
    }

    //pub fn feature_filter<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&self, filter: /*Unknown conversion*//*Unimplemented*/PluginFeatureFilter, first: bool, user_data: P) -> Vec<PluginFeature> {
    //    unsafe { TODO: call ffi::gst_registry_feature_filter() }
    //}

    /// Find the pluginfeature with the given name and type in the registry.
    /// ## `name`
    /// the pluginfeature name to find
    /// ## `type_`
    /// the pluginfeature type to find
    ///
    /// # Returns
    ///
    /// the pluginfeature with the
    ///  given name and type or `None` if the plugin was not
    ///  found. `GstObjectExt::unref` after usage.
    ///
    /// MT safe.
    pub fn find_feature(&self, name: &str, type_: glib::types::Type) -> Option<PluginFeature> {
        unsafe {
            from_glib_full(ffi::gst_registry_find_feature(self.to_glib_none().0, name.to_glib_none().0, type_.to_glib()))
        }
    }

    /// Find the plugin with the given name in the registry.
    /// The plugin will be reffed; caller is responsible for unreffing.
    /// ## `name`
    /// the plugin name to find
    ///
    /// # Returns
    ///
    /// the plugin with the given name
    ///  or `None` if the plugin was not found. `GstObjectExt::unref` after
    ///  usage.
    ///
    /// MT safe.
    pub fn find_plugin(&self, name: &str) -> Option<Plugin> {
        unsafe {
            from_glib_full(ffi::gst_registry_find_plugin(self.to_glib_none().0, name.to_glib_none().0))
        }
    }

    /// Retrieves a `glib::List` of `PluginFeature` of `type_`.
    /// ## `type_`
    /// a `glib::Type`.
    ///
    /// # Returns
    ///
    /// a `glib::List` of
    ///  `PluginFeature` of `type_`. Use `PluginFeature::list_free` after use
    ///
    /// MT safe.
    pub fn get_feature_list(&self, type_: glib::types::Type) -> Vec<PluginFeature> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::gst_registry_get_feature_list(self.to_glib_none().0, type_.to_glib()))
        }
    }

    /// Retrieves a `glib::List` of features of the plugin with name `name`.
    /// ## `name`
    /// a plugin name.
    ///
    /// # Returns
    ///
    /// a `glib::List` of
    ///  `PluginFeature`. Use `PluginFeature::list_free` after usage.
    pub fn get_feature_list_by_plugin(&self, name: &str) -> Vec<PluginFeature> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::gst_registry_get_feature_list_by_plugin(self.to_glib_none().0, name.to_glib_none().0))
        }
    }

    /// Returns the registry's feature list cookie. This changes
    /// every time a feature is added or removed from the registry.
    ///
    /// # Returns
    ///
    /// the feature list cookie.
    pub fn get_feature_list_cookie(&self) -> u32 {
        unsafe {
            ffi::gst_registry_get_feature_list_cookie(self.to_glib_none().0)
        }
    }

    /// Get a copy of all plugins registered in the given registry. The refcount
    /// of each element in the list in incremented.
    ///
    /// # Returns
    ///
    /// a `glib::List` of `Plugin`.
    ///  Use `Plugin::list_free` after usage.
    ///
    /// MT safe.
    pub fn get_plugin_list(&self) -> Vec<Plugin> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ffi::gst_registry_get_plugin_list(self.to_glib_none().0))
        }
    }

    /// Look up a plugin in the given registry with the given filename.
    /// If found, plugin is reffed.
    /// ## `filename`
    /// the name of the file to look up
    ///
    /// # Returns
    ///
    /// the `Plugin` if found, or
    ///  `None` if not. `GstObjectExt::unref` after usage.
    pub fn lookup(&self, filename: &str) -> Option<Plugin> {
        unsafe {
            from_glib_full(ffi::gst_registry_lookup(self.to_glib_none().0, filename.to_glib_none().0))
        }
    }

    /// Find a `PluginFeature` with `name` in `self`.
    /// ## `name`
    /// a `PluginFeature` name
    ///
    /// # Returns
    ///
    /// a `PluginFeature` with its refcount incremented,
    ///  use `GstObjectExt::unref` after usage.
    ///
    /// MT safe.
    pub fn lookup_feature(&self, name: &str) -> Option<PluginFeature> {
        unsafe {
            from_glib_full(ffi::gst_registry_lookup_feature(self.to_glib_none().0, name.to_glib_none().0))
        }
    }

    //pub fn plugin_filter<P: Into<Option</*Unimplemented*/Fundamental: Pointer>>>(&self, filter: /*Unknown conversion*//*Unimplemented*/PluginFilter, first: bool, user_data: P) -> Vec<Plugin> {
    //    unsafe { TODO: call ffi::gst_registry_plugin_filter() }
    //}

    /// Remove the feature from the registry.
    ///
    /// MT safe.
    /// ## `feature`
    /// the feature to remove
    pub fn remove_feature<P: IsA<PluginFeature>>(&self, feature: &P) {
        unsafe {
            ffi::gst_registry_remove_feature(self.to_glib_none().0, feature.to_glib_none().0);
        }
    }

    /// Remove the plugin from the registry.
    ///
    /// MT safe.
    /// ## `plugin`
    /// the plugin to remove
    pub fn remove_plugin(&self, plugin: &Plugin) {
        unsafe {
            ffi::gst_registry_remove_plugin(self.to_glib_none().0, plugin.to_glib_none().0);
        }
    }

    /// Scan the given path for plugins to add to the registry. The syntax of the
    /// path is specific to the registry.
    /// ## `path`
    /// the path to scan
    ///
    /// # Returns
    ///
    /// `true` if registry changed
    pub fn scan_path<P: AsRef<std::path::Path>>(&self, path: P) -> bool {
        unsafe {
            from_glib(ffi::gst_registry_scan_path(self.to_glib_none().0, path.as_ref().to_glib_none().0))
        }
    }

    /// Retrieves the singleton plugin registry. The caller does not own a
    /// reference on the registry, as it is alive as long as GStreamer is
    /// initialized.
    ///
    /// # Returns
    ///
    /// the `Registry`.
    pub fn get() -> Registry {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_none(ffi::gst_registry_get())
        }
    }

    pub fn connect_feature_added<F: Fn(&Registry, &PluginFeature) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe {
            let f: Box_<Box_<Fn(&Registry, &PluginFeature) + Send + Sync + 'static>> = Box_::new(Box_::new(f));
            connect(self.to_glib_none().0, "feature-added",
                transmute(feature_added_trampoline as usize), Box_::into_raw(f) as *mut _)
        }
    }

    pub fn connect_plugin_added<F: Fn(&Registry, &Plugin) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe {
            let f: Box_<Box_<Fn(&Registry, &Plugin) + Send + Sync + 'static>> = Box_::new(Box_::new(f));
            connect(self.to_glib_none().0, "plugin-added",
                transmute(plugin_added_trampoline as usize), Box_::into_raw(f) as *mut _)
        }
    }
}

unsafe impl Send for Registry {}
unsafe impl Sync for Registry {}

unsafe extern "C" fn feature_added_trampoline(this: *mut ffi::GstRegistry, feature: *mut ffi::GstPluginFeature, f: glib_ffi::gpointer) {
    let f: &&(Fn(&Registry, &PluginFeature) + Send + Sync + 'static) = transmute(f);
    f(&from_glib_borrow(this), &from_glib_borrow(feature))
}

unsafe extern "C" fn plugin_added_trampoline(this: *mut ffi::GstRegistry, plugin: *mut ffi::GstPlugin, f: glib_ffi::gpointer) {
    let f: &&(Fn(&Registry, &Plugin) + Send + Sync + 'static) = transmute(f);
    f(&from_glib_borrow(this), &from_glib_borrow(plugin))
}