diff --git a/quest/m1/explicit-state/README.md b/quest/m1/explicit-state/README.md index f672dab642d..1ac75384e30 100644 --- a/quest/m1/explicit-state/README.md +++ b/quest/m1/explicit-state/README.md @@ -73,7 +73,6 @@ which collapses that state and waits on [Drop set](/quest/m1/drop-set.md). - [js/qmux close](/quest/m1/explicit-state/qmux-close.md) - a graceful remote close ends qmux streams cleanly - [FFI handle state](/quest/m1/explicit-state/ffi-state.md) - FFI handles hold one state each: video follow under one lock, requests answered once, drivers Cancelled after shutdown - [moq-net phase data](/quest/m1/explicit-state/net-phases.md) - moq-net's session and subscription state machines keep each phase's data in its variant -- [TS and fMP4 export lifecycles](/quest/m1/explicit-state/mux-gst.md) - the TS export program and tracks and the fMP4 export track are explicit state ## Related diff --git a/quest/m1/explicit-state/mux-gst.md b/quest/m1/explicit-state/mux-gst.md deleted file mode 100644 index ead70fb397a..00000000000 --- a/quest/m1/explicit-state/mux-gst.md +++ /dev/null @@ -1,38 +0,0 @@ -# [M] TS and fMP4 export lifecycles - -## Goal - -The TS and fMP4 export lifecycles in moq-mux are explicit state: the TS -export program and its tracks, the TS follower, passthrough, and group -buffer, and the fMP4 export track. Each phase's data lives in its variant, -and no `.expect()` or flag ordering stands in for a phase. - -## Plan - -The shared decisions are in the [epic](/quest/m1/explicit-state/README.md). - -No confirmed bug is folded into this child. Two suspected ones were checked -and are not defects: - -- An fMP4 export track that ends before it is described, while the catalog - stays live, keeps `init_ready()` false. That wait is bounded: once other - tracks have media it fails at the init queue limit, naming the track. - Otherwise the live catalog can still move the rendition. -- A withdrawn TS track whose parked frames already went out stays listed. - This is intended, and `revision_test.rs` covers it. - -Instances to redesign: - -- **TS export `Program`** (`rs/moq-mux/src/container/ts/export.rs`): - `catalog`, `catalog_ended`, `cataloged`, `retired`, `left`, `psi`, and - others. `resume` hand-resets six fields. -- **TS export track**: `finished` carries five meanings, and the "gone" - predicate ANDs eight conditions. -- **TS `Follower.refused`** (`ts/follower.rs`) belongs inside `Serving`. The - TS `Passthrough` start and clock, and the `GroupBuffer` content kind, also - become explicit. -- **fMP4 export track** (`rs/moq-mux/src/container/fmp4/export.rs`): - `source`, `described`, `finished`, `resume`, and `group_finished` encode - its phases. Land after - [moq-dev/moq#7916](https://github.com/moq-dev/moq/pull/7916), which - rewrites this file. diff --git a/rs/moq-mux/src/container/consumer.rs b/rs/moq-mux/src/container/consumer.rs index 29d0872c2a7..b1cc3a6dd4e 100644 --- a/rs/moq-mux/src/container/consumer.rs +++ b/rs/moq-mux/src/container/consumer.rs @@ -554,7 +554,7 @@ impl Consumer { // buffers past a full max buffer, so a start is always found. let any_frame = self.pending.iter_mut().enumerate().any(|(i, group)| { let _ = group.poll_min_timestamp(waiter, &self.format, i == 0); - !group.empty + !group.is_empty() }); if any_frame { // A live reader takes the newest group, though its frames may still be on their @@ -670,8 +670,8 @@ impl Consumer { Poll::Ready(Ok(None)) => { // A data track's marker carries no timestamp, so it ends where this reader // got to. Before the first item there is nothing to end. - if group.bare - && !std::mem::replace(&mut group.ended, true) + if let Content::Bare { ended } = &mut group.content + && !std::mem::replace(ended, true) && let Some(edge) = self.edge { let end = u64::try_from(edge.as_micros()) @@ -755,7 +755,7 @@ impl Consumer { if finished || elapsed || aged || excluded || full { let _ = self.pending.front_mut().unwrap().look_all(waiter, &self.format); let front = self.pending.front_mut().unwrap(); - let empty = front.empty && matches!(front.group.poll_finished(waiter), Poll::Ready(Ok(_))); + let empty = front.is_empty() && matches!(front.group.poll_finished(waiter), Poll::Ready(Ok(_))); let front = self.pending.front().unwrap(); let next_start = front.min_timestamp.map(std::time::Duration::from).or(front.max_end); // A data marker has no timestamp to prove continuity across the hole. @@ -1111,7 +1111,7 @@ impl Consumer { self.pending.retain_mut(|group| { // Keep the cursor's gap and anything already decoded, including markers and faults. // A never-read evicted group holds no event that its missing sequence cannot say. - group.sequence == current || !group.empty || !group.poll_aborted(waiter) + group.sequence == current || !group.is_empty() || !group.poll_aborted(waiter) }); self.collect_at = self.pending.len().saturating_mul(2).max(1); } @@ -1239,18 +1239,7 @@ struct GroupBuffer { // The current frame index within the group. index: usize, - // Whether the group has carried any wire frame. Empty groups (zero objects) mean - // nothing; a finished group with wire frames and no media is a marker. - empty: bool, - - // Whether a decodable (non-marker) frame was buffered. - media: bool, - - // Whether this is a data track's marker group: one object with no bytes. - bare: bool, - - // Whether a marker with no timestamp of its own was read as an end. - ended: bool, + content: Content, // A decode error found while looking ahead, delivered after the frames buffered before it. fault: Option, @@ -1277,6 +1266,19 @@ struct GroupBuffer { max_end: Option, } +/// What a group has carried so far. +enum Content { + /// No wire frame: an empty group means nothing. + Empty, + /// Wire frames but no media, so a marker once it finishes. + Marker, + /// A data track's marker: one object with no bytes. `ended` once it was read as an end, + /// since it has no timestamp of its own. + Bare { ended: bool }, + /// A decodable (non-marker) frame was buffered. + Media, +} + impl GroupBuffer { fn new(group: moq_net::group::Consumer, memory: Memory) -> Self { let mut this = Self { @@ -1284,10 +1286,7 @@ impl GroupBuffer { late: group.arrived(), group, index: 0, - empty: true, - media: false, - bare: false, - ended: false, + content: Content::Empty, fault: None, buffered: VecDeque::new(), markers: VecDeque::new(), @@ -1336,17 +1335,22 @@ impl GroupBuffer { Err(err) if self.poll_aborted(waiter) => return Poll::Ready(Err(Failure::Lost(err.into()))), Err(err) => return Poll::Ready(Err(Failure::Decode(err.into()))), }; - self.empty = false; + if matches!(self.content, Content::Empty) { + self.content = Content::Marker; + } let arrived = wire.arrived(); let size = wire.payload.len(); let frames = format.decode(wire).map_err(Failure::Decode)?; if format.kind() == super::Kind::Data { // A data track's marker is one object with no bytes, alone in its group. - if self.bare || (frames.is_empty() && self.media) { - let err = moq_net::Error::Decode(moq_net::DecodeError::ExpectedEnd).into(); - return Poll::Ready(Err(Failure::Decode(err))); + match (&self.content, frames.is_empty()) { + (Content::Bare { .. }, _) | (Content::Media, true) => { + let err = moq_net::Error::Decode(moq_net::DecodeError::ExpectedEnd).into(); + return Poll::Ready(Err(Failure::Decode(err))); + } + (_, true) => self.content = Content::Bare { ended: false }, + (_, false) => {} } - self.bare = frames.is_empty(); } // Decoded payloads are views of the wire payload, which stays allocated while any of them is @@ -1384,7 +1388,7 @@ impl GroupBuffer { // the container's flag otherwise so CMAF mid-group keyframes survive. frame.keyframe = frame.keyframe || self.index == 0; self.index += 1; - self.media = true; + self.content = Content::Media; let cost = frame_cost(&frame); views += frame.payload.len(); @@ -1495,7 +1499,12 @@ impl GroupBuffer { } fn marker(&self) -> bool { - !self.empty && !self.media + matches!(self.content, Content::Marker | Content::Bare { .. }) + } + + /// The group has carried no wire frame. + fn is_empty(&self) -> bool { + matches!(self.content, Content::Empty) } fn charge(&mut self, bytes: usize) { @@ -5730,7 +5739,7 @@ mod tests { track.finish().unwrap(); assert!(consumer.poll_received(&kio::Waiter::noop()).is_pending()); assert!( - consumer.pending.iter().all(|group| group.empty), + consumer.pending.iter().all(|group| group.is_empty()), "receipt must not decode payload" ); group.finish().unwrap(); diff --git a/rs/moq-mux/src/container/fmp4/export.rs b/rs/moq-mux/src/container/fmp4/export.rs index c0f69acc8b2..0e09351d057 100644 --- a/rs/moq-mux/src/container/fmp4/export.rs +++ b/rs/moq-mux/src/container/fmp4/export.rs @@ -63,7 +63,7 @@ const INIT_QUEUE: Duration = Duration::from_secs(30); /// fragments belong to one track. pub struct Export { source: crate::Source, - catalog: Option, + catalog: Feed, max_delay: Duration, fragment_duration: Option, @@ -76,23 +76,12 @@ pub struct Export { /// so a publisher cycling names holds only what it recorded. departed: HashMap, - /// The first track id the init segment does not declare. - first_late_id: u32, - - /// Fragments cut before the init segment, written right after it in the order they - /// were cut. - queued: VecDeque<(String, Fragment)>, - - /// Set after the init segment has been emitted, which fixes the track set. - init_emitted: bool, + init: Init, /// Keep a rendition that joins after the init segment instead of failing, for a writer /// that declares it elsewhere. Its fragments carry a track id the init never declared. late: bool, - /// Set by [`stop`](Self::stop), after which nothing more is read. - stopped: bool, - /// The `tkhd.track_id` the next new rendition gets. next_track_id: u32, @@ -106,6 +95,50 @@ pub struct Export { sequence_number: u32, } +/// Where the catalog subscription stands. +enum Feed { + Live(S), + /// The publisher ended the catalog. + Ended, + /// [`Export::stop`] dropped it, so media no track could be described for is discarded. + Stopped, +} + +impl Feed { + fn live(&self) -> bool { + matches!(self, Feed::Live(_)) + } +} + +/// Whether the init segment went out, which fixes the track set. +enum Init { + /// Fragments cut before the init segment, written right after it in the order they were cut. + Waiting(VecDeque<(String, Fragment)>), + /// `first_late` is the first track id the init does not declare, and `queued` the + /// fragments cut while it waited, still to go out. + Written { + first_late: u32, + queued: VecDeque, + }, +} + +impl Init { + fn written(&self) -> bool { + matches!(self, Init::Written { .. }) + } + + /// The init segment went out, declaring every track id below `first_late`. + fn write(&mut self, first_late: u32) { + if let Init::Waiting(queued) = self { + let queued = std::mem::take(queued) + .into_iter() + .map(|(_, fragment)| fragment) + .collect(); + *self = Init::Written { first_late, queued }; + } + } +} + /// One emitted CMAF chunk: the init segment, then media fragments. /// /// The two carry different metadata, so they are separate variants rather than one @@ -233,9 +266,107 @@ struct Declaration { provisional: bool, } +/// Where a track's subscription stands. +enum Phase { + /// Reading the rendition's subscription. + Reading(Subscription), + /// The subscription ended while the rendition stays in the catalog. + Ended(Subscription), + /// The rendition left the catalog after the init. One that returns resumes here. + Departed(Resume), + /// [`Export::stop`] ended the read. + Stopped, +} + +/// A track's subscription to its rendition. +struct Subscription { + source: ExportSource, + /// Checked against the track's declaration. Until then its frames are dropped: they + /// come before the codec configuration they need. + described: bool, +} + +/// A run of frames that becomes one fragment. +struct Batch { + frames: Vec, + /// The first frame was a keyframe, so the fragment can start an HLS segment. + /// Meaningless for audio. + independent: bool, +} + +/// The frames a track accumulates for its next moof+mdat, flushed when the next frame +/// opens a group or on the duration cap. +#[derive(Default)] +enum Buffer { + #[default] + Empty, + Open(Batch), + /// The source group closed, so the fragment can be emitted. + Closed(Batch), +} + +impl Buffer { + fn batch(&self) -> Option<&Batch> { + match self { + Buffer::Empty => None, + Buffer::Open(batch) | Buffer::Closed(batch) => Some(batch), + } + } + + fn is_empty(&self) -> bool { + self.batch().is_none() + } + + fn is_closed(&self) -> bool { + matches!(self, Buffer::Closed(_)) + } + + fn first(&self) -> Option<&Frame> { + self.batch()?.frames.first() + } + + fn last(&self) -> Option<&Frame> { + self.batch()?.frames.last() + } + + fn last_mut(&mut self) -> Option<&mut Frame> { + match self { + Buffer::Empty => None, + Buffer::Open(batch) | Buffer::Closed(batch) => batch.frames.last_mut(), + } + } + + /// Append to the open run, or open one with `frame`. + fn push(&mut self, frame: Frame) { + match self { + Buffer::Open(batch) => batch.frames.push(frame), + _ => { + *self = Buffer::Open(Batch { + independent: frame.keyframe, + frames: vec![frame], + }) + } + } + } + + /// End the open run, so its fragment can go out. + fn close(&mut self) { + *self = match std::mem::take(self) { + Buffer::Open(batch) => Buffer::Closed(batch), + buffer => buffer, + }; + } + + fn take(&mut self) -> Option { + match std::mem::take(self) { + Buffer::Empty => None, + Buffer::Open(batch) | Buffer::Closed(batch) => Some(batch), + } + } +} + struct Fmp4Track { - /// The subscription while the rendition is in the catalog. - source: Option, + phase: Phase, config: Rendition, @@ -243,20 +374,10 @@ struct Fmp4Track { /// must keep its sample entry, except to settle a provisional one. declaration: Option, - /// The current subscription has been checked against `declaration`. Until then its - /// frames are dropped: they come before the codec configuration they need. - described: bool, - /// The next decoded frame from the source, used for cross-track timestamp ordering. pending: Option, - /// Frames accumulated for the current fragment. Flushed as a single - /// moof+mdat when the next frame opens a group, or on the duration cap. - buffer: Vec, - - /// Whether the first frame of the current `buffer` was a keyframe, i.e. the - /// fragment it produces can start an HLS segment. Meaningless for audio. - buffer_independent: bool, + buffer: Buffer, /// True if this track is video. Its samples are independently decodable only /// at a keyframe; audio samples always are. @@ -269,14 +390,6 @@ struct Fmp4Track { /// (Legacy / LOC sources). Derived from the catalog framerate / sample rate. default_frame: Duration, - /// Whether the source has signalled end-of-track, or the rendition left the catalog. - finished: bool, - /// Where the subscription stopped when the rendition left the catalog, which a - /// resubscription resumes from. - resume: Option, - /// The source group closed, so its buffered fragment can be emitted. - group_finished: bool, - /// The latest presentation time written, so a resubscription that replays it fails /// instead of writing the track's history twice. written: Option, @@ -306,19 +419,17 @@ impl Fmp4Track { Rendition::Audio(config) => catalog_timescale_audio(config)?, }; Ok(Self { - source: Some(source), + phase: Phase::Reading(Subscription { + source, + described: false, + }), is_video: config.is_video(), opus: matches!(&config, Rendition::Audio(config) if matches!(config.codec, AudioCodec::Opus)), default_frame: config.default_frame(), config, declaration: None, - described: false, pending: None, - buffer: Vec::new(), - buffer_independent: false, - finished: false, - resume: None, - group_finished: false, + buffer: Buffer::Empty, written: None, decode_end: None, backlog: None, @@ -340,13 +451,54 @@ impl Fmp4Track { .map(|frame| Duration::from(frame.timestamp)) } + /// The subscription, while the rendition is in the catalog. + fn subscription(&self) -> Option<&Subscription> { + match &self.phase { + Phase::Reading(subscription) | Phase::Ended(subscription) => Some(subscription), + Phase::Departed(_) | Phase::Stopped => None, + } + } + + fn subscription_mut(&mut self) -> Option<&mut Subscription> { + match &mut self.phase { + Phase::Reading(subscription) | Phase::Ended(subscription) => Some(subscription), + Phase::Departed(_) | Phase::Stopped => None, + } + } + + /// Nothing more will be read: the subscription ended, or the rendition left. + fn finished(&self) -> bool { + !matches!(self.phase, Phase::Reading(_)) + } + + fn described(&self) -> bool { + self.subscription().is_some_and(|subscription| subscription.described) + } + + /// The source signalled end-of-track. + fn end(&mut self) { + self.phase = match std::mem::replace(&mut self.phase, Phase::Stopped) { + Phase::Reading(subscription) => Phase::Ended(subscription), + phase => phase, + }; + } + + /// The rendition left the catalog after the init: end the subscription, keeping where + /// it stopped for a return. What was already read still goes out. + fn depart(&mut self) { + self.phase = match std::mem::replace(&mut self.phase, Phase::Stopped) { + Phase::Reading(subscription) | Phase::Ended(subscription) => Phase::Departed(subscription.source.resume()), + phase => phase, + }; + } + /// Apply a newer catalog entry for the same rendition. fn reconfigure(&mut self, name: &str, config: Rendition) -> Result<()> { if config.is_video() != self.is_video { return Err(Error::TrackChanged(name.to_string()).into()); } - if let (Rendition::Video(video), Some(source)) = (&config, self.source.as_mut()) - && !source.note_catalog(video) + if let (Rendition::Video(video), Some(subscription)) = (&config, self.subscription_mut()) + && !subscription.source.note_catalog(video) { return Err(Error::TrackChanged(name.to_string()).into()); } @@ -359,15 +511,16 @@ impl Fmp4Track { /// dropped, and the buffered run goes out as a fragment of its own, before the new track's. fn moved(&mut self) { self.pending = None; - self.group_finished = !self.buffer.is_empty(); + self.buffer.close(); } /// Resubscribe a rendition that left the catalog after the init and came back, or moved /// to another track. fn rejoin(&mut self, name: &str, config: Rendition, source: ExportSource) -> Result<()> { - self.source = Some(source); - self.finished = false; - self.described = false; + self.phase = Phase::Reading(Subscription { + source, + described: false, + }); self.reconfigure(name, config) } @@ -392,7 +545,9 @@ impl Fmp4Track { } None => self.declaration = Some(declaration), } - self.described = true; + if let Some(subscription) = self.subscription_mut() { + subscription.described = true; + } Ok(()) } @@ -406,7 +561,7 @@ impl Fmp4Track { Rendition::Video(config) => match &config.container { Container::Cmaf { init, .. } => init, Container::Legacy | Container::Loc => { - let Some(source) = self.source.as_ref() else { + let Some(Subscription { source, .. }) = self.subscription() else { return Ok(None); }; if !source.header_ready() || !source.video_geometry_ready(config) { @@ -513,16 +668,13 @@ impl Export { pub fn new(source: crate::Source, catalog: S) -> Self { Self { source, - catalog: Some(catalog), + catalog: Feed::Live(catalog), max_delay: Duration::ZERO, fragment_duration: None, tracks: HashMap::new(), departed: HashMap::new(), - first_late_id: u32::MAX, - queued: VecDeque::new(), - init_emitted: false, + init: Init::Waiting(VecDeque::new()), late: false, - stopped: false, next_track_id: 1, sequence_number: 1, } @@ -574,12 +726,10 @@ impl Export { /// /// Stopping before the init segment could be written ends the export without writing. pub(crate) fn stop(&mut self) { - self.catalog = None; + self.catalog = Feed::Stopped; for track in self.tracks.values_mut() { - track.source = None; - track.finished = true; + track.phase = Phase::Stopped; } - self.stopped = true; } /// The `trak` and `trex` declaring `track_id`, once the track could be described. @@ -629,11 +779,11 @@ impl Export { // samples that is. loop { // 1. Drain catalog updates and (un)subscribe tracks accordingly. - while let Some(catalog) = self.catalog.as_mut() { + while let Feed::Live(catalog) = &mut self.catalog { match catalog.poll_next(waiter)? { Poll::Ready(Some(snapshot)) => self.update_catalog(&snapshot.without_ext())?, Poll::Ready(None) => { - self.catalog = None; + self.catalog = Feed::Ended; break; } Poll::Pending => break, @@ -647,16 +797,16 @@ impl Export { // A track that can't be described yet drops its frames, so the source keeps // polling for the SPS/PPS-bearing keyframe instead of parking. for (name, track) in &mut self.tracks { - if track.pending.is_some() || track.finished || track.group_finished { + if track.pending.is_some() || track.buffer.is_closed() { continue; } - while let Some(source) = track.source.as_mut() { - match source.poll_event(waiter)? { + while let Phase::Reading(subscription) = &mut track.phase { + match subscription.source.poll_event(waiter)? { Poll::Ready(Some(Event::Frame { frame, .. })) => { // A keyframe may carry new parameter sets, so check it still matches. - if !track.described || frame.keyframe { + if !track.described() || frame.keyframe { track.describe(name)?; - if !track.described { + if !track.described() { continue; } } @@ -681,11 +831,11 @@ impl Export { if track.buffer.is_empty() { continue; } - track.group_finished = true; + track.buffer.close(); break; } Poll::Ready(None) => { - track.finished = true; + track.end(); break; } Poll::Pending => break, @@ -695,21 +845,22 @@ impl Export { // 3. Emit the init segment once every track can be described, then the // fragments queued while it waited. - if !self.init_emitted { + if let Init::Waiting(queued) = &self.init { if self.init_ready() { let init = self.build_init()?; - self.init_emitted = true; - self.first_late_id = self.next_track_id; + self.init.write(self.next_track_id); return Poll::Ready(Ok(Some(Chunk::Init(init)))); } - if self.catalog.is_none() && self.tracks.values().all(|t| t.finished) { + if !self.catalog.live() && self.tracks.values().all(Fmp4Track::finished) { // Media was read but can't be written without every track's description. - if !self.queued.is_empty() && !self.stopped { + if !queued.is_empty() && !matches!(self.catalog, Feed::Stopped) { return Poll::Ready(Err(Error::TrackUndescribed(self.undescribed()).into())); } return Poll::Ready(Ok(None)); } - } else if let Some((_, fragment)) = self.queued.pop_front() { + } else if let Init::Written { queued, .. } = &mut self.init + && let Some(fragment) = queued.pop_front() + { return Poll::Ready(Ok(Some(Chunk::Fragment(fragment)))); } @@ -717,9 +868,8 @@ impl Export { // (or an ended track's tail) once no other track holds earlier content. if let Some(name) = self.completed_fragment() { let track = self.tracks.get_mut(&name).unwrap(); - track.group_finished = false; - let frames = std::mem::take(&mut track.buffer); - let fragment = emit_fragment(&name, track, &mut self.sequence_number, frames, None)?; + let batch = track.buffer.take().unwrap(); + let fragment = emit_fragment(&name, track, &mut self.sequence_number, batch, None)?; if let Some(chunk) = self.deliver(name, fragment) { return Poll::Ready(Ok(Some(chunk))); } @@ -745,7 +895,7 @@ impl Export { let track = self.tracks.get_mut(&name).unwrap(); let frame = track.pending.take().unwrap(); // Everything a track reads before the init is queued or buffered behind it. - if !self.init_emitted { + if !self.init.written() { let start = *track.backlog.get_or_insert(frame.timestamp.into()); if Duration::from(frame.timestamp).saturating_sub(start) > INIT_QUEUE { return Poll::Ready(Err(Error::TrackUndescribed(self.undescribed()).into())); @@ -754,32 +904,29 @@ impl Export { // A zero cap emits one sample at a time. Unknown video duration still // needs a successor timestamp or endpoint before it can be encoded. let fragment = if frag == Some(Duration::ZERO) { - if !track.buffer.is_empty() { + if let Some(batch) = track.buffer.take() { // A catalog change can leave buffered frames behind. Drain them // first and retry this frame on the next poll. - let frames = std::mem::take(&mut track.buffer); - let fragment = emit_fragment(&name, track, &mut self.sequence_number, frames, Some(&frame))?; + let fragment = emit_fragment(&name, track, &mut self.sequence_number, batch, Some(&frame))?; track.pending = Some(frame); fragment + } else if track.is_video && frame.duration.is_none() { + track.buffer.push(frame); + continue; } else { - track.buffer_independent = frame.keyframe; - if track.is_video && frame.duration.is_none() { - track.buffer.push(frame); - continue; - } - emit_fragment(&name, track, &mut self.sequence_number, vec![frame], None)? + let batch = Batch { + independent: frame.keyframe, + frames: vec![frame], + }; + emit_fragment(&name, track, &mut self.sequence_number, batch, None)? } - } else if should_flush(track, &frame, frag) { - let frames = std::mem::take(&mut track.buffer); - let fragment = emit_fragment(&name, track, &mut self.sequence_number, frames, Some(&frame))?; + } else if should_flush(&track.buffer, &frame, frag) { + let batch = track.buffer.take().unwrap(); + let fragment = emit_fragment(&name, track, &mut self.sequence_number, batch, Some(&frame))?; // The flushed run is done; the incoming frame opens the next buffer. - track.buffer_independent = frame.keyframe; track.buffer.push(frame); fragment } else { - if track.buffer.is_empty() { - track.buffer_independent = frame.keyframe; - } track.buffer.push(frame); // Frame appended to buffer; go round again to look for more work or a flush. continue; @@ -798,20 +945,17 @@ impl Export { .tracks .iter() .filter_map(|(name, t)| { - if (t.finished || t.group_finished) && !t.buffer.is_empty() { - Some((Duration::from(t.buffer[0].timestamp), !t.is_video, name.clone())) - } else { - None - } + let first = t.buffer.first()?; + (t.finished() || t.buffer.is_closed()) + .then(|| (Duration::from(first.timestamp), !t.is_video, name.clone())) }) .min() .map(|(_, _, name)| name); if let Some(name) = flushable { let track = self.tracks.get_mut(&name).unwrap(); - track.group_finished = false; - let frames = std::mem::take(&mut track.buffer); - let fragment = emit_fragment(&name, track, &mut self.sequence_number, frames, None)?; + let batch = track.buffer.take().unwrap(); + let fragment = emit_fragment(&name, track, &mut self.sequence_number, batch, None)?; if let Some(chunk) = self.deliver(name, fragment) { return Poll::Ready(Ok(Some(chunk))); } @@ -819,9 +963,9 @@ impl Export { } // 7. If catalog is closed and every track is finished and drained, we're done. - if self.init_emitted - && self.catalog.is_none() - && self.tracks.values().all(|t| t.finished && t.buffer.is_empty()) + if self.init.written() + && !self.catalog.live() + && self.tracks.values().all(|t| t.finished() && t.buffer.is_empty()) { return Poll::Ready(Ok(None)); } @@ -833,11 +977,13 @@ impl Export { /// Hand a fragment to the caller, or queue it behind the init segment that has yet /// to go out. fn deliver(&mut self, name: String, fragment: Fragment) -> Option { - if self.init_emitted { - return Some(Chunk::Fragment(fragment)); + match &mut self.init { + Init::Written { .. } => Some(Chunk::Fragment(fragment)), + Init::Waiting(queued) => { + queued.push_back((name, fragment)); + None + } } - self.queued.push_back((name, fragment)); - None } /// The renditions holding up the init segment, by name. @@ -845,7 +991,7 @@ impl Export { let mut names: Vec = self .tracks .iter() - .filter(|(_, track)| !track.described) + .filter(|(_, track)| !track.described()) .map(|(name, _)| name.clone()) .collect(); names.sort(); @@ -857,15 +1003,15 @@ impl Export { let earliest = self.tracks.values().filter_map(Fmp4Track::next_start).min()?; self.tracks .iter() - .filter(|(_, track)| { - !track.buffer.is_empty() - // A stopped track's pending frame joins its buffer first. - && ((track.finished && track.pending.is_none()) - || track.group_finished - || (self.fragment_duration == Some(Duration::ZERO) - && track.buffer.last().is_some_and(|frame| frame.duration.is_some()))) + .filter_map(|(name, track)| { + let first = track.buffer.first()?; + // A stopped track's pending frame joins its buffer first. + let complete = (track.finished() && track.pending.is_none()) + || track.buffer.is_closed() + || (self.fragment_duration == Some(Duration::ZERO) + && track.buffer.last().is_some_and(|frame| frame.duration.is_some())); + complete.then(|| (Duration::from(first.timestamp), !track.is_video, name)) }) - .map(|(name, track)| (Duration::from(track.buffer[0].timestamp), !track.is_video, name)) .filter(|(start, _, _)| *start <= earliest) .min() .map(|(_, _, name)| name.clone()) @@ -910,15 +1056,15 @@ impl Export { let moved = self .tracks .get(&name) - .and_then(|track| track.source.as_ref()) - .is_some_and(|source| !config.read_by(source, &self.source, &name)); + .and_then(Fmp4Track::subscription) + .is_some_and(|subscription| !config.read_by(&subscription.source, &self.source, &name)); // Before the init nothing of a moved rendition went out, so it starts over as a new one. - if moved && !self.init_emitted { + if moved && let Init::Waiting(queued) = &mut self.init { self.tracks.remove(&name); - self.queued.retain(|(queued, _)| *queued != name); + queued.retain(|(queued, _)| *queued != name); } match self.tracks.get_mut(&name) { - Some(track) if track.source.is_some() && !moved => track.reconfigure(&name, config)?, + Some(track) if track.subscription().is_some() && !moved => track.reconfigure(&name, config)?, Some(track) => { let Some(source) = config.subscribe(&self.source, &name, self.max_delay)? else { continue; @@ -926,15 +1072,13 @@ impl Export { // The publisher still caches the group the rendition left on, which a fresh // subscription would start at, so resume after what was read. A move is new // content, which continues past what was read under the declared track. - let source = match track.source.take() { - Some(previous) => { + let source = match std::mem::replace(&mut track.phase, Phase::Stopped) { + Phase::Reading(previous) | Phase::Ended(previous) => { track.moved(); - source.continuing(&previous) + source.continuing(&previous.source) } - None => match &track.resume { - Some(resume) => source.resuming(resume), - None => source, - }, + Phase::Departed(resume) => source.resuming(&resume), + Phase::Stopped => source, }; track.rejoin(&name, config, source)?; } @@ -944,7 +1088,7 @@ impl Export { let Some(source) = config.subscribe(&self.source, &name, self.max_delay)? else { continue; }; - if self.init_emitted && !self.late { + if self.init.written() && !self.late { return Err(Error::TrackAdded(name).into()); } let mut track = Fmp4Track::new(source, config, self.next_track_id)?; @@ -956,30 +1100,29 @@ impl Export { active.insert(name); } - if self.init_emitted { - // The init declared these tracks, so keep them for a rendition that returns. - // Ending the subscription writes out what was already read. - for (name, track) in &mut self.tracks { - if !active.contains(name) - && let Some(source) = track.source.take() - { - track.resume = Some(source.resume()); - track.finished = true; - track.described = false; + match &mut self.init { + Init::Written { first_late, .. } => { + // The init declared these tracks, so keep them for a rendition that returns. + // Ending the subscription writes out what was already read. + for (name, track) in &mut self.tracks { + if !active.contains(name) { + track.depart(); + } } - } - // A departed track still holding media waits for the next catalog to move. - let drained = self - .tracks - .extract_if(|_, track| track.source.is_none() && track.pending.is_none() && track.buffer.is_empty()); - for (name, track) in drained { - if track.written.is_some() || track.track_id < self.first_late_id { - self.departed.insert(name, track); + // A departed track still holding media waits for the next catalog to move. + let drained = self.tracks.extract_if(|_, track| { + track.subscription().is_none() && track.pending.is_none() && track.buffer.is_empty() + }); + for (name, track) in drained { + if track.written.is_some() || track.track_id < *first_late { + self.departed.insert(name, track); + } } } - } else { - self.tracks.retain(|name, _| active.contains(name)); - self.queued.retain(|(name, _)| active.contains(name)); + Init::Waiting(queued) => { + self.tracks.retain(|name, _| active.contains(name)); + queued.retain(|(name, _)| active.contains(name)); + } } Ok(()) @@ -987,7 +1130,7 @@ impl Export { /// True once every track can be described, so the merged init segment can be built. fn init_ready(&self) -> bool { - !self.tracks.is_empty() && self.tracks.values().all(|t| t.described) + !self.tracks.is_empty() && self.tracks.values().all(Fmp4Track::described) } /// Build the merged ftyp + multi-track moov init segment, video first, each kind in @@ -1061,17 +1204,19 @@ pub(crate) fn extract_init( Ok(()) } -/// Should we flush `track.buffer` before appending the incoming `frame`? +/// Should we flush `buffer` before appending the incoming `frame`? /// /// Triggers when `frame` opens a group, or on the duration cap. The keyframe bit is the /// group boundary on every track: a video keyframe closes a GOP, and an audio frame /// carries it only where the publisher cut a group, which is the boundary the file /// should keep rather than one invented here. Per-frame output is handled before /// this check. -fn should_flush(track: &Fmp4Track, frame: &Frame, fragment_duration: Option) -> bool { - if track.buffer.is_empty() { - return false; - } +fn should_flush(buffer: &Buffer, frame: &Frame, fragment_duration: Option) -> bool { + let batch = match buffer { + Buffer::Empty => return false, + Buffer::Closed(_) => return true, + Buffer::Open(batch) => batch, + }; if frame.keyframe { return true; } @@ -1082,7 +1227,7 @@ fn should_flush(track: &Fmp4Track, frame: &Frame, fragment_duration: Option, + batch: Batch, successor: Option<&Frame>, ) -> Result { + let Batch { + mut frames, + independent, + } = batch; apply_codec_durations(&mut frames, track.opus); + // A run that starts or ends inside a GOP sees only part of its reordering. + let partial = !independent || successor.is_some_and(|next| !next.keyframe); // Every audio sample is independently decodable, so every audio fragment is // independent; video is only when its buffer opened on a keyframe (a GOP boundary). - let independent = !track.is_video || track.buffer_independent; + let independent = !track.is_video || independent; if let (Some(first), Some(written)) = (frames.first(), track.written) { let start = Duration::from(first.timestamp); if independent && start <= written { @@ -1157,8 +1308,6 @@ fn emit_fragment( let latest = frames.iter().map(|frame| Duration::from(frame.timestamp)).max(); track.written = track.written.max(latest); let timescale = moq_net::Timescale::new(track.timescale)?; - // A run that starts or ends inside a GOP sees only part of its reordering. - let partial = !track.buffer_independent || successor.is_some_and(|next| !next.keyframe); let run = if partial && track.config.may_reorder() { Run::PartialGop } else { @@ -1430,6 +1579,17 @@ mod tests { } } + // A rendition moving off a track whose group already closed keeps that fragment. + #[test] + fn closing_a_closed_buffer_keeps_its_frames() { + let mut buffer = Buffer::Empty; + buffer.push(group_start(0)); + buffer.close(); + buffer.close(); + assert!(buffer.is_closed()); + assert_eq!(buffer.take().map(|batch| batch.frames.len()), Some(1)); + } + /// A frame that opens a group. Every group starts with a keyframe, so this is what /// the far side of a group boundary looks like to `infer_missing_durations`. fn group_start(timestamp_us: u64) -> Frame { diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 91149d6ef5f..1ba4bd8da58 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -135,23 +135,18 @@ where S::Ext: catalog::Catalog, { id: u64, - retired: bool, - /// Why the publisher left, reported once the output it buffered has gone out. - left: Option, expected: Vec, expected_changed: bool, source: crate::Source, + /// Kept once read no further, for its format, which a follower subscribes a return with, + /// until a follow runs out on it: a replacement is then selected, and the subscription goes. catalog: Option, - /// Keep the concrete follower's format after EOF without polling an ended stream again. - catalog_ended: bool, /// A snapshot arrived on `catalog` since it was subscribed. cataloged: bool, + feed: Feed, /// The instance of the broadcast being exported, `None` for one not resolved through an /// origin. instance: Option, - /// Tracks [`Export::follow`] left on the ended broadcast, resubscribed as the returned - /// catalog lists them. - stale: HashSet, /// The PMT `version_number` the program's first tables go out at, advanced by each switch to /// another instance so a demux that caches tables by version reads the new ones. version: VersionNumber, @@ -237,6 +232,17 @@ where /// Each elementary stream's access units and silence on the PCR returned ([`Export::stats`]). liveness: super::import::Liveness, } +/// Where a program's input stands. +enum Feed { + /// Reading catalog updates. + Live, + /// The publisher ended the catalog. + Ended, + /// The input stopped ([`Export::retire`]), with why the publisher left if it did, reported + /// once the output it buffered has gone out. + Retired(Option), +} + /// What a queued output frame tells [`Export::stats`], applied only once the frame is /// returned: a rewind drops the queue unwritten. struct Tally { @@ -323,16 +329,13 @@ struct Track { steps: u64, /// The highest PTS read on the track's timeline, and the marker count it was read under. high: Option<(u64, Timestamp)>, - finished: bool, + read: Read, pid: u16, /// The PMT lists it: from its first frame out until its last has gone and the catalog has /// dropped it. Its PID, kind and descriptors stay as listed. listed: bool, /// The catalog lists it. cataloged: bool, - /// Read no further because the catalog withdrew it before the PMT listed it. A return - /// under the same name reads it on. - withdrawn: bool, /// Frames released while its PID still carries the stream before it, which go out once /// that stream's last has. parked: VecDeque, @@ -380,6 +383,19 @@ struct Track { released: Option<(Timestamp, Timestamp, Option)>, } +/// Whether a track still reads its source. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Read { + Live, + /// Read to its end: the source ended, the program retired, or a track listed on its PID + /// replaced it. + Ended, + /// The catalog withdrew it before the PMT listed it. A return under the same name reads it on. + Withdrawn, + /// [`Export::follow`] left it on the ended broadcast. The returned catalog resubscribes it. + Stale, +} + /// Shared program state needed while reading one track. struct Input<'a> { waiting_for_header: bool, @@ -402,11 +418,10 @@ impl Track { base: (0, 0, 0), steps: 0, high: None, - finished: false, + read: Read::Live, pid: 0, listed: false, cataloged: true, - withdrawn: false, parked: VecDeque::new(), kind, descriptors: Vec::new(), @@ -569,7 +584,7 @@ impl Track { if self.key.name.is_empty() { self.key.name = name.to_string(); } - if self.pending.is_some() || self.finished { + if self.pending.is_some() || self.read != Read::Live { return Ok(()); } let is_video = matches!(self.kind, Kind::Video(_)); @@ -631,7 +646,7 @@ impl Track { Poll::Ready(Some(Event::GroupEnd)) => self.settle = true, Poll::Ready(Some(Event::FrameEnd { .. })) => {} Poll::Ready(None) => { - self.finished = true; + self.read = Read::Ended; break; } Poll::Pending => { @@ -643,7 +658,7 @@ impl Track { } } // Nothing more can come in below the frames still held for their DTS. - if self.finished && !input.waiting_for_header { + if self.read == Read::Ended && !input.waiting_for_header { self.release(name, input.jitter, true)?; } Ok(()) @@ -795,7 +810,7 @@ impl Track { let Some((mut pending, description)) = item else { continue; }; - if !self.finished { + if self.read == Read::Live { pending.arrived = pending.arrived.max(self.latest); } self.push(name, pending, Some(dts), description, jitter)?; @@ -817,7 +832,7 @@ impl Track { let Some(((mut pending, description), dts)) = self.clock.pop(lookahead, delay, settle) else { break; }; - if !self.finished { + if self.read == Read::Live { pending.arrived = pending.arrived.max(self.latest); } self.push(name, pending, Some(dts.into()), description, jitter)?; @@ -1555,16 +1570,13 @@ where let instance = source.broadcast().info().instance.clone(); Self { id: 0, - retired: false, - left: None, expected: Vec::new(), expected_changed: false, source, catalog: Some(catalog), - catalog_ended: false, cataloged: false, + feed: Feed::Live, instance, - stale: HashSet::new(), version: VersionNumber::default(), pat_version: VersionNumber::default(), flagged: None, @@ -1703,7 +1715,10 @@ where if let Err(err) = self.prepare_pending(waiter) { // A replacement that leaves before it commits is no switch: the old program // plays on, and a later replacement gets its own follow window. - if self.jitter.is_committed() || self.program.retired || !super::follower::left(&err) { + if self.jitter.is_committed() + || matches!(self.program.feed, Feed::Retired(_)) + || !super::follower::left(&err) + { return Poll::Ready(Err(err)); } tracing::warn!(%err, "replacement left before it went out, keeping the old program"); @@ -1779,7 +1794,7 @@ where .program .tracks .values_mut() - .all(|track| track.finished || track.source.poll_received(waiter).is_ready()), + .all(|track| track.read != Read::Live || track.source.poll_received(waiter).is_ready()), ); // 4. Mux each frame the jitter buffer lets go (the first carries the buffered @@ -1873,7 +1888,7 @@ where // Releasing a due frame opens room in its source. Read it before parking: // a zero-delay slot may need another frame to prove the slot is complete. let track = self.program.tracks.get_mut(&name).expect("the released frame's track"); - let finished = track.finished; + let live = track.read == Read::Live; let filled = track.fill( &name, waiter, @@ -1884,16 +1899,16 @@ where trimmed: &mut self.program.trimmed, }, ); - let ended = !finished && track.finished; + let ended = live && track.read != Read::Live; if let Err(err) = filled { self.leave(err)?; } - if ended || self.program.retired { + if ended || matches!(self.program.feed, Feed::Retired(_)) { self.program.schedule.set_ended( self.program .tracks .values_mut() - .all(|track| track.finished || track.source.poll_received(waiter).is_ready()), + .all(|track| track.read != Read::Live || track.source.poll_received(waiter).is_ready()), ); } } @@ -1915,7 +1930,11 @@ where // independent of the catalog: a retained track finishes while the broadcast stays // live, and holding its tail until the catalog closed would strand it indefinitely. let drained = !self.program.tracks.is_empty() - && self.program.tracks.values().all(|t| t.finished && t.parked.is_empty()) + && self + .program + .tracks + .values() + .all(|t| t.read != Read::Live && t.parked.is_empty()) && self.jitter.is_empty() && self.held.is_none(); if drained { @@ -1930,7 +1949,7 @@ where } // SI emission rides media frames, so a snapshot that arrived behind the // last one gets one trailing flush before the stream ends. - if self.program.catalog_ended && !self.program.si_flushed { + if !matches!(self.program.feed, Feed::Live) && !self.program.si_flushed { self.program.si_flushed = true; if let Some(out) = self.write_si_tail()? { return Poll::Ready(Ok(Some(out))); @@ -1940,8 +1959,8 @@ where let pending_ended = !self.jitter.is_committed() && self.pending.as_ref().is_some_and(|pending| { - let finished = pending.catalog_ended - && (pending.tracks.is_empty() || pending.tracks.values().all(|track| track.finished)); + let finished = !matches!(pending.feed, Feed::Live) + && (pending.tracks.is_empty() || pending.tracks.values().all(|track| track.read != Read::Live)); let no_video_start = pending.video_out.is_none() && pending .tracks @@ -1963,8 +1982,14 @@ where } // End of stream once the catalog is closed too: nothing more can appear. - if self.pending.is_none() && self.program.catalog_ended && (drained || self.program.tracks.is_empty()) { - return Poll::Ready(self.program.left.take().map_or(Ok(None), Err)); + if self.pending.is_none() + && !matches!(self.program.feed, Feed::Live) + && (drained || self.program.tracks.is_empty()) + { + return Poll::Ready(match &mut self.program.feed { + Feed::Retired(left) => left.take().map_or(Ok(None), Err), + Feed::Live | Feed::Ended => Ok(None), + }); } Poll::Pending @@ -2044,24 +2069,22 @@ where } fn prepare(&mut self, waiter: &kio::Waiter) -> crate::Result { - if self.program.retired || (self.program.id == self.active_program && self.jitter.is_committed()) { + if matches!(self.program.feed, Feed::Retired(_)) + || (self.program.id == self.active_program && self.jitter.is_committed()) + { return Ok(true); } // 1. Drain catalog updates, discovering the track layout. - while !self.program.catalog_ended { - match self - .program - .catalog - .as_mut() - .expect("an open catalog") - .poll_next(waiter)? - { + while matches!(self.program.feed, Feed::Live) + && let Some(catalog) = self.program.catalog.as_mut() + { + match catalog.poll_next(waiter)? { Poll::Ready(Some(snapshot)) => { self.program.cataloged = true; self.update_catalog(snapshot)? } Poll::Ready(None) => { - self.program.catalog_ended = true; + self.program.feed = Feed::Ended; break; } Poll::Pending => break, @@ -2105,10 +2128,6 @@ where // packets instead, letting the leading PAT/PMT inherit a real timestamp. if self.program.psi.is_none() { if self.program.tracks.is_empty() { - // No tracks yet. If the catalog is also done, the broadcast is empty. - if self.program.catalog_ended { - return Ok(false); - } return Ok(false); } if !self.header_ready() || !self.video_ready() { @@ -2116,10 +2135,6 @@ where // and, when the program has a video rendition, its first keyframe is // buffered: the stream must begin on that keyframe so the in-band // parameter sets lead it. An audio-only program has nothing to wait for. - // If every track finished without producing a config, it can't be muxed. - if self.program.catalog_ended && self.program.tracks.values().all(|t| t.finished) { - return Ok(false); - } return Ok(false); } self.build_psi()?; @@ -2178,16 +2193,14 @@ where return Err(err); } tracing::debug!(target: "moq_mux::container::ts::handoff", phase = "left", now = ?kio::clock::now(), program = self.program.id, committed = self.jitter.is_committed(), %err); - if self.pending.is_none() { - self.program.left = Some(err); - } + let left = self.pending.is_none().then_some(err); self.follow_timer = None; self.follow_until = None; - self.retire() + self.retire(left) } fn stop_old(&mut self, waiter: &kio::Waiter) -> crate::Result<()> { - if self.program.retired { + if matches!(self.program.feed, Feed::Retired(_)) { return Ok(()); } @@ -2208,21 +2221,20 @@ where self.follow_timer = None; self.follow_until = None; self.program.catalog = None; - self.retire() + self.retire(None) } - /// Stop the program's input. What it already buffered still goes out, unless a - /// committed replacement's switch drops it. - fn retire(&mut self) -> crate::Result<()> { - self.program.retired = true; - self.program.catalog_ended = true; + /// Stop the program's input, reporting `left` once what it already buffered has gone out, + /// unless a committed replacement's switch drops it. + fn retire(&mut self, left: Option) -> crate::Result<()> { + self.program.feed = Feed::Retired(left); for (name, track) in &mut self.program.tracks { - if !track.finished { + if track.read == Read::Live { if !self.jitter.is_committed() { track.release(name, &mut self.jitter, true)?; } track.source.stop(); - track.finished = true; + track.read = Read::Ended; } } for si in self.program.si.values_mut() { @@ -2385,9 +2397,7 @@ where } if self.program.psi.is_some() { - if !self.program.stale.is_empty() { - self.resubscribe(&catalog, &mpegts)?; - } + self.resubscribe(&catalog, &mpegts)?; self.rebind(&catalog)?; // A listed track keeps its PID, kind and descriptors, which its packets went out // under, even when the catalog moves it to another track: a stream the source @@ -2407,13 +2417,13 @@ where // name that returns before that is gone is the same stream, so it is read on. for (name, track) in self.program.tracks.iter_mut() { track.cataloged = active.contains_key(name); - if track.cataloged && std::mem::take(&mut track.withdrawn) { - track.finished = false; - } - if !track.cataloged && !track.listed && !track.finished { - track.release(name, &mut self.jitter, true)?; - track.finished = true; - track.withdrawn = true; + match track.read { + Read::Withdrawn if track.cataloged => track.read = Read::Live, + Read::Live if !track.cataloged && !track.listed => { + track.release(name, &mut self.jitter, true)?; + track.read = Read::Withdrawn; + } + _ => {} } } self.revise()?; @@ -2566,7 +2576,7 @@ where .program .tracks .iter() - .filter(|(_, track)| !track.finished) + .filter(|(_, track)| track.read == Read::Live) .filter(|(_, track)| track.kind.class() != super::stats::Class::Data || track.buffer.is_some()); self.program.expected = continuous.map(|(_, track)| track.key.clone()).collect(); self.program.expected_changed = true; @@ -2584,13 +2594,13 @@ where /// Point each stale track this snapshot lists at the returned broadcast. /// /// The PIDs and PMT stay as announced. A track the returned catalog does not list stays - /// finished, silent on its PID. The new subscription's playhead jumped from wherever the + /// stale, silent on its PID. The new subscription's playhead jumped from wherever the /// old one stopped, so its first frame counts as a skip: late frames drop, and a leap /// ahead opens a new generation. fn resubscribe(&mut self, catalog: &Catalog, mpegts: &catalog::Mpegts) -> anyhow::Result<()> { let budget = self.budget(); for (name, track) in self.program.tracks.iter_mut() { - if !self.program.stale.contains(name) { + if track.read != Read::Stale { continue; } let source = if let Some(config) = catalog.video.renditions.get(name) { @@ -2612,14 +2622,13 @@ where let Some(source) = source else { continue; }; - self.program.stale.remove(name); // The new subscription counts as cutting whatever group the old one was reading. track.base.0 += track.source.markers(); track.base.1 += track.source.skips() + 1; track.base.2 += track.source.cuts() + 1; track.source = source; track.subscribed = kio::clock::now(); - track.finished = false; + track.read = Read::Live; } Ok(()) } @@ -2665,7 +2674,7 @@ where track.clock = DecodeClock::default(); track.declared = None; track.source = source.continuing(&track.source); - track.finished = false; + track.read = Read::Live; track.subscribed = kio::clock::now(); } Ok(()) @@ -2773,7 +2782,8 @@ where /// every source has finished and the tail runs out. #[cfg(test)] pub(super) fn next_due(&self) -> Option { - let finished = !self.program.tracks.is_empty() && self.program.tracks.values().all(|track| track.finished); + let finished = + !self.program.tracks.is_empty() && self.program.tracks.values().all(|track| track.read != Read::Live); let slot = self .slot_due() .filter(|_| self.program.schedule.queued() || self.packing() || finished) @@ -2908,19 +2918,15 @@ where fn resume(&mut self, catalog: S) -> Result<(), crate::Error> { self.program.catalog = Some(catalog); - self.program.catalog_ended = false; self.program.cataloged = false; - self.program.retired = false; - self.program.left = None; + self.program.feed = Feed::Live; self.program.si_flushed = false; for si in self.program.si.values_mut() { si.track.clear(); } for (name, track) in &mut self.program.tracks { track.release(name, &mut self.jitter, true)?; - track.finished = true; - track.withdrawn = false; - self.program.stale.insert(name.clone()); + track.read = Read::Stale; } Ok(()) } @@ -3022,7 +3028,7 @@ where .tracks .values() .filter(|t| matches!(t.kind, Kind::Video(_))) - .all(|t| t.pending.is_some() || t.finished) + .all(|t| t.pending.is_some() || t.read != Read::Live) } /// The smallest timestamp among the video tracks' buffered frames: the first @@ -3325,14 +3331,11 @@ where .iter_mut() .find(|(other, track)| track.listed && track.pid == pid && other.as_str() != name) { - if !track.finished { + if track.read == Read::Live { track.release(holder, &mut self.jitter, true)?; - track.finished = true; } - // Replaced, so a return of its name no longer reads it on. - track.withdrawn = false; - // A same-instance return that put another track on its PID replaced it. - self.program.stale.remove(holder.as_str()); + // Replaced, so neither a return of its name nor a same-instance return reads it on. + track.read = Read::Ended; return Ok(false); } self.program.tracks.get_mut(name).expect("checked above").listed = true; @@ -3347,27 +3350,26 @@ where if self.program.psi.is_none() { return Ok(()); } - let gone: Vec = - self.program - .tracks - .iter() - .filter(|(name, track)| { - // A track the same instance's return has yet to resubscribe stays, silent on its PID. - !track.cataloged - && !self.program.stale.contains(name.as_str()) - && track.finished && track.pack.is_none() - && track.parked.is_empty() - && !self.jitter.holds(&track.key) - && self.held.as_ref().is_none_or(|held| held.track != track.key) - && !(track.listed && self.program.schedule.holds(track.pid)) - }) - .map(|(name, _)| name.clone()) - .collect(); + let gone: Vec = self + .program + .tracks + .iter() + .filter(|(_, track)| { + // A track the same instance's return has yet to resubscribe stays, silent on its PID. + !track.cataloged + && matches!(track.read, Read::Ended | Read::Withdrawn) + && track.pack.is_none() + && track.parked.is_empty() + && !self.jitter.holds(&track.key) + && self.held.as_ref().is_none_or(|held| held.track != track.key) + && !(track.listed && self.program.schedule.holds(track.pid)) + }) + .map(|(name, _)| name.clone()) + .collect(); let mut changed = false; for name in gone { let track = self.program.tracks.remove(&name).expect("collected above"); self.jitter.forget(&TrackKey::new(self.program.id, &name)); - self.program.stale.remove(&name); if track.listed { self.program.liveness.retire(track.pid); self.program.schedule.forget(track.pid); diff --git a/rs/moq-mux/src/container/ts/follower.rs b/rs/moq-mux/src/container/ts/follower.rs index 3ceb2887dfe..2370f4a537d 100644 --- a/rs/moq-mux/src/container/ts/follower.rs +++ b/rs/moq-mux/src/container/ts/follower.rs @@ -19,6 +19,9 @@ enum Serving { Return(moq_net::broadcast::Consumer), /// Another stream: a broadcast of another instance. Other(moq_net::broadcast::Consumer), + /// A return or another stream that refused its catalog: nothing is followed until the + /// next change. + Refused, } /// Subscribing to an instance's catalog, to carry the export on into it. @@ -86,8 +89,6 @@ pub struct Follower { closed: bool, /// Why the last return refused its catalog, reported instead of the end if the follow runs out. failed: Option, - /// The instance serving now refused its catalog: nothing is followed until the next change. - refused: bool, follow: Duration, stitch: bool, timer: Option>>, @@ -112,7 +113,6 @@ impl Follower { priming: true, closed: false, failed: None, - refused: false, follow: Duration::ZERO, stitch: false, timer: None, @@ -206,11 +206,10 @@ impl Follower { State::Settling { end, deadline } => { self.poll_instances(waiter)?; let target = match &self.serving { - _ if self.refused => None, Serving::Return(target) => Some(target.clone()), Serving::Other(target) if self.stitch => Some(target.clone()), Serving::Other(_) => return Poll::Ready(Err(crate::Error::Replaced(self.path.to_string()))), - Serving::Ours | Serving::Gone => None, + Serving::Ours | Serving::Gone | Serving::Refused => None, }; if let Some(target) = target { self.state = State::Resuming { @@ -253,7 +252,7 @@ impl Follower { if !left(&err) { self.failed = Some(err); } - self.refused = true; + self.serving = Serving::Refused; self.state = State::Settling { end, deadline }; } Poll::Pending => { @@ -278,7 +277,7 @@ impl Follower { /// subscriptions yield priority while acquiring the replacement. fn poll_switch(&mut self, waiter: &kio::Waiter, switching: &mut Option>) -> crate::Result { let target = match &self.serving { - Serving::Other(target) if self.stitch && !self.refused => target, + Serving::Other(target) if self.stitch => target, _ => { if switching.take().is_some() { self.export.source().priority(0)?; @@ -305,7 +304,7 @@ impl Follower { Poll::Ready(Err(err)) if left(&err) => { tracing::warn!(path = %self.path, %err, "replacement went before its catalog subscribed"); *switching = None; - self.refused = true; + self.serving = Serving::Refused; self.export.source().priority(0)?; Ok(false) } @@ -358,7 +357,6 @@ impl Follower { } any = true; } - self.refused &= !any; Ok(any) } diff --git a/rs/moq-mux/src/container/ts/passthrough.rs b/rs/moq-mux/src/container/ts/passthrough.rs index 190f3484276..fa28aae3869 100644 --- a/rs/moq-mux/src/container/ts/passthrough.rs +++ b/rs/moq-mux/src/container/ts/passthrough.rs @@ -143,20 +143,14 @@ pub struct Passthrough { /// Whether the PMT of the clock's program has been read. mapped: bool, - timebase: Option, + timing: Clock, /// A `discontinuity_indicator` on the clock PID, waiting for the PCR that it flags (normally /// in the same packet). pending_break: bool, - /// Before the first group: the PCR time the importer started waiting for a group start at. - waiting_since: Option, /// The PCR time of the last object written, which no later one goes below. written: u64, - /// The object being filled, from its first packet. - open: Option, - /// Closed objects waiting on the next PCR for their timestamps. - queue: VecDeque, - group: Option, + groups: Groups, /// Whether every group so far started at a random access point, and the layout allowed random /// access throughout. all_random_access: bool, @@ -187,6 +181,28 @@ struct Program { video: Vec, } +/// The clock pacing the multiplex. +enum Clock { + /// No PCR yet: nothing can be timed, so no object opens. + Unseen, + Seen { + timebase: Timebase, + /// The object being filled, from its first packet. + open: Option, + /// Closed objects waiting on the next PCR for their timestamps. + queue: VecDeque, + }, +} + +/// The groups written so far. +enum Groups { + /// Before the first group: the PCR time the importer started waiting for a group start at, + /// once the clock's program is mapped and a PCR seen. + Waiting(Option), + /// The group being written. + Writing(Group), +} + /// The PCRs since the importer's first, or since the last flagged jump. struct Timebase { /// The last PCR, raw. @@ -260,13 +276,10 @@ impl Passthrough { access_pid: None, single: false, mapped: false, - timebase: None, + timing: Clock::Unseen, pending_break: false, - waiting_since: None, written: 0, - open: None, - queue: VecDeque::new(), - group: None, + groups: Groups::Waiting(None), all_random_access: true, mux_rate: Meter::default(), input: crate::container::Input::Open, @@ -442,24 +455,29 @@ impl Passthrough { start = Some(Start::RandomAccess); } - if start.is_some() || (clock && self.started()) { - if !clock { - self.close_object(); + let opens = start.is_some() || (clock && self.started()); + let Clock::Seen { open, queue, .. } = &mut self.timing else { + return Ok(()); + }; + if opens { + // A PCR packet already closed the object before it. + if !clock && let Some(object) = open.take() { + queue.push_back(object); } let mut payload = BytesMut::with_capacity(TsPacket::SIZE * 8); payload.extend_from_slice(pkt); - self.open = Some(Object { + *open = Some(Object { at, payload, start, discontinuity, }); - } else if let Some(open) = self.open.as_mut() { + } else if let Some(open) = open.as_mut() { open.payload.extend_from_slice(pkt); } - let waiting = self.open.as_ref().map_or(0, |open| open.payload.len() as u64) - + self.queue.iter().map(|object| object.payload.len() as u64).sum::(); + let waiting = open.as_ref().map_or(0, |open| open.payload.len() as u64) + + queue.iter().map(|object| object.payload.len() as u64).sum::(); if waiting > GROUP_BYTES_MAX { return Err(crate::Error::PcrGap { pid: self.pcr_pid.unwrap_or_default(), @@ -476,13 +494,19 @@ impl Passthrough { self.close_object(); let flagged = std::mem::take(&mut self.pending_break); - let Some(timebase) = self.timebase.as_mut() else { + let Clock::Seen { timebase, .. } = &mut self.timing else { // The first PCR starts the count at its own value. - self.timebase = Some(Timebase { - raw, - samples: VecDeque::from([Sample { at, ticks: raw }]), - }); - self.waiting_since = self.mapped.then_some(raw); + self.timing = Clock::Seen { + timebase: Timebase { + raw, + samples: VecDeque::from([Sample { at, ticks: raw }]), + }, + open: None, + queue: VecDeque::new(), + }; + if let Groups::Waiting(since) = &mut self.groups { + *since = self.mapped.then_some(raw); + } if self.mux_rate.pcr(raw, self.offset / TsPacket::SIZE as u64) { self.record()?; } @@ -506,10 +530,15 @@ impl Passthrough { // and the new side carries on from there. self.drain_queue()?; let ticks = (last + step).max(self.written); - self.timebase = Some(Timebase { - raw, - samples: VecDeque::from([Sample { at, ticks }]), - }); + // The object before was closed and the queue drained, so both start empty. + self.timing = Clock::Seen { + timebase: Timebase { + raw, + samples: VecDeque::from([Sample { at, ticks }]), + }, + open: None, + queue: VecDeque::new(), + }; if self.mux_rate.discontinuity() { self.record()?; } @@ -517,8 +546,8 @@ impl Passthrough { self.record()?; } // The jump is not elapsed time, so the wait for the first group starts over. - if self.waiting_since.is_some() { - self.waiting_since = Some(ticks); + if let Groups::Waiting(Some(since)) = &mut self.groups { + *since = ticks; } return Ok(self.started().then_some(Start::Discontinuity)); } @@ -534,10 +563,10 @@ impl Passthrough { self.record()?; } - let since = match &self.group { - Some(group) => Some(group.ticks), - None if self.mapped => Some(*self.waiting_since.get_or_insert(ticks)), - None => None, + let since = match &mut self.groups { + Groups::Writing(group) => Some(group.ticks), + Groups::Waiting(since) if self.mapped => Some(*since.get_or_insert(ticks)), + Groups::Waiting(_) => None, }; Ok(since .filter(|since| ticks.saturating_sub(*since) >= FALLBACK_TICKS && self.can_start()) @@ -547,28 +576,35 @@ impl Passthrough { /// Whether a group may start here: once the first one has, always; before it, only once the /// clock's program is mapped and a PCR has been seen. fn can_start(&self) -> bool { - self.started() || (self.mapped && self.timebase.is_some()) + self.started() || (self.mapped && matches!(self.timing, Clock::Seen { .. })) } /// Whether the first group has started, written or still waiting on its timestamp. fn started(&self) -> bool { - self.group.is_some() || self.open.is_some() || !self.queue.is_empty() + matches!(self.groups, Groups::Writing(_)) + || matches!(&self.timing, Clock::Seen { open, queue, .. } if open.is_some() || !queue.is_empty()) } fn close_object(&mut self) { - if let Some(object) = self.open.take() { - self.queue.push_back(object); + if let Clock::Seen { open, queue, .. } = &mut self.timing + && let Some(object) = open.take() + { + queue.push_back(object); } } /// Time and write every waiting object against the current timebase. fn drain_queue(&mut self) -> crate::Result<()> { - while let Some(object) = self.queue.pop_front() { - let timebase = self.timebase.as_ref().expect("an object follows a PCR"); + loop { + let Clock::Seen { timebase, queue, .. } = &mut self.timing else { + return Ok(()); + }; + let Some(object) = queue.pop_front() else { + return Ok(()); + }; let ticks = locate(object.at, &timebase.samples); self.write(object, ticks)?; } - Ok(()) } fn write(&mut self, object: Object, ticks: u64) -> crate::Result<()> { @@ -579,31 +615,34 @@ impl Passthrough { // The first object, which starts a group, anchors the shift and is live on arrival. let timestamp = self.shift.shift(timestamp)?; let size = object.payload.len() as u64; - let start = object.start.or_else(|| { - self.group - .as_ref() - .filter(|group| group.bytes + size > GROUP_BYTES_MAX || group.objects >= GROUP_OBJECTS_MAX) - .map(|_| Start::Full) - }); + let start = match (object.start, &mut self.groups) { + (Some(start), _) => Some(start), + (None, Groups::Writing(group)) => { + if group.bytes + size > GROUP_BYTES_MAX || group.objects >= GROUP_OBJECTS_MAX { + Some(Start::Full) + } else { + group.bytes += size; + group.objects += 1; + None + } + } + // Only an object that starts a group opens before the first one. + (None, Groups::Waiting(_)) => Some(Start::Fallback), + }; if let Some(start) = start { - if object.discontinuity && self.group.is_some() { + if object.discontinuity && matches!(self.groups, Groups::Writing(_)) { self.media.discontinuity()?; } if start != Start::RandomAccess || !self.single { self.all_random_access = false; } - self.group = Some(Group { + self.groups = Groups::Writing(Group { ticks, - bytes: 0, - objects: 0, + bytes: size, + objects: 1, }); } - - // Only an object that starts a group is ever opened before the first one. - let group = self.group.as_mut().expect("the first object starts a group"); - group.bytes += size; - group.objects += 1; self.media.write(crate::container::Frame { timestamp, duration: None, @@ -619,7 +658,7 @@ impl Passthrough { /// Write the section to the catalog if it changed. Nothing is written before the first group. fn record(&mut self) -> crate::Result<()> { - if self.group.is_none() && self.section.is_none() { + if matches!(self.groups, Groups::Waiting(_)) && self.section.is_none() { return Ok(()); } let mut section = hang::catalog::M2ts::new(self.media.name());