1use crate::{
7    ffi, WebRTCICECandidateStats, WebRTCICEComponent, WebRTCICEStream, WebRTCICETransport,
8};
9use glib::{
10    object::ObjectType as _,
11    prelude::*,
12    signal::{connect_raw, SignalHandlerId},
13    translate::*,
14};
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18    #[doc(alias = "GstWebRTCICE")]
19    pub struct WebRTCICE(Object<ffi::GstWebRTCICE, ffi::GstWebRTCICEClass>) @extends gst::Object;
20
21    match fn {
22        type_ => || ffi::gst_webrtc_ice_get_type(),
23    }
24}
25
26impl WebRTCICE {
27    pub const NONE: Option<&'static WebRTCICE> = None;
28}
29
30unsafe impl Send for WebRTCICE {}
31unsafe impl Sync for WebRTCICE {}
32
33pub trait WebRTCICEExt: IsA<WebRTCICE> + 'static {
34    #[doc(alias = "gst_webrtc_ice_add_stream")]
35    fn add_stream(&self, session_id: u32) -> Option<WebRTCICEStream> {
36        unsafe {
37            from_glib_full(ffi::gst_webrtc_ice_add_stream(
38                self.as_ref().to_glib_none().0,
39                session_id,
40            ))
41        }
42    }
43
44    #[doc(alias = "gst_webrtc_ice_add_turn_server")]
45    fn add_turn_server(&self, uri: &str) -> bool {
46        unsafe {
47            from_glib(ffi::gst_webrtc_ice_add_turn_server(
48                self.as_ref().to_glib_none().0,
49                uri.to_glib_none().0,
50            ))
51        }
52    }
53
54    #[doc(alias = "gst_webrtc_ice_find_transport")]
55    fn find_transport(
56        &self,
57        stream: &impl IsA<WebRTCICEStream>,
58        component: WebRTCICEComponent,
59    ) -> Option<WebRTCICETransport> {
60        unsafe {
61            from_glib_full(ffi::gst_webrtc_ice_find_transport(
62                self.as_ref().to_glib_none().0,
63                stream.as_ref().to_glib_none().0,
64                component.into_glib(),
65            ))
66        }
67    }
68
69    #[doc(alias = "gst_webrtc_ice_gather_candidates")]
70    fn gather_candidates(&self, stream: &impl IsA<WebRTCICEStream>) -> bool {
71        unsafe {
72            from_glib(ffi::gst_webrtc_ice_gather_candidates(
73                self.as_ref().to_glib_none().0,
74                stream.as_ref().to_glib_none().0,
75            ))
76        }
77    }
78
79    #[doc(alias = "gst_webrtc_ice_get_http_proxy")]
80    #[doc(alias = "get_http_proxy")]
81    fn http_proxy(&self) -> glib::GString {
82        unsafe {
83            from_glib_full(ffi::gst_webrtc_ice_get_http_proxy(
84                self.as_ref().to_glib_none().0,
85            ))
86        }
87    }
88
89    #[doc(alias = "gst_webrtc_ice_get_is_controller")]
90    #[doc(alias = "get_is_controller")]
91    fn is_controller(&self) -> bool {
92        unsafe {
93            from_glib(ffi::gst_webrtc_ice_get_is_controller(
94                self.as_ref().to_glib_none().0,
95            ))
96        }
97    }
98
99    #[doc(alias = "gst_webrtc_ice_get_local_candidates")]
100    #[doc(alias = "get_local_candidates")]
101    fn local_candidates(&self, stream: &impl IsA<WebRTCICEStream>) -> Vec<WebRTCICECandidateStats> {
102        unsafe {
103            FromGlibPtrContainer::from_glib_full(ffi::gst_webrtc_ice_get_local_candidates(
104                self.as_ref().to_glib_none().0,
105                stream.as_ref().to_glib_none().0,
106            ))
107        }
108    }
109
110    #[doc(alias = "gst_webrtc_ice_get_remote_candidates")]
111    #[doc(alias = "get_remote_candidates")]
112    fn remote_candidates(
113        &self,
114        stream: &impl IsA<WebRTCICEStream>,
115    ) -> Vec<WebRTCICECandidateStats> {
116        unsafe {
117            FromGlibPtrContainer::from_glib_full(ffi::gst_webrtc_ice_get_remote_candidates(
118                self.as_ref().to_glib_none().0,
119                stream.as_ref().to_glib_none().0,
120            ))
121        }
122    }
123
124    #[doc(alias = "gst_webrtc_ice_get_selected_pair")]
125    #[doc(alias = "get_selected_pair")]
126    fn selected_pair(
127        &self,
128        stream: &impl IsA<WebRTCICEStream>,
129    ) -> Option<(WebRTCICECandidateStats, WebRTCICECandidateStats)> {
130        unsafe {
131            let mut local_stats = std::ptr::null_mut();
132            let mut remote_stats = std::ptr::null_mut();
133            let ret = from_glib(ffi::gst_webrtc_ice_get_selected_pair(
134                self.as_ref().to_glib_none().0,
135                stream.as_ref().to_glib_none().0,
136                &mut local_stats,
137                &mut remote_stats,
138            ));
139            if ret {
140                Some((from_glib_full(local_stats), from_glib_full(remote_stats)))
141            } else {
142                None
143            }
144        }
145    }
146
147    #[doc(alias = "gst_webrtc_ice_get_stun_server")]
148    #[doc(alias = "get_stun_server")]
149    fn stun_server(&self) -> Option<glib::GString> {
150        unsafe {
151            from_glib_full(ffi::gst_webrtc_ice_get_stun_server(
152                self.as_ref().to_glib_none().0,
153            ))
154        }
155    }
156
157    #[doc(alias = "gst_webrtc_ice_get_turn_server")]
158    #[doc(alias = "get_turn_server")]
159    fn turn_server(&self) -> Option<glib::GString> {
160        unsafe {
161            from_glib_full(ffi::gst_webrtc_ice_get_turn_server(
162                self.as_ref().to_glib_none().0,
163            ))
164        }
165    }
166
167    #[doc(alias = "gst_webrtc_ice_set_force_relay")]
168    fn set_force_relay(&self, force_relay: bool) {
169        unsafe {
170            ffi::gst_webrtc_ice_set_force_relay(
171                self.as_ref().to_glib_none().0,
172                force_relay.into_glib(),
173            );
174        }
175    }
176
177    #[doc(alias = "gst_webrtc_ice_set_http_proxy")]
178    fn set_http_proxy(&self, uri: &str) {
179        unsafe {
180            ffi::gst_webrtc_ice_set_http_proxy(
181                self.as_ref().to_glib_none().0,
182                uri.to_glib_none().0,
183            );
184        }
185    }
186
187    #[doc(alias = "gst_webrtc_ice_set_is_controller")]
188    fn set_is_controller(&self, controller: bool) {
189        unsafe {
190            ffi::gst_webrtc_ice_set_is_controller(
191                self.as_ref().to_glib_none().0,
192                controller.into_glib(),
193            );
194        }
195    }
196
197    #[doc(alias = "gst_webrtc_ice_set_local_credentials")]
198    fn set_local_credentials(
199        &self,
200        stream: &impl IsA<WebRTCICEStream>,
201        ufrag: &str,
202        pwd: &str,
203    ) -> bool {
204        unsafe {
205            from_glib(ffi::gst_webrtc_ice_set_local_credentials(
206                self.as_ref().to_glib_none().0,
207                stream.as_ref().to_glib_none().0,
208                ufrag.to_glib_none().0,
209                pwd.to_glib_none().0,
210            ))
211        }
212    }
213
214    #[doc(alias = "gst_webrtc_ice_set_on_ice_candidate")]
215    fn set_on_ice_candidate<P: Fn(&WebRTCICE, u32, &str) + Send + Sync + 'static>(&self, func: P) {
216        let func_data: Box_<P> = Box_::new(func);
217        unsafe extern "C" fn func_func<P: Fn(&WebRTCICE, u32, &str) + Send + Sync + 'static>(
218            ice: *mut ffi::GstWebRTCICE,
219            stream_id: std::ffi::c_uint,
220            candidate: *const std::ffi::c_char,
221            user_data: glib::ffi::gpointer,
222        ) {
223            let ice = from_glib_borrow(ice);
224            let candidate: Borrowed<glib::GString> = from_glib_borrow(candidate);
225            let callback = &*(user_data as *mut P);
226            (*callback)(&ice, stream_id, candidate.as_str())
227        }
228        let func = Some(func_func::<P> as _);
229        unsafe extern "C" fn notify_func<P: Fn(&WebRTCICE, u32, &str) + Send + Sync + 'static>(
230            data: glib::ffi::gpointer,
231        ) {
232            let _callback = Box_::from_raw(data as *mut P);
233        }
234        let destroy_call3 = Some(notify_func::<P> as _);
235        let super_callback0: Box_<P> = func_data;
236        unsafe {
237            ffi::gst_webrtc_ice_set_on_ice_candidate(
238                self.as_ref().to_glib_none().0,
239                func,
240                Box_::into_raw(super_callback0) as *mut _,
241                destroy_call3,
242            );
243        }
244    }
245
246    #[doc(alias = "gst_webrtc_ice_set_remote_credentials")]
247    fn set_remote_credentials(
248        &self,
249        stream: &impl IsA<WebRTCICEStream>,
250        ufrag: &str,
251        pwd: &str,
252    ) -> bool {
253        unsafe {
254            from_glib(ffi::gst_webrtc_ice_set_remote_credentials(
255                self.as_ref().to_glib_none().0,
256                stream.as_ref().to_glib_none().0,
257                ufrag.to_glib_none().0,
258                pwd.to_glib_none().0,
259            ))
260        }
261    }
262
263    #[doc(alias = "gst_webrtc_ice_set_stun_server")]
264    fn set_stun_server(&self, uri: Option<&str>) {
265        unsafe {
266            ffi::gst_webrtc_ice_set_stun_server(
267                self.as_ref().to_glib_none().0,
268                uri.to_glib_none().0,
269            );
270        }
271    }
272
273    #[doc(alias = "gst_webrtc_ice_set_tos")]
274    fn set_tos(&self, stream: &impl IsA<WebRTCICEStream>, tos: u32) {
275        unsafe {
276            ffi::gst_webrtc_ice_set_tos(
277                self.as_ref().to_glib_none().0,
278                stream.as_ref().to_glib_none().0,
279                tos,
280            );
281        }
282    }
283
284    #[doc(alias = "gst_webrtc_ice_set_turn_server")]
285    fn set_turn_server(&self, uri: Option<&str>) {
286        unsafe {
287            ffi::gst_webrtc_ice_set_turn_server(
288                self.as_ref().to_glib_none().0,
289                uri.to_glib_none().0,
290            );
291        }
292    }
293
294    #[cfg(feature = "v1_20")]
295    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
296    #[doc(alias = "max-rtp-port")]
297    fn max_rtp_port(&self) -> u32 {
298        ObjectExt::property(self.as_ref(), "max-rtp-port")
299    }
300
301    #[cfg(feature = "v1_20")]
302    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
303    #[doc(alias = "max-rtp-port")]
304    fn set_max_rtp_port(&self, max_rtp_port: u32) {
305        ObjectExt::set_property(self.as_ref(), "max-rtp-port", max_rtp_port)
306    }
307
308    #[cfg(feature = "v1_20")]
309    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
310    #[doc(alias = "min-rtp-port")]
311    fn min_rtp_port(&self) -> u32 {
312        ObjectExt::property(self.as_ref(), "min-rtp-port")
313    }
314
315    #[cfg(feature = "v1_20")]
316    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
317    #[doc(alias = "min-rtp-port")]
318    fn set_min_rtp_port(&self, min_rtp_port: u32) {
319        ObjectExt::set_property(self.as_ref(), "min-rtp-port", min_rtp_port)
320    }
321
322    #[doc(alias = "add-local-ip-address")]
323    fn connect_add_local_ip_address<F: Fn(&Self, &str) -> bool + Send + Sync + 'static>(
324        &self,
325        f: F,
326    ) -> SignalHandlerId {
327        unsafe extern "C" fn add_local_ip_address_trampoline<
328            P: IsA<WebRTCICE>,
329            F: Fn(&P, &str) -> bool + Send + Sync + 'static,
330        >(
331            this: *mut ffi::GstWebRTCICE,
332            address: *mut std::ffi::c_char,
333            f: glib::ffi::gpointer,
334        ) -> glib::ffi::gboolean {
335            let f: &F = &*(f as *const F);
336            f(
337                WebRTCICE::from_glib_borrow(this).unsafe_cast_ref(),
338                &glib::GString::from_glib_borrow(address),
339            )
340            .into_glib()
341        }
342        unsafe {
343            let f: Box_<F> = Box_::new(f);
344            connect_raw(
345                self.as_ptr() as *mut _,
346                c"add-local-ip-address".as_ptr() as *const _,
347                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
348                    add_local_ip_address_trampoline::<Self, F> as *const (),
349                )),
350                Box_::into_raw(f),
351            )
352        }
353    }
354
355    fn emit_add_local_ip_address(&self, address: &str) -> bool {
356        self.emit_by_name("add-local-ip-address", &[&address])
357    }
358
359    #[cfg(feature = "v1_20")]
360    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
361    #[doc(alias = "max-rtp-port")]
362    fn connect_max_rtp_port_notify<F: Fn(&Self) + Send + Sync + 'static>(
363        &self,
364        f: F,
365    ) -> SignalHandlerId {
366        unsafe extern "C" fn notify_max_rtp_port_trampoline<
367            P: IsA<WebRTCICE>,
368            F: Fn(&P) + Send + Sync + 'static,
369        >(
370            this: *mut ffi::GstWebRTCICE,
371            _param_spec: glib::ffi::gpointer,
372            f: glib::ffi::gpointer,
373        ) {
374            let f: &F = &*(f as *const F);
375            f(WebRTCICE::from_glib_borrow(this).unsafe_cast_ref())
376        }
377        unsafe {
378            let f: Box_<F> = Box_::new(f);
379            connect_raw(
380                self.as_ptr() as *mut _,
381                c"notify::max-rtp-port".as_ptr() as *const _,
382                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
383                    notify_max_rtp_port_trampoline::<Self, F> as *const (),
384                )),
385                Box_::into_raw(f),
386            )
387        }
388    }
389
390    #[cfg(feature = "v1_20")]
391    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
392    #[doc(alias = "min-rtp-port")]
393    fn connect_min_rtp_port_notify<F: Fn(&Self) + Send + Sync + 'static>(
394        &self,
395        f: F,
396    ) -> SignalHandlerId {
397        unsafe extern "C" fn notify_min_rtp_port_trampoline<
398            P: IsA<WebRTCICE>,
399            F: Fn(&P) + Send + Sync + 'static,
400        >(
401            this: *mut ffi::GstWebRTCICE,
402            _param_spec: glib::ffi::gpointer,
403            f: glib::ffi::gpointer,
404        ) {
405            let f: &F = &*(f as *const F);
406            f(WebRTCICE::from_glib_borrow(this).unsafe_cast_ref())
407        }
408        unsafe {
409            let f: Box_<F> = Box_::new(f);
410            connect_raw(
411                self.as_ptr() as *mut _,
412                c"notify::min-rtp-port".as_ptr() as *const _,
413                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
414                    notify_min_rtp_port_trampoline::<Self, F> as *const (),
415                )),
416                Box_::into_raw(f),
417            )
418        }
419    }
420}
421
422impl<O: IsA<WebRTCICE>> WebRTCICEExt for O {}