Skip to content
Closed
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
38 changes: 35 additions & 3 deletions js/net/src/ietf/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const INVALID_RANGE = 0x11;
const INVALID_JOINING_REQUEST_ID = 0x32;

/** A publisher serving the `room` broadcast, with its `video` track and the client's uni streams. */
function serve(version: IetfVersion, grant?: Signal<Grant | undefined>) {
function serve(version: IetfVersion, grant?: Signal<Grant | undefined>, priority?: number) {
const pair = createMockTransportPair(ALPNS[version]);
const origin = new OriginProducer();
const pub = new Publisher({
Expand All @@ -45,7 +45,7 @@ function serve(version: IetfVersion, grant?: Signal<Grant | undefined>) {
});
const publication = origin.createBroadcast(Path.from("room"));
publication.announce();
const track = publication.broadcast.createTrack("video", { timescale: Timescale.MILLI });
const track = publication.broadcast.createTrack("video", { timescale: Timescale.MILLI, priority });
const uni = pair.client.incomingUnidirectionalStreams.getReader() as ReadableStreamDefaultReader<
ReadableStream<Uint8Array>
>;
Expand Down Expand Up @@ -296,6 +296,37 @@ test("a FETCH serves several groups in order", async () => {
}
});

/** The Publisher Priority each object on a fetch stream states, absent where it inherits. */
async function priorities(raw: Uint8Array, version: IetfVersion): Promise<(number | undefined)[]> {
const reader = new Reader(undefined, raw, version);
expect(await reader.u53()).toBe(FetchHeader.type);
await FetchHeader.decode(reader, version);
const out: (number | undefined)[] = [];
while (!(await reader.done())) {
const flags = await reader.u53();
if (flags & 0x08) await reader.u53();
if (flags & 0x04) await reader.u53();
out.push(flags & 0x10 ? await reader.u8() : undefined);
if (flags & 0x20) await reader.read(await reader.u53());
await reader.read(await reader.u53());
}
return out;
}

// A fetch stream's first object states the track's Publisher Priority, as a subgroup header
// does, and the rest inherit it. Model priority 72 is wire 55, lower first.
test("a FETCH states the track's priority on its first object", async () => {
for (const version of VERSIONS) {
using fx = serve(version, undefined, 72);
publishPairs(fx.track, 2);
const type = range(version, at(0, 0), at(1, 2));
const answer = served(await fetch(fx, new Fetch({ requestId: FETCH_ID, type })));
expect(answer.objects).toEqual(pairs([0, 1]));
expect(answer.ok.properties.priority).toBe(55);
expect(await priorities(answer.raw, version)).toEqual([55, undefined, undefined, undefined]);
}
});

// Only a finished track's final object ends it, wherever the range starts or ends.
test("a FETCH through the final object reports the end of track", async () => {
for (const version of VERSIONS) {
Expand Down Expand Up @@ -514,7 +545,8 @@ test("draft-18: a joining FETCH serves the head its subscription starts after",
await reader.u53();
await FetchHeader.decode(reader, D18);
expect(await reader.u53()).toBe(0x3c);
expect([await reader.u53(), await reader.u53(), await reader.u8()]).toEqual([5, 0, 0]);
// The priority is the track's, as FETCH_OK and every subgroup header state it.
expect([await reader.u53(), await reader.u53(), await reader.u8()]).toEqual([5, 0, 127]);
await reader.read(await reader.u53());
await reader.read(await reader.u53());
expect(await reader.u53()).toBe(0x20);
Expand Down
17 changes: 11 additions & 6 deletions js/net/src/ietf/ietf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,18 @@ test("Frame reads only the current LOC Timestamp ID on every draft", async () =>

// A fetch stream's first object is the only one carrying absolute ids, so a wrong flag byte
// there silently renumbers every object after it. This codec is the sole serialization path
// for a served fill.
// for a served fill. Its priority is the track's, as a subgroup header's is: model priority
// 72 is wire 55, lower first.
test("FetchFrame: the first object carries its absolute ids and the rest inherit them", async () => {
const timestamp = new Timestamp(96_000, Timescale.MILLI);
const frame = new FetchFrame({ payload: new Uint8Array([0xaa, 0xbb]), timestamp });
const frame = new FetchFrame({ payload: new Uint8Array([0xaa, 0xbb]), timestamp, priority: 55 });

const first = await encodeFetchFrameVersioned(frame, { group: 7, object: 3, first: true }, Version.DRAFT_22);
expect(Array.from(first)).toEqual([
0x3c, // GROUP_ID | OBJECT_ID | PRIORITY | PROPERTIES
7, // group id
3, // object id
0, // publisher priority
55, // publisher priority
0x04, // properties length
0x10, // TIMESTAMP
0xc1,
Expand Down Expand Up @@ -845,7 +846,11 @@ test("FetchFrame: the first object carries its absolute ids and the rest inherit
// A fetch object has no status field, unlike a subgroup object: a zero payload length is
// simply an empty object, and writing a status after it would desync the stream.
test("FetchFrame: an empty payload is a zero length, with no status byte", async () => {
const frame = new FetchFrame({ payload: new Uint8Array(), timestamp: new Timestamp(0, Timescale.MILLI) });
const frame = new FetchFrame({
payload: new Uint8Array(),
timestamp: new Timestamp(0, Timescale.MILLI),
priority: 0,
});
const encoded = await encodeFetchFrameVersioned(frame, { group: 1, object: 0, first: true }, Version.DRAFT_22);

expect(Array.from(encoded)).toEqual([
Expand All @@ -864,7 +869,7 @@ test("FetchFrame: an empty payload is a zero length, with no status byte", async
// omitted rather than carrying a value in units the peer was never told. This is what a
// subscriber that sent INCLUDE_PROPERTIES=0 gets.
test("FetchFrame: an unstamped object omits the properties field", async () => {
const frame = new FetchFrame({ payload: new Uint8Array([0x01]) });
const frame = new FetchFrame({ payload: new Uint8Array([0x01]), priority: 0 });
const encoded = await encodeFetchFrameVersioned(frame, { group: 0, object: 0, first: false }, Version.DRAFT_22);

// Flags 0 (subgroup zero, no fields present), then the payload.
Expand All @@ -873,7 +878,7 @@ test("FetchFrame: an unstamped object omits the properties field", async () => {

// The first object still carries its absolute ids and priority; only the properties go.
test("FetchFrame: an unstamped first object keeps its ids", async () => {
const frame = new FetchFrame({ payload: new Uint8Array([0x01]) });
const frame = new FetchFrame({ payload: new Uint8Array([0x01]), priority: 0 });
const encoded = await encodeFetchFrameVersioned(frame, { group: 4, object: 2, first: true }, Version.DRAFT_22);

expect(Array.from(encoded)).toEqual([
Expand Down
10 changes: 8 additions & 2 deletions js/net/src/ietf/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,16 @@ export class FetchFrame {
payload: Uint8Array;
/** The presentation timestamp carried in object properties, when present. */
timestamp?: Timestamp;
/**
* The track's Publisher Priority on the wire, which the stream's first object states for
* every object after it, as a subgroup header does.
*/
priority: number;

constructor({ payload, timestamp }: { payload: Uint8Array; timestamp?: Timestamp }) {
constructor({ payload, timestamp, priority }: { payload: Uint8Array; timestamp?: Timestamp; priority: number }) {
this.payload = payload;
this.timestamp = timestamp;
this.priority = priority;
}

/**
Expand All @@ -419,7 +425,7 @@ export class FetchFrame {
position.first ? position.group : Math.abs(position.group - (position.previousGroup as number)) - 1,
);
await w.u53(position.object);
if (position.first) await w.u8(0);
if (position.first) await w.u8(this.priority);
} else {
// Same group and priority; the Object ID is the prior one plus one.
await w.u53(stamped ? FETCH_PROPERTIES : 0);
Expand Down
82 changes: 82 additions & 0 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4342,6 +4342,88 @@ test("draft-22: cancellation releases a fill parked on flow control", async () =
}
});

// A fill reads the cache through forks of its own, ranked at the converted subscriber
// priority. Copied unconverted, wire 100 would raise the track's aggregate from 27 to 100,
// and wire 191 is outside the model's -128..=127, so the fork throws and the fill never runs.
test("draft-22: a SUBSCRIBE's fill reads the cache at the converted subscriber priority", async () => {
const fx = fixture();
const track = fx.broadcast.createTrack("video", { timescale: Timescale.MILLI });
writeGroup(track, 2);
const { client } = await runSubscribe(
fx,
new Subscribe({
requestId: 0n,
trackNamespace: Path.from("test"),
trackName: "video",
subscriberPriority: 191,
filter: { kind: "absolute", startGroup: 10n, startObject: 0n },
fill: { filter: { kind: "unfiltered" }, rangeFilters: false },
}),
);
try {
expect(track.subscription.peek()?.priority).toBe(-64);
const stream = await nextUni(fx.uni);
if (!stream) throw new Error("missing fill stream");
const served = await readFill(new Reader(stream, undefined, V22), "ascending");
expect(served.reset).toBeUndefined();
expect(served.objects).toEqual([0, 1].map((id) => ({ group: 0, id, payload: `0.${id}` })));
} finally {
client.close();
track.finish();
track.close();
fx.close();
}
});

test("draft-22: an update's fill reads the cache at the converted subscriber priority", async () => {
for (const [wire, model] of [
[100, 27],
[191, -64],
]) {
const fx = fixture();
const parked = Promise.withResolvers<void>();
const stalled = new WritableStream<Uint8Array>({
write() {
parked.resolve();
return new Promise<void>(() => {});
},
});
const track = fx.broadcast.createTrack("video", { timescale: Timescale.MILLI });
writeGroup(track, 1);
const { client } = await runSubscribe(
fx,
new Subscribe({
requestId: 0n,
trackNamespace: Path.from("test"),
trackName: "video",
subscriberPriority: wire,
filter: { kind: "absolute", startGroup: 10n, startObject: 0n },
}),
);
try {
expect(track.subscription.peek()?.priority).toBe(model);
spyOn(fx.pair.server, "createUnidirectionalStream").mockImplementationOnce(async () => stalled);
const parameters = new Parameters();
parameters.fillParameters = Filter.encodeFill(
{ filter: { kind: "unfiltered" }, groupOrder: "ascending", rangeFilters: false },
fx.version,
);
await client.writer.u53(SubscribeUpdate.id);
await new SubscribeUpdate({ requestId: 2n, parameters }).encode(client.writer, fx.version);
expect(await client.reader.u53()).toBe(RequestOk.id);
await RequestOk.decode(client.reader, fx.version);
// The fill is parked writing its header, so the fork it reads through is still live.
await parked.promise;
expect(track.subscription.peek()?.priority, `wire ${wire}`).toBe(model);
} finally {
client.close();
track.finish();
track.close();
fx.close();
}
}
});

test("draft-22: narrowing gives up an excluded group's open stream", async () => {
const fx = fixture();
const track = fx.broadcast.createTrack("video", { timescale: Timescale.MILLI });
Expand Down
37 changes: 33 additions & 4 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ interface RunFill {
/** The subscriber's delivery priority, applied to the cache subscription the fill reads. */
priority: number;

/** The track's Publisher Priority on the wire, which the fill's first object states. */
publisherPriority: number;

/**
* The subscription's ranking. The fill joins it as the group it fills, so backfill goes out
* in the subscription's GROUP_ORDER rather than ahead of the live groups or behind them all.
Expand Down Expand Up @@ -809,6 +812,7 @@ export class Publisher {
namespace: name,
properties,
timescale,
publisherPriority,
ranking: ranked,
}
: { kind: "unsupported" },
Expand Down Expand Up @@ -863,6 +867,7 @@ export class Publisher {
filling = this.#runFill({
requestId,
priority: fromWire(msg.subscriberPriority),
publisherPriority,
ranking: ranked,
fill: selected,
cache: source,
Expand Down Expand Up @@ -1410,7 +1415,8 @@ export class Publisher {
* fill-failure signal. Nothing here touches the subscription either way.
*/
async #runFill(options: RunFill) {
const { requestId, ranking, fill, cache, timescale, unsubscribed, cancelled, streams } = options;
const { requestId, ranking, fill, cache, timescale, publisherPriority, unsubscribed, cancelled, streams } =
options;
const version = this.#session.version;

// Everything is inside the try so the cache fork is released on every path out,
Expand Down Expand Up @@ -1451,7 +1457,15 @@ export class Publisher {
if (!group) continue;
const slice = groupSlice(fill, sequence);
try {
previous = await this.#writeFillGroup({ stream, group, slice, timescale, cancelled, previous });
previous = await this.#writeFillGroup({
stream,
group,
slice,
timescale,
publisherPriority,
cancelled,
previous,
});
} finally {
group.close();
}
Expand Down Expand Up @@ -1485,13 +1499,16 @@ export class Publisher {
group,
slice,
timescale,
publisherPriority,
cancelled,
previous,
}: {
stream: Writer;
group: group.Consumer;
slice: GroupSlice;
timescale: Timescale | undefined;
/** The track's Publisher Priority on the wire, which the stream's first object states. */
publisherPriority: number;
cancelled: AbortSignal;
previous?: number;
}): Promise<number | undefined> {
Expand Down Expand Up @@ -1524,6 +1541,7 @@ export class Publisher {
first,
previous,
timescale,
publisherPriority,
stop: cancelled,
});
first = false;
Expand All @@ -1542,6 +1560,7 @@ export class Publisher {
first,
previous,
timescale,
publisherPriority,
stop,
}: {
stream: Writer;
Expand All @@ -1551,9 +1570,11 @@ export class Publisher {
first: boolean;
previous: number | undefined;
timescale: Timescale | undefined;
/** The track's Publisher Priority on the wire, which the stream's first object states. */
publisherPriority: number;
stop: AbortSignal;
}): Promise<number> {
const obj = new FetchFrame({ payload: frame.payload, timestamp: frame.timestamp });
const obj = new FetchFrame({ payload: frame.payload, timestamp: frame.timestamp, priority: publisherPriority });
const position = {
group: sequence,
object: frame.sequence,
Expand Down Expand Up @@ -3186,6 +3207,7 @@ export class Publisher {
ok: new FetchOk({ endOfTrack, endLocation, properties: trackProperties(info, msg.propertiesWanted) }),
// A Timestamp goes out only when FETCH_OK actually carries TIMESCALE.
timescale: msg.propertiesWanted ? (info.timescale ?? undefined) : undefined,
publisherPriority: toWire(info.priority),
// Its own rank, so it competes with group streams by its priority alone.
sendOrder: sendOrder({ priority }),
order: msg.groupOrder ?? "ascending",
Expand Down Expand Up @@ -3279,6 +3301,7 @@ export class Publisher {
// Where the subscription starts, never the end of the track.
ok: new FetchOk({ endOfTrack: false, endLocation: end, properties: join.properties }),
timescale: join.timescale,
publisherPriority: join.publisherPriority,
// Ranked as the group it heads, ahead of that group's live rest.
sendOrder: join.ranking.rank(sequence, 0),
ranking: join.ranking,
Expand All @@ -3293,7 +3316,7 @@ export class Publisher {
*/
async #answerFetch(msg: Fetch, request: RequestStream, answer: FetchAnswer, stop: AbortSignal) {
const version = this.#session.version;
const { cache, range, last, timescale, ranking } = answer;
const { cache, range, last, timescale, publisherPriority, ranking } = answer;
const order = [...answer.groups, last.sequence];
if (answer.order === "descending") order.reverse();

Expand Down Expand Up @@ -3323,6 +3346,7 @@ export class Publisher {
first,
previous,
timescale,
publisherPriority,
stop,
});
first = false;
Expand All @@ -3339,6 +3363,7 @@ export class Publisher {
group,
slice,
timescale,
publisherPriority,
cancelled: stop,
previous,
});
Expand Down Expand Up @@ -3462,6 +3487,8 @@ type Joinable =
properties: Properties.Properties;
/** The units the subscription stamps objects in, if any; see {@link RunGroup.timescale}. */
timescale?: Timescale;
/** The track's Publisher Priority on the wire, which the fetch's first object states. */
publisherPriority: number;
/** The subscription's ranking, which the fetch stream joins as the group it heads. */
ranking: Priority;
};
Expand All @@ -3488,6 +3515,8 @@ interface FetchAnswer {
ok: FetchOk;
/** The units objects are stamped in, if any; see {@link RunGroup.timescale}. */
timescale?: Timescale;
/** The track's Publisher Priority on the wire, which the stream's first object states. */
publisherPriority: number;
/** The fetch stream's send order. */
sendOrder: number;
/** The subscription ranking the fetch stream joins, if any. */
Expand Down
Loading