diff --git a/doc/lib/rs/moq-mux.md b/doc/lib/rs/moq-mux.md index a29e578d10f..2771d1485a5 100644 --- a/doc/lib/rs/moq-mux.md +++ b/doc/lib/rs/moq-mux.md @@ -30,6 +30,10 @@ set at the init segment, so a new rendition or a changed configuration ends the export. The [CLI page](/bin/cli#import) covers these and the MPEG-TS specifics: one program per broadcast, damaged packets, and feed checks. +Exporters take a catalog stream alongside the source. Choose its encoding and +rendition selection before exporting; the same stream shape drives every container. +MPEG-TS metadata is retained when the stream uses `ts::Ext`. + A container read yields frames plus two markers in delivery order: a gap where media after what was read will never arrive (a skip, a hole), carrying where it starts and how long it lasts as an upper bound, and an end where the publisher diff --git a/doc/setup/upgrade.md b/doc/setup/upgrade.md index cd5cceeeeff..2156a52e141 100644 --- a/doc/setup/upgrade.md +++ b/doc/setup/upgrade.md @@ -81,6 +81,11 @@ These land with the next breaking release, not the 2026-09-23 train. the same two flags. In Rust, moq-mux's `ts::Export::resume` is `ts::Export::follow`, `Source::returned` is gone, and `ts::Follower` follows a path's announcements the way both exports do. + `ts::Export::new(source, catalog)` now takes a catalog stream, like the other + container exporters. Subscribe with `source.catalog::(format).await?` + to retain MPEG-TS metadata, or use `()` for media only; select renditions on the + stream before passing it in. `follow(broadcast, catalog)` also takes the replacement + stream. Both methods are synchronous; `with_ts` and `with_catalog_format` are removed. - **moq-mux's `Source` is built from the resolved catalog broadcast.** `Source::new(origin, path)` is `Source::new(origin, broadcast)`: resolve the broadcast first (`origin.request_broadcast(path, None)`), and its path is diff --git a/quest/m1/README.md b/quest/m1/README.md index 2acee7fc5cd..5c896317987 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -160,7 +160,6 @@ above them. - [Vulkan Video encode on AMD](/quest/m1/vulkan-encode.md) - H.264 and H.265 from an external Vulkan image on RADV, hand-rolled on ash, proven on an RX 9070 - [VA-API encodes an external Vulkan image](/quest/m1/vaapi-vulkan-import.md) - an explicit-sync DMA-BUF reaches Intel's encoder, proven on Arrow Lake - [Malformed moq-transport input](/quest/m1/ietf-malformed-close.md) - malformed draft-18 and draft-21 control input closes the session with the draft's code, or PROTOCOL_VIOLATION where a code is a real burden and the fallback is recorded in `doc/concept/standard.md`, in moq-net and js/net -- [ts::Export catalog stream](/quest/m1/ts-export-catalog.md) - TS export takes (source, catalog) like the other exporters - [Binding track resolver](/quest/m1/binding-track-helper.md) - binding apps subscribe to a rendition without inlining `track` or the key - [Exporters follow a rendition to a new track](/quest/m1/export-rendition-rebind.md) - the fMP4, TS, MKV and FLV exporters rebuild a rendition whose resolved `track` or `broadcast` changes under the same ID diff --git a/quest/m1/ts-export-catalog.md b/quest/m1/ts-export-catalog.md deleted file mode 100644 index accd1ebb489..00000000000 --- a/quest/m1/ts-export-catalog.md +++ /dev/null @@ -1,7 +0,0 @@ -# [S] ts::Export takes a catalog stream - -## Goal - -`ts::Export::new` takes `(source, catalog)` like `flv::Export`, `fmp4::Export`, -and `mkv::Export` (#4987), and `with_catalog_format` moves onto the catalog -stream, with no new public type and no compatibility shim. diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index ec1c356720b..99c30306f24 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -675,10 +675,18 @@ pub(crate) mod tests { const VERBATIM_PES_PID: u16 = 0x104; const VERBATIM_PES_STREAM_ID: u8 = 0xC0; + async fn open_export( + source: moq_mux::Source, + format: CatalogFormat, + ) -> moq_mux::Result>> { + let catalog = source.catalog::(format).await?; + Ok(Export::new(source, catalog)) + } + /// Drain an exporter, concatenating every frame's payload until output stops. The /// producers stay alive (retained tracks), so the stream never hard-ends; pull until a /// `next()` blocks, surfaced here as a timeout once the buffered frames are gone. - async fn drain(mut exporter: Export) -> Vec { + async fn drain(mut exporter: Export>) -> Vec { let mut out = Vec::new(); // Past the export's release delay and the send-ahead window behind it, so the first // frame goes out before it ends. @@ -780,7 +788,7 @@ pub(crate) mod tests { // `catalog`, the producers, and `import` stay alive: the exporter subscribes to // the retained tracks. drain( - Export::with_ts(resolved(&origin, "cli").await, CatalogFormat::Hang) + open_export(resolved(&origin, "cli").await, CatalogFormat::Hang) .await .unwrap() .with_delay(RECORDING_MAX_AGE), @@ -812,7 +820,7 @@ pub(crate) mod tests { } /// Full CLI round-trip: a TS feed with undecoded streams goes through `Publish` - /// (which selects the `mpegts` catalog) and the subscribe-side `Export::with_ts`, + /// (which selects the `mpegts` catalog) and the subscribe-side catalog stream, /// and the SCTE-35 section and the verbatim PES survive with their PIDs, framing, /// PES stream_id, and byte-exact payloads. async fn ts_verbatim_round_trip(format: CatalogFormat) { @@ -835,10 +843,10 @@ pub(crate) mod tests { decoder.decode_chunk(&input).unwrap(); decoder.finish().unwrap(); - // Subscribe side: the same `with_ts` call `run_ts` makes, re-emitting the + // Subscribe side: the same catalog stream `run_ts` selects, re-emitting the // ancillary streams verbatim. let output = drain( - Export::with_ts(resolved(&origin, "cli").await, format) + open_export(resolved(&origin, "cli").await, format) .await .unwrap() .with_delay(RECORDING_MAX_AGE), diff --git a/rs/moq-cli/src/subscribe.rs b/rs/moq-cli/src/subscribe.rs index 15ec3457d15..b6f7555d30a 100644 --- a/rs/moq-cli/src/subscribe.rs +++ b/rs/moq-cli/src/subscribe.rs @@ -378,12 +378,11 @@ impl Subscribe { // TS emits PAT/PMT then a continuous PES stream (re-emitting PAT/PMT at // keyframes for tune-in). Avc3/Hev1 sources pass through as Annex-B; AAC - // is re-framed as ADTS. `fragment_duration` does not apply to TS. `with_ts` + // is re-framed as ADTS. `fragment_duration` does not apply to TS. The catalog stream // selects the `mpegts` catalog extension so undecoded elementary streams // (SCTE-35, teletext, DVB AC-3, ...) are re-emitted verbatim on their PIDs. - let mut ts = moq_mux::container::ts::Export::with_ts(self.source, self.catalog) - .await? - .with_delay(self.args.max_delay); + let catalog = self.source.catalog::(self.catalog).await?; + let mut ts = moq_mux::container::ts::Export::new(self.source, catalog).with_delay(self.args.max_delay); if let Some(mux_rate) = self.args.mux_rate { ts = ts.with_mux_rate(mux_rate); } diff --git a/rs/moq-mux/benches/export.rs b/rs/moq-mux/benches/export.rs index df6c204ba09..5e758963369 100644 --- a/rs/moq-mux/benches/export.rs +++ b/rs/moq-mux/benches/export.rs @@ -84,7 +84,7 @@ async fn export(format: &str, source: moq_mux::Source, stream: moq_mux::catalog: chunks += 1; } } else if format == "ts" { - let mut export = moq_mux::container::ts::Export::new(source).await.unwrap(); + let mut export = moq_mux::container::ts::Export::new(source, stream); while let Some(chunk) = export.next().await.unwrap() { std::hint::black_box(chunk); chunks += 1; diff --git a/rs/moq-mux/src/catalog/consumer.rs b/rs/moq-mux/src/catalog/consumer.rs index 94bcf118897..267d0400d38 100644 --- a/rs/moq-mux/src/catalog/consumer.rs +++ b/rs/moq-mux/src/catalog/consumer.rs @@ -16,6 +16,8 @@ use super::{CatalogFormat, Stream}; /// [`Select`](super::Select) to narrow the rendition set before handing the /// stream to an exporter. /// +/// Reaching EOF releases the subscription and decoder while retaining its format. +/// /// The variants are an implementation detail: drive it through the [`Stream`] /// trait rather than matching on them. New catalog encodings may be added. #[non_exhaustive] @@ -24,9 +26,19 @@ pub enum Consumer { Hang(super::hang::Consumer), #[doc(hidden)] Msf(super::msf::Consumer), + #[doc(hidden)] + Closed(CatalogFormat), } impl Consumer { + pub(crate) fn format(&self) -> CatalogFormat { + match self { + Self::Hang(consumer) => consumer.format(), + Self::Msf(_) => CatalogFormat::Msf, + Self::Closed(format) => *format, + } + } + /// Subscribe to the catalog track advertised by `format`. pub async fn new(broadcast: &moq_net::broadcast::Consumer, format: CatalogFormat) -> Result { Ok(match format { @@ -56,9 +68,45 @@ impl Stream for Consumer { type Ext = E; fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>>> { - match self { - Self::Hang(c) => c.poll_next(waiter), - Self::Msf(c) => c.poll_next(waiter), + let next = match self { + Self::Hang(consumer) => consumer.poll_next(waiter), + Self::Msf(consumer) => consumer.poll_next(waiter), + Self::Closed(_) => return Poll::Ready(Ok(None)), + }; + if matches!(&next, Poll::Ready(Ok(None))) { + // A follower still needs the format, but no ended subscription or decoder. + *self = Self::Closed(self.format()); + } + next + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test(start_paused = true)] + async fn eof_releases_each_catalog_subscription() { + for format in [CatalogFormat::Hang, CatalogFormat::HangZ, CatalogFormat::Msf] { + let name = match format { + CatalogFormat::Hang => hang::Catalog::DEFAULT_NAME, + CatalogFormat::HangZ => hang::Catalog::COMPRESSED_NAME, + CatalogFormat::Msf => moq_msf::DEFAULT_NAME, + }; + let broadcast = moq_net::broadcast::Info::new().produce(); + let track = broadcast + .create_track(name, hang::Catalog::default_track_info()) + .unwrap(); + let mut consumer = Consumer::<()>::new(&broadcast.consume(), format).await.unwrap(); + assert!(track.subscription().is_some(), "{format:?}: subscribed"); + track.finish().unwrap(); + assert!(consumer.next().await.unwrap().is_none()); + assert!( + track.subscription().is_none(), + "{format:?}: EOF must release its subscriber" + ); + assert_eq!(consumer.format(), format); + assert!(consumer.next().await.unwrap().is_none()); } } } diff --git a/rs/moq-mux/src/catalog/hang/consumer.rs b/rs/moq-mux/src/catalog/hang/consumer.rs index 9a8ed649fbc..0c7465b79e5 100644 --- a/rs/moq-mux/src/catalog/hang/consumer.rs +++ b/rs/moq-mux/src/catalog/hang/consumer.rs @@ -4,6 +4,7 @@ use moq_net::PathOwned; use super::{Catalog, CatalogExt}; use crate::Result; +use crate::catalog::CatalogFormat; /// A catalog consumer, used to receive catalog updates and discover tracks. /// @@ -18,6 +19,7 @@ use crate::Result; /// A catalog that misuses that reference is rejected outright (see [`Self::poll_next`]). pub struct Consumer { inner: moq_json::snapshot::Consumer>, + format: CatalogFormat, /// Path of the broadcast serving this catalog, which every rendition's `broadcast` /// reference resolves against. base: PathOwned, @@ -27,6 +29,7 @@ impl Consumer { /// Create a new catalog consumer from a MoQ track subscriber (uncompressed `catalog.json`). pub fn new(track: moq_net::track::Subscriber) -> Self { Self { + format: CatalogFormat::Hang, base: track.broadcast().path.clone(), inner: moq_json::snapshot::Consumer::new(track, moq_json::snapshot::consumer::Config::default()), } @@ -39,11 +42,16 @@ impl Consumer { let mut config = moq_json::snapshot::consumer::Config::default(); config.compression = moq_json::Compression::Deflate; Self { + format: CatalogFormat::HangZ, base: track.broadcast().path.clone(), inner: moq_json::snapshot::Consumer::new(track, config), } } + pub(crate) fn format(&self) -> CatalogFormat { + self.format + } + /// Poll for the next catalog update. /// /// Fails with [`Error::EscapingBroadcast`](crate::Error::EscapingBroadcast) if any rendition's diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 04f29eed5e8..412536e27a5 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -40,8 +40,9 @@ use mpeg2ts::ts::{ use moq_net::Timestamp; +use super::catalog::Catalog as _; +use crate::catalog::Stream; use crate::catalog::hang::Catalog; -use crate::catalog::{CatalogFormat, Stream}; use crate::codec::video::{Hrd, Reorder}; use crate::codec::{aac, annexb, opus}; use crate::container::decode::{DecodeClock, MAX_REORDER}; @@ -99,10 +100,14 @@ fn sanitize_mux_rate(rate: u64) -> Option { /// leading PAT/PMT rides on the first frame (so it inherits a real timestamp), and /// is re-emitted at video keyframes and periodically for mid-stream tune-in. /// Returns `None` when the broadcast ends. -pub struct Export { +pub struct Export +where + S::Ext: catalog::Catalog, +{ source: crate::Source, - catalog: Option>, - catalog_format: CatalogFormat, + catalog: S, + /// 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, /// The publisher epoch of the broadcast being exported, `None` for an epochless route. @@ -880,53 +885,21 @@ impl SiTrack { } } -impl Export { - /// Subscribe to `source`, using the default catalog format. - pub async fn new(source: crate::Source) -> Result { - Self::with_catalog_format(source, CatalogFormat::default()).await - } - - /// Subscribe to `source`, selecting an explicit catalog format. Media only; - /// any catalog extension (e.g. the `mpegts` verbatim streams) is ignored. - pub async fn with_catalog_format( - source: crate::Source, - catalog_format: CatalogFormat, - ) -> Result { - Self::build(source, catalog_format).await - } -} - -impl Export { - /// Subscribe to `source`, exporting its `mpegts` verbatim streams (SCTE-35, - /// private data, ...) back to MPEG-TS alongside the media. The `Self` type pins - /// the extension, so callers write `Export::with_ts(..)` with no turbofish (the - /// plain constructors are media-only). - pub async fn with_ts(source: crate::Source, catalog_format: CatalogFormat) -> Result { - Self::build(source, catalog_format).await - } -} - -impl Export { - /// Shared constructor. The public entry points each live on a concrete - /// `Export` impl that pins `E`, so the extension is chosen by which one you call. - async fn build(source: crate::Source, catalog_format: CatalogFormat) -> Result { - let broadcast = source.broadcast(); - let catalog = crate::catalog::Consumer::new(&broadcast, catalog_format).await?; - Ok(Self::build_with(source, &broadcast, catalog_format, catalog)) - } - - /// Export `broadcast`, the one `source` holds at its path, from its subscribed `catalog`. - fn build_with( - source: crate::Source, - broadcast: &moq_net::broadcast::Consumer, - catalog_format: CatalogFormat, - catalog: crate::catalog::Consumer, - ) -> Self { - let instance = broadcast.info().epoch.clone(); +impl Export +where + S::Ext: catalog::Catalog, +{ + /// Export `source` as MPEG-TS, driving track subscriptions from `catalog`. + /// + /// The caller chooses the catalog format and rendition selection on any [`Stream`]. + /// The stream remains owned until the export is dropped or follows another broadcast; + /// concrete catalog consumers release their subscription and decoder when they end. + pub fn new(source: crate::Source, catalog: S) -> Self { + let instance = source.broadcast().info().epoch.clone(); Self { source, - catalog: Some(catalog), - catalog_format, + catalog, + catalog_ended: false, cataloged: false, instance, stale: HashSet::new(), @@ -1034,14 +1007,14 @@ impl Export { pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { // 1. Drain catalog updates, discovering the track layout. - while let Some(catalog) = self.catalog.as_mut() { - match catalog.poll_next(waiter)? { + while !self.catalog_ended { + match self.catalog.poll_next(waiter)? { Poll::Ready(Some(snapshot)) => { self.cataloged = true; self.update_catalog(snapshot)? } Poll::Ready(None) => { - self.catalog = None; + self.catalog_ended = true; break; } Poll::Pending => break, @@ -1073,7 +1046,7 @@ impl Export { if self.psi.is_none() { if self.tracks.is_empty() { // No tracks yet. If the catalog is also done, the broadcast is empty. - if self.catalog.is_none() { + if self.catalog_ended { return Poll::Ready(Ok(None)); } return Poll::Pending; @@ -1084,7 +1057,7 @@ impl Export { // 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.catalog.is_none() && self.tracks.values().all(|t| t.finished) { + if self.catalog_ended && self.tracks.values().all(|t| t.finished) { return Poll::Ready(Ok(None)); } return Poll::Pending; @@ -1213,7 +1186,7 @@ impl Export { } // SI emission rides media frames, so a snapshot that arrived behind the // last one gets one trailing flush before the stream ends. - if self.catalog.is_none() && !self.si_flushed { + if self.catalog_ended && !self.si_flushed { self.si_flushed = true; if let Some(out) = self.write_si_tail()? { return Poll::Ready(Ok(Some(out))); @@ -1222,7 +1195,7 @@ impl Export { } // End of stream once the catalog is closed too: nothing more can appear. - if self.catalog.is_none() && (drained || self.tracks.is_empty()) { + if self.catalog_ended && (drained || self.tracks.is_empty()) { return Poll::Ready(Ok(None)); } @@ -1295,7 +1268,7 @@ impl Export { Ok(()) } - fn update_catalog(&mut self, mut catalog: Catalog) -> anyhow::Result<()> { + fn update_catalog(&mut self, mut catalog: Catalog) -> anyhow::Result<()> { self.source.retain_selectable(&mut catalog); // The MPEG-TS section lives in the extension. The trait only exposes @@ -1515,7 +1488,7 @@ impl Export { /// finished, 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<()> { + fn resubscribe(&mut self, catalog: &Catalog, mpegts: &catalog::Mpegts) -> anyhow::Result<()> { let budget = self.budget(); for (name, track) in self.tracks.iter_mut() { if !self.stale.contains(name) { @@ -1614,7 +1587,7 @@ impl Export { self.instance.as_ref() } - /// Carry on into `broadcast`, the one now at this export's path. + /// Carry on into `broadcast`, using the caller's replacement `catalog` stream. /// /// The same publisher instance (an equal epoch) continues the stream: the program and /// its PSI stay as announced, and its catalog resubscribes each track as it lists it, so a @@ -1626,16 +1599,11 @@ impl Export { /// flags the break with `discontinuity_indicator`, and each stream starts on a keyframe. /// The caller decides whether to follow a replacement at all; [`Follower`](super::Follower) /// decides from the path's announcements. - pub async fn follow(mut self, broadcast: moq_net::broadcast::Consumer) -> Result { - let catalog = crate::catalog::Consumer::new(&broadcast, self.catalog_format).await?; + pub fn follow(mut self, broadcast: moq_net::broadcast::Consumer, catalog: S) -> Result { self.followed(&broadcast, catalog)?; Ok(self) } - pub(super) fn catalog_format(&self) -> CatalogFormat { - self.catalog_format - } - /// Whether the catalog this export last subscribed has delivered a snapshot. pub(super) fn cataloged(&self) -> bool { self.cataloged @@ -1645,7 +1613,7 @@ impl Export { pub(super) fn followed( &mut self, broadcast: &moq_net::broadcast::Consumer, - catalog: crate::catalog::Consumer, + catalog: S, ) -> Result<(), crate::Error> { let same = self.instance.is_some() && broadcast.info().epoch == self.instance; // The same instance keeps its siblings, but the old broadcast's routes are gone. Another @@ -1655,7 +1623,7 @@ impl Export { false => self.source = self.source.switched(broadcast), } if !same || self.psi.is_none() { - let mut next = Self::build_with(self.source.clone(), broadcast, self.catalog_format, catalog); + let mut next = Self::new(self.source.clone(), catalog); next.replay = self.replay; next = next.with_delay(self.delay); if let Some(rate) = self.mux_rate_override { @@ -1681,7 +1649,8 @@ impl Export { return Ok(()); } - self.catalog = Some(catalog); + self.catalog = catalog; + self.catalog_ended = false; self.cataloged = false; self.si_flushed = false; // A name no entry carries, so the returned catalog repoints every SI entry. @@ -2504,6 +2473,12 @@ impl Export { } } +impl Export> { + pub(super) fn catalog_format(&self) -> crate::catalog::CatalogFormat { + self.catalog.format() + } +} + /// One adaptation-field-only TS packet carrying `ticks` (continuous 90 kHz) as its /// PCR, laid out by hand: the `mpeg2ts` serializer writes the six reserved bits /// between PCR base and extension as zeros where ISO 13818-1 requires ones, and diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index bbd7ac92732..0952f3709cb 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -21,6 +21,21 @@ use crate::container::ts::{Export, catalog as tscat, stats}; use crate::container::{Frame, Kind, Producer}; use moq_net::Timestamp; +/// Open a media-only export on the default catalog stream. +pub(super) async fn open_export(source: crate::Source) -> crate::Result> { + let catalog = source.catalog::<()>(Default::default()).await?; + Ok(Export::new(source, catalog)) +} + +/// Open an export with the MPEG-TS catalog extension and explicit encoding. +pub(super) async fn open_ts( + source: crate::Source, + format: crate::catalog::CatalogFormat, +) -> crate::Result>> { + let catalog = source.catalog::(format).await?; + Ok(Export::new(source, catalog)) +} + /// A millisecond track: the frames written here carry timestamps. fn timed() -> moq_net::track::Info { moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI) @@ -82,11 +97,14 @@ const DRAIN: std::time::Duration = RECORDING_MAX_AGE .saturating_add(std::time::Duration::from_secs(1)); async fn drain(consumer: moq_net::broadcast::Consumer) -> BytesMut { - drain_with(Export::new(crate::source::announced(&consumer).await).await.unwrap()).await + drain_with(open_export(crate::source::announced(&consumer).await).await.unwrap()).await } /// `drain` for an exporter built with an explicit catalog extension. -async fn drain_with(exporter: Export) -> BytesMut { +async fn drain_with(exporter: Export) -> BytesMut +where + S::Ext: tscat::Catalog, +{ let mut exporter = exporter.with_delay(RECORDING_MAX_AGE).with_replay(); let mut out = BytesMut::new(); // `while let Ok` stops on the first timeout (`Pending`: no more output). @@ -113,6 +131,151 @@ fn assert_packet_aligned(ts: &[u8]) { ); } +/// A custom stream need not carry a physical catalog track, and may be non-fused. +#[tokio::test(start_paused = true)] +async fn custom_selected_catalog_drives_export_and_follow() { + use crate::catalog::{Stream, hang::Catalog}; + use std::task::Poll; + + struct Once { + snapshot: Option, + ended: bool, + } + impl Stream for Once { + type Ext = (); + + fn poll_next(&mut self, _: &kio::Waiter) -> Poll>> { + assert!(!self.ended, "an ended catalog must not be polled again"); + let snapshot = self.snapshot.take(); + self.ended = snapshot.is_none(); + Poll::Ready(Ok(snapshot)) + } + } + + let origin = crate::source::produce_origin(); + let mut export: Option>> = None; + for payload in [b"first".as_slice(), b"next".as_slice()] { + let broadcast = origin.create_broadcast("live").unwrap(); + broadcast + .announce(moq_net::origin::Route::default().with_epoch(moq_net::Epoch::mint())) + .unwrap(); + let mut snapshot = Catalog::default(); + let mut producers = Vec::new(); + for (name, payload) in [("keep", payload), ("drop", b"unselected".as_slice())] { + let track = broadcast + .create_track(name, hang::container::track_info(hang::catalog::PRIORITY.audio)) + .unwrap(); + let mut config = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + config.container = Container::Legacy; + snapshot.audio.renditions.insert(name.into(), config); + let mut producer = Producer::new(track, HangContainer::Legacy(Kind::Data)); + producer + .write(Frame { + timestamp: Timestamp::ZERO, + duration: None, + payload: Bytes::copy_from_slice(payload), + keyframe: true, + }) + .unwrap(); + producer.finish().unwrap(); + producers.push(producer); + } + let stream = Once { + snapshot: Some(snapshot), + ended: false, + } + .select(crate::select::Broadcast::default().audio(crate::select::Audio::default().name("keep"))); + let source = crate::source::resolved(&origin, "live").await; + let mut current = match export.take() { + None => Export::new(source, stream).with_delay(RECORDING_MAX_AGE).with_replay(), + Some(previous) => previous.follow(source.broadcast(), stream).unwrap(), + }; + let (frames, end) = drain_to_end(&mut current).await; + end.unwrap(); + let bytes: Vec = frames.iter().flat_map(|frame| frame.payload.iter().copied()).collect(); + assert_packet_aligned(&bytes); + let mut packets = TsPacketReader::new(Cursor::new(&bytes)); + let mut pmt = false; + while let Some(packet) = packets.read_ts_packet().unwrap() { + if let Some(TsPayload::Pmt(program)) = packet.payload { + pmt = true; + assert_eq!(program.es_info.len(), 1); + } + } + assert!(pmt); + let mut pes = PesPacketReader::new(TsPacketReader::new(Cursor::new(&bytes))); + let packet = pes.read_pes_packet().unwrap().unwrap(); + assert_eq!(&packet.data[7..], payload); + assert!(pes.read_pes_packet().unwrap().is_none()); + assert!(current.next().await.unwrap().is_none()); + export = Some(current); + drop((broadcast, producers)); + } +} + +/// A follower built after EOF keeps the explicit compressed encoding without keeping its reader. +#[tokio::test(start_paused = true)] +async fn a_follower_keeps_hangz_after_catalog_eof() { + use crate::catalog::{CatalogFormat, hang::Catalog}; + use std::task::Poll; + + let origin = crate::source::produce_origin(); + let epoch = moq_net::Epoch::mint(); + let publish = || { + let broadcast = origin.create_broadcast("live").unwrap(); + let track = broadcast + .create_track(hang::Catalog::COMPRESSED_NAME, hang::Catalog::default_track_info()) + .unwrap(); + let mut config = moq_json::snapshot::Config::default(); + config.compression = moq_json::Compression::Deflate; + let mut catalog = moq_json::snapshot::Producer::::new(track.clone(), config); + catalog.update(&Catalog::default()).unwrap(); + broadcast + .announce(moq_net::origin::Route::default().with_epoch(epoch.clone())) + .unwrap(); + (broadcast, track, catalog) + }; + let (broadcast, mut track, mut catalog) = publish(); + catalog.finish().unwrap(); + let source = crate::source::resolved(&origin, "live").await; + let stream = source.catalog::<()>(CatalogFormat::HangZ).await.unwrap(); + let mut export = Export::new(source, stream); + assert!(export.next().await.unwrap().is_none()); + assert_eq!(export.catalog_format(), CatalogFormat::HangZ); + // The routed subscription is released when the origin driver observes the local drop. + tokio::time::timeout(Duration::from_secs(1), async { + while track.subscription().is_some() { + track.subscription_changed().await.unwrap(); + } + }) + .await + .expect("EOF must release the first compressed reader"); + drop((broadcast, track, catalog)); + let mut follower = super::Follower::new(export) + .unwrap() + .with_linger(Duration::from_secs(1)); + let (broadcast, track, mut catalog) = publish(); + // Only catalog.json.z exists, so falling back to the default encoding cannot subscribe. + tokio::time::timeout( + Duration::from_secs(1), + kio::wait(|waiter| { + if let Poll::Ready(Err(err)) = follower.poll_next(waiter) { + panic!("compressed return failed: {err:?}"); + } + if track.subscription().is_some() { + Poll::Ready(()) + } else { + Poll::Pending + } + }), + ) + .await + .expect("compressed catalog resubscribed"); + assert_eq!(follower.export().catalog_format(), CatalogFormat::HangZ); + catalog.finish().unwrap(); + drop((follower, broadcast, track, catalog)); +} + #[tokio::test(start_paused = true)] async fn export_aac_roundtrip() { let mut broadcast = moq_net::broadcast::Info::new().produce(); @@ -434,7 +597,7 @@ async fn export_lead_audio() -> BytesMut { video.finish().unwrap(); audio.finish().unwrap(); - let exporter = Export::new(crate::source::announced(&consumer).await).await.unwrap(); + let exporter = open_export(crate::source::announced(&consumer).await).await.unwrap(); // The producers stay alive through the drain so the retained tracks are readable. drain_with(exporter).await } @@ -984,7 +1147,7 @@ async fn export_pcr_wraps_below_zero_at_start() { } producer.finish().unwrap(); - let mut exporter = Export::new(crate::source::announced(&consumer).await) + let mut exporter = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(RECORDING_MAX_AGE); @@ -1422,7 +1585,7 @@ async fn exported_ac3_pes(input: Vec) -> Vec<(u64, Vec)> { import.decode(&BytesMut::from(&data[..])).unwrap(); import.finish().unwrap(); let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -1536,7 +1699,7 @@ async fn export_scte35_roundtrip() { // `import`, `catalog`, and `scte_producer` stay alive: retained tracks. The // exporter must carry the extension to see the mpegts section. let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -1674,7 +1837,7 @@ async fn export_pes_verbatim_roundtrip() { // `import`, `catalog`, and `data_producer` stay alive: retained tracks. let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -1766,7 +1929,7 @@ async fn scte35_without_video_export_is_rejected() { producer.cut(None).unwrap(); producer.finish().unwrap(); - let mut exporter = Export::with_ts( + let mut exporter = open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -2049,7 +2212,7 @@ async fn aac_program_config_follows_each_table() { .renditions .insert("a.aac".to_string(), cfg); let mut producer = Producer::new(track, HangContainer::Legacy(Kind::Audio)); - let mut export = Export::new(crate::source::announced(&consumer).await) + let mut export = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -2270,7 +2433,7 @@ async fn aac_export_refuses_what_adts_cannot_label() { .unwrap(); producer.finish().unwrap(); - let mut exporter = Export::new(crate::source::announced(&consumer).await).await.unwrap(); + let mut exporter = open_export(crate::source::announced(&consumer).await).await.unwrap(); let err = loop { match tokio::time::timeout(DRAIN, exporter.next()).await { Ok(Ok(Some(_))) => continue, @@ -2569,7 +2732,7 @@ async fn scte35_fixtures_survive_roundtrip() { // Export and re-ingest. let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -2818,7 +2981,7 @@ async fn service_layer_survives_roundtrip() { // `import` and `catalog` stay alive: retained tracks the exporter subscribes to. let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -2973,7 +3136,7 @@ async fn si_pids_are_re_emitted_on_their_own_interval() { producer.finish().unwrap(); let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -3062,7 +3225,7 @@ async fn inline_si_form_is_re_emitted() { producer.finish().unwrap(); let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -3154,7 +3317,7 @@ async fn export_rejects_si_pid_on_the_pmt() { } let _producer = Producer::new(track, HangContainer::Legacy(crate::container::Kind::Data)); - let mut exporter = Export::with_ts( + let mut exporter = open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -3206,7 +3369,7 @@ async fn export_rejects_si_pid_on_an_elementary_stream() { } let _producer = Producer::new(track, HangContainer::Legacy(crate::container::Kind::Data)); - let mut exporter = Export::with_ts( + let mut exporter = open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -3224,8 +3387,8 @@ async fn export_rejects_si_pid_on_an_elementary_stream() { } /// A `mpegts`-extension exporter over an announced broadcast. -async fn export_of(consumer: &moq_net::broadcast::Consumer) -> Export { - Export::with_ts( +async fn export_of(consumer: &moq_net::broadcast::Consumer) -> Export> { + open_ts( crate::source::announced(consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -4103,7 +4266,7 @@ async fn a_selected_programs_si_survives_export() { rig.import.finish().unwrap(); let ts = drain_with( - Export::with_ts( + open_ts( crate::source::announced(&rig.consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -4330,7 +4493,7 @@ async fn stale_si_entry_does_not_block_output() { producer.cut(None).unwrap(); producer.finish().unwrap(); - let mut exporter = Export::with_ts( + let mut exporter = open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -4582,7 +4745,7 @@ async fn export_opus_track(channel_count: u32, description: Option) -> Re .unwrap(); producer.finish().unwrap(); - let mut exporter = Export::new(crate::source::announced(&consumer).await) + let mut exporter = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -4789,7 +4952,7 @@ async fn export_twice(with_video: bool) -> (Vec, Vec) { }; let source = crate::source::announced(&consumer).await; - let export = || Export::with_ts(source.clone(), crate::catalog::CatalogFormat::Hang); + let export = || open_ts(source.clone(), crate::catalog::CatalogFormat::Hang); let mut a = export().await.unwrap(); let mut b = None; let (mut out_a, mut out_b) = (Vec::new(), Vec::new()); @@ -4839,7 +5002,10 @@ async fn export_twice(with_video: bool) -> (Vec, Vec) { /// Pull every frame an exporter can render right now, like `drain` but keeping the frames /// whole (this compares them one by one, not as one byte stream). -async fn drain_frames(export: &mut Export) -> Vec { +async fn drain_frames(export: &mut Export) -> Vec +where + S::Ext: tscat::Catalog, +{ let mut out = Vec::new(); loop { out.extend(poll_frames(export)); @@ -5004,8 +5170,8 @@ impl Interleave { } /// An exporter with its subscriptions resolved, so later polls need no runtime. - async fn export(&self, delay: Duration) -> Export { - let mut export = Export::new(self.source.clone()).await.unwrap().with_delay(delay); + async fn export(&self, delay: Duration) -> Export { + let mut export = open_export(self.source.clone()).await.unwrap().with_delay(delay); assert!(drain_frames(&mut export).await.is_empty()); export } @@ -5035,7 +5201,7 @@ impl Interleave { /// Write every audio frame that starts before `until` microseconds, one at a time, /// polling `export` after each. - fn audio_until(&mut self, until: u64, export: &mut Export, out: &mut Vec) { + fn audio_until(&mut self, until: u64, export: &mut Export, out: &mut Vec) { while self.audio_index * AUDIO_US < until { let index = self.audio_index; self.audio @@ -5058,7 +5224,10 @@ impl Interleave { } /// Pull every frame an exporter can render without letting time pass. -fn poll_frames(export: &mut Export) -> Vec { +fn poll_frames(export: &mut Export) -> Vec +where + S::Ext: tscat::Catalog, +{ let waiter = kio::Waiter::noop(); let mut out = Vec::new(); while let std::task::Poll::Ready(frame) = export.poll_next(&waiter) { @@ -5402,7 +5571,7 @@ async fn repointed_si_entry_resubscribes() { // Writes both GOPs up front and only then reads, so it needs a replay window: // the real-time default would take the live edge and skip the first. - let mut exporter = Export::with_ts( + let mut exporter = open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -5531,7 +5700,7 @@ async fn si_revision_after_final_media_frame_is_flushed() { producer.cut(None).unwrap(); producer.finish().unwrap(); - let mut exporter = Export::with_ts( + let mut exporter = open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -5581,7 +5750,7 @@ struct SiCadenceRig { started: bool, producer: Producer, si_track: moq_net::track::Producer, - exporter: Export, + exporter: Export>, catalog: crate::catalog::Producer, } @@ -5627,7 +5796,7 @@ async fn si_cadence_rig(pid: u16, table_id: u8, interval: Duration) -> SiCadence } let producer = Producer::new(track, HangContainer::Legacy(crate::container::Kind::Data)); - let exporter = Export::with_ts( + let exporter = open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -6074,7 +6243,7 @@ async fn export_cbr_video() -> Vec { } video.finish().unwrap(); - let mut exporter = Export::new(crate::source::announced(&consumer).await) + let mut exporter = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_mux_rate(20_000_000) @@ -6140,7 +6309,7 @@ async fn a_frame_on_a_slot_boundary_finishes_before_it_decodes() { } video.finish().unwrap(); - let mut exporter = Export::new(crate::source::announced(&consumer).await).await.unwrap(); + let mut exporter = open_export(crate::source::announced(&consumer).await).await.unwrap(); let frames = drain_frames(&mut exporter).await; let ts: Vec = frames.iter().flat_map(|f| f.payload.iter().copied()).collect(); let packets: Vec<&[u8]> = ts.chunks(188).collect(); @@ -6260,7 +6429,7 @@ async fn slices_go_out_on_the_clock() { let consumer = broadcast.consume(); let mut catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); let mut audio = aac_rendition(&mut broadcast, &mut catalog, "a.aac"); - let mut export = Export::new(crate::source::announced(&consumer).await) + let mut export = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(Duration::from_millis(1_500)); @@ -6305,7 +6474,7 @@ async fn pcr_stays_among_the_bytes_across_reordered_tracks() { import.decode(&BytesMut::from(&data[..])).unwrap(); import.finish().unwrap(); - let mut exporter = Export::new(crate::source::announced(&consumer).await) + let mut exporter = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_mux_rate(20_000_000) @@ -6425,7 +6594,7 @@ async fn a_heavy_passage_fills_the_slots_before_it() { } producer.finish().unwrap(); - let mut export = Export::new(crate::source::announced(&consumer).await) + let mut export = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_mux_rate(2_000_000) @@ -6669,7 +6838,7 @@ async fn export_pcr_is_its_byte_position_at_the_rate() { } producer.finish().unwrap(); - let export = Export::new(crate::source::announced(&consumer).await) + let export = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_mux_rate(1_000_000); @@ -6779,8 +6948,8 @@ impl Reordered { } /// An exporter with its subscriptions resolved, so later polls need no runtime. - async fn export(&self, max_age: Duration) -> Export { - let mut export = Export::new(self.source.clone()).await.unwrap().with_delay(max_age); + async fn export(&self, max_age: Duration) -> Export { + let mut export = open_export(self.source.clone()).await.unwrap().with_delay(max_age); assert!(drain_frames(&mut export).await.is_empty()); export } @@ -6847,7 +7016,7 @@ async fn export_reordered( ) -> (Vec, Vec) { let delay = Duration::from_millis(500); let mut rig = Reordered::new(sps, pps, pyramid).await; - let padded = |export: Export| match rate { + let padded = |export: Export| match rate { Some(rate) => export.with_mux_rate(rate), None => export, }; @@ -6870,7 +7039,7 @@ async fn export_reordered( out_b.extend(poll_frames(b)); } if tick + 1 == JOIN { - b = Some(padded(Export::new(rig.source.clone()).await.unwrap().with_delay(delay))); + b = Some(padded(open_export(rig.source.clone()).await.unwrap().with_delay(delay))); } } rig.finish(); @@ -7126,7 +7295,7 @@ async fn a_skipped_group_keeps_the_clock() { } let delay = Duration::from_millis(500); - let mut export = Export::new(crate::source::announced(&consumer).await) + let mut export = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(delay); @@ -7189,7 +7358,7 @@ async fn a_timeline_restarting_at_zero_fails_the_export() { .insert("a.aac".to_string(), cfg); } - let mut export = Export::new(crate::source::announced(&consumer).await) + let mut export = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(Duration::from_millis(500)); @@ -7249,7 +7418,7 @@ async fn a_late_join_starts_at_the_live_edge() { }) .unwrap(); if k == 275 { - let joined = Export::new(crate::source::announced(&consumer).await).await.unwrap(); + let joined = open_export(crate::source::announced(&consumer).await).await.unwrap(); export = Some(joined.with_delay(Duration::from_secs(2))); } if let Some(export) = export.as_mut() { @@ -7312,7 +7481,10 @@ fn pes_count(frames: &[Frame]) -> usize { } /// Pull frames until the export ends, returning them and how it ended. -async fn drain_to_end(export: &mut Export) -> (Vec, crate::Result<()>) { +async fn drain_to_end(export: &mut Export) -> (Vec, crate::Result<()>) +where + S::Ext: tscat::Catalog, +{ let mut out = Vec::new(); loop { match tokio::time::timeout(DRAIN, export.next()) @@ -7339,7 +7511,7 @@ async fn a_track_leaving_the_catalog_is_read_to_its_end() { let mut kept = aac_rendition(&mut broadcast, &mut catalog, "a.aac"); let mut leaving = aac_rendition(&mut broadcast, &mut catalog, "b.aac"); - let mut export = Export::new(crate::source::announced(&consumer).await) + let mut export = open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -7473,7 +7645,7 @@ async fn same_instance_returns(finish: bool) { let (mut broadcast, mut catalog) = publish_live::<()>(&origin, route()); let mut track = aac_rendition(&mut broadcast, &mut catalog, "a.aac"); - let mut export = Export::new(crate::source::resolved(&origin, "live").await) + let mut export = open_export(crate::source::resolved(&origin, "live").await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -7498,7 +7670,8 @@ async fn same_instance_returns(finish: bool) { let (mut broadcast, mut catalog) = publish_live::<()>(&origin, route()); std::ops::DerefMut::deref_mut(&mut catalog.modify().unwrap()); let back = origin.consume().routed_broadcast("live").await.unwrap(); - let mut export = export.follow(back).await.unwrap(); + let stream = crate::catalog::Consumer::new(&back, Default::default()).await.unwrap(); + let mut export = export.follow(back, stream).unwrap(); frames.extend(drain_frames(&mut export).await); let mut track = aac_rendition(&mut broadcast, &mut catalog, "a.aac"); let resumed = start.elapsed().as_millis() as u64; @@ -7551,7 +7724,7 @@ async fn a_return_keeps_catalog_and_group_history() { let clock = catalog.snapshot().clock; let source = crate::source::resolved(&origin, "live").await; let old = source.broadcast(); - let mut export = Export::new(source) + let mut export = open_export(source) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -7572,7 +7745,8 @@ async fn a_return_keeps_catalog_and_group_history() { let back = origin.consume().routed_broadcast("live").await.unwrap(); assert_eq!(back.info().epoch.as_ref(), Some(&epoch)); assert!(!back.is_clone(&old), "the withdrawn front is replaced"); - let mut export = export.follow(back).await.unwrap(); + let stream = crate::catalog::Consumer::new(&back, Default::default()).await.unwrap(); + let mut export = export.follow(back, stream).unwrap(); let resumed = start.elapsed().as_millis() as u64; for (sequence, ms) in (resumed..resumed + 200).step_by(20).enumerate() { write_aac(&mut track, ms); @@ -7603,7 +7777,7 @@ async fn a_switch_carries_through_an_instance_that_never_built_its_tables() { let (mut old, mut old_catalog) = publish_live::<()>(&origin, route()); let mut old_audio = aac_rendition(&mut old, &mut old_catalog, "a.aac"); - let mut export = Export::new(crate::source::resolved(&origin, "live").await) + let mut export = open_export(crate::source::resolved(&origin, "live").await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -7617,7 +7791,8 @@ async fn a_switch_carries_through_an_instance_that_never_built_its_tables() { // Switched to an instance whose catalog lists nothing, so it never builds its tables... let (_bare, _bare_catalog) = publish_live::<()>(&origin, route()); let bare = origin.consume().request_broadcast("live", None).await.unwrap(); - let mut export = export.follow(bare).await.unwrap(); + let stream = crate::catalog::Consumer::new(&bare, Default::default()).await.unwrap(); + let mut export = export.follow(bare, stream).unwrap(); assert!( pats(&drain_frames(&mut export).await).is_empty(), "no tables for an empty catalog" @@ -7627,7 +7802,10 @@ async fn a_switch_carries_through_an_instance_that_never_built_its_tables() { let (mut new, mut new_catalog) = publish_live::<()>(&origin, route()); let mut new_audio = aac_rendition(&mut new, &mut new_catalog, "b.aac"); let replacement = origin.consume().request_broadcast("live", None).await.unwrap(); - let mut export = export.follow(replacement).await.unwrap(); + let stream = crate::catalog::Consumer::new(&replacement, Default::default()) + .await + .unwrap(); + let mut export = export.follow(replacement, stream).unwrap(); for ms in (0..200).step_by(20) { write_aac(&mut new_audio, ms); } @@ -7657,7 +7835,7 @@ async fn another_instance_is_a_full_program_switch() { let (mut old, mut old_catalog) = publish_live::<()>(&origin, route()); let mut old_audio = aac_rendition(&mut old, &mut old_catalog, "a.aac"); - let mut export = Export::new(crate::source::resolved(&origin, "live").await) + let mut export = open_export(crate::source::resolved(&origin, "live").await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -7675,7 +7853,10 @@ async fn another_instance_is_a_full_program_switch() { let mut video = h264_rendition(&mut new, &mut new_catalog, "video.avc1"); let mut new_audio = aac_rendition(&mut new, &mut new_catalog, "b.aac"); let replacement = origin.consume().request_broadcast("live", None).await.unwrap(); - let mut export = export.follow(replacement).await.unwrap(); + let stream = crate::catalog::Consumer::new(&replacement, Default::default()) + .await + .unwrap(); + let mut export = export.follow(replacement, stream).unwrap(); // The old instance carries on ten seconds ahead, where none of it may land. for ms in (10_000..10_200).step_by(20) { @@ -7743,7 +7924,7 @@ async fn a_switch_moving_the_pmt_advances_the_pat() { }; let (_old, _old_catalog, mut old_track) = publish(0x100); - let mut export = Export::with_ts( + let mut export = open_ts( crate::source::resolved(&origin, "live").await, crate::catalog::CatalogFormat::Hang, ) @@ -7758,7 +7939,10 @@ async fn a_switch_moving_the_pmt_advances_the_pat() { let (_new, mut new_catalog, mut new_track) = publish(0x200); let replacement = origin.consume().request_broadcast("live", None).await.unwrap(); - let mut export = export.follow(replacement).await.unwrap(); + let stream = crate::catalog::Consumer::new(&replacement, Default::default()) + .await + .unwrap(); + let mut export = export.follow(replacement, stream).unwrap(); for ms in (0..200).step_by(20) { write_aac(&mut new_track, ms); } @@ -7787,7 +7971,7 @@ async fn export_stats_count_only_returned_output() { let (mut broadcast, mut catalog) = publish_live::<()>(&origin, route()); let mut track = aac_rendition(&mut broadcast, &mut catalog, "a.aac"); - let mut export = Export::new(crate::source::resolved(&origin, "live").await) + let mut export = open_export(crate::source::resolved(&origin, "live").await) .await .unwrap() .with_delay(RECORDING_MAX_AGE); @@ -7813,7 +7997,8 @@ async fn export_stats_count_only_returned_output() { let (mut broadcast, mut catalog) = publish_live::<()>(&origin, route()); let mut track = aac_rendition(&mut broadcast, &mut catalog, "a.aac"); let back = origin.consume().routed_broadcast("live").await.unwrap(); - let mut export = export.follow(back).await.unwrap(); + let stream = crate::catalog::Consumer::new(&back, Default::default()).await.unwrap(); + let mut export = export.follow(back, stream).unwrap(); for ms in (1_000..1_200).step_by(20) { write_aac(&mut track, ms); } @@ -7861,7 +8046,7 @@ async fn export_liveness(sample: u64, stop: u64) -> (stats::Export, stats::Expor aac_rendition(&mut broadcast, &mut catalog, "secondary.aac"), ]; - let mut export = Export::new(crate::source::announced(&consumer).await).await.unwrap(); + let mut export = open_export(crate::source::announced(&consumer).await).await.unwrap(); assert!(export.stats().streams.is_empty(), "no row before the program tables"); let (mut sampled, mut frames) = (None, Vec::new()); let mut audio_index = 0; @@ -7975,7 +8160,7 @@ async fn export_stats_count_a_gap_longer_than_the_backfill() { let mut stopped = aac_rendition(&mut broadcast, &mut catalog, "a.aac"); let mut running = aac_rendition(&mut broadcast, &mut catalog, "b.aac"); - let mut export = Export::new(crate::source::announced(&consumer).await).await.unwrap(); + let mut export = open_export(crate::source::announced(&consumer).await).await.unwrap(); for ms in (0..2_000).step_by(20) { write_aac(&mut stopped, ms); write_aac(&mut running, ms); @@ -8059,7 +8244,7 @@ async fn large_pmt(streams: u16, mux_rate: Option) -> usize { producers.push(producer); } catalog.modify().unwrap().ext.mpegts = config; - let mut exporter = Export::with_ts(crate::source::announced(&broadcast.consume()).await, Default::default()) + let mut exporter = open_ts(crate::source::announced(&broadcast.consume()).await, Default::default()) .await .unwrap(); if let Some(mux_rate) = mux_rate { @@ -8135,7 +8320,7 @@ fn publish_bare( /// Follow the broadcast at `live` with a fresh export of it. async fn follower(origin: &moq_net::origin::Producer) -> super::Follower { let source = crate::source::resolved(origin, "live").await; - super::Follower::new(Export::new(source).await.unwrap()).unwrap() + super::Follower::new(open_export(source).await.unwrap()).unwrap() } /// End a catalog-only broadcast cleanly. @@ -8421,7 +8606,7 @@ async fn a_follower_built_after_its_route_went_follows_the_return() { let origin = crate::source::produce_origin(); let epoch = moq_net::Epoch::mint(); let first = publish_bare(&origin, Some(&epoch)); - let export = Export::new(crate::source::resolved(&origin, "live").await) + let export = open_export(crate::source::resolved(&origin, "live").await) .await .unwrap(); let start = tokio::time::Instant::now(); @@ -8472,7 +8657,7 @@ async fn a_follower_takes_its_own_instance_winning_back_for_no_replacement() { } }); let source = crate::source::resolved(&origin, "pool/job").await; - let mut follower = super::Follower::new(Export::new(source).await.unwrap()).unwrap(); + let mut follower = super::Follower::new(open_export(source).await.unwrap()).unwrap(); // A more specific route takes the path for a while, then goes. let exact = origin.publish("pool/job", route(moq_net::Epoch::mint())).unwrap(); @@ -8586,7 +8771,7 @@ async fn a_follower_continues_through_a_same_epoch_handoff_after_a_gap() { let mut catalog = crate::catalog::Producer::<()>::new(&mut exact, Default::default()).unwrap(); let mut track = aac_rendition(&mut exact, &mut catalog, "a.aac"); let source = crate::source::resolved(&origin, "pool/job").await; - let export = Export::new(source) + let export = open_export(source) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -8653,7 +8838,7 @@ async fn a_follower_ignores_a_covering_prefix_while_the_exact_route_serves() { let mut exact = origin.publish("pool/job", route(moq_net::Epoch::mint())).unwrap(); let catalog = crate::catalog::Producer::<()>::new(&mut exact, Default::default()).unwrap(); let source = crate::source::resolved(&origin, "pool/job").await; - let mut follower = super::Follower::new(Export::new(source).await.unwrap()).unwrap(); + let mut follower = super::Follower::new(open_export(source).await.unwrap()).unwrap(); let quiet = Duration::from_secs(1); let pool = origin.dynamic("pool", route(moq_net::Epoch::mint())).unwrap(); @@ -8908,7 +9093,7 @@ fn audio_pts(frames: &[Frame]) -> Vec { async fn a_late_rendition_never_reads_a_replacement() { let origin = crate::source::produce_origin(); let (mut old, mut old_catalog) = publish_live::<()>(&origin, Default::default()); - let mut export = Export::new(crate::source::resolved(&origin, "live").await) + let mut export = open_export(crate::source::resolved(&origin, "live").await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -8966,7 +9151,7 @@ async fn a_repointed_si_entry_never_reads_a_replacement() { .entry(0x0011) .or_default() .insert(0x42, entry("a.si")); - let mut export = Export::with_ts( + let mut export = open_ts( crate::source::resolved(&origin, "live").await, crate::catalog::CatalogFormat::Hang, ) @@ -9016,7 +9201,7 @@ async fn a_repointed_si_entry_never_reads_a_replacement() { async fn a_switch_reads_the_instance_it_followed() { let origin = crate::source::produce_origin(); let (_old, _old_catalog) = publish_live::<()>(&origin, Default::default()); - let export = Export::new(crate::source::resolved(&origin, "live").await) + let export = open_export(crate::source::resolved(&origin, "live").await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) @@ -9036,7 +9221,11 @@ async fn a_switch_reads_the_instance_it_followed() { write_aac(&mut newer_audio, ms); } - let mut export = export.follow(broadcast).await.unwrap(); + let stream = crate::catalog::Consumer::new(&broadcast, Default::default()) + .await + .unwrap(); + + let mut export = export.follow(broadcast, stream).unwrap(); let pts = audio_pts(&drain_frames(&mut export).await); assert_eq!(pts.len(), 10, "the followed instance's audio went out: {pts:?}"); assert!( @@ -9054,7 +9243,7 @@ async fn a_backlog_stays_in_the_source_until_due() { let mut config = AudioConfig::new(AAC { profile: 2 }, 44100, 2); config.description = Some(Bytes::from_static(&[0x12, 0x10])); let mut live = Live::audio(config); - let mut export = Export::new(live.source().await).await.unwrap().with_delay(delay); + let mut export = open_export(live.source().await).await.unwrap().with_delay(delay); let start = tokio::time::Instant::now(); live.track.write(raw_frame(0, b"first", true)).unwrap(); // A zero-delay slot needs a later frame to prove it is complete. diff --git a/rs/moq-mux/src/container/ts/export_timing_test.rs b/rs/moq-mux/src/container/ts/export_timing_test.rs index 33071b3cbae..0b9ad6f115f 100644 --- a/rs/moq-mux/src/container/ts/export_timing_test.rs +++ b/rs/moq-mux/src/container/ts/export_timing_test.rs @@ -183,7 +183,7 @@ impl Live { /// A receiver joining now. async fn join(&self, every: Duration) -> Leg { - let export = Export::new(self.source.clone()) + let export = super::export_test::open_export(self.source.clone()) .await .unwrap() .with_delay(DELAY) @@ -295,7 +295,7 @@ impl Live { /// One receiver: an exporter and what it rendered, with the instant each slot went out. struct Leg { - export: Export, + export: Export, /// How often it reads its subscription. Zero reads after every frame the source sends; a /// coarser cadence stands for a receiver behind a burstier network or scheduler. every: Duration, diff --git a/rs/moq-mux/src/container/ts/follower.rs b/rs/moq-mux/src/container/ts/follower.rs index 21a2592a30c..e7df986ecb3 100644 --- a/rs/moq-mux/src/container/ts/follower.rs +++ b/rs/moq-mux/src/container/ts/follower.rs @@ -85,7 +85,7 @@ enum State { /// /// An epochless route has no instance to match, so any return of one is a replacement. pub struct Follower { - export: Export, + export: Export>, state: State, announced: moq_net::announce::Follow, path: moq_net::PathOwned, @@ -107,7 +107,7 @@ impl Follower { /// Follow the path `export` reads, from the broadcast it resolved. /// /// Fails when the export's origin can never cover its path. - pub fn new(export: Export) -> crate::Result { + pub fn new(export: Export>) -> crate::Result { let source = export.source(); let announced = source.origin().follow(source.path())?; let path = source.path().clone(); @@ -157,7 +157,7 @@ impl Follower { } /// The export being carried on. - pub fn export(&self) -> &Export { + pub fn export(&self) -> &Export> { &self.export } diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index daad1e569c6..8f8267c6b68 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -4162,7 +4162,7 @@ pub(super) mod test { import.decode(&bytes).unwrap(); import.finish().unwrap(); - let exporter = crate::container::ts::Export::with_ts( + let exporter = crate::container::ts::export_test::open_ts( crate::source::announced(&consumer).await, crate::catalog::CatalogFormat::Hang, ) @@ -7264,7 +7264,7 @@ pub(super) mod test { "the first group ends at the break, one frame after its last picture" ); - let mut exporter = super::super::Export::new(crate::source::announced(&consumer).await) + let mut exporter = crate::container::ts::export_test::open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(Duration::from_secs(30)) @@ -7881,7 +7881,7 @@ pub(super) mod test { // and the boundary with them. The paused clock runs out the delay and every gap in // the media without a real wait. let delay = std::time::Duration::from_secs(3600); - let mut exporter = crate::container::ts::Export::new(crate::source::announced(&consumer).await) + let mut exporter = crate::container::ts::export_test::open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(delay) diff --git a/rs/moq-mux/src/container/ts/import_test.rs b/rs/moq-mux/src/container/ts/import_test.rs index 5a31923c29b..3b804dccf95 100644 --- a/rs/moq-mux/src/container/ts/import_test.rs +++ b/rs/moq-mux/src/container/ts/import_test.rs @@ -474,7 +474,7 @@ async fn import_export_import_roundtrip() { // Re-export to TS. `import` and `catalog` stay alive so the exporter can // subscribe to the finished, retained tracks. - let mut exporter = crate::container::ts::Export::new(crate::source::announced(&consumer).await) + let mut exporter = crate::container::ts::export_test::open_export(crate::source::announced(&consumer).await) .await .unwrap() .with_delay(RECORDING_MAX_AGE) diff --git a/rs/moq-srt/src/ts.rs b/rs/moq-srt/src/ts.rs index c1f38d69d29..e4f08162eb0 100644 --- a/rs/moq-srt/src/ts.rs +++ b/rs/moq-srt/src/ts.rs @@ -208,9 +208,8 @@ impl Subscriber { // broadcast (the catalog `broadcast` field) through the same origin. let broadcast = origin.request_broadcast(path, None).await?; let source = moq_mux::Source::new(origin.clone(), broadcast); - let export = ts::Export::with_ts(source, moq_mux::catalog::CatalogFormat::Hang) - .await? - .with_delay(options.latency); + let catalog = source.catalog::(moq_mux::catalog::CatalogFormat::Hang).await?; + let export = ts::Export::new(source, catalog).with_delay(options.latency); let follower = ts::Follower::new(export)? .with_linger(options.linger) .with_stitch(options.stitch);