Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drafts/draft-lcurley-moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ The second case is easy to get wrong, so to be explicit: a subscriber that reque
The frame offset belonged to group 5 and is gone along with the rest of it; it does not carry forward to whichever group the publisher resolved to.

**Largest Group** and **Largest Frame**:
The largest position the publisher has for the track when it answers: the last frame that exists, like moq-transport's Largest Location.
The largest position the publisher knows of for the track when it answers: the last frame that exists, like moq-transport's Largest Location, including one a relay's source reported that has yet to arrive.
`Largest Group` is the group sequence plus one, and 0 means the track has nothing yet, in which case `Largest Frame` is absent.
A subscriber takes it as where the live feed is: a relay holding groups from an earlier subscription serves them from cache only once this says they are current, which a publisher answering promptly lets it do without waiting for a frame.

Expand Down
6 changes: 6 additions & 0 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ export const hooks: {
// pending: /quest/m1/public-model/serve.md
/** The newest frame buffered for a subscriber, which wire answers name; assigned by `track.ts`. */
bufferedLargest: (subscriber: Subscriber) => Location | undefined;
// private: capability gap
/** The track's largest known now, past what is buffered once the source reported newer; assigned by `track.ts`. */
peekLargest: (subscriber: Subscriber) => Location | undefined;
/** Call `fn` with a group's frame count now and on every change; assigned by `group.ts`. */
groupProgress: (group: GroupProducer, fn: (total: number) => void) => Dispose;
// pending: /quest/m1/public-model/ingest.md
Expand Down Expand Up @@ -387,6 +390,9 @@ export const hooks: {
bufferedLargest: () => {
throw new Error("track.ts not loaded");
},
peekLargest: () => {
throw new Error("track.ts not loaded");
},
groupProgress: () => {
throw new Error("group.ts not loaded");
},
Expand Down
38 changes: 38 additions & 0 deletions js/net/src/lite/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2245,6 +2245,44 @@ test.each([0, 1])("lite draft-07 reports the cached largest position when starti
}
});

// A relay that holds only the first group of a backlog answers with the largest its source
// reported, so a live joiner downstream starts at the newest group rather than the oldest one
// the budget serves.
test("lite draft-07 reports the source's largest position past what a relay holds", async () => {
const version = Version.DRAFT_07;
const pair = createMockTransportPair(ALPN_07_WIP);
const origin = new OriginProducer();
const publisher = new Publisher(pair.server, version, randomHop(), origin.consume());
const broadcast = publish(origin, Path.from("relayed"));
const track = broadcast.createTrack("video", { timescale: Timescale.MILLI });
// The source answered with group 4 as its largest; only group 0 has landed.
hooks.reportedLargest(track, { group: 4, frame: 0 });
const group = new GroupProducer(0);
group.writeFrame(textFrame("oldest"));
group.close();
track.writeGroup(group);
const client = await Stream.open(pair.client, { version });
const server = await Stream.accept(pair.server, version);
if (!server) throw new Error("missing subscribe stream");
const running = publisher.runSubscribe(
replaySubscribe({ id: 0n, broadcast: Path.from("relayed"), track: "video", priority: 0 }),
server,
);
try {
const response = await decodeSubscribeResponse(client.reader, version);
if (!("start" in response)) throw new Error("expected SUBSCRIBE_START");
expect([response.start.group, response.start.largest]).toEqual([0, { group: 4, frame: 0 }]);
} finally {
client.close();
publisher.close();
broadcast.close();
origin.close();
pair.client.close();
pair.server.close();
await running;
}
});

async function settleControls(): Promise<void> {
await new Promise<void>((resolve) => {
const channel = new MessageChannel();
Expand Down
13 changes: 12 additions & 1 deletion js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ class SubscriptionControls {
}
}

// The later of two locations, either of which may be unknown.
function newer(a: track.Location | undefined, b: track.Location | undefined): track.Location | undefined {
if (!a || !b) return a ?? b;
return b.group > a.group || (b.group === a.group && b.frame > a.frame) ? b : a;
}

// Register both readiness sources in the same turn after the caller observed neither ready.
// The winner disposes both registrations, so an idle subscription accumulates nothing no
// matter how many times it wakes.
Expand Down Expand Up @@ -1120,7 +1126,12 @@ export class Publisher {
// Exactly-once arrival-order serving. This synchronous package-internal pop
// and frameRange call are the operation's linearization point.
// Popping or filtering a group removes the subscriber's view of its edge.
const largest = !startSent && hasLargest(this.version) ? hooks.bufferedLargest(track) : undefined;
// A relay can hold just the first group of a backlog its source already
// answered past, so the source's largest says where the live feed is.
const largest =
!startSent && hasLargest(this.version)
? newer(hooks.bufferedLargest(track), hooks.peekLargest(track))
: undefined;
const recv = hooks.tryRecvGroup(track, startSent);
switch (recv.kind) {
case "error":
Expand Down
1 change: 1 addition & 0 deletions js/net/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,7 @@ export class Subscriber {
hooks.tryRecvGroup = (subscriber, started) => subscriber.#tryRecvGroup(started);
hooks.latestSequence = (subscriber) => subscriber.#state.latest;
hooks.bufferedLargest = (subscriber) => subscriber.#bufferedLargest();
hooks.peekLargest = (subscriber) => subscriber.#state.largest.peek();
hooks.groupChanged = (subscriber, fn) => subscriber.#groupChanged(fn);
hooks.fetchBudget = (subscriber, maxDelay) => {
subscriber.#fetchBudget = maxDelay ?? Number.POSITIVE_INFINITY;
Expand Down
1 change: 0 additions & 1 deletion quest/m1/test-flakes-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Public API: none. Wire: none.

## Required

- [TS MP2 join hole](/quest/m1/test-flakes-2/ts-mp2-join-hole.md) - the TS export skips no MPEG-1 audio group right after its live join, fixed at the cause
- [Auth refresh mocks time](/quest/m1/test-flakes-2/auth-refresh-mock-time.md) - moq-relay's auth refresh and fixed-lease tests measure a grant's life on one mocked clock
- [Relay burst drill](/quest/m1/test-flakes-2/relay-burst-impaired.md) - the impaired transport-peer burst drill never aborts its subscription while the link is up
- [BBR marking seed](/quest/m1/test-flakes-2/bbr-marking-seed.md) - the BBR marking test seeds its probe schedule, replays a failing seed, and its rare drop is fixed at the cause
Expand Down
52 changes: 0 additions & 52 deletions quest/m1/test-flakes-2/ts-mp2-join-hole.md

This file was deleted.

9 changes: 4 additions & 5 deletions quest/m2/audio-join-budget.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ session.
Reproduce it the same way first: a publisher bursting one-frame groups over
`moq_net::transport::memory` before the update lands. If it holds, subscribe with the full
budget from the start and keep the warmup reach local, as the export now picks its live
edge in the consumer instead of on the wire.

## Related

- [TS MP2 join hole](/quest/m1/test-flakes-2/ts-mp2-join-hole.md) - the TS export still loses MP2 groups after its live join; it may share this race
edge in the consumer instead of on the wire. The export anchors that edge on the
publisher's largest (`track::Subscriber::poll_largest`), which a relay now forwards from
its source; `moq_mux::container::test_util::Link::held` delivers one group stream ahead of
the rest, the way the export's join test does.
30 changes: 19 additions & 11 deletions rs/moq-mux/src/container/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct Consumer<F: Container> {
// subscription names no start. Cleared once a first group is chosen.
startup: bool,

// Latch onto the newest group to arrive instead, discarding what arrived below it.
// Latch onto the newest group instead, discarding what arrived below it.
newest: bool,

// How far we may drift from the live edge before skipping a group.
Expand Down Expand Up @@ -193,9 +193,10 @@ impl<F: Container> Consumer<F> {
}
}

/// Start at the newest group to arrive, the live edge, rather than the floor or the oldest
/// group the publisher serves. The subscription keeps its budget, so every group after the
/// start arrives as it would for any reader; what arrived below the start is discarded.
/// Start at the newest group, the live edge, rather than the floor or the oldest group the
/// publisher serves: the publisher's largest when it reports one past what has arrived. The
/// subscription keeps its budget, so every group after the start arrives as it would for any
/// reader; what arrived below the start is discarded.
pub(crate) fn live(mut self) -> Self {
self.startup = true;
self.newest = true;
Expand Down Expand Up @@ -299,14 +300,21 @@ impl<F: Container> Consumer<F> {
!group.empty
});
if any_frame {
// A live reader takes the newest group announced so far, though its frames may
// still be on their way, and discards the older ones the budget also delivers.
// A live reader takes the newest group, though its frames may still be on their
// way, and discards the older ones the budget also delivers. The publisher's
// largest names it before its stream lands: the backlog's oldest group can arrive
// first, and starting there loses the groups after it once the live edge moves a
// budget past them.
let start = match self.newest {
true => self.pending.back(),
false => self.pending.front(),
}
.expect("a group has a frame")
.sequence;
true => {
let mut start = self.pending.back().expect("a group has a frame").sequence;
while let Poll::Ready(Some(largest)) = self.track.poll_largest(waiter) {
start = start.max(largest.group);
}
start
}
false => self.pending.front().expect("a group has a frame").sequence,
};
// Nothing has been read, so discarding what lies below the start is no gap.
self.pending.retain(|group| group.sequence >= start);
self.current = start;
Expand Down
126 changes: 91 additions & 35 deletions rs/moq-mux/src/container/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ impl ExportSource {
})
}

/// Start at the newest group to arrive, the live edge, rather than the oldest one the max
/// delay still reaches. The subscription carries the max delay from the start: under a zero
/// budget every hop drops each group a newer one supersedes, and raising it later cannot
/// recover the groups that follow the edge.
/// Start at the newest group, the live edge, rather than the oldest one the max delay still
/// reaches. The subscription carries the max delay from the start: under a zero budget every
/// hop drops each group a newer one supersedes, and raising it later cannot recover the
/// groups that follow the edge.
pub fn live(mut self) -> Self {
self.live = true;
self
Expand Down Expand Up @@ -716,39 +716,10 @@ mod tests {
use std::time::Duration;

use crate::container::Container as _;
use crate::container::test_util::raw_frame;
use moq_net::transport::memory;
use crate::container::test_util::{Link, origin, raw_frame};

let origin = |hop| {
let config = moq_net::origin::Config::new(moq_net::Hop::new(hop).unwrap());
let (producer, driver) = moq_net::origin::Producer::new(config);
tokio::spawn(moq_net::time::run(driver));
producer
};
let (publisher, subscriber) = (origin(1), origin(2));
let version: moq_net::Version = "moq-lite-07-wip".parse().unwrap();
let mut config = memory::Config::default();
config.protocol = Some(version.alpn().to_string());
let (client_transport, server_transport) = memory::pair(config);
let client = moq_net::Client::new()
.with_versions(version.into())
.with_publisher(publisher.consume());
let server = moq_net::Server::new()
.with_versions(version.into())
.with_subscriber(subscriber.clone());
// Each driver runs as soon as its handshake resolves: the accept waits on the client's SETUP.
let _sessions = tokio::join!(
async {
let (session, driver) = client.connect(moq_net::time::now(), client_transport).await.unwrap();
tokio::spawn(moq_net::time::run(driver));
session
},
async {
let (session, driver) = server.accept(moq_net::time::now(), server_transport).await.unwrap();
tokio::spawn(moq_net::time::run(driver));
session
}
);
let _link = Link::new(&publisher, &subscriber).await;

let broadcast = publisher.create_broadcast("room").unwrap();
let track = broadcast
Expand Down Expand Up @@ -804,6 +775,91 @@ mod tests {
assert_eq!(export.skips(), 0);
}

/// A live start begins at the publisher's newest group, even when an older group of the
/// backlog the budget serves arrives first. Starting at that older group reads it, then
/// skips the groups after it once the next PES moves the live edge a budget past them: a
/// gap right after the first frame. Behind a relay holding only that first group, the
/// relay passes on the newest group its source named.
#[tokio::test(start_paused = true)]
async fn a_live_start_behind_a_backlog_starts_at_the_newest_group() {
use std::time::Duration;

use crate::container::Container as _;
use crate::container::test_util::{Link, origin, raw_frame};

for relay in [false, true] {
let (publisher, subscriber) = (origin(1), origin(3));
// SETUP takes the first uni stream; the second is the backlog's oldest group.
let (held, _downstream) = match relay {
true => {
let middle = origin(2);
let held = Link::held(&publisher, &middle, 2).await;
(held, Some(Link::new(&middle, &subscriber).await))
}
false => (Link::held(&publisher, &subscriber, 2).await, None),
};

let broadcast = publisher.create_broadcast("room").unwrap();
let track = broadcast
.create_track("audio", hang::container::track_info(hang::catalog::PRIORITY.audio))
.unwrap();
broadcast.announce(Default::default()).unwrap();
let consumer = subscriber.consume();
consumer.routed("room").await.unwrap();
let remote = consumer.request_broadcast("room", None).await.unwrap();
let source = crate::Source::new(consumer, remote);

// MPEG-1 audio from TS: one 24 ms frame per group, a PES of five written at once.
let wire = HangContainer::Legacy(crate::container::Kind::Audio);
let pes = |pes: u64| {
for sequence in pes * 5..pes * 5 + 5 {
let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap();
wire.write(&mut group, &[raw_frame(sequence * 24_000, b"audio", true)])
.unwrap();
group.finish().unwrap();
}
};
// About two seconds of it, so the budget's backlog reaches back about 450 ms.
for n in 0..17 {
pes(n);
tokio::time::sleep(Duration::from_millis(120)).await;
}

let mut export = ExportSource::for_audio(&source, "audio", &audio(None), Duration::from_millis(450))
.unwrap()
.unwrap()
.live();
let mut read = async || {
tokio::time::timeout(Duration::from_millis(100), kio::wait(|waiter| export.poll_read(waiter)))
.await
.ok()
.map(|frame| frame.unwrap().map(|frame| frame.timestamp.as_micros()))
};
let mut timestamps = Vec::new();
// Only the backlog's oldest group has landed.
if let Some(Some(timestamp)) = read().await {
timestamps.push(timestamp);
}
// The next PES moves the live edge on before the rest of the backlog lands.
pes(17);
tokio::time::sleep(Duration::from_millis(120)).await;
pes(18);
held.open();
while let Some(Some(timestamp)) = read().await {
timestamps.push(timestamp);
}
track.finish().unwrap();
while let Some(Some(timestamp)) = read().await {
timestamps.push(timestamp);
}

// Group 84 was the newest when the export joined.
let expected: Vec<u128> = (84..95).map(|sequence| sequence * 24_000).collect();
assert_eq!(timestamps, expected, "relay: {relay}");
assert_eq!(export.skips(), 0, "relay: {relay}");
}
}

async fn audio_export() -> (moq_net::track::Producer, ExportSource) {
use std::time::Duration;
let live = Live::audio(audio(None));
Expand Down
Loading
Loading