| 1 | // room-quality.test.mjs — the anonymous quality report and "Report a |
| 2 | // problem" (spec §15.26, 2026-10-03), gated without a browser. |
| 3 | // |
| 4 | // node --test test/browser/room-quality.test.mjs |
| 5 | // |
| 6 | // §15.29 (2026-10-28) adds the window's round trip and jitter, the |
| 7 | // candidates, the encoder and decoder, the codec, the limit that held |
| 8 | // longest, and the automatic report's rules and gate: the same lifting, |
| 9 | // and a relayed walk whose candidates, implementation names and |
| 10 | // addresses the report must reduce to types, counts and enums. |
| 11 | // |
| 12 | // The report's builders are lifted out of the shipped room.js by name, |
| 13 | // with the constants they read, as room-reconnect.test.mjs does; the |
| 14 | // client's description and the sheet's answers out of report.js. The |
| 15 | // fixtures are getStats walks as Chromium writes them, two windows |
| 16 | // apart, with the fields a report must never carry — candidate |
| 17 | // addresses and ports — present in them, so their absence from the |
| 18 | // report is a finding and not an accident of the fixture. |
| 19 | import { test } from 'node:test'; |
| 20 | import assert from 'node:assert/strict'; |
| 21 | import { readFileSync } from 'node:fs'; |
| 22 | import { fileURLToPath } from 'node:url'; |
| 23 | |
| 24 | const roomSrc = readFileSync(fileURLToPath(new URL('../../internal/calls/static/room.js', import.meta.url)), 'utf8'); |
| 25 | const reportSrc = readFileSync(fileURLToPath(new URL('../../internal/calls/static/report.js', import.meta.url)), 'utf8'); |
| 26 | |
| 27 | function lift(src, file, name) { |
| 28 | const start = src.indexOf('function ' + name + '('); |
| 29 | assert.notEqual(start, -1, name + ' is not in ' + file + ' any more — this test is stale, not passing'); |
| 30 | let depth = 0; |
| 31 | for (let i = src.indexOf('{', start); i < src.length; i++) { |
| 32 | if (src[i] === '{') depth += 1; |
| 33 | else if (src[i] === '}') { |
| 34 | depth -= 1; |
| 35 | if (depth === 0) return src.slice(start, i + 1); |
| 36 | } |
| 37 | } |
| 38 | assert.fail('unbalanced braces reading ' + name); |
| 39 | } |
| 40 | function liftConst(name) { |
| 41 | const m = roomSrc.match(new RegExp('const ' + name + ' = ([0-9.e]+);')); |
| 42 | assert.ok(m, name + ' is not a numeric const in room.js any more'); |
| 43 | return 'const ' + name + ' = ' + m[1] + ';'; |
| 44 | } |
| 45 | |
| 46 | const CONSTS = ['QUALITY_MAX_PEERS', 'QUALITY_MAX_SECONDS', 'QUALITY_MAX_MILLIS', 'QUALITY_MAX_COUNT', |
| 47 | 'QUALITY_MAX_KBPS', 'QUALITY_MAX_PIXELS', 'QUALITY_MAX_FPS', 'QUALITY_LOOK_MAX', |
| 48 | 'AUTO_FREEZES', 'AUTO_RTT_MS', 'AUTO_RECONNECTING_SECONDS', 'AUTO_REPORTS_PER_CALL', 'AUTO_REPORT_EVERY', |
| 49 | 'AUTO_BODY_MAX'].map(liftConst).join('\n'); |
| 50 | const ROOM = ['qualityCeiling', 'qualitySet', 'qualityReading', 'qualityPeer', 'qualityWatchRecord', |
| 51 | 'qualityWatch', 'qualityTake', 'qualityReport', 'deviceKind', 'coderKind', 'codecKind', 'qualitySample', |
| 52 | 'qualityFacts', 'qualityTrouble', 'autoReportGate', 'autoReportAllowed', 'autoReportDecision', 'autoReportBody']; |
| 53 | // eslint-disable-next-line no-new-func |
| 54 | const q = new Function(CONSTS + '\n' + ROOM.map((n) => lift(roomSrc, 'room.js', n)).join('\n') + |
| 55 | '; return { ' + ROOM.join(', ') + ' };')(); |
| 56 | const REPORT = ['reportClient', 'problemAnswer', 'problemBody']; |
| 57 | // eslint-disable-next-line no-new-func |
| 58 | const r = new Function(REPORT.map((n) => lift(reportSrc, 'report.js', n)).join('\n') + |
| 59 | '; return { ' + REPORT.join(', ') + ' };')(); |
| 60 | |
| 61 | // The bounds must be protocol/quality.go's, or the server refuses what |
| 62 | // this clamps to. |
| 63 | test('the bounds are the protocol package\'s', () => { |
| 64 | const go = readFileSync(fileURLToPath(new URL('../../protocol/quality.go', import.meta.url)), 'utf8'); |
| 65 | const bound = (name) => { |
| 66 | const m = go.match(new RegExp(name + '\\s*=\\s*([0-9_.e]+)(?:\\s*<<\\s*([0-9]+))?')); |
| 67 | assert.ok(m, name + ' is not in protocol/quality.go'); |
| 68 | return Number(m[1].replace(/_/g, '')) * 2 ** Number(m[2] || 0); |
| 69 | }; |
| 70 | const room = (name) => Number(liftConst(name).match(/= ([0-9.e]+);/)[1]); |
| 71 | assert.equal(room('QUALITY_MAX_PEERS'), bound('MaxQualityPeers')); |
| 72 | assert.equal(room('QUALITY_MAX_SECONDS'), bound('MaxQualitySeconds')); |
| 73 | assert.equal(room('QUALITY_MAX_MILLIS'), bound('MaxQualityMillis')); |
| 74 | assert.equal(room('QUALITY_MAX_COUNT'), bound('MaxQualityCount')); |
| 75 | assert.equal(room('QUALITY_MAX_KBPS'), bound('MaxQualityKbps')); |
| 76 | assert.equal(room('QUALITY_MAX_PIXELS'), bound('MaxQualityPixels')); |
| 77 | assert.equal(room('QUALITY_MAX_FPS'), bound('MaxQualityFPS')); |
| 78 | assert.equal(room('QUALITY_MAX_BODY'), bound('MaxQualityBody')); |
| 79 | // §15.29: the automatic report's rules and limits. |
| 80 | assert.equal(room('AUTO_FREEZES'), bound('AutoFreezes')); |
| 81 | assert.equal(room('AUTO_RTT_MS'), bound('AutoRTTMillis')); |
| 82 | assert.equal(room('AUTO_RECONNECTING_SECONDS'), bound('AutoReconnectingSeconds')); |
| 83 | assert.equal(room('AUTO_REPORTS_PER_CALL'), bound('AutoReportsPerCall')); |
| 84 | assert.equal(room('AUTO_REPORT_EVERY'), bound('AutoReportEveryMinutes') * 60000); |
| 85 | assert.ok(room('AUTO_BODY_MAX') <= bound('MaxProblemBody')); |
| 86 | // And the triggers, in the order the protocol says a client checks them. |
| 87 | const triggers = go.match(/AutoTriggers = \[\]string\{([^}]*)\}/); |
| 88 | assert.ok(triggers, 'AutoTriggers is not in protocol/quality.go'); |
| 89 | assert.deepEqual(JSON.parse('[' + triggers[1] + ']'), ['freezes', 'rtt', 'reconnecting', 'screen']); |
| 90 | }); |
| 91 | |
| 92 | // ── fixtures ───────────────────────────────────────────────────────── |
| 93 | |
| 94 | const PEER = 'a1b2c3d4e5f60718293a4b5c6d7e8f90'; |
| 95 | const OTHER = '0f1e2d3c4b5a69788796a5b4c3d2e1f0'; |
| 96 | |
| 97 | // One connection's walk, Chromium's shape. `t` scales the counters, so |
| 98 | // window two is window one plus what happened in thirty seconds. |
| 99 | function walk(t) { |
| 100 | const reports = [ |
| 101 | { id: 'OA', type: 'outbound-rtp', kind: 'audio', mid: '0', ssrc: 11, bytesSent: 10000 + t * 4000, packetsSent: 100 + t * 50 }, |
| 102 | { id: 'OV', type: 'outbound-rtp', kind: 'video', mid: '1', ssrc: 12, bytesSent: 500000 + t * 100000, packetsSent: 1000 + t * 300, |
| 103 | frameWidth: 640, frameHeight: 480, framesPerSecond: 29.97, qualityLimitationReason: t ? 'bandwidth' : 'none' }, |
| 104 | { id: 'RIA', type: 'remote-inbound-rtp', kind: 'audio', localId: 'OA', ssrc: 11, packetsLost: 2 + t * 3, roundTripTime: 0.0423, jitter: 0.0021 }, |
| 105 | { id: 'RIV', type: 'remote-inbound-rtp', kind: 'video', localId: 'OV', ssrc: 12, packetsLost: 5 + t * 7, roundTripTime: 0.05, jitter: 0.004 }, |
| 106 | { id: 'IA', type: 'inbound-rtp', kind: 'audio', mid: '0', ssrc: 21, bytesReceived: 20000 + t * 3750, packetsReceived: 200 + t * 1500, |
| 107 | packetsLost: 1 + t * 15, jitter: 0.0031, totalSamplesReceived: 48000 + t * 1440000, concealedSamples: 100 + t * 4800, |
| 108 | insertedSamplesForDeceleration: 10 + t * 20, removedSamplesForAcceleration: 5 + t * 30, |
| 109 | jitterBufferDelay: 2880 + t * 86400, jitterBufferEmittedCount: 48000 + t * 1440000 }, |
| 110 | { id: 'IV', type: 'inbound-rtp', kind: 'video', mid: '1', ssrc: 22, bytesReceived: 400000 + t * 3000000, packetsReceived: 900 + t * 3000, |
| 111 | packetsLost: 0 + t * 30, jitter: 0.012, freezeCount: 1 + t * 2, totalFreezesDuration: 0.5 + t * 1.25, framesDecoded: 300 + t * 900, |
| 112 | framesDropped: 2 + t * 4, frameWidth: 1280, frameHeight: 720, framesPerSecond: 30 }, |
| 113 | { id: 'T', type: 'transport', selectedCandidatePairId: 'CP' }, |
| 114 | { id: 'CP', type: 'candidate-pair', localCandidateId: 'L', remoteCandidateId: 'R', state: 'succeeded', nominated: true, |
| 115 | currentRoundTripTime: 0.031 }, |
| 116 | { id: 'L', type: 'local-candidate', candidateType: 'relay', relayProtocol: 'tls', protocol: 'udp', address: '192.0.2.44', port: 54321, |
| 117 | ip: '192.0.2.44', url: 'turns:relay.example.net:443' }, |
| 118 | { id: 'R', type: 'remote-candidate', candidateType: 'srflx', protocol: 'udp', address: '198.51.100.7', port: 61000, ip: '198.51.100.7' }, |
| 119 | ]; |
| 120 | if (t) { |
| 121 | // A screen arriving in window two only: new streams count from zero. |
| 122 | reports.push({ id: 'IS', type: 'inbound-rtp', kind: 'video', mid: '3', ssrc: 23, bytesReceived: 750000, packetsReceived: 800, |
| 123 | packetsLost: 4, jitter: 0.02, freezeCount: 3, totalFreezesDuration: 6, framesDecoded: 150, framesDropped: 9, |
| 124 | frameWidth: 1920, frameHeight: 1080, framesPerSecond: 5 }); |
| 125 | } |
| 126 | return new Map(reports.map((x) => [x.id, x])); |
| 127 | } |
| 128 | const classify = (x) => (x.kind === 'audio' ? 'audio' : x.mid === '3' ? 'screen' : 'video'); |
| 129 | const FORBIDDEN = ['192.0.2.44', '198.51.100.7', '54321', '61000', 'relay.example.net', 'address', 'port', '"ip"', 'url']; |
| 130 | |
| 131 | // ── the report ─────────────────────────────────────────────────────── |
| 132 | |
| 133 | test('two windows: deltas, rates over the window, gauges as they stand', () => { |
| 134 | const one = q.qualityReading(walk(0), classify); |
| 135 | const two = q.qualityReading(walk(1), classify); |
| 136 | const p = q.qualityPeer(one, two, 30); |
| 137 | |
| 138 | assert.deepEqual(p.path, { local: 'relay', remote: 'srflx', protocol: 'tls' }); |
| 139 | assert.equal(p.rtt, 31); |
| 140 | |
| 141 | assert.deepEqual(p.out.audio, { kbps: 1.1, packets: 50, lost: 3, rtt: 42.3, jitter: 2.1 }); |
| 142 | assert.deepEqual(p.out.video, { kbps: 26.7, packets: 300, width: 640, height: 480, fps: 30, limit: 'bandwidth', lost: 7, rtt: 50, jitter: 4 }); |
| 143 | |
| 144 | assert.deepEqual(p.in.audio, { |
| 145 | kbps: 1, packets: 1500, lost: 15, jitter: 3.1, samples: 1440000, concealed: 4800, inserted: 20, removed: 30, |
| 146 | // 86,400 s of buffer delay summed over 1,440,000 emitted samples: 60 ms each. |
| 147 | jitterBufferMs: 60, |
| 148 | }); |
| 149 | assert.deepEqual(p.in.video, { |
| 150 | kbps: 800, packets: 3000, lost: 30, jitter: 12, freezes: 2, freezeSeconds: 1.3, decoded: 900, dropped: 4, |
| 151 | width: 1280, height: 720, fps: 30, |
| 152 | }); |
| 153 | // The screen was new in window two: everything it has counted is this |
| 154 | // window's. |
| 155 | assert.deepEqual(p.in.screen, { |
| 156 | kbps: 200, packets: 800, lost: 4, jitter: 20, freezes: 3, freezeSeconds: 6, decoded: 150, dropped: 9, |
| 157 | width: 1920, height: 1080, fps: 5, |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | test('the first window counts from zero, and a counter that goes back counts nothing', () => { |
| 162 | const first = q.qualityPeer(null, q.qualityReading(walk(0), classify), 10); |
| 163 | assert.equal(first.out.audio.packets, 100); |
| 164 | assert.equal(first.in.video.decoded, 300); |
| 165 | const back = q.qualityPeer(q.qualityReading(walk(1), classify), q.qualityReading(walk(0), classify), 30); |
| 166 | assert.equal(back.out.audio.packets, 0); |
| 167 | assert.equal(back.in.audio.lost, 0); |
| 168 | }); |
| 169 | |
| 170 | test('never an address, a port or a URL, whatever the walk carries', () => { |
| 171 | const report = q.qualityReport({ |
| 172 | seconds: 30, |
| 173 | client: { app: 'web', browser: 'chrome' }, |
| 174 | devices: { input: 'bluetooth', output: 'built-in' }, |
| 175 | peers: [{ id: PEER, prev: q.qualityReading(walk(0), classify), now: q.qualityReading(walk(1), classify) }], |
| 176 | }); |
| 177 | const json = JSON.stringify(report); |
| 178 | for (const bad of FORBIDDEN) assert.ok(!json.includes(bad), 'the report carries ' + bad + ': ' + json); |
| 179 | assert.equal(report.v, 1); |
| 180 | assert.equal(report.seconds, 30); |
| 181 | assert.equal(report.peers[0].peer, PEER); |
| 182 | assert.equal(report.final, undefined); |
| 183 | }); |
| 184 | |
| 185 | test('clamped to the protocol\'s ceilings; NaN, infinite and negative left out', () => { |
| 186 | const now = { |
| 187 | streams: new Map([ |
| 188 | ['in:x', { dir: 'in', kind: 'video', counters: { bytesReceived: 1e12, packetsReceived: 5e9, packetsLost: -3, framesDecoded: NaN }, |
| 189 | gauges: { jitter: -0.5, frameWidth: 99999, frameHeight: Infinity, framesPerSecond: 1000 } }], |
| 190 | ['remote:y', { dir: 'remote', kind: 'audio', counters: { packetsLost: 2 }, gauges: { roundTripTime: 1e6, jitter: NaN } }], |
| 191 | ]), |
| 192 | path: { local: 'host', remote: 'host', protocol: 'udp', rtt: -1 }, |
| 193 | }; |
| 194 | const p = q.qualityPeer(null, now, 1); |
| 195 | assert.deepEqual(p.in.video, { kbps: 1e6, packets: 1e9, lost: 0, width: 16384, fps: 240 }); |
| 196 | assert.deepEqual(p.out.audio, { lost: 2, rtt: 600000 }); |
| 197 | assert.equal(p.rtt, undefined); |
| 198 | const report = q.qualityReport({ seconds: 5000, client: { app: 'web' }, peers: [] }); |
| 199 | assert.equal(report.seconds, 600); |
| 200 | assert.equal(report.peers, undefined); |
| 201 | }); |
| 202 | |
| 203 | test('an unknown candidate type, an unknown transport, and Firefox\'s selected pair', () => { |
| 204 | const ff = new Map([ |
| 205 | ['P', { id: 'P', type: 'candidate-pair', selected: true, localCandidateId: 'L', remoteCandidateId: 'R' }], |
| 206 | ['L', { id: 'L', type: 'local-candidate', candidateType: 'weird', protocol: 'sctp', address: '10.0.0.1' }], |
| 207 | ['R', { id: 'R', type: 'remote-candidate', candidateType: 'prflx', protocol: 'tcp' }], |
| 208 | ]); |
| 209 | const p = q.qualityPeer(null, q.qualityReading(ff, classify), 30); |
| 210 | assert.deepEqual(p.path, { local: 'unknown', remote: 'prflx', protocol: 'unknown' }); |
| 211 | const tcpRelay = new Map([ |
| 212 | ['T', { id: 'T', type: 'transport', selectedCandidatePairId: 'P' }], |
| 213 | ['P', { id: 'P', type: 'candidate-pair', localCandidateId: 'L', remoteCandidateId: 'R' }], |
| 214 | ['L', { id: 'L', type: 'local-candidate', candidateType: 'host', protocol: 'tcp' }], |
| 215 | ['R', { id: 'R', type: 'remote-candidate', candidateType: 'host' }], |
| 216 | ]); |
| 217 | assert.equal(q.qualityPeer(null, q.qualityReading(tcpRelay, classify), 30).path.protocol, 'tcp'); |
| 218 | }); |
| 219 | |
| 220 | test('at most four peers, each a well-formed peer id, none with nothing to say', () => { |
| 221 | const now = q.qualityReading(walk(0), classify); |
| 222 | const ids = ['1', '2', '3', '4', '5', '6'].map((d) => d.repeat(32)); |
| 223 | const report = q.qualityReport({ |
| 224 | seconds: 30, |
| 225 | client: { app: 'web' }, |
| 226 | peers: [ |
| 227 | { id: 'not a peer id', prev: null, now }, |
| 228 | { id: OTHER, prev: null, now: { streams: new Map(), path: null } }, |
| 229 | ...ids.map((id) => ({ id, prev: null, now })), |
| 230 | ], |
| 231 | }); |
| 232 | assert.deepEqual(report.peers.map((p) => p.peer), ids.slice(0, 4)); |
| 233 | }); |
| 234 | |
| 235 | test('the screen ladder and the mending by person, and only what happened', () => { |
| 236 | const now = q.qualityReading(walk(1), classify); |
| 237 | const report = q.qualityReport({ |
| 238 | seconds: 30, |
| 239 | final: true, |
| 240 | client: { app: 'web' }, |
| 241 | events: { reconnecting: 1, reconnectingSeconds: 4.2 }, |
| 242 | peers: [{ id: PEER, prev: null, now, ladder: { keyframes: 3, rebuilds: 1, reshares: 0, failures: 0 }, reattaches: 1, |
| 243 | events: { cantHearSeconds: 2.5, noSoundFromUs: true } }], |
| 244 | }); |
| 245 | assert.equal(report.final, true); |
| 246 | assert.deepEqual(report.events, { reconnecting: 1, reconnectingSeconds: 4.2 }); |
| 247 | const p = report.peers[0]; |
| 248 | assert.deepEqual(p.out.screen, { keyframes: 3, rebuilds: 1 }); |
| 249 | assert.equal(p.in.screen.reattaches, 1); |
| 250 | assert.deepEqual(p.events, { cantHearSeconds: 2.5, noSoundFromUs: true }); |
| 251 | }); |
| 252 | |
| 253 | test('the screen stalls judged here, with what the statistics said, and steps held back as a loop', () => { |
| 254 | const now = q.qualityReading(walk(1), classify); |
| 255 | const report = q.qualityReport({ |
| 256 | seconds: 30, |
| 257 | client: { app: 'web' }, |
| 258 | peers: [{ id: PEER, prev: null, now, ladder: { keyframes: 1, rebuilds: 0, reshares: 0, failures: 1, damped: 1 }, |
| 259 | reattaches: 0, stalls: { stalls: 2, noPackets: 2, undecoded: 0 } }], |
| 260 | }); |
| 261 | const p = report.peers[0]; |
| 262 | assert.deepEqual(p.out.screen, { keyframes: 1, failures: 1, damped: 1 }); |
| 263 | assert.deepEqual(p.in.screen, Object.assign({}, p.in.screen, { stalls: 2, stallsNoPackets: 2 })); |
| 264 | assert.equal(p.in.screen.stallsUndecoded, undefined, 'none is left out'); |
| 265 | assert.equal(p.in.screen.reattaches, undefined); |
| 266 | }); |
| 267 | |
| 268 | // ── §15.29: the window's detail ────────────────────────────────────── |
| 269 | |
| 270 | // A relayed connection, Chromium's shape, with what §15.29 reads: the |
| 271 | // encoder's and decoder's implementation names (never reported), the |
| 272 | // codecs, qualityLimitationDurations, the pair's round-trip totals, and |
| 273 | // every candidate this side gathered and was offered, addresses, ports |
| 274 | // and the relay's URL included. This side found its own public address |
| 275 | // (srflx) and nothing direct worked: the pair that carries the call is |
| 276 | // this side's relay, reached over TURN/TLS, to the far side's srflx. |
| 277 | // `opts.hostPair` adds a host pair that succeeded; `opts.relayUdp` reaches |
| 278 | // the relay over UDP instead. |
| 279 | function relayWalk(t, opts = {}) { |
| 280 | const reports = [ |
| 281 | { id: 'COA', type: 'codec', mimeType: 'audio/opus', payloadType: 111, clockRate: 48000, sdpFmtpLine: 'minptime=10;useinbandfec=1' }, |
| 282 | { id: 'COV', type: 'codec', mimeType: 'video/H264', payloadType: 102, sdpFmtpLine: 'profile-level-id=42e01f' }, |
| 283 | { id: 'COS', type: 'codec', mimeType: 'video/VP9', payloadType: 98 }, |
| 284 | { id: 'CIV', type: 'codec', mimeType: 'video/AV1', payloadType: 45 }, |
| 285 | { id: 'OA', type: 'outbound-rtp', kind: 'audio', mid: '0', ssrc: 11, codecId: 'COA', bytesSent: 10000 + t * 4000, packetsSent: 100 + t * 50 }, |
| 286 | { id: 'OV', type: 'outbound-rtp', kind: 'video', mid: '1', ssrc: 12, codecId: 'COV', bytesSent: 500000 + t * 100000, |
| 287 | packetsSent: 1000 + t * 300, frameWidth: 640, frameHeight: 360, framesPerSecond: 30, |
| 288 | // The gauge says "none" at the moment the window ends; for 21 of the |
| 289 | // window's 30 seconds it was the bandwidth. |
| 290 | qualityLimitationReason: 'none', qualityLimitationDurations: { none: 10 + t * 5, cpu: 1 + t * 4, bandwidth: 2 + t * 21, other: 0 }, |
| 291 | encoderImplementation: 'ExternalEncoder (VideoToolbox)', powerEfficientEncoder: true }, |
| 292 | { id: 'OS', type: 'outbound-rtp', kind: 'video', mid: '3', ssrc: 13, codecId: 'COS', bytesSent: 90000 + t * 60000, |
| 293 | packetsSent: 200 + t * 100, frameWidth: 1920, frameHeight: 1080, framesPerSecond: 5, qualityLimitationReason: 'cpu', |
| 294 | // A tie: fifteen seconds each of cpu and bandwidth. Bandwidth is named. |
| 295 | qualityLimitationDurations: { none: 3, cpu: t * 15, bandwidth: t * 15, other: 0 }, encoderImplementation: 'libvpx' }, |
| 296 | { id: 'IA', type: 'inbound-rtp', kind: 'audio', mid: '0', ssrc: 21, codecId: 'COA', bytesReceived: 20000 + t * 3750, |
| 297 | packetsReceived: 200 + t * 1500, packetsLost: 1 + t * 15, jitter: 0.0031 }, |
| 298 | { id: 'IV', type: 'inbound-rtp', kind: 'video', mid: '1', ssrc: 22, codecId: 'CIV', bytesReceived: 400000 + t * 3000000, |
| 299 | packetsReceived: 900 + t * 3000, packetsLost: 0, jitter: 0.012, freezeCount: 1 + t * 3, totalFreezesDuration: 0.5 + t, |
| 300 | framesDecoded: 300 + t * 900, framesDropped: 0, frameWidth: 1280, frameHeight: 720, framesPerSecond: 30, |
| 301 | decoderImplementation: 'ExternalDecoder' }, |
| 302 | { id: 'T', type: 'transport', selectedCandidatePairId: 'CP3' }, |
| 303 | // The selected pair: this side's relay to the far side's srflx. |
| 304 | { id: 'CP3', type: 'candidate-pair', localCandidateId: 'L3', remoteCandidateId: 'R1', state: 'succeeded', nominated: true, |
| 305 | currentRoundTripTime: 0.12, totalRoundTripTime: 3 + t * 4.5, responsesReceived: 30 + t * 30 }, |
| 306 | { id: 'CP1', type: 'candidate-pair', localCandidateId: 'L1', remoteCandidateId: 'R2', state: 'failed' }, |
| 307 | { id: 'CP2', type: 'candidate-pair', localCandidateId: 'L2', remoteCandidateId: 'R1', state: 'in-progress' }, |
| 308 | { id: 'L1', type: 'local-candidate', candidateType: 'host', protocol: 'udp', address: '192.168.1.20', port: 50000, ip: '192.168.1.20' }, |
| 309 | { id: 'L2', type: 'local-candidate', candidateType: 'srflx', protocol: 'udp', address: '203.0.113.9', port: 50001, ip: '203.0.113.9', |
| 310 | url: 'stun:stun.example.net:3478' }, |
| 311 | { id: 'L3', type: 'local-candidate', candidateType: 'relay', protocol: 'udp', relayProtocol: opts.relayUdp ? 'udp' : 'tls', |
| 312 | address: '192.0.2.44', port: 54321, ip: '192.0.2.44', url: 'turns:relay.example.net:443' }, |
| 313 | { id: 'R1', type: 'remote-candidate', candidateType: 'srflx', protocol: 'udp', address: '198.51.100.7', port: 61000, ip: '198.51.100.7' }, |
| 314 | { id: 'R2', type: 'remote-candidate', candidateType: 'host', protocol: 'udp', address: '10.1.2.3', port: 61001, ip: '10.1.2.3' }, |
| 315 | ]; |
| 316 | if (opts.hostPair) { |
| 317 | reports.push({ id: 'CP4', type: 'candidate-pair', localCandidateId: 'L1', remoteCandidateId: 'R2', state: 'succeeded' }); |
| 318 | } |
| 319 | return new Map(reports.map((x) => [x.id, x])); |
| 320 | } |
| 321 | const RELAY_FORBIDDEN = [...FORBIDDEN, '192.168.1.20', '203.0.113.9', '10.1.2.3', '50000', '50001', '61001', 'stun.example.net', |
| 322 | 'ExternalEncoder', 'VideoToolbox', 'libvpx', 'ExternalDecoder', 'video/', 'audio/', 'profile-level-id', 'useinbandfec', 'CP3', 'L3']; |
| 323 | |
| 324 | // A window's once-a-second looks: round trips of 100, 900 and 120 ms (one |
| 325 | // bad second), and audio jitter of 10 and 30 ms. |
| 326 | function sampled() { |
| 327 | let acc = null; |
| 328 | const look = (rtt, jitter) => ({ |
| 329 | path: { local: 'relay', remote: 'srflx', protocol: 'tls', rtt }, |
| 330 | streams: new Map([['inbound-rtp:IA', { dir: 'in', kind: 'audio', counters: {}, gauges: { jitter } }]]), |
| 331 | }); |
| 332 | acc = q.qualitySample(acc, look(100, 0.01)); |
| 333 | acc = q.qualitySample(acc, look(900, 0.03)); |
| 334 | acc = q.qualitySample(acc, look(120, undefined)); |
| 335 | return acc; |
| 336 | } |
| 337 | |
| 338 | test('§15.29: a relayed call says why — candidates by type, which pairs worked, the path — and never an address', () => { |
| 339 | const one = q.qualityReading(relayWalk(0), classify); |
| 340 | const two = q.qualityReading(relayWalk(1), classify); |
| 341 | const report = q.qualityReport({ |
| 342 | seconds: 30, |
| 343 | client: { app: 'web', browser: 'chrome' }, |
| 344 | shareMode: 'detail', |
| 345 | peers: [{ id: PEER, prev: one, now: two, sample: sampled() }], |
| 346 | }); |
| 347 | const json = JSON.stringify(report); |
| 348 | for (const bad of RELAY_FORBIDDEN) assert.ok(!json.includes(bad), 'the report carries ' + bad + ': ' + json); |
| 349 | const p = report.peers[0]; |
| 350 | assert.deepEqual(p.path, { local: 'relay', remote: 'srflx', protocol: 'tls' }); |
| 351 | // Found its own public address, offered the far side's host and srflx, |
| 352 | // and nothing but the relay worked; the relay is reached over TLS. |
| 353 | assert.deepEqual(p.candidates, { host: 1, srflx: 1, relay: 1, remoteHost: 1, remoteSrflx: 1, |
| 354 | directOk: false, localDirectOk: false, udpOk: false }); |
| 355 | |
| 356 | // The same, with the relay over UDP: UDP the whole way to the relay. |
| 357 | const udp = q.qualityPeer(null, q.qualityReading(relayWalk(1, { relayUdp: true }), classify), 30); |
| 358 | assert.deepEqual(udp.path, { local: 'relay', remote: 'srflx', protocol: 'udp' }); |
| 359 | assert.equal(udp.candidates.udpOk, true); |
| 360 | assert.equal(udp.candidates.directOk, false); |
| 361 | // And a host pair that succeeded while the relay was chosen: direct |
| 362 | // worked, from this side's own network. |
| 363 | const direct = q.qualityPeer(null, q.qualityReading(relayWalk(1, { hostPair: true }), classify), 30); |
| 364 | assert.deepEqual([direct.candidates.directOk, direct.candidates.localDirectOk, direct.candidates.udpOk], [true, true, true]); |
| 365 | }); |
| 366 | |
| 367 | test('§15.29: this side\'s own end worked to the far side\'s relay — localDirectOk without directOk', () => { |
| 368 | const walk = new Map([ |
| 369 | ['T', { id: 'T', type: 'transport', selectedCandidatePairId: 'P' }], |
| 370 | ['P', { id: 'P', type: 'candidate-pair', localCandidateId: 'L', remoteCandidateId: 'R', state: 'succeeded' }], |
| 371 | ['L', { id: 'L', type: 'local-candidate', candidateType: 'host', protocol: 'udp', address: '10.0.0.2' }], |
| 372 | ['R', { id: 'R', type: 'remote-candidate', candidateType: 'relay', protocol: 'udp', address: '192.0.2.99' }], |
| 373 | ]); |
| 374 | const p = q.qualityPeer(null, q.qualityReading(walk, classify), 30); |
| 375 | assert.deepEqual(p.candidates, { host: 1, remoteRelay: 1, directOk: false, localDirectOk: true, udpOk: true }); |
| 376 | assert.deepEqual(p.path, { local: 'host', remote: 'relay', protocol: 'udp' }); |
| 377 | // A walk with no candidates at all says nothing about them, rather than |
| 378 | // claiming nothing worked. |
| 379 | const none = q.qualityPeer(null, q.qualityReading(new Map([['T', { id: 'T', type: 'transport' }]]), classify), 30); |
| 380 | assert.equal(none.candidates, undefined); |
| 381 | }); |
| 382 | |
| 383 | test('§15.29: the window\'s round trip — the pair\'s own mean, the highest second — and its jitter', () => { |
| 384 | const one = q.qualityReading(relayWalk(0), classify); |
| 385 | const two = q.qualityReading(relayWalk(1), classify); |
| 386 | const p = q.qualityPeer(one, two, 30, sampled()); |
| 387 | // 4.5 s of round trip over thirty responses: 150 ms each, whatever the |
| 388 | // once-a-second readings averaged. |
| 389 | assert.equal(p.rttAvg, 150); |
| 390 | // The bad second, which the last walk's 120 ms would have hidden. |
| 391 | assert.equal(p.rtt, 120); |
| 392 | assert.equal(p.rttMax, 900); |
| 393 | assert.equal(p.jitterAvg, 20); |
| 394 | assert.equal(p.jitterMax, 30); |
| 395 | |
| 396 | // The first window counts the pair's totals from zero. |
| 397 | assert.equal(q.qualityPeer(null, two, 30, null).rttAvg, (7.5 / 60) * 1000); |
| 398 | // A new pair counts from zero too: last window's totals were another |
| 399 | // pair's. |
| 400 | const other = q.qualityReading(relayWalk(0), classify); |
| 401 | other.path.pair = 'CPold'; |
| 402 | other.path.totalRoundTripTime = 100; |
| 403 | other.path.responsesReceived = 1000; |
| 404 | assert.equal(q.qualityPeer(other, two, 30, null).rttAvg, 125); |
| 405 | // No totals (Safari, Firefox): the mean of the readings. |
| 406 | const bare = q.qualityReading(walk(1), classify); |
| 407 | const fromSamples = q.qualityPeer(null, bare, 30, sampled()); |
| 408 | assert.equal(fromSamples.rttAvg, 373.3); |
| 409 | assert.equal(fromSamples.rttMax, 900); |
| 410 | // No readings and no totals: the highest is the last walk's, and there |
| 411 | // is no mean and no jitter to give. |
| 412 | const alone = q.qualityPeer(null, bare, 30, null); |
| 413 | assert.equal(alone.rttMax, 31); |
| 414 | assert.equal(alone.rttAvg, undefined); |
| 415 | assert.equal(alone.jitterAvg, undefined); |
| 416 | }); |
| 417 | |
| 418 | test('§15.29: the sampler reads the audio\'s jitter, the camera\'s when no audio arrives, and nothing else', () => { |
| 419 | const camera = { path: null, streams: new Map([ |
| 420 | ['out', { dir: 'out', kind: 'audio', counters: {}, gauges: { jitter: 0.5 } }], |
| 421 | ['cam', { dir: 'in', kind: 'video', counters: {}, gauges: { jitter: 0.012 } }], |
| 422 | ]) }; |
| 423 | const a = q.qualitySample(null, camera); |
| 424 | assert.deepEqual(a, { rttSum: 0, rttN: 0, rttMax: 0, jitterSum: 12, jitterN: 1, jitterMax: 12 }); |
| 425 | const both = { path: { rtt: NaN }, streams: new Map([ |
| 426 | ['cam', { dir: 'in', kind: 'video', counters: {}, gauges: { jitter: 0.012 } }], |
| 427 | ['mic', { dir: 'in', kind: 'audio', counters: {}, gauges: { jitter: 0.004 } }], |
| 428 | ]) }; |
| 429 | assert.deepEqual(q.qualitySample(null, both), { rttSum: 0, rttN: 0, rttMax: 0, jitterSum: 4, jitterN: 1, jitterMax: 4 }); |
| 430 | assert.deepEqual(q.qualitySample(null, { path: null, streams: new Map() }), |
| 431 | { rttSum: 0, rttN: 0, rttMax: 0, jitterSum: 0, jitterN: 0, jitterMax: 0 }); |
| 432 | }); |
| 433 | |
| 434 | test('§15.29: the limit that held longest, how long, the encoder and decoder, the codec and the mode', () => { |
| 435 | const one = q.qualityReading(relayWalk(0), classify); |
| 436 | const two = q.qualityReading(relayWalk(1), classify); |
| 437 | const report = q.qualityReport({ seconds: 30, client: { app: 'web' }, shareMode: 'detail', |
| 438 | peers: [{ id: PEER, prev: one, now: two }] }); |
| 439 | const p = report.peers[0]; |
| 440 | // The gauge said none; the window was 21 s bandwidth, 4 s cpu, 5 s none. |
| 441 | assert.equal(p.out.video.limit, 'bandwidth'); |
| 442 | assert.equal(p.out.video.limitedBandwidthSeconds, 21); |
| 443 | assert.equal(p.out.video.limitedCpuSeconds, 4); |
| 444 | assert.equal(p.out.video.encoder, 'hardware'); |
| 445 | assert.equal(p.out.video.codec, 'h264'); |
| 446 | // A tie goes to bandwidth; the screen's encoder is software by its name. |
| 447 | assert.deepEqual([p.out.screen.limit, p.out.screen.limitedBandwidthSeconds, p.out.screen.limitedCpuSeconds], |
| 448 | ['bandwidth', 15, 15]); |
| 449 | assert.equal(p.out.screen.encoder, 'software'); |
| 450 | assert.equal(p.out.screen.codec, 'vp9'); |
| 451 | assert.equal(p.out.screen.mode, 'detail'); |
| 452 | assert.equal(p.out.video.mode, undefined, 'the mode is the screen\'s'); |
| 453 | assert.equal(p.out.audio.codec, undefined, 'audio is opus, and says nothing'); |
| 454 | assert.equal(p.in.video.decoder, 'hardware'); |
| 455 | assert.equal(p.in.video.codec, 'av1'); |
| 456 | // The first window: everything held so far is the window's, and a |
| 457 | // stream that never left none is none. |
| 458 | const first = q.qualityPeer(null, one, 10); |
| 459 | assert.equal(first.out.video.limit, 'none'); |
| 460 | assert.equal(first.out.video.limitedBandwidthSeconds, 2); |
| 461 | // Not sharing, or a mode it does not know: no mode. |
| 462 | const quiet = q.qualityReport({ seconds: 30, client: { app: 'web' }, shareMode: '', peers: [{ id: PEER, prev: one, now: two }] }); |
| 463 | assert.equal(quiet.peers[0].out.screen.mode, undefined); |
| 464 | const odd = q.qualityReport({ seconds: 30, client: { app: 'web' }, shareMode: 'cinema', peers: [{ id: PEER, prev: one, now: two }] }); |
| 465 | assert.equal(odd.peers[0].out.screen.mode, undefined); |
| 466 | // Nothing held in the window at all: the gauge, as before. |
| 467 | const still = q.qualityPeer(two, two, 30); |
| 468 | assert.equal(still.out.video.limit, 'none'); |
| 469 | assert.equal(still.out.screen.limit, 'cpu'); |
| 470 | assert.equal(still.out.video.limitedBandwidthSeconds, 0); |
| 471 | }); |
| 472 | |
| 473 | test('§15.29: an encoder\'s or decoder\'s kind, never its name', () => { |
| 474 | const cases = [ |
| 475 | ['ExternalEncoder', undefined, 'hardware'], |
| 476 | ['VideoToolbox', undefined, 'hardware'], |
| 477 | ['MediaCodec', undefined, 'hardware'], |
| 478 | ['VaapiVideoEncodeAccelerator', undefined, 'hardware'], |
| 479 | ['V4L2', undefined, 'hardware'], |
| 480 | ['D3D11VideoDecoder', undefined, 'hardware'], |
| 481 | ['DXVAVideoDecodeAccelerator', undefined, 'hardware'], |
| 482 | ['NVENC', undefined, 'hardware'], |
| 483 | ['MediaFoundationVideoEncodeAccelerator', undefined, 'hardware'], |
| 484 | ['QSV', undefined, 'hardware'], |
| 485 | ['AMF', undefined, 'hardware'], |
| 486 | ['libvpx', undefined, 'software'], |
| 487 | ['SimulcastEncoderAdapter (libvpx, libvpx)', undefined, 'software'], |
| 488 | ['OpenH264', undefined, 'software'], |
| 489 | ['libaom', undefined, 'software'], |
| 490 | ['dav1d', undefined, 'software'], |
| 491 | ['FFmpeg', undefined, 'software'], |
| 492 | ['libavcodec', undefined, 'software'], |
| 493 | ['FallbackEncoder', undefined, 'software'], |
| 494 | ['Something new', undefined, 'unknown'], |
| 495 | ['', undefined, ''], |
| 496 | [undefined, undefined, ''], |
| 497 | [42, undefined, ''], |
| 498 | // The browser's own word wins over the name. |
| 499 | ['libvpx', true, 'hardware'], |
| 500 | ['ExternalEncoder', false, 'software'], |
| 501 | [undefined, true, 'hardware'], |
| 502 | [undefined, false, 'software'], |
| 503 | ]; |
| 504 | for (const [impl, pe, want] of cases) assert.equal(q.coderKind(impl, pe), want, String(impl) + ' / ' + pe); |
| 505 | }); |
| 506 | |
| 507 | test('§15.29: a codec\'s family by its MIME type', () => { |
| 508 | const cases = [ |
| 509 | ['video/VP8', 'vp8'], ['video/vp9', 'vp9'], ['video/H264', 'h264'], ['video/H265', 'h265'], ['video/HEVC', 'h265'], |
| 510 | ['video/AV1', 'av1'], ['audio/opus', 'opus'], ['audio/PCMU', 'other'], ['video/rtx', 'other'], ['', ''], [undefined, ''], |
| 511 | ]; |
| 512 | for (const [mime, want] of cases) assert.equal(q.codecKind(mime), want, String(mime)); |
| 513 | }); |
| 514 | |
| 515 | // ── §15.29: the automatic report ───────────────────────────────────── |
| 516 | |
| 517 | test('§15.29: the facts a window\'s report holds', () => { |
| 518 | const report = { |
| 519 | events: { reconnectingSeconds: 2 }, |
| 520 | peers: [ |
| 521 | { peer: PEER, rtt: 90, rttMax: 300, in: { video: { freezes: 2 }, screen: { freezes: 1 } }, |
| 522 | out: { audio: { rtt: 950 }, screen: { keyframes: 4 } }, events: { reconnectingSeconds: 6.5 } }, |
| 523 | { peer: OTHER, rtt: 1200, in: { video: { freezes: 1 } }, out: { screen: { rebuilds: 1 } } }, |
| 524 | ], |
| 525 | }; |
| 526 | assert.deepEqual(q.qualityFacts(report), { freezes: 4, maxRtt: 1200, reconnectingSeconds: 6.5, screenEscalated: true }); |
| 527 | // Keyframes alone are the ladder's first step, not trouble. |
| 528 | assert.deepEqual(q.qualityFacts({ peers: [{ peer: PEER, out: { screen: { keyframes: 9, damped: 2 } } }] }), |
| 529 | { freezes: 0, maxRtt: 0, reconnectingSeconds: 0, screenEscalated: false }); |
| 530 | assert.deepEqual(q.qualityFacts({ v: 1 }), { freezes: 0, maxRtt: 0, reconnectingSeconds: 0, screenEscalated: false }); |
| 531 | for (const k of ['reshares', 'failures']) { |
| 532 | assert.equal(q.qualityFacts({ peers: [{ peer: PEER, out: { screen: { [k]: 1 } } }] }).screenEscalated, true, k); |
| 533 | } |
| 534 | // The call's own "Reconnecting…" counts as anyone's. |
| 535 | assert.equal(q.qualityFacts({ events: { reconnectingSeconds: 9 }, peers: [] }).reconnectingSeconds, 9); |
| 536 | }); |
| 537 | |
| 538 | test('§15.29: the rules — freezes and round trip two windows in a row, a long reconnect or a screen in one', () => { |
| 539 | const f = (o) => Object.assign({ freezes: 0, maxRtt: 0, reconnectingSeconds: 0, screenEscalated: false }, o); |
| 540 | // Freezes: three or more in this window and the one before it. |
| 541 | assert.equal(q.qualityTrouble(f({ freezes: 3 }), f({ freezes: 3 })), 'freezes'); |
| 542 | assert.equal(q.qualityTrouble(f({ freezes: 9 }), f({ freezes: 4 })), 'freezes'); |
| 543 | assert.equal(q.qualityTrouble(null, f({ freezes: 50 })), '', 'the first window has no window before it'); |
| 544 | assert.equal(q.qualityTrouble(f({ freezes: 2 }), f({ freezes: 50 })), ''); |
| 545 | assert.equal(q.qualityTrouble(f({ freezes: 50 }), f({ freezes: 2 })), ''); |
| 546 | // Round trip: above 800 ms in both. |
| 547 | assert.equal(q.qualityTrouble(f({ maxRtt: 801 }), f({ maxRtt: 2000 })), 'rtt'); |
| 548 | assert.equal(q.qualityTrouble(f({ maxRtt: 800 }), f({ maxRtt: 2000 })), '', 'at 800 is not above it'); |
| 549 | assert.equal(q.qualityTrouble(f({ maxRtt: 2000 }), f({ maxRtt: 800 })), ''); |
| 550 | assert.equal(q.qualityTrouble(null, f({ maxRtt: 5000 })), ''); |
| 551 | // Reconnecting: longer than five seconds, in one window. |
| 552 | assert.equal(q.qualityTrouble(null, f({ reconnectingSeconds: 5.1 })), 'reconnecting'); |
| 553 | assert.equal(q.qualityTrouble(null, f({ reconnectingSeconds: 5 })), ''); |
| 554 | // The screen past its keyframes, in one window. |
| 555 | assert.equal(q.qualityTrouble(null, f({ screenEscalated: true })), 'screen'); |
| 556 | // Quiet: nothing. |
| 557 | assert.equal(q.qualityTrouble(f({}), f({})), ''); |
| 558 | // In the protocol's order. |
| 559 | const all = f({ freezes: 5, maxRtt: 900, reconnectingSeconds: 8, screenEscalated: true }); |
| 560 | assert.equal(q.qualityTrouble(all, all), 'freezes'); |
| 561 | assert.equal(q.qualityTrouble(f({ maxRtt: 900 }), f({ maxRtt: 900, reconnectingSeconds: 8, screenEscalated: true })), 'rtt'); |
| 562 | assert.equal(q.qualityTrouble(null, f({ reconnectingSeconds: 8, screenEscalated: true })), 'reconnecting'); |
| 563 | |
| 564 | // From real windows: two of the relayed walk, three freezes each. |
| 565 | const w0 = q.qualityReading(relayWalk(0), classify); |
| 566 | const w1 = q.qualityReading(relayWalk(1), classify); |
| 567 | const w2 = q.qualityReading(relayWalk(2), classify); |
| 568 | const at = (prev, now) => q.qualityFacts(q.qualityReport({ seconds: 30, client: { app: 'web' }, peers: [{ id: PEER, prev, now }] })); |
| 569 | const one = at(w0, w1); |
| 570 | const two = at(w1, w2); |
| 571 | assert.equal(one.freezes, 3); |
| 572 | assert.equal(q.qualityTrouble(null, one), ''); |
| 573 | assert.equal(q.qualityTrouble(one, two), 'freezes'); |
| 574 | // A quiet window between them breaks the run. |
| 575 | assert.equal(q.qualityTrouble(at(w2, w2), two), ''); |
| 576 | }); |
| 577 | |
| 578 | test('§15.29: at most three a participation, ten minutes apart, and a new participation a new gate', () => { |
| 579 | const MIN = 60000; |
| 580 | let gate = q.autoReportGate(null, 'a'.repeat(32)); |
| 581 | assert.deepEqual(gate, { token: 'a'.repeat(32), count: 0, last: null }); |
| 582 | assert.equal(q.autoReportAllowed(gate, 0), true); |
| 583 | gate.count = 1; |
| 584 | gate.last = 1000; |
| 585 | assert.equal(q.autoReportAllowed(gate, 1000 + 10 * MIN - 1), false); |
| 586 | assert.equal(q.autoReportAllowed(gate, 1000 + 10 * MIN), true); |
| 587 | gate.count = 3; |
| 588 | assert.equal(q.autoReportAllowed(gate, 1000 + 60 * MIN), false); |
| 589 | // A resumed stream's hello carries the same token: the same gate. |
| 590 | assert.equal(q.autoReportGate(gate, 'a'.repeat(32)), gate); |
| 591 | // A new participation's token: a fresh one. |
| 592 | const fresh = q.autoReportGate(gate, 'b'.repeat(32)); |
| 593 | assert.notEqual(fresh, gate); |
| 594 | assert.deepEqual(fresh, { token: 'b'.repeat(32), count: 0, last: null }); |
| 595 | |
| 596 | // A call in trouble for fifty minutes, a window every thirty seconds: |
| 597 | // sent in the first window that may, ten minutes later, ten after that, |
| 598 | // and never again. |
| 599 | const trouble = { freezes: 0, maxRtt: 0, reconnectingSeconds: 30, screenEscalated: false }; |
| 600 | gate = q.autoReportGate(null, 'c'.repeat(32)); |
| 601 | const sent = []; |
| 602 | for (let now = 30000; now <= 50 * MIN; now += 30000) { |
| 603 | const trigger = q.autoReportDecision({ facts: trouble, prevFacts: trouble, gate, now, on: true, serverOff: false, |
| 604 | token: gate.token, final: false, force: '' }); |
| 605 | if (trigger) { |
| 606 | gate.count += 1; |
| 607 | gate.last = now; |
| 608 | sent.push(now); |
| 609 | } |
| 610 | } |
| 611 | assert.deepEqual(sent, [30000, 30000 + 10 * MIN, 30000 + 20 * MIN]); |
| 612 | }); |
| 613 | |
| 614 | test('§15.29: never with the switch off, the kill switch thrown, no token, or the final report', () => { |
| 615 | const trouble = { freezes: 0, maxRtt: 0, reconnectingSeconds: 30, screenEscalated: false }; |
| 616 | const quiet = { freezes: 0, maxRtt: 0, reconnectingSeconds: 0, screenEscalated: false }; |
| 617 | const base = { facts: trouble, prevFacts: null, gate: q.autoReportGate(null, 't'), now: 1, on: true, serverOff: false, |
| 618 | token: 'f'.repeat(32), final: false, force: '' }; |
| 619 | const d = (o) => q.autoReportDecision(Object.assign({}, base, o)); |
| 620 | assert.equal(d({}), 'reconnecting'); |
| 621 | // "Send anonymous quality reports" off: nothing automatic either. |
| 622 | assert.equal(d({ on: false }), ''); |
| 623 | assert.equal(d({ on: false, force: 'freezes' }), '', 'not even forced'); |
| 624 | assert.equal(d({ serverOff: true }), ''); |
| 625 | assert.equal(d({ token: '' }), ''); |
| 626 | assert.equal(d({ final: true }), ''); |
| 627 | assert.equal(d({ facts: quiet }), ''); |
| 628 | // The gate's hook stands in for a rule, and the gate still decides. |
| 629 | assert.equal(d({ facts: quiet, force: 'screen' }), 'screen'); |
| 630 | assert.equal(d({ facts: quiet, force: 'everything' }), '', 'a trigger the protocol does not name is no trigger'); |
| 631 | assert.equal(d({ force: 'rtt', gate: { token: 't', count: 3, last: null } }), ''); |
| 632 | }); |
| 633 | |
| 634 | test('§15.29: the automatic report\'s body fits the server\'s 64 KB, cutting the diagnostics', () => { |
| 635 | const client = { app: 'web' }; |
| 636 | assert.deepEqual(JSON.parse(q.autoReportBody('rtt', 'all fine', client, 65024)), { trigger: 'rtt', diagnostics: 'all fine', client }); |
| 637 | const big = q.autoReportBody('freezes', 'line\n'.repeat(40000), client, 65024); |
| 638 | assert.ok(new TextEncoder().encode(big).length <= 65024); |
| 639 | const parsed = JSON.parse(big); |
| 640 | assert.equal(parsed.trigger, 'freezes'); |
| 641 | assert.ok(parsed.diagnostics.startsWith('line\n')); |
| 642 | assert.ok(parsed.diagnostics.endsWith('[cut short to fit]')); |
| 643 | assert.deepEqual(Object.keys(parsed), ['trigger', 'diagnostics', 'client']); |
| 644 | }); |
| 645 | |
| 646 | test('§15.29: a window that sent one names its reference, and only a reference', () => { |
| 647 | const now = q.qualityReading(walk(1), classify); |
| 648 | const r = (autoReport) => q.qualityReport({ seconds: 30, client: { app: 'web' }, autoReport, peers: [{ id: PEER, prev: null, now }] }); |
| 649 | assert.equal(r('K7Q2M9PD').autoReport, 'K7Q2M9PD'); |
| 650 | assert.equal(r(undefined).autoReport, undefined); |
| 651 | assert.equal(r('<b>x</b>').autoReport, undefined); |
| 652 | assert.equal(r('k7q2m9pd').autoReport, undefined); |
| 653 | }); |
| 654 | |
| 655 | // ── the notices ────────────────────────────────────────────────────── |
| 656 | |
| 657 | test('the notices: counted once each time they appear, timed while they show', () => { |
| 658 | const rec = q.qualityWatchRecord(); |
| 659 | const quiet = { reconnecting: false, cantHear: false }; |
| 660 | const look = (t, call, peer) => q.qualityWatch(rec, t, { call, audioBlocked: false, peers: new Map([[PEER, peer]]) }); |
| 661 | // Can't hear us for the first 1.4 s; then a call-wide "Reconnecting…" |
| 662 | // from 5 s to 8 s; then one on this person from 10 s to 11 s. |
| 663 | for (let t = 0; t <= 15000; t += 200) { |
| 664 | look(t, t >= 5000 && t < 8000, { |
| 665 | reconnecting: t >= 10000 && t < 11000, |
| 666 | cantHear: t < 1400, |
| 667 | noSound: t === 12000, |
| 668 | }); |
| 669 | } |
| 670 | const got = q.qualityTake(rec); |
| 671 | assert.deepEqual(got.call, { reconnecting: 1, reconnectingSeconds: 3 }); |
| 672 | assert.deepEqual(got.peers.get(PEER), { reconnecting: 1, reconnectingSeconds: 1, cantHearSeconds: 1.4, noSound: true }); |
| 673 | // A quiet window says nothing. |
| 674 | look(15200, false, quiet); |
| 675 | look(15400, false, quiet); |
| 676 | const next = q.qualityTake(rec); |
| 677 | assert.deepEqual(next.call, {}); |
| 678 | assert.equal(next.peers.size, 0); |
| 679 | }); |
| 680 | |
| 681 | test('a suspended tab\'s gap is not a notice someone saw, and someone gone is forgotten', () => { |
| 682 | const rec = q.qualityWatchRecord(); |
| 683 | q.qualityWatch(rec, 0, { call: true, audioBlocked: true, peers: new Map([[PEER, { reconnecting: false, cantHear: true }]]) }); |
| 684 | q.qualityWatch(rec, 60000, { call: true, audioBlocked: false, peers: new Map() }); |
| 685 | const got = q.qualityTake(rec); |
| 686 | assert.deepEqual(got.call, { reconnecting: 1, reconnectingSeconds: 1, audioBlocked: true }); |
| 687 | assert.deepEqual(got.peers.get(PEER), { cantHearSeconds: 1 }); |
| 688 | assert.equal(rec.peers.size, 0); |
| 689 | }); |
| 690 | |
| 691 | // ── the devices and the client ─────────────────────────────────────── |
| 692 | |
| 693 | test('a device\'s kind by its label, which never leaves', () => { |
| 694 | const cases = [ |
| 695 | ['Paul’s AirPods Pro', 'bluetooth'], |
| 696 | ['Bose QC35 II', 'bluetooth'], |
| 697 | ['Galaxy Buds2 Pro (Bluetooth)', 'bluetooth'], |
| 698 | ['MacBook Pro Microphone (Built-in)', 'built-in'], |
| 699 | ['Default - MacBook Air Speakers (Built-in)', 'built-in'], |
| 700 | ['Microphone Array (Realtek(R) Audio)', 'built-in'], |
| 701 | ['Scarlett 2i2 USB', 'usb'], |
| 702 | ['USB Audio Device (0d8c:0014)', 'usb'], |
| 703 | ['External Headphones', 'wired'], |
| 704 | ['BlackHole 2ch (Virtual)', 'virtual'], |
| 705 | ['Paul’s iPhone Microphone', 'continuity'], |
| 706 | ['Living Room (AirPlay)', 'airplay'], |
| 707 | ['Studio Display Microphone', 'unknown'], |
| 708 | ['', 'unknown'], |
| 709 | [undefined, 'unknown'], |
| 710 | ]; |
| 711 | for (const [label, kind] of cases) assert.equal(q.deviceKind(label), kind, String(label)); |
| 712 | }); |
| 713 | |
| 714 | test('the client: enums and digits from the user agent, never the string', () => { |
| 715 | const MAC_CHROME = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.7204.50 Safari/537.36'; |
| 716 | const cases = [ |
| 717 | [MAC_CHROME, { platform: 'macOS', platformVersion: '15.5.0' }, 'dev', 0, |
| 718 | { app: 'web', version: 'dev', browser: 'chrome', browserVersion: '138.0', os: 'macos', osVersion: '15.5.0' }], |
| 719 | // No hints: macOS's frozen 10_15_7 is not a version. |
| 720 | [MAC_CHROME, null, 'de04e2d', 0, { app: 'web', version: 'de04e2d', browser: 'chrome', browserVersion: '138.0', os: 'macos' }], |
| 721 | ['Mozilla/5.0 (iPhone; CPU iPhone OS 17_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Mobile/15E148 Safari/604.1', |
| 722 | null, '', 5, { app: 'web', browser: 'safari', browserVersion: '17.4', os: 'ios', osVersion: '17.4.1' }], |
| 723 | // An iPad asking for the desktop site says Macintosh; its touch points say otherwise. |
| 724 | ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15', |
| 725 | null, '', 5, { app: 'web', browser: 'safari', browserVersion: '18.2', os: 'ipados' }], |
| 726 | ['Mozilla/5.0 (iPad; CPU OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1', |
| 727 | null, '', 5, { app: 'web', browser: 'chrome', browserVersion: '120.0', os: 'ipados', osVersion: '16.6' }], |
| 728 | ['Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.3351.55', |
| 729 | { platform: 'Windows', platformVersion: '19.0.0' }, '', 0, |
| 730 | { app: 'web', browser: 'edge', browserVersion: '138.0', os: 'windows', osVersion: '19.0.0' }], |
| 731 | ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:139.0) Gecko/20100101 Firefox/139.0', null, '', 0, |
| 732 | { app: 'web', browser: 'firefox', browserVersion: '139.0', os: 'linux' }], |
| 733 | ['Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Mobile Safari/537.36', null, '', 5, |
| 734 | { app: 'web', browser: 'chrome', browserVersion: '137.0', os: 'android', osVersion: '10' }], |
| 735 | ['Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 OPR/120.0.0.0', |
| 736 | null, 'not a version!', 0, { app: 'web', browser: 'other', os: 'chromeos' }], |
| 737 | ['', null, '', 0, { app: 'web', browser: 'other', os: 'other' }], |
| 738 | ]; |
| 739 | for (const [ua, hints, version, touch, want] of cases) { |
| 740 | const got = r.reportClient(ua, hints, version, touch); |
| 741 | assert.deepEqual(got, want, ua); |
| 742 | const json = JSON.stringify(got); |
| 743 | assert.ok(!json.includes('Mozilla') && !json.includes('AppleWebKit'), json); |
| 744 | } |
| 745 | }); |
| 746 | |
| 747 | // ── "Report a problem" ─────────────────────────────────────────────── |
| 748 | |
| 749 | test('the sheet\'s answers', () => { |
| 750 | assert.deepEqual(r.problemAnswer(200, { ref: 'K7Q2M9PD' }), { ref: 'K7Q2M9PD', text: '' }); |
| 751 | assert.equal(r.problemAnswer(200, { ref: '<b>x</b>' }).text, 'The report could not be sent.'); |
| 752 | assert.equal(r.problemAnswer(429, null).text, 'Too many reports from this call — try again in a minute.'); |
| 753 | assert.equal(r.problemAnswer(404, {}).text, 'This call can no longer be reported.'); |
| 754 | assert.equal(r.problemAnswer(403, {}).text, 'The report could not be sent.'); |
| 755 | assert.equal(r.problemAnswer(0, null).text, 'The report could not be sent.'); |
| 756 | }); |
| 757 | |
| 758 | test('the problem body fits the server\'s 64 KB, cutting the diagnostics and never the note', () => { |
| 759 | const client = { app: 'web' }; |
| 760 | const small = JSON.parse(r.problemBody('It froze', 'all fine', client, 65000)); |
| 761 | assert.deepEqual(small, { note: 'It froze', diagnostics: 'all fine', client }); |
| 762 | const big = r.problemBody('x'.repeat(1200), 'line\n'.repeat(40000), client, 65000); |
| 763 | assert.ok(new TextEncoder().encode(big).length <= 65000); |
| 764 | const parsed = JSON.parse(big); |
| 765 | assert.equal(parsed.note.length, 1000); |
| 766 | assert.ok(parsed.diagnostics.endsWith('[cut short to fit]')); |
| 767 | }); |
| 768 | |
| 769 | test('the diagnostics a problem report sends name nobody', () => { |
| 770 | const names = new Map([[PEER, 'Paul Campbell'], [OTHER, 'bea']]); |
| 771 | const pc = { connectionState: 'connected', signalingState: 'stable', iceConnectionState: 'connected' }; |
| 772 | const pcs = new Map([[PEER, { pc, shareSend: { width: 1280, height: 720, fps: 30, framesEncoded: 9, framesSent: 9, keyFramesEncoded: 1 } }, |
| 773 | ], [OTHER, { pc, shareSend: null, shareTx: {} }]]); |
| 774 | const env = { |
| 775 | names, pcs, |
| 776 | state: { admitted: true, stopped: false }, |
| 777 | share: { stream: {}, track: {} }, |
| 778 | shareMode: () => 'balanced', |
| 779 | screenAsks: new Map([[OTHER, { stage: 'keyframes' }]]), |
| 780 | shareCounts: { keyframes: 1, rebuilds: 0, reshares: 0, failures: 0 }, |
| 781 | screenReadings: new Map([[PEER + '#share', { mid: '3', width: 1, height: 1 }]]), |
| 782 | readingSummary: () => 'a reading', |
| 783 | screenStalled: [PEER], |
| 784 | screenReattaches: 0, |
| 785 | screenLines: ['10:00:00.000 keyframe for ' + OTHER.slice(0, 6)], |
| 786 | SCREEN_LOG_LINES: 200, |
| 787 | SHARE_SUFFIX: '#share', |
| 788 | navigator: { userAgent: 'Test/1.0' }, |
| 789 | }; |
| 790 | // eslint-disable-next-line no-new-func |
| 791 | const callDiagnostics = new Function(...Object.keys(env), lift(roomSrc, 'room.js', 'callDiagnostics') + |
| 792 | '; return callDiagnostics;')(...Object.values(env)); |
| 793 | const anonymous = callDiagnostics({ anonymous: true }); |
| 794 | assert.ok(!/Paul|Campbell|bea\b/.test(anonymous), anonymous); |
| 795 | assert.ok(anonymous.includes('peer ' + PEER.slice(0, 6) + ': connection connected'), anonymous); |
| 796 | assert.ok(anonymous.includes('to peer ' + OTHER.slice(0, 6)), anonymous); |
| 797 | assert.ok(anonymous.includes('recovering peer ' + OTHER.slice(0, 6)), anonymous); |
| 798 | assert.ok(anonymous.includes('stalled here: peer ' + PEER.slice(0, 6)), anonymous); |
| 799 | // "Copy call diagnostics" keeps the names. |
| 800 | const named = callDiagnostics(); |
| 801 | assert.ok(named.includes('Paul Campbell (' + PEER.slice(0, 6) + ')'), named); |
| 802 | assert.ok(named.includes('recovering bea'), named); |
| 803 | }); |
| 804 | |