Skip to main content

gstreamer/
stream_collection.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{boxed::Box as Box_, fmt, mem::transmute};
4
5use glib::{
6    object::ObjectType as ObjectType_,
7    signal::{SignalHandlerId, connect_raw},
8    translate::*,
9};
10
11use crate::{Stream, StreamCollection, ffi};
12
13crate::utils::define_fixed_size_iter!(
14    Iter,
15    &'a StreamCollection,
16    Stream,
17    |collection: &StreamCollection| collection.len(),
18    |collection: &StreamCollection, idx: usize| unsafe {
19        from_glib_none(ffi::gst_stream_collection_get_stream(
20            collection.to_glib_none().0,
21            idx as u32,
22        ))
23    }
24);
25
26#[derive(Debug, Clone)]
27#[must_use = "The builder must be built to be used"]
28pub struct StreamCollectionBuilder(StreamCollection);
29
30impl StreamCollectionBuilder {
31    #[doc(alias = "gst_stream_collection_add_stream")]
32    pub fn stream(self, stream: Stream) -> Self {
33        unsafe {
34            ffi::gst_stream_collection_add_stream(
35                (self.0).to_glib_none().0,
36                stream.into_glib_ptr(),
37            );
38        }
39
40        self
41    }
42
43    #[doc(alias = "gst_stream_collection_add_stream")]
44    pub fn stream_if(self, stream: Stream, predicate: bool) -> Self {
45        if predicate {
46            unsafe {
47                ffi::gst_stream_collection_add_stream(
48                    (self.0).to_glib_none().0,
49                    stream.into_glib_ptr(),
50                );
51            }
52
53            self
54        } else {
55            self
56        }
57    }
58
59    #[doc(alias = "gst_stream_collection_add_stream")]
60    pub fn stream_if_some(self, stream: Option<Stream>) -> Self {
61        if let Some(stream) = stream {
62            self.stream(stream)
63        } else {
64            self
65        }
66    }
67
68    pub fn streams(self, streams: impl IntoIterator<Item = Stream>) -> Self {
69        for stream in streams.into_iter() {
70            unsafe {
71                ffi::gst_stream_collection_add_stream(
72                    (self.0).to_glib_none().0,
73                    stream.into_glib_ptr(),
74                );
75            }
76        }
77
78        self
79    }
80
81    pub fn streams_if(self, streams: impl IntoIterator<Item = Stream>, predicate: bool) -> Self {
82        if predicate {
83            for stream in streams.into_iter() {
84                unsafe {
85                    ffi::gst_stream_collection_add_stream(
86                        (self.0).to_glib_none().0,
87                        stream.into_glib_ptr(),
88                    );
89                }
90            }
91
92            self
93        } else {
94            self
95        }
96    }
97
98    pub fn streams_if_some(self, streams: Option<impl IntoIterator<Item = Stream>>) -> Self {
99        if let Some(streams) = streams {
100            self.streams(streams)
101        } else {
102            self
103        }
104    }
105
106    pub fn streams_if_not_empty(self, streams: impl IntoIterator<Item = Stream>) -> Self {
107        let mut streams = streams.into_iter().peekable();
108        if streams.peek().is_some() {
109            self.streams(streams)
110        } else {
111            self
112        }
113    }
114
115    #[must_use = "Building the stream collection without using it has no effect"]
116    pub fn build(self) -> StreamCollection {
117        self.0
118    }
119}
120
121impl StreamCollection {
122    #[doc(alias = "gst_stream_collection_new")]
123    pub fn builder(upstream_id: Option<&str>) -> StreamCollectionBuilder {
124        assert_initialized_main_thread!();
125        let upstream_id = upstream_id.to_glib_none();
126        let collection = unsafe { from_glib_full(ffi::gst_stream_collection_new(upstream_id.0)) };
127
128        StreamCollectionBuilder(collection)
129    }
130
131    /// The stream notify signal is used to be notified of property changes to
132    /// streams within the collection.
133    /// ## `prop_stream`
134    /// the [`Stream`][crate::Stream] that originated the signal
135    /// ## `prop`
136    /// the property that changed
137    #[doc(alias = "stream-notify")]
138    pub fn connect_stream_notify<
139        F: Fn(&Self, &Stream, &glib::ParamSpec) + Send + Sync + 'static,
140    >(
141        &self,
142        detail: Option<&str>,
143        f: F,
144    ) -> SignalHandlerId {
145        unsafe extern "C" fn stream_notify_trampoline<
146            F: Fn(&StreamCollection, &Stream, &glib::ParamSpec) + Send + Sync + 'static,
147        >(
148            this: *mut ffi::GstStreamCollection,
149            object: *mut ffi::GstStream,
150            p0: *mut glib::gobject_ffi::GParamSpec,
151            f: glib::ffi::gpointer,
152        ) {
153            unsafe {
154                let f: &F = &*(f as *const F);
155                f(
156                    &from_glib_borrow(this),
157                    &from_glib_borrow(object),
158                    &from_glib_borrow(p0),
159                )
160            }
161        }
162        unsafe {
163            let f: Box_<F> = Box_::new(f);
164            let detailed_signal_name = detail.map(|name| format!("stream-notify::{name}\0"));
165            let signal_name: &[u8] = detailed_signal_name
166                .as_ref()
167                .map_or(&b"stream-notify\0"[..], |n| n.as_bytes());
168            connect_raw(
169                self.as_ptr() as *mut _,
170                signal_name.as_ptr() as *const _,
171                Some(transmute::<*const (), unsafe extern "C" fn()>(
172                    stream_notify_trampoline::<F> as *const (),
173                )),
174                Box_::into_raw(f),
175            )
176        }
177    }
178
179    pub fn iter(&self) -> Iter<'_> {
180        Iter::new(self)
181    }
182
183    pub fn len(&self) -> usize {
184        self.size() as usize
185    }
186
187    pub fn is_empty(&self) -> bool {
188        self.len() == 0
189    }
190
191    pub fn debug(&self) -> Debug<'_> {
192        Debug(self)
193    }
194}
195
196impl<'a> IntoIterator for &'a StreamCollection {
197    type IntoIter = Iter<'a>;
198    type Item = Stream;
199
200    fn into_iter(self) -> Self::IntoIter {
201        self.iter()
202    }
203}
204
205pub struct Debug<'a>(&'a StreamCollection);
206
207impl fmt::Debug for Debug<'_> {
208    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
209        struct Streams<'a>(&'a StreamCollection);
210
211        impl fmt::Debug for Streams<'_> {
212            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
213                let mut f = f.debug_list();
214
215                for stream in self.0.iter() {
216                    f.entry(&stream.debug());
217                }
218
219                f.finish()
220            }
221        }
222
223        let streams = Streams(self.0);
224
225        f.debug_struct("StreamCollection")
226            .field("streams", &streams)
227            .finish()
228    }
229}