oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1// room-adapt.test.mjs — adaptive video (spec §15.30, 2026-10-28), gated
2// without a browser.
3//
4// node --test test/browser/room-adapt.test.mjs
5//
6// The receiver's watch (adaptStep and its helpers: down fast, up slow, a
7// probe up that fails doubling the wait), what a sender sends at each level
8// (the camera giving way before a shared screen, a screen never paused),
9// and the report's `video` member, lifted out of the shipped room.js by
10// name with the constants they read, on a fake clock. The apps'
11// VideoAdaptTests are the twin of these, number for number.
12import { test } from 'node:test';
13import assert from 'node:assert/strict';
14import { readFileSync } from 'node:fs';
15import { fileURLToPath } from 'node:url';
16
17const src = readFileSync(fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)), 'utf8');
18
19function lift(name) {
20 const start = src.indexOf('function ' + name + '(');
21 assert.notEqual(start, -1, name + ' is not in room.js any more — this test is stale, not passing');
22 let depth = 0;
23 for (let i = src.indexOf('{', start); i < src.length; i++) {
24 if (src[i] === '{') depth += 1;
25 else if (src[i] === '}') {
26 depth -= 1;
27 if (depth === 0) return src.slice(start, i + 1);
28 }
29 }
30 assert.fail('unbalanced braces reading ' + name);
31}
32function liftConst(name) {
33 const m = src.match(new RegExp('const ' + name + ' = ([^;]+);'));
34 assert.ok(m, name + ' is not a const in room.js any more');
35 return 'const ' + name + ' = ' + m[1] + ';';
36}
37const CONSTS = ['ADAPT_LEVELS', 'ADAPT_WINDOW', 'ADAPT_SEVERE_AFTER', 'ADAPT_DOWN_FREEZES', 'ADAPT_DOWN_LOSS',
38 'ADAPT_DOWN_RTT', 'ADAPT_DOWN_JITTER_BUFFER', 'ADAPT_DOWN_DROPPED', 'ADAPT_DOWN_DROPPED_MIN', 'ADAPT_DOWN_FREEZE_SECONDS', 'ADAPT_DOWN_KEYFRAME_ASKS', 'ADAPT_DOWN_LOAD_SECONDS', 'ADAPT_SEVERE_FREEZES',
39 'ADAPT_SEVERE_FREEZE_SECONDS', 'ADAPT_SEVERE_LOSS', 'ADAPT_CLEAN_LOSS', 'ADAPT_CLEAN_RTT', 'ADAPT_CLEAN_JITTER_BUFFER',
40 'ADAPT_UP_AFTER', 'ADAPT_PROBE_WATCH', 'ADAPT_PROBE_JUDGE', 'ADAPT_UP_MAX', 'ADAPT_CAMERA', 'ADAPT_SCREEN',
41 'ADAPT_MIN_NATIVE_VERSION', 'QUALITY_MAX_SECONDS'].map(liftConst).join('\n');
42const NAMES = ['adaptRecord', 'adaptSample', 'loadSample', 'adaptWhy', 'adaptWindow', 'adaptTrouble', 'adaptClean', 'adaptMove', 'adaptStep',
43 'videoFloor', 'adaptAsks', 'askedOf', 'cameraCap', 'cameraAtLevel', 'sharePlan', 'screenAtLevel', 'parseClient',
44 'compareVersions', 'honoursAsks', 'adaptNoticeWords', 'withVideoTime', 'mediaFrom', 'mediaKey', 'mediaPayload'];
45// eslint-disable-next-line no-new-func
46const r = new Function(CONSTS + '\n' + NAMES.map(lift).join('\n') + '; return { ' + NAMES.join(', ') +
47 ', ADAPT_UP_AFTER, ADAPT_UP_MAX };')();
48
49const P1 = 'a'.repeat(32);
50const P2 = 'b'.repeat(32);
51
52// Trouble that is not severe: a freeze every four seconds, two or three in
53// any ten.
54const freezing = (t) => clean({ freezes: t % 4 === 0 ? 1 : 0 });
55
56// One second's sample: clean unless told otherwise.
57const clean = (o = {}) => Object.assign({ freezes: 0, freezeSeconds: 0, packets: 100, lost: 0, rtt: 80,
58 jitterBufferMs: 60, decoded: 30, dropped: 0 }, o);
59
60// Once a second from `from` to `to` seconds (inclusive), each second's
61// sample from `at(t)`; the level after every look, and the seconds it
62// changed at.
63function run(rec, from, to, at) {
64 const changes = [];
65 for (let t = from; t <= to; t++) {
66 const before = rec.level;
67 r.adaptStep(rec, t * 1000, at(t));
68 if (rec.level !== before) changes.push([t, rec.level]);
69 }
70 return changes;
71}
72
73test('the numbers are the approved ones', () => {
74 assert.equal(r.ADAPT_UP_AFTER, 45000);
75 assert.equal(r.ADAPT_UP_MAX, 300000);
76});
77
78test('trouble that lasts steps down one level every ten seconds, to audio only, and no further', () => {
79 const rec = r.adaptRecord();
80 const seen = run(rec, 0, 60, freezing);
81 assert.deepEqual(seen, [[10, 1], [20, 2], [30, 3]]);
82 assert.equal(rec.level, 3);
83 assert.equal(rec.downs, 3);
84});
85
86test('severe trouble steps two levels at once, five seconds in', () => {
87 const rec = r.adaptRecord();
88 assert.deepEqual(run(rec, 0, 12, () => clean({ freezes: 2 })), [[5, 2], [10, 3]]);
89 const lossy = r.adaptRecord();
90 assert.deepEqual(run(lossy, 0, 6, () => clean({ packets: 80, lost: 20 })), [[5, 2]]);
91});
92
93test('a freeze now and then, a little loss, or a slow link alone is not trouble', () => {
94 const rare = r.adaptRecord();
95 assert.deepEqual(run(rare, 0, 60, (t) => clean({ freezes: t % 11 === 0 ? 1 : 0 })), []);
96 const lossy = r.adaptRecord();
97 assert.deepEqual(run(lossy, 0, 60, () => clean({ packets: 97, lost: 3 })), []);
98 const slow = r.adaptRecord();
99 assert.deepEqual(run(slow, 0, 60, () => clean({ rtt: 900 })), []);
100});
101
102test('loss over five percent, a slow link with freezes, a deep jitter buffer and dropped frames each step down', () => {
103 for (const [what, sample] of [
104 ['loss', () => clean({ packets: 94, lost: 6 })],
105 ['one freeze on a 700 ms round trip', (t) => clean({ rtt: 700, freezes: t === 5 ? 1 : 0 })],
106 ['the jitter buffer', () => clean({ jitterBufferMs: 650 })],
107 ['frames dropped', () => clean({ decoded: 20, dropped: 10 })],
108 ['one long freeze', (t) => clean({ freezes: t === 5 ? 1 : 0, freezeSeconds: t === 5 ? 2.5 : 0 })],
109 ['a keyframe asked for every three seconds', (t) => clean({ keyframeAsks: t % 3 === 0 ? 1 : 0 })],
110 ]) {
111 const rec = r.adaptRecord();
112 assert.deepEqual(run(rec, 0, 10, sample), [[10, 1]], what);
113 }
114});
115
116test('the round trip is the window median: one spike is not a slow link', () => {
117 const rec = r.adaptRecord();
118 assert.deepEqual(run(rec, 0, 30, (t) => clean({ rtt: t % 10 === 3 ? 2000 : 100, freezes: t % 10 === 3 ? 1 : 0 })), []);
119});
120
121test('a second with nothing measured is no news either way', () => {
122 const rec = r.adaptRecord();
123 run(rec, 0, 9, freezing);
124 assert.deepEqual(run(rec, 10, 40, () => null), []);
125 assert.equal(rec.level, 0, 'a connection that is down says nothing about the link');
126 assert.equal(r.adaptSample({}, true), null);
127 assert.equal(r.adaptSample({ in: { audio: { packets: 0 } } }, true), null);
128});
129
130test('freezes count only while the camera is watched', () => {
131 const q = { rtt: 50, in: { audio: { packets: 50, lost: 1, jitterBufferMs: 70 }, video: { packets: 90, freezes: 2, freezeSeconds: 1.5, decoded: 30, dropped: 1 },
132 screen: { packets: 10, lost: 1, decoded: 1 } } };
133 assert.deepEqual(r.adaptSample(q, true, 2), { freezes: 2, freezeSeconds: 1.5, keyframeAsks: 2, packets: 150, lost: 2, rtt: 50, jitterBufferMs: 70, decoded: 31, dropped: 1 });
134 const off = r.adaptSample(q, false, 2);
135 assert.equal(off.freezes, 0);
136 assert.equal(off.keyframeAsks, 0);
137 assert.equal(off.freezeSeconds, 0);
138 assert.equal(off.packets, 150);
139});
140
141test('clean seconds step up one level after forty-five, and again after another forty-five', () => {
142 const rec = r.adaptRecord();
143 run(rec, 0, 20, freezing);
144 assert.equal(rec.level, 2);
145 // Clean from 21 s: up at 21 + 45; the probe holds, and the next clean run
146 // starts the second after the step, so up again at 67 + 45.
147 const seen = run(rec, 21, 200, () => clean());
148 assert.deepEqual(seen, [[66, 1], [112, 0]]);
149 assert.equal(rec.upWait, r.ADAPT_UP_AFTER, 'a probe that holds puts the wait back');
150});
151
152test('a keyframe asked for is not a clean second', () => {
153 const rec = r.adaptRecord();
154 run(rec, 0, 10, freezing);
155 const seen = run(rec, 11, 120, (t) => clean({ keyframeAsks: t === 30 ? 1 : 0 }));
156 assert.deepEqual(seen, [[76, 0]]);
157});
158
159test('one bad second resets the clean run', () => {
160 const rec = r.adaptRecord();
161 run(rec, 0, 10, freezing);
162 assert.equal(rec.level, 1);
163 const seen = run(rec, 11, 120, (t) => clean({ lost: t === 40 ? 10 : 0, packets: 100 }));
164 assert.deepEqual(seen, [[86, 0]], 'clean from 41 s, so up at 86 s');
165});
166
167test('a probe that fails within twenty seconds steps straight back and doubles the wait, to five minutes at most', () => {
168 const rec = r.adaptRecord();
169 run(rec, 0, 10, freezing);
170 assert.equal(rec.level, 1);
171 // The link carries Reduced and not Normal: clean at 1, freezing at 0.
172 const at = () => (rec.level === 0 ? clean({ freezes: 1 }) : clean());
173 const seen = run(rec, 11, 1200, at);
174 const ups = seen.filter(([, l]) => l === 0).map(([t]) => t);
175 const backs = seen.filter(([, l]) => l === 1).map(([t]) => t);
176 // Up after 45 s clean, back 5 s later; then 90, 180, 300, 300 … of
177 // clean seconds counted from the second after each step back.
178 assert.deepEqual(ups, [56, 152, 338, 644, 950]);
179 assert.deepEqual(backs, [61, 157, 343, 649, 955]);
180 assert.equal(rec.upWait, r.ADAPT_UP_MAX);
181});
182
183test('trouble after the probe watch is an ordinary step down, and the wait is back to its base', () => {
184 const rec = r.adaptRecord();
185 run(rec, 0, 10, freezing);
186 run(rec, 11, 56, () => clean());
187 assert.equal(rec.level, 0);
188 const seen = run(rec, 57, 100, (t) => (t >= 80 ? freezing(t) : clean()));
189 assert.deepEqual(seen, [[84, 1], [94, 2]], 'freezes at 80 and 84 s: the window is trouble at 84, and ten seconds on');
190 assert.equal(rec.upWait, r.ADAPT_UP_AFTER);
191});
192
193test('the person’s own setting is a floor under the watch, and only levels below normal are asked', () => {
194 const levels = new Map([[P1, 0], [P2, 1]]);
195 assert.deepEqual(r.adaptAsks(levels, 'auto'), { [P2]: 'reduced' });
196 assert.deepEqual(r.adaptAsks(levels, 'low'), { [P1]: 'minimal', [P2]: 'minimal' });
197 assert.deepEqual(r.adaptAsks(new Map([[P1, 3]]), 'low'), { [P1]: 'audio' });
198 assert.deepEqual(r.adaptAsks(levels, 'audio'), { [P1]: 'audio', [P2]: 'audio' });
199 assert.deepEqual(r.adaptAsks(new Map([[P1, 0]]), 'auto'), {});
200 const many = new Map(Array.from({ length: 20 }, (_, i) => [i.toString(16).padStart(32, '0'), 2]));
201 assert.equal(Object.keys(r.adaptAsks(many, 'auto')).length, 15);
202});
203
204test('a sender reads only its own id, and only the wire’s levels', () => {
205 assert.equal(r.askedOf({ video: { [P1]: 'minimal' } }, P1), 2);
206 assert.equal(r.askedOf({ video: { [P1]: 'minimal' } }, P2), 0);
207 assert.equal(r.askedOf({ video: { [P1]: 'tiny' } }, P1), 0);
208 assert.equal(r.askedOf(null, P1), 0);
209 assert.equal(r.askedOf({ muted: false }, P1), 0);
210});
211
212test('the camera at each level, fitted by its long and short sides', () => {
213 assert.deepEqual(r.cameraAtLevel(0, false, 640, 480), { maxFramerate: undefined, active: true, maxBitrate: undefined, scaleResolutionDownBy: 1 });
214 assert.deepEqual(r.cameraAtLevel(1, false, 640, 480), { maxBitrate: 350000, maxFramerate: 15, scaleResolutionDownBy: 480 / 360, active: true });
215 assert.deepEqual(r.cameraAtLevel(1, false, 1280, 720), { maxBitrate: 350000, maxFramerate: 15, scaleResolutionDownBy: 2, active: true });
216 // A portrait camera is boxed the right way round: 360 × 640 at most.
217 assert.equal(r.cameraAtLevel(1, false, 720, 1280).scaleResolutionDownBy, 2);
218 assert.deepEqual(r.cameraAtLevel(2, false, 640, 480), { maxBitrate: 120000, maxFramerate: 8, scaleResolutionDownBy: 480 / 180, active: true });
219 assert.equal(r.cameraAtLevel(3, false, 640, 480).active, false);
220 // Unknown size: no scaling, the rest as the level says.
221 assert.equal(r.cameraAtLevel(2, false, 0, 0).scaleResolutionDownBy, 1);
222});
223
224test('while a screen is shared the camera gives way first', () => {
225 // Normal: §5.4's cap beside the screen.
226 assert.deepEqual(r.cameraAtLevel(0, true, 640, 720), Object.assign({ maxFramerate: undefined, active: true }, r.cameraCap(true, 720)));
227 // Reduced asks the camera for minimal, and minimal for none.
228 assert.equal(r.cameraAtLevel(1, true, 640, 480).maxBitrate, 120000);
229 assert.equal(r.cameraAtLevel(1, true, 640, 480).active, true);
230 assert.equal(r.cameraAtLevel(2, true, 640, 480).active, false);
231 assert.equal(r.cameraAtLevel(3, true, 640, 480).active, false);
232});
233
234test('a screen keeps its size and gives up frame rate first, is made smaller only at minimal, and is never paused', () => {
235 const plan = r.sharePlan('balanced', 1920, 1080);
236 assert.deepEqual(r.screenAtLevel(plan, 0, 1920, 1080), plan);
237 const reduced = r.screenAtLevel(plan, 1, 1920, 1080);
238 assert.equal(reduced.encoding.maxFramerate, 10);
239 assert.equal(reduced.encoding.maxBitrate, 1200000);
240 assert.equal(reduced.encoding.scaleResolutionDownBy, plan.encoding.scaleResolutionDownBy, 'the size is kept');
241 assert.equal(reduced.degradationPreference, 'maintain-resolution');
242 const minimal = r.screenAtLevel(plan, 2, 1920, 1080);
243 assert.equal(minimal.encoding.maxFramerate, 5);
244 assert.equal(minimal.encoding.maxBitrate, 500000);
245 assert.equal(minimal.encoding.scaleResolutionDownBy, 1.5);
246 const audio = r.screenAtLevel(plan, 3, 1920, 1080);
247 assert.deepEqual(audio, minimal, 'audio only sends the screen as minimal does');
248 assert.notEqual(audio.encoding.active, false);
249 // Motion's own smaller cap is kept where it is smaller still.
250 const motion = r.sharePlan('motion', 3840, 2160);
251 assert.equal(r.screenAtLevel(motion, 2, 3840, 2160).encoding.scaleResolutionDownBy, 3840 / 1280);
252 // The plan it was given is not changed under it.
253 assert.equal(plan.encoding.maxFramerate, 30);
254});
255
256test('browsers and new apps act on an ask; an app that does not say, or an old one, does not', () => {
257 assert.equal(r.honoursAsks(''), true);
258 assert.equal(r.honoursAsks('mac 0.4.15'), true);
259 assert.equal(r.honoursAsks('mac 0.4.14'), false);
260 assert.equal(r.honoursAsks('iphone 0.5'), true);
261 assert.equal(r.honoursAsks('mac 0.4.13'), false);
262 assert.equal(r.honoursAsks('native'), false);
263 assert.equal(r.honoursAsks('something else'), false);
264});
265
266test('the sender’s notice names the people, and says audio only only when that is all it is', () => {
267 assert.equal(r.adaptNoticeWords([]), '');
268 assert.equal(r.adaptNoticeWords([{ name: 'David', level: 2 }]), 'Sending reduced video to David (their connection is weak)');
269 assert.equal(r.adaptNoticeWords([{ name: 'David', level: 3 }]), 'Sending only audio to David (their connection is weak)');
270 assert.equal(r.adaptNoticeWords([{ name: 'David', level: 1 }, { name: 'Ana', level: 3 }, { name: 'Cy', level: 2 }]),
271 'Sending reduced video to David, Ana and Cy (their connections are weak)');
272});
273
274test('the report carries the seconds at each level, asked and sent, only when there were any', () => {
275 assert.equal(r.withVideoTime(null, undefined), null);
276 assert.deepEqual(r.withVideoTime(null, { asked: [0, 24.04, 0], sent: [0, 0, 0] }), { askedSeconds: [0, 24, 0] });
277 assert.deepEqual(r.withVideoTime({ noSound: true }, { asked: [0, 0, 0], sent: [5, 0, 1] }), { noSound: true, sentSeconds: [5, 0, 1] });
278 assert.equal(r.withVideoTime(null, { asked: [0, 0, 0], sent: [0, 0, 0] }), null);
279});
280
281test('the media report carries the asks, and reads them back leniently', () => {
282 const mine = { muted: false, cameraOff: false };
283 assert.deepEqual(r.mediaPayload(mine, false, [], false, [], [], { [P1]: 'minimal' }),
284 { muted: false, cameraOff: false, hearing: [], video: { [P1]: 'minimal' } });
285 assert.deepEqual(r.mediaPayload(mine, false, [], false, [], [], {}), { muted: false, cameraOff: false, hearing: [] });
286 assert.deepEqual(r.mediaPayload(mine, false, [], true, [], [], { [P1]: 'minimal' }), mine, 'an old server gets the two keys');
287 const back = r.mediaFrom({ muted: false, cameraOff: false, video: { [P1]: 'audio', [P2]: 'tiny', short: 'minimal' } });
288 assert.deepEqual(back.video, { [P1]: 'audio' });
289 assert.equal(r.mediaFrom({ muted: false, cameraOff: false, video: [P1] }).video, undefined);
290 // The same asks in another order are the same report; a level changed is not.
291 const a = r.mediaKey({ muted: false, cameraOff: false, video: { [P1]: 'audio', [P2]: 'reduced' } });
292 const b = r.mediaKey({ muted: false, cameraOff: false, video: { [P2]: 'reduced', [P1]: 'audio' } });
293 const c = r.mediaKey({ muted: false, cameraOff: false, video: { [P2]: 'minimal', [P1]: 'audio' } });
294 assert.equal(a, b);
295 assert.notEqual(a, c);
296 assert.notEqual(r.mediaKey({ muted: false, cameraOff: false }), a);
297});
298
299test('the sender’s own watch: this device struggling for 8 of 10 seconds lowers its camera, and calm brings it back', () => {
300 const rec = r.adaptRecord();
301 // Struggling every second: a level every ten, never severe.
302 assert.deepEqual(run(rec, 0, 35, () => r.loadSample(true)), [[10, 1], [20, 2], [30, 3]]);
303 // Calm: up after forty-five seconds, as any watch.
304 const calm = r.adaptRecord();
305 run(calm, 0, 10, () => r.loadSample(true));
306 assert.equal(calm.level, 1);
307 assert.deepEqual(run(calm, 11, 60, () => r.loadSample(false)), [[56, 0]]);
308 // Seven seconds in ten is not sustained.
309 const brief = r.adaptRecord();
310 assert.deepEqual(run(brief, 0, 60, (t) => r.loadSample(t % 10 < 7)), []);
311 // A struggling second is not a clean one.
312 assert.equal(r.adaptSample({ in: {} }, true), null);
313 const one = r.loadSample(true);
314 assert.equal(one.load, true);
315});
316