Skip to main content

moq_net/model/
meta.rs

1//! Metadata subscriptions: a track's group and frame progress without its payload.
2//!
3//! A metadata subscription is a subscription of its own beside any payload subscription to
4//! the same track, with its mode fixed for its life. Where the hop carries the mode (moq-lite-07),
5//! it travels upstream as a metadata subscription and no payload crosses that hop for it. Over
6//! an older version, which cannot carry the mode, it rides the track's payload subscription and
7//! drops the payload, so it never downloads the payload twice.
8//!
9//! A reader that wants the payload after all opens a payload subscription from the group it
10//! reached and skips to its first missing frame ([`group::Consumer::skip_to`]): nothing changes
11//! mode in place.
12
13use std::ops::Deref;
14use std::task::{Poll, ready};
15
16use crate::{Result, broadcast, frame, group, track};
17
18/// A lazy handle to a track's metadata, from [`broadcast::Consumer::meta`].
19///
20/// Like [`track::Consumer`], holding it requests nothing: only a [`Self::subscribe`] made from
21/// it is demand.
22#[derive(Clone)]
23pub struct Consumer {
24	inner: track::Consumer,
25}
26
27impl Consumer {
28	pub(super) fn new(inner: track::Consumer) -> Self {
29		Self { inner }
30	}
31
32	/// The track name this handle is bound to.
33	pub fn name(&self) -> &str {
34		self.inner.name()
35	}
36
37	/// The broadcast this track belongs to, as reached through the handle it came from.
38	pub fn broadcast(&self) -> &broadcast::Info {
39		self.inner.broadcast()
40	}
41
42	/// Open a live metadata subscription, delivering the groups `subscription` covers as
43	/// [`track::Consumer::subscribe`] does, each frame as its header alone.
44	pub fn subscribe(&self, subscription: impl Into<Option<track::Subscription>>) -> kio::Pending<Subscribing> {
45		kio::Pending::new(Subscribing {
46			inner: self.inner.subscribe(subscription).into_inner(),
47		})
48	}
49}
50
51/// The pollable state of a [`Consumer::subscribe`]; awaited via the [`kio::Pending`] wrapper.
52pub struct Subscribing {
53	inner: track::Subscribing,
54}
55
56impl Subscribing {
57	/// Poll until the publisher confirms the subscription, yielding the [`Subscriber`].
58	pub fn poll_ok(&self, waiter: &kio::Waiter) -> Poll<Result<Subscriber>> {
59		let inner = ready!(self.inner.poll_ok(waiter))?;
60		Poll::Ready(Ok(Subscriber { inner }))
61	}
62
63	/// Change the subscription preferences before (or after) it resolves; see
64	/// [`track::Subscribing::update`].
65	pub fn update(&mut self, subscription: track::Subscription) -> Result<()> {
66		self.inner.update(subscription)
67	}
68}
69
70impl kio::Task for Subscribing {
71	type Output = Result<Subscriber>;
72
73	fn poll(&mut self, waiter: &kio::Waiter) -> Poll<Self::Output> {
74		self.poll_ok(waiter)
75	}
76}
77
78/// A live metadata subscription: [`track::Subscriber`] whose groups hold headers alone.
79pub struct Subscriber {
80	inner: track::Subscriber,
81}
82
83impl Subscriber {
84	/// The track's [`track::Info`], resolved when the subscription was established.
85	pub fn info(&self) -> &track::Info {
86		self.inner.info()
87	}
88
89	/// The track's name, unique within its broadcast.
90	pub fn name(&self) -> &str {
91		self.inner.name()
92	}
93
94	/// The broadcast this track belongs to, as reached through the handle it came from.
95	pub fn broadcast(&self) -> &broadcast::Info {
96		self.inner.broadcast()
97	}
98
99	/// Create a handle for updating this subscriber's delivery preferences.
100	pub fn control(&self) -> track::Control {
101		self.inner.control()
102	}
103
104	/// Receive the next group, as [`track::Subscriber::poll_recv_group`] does.
105	#[cfg_attr(feature = "async", kio::wrapper)]
106	pub fn poll_recv_group(&mut self, waiter: &kio::Waiter) -> Poll<Result<Option<Group>>> {
107		let group = ready!(self.inner.poll_recv_group(waiter))?;
108		Poll::Ready(Ok(group.map(Group::new)))
109	}
110
111	/// The track's declared end, as [`track::Subscriber::poll_finished`] reports it.
112	#[cfg_attr(feature = "async", kio::wrapper)]
113	pub fn poll_finished(&mut self, waiter: &kio::Waiter) -> Poll<Result<track::Position>> {
114		self.inner.poll_finished(waiter)
115	}
116
117	/// Replace this subscriber's delivery preferences; see [`track::Subscriber::update`].
118	pub fn update(&mut self, subscription: track::Subscription) -> Result<()> {
119		self.inner.update(subscription)
120	}
121}
122
123/// One group of a metadata subscription, read header by header.
124///
125/// Dereferences to its [`group::Info`] for the group's start.
126pub struct Group {
127	inner: group::Consumer,
128}
129
130impl Deref for Group {
131	type Target = group::Info;
132
133	fn deref(&self) -> &Self::Target {
134		&self.inner
135	}
136}
137
138impl Group {
139	fn new(inner: group::Consumer) -> Self {
140		// Stored slices carry each frame's plain size beside them, so a header read neither
141		// waits for nor inflates a compressed frame.
142		Self {
143			inner: inner.compressed(),
144		}
145	}
146
147	/// The index of the next frame this reader returns.
148	pub fn index(&self) -> u64 {
149		self.inner.index()
150	}
151
152	/// The next frame's header: its timestamp and declared plain size, without its payload.
153	///
154	/// Returns `None` once the group ends. A real empty frame reads as size 0.
155	#[cfg_attr(feature = "async", kio::wrapper)]
156	pub fn poll_next_header(&mut self, waiter: &kio::Waiter) -> Poll<Result<Option<frame::Info>>> {
157		let Some(frame) = ready!(self.inner.poll_next_frame(waiter))? else {
158			return Poll::Ready(Ok(None));
159		};
160		// Nothing waits for the payload, so a copy failing partway through it tears nothing.
161		self.inner.forget_payload();
162		Poll::Ready(Ok(Some(frame::Info {
163			size: frame.inflated.unwrap_or(frame.size),
164			timestamp: frame.timestamp,
165		})))
166	}
167}
168
169impl std::fmt::Debug for Group {
170	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171		f.debug_struct("Group").field("start", &self.start).finish()
172	}
173}