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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{translate::*, value::ToSendValue};
use gst::{ffi as gst_ffi, prelude::*, Element, Message, Seqnum};

use crate::ffi;

macro_rules! message_builder_generic_impl {
    ($new_fn:expr) => {
        #[allow(clippy::needless_update)]
        pub fn src<O: IsA<Element> + Cast + Clone>(self, src: &O) -> Self {
            Self {
                builder: self.builder.src(src),
                ..self
            }
        }

        #[allow(clippy::needless_update)]
        pub fn src_if<O: IsA<Element> + Cast + Clone>(self, src: &O, predicate: bool) -> Self {
            if predicate {
                self.src(src)
            } else {
                self
            }
        }

        #[allow(clippy::needless_update)]
        pub fn src_if_some<O: IsA<Element> + Cast + Clone>(self, src: Option<&O>) -> Self {
            if let Some(src) = src {
                self.src(src)
            } else {
                self
            }
        }

        #[doc(alias = "gst_message_set_seqnum")]
        #[allow(clippy::needless_update)]
        pub fn seqnum(self, seqnum: Seqnum) -> Self {
            Self {
                builder: self.builder.seqnum(seqnum),
                ..self
            }
        }

        #[doc(alias = "gst_message_set_seqnum")]
        #[allow(clippy::needless_update)]
        pub fn seqnum_if(self, seqnum: Seqnum, predicate: bool) -> Self {
            if predicate {
                self.seqnum(seqnum)
            } else {
                self
            }
        }

        #[doc(alias = "gst_message_set_seqnum")]
        #[allow(clippy::needless_update)]
        pub fn seqnum_if_some(self, seqnum: Option<Seqnum>) -> Self {
            if let Some(seqnum) = seqnum {
                self.seqnum(seqnum)
            } else {
                self
            }
        }

        #[cfg(feature = "v1_26")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
        #[doc(alias = "gst_message_set_details")]
        pub fn details(self, details: gst::Structure) -> Self {
            Self {
                builder: self.builder.details(details),
                ..self
            }
        }

        #[cfg(feature = "v1_26")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
        #[doc(alias = "gst_message_set_details")]
        pub fn details_if(self, details: gst::Structure, predicate: bool) -> Self {
            if predicate {
                self.details(details)
            } else {
                self
            }
        }

        #[cfg(feature = "v1_26")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
        #[doc(alias = "gst_message_set_details")]
        pub fn details_if_some(self, details: Option<gst::Structure>) -> Self {
            if let Some(details) = details {
                self.details(details)
            } else {
                self
            }
        }

        #[cfg(feature = "v1_26")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
        #[doc(alias = "gst_missing_plugin_message_set_stream_id")]
        pub fn stream_id(self, stream_id: &'a str) -> Self {
            Self {
                stream_id: Some(stream_id),
                ..self
            }
        }

        #[cfg(feature = "v1_26")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
        #[doc(alias = "gst_missing_plugin_message_set_stream_id")]
        pub fn stream_id_if(self, stream_id: &'a str, predicate: bool) -> Self {
            if predicate {
                self.stream_id(stream_id)
            } else {
                self
            }
        }

        #[cfg(feature = "v1_26")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
        #[doc(alias = "gst_missing_plugin_message_set_stream_id")]
        pub fn stream_id_if_some(self, stream_id: Option<&'a str>) -> Self {
            if let Some(stream_id) = stream_id {
                self.stream_id(stream_id)
            } else {
                self
            }
        }

        // rustdoc-stripper-ignore-next
        /// Sets field `name` to the given value `value`.
        ///
        /// Overrides any default or previously defined value for `name`.
        pub fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self {
            Self {
                builder: self.builder.other_field(name, value),
                ..self
            }
        }

        gst::impl_builder_gvalue_extra_setters!(other_field);

        #[deprecated = "use builder.other_field() instead"]
        #[allow(clippy::needless_update)]
        pub fn other_fields(
            self,
            other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))],
        ) -> Self {
            Self {
                builder: self.builder.other_fields(other_fields),
                ..self
            }
        }

        #[must_use = "Building the message without using it has no effect"]
        #[allow(clippy::redundant_closure_call)]
        pub fn build(mut self) -> Message {
            skip_assert_initialized!();
            unsafe {
                let src = self.builder.src.to_glib_none().0;
                let msg = $new_fn(&mut self, src);
                if let Some(seqnum) = self.builder.seqnum {
                    gst_ffi::gst_message_set_seqnum(msg, seqnum.into_glib());
                }

                #[cfg(feature = "v1_26")]
                if let Some(details) = self.builder.details {
                    gst_ffi::gst_message_set_details(msg, details.into_glib_ptr());
                }

                if !self.builder.other_fields.is_empty() {
                    let structure = gst_ffi::gst_message_writable_structure(msg);

                    if !structure.is_null() {
                        let structure =
                            gst::StructureRef::from_glib_borrow_mut(structure as *mut _);

                        for (k, v) in self.builder.other_fields {
                            structure.set_value(k, v);
                        }
                    }
                }

                from_glib_full(msg)
            }
        }
    };
}

struct MessageBuilder<'a> {
    src: Option<Element>,
    seqnum: Option<Seqnum>,
    #[cfg(feature = "v1_26")]
    details: Option<gst::Structure>,
    other_fields: Vec<(&'a str, glib::SendValue)>,
}

impl<'a> MessageBuilder<'a> {
    fn new() -> Self {
        skip_assert_initialized!();
        Self {
            src: None,
            seqnum: None,
            #[cfg(feature = "v1_26")]
            details: None,
            other_fields: Vec::new(),
        }
    }

    fn src<O: IsA<Element> + Cast + Clone>(self, src: &O) -> Self {
        Self {
            src: Some(src.clone().upcast::<Element>()),
            ..self
        }
    }

    fn seqnum(self, seqnum: Seqnum) -> Self {
        Self {
            seqnum: Some(seqnum),
            ..self
        }
    }

    #[cfg(feature = "v1_26")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
    #[doc(alias = "gst_message_set_details")]
    fn details(self, details: gst::Structure) -> Self {
        Self {
            details: Some(details),
            ..self
        }
    }

    fn other_field(self, name: &'a str, value: impl ToSendValue) -> Self {
        let mut other_fields = self.other_fields;
        other_fields.push((name, value.to_send_value()));

        Self {
            other_fields,
            ..self
        }
    }

    fn other_fields(self, other_fields: &[(&'a str, &'a (dyn ToSendValue + Sync))]) -> Self {
        let mut s = self;

        for (name, value) in other_fields {
            s = s.other_field(name, value.to_send_value());
        }

        s
    }
}

enum MessageBuilderDetail<'a> {
    Decoder(&'a gst::Caps),
    Encoder(&'a gst::Caps),
    Element(&'a str),
    Sink(&'a str),
    Src(&'a str),
}

#[must_use = "The builder must be built to be used"]
pub struct MissingPluginMessageBuilder<'a> {
    builder: MessageBuilder<'a>,
    detail: MessageBuilderDetail<'a>,
    #[cfg(feature = "v1_26")]
    stream_id: Option<&'a str>,
}

impl<'a> MissingPluginMessageBuilder<'a> {
    fn new(detail: MessageBuilderDetail<'a>) -> Self {
        skip_assert_initialized!();
        Self {
            builder: MessageBuilder::new(),
            detail,
            #[cfg(feature = "v1_26")]
            stream_id: None,
        }
    }

    message_builder_generic_impl!(|s: &Self, src| {
        let msg = match s.detail {
            MessageBuilderDetail::Decoder(caps) => {
                ffi::gst_missing_decoder_message_new(src, caps.to_glib_none().0)
            }
            MessageBuilderDetail::Encoder(caps) => {
                ffi::gst_missing_encoder_message_new(src, caps.to_glib_none().0)
            }
            MessageBuilderDetail::Element(name) => {
                ffi::gst_missing_element_message_new(src, name.to_glib_none().0)
            }
            MessageBuilderDetail::Sink(protocol) => {
                ffi::gst_missing_uri_sink_message_new(src, protocol.to_glib_none().0)
            }
            MessageBuilderDetail::Src(protocol) => {
                ffi::gst_missing_uri_source_message_new(src, protocol.to_glib_none().0)
            }
        };

        #[cfg(feature = "v1_26")]
        if let Some(stream_id) = s.stream_id {
            ffi::gst_missing_plugin_message_set_stream_id(msg, stream_id.to_glib_none().0);
        }

        msg
    });
}

#[derive(Clone, Debug)]
pub struct MissingPluginMessage<'a> {
    pub msg: &'a gst::MessageRef,
}

impl<'a> MissingPluginMessage<'a> {
    #[doc(alias = "gst_missing_decoder_message_new")]
    #[allow(clippy::new_ret_no_self)]
    pub fn for_decoder(caps: &'a gst::Caps) -> gst::Message {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Decoder(caps)).build()
    }

    #[doc(alias = "gst_missing_decoder_message_new")]
    pub fn builder_for_decoder(caps: &'a gst::Caps) -> MissingPluginMessageBuilder<'a> {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Decoder(caps))
    }

    #[doc(alias = "gst_missing_encoder_message_new")]
    #[allow(clippy::new_ret_no_self)]
    pub fn for_encoder(caps: &'a gst::Caps) -> gst::Message {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Encoder(caps)).build()
    }

    #[doc(alias = "gst_missing_encoder_message_new")]
    pub fn builder_for_encoder(caps: &'a gst::Caps) -> MissingPluginMessageBuilder<'a> {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Encoder(caps))
    }

    #[doc(alias = "gst_missing_element_message_new")]
    #[allow(clippy::new_ret_no_self)]
    pub fn for_element(name: &'a str) -> gst::Message {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Element(name)).build()
    }

    #[doc(alias = "gst_missing_element_message_new")]
    pub fn builder_for_element(name: &'a str) -> MissingPluginMessageBuilder<'a> {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Element(name))
    }

    #[doc(alias = "gst_missing_uri_source_message_new")]
    #[allow(clippy::new_ret_no_self)]
    pub fn for_uri_source(protocol: &'a str) -> gst::Message {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Src(protocol)).build()
    }

    #[doc(alias = "gst_missing_uri_source_message_new")]
    pub fn builder_for_uri_source(protocol: &'a str) -> MissingPluginMessageBuilder<'a> {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Src(protocol))
    }

    #[doc(alias = "gst_missing_uri_sink_message_new")]
    #[allow(clippy::new_ret_no_self)]
    pub fn for_uri_sink(protocol: &'a str) -> gst::Message {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Sink(protocol)).build()
    }

    #[doc(alias = "gst_missing_uri_sink_message_new")]
    pub fn builder_for_uri_sink(protocol: &'a str) -> MissingPluginMessageBuilder<'a> {
        skip_assert_initialized!();
        MissingPluginMessageBuilder::new(MessageBuilderDetail::Sink(protocol))
    }

    #[doc(alias = "gst_is_missing_plugin_message")]
    pub fn is(msg: &gst::MessageRef) -> bool {
        skip_assert_initialized!();
        unsafe {
            from_glib(ffi::gst_is_missing_plugin_message(mut_override(
                msg.as_ptr(),
            )))
        }
    }

    pub fn parse(msg: &'a gst::MessageRef) -> Result<Self, glib::error::BoolError> {
        skip_assert_initialized!();
        if Self::is(msg) {
            Ok(MissingPluginMessage { msg })
        } else {
            Err(glib::bool_error!("Invalid missing plugin message"))
        }
    }

    #[doc(alias = "gst_missing_plugin_message_get_description")]
    pub fn description(&self) -> glib::GString {
        unsafe {
            from_glib_full(ffi::gst_missing_plugin_message_get_description(
                mut_override(self.msg.as_ptr()),
            ))
        }
    }

    #[doc(alias = "gst_missing_plugin_message_get_installer_detail")]
    pub fn installer_detail(&self) -> glib::GString {
        unsafe {
            from_glib_full(ffi::gst_missing_plugin_message_get_installer_detail(
                mut_override(self.msg.as_ptr()),
            ))
        }
    }

    #[cfg(feature = "v1_26")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
    #[doc(alias = "gst_missing_plugin_message_get_stream_id")]
    pub fn stream_id(&self) -> Option<&glib::GStr> {
        unsafe {
            let stream_id =
                ffi::gst_missing_plugin_message_get_stream_id(mut_override(self.msg.as_ptr()));
            if stream_id.is_null() {
                None
            } else {
                Some(glib::GStr::from_ptr(stream_id))
            }
        }
    }
}

#[doc(alias = "gst_missing_decoder_installer_detail_new")]
pub fn missing_decoder_installer_detail_new(caps: &gst::Caps) -> glib::GString {
    skip_assert_initialized!();
    unsafe {
        from_glib_full(ffi::gst_missing_decoder_installer_detail_new(mut_override(
            caps.as_ptr(),
        )))
    }
}

#[doc(alias = "gst_missing_encoder_installer_detail_new")]
pub fn missing_encoder_installer_detail_new(caps: &gst::Caps) -> glib::GString {
    skip_assert_initialized!();
    unsafe {
        from_glib_full(ffi::gst_missing_encoder_installer_detail_new(mut_override(
            caps.as_ptr(),
        )))
    }
}

#[doc(alias = "gst_missing_element_installer_detail_new")]
pub fn missing_element_installer_detail_new(name: &str) -> glib::GString {
    skip_assert_initialized!();
    unsafe {
        from_glib_full(ffi::gst_missing_element_installer_detail_new(mut_override(
            name.to_glib_none().0,
        )))
    }
}

#[doc(alias = "gst_missing_uri_source_installer_detail_new")]
pub fn missing_uri_source_installer_detail_new(protocol: &str) -> glib::GString {
    skip_assert_initialized!();
    unsafe {
        from_glib_full(ffi::gst_missing_uri_source_installer_detail_new(
            mut_override(protocol.to_glib_none().0),
        ))
    }
}

#[doc(alias = "gst_missing_uri_sink_installer_detail_new")]
pub fn missing_uri_sink_installer_detail_new(protocol: &str) -> glib::GString {
    skip_assert_initialized!();
    unsafe {
        from_glib_full(ffi::gst_missing_uri_sink_installer_detail_new(
            mut_override(protocol.to_glib_none().0),
        ))
    }
}

#[doc(alias = "gst_install_plugins_supported")]
pub fn install_plugins_supported() -> bool {
    skip_assert_initialized!();
    unsafe { from_glib(ffi::gst_install_plugins_supported()) }
}

#[doc(alias = "gst_install_plugins_installation_in_progress")]
pub fn install_plugins_installation_in_progress() -> bool {
    skip_assert_initialized!();
    unsafe { from_glib(ffi::gst_install_plugins_installation_in_progress()) }
}

#[doc(alias = "gst_install_plugins_sync")]
pub fn install_plugins_sync(
    details: &[&str],
    ctx: Option<&crate::InstallPluginsContext>,
) -> crate::InstallPluginsReturn {
    skip_assert_initialized!();
    unsafe {
        from_glib(ffi::gst_install_plugins_sync(
            ToGlibPtr::<*const *mut _>::to_glib_none(&glib::StrV::from(details)).0
                as *const *const _,
            ctx.to_glib_none().0,
        ))
    }
}

#[doc(alias = "gst_install_plugins_async")]
pub fn install_plugins_async<F: FnOnce(crate::InstallPluginsReturn) + Send + 'static>(
    details: &[&str],
    ctx: Option<&crate::InstallPluginsContext>,
    func: F,
) -> crate::InstallPluginsReturn {
    skip_assert_initialized!();

    let user_data: Box<Option<F>> = Box::new(Some(func));

    unsafe extern "C" fn trampoline<F: FnOnce(crate::InstallPluginsReturn) + Send + 'static>(
        ret: ffi::GstInstallPluginsReturn,
        user_data: glib::ffi::gpointer,
    ) {
        let callback = Box::from_raw(user_data as *mut F);
        callback(from_glib(ret));
    }

    unsafe {
        from_glib(ffi::gst_install_plugins_async(
            ToGlibPtr::<*const *mut _>::to_glib_none(&glib::StrV::from(details)).0
                as *const *const _,
            ctx.to_glib_none().0,
            Some(trampoline::<F>),
            Box::into_raw(user_data) as *mut _,
        ))
    }
}