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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
// DO NOT EDIT

use crate::DiscovererInfo;
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// The [`Discoverer`][crate::Discoverer] is a utility object which allows to get as much
    /// information as possible from one or many URIs.
    ///
    /// It provides two APIs, allowing usage in blocking or non-blocking mode.
    ///
    /// The blocking mode just requires calling [`discover_uri()`][Self::discover_uri()]
    /// with the URI one wishes to discover.
    ///
    /// The non-blocking mode requires a running [`glib::MainLoop`][crate::glib::MainLoop] iterating a
    /// [`glib::MainContext`][crate::glib::MainContext], where one connects to the various signals, appends the
    /// URIs to be processed (through [`discover_uri_async()`][Self::discover_uri_async()]) and then
    /// asks for the discovery to begin (through [`start()`][Self::start()]).
    /// By default this will use the GLib default main context unless you have
    /// set a custom context using [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()].
    ///
    /// All the information is returned in a [`DiscovererInfo`][crate::DiscovererInfo] structure.
    ///
    /// ## Properties
    ///
    ///
    /// #### `timeout`
    ///  The duration (in nanoseconds) after which the discovery of an individual
    /// URI will timeout.
    ///
    /// If the discovery of a URI times out, the [`DiscovererResult::Timeout`][crate::DiscovererResult::Timeout] will be
    /// set on the result flags.
    ///
    /// Readable | Writeable | Construct
    ///
    ///
    /// #### `use-cache`
    ///  Readable | Writeable | Construct
    ///
    /// ## Signals
    ///
    ///
    /// #### `discovered`
    ///  Will be emitted in async mode when all information on a URI could be
    /// discovered, or an error occurred.
    ///
    /// When an error occurs, `info` might still contain some partial information,
    /// depending on the circumstances of the error.
    ///
    ///
    ///
    ///
    /// #### `finished`
    ///  Will be emitted in async mode when all pending URIs have been processed.
    ///
    ///
    ///
    ///
    /// #### `load-serialized-info`
    ///  Retrieves information about a URI from and external source of information,
    /// like a cache file. This is used by the discoverer to speed up the
    /// discovery.
    ///
    ///
    ///
    ///
    /// #### `source-setup`
    ///  This signal is emitted after the source element has been created for, so
    /// the URI being discovered, so it can be configured by setting additional
    /// properties (e.g. set a proxy server for an http source, or set the device
    /// and read speed for an audio cd source).
    ///
    /// This signal is usually emitted from the context of a GStreamer streaming
    /// thread.
    ///
    ///
    ///
    ///
    /// #### `starting`
    ///  Will be emitted when the discover starts analyzing the pending URIs
    ///
    ///
    ///
    /// # Implements
    ///
    /// [`trait@glib::ObjectExt`]
    #[doc(alias = "GstDiscoverer")]
    pub struct Discoverer(Object<ffi::GstDiscoverer, ffi::GstDiscovererClass>);

    match fn {
        type_ => || ffi::gst_discoverer_get_type(),
    }
}

impl Discoverer {
    /// Creates a new [`Discoverer`][crate::Discoverer] with the provided timeout.
    /// ## `timeout`
    /// timeout per file, in nanoseconds. Allowed are values between
    ///  one second (`GST_SECOND`) and one hour (3600 * `GST_SECOND`)
    ///
    /// # Returns
    ///
    /// The new [`Discoverer`][crate::Discoverer].
    /// If an error occurred when creating the discoverer, `err` will be set
    /// accordingly and [`None`] will be returned. If `err` is set, the caller must
    /// free it when no longer needed using `g_error_free()`.
    #[doc(alias = "gst_discoverer_new")]
    pub fn new(timeout: gst::ClockTime) -> Result<Discoverer, glib::Error> {
        assert_initialized_main_thread!();
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::gst_discoverer_new(timeout.into_glib(), &mut error);
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Synchronously discovers the given `uri`.
    ///
    /// A copy of `uri` will be made internally, so the caller can safely `g_free()`
    /// afterwards.
    /// ## `uri`
    /// The URI to run on.
    ///
    /// # Returns
    ///
    /// the result of the scanning. Can be [`None`] if an
    /// error occurred.
    #[doc(alias = "gst_discoverer_discover_uri")]
    pub fn discover_uri(&self, uri: &str) -> Result<DiscovererInfo, glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::gst_discoverer_discover_uri(
                self.to_glib_none().0,
                uri.to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Appends the given `uri` to the list of URIs to discoverer. The actual
    /// discovery of the `uri` will only take place if [`start()`][Self::start()] has
    /// been called.
    ///
    /// A copy of `uri` will be made internally, so the caller can safely `g_free()`
    /// afterwards.
    /// ## `uri`
    /// the URI to add.
    ///
    /// # Returns
    ///
    /// [`true`] if the `uri` was successfully appended to the list of pending
    /// uris, else [`false`]
    #[doc(alias = "gst_discoverer_discover_uri_async")]
    pub fn discover_uri_async(&self, uri: &str) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_discoverer_discover_uri_async(self.to_glib_none().0, uri.to_glib_none().0),
                "Failed to add URI to list of discovers"
            )
        }
    }

    /// Allow asynchronous discovering of URIs to take place.
    /// A [`glib::MainLoop`][crate::glib::MainLoop] must be available for [`Discoverer`][crate::Discoverer] to properly work in
    /// asynchronous mode.
    #[doc(alias = "gst_discoverer_start")]
    pub fn start(&self) {
        unsafe {
            ffi::gst_discoverer_start(self.to_glib_none().0);
        }
    }

    /// Stop the discovery of any pending URIs and clears the list of
    /// pending URIS (if any).
    #[doc(alias = "gst_discoverer_stop")]
    pub fn stop(&self) {
        unsafe {
            ffi::gst_discoverer_stop(self.to_glib_none().0);
        }
    }

    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "use-cache")]
    pub fn uses_cache(&self) -> bool {
        ObjectExt::property(self, "use-cache")
    }

    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "use-cache")]
    pub fn set_use_cache(&self, use_cache: bool) {
        ObjectExt::set_property(self, "use-cache", use_cache)
    }

    /// Will be emitted in async mode when all information on a URI could be
    /// discovered, or an error occurred.
    ///
    /// When an error occurs, `info` might still contain some partial information,
    /// depending on the circumstances of the error.
    /// ## `info`
    /// the results [`DiscovererInfo`][crate::DiscovererInfo]
    /// ## `error`
    /// [`glib::Error`][crate::glib::Error], which will be non-NULL
    ///  if an error occurred during
    ///  discovery. You must not free
    ///  this [`glib::Error`][crate::glib::Error], it will be freed by
    ///  the discoverer.
    #[doc(alias = "discovered")]
    pub fn connect_discovered<
        F: Fn(&Self, &DiscovererInfo, Option<&glib::Error>) + Send + Sync + 'static,
    >(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn discovered_trampoline<
            F: Fn(&Discoverer, &DiscovererInfo, Option<&glib::Error>) + Send + Sync + 'static,
        >(
            this: *mut ffi::GstDiscoverer,
            info: *mut ffi::GstDiscovererInfo,
            error: *mut glib::ffi::GError,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &from_glib_borrow(info),
                Option::<glib::Error>::from_glib_borrow(error)
                    .as_ref()
                    .as_ref(),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"discovered\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    discovered_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Will be emitted in async mode when all pending URIs have been processed.
    #[doc(alias = "finished")]
    pub fn connect_finished<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn finished_trampoline<F: Fn(&Discoverer) + Send + Sync + 'static>(
            this: *mut ffi::GstDiscoverer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"finished\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    finished_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Retrieves information about a URI from and external source of information,
    /// like a cache file. This is used by the discoverer to speed up the
    /// discovery.
    /// ## `uri`
    /// THe URI to load the serialized info for
    ///
    /// # Returns
    ///
    /// The [`DiscovererInfo`][crate::DiscovererInfo] representing
    /// `uri`, or [`None`] if no information
    #[cfg(feature = "v1_24")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
    #[doc(alias = "load-serialized-info")]
    pub fn connect_load_serialized_info<
        F: Fn(&Self, &str) -> Option<DiscovererInfo> + Send + Sync + 'static,
    >(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn load_serialized_info_trampoline<
            F: Fn(&Discoverer, &str) -> Option<DiscovererInfo> + Send + Sync + 'static,
        >(
            this: *mut ffi::GstDiscoverer,
            uri: *mut libc::c_char,
            f: glib::ffi::gpointer,
        ) -> *mut ffi::GstDiscovererInfo {
            let f: &F = &*(f as *const F);
            f(
                &from_glib_borrow(this),
                &glib::GString::from_glib_borrow(uri),
            )
            .to_glib_full()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"load-serialized-info\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    load_serialized_info_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// This signal is emitted after the source element has been created for, so
    /// the URI being discovered, so it can be configured by setting additional
    /// properties (e.g. set a proxy server for an http source, or set the device
    /// and read speed for an audio cd source).
    ///
    /// This signal is usually emitted from the context of a GStreamer streaming
    /// thread.
    /// ## `source`
    /// source element
    #[doc(alias = "source-setup")]
    pub fn connect_source_setup<F: Fn(&Self, &gst::Element) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn source_setup_trampoline<
            F: Fn(&Discoverer, &gst::Element) + Send + Sync + 'static,
        >(
            this: *mut ffi::GstDiscoverer,
            source: *mut gst::ffi::GstElement,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this), &from_glib_borrow(source))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"source-setup\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    source_setup_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    /// Will be emitted when the discover starts analyzing the pending URIs
    #[doc(alias = "starting")]
    pub fn connect_starting<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn starting_trampoline<F: Fn(&Discoverer) + Send + Sync + 'static>(
            this: *mut ffi::GstDiscoverer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"starting\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    starting_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "use-cache")]
    pub fn connect_use_cache_notify<F: Fn(&Self) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_use_cache_trampoline<
            F: Fn(&Discoverer) + Send + Sync + 'static,
        >(
            this: *mut ffi::GstDiscoverer,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::use-cache\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    notify_use_cache_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

unsafe impl Send for Discoverer {}
unsafe impl Sync for Discoverer {}