Skip to main content

moq_mux/catalog/
stream.rs

1//! Catalog stream trait.
2//!
3//! [`Stream`] yields a sequence of [`Catalog<E>`](super::hang::Catalog) snapshots, each with the
4//! timestamp it takes effect at. Both the
5//! raw [`Consumer`](super::Consumer) and the rendition-selecting
6//! [`Select`](super::Select) wrapper implement it, so exporters can be written
7//! against the trait and the caller picks the selection policy.
8//!
9//! The yielded catalog carries the application extension `E` (defaulting to
10//! `()` for media-only catalogs) via the [`Ext`](Stream::Ext) associated type,
11//! so an exporter that only touches `video`/`audio` works for any extension.
12
13use std::task::Poll;
14
15use super::Select;
16use super::hang::{Catalog, CatalogExt};
17
18/// A stream of catalog snapshots.
19///
20/// `poll_next` returns the next snapshot (a full catalog, not a delta) with the timestamp it takes
21/// effect at, or `None` once the underlying track has ended. Late snapshots supersede
22/// earlier ones, so an implementation may drop intermediate snapshots.
23///
24/// Stream types are required to be `Send + 'static` so they can be moved
25/// across threads and held inside exporters without per-call bounds.
26pub trait Stream: Send + 'static {
27	/// The application extension carried by the yielded catalog (`()` for media-only).
28	type Ext: CatalogExt;
29
30	/// Poll for the next catalog snapshot and the timestamp it takes effect at on the broadcast's
31	/// media timeline, or `None` once the stream ends. A scale-less catalog track has no timeline:
32	/// there the timestamp is the group key (see [`moq_json::Timed`]), so apply it on arrival.
33	#[allow(clippy::type_complexity, reason = "the same shape every catalog consumer returns")]
34	fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll<crate::Result<Option<moq_json::Timed<Catalog<Self::Ext>>>>>;
35
36	/// Wait for the next snapshot.
37	#[cfg(feature = "async")]
38	fn next(
39		&mut self,
40	) -> impl std::future::Future<Output = crate::Result<Option<moq_json::Timed<Catalog<Self::Ext>>>>> + Send
41	where
42		Self: Sized,
43	{
44		async move { kio::wait(|waiter| self.poll_next(waiter)).await }
45	}
46
47	/// Wrap this stream in a [`Select`] that drops every rendition `selection`
48	/// doesn't keep.
49	fn select(self, selection: crate::select::Broadcast) -> Select<Self>
50	where
51		Self: Sized,
52	{
53		Select::new(self, selection)
54	}
55}
56
57/// A boxed stream forwards to the stream it holds, so `Box<dyn Stream<Ext = E>>` is a stream.
58impl<S: Stream + ?Sized> Stream for Box<S> {
59	type Ext = S::Ext;
60
61	fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll<crate::Result<Option<moq_json::Timed<Catalog<Self::Ext>>>>> {
62		(**self).poll_next(waiter)
63	}
64}