Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { simulatorAvcConfig } from "./avcConfig.ts";
4
5// Captured from the hub before and after its encoder falls back from low-latency rate control.
6const lowLatency = Uint8Array.from([
7 1, 100, 0, 51, 255, 225, 0, 22, 39, 100, 0, 51, 172, 19, 20, 60, 4, 192, 20, 158, 106, 154, 129,
8 1, 1, 3, 194, 1, 8, 248, 1, 0, 4, 40, 238, 60, 176,
9]);
10const fallback = Uint8Array.from([
11 1, 100, 0, 51, 255, 225, 0, 12, 39, 100, 0, 51, 172, 86, 128, 76, 1, 73, 230, 164, 1, 0, 4, 40,
12 238, 60, 176,
13]);
14const repaired = Uint8Array.from([
15 1, 100, 0, 51, 255, 225, 0, 17, 39, 100, 0, 51, 172, 86, 128, 76, 1, 73, 230, 168, 6, 208, 68, 35,
16 80, 1, 0, 4, 40, 238, 60, 176,
17]);
18
19describe("simulator AVC reorder metadata", () => {
20 it("adds zero reordering and one reference frame to the captured fallback SPS", () => {
21 expect(simulatorAvcConfig(fallback)).toEqual(repaired);
22 expect(simulatorAvcConfig(repaired)).toBe(repaired);
23 });
24
25 it("preserves existing VUI and leaves the original codec description untouched", () => {
26 const original = fallback.slice();
27 simulatorAvcConfig(fallback);
28 expect(fallback).toEqual(original);
29 expect(simulatorAvcConfig(lowLatency)).toBe(lowLatency);
30 });
31
32 it("repairs multiple SPS entries while preserving PPS and profile extensions", () => {
33 const extensions = [0xfd, 0xf8, 0xf8, 0];
34 const description = Uint8Array.from([
35 ...fallback.subarray(0, 5),
36 0xe2,
37 ...fallback.subarray(6, 20),
38 ...lowLatency.subarray(6, 30),
39 ...fallback.subarray(20),
40 ...extensions,
41 ]);
42 expect(simulatorAvcConfig(description)).toEqual(
43 Uint8Array.from([
44 ...description.subarray(0, 6),
45 ...repaired.subarray(6, 25),
46 ...lowLatency.subarray(6, 30),
47 ...fallback.subarray(20),
48 ...extensions,
49 ]),
50 );
51 });
52
53 it("passes through every truncated prefix of the captured configurations", () => {
54 for (const config of [fallback, lowLatency]) {
55 for (let length = 0; length < config.length; length++) {
56 const truncated = config.subarray(0, length);
57 expect(simulatorAvcConfig(truncated)).toBe(truncated);
58 }
59 }
60 });
61
62 it("does not modify unsupported profiles, scaling lists, or invalid parameter-set lengths", () => {
63 const unsupportedProfile = fallback.slice();
64 unsupportedProfile[9] = 110;
65 const scalingLists = fallback.slice();
66 scalingLists[12] = scalingLists[12]! | 1;
67 const invalidSpsLength = fallback.slice();
68 invalidSpsLength[6] = 0xff;
69 const invalidPpsLength = fallback.slice();
70 invalidPpsLength[21] = 0xff;
71 for (const config of [unsupportedProfile, scalingLists, invalidSpsLength, invalidPpsLength]) {
72 expect(simulatorAvcConfig(config)).toBe(config);
73 }
74 });
75
76 it("requires valid SPS trailing bits before appending VUI metadata", () => {
77 const invalidPadding = fallback.slice();
78 invalidPadding[19] = 0;
79 expect(simulatorAvcConfig(invalidPadding)).toBe(invalidPadding);
80 });
81
82 it("passes through invalid NAL headers and unterminated Exp-Golomb values", () => {
83 const forbiddenBit = fallback.slice();
84 forbiddenBit[8] = forbiddenBit[8]! | 0x80;
85 expect(simulatorAvcConfig(forbiddenBit)).toBe(forbiddenBit);
86 const unterminated = fallback.slice();
87 unterminated.fill(0, 12, 20);
88 expect(simulatorAvcConfig(unterminated)).toBe(unterminated);
89 const overlong = Uint8Array.from([
90 ...fallback.subarray(0, 6),
91 0,
92 13,
93 39,
94 100,
95 0,
96 51,
97 0,
98 0,
99 3,
100 0,
101 0,
102 3,
103 0,
104 0,
105 0,
106 ...fallback.subarray(20),
107 ]);
108 expect(simulatorAvcConfig(overlong)).toBe(overlong);
109 });
110
111 it("bounds parsing of oversized configurations and SPS data", () => {
112 const oversizedConfig = new Uint8Array(64 * 1024 + 1);
113 oversizedConfig.set(fallback);
114 expect(simulatorAvcConfig(oversizedConfig)).toBe(oversizedConfig);
115 const oversizedSps = new Uint8Array(6 + 2 + 4097 + 7);
116 oversizedSps.set(fallback.subarray(0, 6));
117 oversizedSps.set([0x10, 1], 6);
118 oversizedSps.set(fallback.subarray(8, 20), 8);
119 oversizedSps.set(fallback.subarray(20), 8 + 4097);
120 expect(simulatorAvcConfig(oversizedSps)).toBe(oversizedSps);
121 });
122});