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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// 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::{Bin, ChildProxy, Clock, ClockTime, Element, Object};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// A [`Pipeline`][crate::Pipeline] is a special [`Bin`][crate::Bin] used as the toplevel container for
    /// the filter graph. The [`Pipeline`][crate::Pipeline] will manage the selection and
    /// distribution of a global [`Clock`][crate::Clock] as well as provide a [`Bus`][crate::Bus] to the
    /// application.
    ///
    /// [`new()`][Self::new()] is used to create a pipeline. when you are done with
    /// the pipeline, use `gst_object_unref()` to free its resources including all
    /// added [`Element`][crate::Element] objects (if not otherwise referenced).
    ///
    /// Elements are added and removed from the pipeline using the [`Bin`][crate::Bin]
    /// methods like [`GstBinExt::add()`][crate::prelude::GstBinExt::add()] and [`GstBinExt::remove()`][crate::prelude::GstBinExt::remove()] (see [`Bin`][crate::Bin]).
    ///
    /// Before changing the state of the [`Pipeline`][crate::Pipeline] (see [`Element`][crate::Element]) a [`Bus`][crate::Bus]
    /// should be retrieved with `gst_pipeline_get_bus()`. This [`Bus`][crate::Bus] should then
    /// be used to receive [`Message`][crate::Message] from the elements in the pipeline. Listening
    /// to the [`Bus`][crate::Bus] is necessary for retrieving error messages from the
    /// [`Pipeline`][crate::Pipeline] and otherwise the [`Pipeline`][crate::Pipeline] might stop without any
    /// indication, why. Furthermore, the [`Pipeline`][crate::Pipeline] posts messages even if
    /// nobody listens on the [`Bus`][crate::Bus], which will pile up and use up memory.
    ///
    /// By default, a [`Pipeline`][crate::Pipeline] will automatically flush the pending [`Bus`][crate::Bus]
    /// messages when going to the NULL state to ensure that no circular
    /// references exist when no messages are read from the [`Bus`][crate::Bus]. This
    /// behaviour can be changed with [`PipelineExt::set_auto_flush_bus()`][crate::prelude::PipelineExt::set_auto_flush_bus()].
    ///
    /// When the [`Pipeline`][crate::Pipeline] performs the PAUSED to PLAYING state change it will
    /// select a clock for the elements. The clock selection algorithm will by
    /// default select a clock provided by an element that is most upstream
    /// (closest to the source). For live pipelines (ones that return
    /// [`StateChangeReturn::NoPreroll`][crate::StateChangeReturn::NoPreroll] from the [`ElementExt::set_state()`][crate::prelude::ElementExt::set_state()] call) this
    /// will select the clock provided by the live source. For normal pipelines
    /// this will select a clock provided by the sinks (most likely the audio
    /// sink). If no element provides a clock, a default [`SystemClock`][crate::SystemClock] is used.
    ///
    /// The clock selection can be controlled with the [`PipelineExt::use_clock()`][crate::prelude::PipelineExt::use_clock()]
    /// method, which will enforce a given clock on the pipeline. With
    /// [`PipelineExt::auto_clock()`][crate::prelude::PipelineExt::auto_clock()] the default clock selection algorithm can be
    /// restored.
    ///
    /// A [`Pipeline`][crate::Pipeline] maintains a running time for the elements. The running
    /// time is defined as the difference between the current clock time and
    /// the base time. When the pipeline goes to READY or a flushing seek is
    /// performed on it, the running time is reset to 0. When the pipeline is
    /// set from PLAYING to PAUSED, the current clock time is sampled and used to
    /// configure the base time for the elements when the pipeline is set
    /// to PLAYING again. The effect is that the running time (as the difference
    /// between the clock time and the base time) will count how much time was spent
    /// in the PLAYING state. This default behaviour can be changed with the
    /// [`ElementExt::set_start_time()`][crate::prelude::ElementExt::set_start_time()] method.
    ///
    /// ## Properties
    ///
    ///
    /// #### `auto-flush-bus`
    ///  Whether or not to automatically flush all messages on the
    /// pipeline's bus when going from READY to NULL state. Please see
    /// [`PipelineExt::set_auto_flush_bus()`][crate::prelude::PipelineExt::set_auto_flush_bus()] for more information on this option.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `delay`
    ///  The expected delay needed for elements to spin up to the
    /// PLAYING state expressed in nanoseconds.
    /// see [`PipelineExt::set_delay()`][crate::prelude::PipelineExt::set_delay()] for more information on this option.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `latency`
    ///  Latency to configure on the pipeline. See [`PipelineExt::set_latency()`][crate::prelude::PipelineExt::set_latency()].
    ///
    /// Readable | Writeable
    /// <details><summary><h4>Bin</h4></summary>
    ///
    ///
    /// #### `async-handling`
    ///  If set to [`true`], the bin will handle asynchronous state changes.
    /// This should be used only if the bin subclass is modifying the state
    /// of its children on its own.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `message-forward`
    ///  Forward all children messages, even those that would normally be filtered by
    /// the bin. This can be interesting when one wants to be notified of the EOS
    /// state of individual elements, for example.
    ///
    /// The messages are converted to an ELEMENT message with the bin as the
    /// source. The structure of the message is named `GstBinForwarded` and contains
    /// a field named `message` that contains the original forwarded [`Message`][crate::Message].
    ///
    /// Readable | Writeable
    /// </details>
    /// <details><summary><h4>Object</h4></summary>
    ///
    ///
    /// #### `name`
    ///  Readable | Writeable | Construct
    ///
    ///
    /// #### `parent`
    ///  The parent of the object. Please note, that when changing the 'parent'
    /// property, we don't emit [`notify`][struct@crate::glib::Object#notify] and [`deep-notify`][struct@crate::Object#deep-notify]
    /// signals due to locking issues. In some cases one can use
    /// [`element-added`][struct@crate::Bin#element-added] or [`element-removed`][struct@crate::Bin#element-removed] signals on the parent to
    /// achieve a similar effect.
    ///
    /// Readable | Writeable
    /// </details>
    ///
    /// # Implements
    ///
    /// [`PipelineExt`][trait@crate::prelude::PipelineExt], [`GstBinExt`][trait@crate::prelude::GstBinExt], [`ElementExt`][trait@crate::prelude::ElementExt], [`GstObjectExt`][trait@crate::prelude::GstObjectExt], [`trait@glib::ObjectExt`], [`ChildProxyExt`][trait@crate::prelude::ChildProxyExt], [`ElementExtManual`][trait@crate::prelude::ElementExtManual], [`ChildProxyExtManual`][trait@crate::prelude::ChildProxyExtManual]
    #[doc(alias = "GstPipeline")]
    pub struct Pipeline(Object<ffi::GstPipeline, ffi::GstPipelineClass>) @extends Bin, Element, Object, @implements ChildProxy;

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

impl Pipeline {
    pub const NONE: Option<&'static Pipeline> = None;
}

unsafe impl Send for Pipeline {}
unsafe impl Sync for Pipeline {}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::Pipeline>> Sealed for T {}
}

/// Trait containing all [`struct@Pipeline`] methods.
///
/// # Implementors
///
/// [`Pipeline`][struct@crate::Pipeline]
pub trait PipelineExt: IsA<Pipeline> + sealed::Sealed + 'static {
    /// Let `self` select a clock automatically. This is the default
    /// behaviour.
    ///
    /// Use this function if you previous forced a fixed clock with
    /// [`use_clock()`][Self::use_clock()] and want to restore the default
    /// pipeline clock selection algorithm.
    ///
    /// MT safe.
    #[doc(alias = "gst_pipeline_auto_clock")]
    fn auto_clock(&self) {
        unsafe {
            ffi::gst_pipeline_auto_clock(self.as_ref().to_glib_none().0);
        }
    }

    /// Check if `self` will automatically flush messages when going to
    /// the NULL state.
    ///
    /// # Returns
    ///
    /// whether the pipeline will automatically flush its bus when
    /// going from READY to NULL state or not.
    ///
    /// MT safe.
    #[doc(alias = "gst_pipeline_get_auto_flush_bus")]
    #[doc(alias = "get_auto_flush_bus")]
    fn is_auto_flush_bus(&self) -> bool {
        unsafe {
            from_glib(ffi::gst_pipeline_get_auto_flush_bus(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Return the configured latency on `self`.
    ///
    /// # Returns
    ///
    /// `self` configured latency, or `GST_CLOCK_TIME_NONE` if none has been configured
    /// because `self` did not reach the PLAYING state yet.
    ///
    /// MT safe.
    #[cfg(feature = "v1_24")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
    #[doc(alias = "gst_pipeline_get_configured_latency")]
    #[doc(alias = "get_configured_latency")]
    fn configured_latency(&self) -> Option<ClockTime> {
        unsafe {
            from_glib(ffi::gst_pipeline_get_configured_latency(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Get the configured delay (see [`set_delay()`][Self::set_delay()]).
    ///
    /// # Returns
    ///
    /// The configured delay.
    ///
    /// MT safe.
    #[doc(alias = "gst_pipeline_get_delay")]
    #[doc(alias = "get_delay")]
    fn delay(&self) -> ClockTime {
        unsafe {
            try_from_glib(ffi::gst_pipeline_get_delay(self.as_ref().to_glib_none().0))
                .expect("mandatory glib value is None")
        }
    }

    /// Gets the latency that should be configured on the pipeline. See
    /// [`set_latency()`][Self::set_latency()].
    ///
    /// # Returns
    ///
    /// Latency to configure on the pipeline or GST_CLOCK_TIME_NONE
    #[doc(alias = "gst_pipeline_get_latency")]
    #[doc(alias = "get_latency")]
    fn latency(&self) -> Option<ClockTime> {
        unsafe {
            from_glib(ffi::gst_pipeline_get_latency(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Gets the current clock used by `self`.
    ///
    /// Unlike [`ElementExt::clock()`][crate::prelude::ElementExt::clock()], this function will always return a
    /// clock, even if the pipeline is not in the PLAYING state.
    ///
    /// # Returns
    ///
    /// a [`Clock`][crate::Clock], unref after usage.
    #[doc(alias = "gst_pipeline_get_pipeline_clock")]
    #[doc(alias = "get_pipeline_clock")]
    fn pipeline_clock(&self) -> Clock {
        unsafe {
            from_glib_full(ffi::gst_pipeline_get_pipeline_clock(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Check if `self` is live.
    ///
    /// # Returns
    ///
    /// [`true`] if `self` is live, [`false`] if not or if it did not reach the PAUSED state yet.
    ///
    /// MT safe.
    #[cfg(feature = "v1_24")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
    #[doc(alias = "gst_pipeline_is_live")]
    fn is_live(&self) -> bool {
        unsafe { from_glib(ffi::gst_pipeline_is_live(self.as_ref().to_glib_none().0)) }
    }

    /// Usually, when a pipeline goes from READY to NULL state, it automatically
    /// flushes all pending messages on the bus, which is done for refcounting
    /// purposes, to break circular references.
    ///
    /// This means that applications that update state using (async) bus messages
    /// (e.g. do certain things when a pipeline goes from PAUSED to READY) might
    /// not get to see messages when the pipeline is shut down, because they might
    /// be flushed before they can be dispatched in the main thread. This behaviour
    /// can be disabled using this function.
    ///
    /// It is important that all messages on the bus are handled when the
    /// automatic flushing is disabled else memory leaks will be introduced.
    ///
    /// MT safe.
    /// ## `auto_flush`
    /// whether or not to automatically flush the bus when
    /// the pipeline goes from READY to NULL state
    #[doc(alias = "gst_pipeline_set_auto_flush_bus")]
    fn set_auto_flush_bus(&self, auto_flush: bool) {
        unsafe {
            ffi::gst_pipeline_set_auto_flush_bus(
                self.as_ref().to_glib_none().0,
                auto_flush.into_glib(),
            );
        }
    }

    /// Set the expected delay needed for all elements to perform the
    /// PAUSED to PLAYING state change. `delay` will be added to the
    /// base time of the elements so that they wait an additional `delay`
    /// amount of time before starting to process buffers and cannot be
    /// `GST_CLOCK_TIME_NONE`.
    ///
    /// This option is used for tuning purposes and should normally not be
    /// used.
    ///
    /// MT safe.
    /// ## `delay`
    /// the delay
    #[doc(alias = "gst_pipeline_set_delay")]
    fn set_delay(&self, delay: ClockTime) {
        unsafe {
            ffi::gst_pipeline_set_delay(self.as_ref().to_glib_none().0, delay.into_glib());
        }
    }

    /// Sets the latency that should be configured on the pipeline. Setting
    /// GST_CLOCK_TIME_NONE will restore the default behaviour of using the minimum
    /// latency from the LATENCY query. Setting this is usually not required and
    /// the pipeline will figure out an appropriate latency automatically.
    ///
    /// Setting a too low latency, especially lower than the minimum latency from
    /// the LATENCY query, will most likely cause the pipeline to fail.
    /// ## `latency`
    /// latency to configure
    #[doc(alias = "gst_pipeline_set_latency")]
    fn set_latency(&self, latency: impl Into<Option<ClockTime>>) {
        unsafe {
            ffi::gst_pipeline_set_latency(
                self.as_ref().to_glib_none().0,
                latency.into().into_glib(),
            );
        }
    }

    /// Force `self` to use the given `clock`. The pipeline will
    /// always use the given clock even if new clock providers are added
    /// to this pipeline.
    ///
    /// If `clock` is [`None`] all clocking will be disabled which will make
    /// the pipeline run as fast as possible.
    ///
    /// MT safe.
    /// ## `clock`
    /// the clock to use
    #[doc(alias = "gst_pipeline_use_clock")]
    fn use_clock(&self, clock: Option<&impl IsA<Clock>>) {
        unsafe {
            ffi::gst_pipeline_use_clock(
                self.as_ref().to_glib_none().0,
                clock.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    #[doc(alias = "auto-flush-bus")]
    fn connect_auto_flush_bus_notify<F: Fn(&Self) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_auto_flush_bus_trampoline<
            P: IsA<Pipeline>,
            F: Fn(&P) + Send + Sync + 'static,
        >(
            this: *mut ffi::GstPipeline,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Pipeline::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::auto-flush-bus\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    notify_auto_flush_bus_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "delay")]
    fn connect_delay_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_delay_trampoline<
            P: IsA<Pipeline>,
            F: Fn(&P) + Send + Sync + 'static,
        >(
            this: *mut ffi::GstPipeline,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Pipeline::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::delay\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    notify_delay_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "latency")]
    fn connect_latency_notify<F: Fn(&Self) + Send + Sync + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_latency_trampoline<
            P: IsA<Pipeline>,
            F: Fn(&P) + Send + Sync + 'static,
        >(
            this: *mut ffi::GstPipeline,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Pipeline::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::latency\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    notify_latency_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<Pipeline>> PipelineExt for O {}