Skip to main content

moq_mux/catalog/
select.rs

1//! Rendition-selecting catalog stream.
2//!
3//! [`Select`] wraps any [`Stream`] and applies a [`select::Broadcast`](crate::select::Broadcast)
4//! to each snapshot, dropping renditions that aren't selected before handing the
5//! catalog to an exporter.
6
7use std::task::{Poll, ready};
8
9use super::Stream;
10use super::hang::Catalog;
11use crate::select;
12
13/// A [`Stream`] that keeps only the renditions a [`select::Broadcast`] selects.
14///
15/// The selection is fixed at construction; every snapshot from the inner stream is
16/// narrowed by it. Build one with [`Stream::select`](super::Stream::select) or
17/// [`Select::new`].
18pub struct Select<S: Stream> {
19	inner: S,
20	selection: select::Broadcast,
21}
22
23impl<S: Stream> Select<S> {
24	/// Wrap `inner`, narrowing every snapshot by `selection`.
25	pub fn new(inner: S, selection: select::Broadcast) -> Self {
26		Self { inner, selection }
27	}
28}
29
30impl<S: Stream> Stream for Select<S> {
31	type Ext = S::Ext;
32
33	fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll<crate::Result<Option<moq_json::Timed<Catalog<S::Ext>>>>> {
34		let next = ready!(self.inner.poll_next(waiter))?;
35		Poll::Ready(Ok(next.map(|mut timed| {
36			self.selection.retain(&mut timed.value);
37			timed
38		})))
39	}
40}
41
42#[cfg(test)]
43mod tests {
44	use std::collections::BTreeMap;
45
46	use hang::catalog::{Container, H264, VideoConfig};
47
48	use super::super::hang::Catalog;
49	use super::*;
50
51	/// A one-shot stream: yields its snapshot once, at [`at`], then ends.
52	struct Once(Option<Catalog>);
53
54	fn at() -> moq_net::Timestamp {
55		moq_net::Timestamp::from_millis(7).unwrap()
56	}
57
58	impl Stream for Once {
59		type Ext = ();
60
61		fn poll_next(&mut self, _: &kio::Waiter) -> Poll<crate::Result<Option<moq_json::Timed<Catalog>>>> {
62			Poll::Ready(Ok(self.0.take().map(|catalog| moq_json::Timed::new(catalog, at()))))
63		}
64	}
65
66	fn h264(name: &str) -> (String, VideoConfig) {
67		let mut config = VideoConfig::new(H264 {
68			profile: 0x42,
69			constraints: 0,
70			level: 0x1e,
71			inline: false,
72		});
73		config.container = Container::Legacy;
74		(name.to_string(), config)
75	}
76
77	#[test]
78	fn narrows_each_snapshot() {
79		let mut catalog = Catalog::default();
80		catalog.video.renditions = BTreeMap::from_iter(vec![h264("lo"), h264("hi")]);
81
82		let selection = select::Broadcast::default().video(select::Video::default().name("hi"));
83		let mut stream = Once(Some(catalog)).select(selection);
84
85		let out = match stream.poll_next(&kio::Waiter::noop()) {
86			Poll::Ready(Ok(Some(c))) => c,
87			other => panic!("expected snapshot, got {other:?}"),
88		};
89		assert_eq!(out.value.video.renditions.keys().collect::<Vec<_>>(), vec!["hi"]);
90		assert_eq!(out.at, at(), "the snapshot keeps its timestamp");
91		assert!(matches!(stream.poll_next(&kio::Waiter::noop()), Poll::Ready(Ok(None))));
92	}
93
94	#[test]
95	fn selects_boxed_stream() {
96		let mut catalog = Catalog::default();
97		catalog.video.renditions = BTreeMap::from_iter(vec![h264("lo"), h264("hi")]);
98
99		let boxed: Box<dyn Stream<Ext = ()>> = Box::new(Once(Some(catalog)));
100		let selection = select::Broadcast::default().video(select::Video::default().name("lo"));
101		let mut stream = boxed.select(selection);
102
103		let out = match stream.poll_next(&kio::Waiter::noop()) {
104			Poll::Ready(Ok(Some(c))) => c,
105			other => panic!("expected snapshot, got {other:?}"),
106		};
107		assert_eq!(out.value.video.renditions.keys().collect::<Vec<_>>(), vec!["lo"]);
108		assert!(matches!(stream.poll_next(&kio::Waiter::noop()), Poll::Ready(Ok(None))));
109	}
110}