236 lines
8.0 KiB
TypeScript
236 lines
8.0 KiB
TypeScript
import { expect, test, vi } from "vitest";
|
|
vi.mock("../src/shared/media-resource", () => ({
|
|
mediaFragmentBytes: () => 60000000,
|
|
mediaManifestBytes: () => 1048576,
|
|
}));
|
|
import { attachWatchDashReader } from "../src/client/channel/watch-dash-loader";
|
|
import { requireClearStaticManifest, WatchDashMedia } from "../src/client/channel/watch-dash-media";
|
|
|
|
test("a DASH update must supply a complete static MPD before Shaka applies it", () => {
|
|
const root = { tagName: "MPD", attributes: {}, children: [], parent: null };
|
|
expect(() => requireClearStaticManifest(root)).not.toThrow();
|
|
expect(() => requireClearStaticManifest({ ...root, tagName: "Patch" })).toThrow("watch_invalid_manifest");
|
|
expect(() => requireClearStaticManifest({ ...root, attributes: { type: "dynamic" } })).toThrow("watch_live_timeline_unsupported");
|
|
});
|
|
|
|
const resource = "10000000-0000-4000-8000-000000000001";
|
|
function sdk() {
|
|
const schemes = new Map<string, any>(),
|
|
filters = new Set<any>();
|
|
return {
|
|
schemes,
|
|
filters,
|
|
library: {
|
|
net: {
|
|
NetworkingEngine: {
|
|
RequestType: { MANIFEST: 0, SEGMENT: 1 },
|
|
AdvancedRequestType: { XLINK: 14, LINKED_MPD: 15 },
|
|
PluginPriority: { APPLICATION: 3 },
|
|
registerScheme: (s: string, p: any) => schemes.set(s, p),
|
|
unregisterScheme: (s: string) => schemes.delete(s),
|
|
},
|
|
},
|
|
util: {
|
|
AbortableOperation: class {
|
|
constructor(
|
|
public promise: Promise<any>,
|
|
public abort: () => Promise<void>,
|
|
) {}
|
|
},
|
|
},
|
|
},
|
|
player: {
|
|
getNetworkingEngine: () => ({
|
|
registerRequestFilter: (f: any) => filters.add(f),
|
|
unregisterRequestFilter: (f: any) => filters.delete(f),
|
|
}),
|
|
},
|
|
};
|
|
}
|
|
const request = (url: string, headers = {}) => ({
|
|
uris: [url],
|
|
method: "GET",
|
|
body: null,
|
|
allowCrossSiteCredentials: false,
|
|
headers,
|
|
});
|
|
test("DASH uses exact range windows and admits only its own opaque media graph", async () => {
|
|
const s = sdk(),
|
|
calls: any[] = [];
|
|
const data = Uint8Array.from({ length: 100000 }, (_, i) => i % 251);
|
|
const life = new AbortController();
|
|
const bridge = attachWatchDashReader(
|
|
s.library as any,
|
|
s.player as any,
|
|
async (id, offset, length) => {
|
|
calls.push({ id, offset, length });
|
|
return {
|
|
mimeType: "video/mp4",
|
|
totalSize: data.length,
|
|
bytes: data.slice(offset, offset + length),
|
|
eof: offset + length >= data.length,
|
|
};
|
|
},
|
|
life.signal,
|
|
);
|
|
const url = bridge.url(resource),
|
|
plugin = [...s.schemes.values()][0],
|
|
req = request(url, { Range: "bytes=7-70006" });
|
|
const out = await plugin(url, req, 1, () => {}).promise;
|
|
expect(new Uint8Array(out.data)).toEqual(data.slice(7, 70007));
|
|
expect(calls.every((c) => c.length <= 65536)).toBe(true);
|
|
const filter = [...s.filters][0];
|
|
expect(() => filter(1, request("https://example.invalid/seg"))).toThrow(
|
|
"watch_source_unavailable",
|
|
);
|
|
expect(() => filter(2, request(url))).toThrow("watch_source_unavailable");
|
|
expect(() => filter(1, request(url + "?override=1"))).toThrow(
|
|
"watch_source_unavailable",
|
|
);
|
|
life.abort();
|
|
expect(s.schemes.size).toBe(0);
|
|
expect(s.filters.size).toBe(0);
|
|
});
|
|
test("retiring DASH aborts an in-flight existing Reader without returning late bytes", async () => {
|
|
const s = sdk(),
|
|
life = new AbortController();
|
|
let cancelled = 0;
|
|
let start!: () => void;
|
|
const began = new Promise<void>((r) => (start = r));
|
|
const bridge = attachWatchDashReader(
|
|
s.library as any,
|
|
s.player as any,
|
|
async (_id, _offset, _length, signal) => {
|
|
start();
|
|
return new Promise((_resolve, reject) =>
|
|
signal.addEventListener(
|
|
"abort",
|
|
() => {
|
|
cancelled++;
|
|
reject(signal.reason);
|
|
},
|
|
{ once: true },
|
|
),
|
|
);
|
|
},
|
|
life.signal,
|
|
);
|
|
const url = bridge.url(resource),
|
|
plugin = [...s.schemes.values()][0],
|
|
reading = plugin(url, request(url), 1, () => {}).promise;
|
|
const rejected = expect(reading).rejects.toMatchObject({
|
|
name: "AbortError",
|
|
});
|
|
await began;
|
|
life.abort();
|
|
await rejected;
|
|
expect(cancelled).toBe(1);
|
|
expect(s.schemes.size).toBe(0);
|
|
});
|
|
test("SegmentBase ranges remain readable from a large selected file without downloading it whole", async () => {
|
|
const s = sdk(),
|
|
life = new AbortController(),
|
|
calls: {
|
|
offset: number;
|
|
length: number;
|
|
}[] = [];
|
|
const size = 128 * 1024 * 1024;
|
|
const bridge = attachWatchDashReader(
|
|
s.library as any,
|
|
s.player as any,
|
|
async (_id, offset, length) => {
|
|
calls.push({ offset, length });
|
|
return {
|
|
mimeType: "video/webm",
|
|
totalSize: size,
|
|
bytes: Uint8Array.from({ length }, (_, i) => (offset + i) % 251),
|
|
eof: offset + length === size,
|
|
};
|
|
},
|
|
life.signal,
|
|
);
|
|
const url = bridge.url(resource),
|
|
plugin = [...s.schemes.values()][0],
|
|
range = request(url, { Range: "bytes=310-552" });
|
|
const result = await plugin(url, range, 1, () => {}).promise;
|
|
expect(new Uint8Array(result.data)).toEqual(
|
|
Uint8Array.from({ length: 243 }, (_, i) => (310 + i) % 251),
|
|
);
|
|
expect(calls).toEqual([
|
|
{ offset: 0, length: 1 },
|
|
{ offset: 310, length: 243 },
|
|
]);
|
|
life.abort();
|
|
});
|
|
|
|
test("DASH rejects an oversized requested allocation before reading its body", async () => {
|
|
const s = sdk(),
|
|
life = new AbortController(),
|
|
calls: number[] = [];
|
|
const bridge = attachWatchDashReader(
|
|
s.library as any,
|
|
s.player as any,
|
|
async (_id, _offset, length) => {
|
|
calls.push(length);
|
|
return {
|
|
mimeType: "video/webm",
|
|
totalSize: 128 * 1024 * 1024,
|
|
bytes: new Uint8Array(length),
|
|
eof: false,
|
|
};
|
|
},
|
|
life.signal,
|
|
);
|
|
const url = bridge.url(resource),
|
|
plugin = [...s.schemes.values()][0];
|
|
for (const [type, headers] of [
|
|
[1, {}],
|
|
[1, { Range: "bytes=0-60000000" }],
|
|
[0, { Range: "bytes=0-1048576" }],
|
|
] as const) {
|
|
await expect(
|
|
plugin(url, request(url, headers), type, () => {}).promise,
|
|
).rejects.toThrow("watch_resource_limit");
|
|
}
|
|
expect(calls).toEqual([1, 1, 1]);
|
|
life.abort();
|
|
});
|
|
|
|
test.each([
|
|
["reader", null, "watch_resource_limit"],
|
|
["reader", new Error("watch_source_failed"), "watch_source_failed"],
|
|
["reader", new Error("watch_read_busy"), "watch_read_busy"],
|
|
["reader", new Error("unknown_outcome"), "unknown_outcome"],
|
|
["reader", new Error("watch_live_timeline_unsupported"), "watch_media_failed"],
|
|
["direct", new Error("watch_invalid_manifest"), "watch_invalid_manifest"],
|
|
["direct", new Error("watch_read_busy"), "watch_media_failed"],
|
|
["direct", { category: 1, data: ["watch_resource_limit", "private diagnostic"] }, "watch_media_unavailable"],
|
|
] as const)("DASH %s reports %s as %s in load rejections and error events", async (mode, error, message) => {
|
|
const s = sdk(), failed = vi.fn();
|
|
class Player extends EventTarget {
|
|
static isBrowserSupported() { return true; }
|
|
getNetworkingEngine = s.player.getNetworkingEngine;
|
|
configure() {}
|
|
async attach() {}
|
|
async destroy() {}
|
|
async load(url: string) {
|
|
try {
|
|
if (mode === "direct") throw error;
|
|
const plugin = [...s.schemes.values()][0];
|
|
await plugin(url, request(url), 1, () => {}).promise;
|
|
} catch (detail) {
|
|
this.dispatchEvent(new CustomEvent("error", { detail }));
|
|
throw detail;
|
|
}
|
|
}
|
|
}
|
|
vi.doMock("shaka-player", () => ({ default: { ...s.library, Player } }));
|
|
const read = error ? vi.fn().mockRejectedValue(error) : vi.fn().mockResolvedValue({ totalSize: 60_000_001 });
|
|
const source = mode === "reader" ? { resourceId: resource, read } : "https://example.invalid/movie.mpd";
|
|
const media = new WatchDashMedia({} as HTMLVideoElement, source, new AbortController().signal, failed);
|
|
try {
|
|
await expect(media.open()).rejects.toMatchObject({ message });
|
|
expect(failed).toHaveBeenCalledWith(expect.objectContaining({ message }));
|
|
} finally { media.close(); vi.doUnmock("shaka-player"); }
|
|
});
|