Files
piik/tests/watch-plan.test.ts
T
AAA 845b907d04 fix(watch): choose usable tracks and retain writer backpressure
Use existing player duration and browser codec support for automatic indexed-track previews, and retain explicit webpage selection. Give source-window notices their intrinsic height. Carry writer delivery separately from ordinary Core effects so expiry does not bypass its queue.
2026-11-29 03:28:22 -05:00

101 lines
7.4 KiB
TypeScript

import { expect, test } from "vitest";
import { defaultIndexedTracks, defaultMpdTracks, watchOffers, watchRoutes } from "../src/client/channel/watch-plan";
import type { WatchIndexedChoice } from "../src/client/channel/watch-page-media";
const id = (value: number) => `00000000-0000-4000-8000-${String(value).padStart(12, "0")}`;
function indexed(value: number, kind: "video" | "audio", size?: [number, number], duration = 60): WatchIndexedChoice {
const candidate = { id: id(value), order: value, name: kind, hostname: "media.test", mimeType: `${kind}/mp4`, totalSize: 100 };
return { candidate, inspection: { candidate,
metadata: { kind, codec: kind === "video" ? "avc1.640028" : "mp4a.40.2", duration, firstPTS: 0, timescale: 1000, language: "und",
...(size ? { width: size[0], height: size[1] } : {}) },
recipe: { choiceId: candidate.id, totalSize: 100, codec: "avc1.640028", timescale: 1000, mediaOffset: 40, initBase64: "aW5pdA==", segments: [[60, 1000]] } } };
}
function track(value: number, kind: "video" | "audio", extra: Partial<{ codec: string; height: number; bandwidth: number; role: string }> = {}) {
const height = extra.height ?? null;
return { id: id(value), kind, label: "", mimeType: `${kind}/mp4`, codec: extra.codec ?? (kind === "video" ? "avc1.640028" : "mp4a.40.2"),
language: "und", role: extra.role ?? "", width: height && Math.round(height * 16 / 9), height, bandwidth: extra.bandwidth ?? null, frameRate: null };
}
test("a start tries the page's own source first, with the site's address behind the App's bytes", () => {
const choices = [{ id: id(1), order: 1, displayURL: "https://media.test/master.m3u8", name: "master.m3u8", format: "hls" as const },
{ id: id(2), order: 2, displayURL: "https://media.test/film.mp4", name: "film.mp4", format: "file" as const }];
const mpd = [{ candidate: { id: id(3), order: 3, name: "manifest.mpd", hostname: "media.test" } }];
expect(watchOffers({ choices, indexed: [indexed(4, "video", [1280, 720]), indexed(5, "audio")], mpd })).toEqual([
{ kind: "forward", id: id(1) }, { kind: "direct", id: id(1) }, { kind: "forward", id: id(2) }, { kind: "direct", id: id(2) },
{ kind: "mpd", choiceId: id(3) }, { kind: "indexed", videoId: id(4), audioId: id(5) }]);
expect(watchOffers({ choices: [], indexed: [], mpd: [] })).toEqual([]);
});
test("separately loaded tracks default to the largest picture and retain audio order on equal durations", () => {
expect(defaultIndexedTracks([indexed(1, "video", [1280, 720]), indexed(2, "audio"), indexed(3, "video", [1920, 1080])]))
.toEqual({ videoId: id(3), audioId: id(2) });
expect(defaultIndexedTracks([indexed(1, "video", [1280, 720])])).toEqual({ videoId: id(1) });
// Sound alone, or a track that could not be read, is not a video.
expect(defaultIndexedTracks([indexed(2, "audio"), { candidate: indexed(1, "video").candidate, error: "watch_indexed_unsupported" }])).toBeUndefined();
});
test("an earlier advertisement audio track does not replace the selected picture's sound", () => {
const tracks = [indexed(1, "video", [1920, 1080], 4), indexed(2, "audio", undefined, 2.02322),
indexed(3, "video", [320, 180], 4.004), indexed(4, "audio", undefined, 4.02322),
indexed(5, "audio", undefined, 4.02322)];
expect(watchOffers({ choices: [], indexed: tracks, mpd: [] }))
.toEqual([{ kind: "indexed", videoId: id(1), audioId: id(4) }]);
});
test("the selected player's duration guides the private preview of separately loaded tracks", () => {
const ad = indexed(1, "video", [1920, 1080], 30);
const film = indexed(2, "video", [1280, 720], 5400);
const audio = [indexed(3, "audio", undefined, 30), indexed(4, "audio", undefined, 5400)];
const offer = (videos: WatchIndexedChoice[], duration?: number | null) =>
watchOffers({ choices: [], indexed: [...videos, ...audio], mpd: [] }, duration)[0];
const adOffer = { kind: "indexed", videoId: id(1), audioId: id(3) };
const filmOffer = { kind: "indexed", videoId: id(2), audioId: id(4) };
for (const videos of [[ad, film], [film, ad]]) {
expect(offer(videos, 5399.5)).toEqual(filmOffer);
expect(offer(videos, 30)).toEqual(adOffer);
for (const unavailable of [undefined, null, 0, Infinity]) expect(offer(videos, unavailable)).toEqual(adOffer);
}
const sameDuration = indexed(2, "video", [1280, 720], 30);
expect(offer([sameDuration, ad], 30)).toEqual(adOffer);
const samePicture = indexed(2, "video", [1920, 1080], 30);
expect(offer([samePicture, ad], 30)).toEqual({ kind: "indexed", videoId: id(2), audioId: id(3) });
});
test("a manifest defaults to the largest picture up to 1080 lines, the widest decoded codec among equals, and the main sound", () => {
const catalog = { sourceId: id(9), tracks: [
track(1, "video", { height: 2160, codec: "av01.0.12M.08" }), track(2, "video", { height: 1080, codec: "av01.0.08M.08" }),
track(3, "video", { height: 1080, bandwidth: 4_000_000 }), track(4, "video", { height: 1080, bandwidth: 6_000_000 }), track(5, "video", { height: 720 }),
track(6, "audio", { role: "dub" }), track(7, "audio", { role: "main" })] };
expect(defaultMpdTracks(catalog)).toEqual({ sourceId: id(9), videoId: id(4), audioId: id(7) });
// What this browser cannot play is never the default.
expect(defaultMpdTracks(catalog, (_mime, codec) => !codec.startsWith("avc1"))).toEqual({ sourceId: id(9), videoId: id(2), audioId: id(7) });
// With every picture above the limit the nearest one is taken; with no sound the picture goes alone.
expect(defaultMpdTracks({ sourceId: id(9), tracks: [track(1, "video", { height: 2160 }), track(2, "video", { height: 1440 })] }))
.toEqual({ sourceId: id(9), videoId: id(2) });
expect(defaultMpdTracks({ sourceId: id(9), tracks: [track(6, "audio")] })).toBeUndefined();
});
test("a viewer's endpoint goes to the site first and to the sharer second, and only where it can", () => {
const byteSource = { sessionId: id(1), resourceId: id(2), format: "hls" as const };
const site = { kind: "website" as const, url: "https://site.test/watch", mediaUrl: "https://media.test/film.mp4" };
expect(watchRoutes({ source: site, byteSource }, () => true)).toEqual(["direct", "peer"]);
expect(watchRoutes({ source: site, byteSource }, () => false)).toEqual(["direct"]);
expect(watchRoutes({ source: { kind: "website", url: "https://site.test/watch" }, byteSource }, () => true)).toEqual(["peer"]);
// A page that only its own player can show asks its person: no route is taken alone.
expect(watchRoutes({ source: { kind: "website", url: "https://site.test/watch" } }, () => true)).toEqual([]);
});
test.each([true, false])("the App's source order survives mixed DASH and HLS lists (DASH first: %s)", async dashFirst => {
const { AppPage } = await import("../src/client/channel/app-link");
const page = new AppPage(id(1), "https://page.test/watch", async () => ({
choices: [{ id: id(2), order: dashFirst ? 1 : 0, name: "Playlist", displayURL: "https://media.test/master.m3u8", format: "hls" }],
mpdCandidates: [{ id: id(3), order: dashFirst ? 0 : 1, name: "Manifest", hostname: "media.test" }],
indexedCandidates: [], byteSource: null,
}), () => {}, () => {});
const { choices, mpdCandidates } = await page.mediaSources();
const offers = watchOffers({ choices, mpd: mpdCandidates.map(candidate => ({ candidate })), indexed: [] });
const hls = [{ kind: "forward", id: id(2) }, { kind: "direct", id: id(2) }];
const dash = { kind: "mpd", choiceId: id(3) };
expect(offers).toEqual(dashFirst ? [dash, ...hls] : [...hls, dash]);
});