Calls: the gate's thin link drops frames through insertable streams, and checks the level the watch actually took (spec 15.30)
The encoded-transform cap never saw a frame in a receiver set up in its track event; insertable streams on the main thread do, and a frame lost holds the rest until a keyframe fits. Severe trouble at reduced goes to audio only, so the scenario checks a paused camera there, and the first step up from the last step down. probehang counts the recovery's messages, not media reports.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+122
−54
test/browser/call.mjs+122 −54
diff --git a/test/browser/call.mjs b/test/browser/call.mjs| index 82650ca..da01441 100644 |
| --- a/test/browser/call.mjs |
| +++ b/test/browser/call.mjs |
| @@ -341,54 +341,92 @@ const consoleLines = new Map(); // label -> string[] |
| // `via` is the browser to open the context in, for the one scenario |
| // (autoplay) that needs a Chromium launched under a different policy. |
| // downlinkCap is §15.30's thin link, for the lowbandwidth scenario: every |
| -// video receiver this page makes gets an encoded transform whose worker |
| -// passes a frame only while a token bucket of `kbit/s` (1.5 s of burst) |
| -// has room for it, and drops it otherwise — before the decoder, as a link |
| -// that cannot carry the picture loses it. window.__capDownlink(kbps) sets |
| -// the cap (0 lifts it) and answers how many receivers are behind it. The |
| -// gate's, never the page's: the worker comes from a blob, which the |
| -// page's own CSP refuses unless the context bypasses it. |
| +// video frame arriving in this page passes a token bucket of `kbit/s` (2 s |
| +// of burst) or is dropped before the decoder — and after a drop, every |
| +// frame until a keyframe fits, which is what a decoder downstream of a |
| +// lossy link has to work with, and what makes it freeze and ask for one. |
| +// window.__capDownlink(kbps) sets the cap (0 lifts it) and answers how many |
| +// video receivers are behind it; window.__capCounts is what passed and what |
| +// was dropped. On the main thread, through insertable streams: every sender |
| +// and receiver of a connection made with them must be piped, so the |
| +// others pass straight through. The gate's, never the page's. |
| function downlinkCap() { |
| - if (typeof RTCRtpScriptTransform === 'undefined') return; |
| const Native = window.RTCPeerConnection; |
| - let worker = null; |
| + if (!Native || typeof RTCRtpSender === 'undefined' || !('createEncodedStreams' in RTCRtpSender.prototype)) return; |
| + const bucket = { cap: 0, tokens: 0, last: 0, lost: false, passed: 0, dropped: 0 }; |
| let receivers = 0; |
| - const work = () => { |
| - if (!worker) { |
| - const code = [ |
| - 'let cap = 0, tokens = 0, last = 0;', |
| - 'onmessage = (m) => { cap = m.data.cap || 0; tokens = cap * 125 * 1.5; last = performance.now(); };', |
| - 'onrtctransform = (e) => {', |
| - ' e.transformer.readable.pipeThrough(new TransformStream({ transform(f, c) {', |
| - ' if (!cap) { c.enqueue(f); return; }', |
| - ' const now = performance.now();', |
| - ' tokens = Math.min(cap * 125 * 1.5, tokens + (now - last) / 1000 * cap * 125);', |
| - ' last = now;', |
| - ' if (tokens >= f.data.byteLength) { tokens -= f.data.byteLength; c.enqueue(f); }', |
| - ' } })).pipeTo(e.transformer.writable);', |
| - '};', |
| - ].join('\n'); |
| - worker = new Worker(URL.createObjectURL(new Blob([code], { type: 'text/javascript' }))); |
| - } |
| - return worker; |
| - }; |
| + const piped = new WeakSet(); |
| + window.__capCounts = bucket; |
| window.__capDownlink = (kbps) => { |
| - work().postMessage({ cap: Number(kbps) || 0 }); |
| + bucket.cap = Number(kbps) || 0; |
| + bucket.tokens = bucket.cap * 125 * 2; |
| + bucket.last = performance.now(); |
| + bucket.lost = false; |
| return receivers; |
| }; |
| + const pass = (end) => { |
| + if (!end || piped.has(end)) return; |
| + piped.add(end); |
| + const { readable, writable } = end.createEncodedStreams(); |
| + readable.pipeTo(writable).catch(() => {}); |
| + }; |
| + const capped = (receiver) => { |
| + if (piped.has(receiver)) return; |
| + piped.add(receiver); |
| + const { readable, writable } = receiver.createEncodedStreams(); |
| + readable.pipeThrough(new TransformStream({ |
| + transform(f, c) { |
| + const b = bucket; |
| + if (!b.cap) { |
| + b.passed += 1; |
| + c.enqueue(f); |
| + return; |
| + } |
| + const now = performance.now(); |
| + b.tokens = Math.min(b.cap * 125 * 2, b.tokens + ((now - b.last) / 1000) * b.cap * 125); |
| + b.last = now; |
| + if (b.lost && f.type !== 'key') { |
| + b.dropped += 1; |
| + return; |
| + } |
| + if (b.tokens >= f.data.byteLength) { |
| + b.tokens -= f.data.byteLength; |
| + b.lost = false; |
| + b.passed += 1; |
| + c.enqueue(f); |
| + } else { |
| + b.lost = true; |
| + b.dropped += 1; |
| + } |
| + }, |
| + })).pipeTo(writable).catch(() => {}); |
| + receivers += 1; |
| + }; |
| + const insertable = (config) => Object.assign({}, config || {}, { encodedInsertableStreams: true }); |
| window.RTCPeerConnection = class extends Native { |
| - constructor(...args) { |
| - super(...args); |
| + constructor(config, ...rest) { |
| + super(insertable(config), ...rest); |
| this.addEventListener('track', (ev) => { |
| - if (!ev.receiver || ev.track.kind !== 'video') return; |
| try { |
| - ev.receiver.transform = new RTCRtpScriptTransform(work(), {}); |
| - receivers += 1; |
| + if (ev.track.kind === 'video') capped(ev.receiver); |
| + else pass(ev.receiver); |
| + if (ev.transceiver) pass(ev.transceiver.sender); |
| } catch { |
| - // A receiver that has started already: not behind the cap. |
| + // Piped already, or a connection closing. |
| } |
| }); |
| } |
| + setConfiguration(config) { return super.setConfiguration(insertable(config)); } |
| + addTrack(...args) { |
| + const sender = super.addTrack(...args); |
| + try { pass(sender); } catch {} |
| + return sender; |
| + } |
| + addTransceiver(...args) { |
| + const tx = super.addTransceiver(...args); |
| + try { pass(tx.sender); } catch {} |
| + return tx; |
| + } |
| }; |
| } |
| @@ -4225,10 +4263,18 @@ const run = { |
| // after it was sent: "that peer is not here", arriving with a third |
| // of the message's budget left. |
| await ctxA.route('**/signal', async (route) => { |
| - if (!firstAt) firstAt = Date.now(); |
| - signals += 1; |
| const m = (route.request().postData() || '').match(/"type":"([a-z-]+)"/); |
| posted.push(m ? m[1] : '?'); |
| + // A media report is state on the broadcast lane, not part of the |
| + // recovery under test: §15.30's asks change when the picture from B |
| + // stops, and again when B is let go. It is answered like the rest |
| + // and not counted. |
| + if (m && m[1] === 'media') { |
| + await route.fulfill({ status: 404, contentType: 'application/json', body: '{"error":"not-found"}' }); |
| + return; |
| + } |
| + if (!firstAt) firstAt = Date.now(); |
| + signals += 1; |
| await sleep(20000); |
| await route.fulfill({ status: 404, contentType: 'application/json', body: '{"error":"not-found"}' }); |
| }); |
| @@ -4263,7 +4309,7 @@ const run = { |
| // scenario sent is the only thing A ever posted, and it is still the |
| // only thing ten seconds later. |
| const atDrop = signals; |
| - check(atDrop === 1, 'the offer was the only thing A posted (' + atDrop + ': ' + posted.join(', ') + ')'); |
| + check(atDrop === 1, 'the offer was the only message A posted (' + atDrop + ': ' + posted.join(', ') + ')'); |
| check(dropped && dropped.stale && dropped.stale.includes(pair.b.peer), |
| 'and A holds the target marked stale, waiting for a roster rather than asking again (' + |
| JSON.stringify(dropped && dropped.stale) + ')'); |
| @@ -6666,7 +6712,7 @@ const run = { |
| const CAP_KBPS = Number(process.env.CALLS_CAP_KBPS) || 60; |
| const state = (page) => page.evaluate(() => window.meet.state()); |
| const ctxA = await newContext('member A', { cookie: cookieA }); |
| - const ctxB = await newContext('member B', { cookie: cookieB, options: { bypassCSP: true } }); |
| + const ctxB = await newContext('member B', { cookie: cookieB }); |
| await ctxB.addInitScript(downlinkCap); |
| const pageA = await openRoom(ctxA); |
| const pageB = await openRoom(ctxB); |
| @@ -6680,6 +6726,7 @@ const run = { |
| const look = async (what) => { |
| const sa = await pageA.evaluate(() => window.meet.state()); |
| const sb = await pageB.evaluate(() => window.meet.state()); |
| + const cap = await pageB.evaluate(() => window.__capCounts ? { passed: window.__capCounts.passed, dropped: window.__capCounts.dropped } : null); |
| const x = sb.adapt[a] || {}; |
| const y = sa.adapt[b] || {}; |
| const row = { |
| @@ -6688,6 +6735,7 @@ const run = { |
| aCamera: y.wire ? y.wire.cameraHeight + 'p@' + Math.round(y.wire.cameraFps || 0) : '-', |
| bAudioIn: x.wire ? x.wire.audioPackets : 0, bFreezes: x.wire ? x.wire.videoFreezes : 0, |
| notice: sa.videoNotice, aAsks: JSON.stringify(sa.videoAsks), bLast: x.last, |
| + cap, |
| aKbps: y.wire && lastBytes ? Math.round(((y.wire.cameraBytes - lastBytes.bytes) * 8) / (Date.now() - lastBytes.at)) : 0, |
| }; |
| if (y.wire) lastBytes = { bytes: y.wire.cameraBytes, at: Date.now() }; |
| @@ -6718,12 +6766,24 @@ const run = { |
| return row.aSends === row.bAsks && row.aSends !== 'normal' ? row : false; |
| }, 5000); |
| check(sent, 'A sends B what B asks for (' + JSON.stringify(timeline[timeline.length - 1]) + ')'); |
| - const small = await until(async () => { |
| - const { sa } = await look('capped, encoder'); |
| - const w = sa.adapt[b] && sa.adapt[b].wire; |
| - return w && w.cameraHeight > 0 && w.cameraHeight <= 180 ? w : false; |
| - }, 15000); |
| - check(small, 'A’s camera to B is encoded at 180 lines or fewer (' + JSON.stringify(timeline[timeline.length - 1]) + ')'); |
| + // Minimal is a camera at 180 lines or fewer; audio only is none at all |
| + // (severe trouble at reduced steps two levels, straight there). |
| + if (down.bAsks === 'minimal') { |
| + const small = await until(async () => { |
| + const { sa } = await look('capped, encoder'); |
| + const w = sa.adapt[b] && sa.adapt[b].wire; |
| + return w && w.cameraHeight > 0 && w.cameraHeight <= 180 ? w : false; |
| + }, 15000); |
| + check(small, 'A’s camera to B is encoded at 180 lines or fewer (' + JSON.stringify(timeline[timeline.length - 1]) + ')'); |
| + } else { |
| + const idle = await until(async () => { |
| + const w1 = (await state(pageA)).adapt[b].wire; |
| + await sleep(2000); |
| + const w2 = (await state(pageA)).adapt[b].wire; |
| + return (await state(pageA)).adapt[b].cameraPaused && w2.cameraBytes - w1.cameraBytes < 2000 ? [w1.cameraBytes, w2.cameraBytes] : false; |
| + }, 12000); |
| + check(idle, 'A’s camera to B is paused at audio only, and sends nothing (' + JSON.stringify(idle) + ')'); |
| + } |
| const notice = (await state(pageA)).videoNotice || ''; |
| check(/^Sending (reduced video|only audio) to .+ \(their connection is weak\)$/.test(notice), |
| 'A is told it is sending B less (' + notice + ')'); |
| @@ -6733,30 +6793,38 @@ const run = { |
| }, a); |
| check(badge && badge.text === 'Low bandwidth' && /less video/.test(badge.label), |
| 'B’s tile for A says Low bandwidth, with words for a screen reader (' + JSON.stringify(badge) + ')'); |
| - const noBadge = await pageA.evaluate((id) => { |
| + const aBadge = await pageA.evaluate((id) => { |
| const el = document.querySelector('#tiles figure[data-tile="' + id + '"] .calls-tile-low'); |
| - return !el || el.hidden; |
| + return { shown: !!(el && !el.hidden), asks: window.meet.state().videoAsks[id] || 'normal' }; |
| }, b); |
| - check(noBadge, 'A, asking nothing of B, shows no badge on B’s tile'); |
| + check(aBadge.shown === (aBadge.asks !== 'normal'), |
| + 'A’s badge on B’s tile follows what A itself asks of B (' + JSON.stringify(aBadge) + ')'); |
| const audioBefore = (await look('capped, audio')).row.bAudioIn; |
| await sleep(3000); |
| const audioAfter = (await look('capped, audio')).row.bAudioIn; |
| check(audioAfter - audioBefore >= 100, 'audio from A keeps arriving at B (' + audioBefore + ' -> ' + audioAfter + ' packets in 3 s)'); |
| const hears = await pageB.evaluate((id) => window.meet.state().hearingList.includes(id), a); |
| check(hears, 'B still hears A'); |
| - // Lift the cap: the first step up on the real wait. |
| + // Lift the cap: the first step up on the real wait, counted from the |
| + // last step down — at audio only nothing arrives to suffer, so every |
| + // second from there is clean, cap or none. |
| await pageB.evaluate(() => window.__capDownlink(0)); |
| - const lifted = Date.now(); |
| + const levels = ['normal', 'reduced', 'minimal', 'audio']; |
| const from = (await look('lifted')).row.bAsks; |
| + let lastDown = 0; |
| + for (let i = 1; i < timeline.length; i++) { |
| + if (levels.indexOf(timeline[i].bAsks) > levels.indexOf(timeline[i - 1].bAsks)) lastDown = timeline[i].t; |
| + } |
| let up = null; |
| - while (!up && Date.now() - lifted < 65000) { |
| + while (!up && (Date.now() - t0) / 1000 - lastDown < 70) { |
| await sleep(1000); |
| const { row } = await look('lifted'); |
| if (row.bAsks !== from) up = row; |
| } |
| - const upAfter = (Date.now() - lifted) / 1000; |
| - check(up && upAfter >= 44 && upAfter <= 62, |
| - 'B steps A’s video up one level 45–60 s after the cap is lifted (' + from + ' -> ' + (up && up.bAsks) + ' after ' + upAfter.toFixed(1) + ' s)'); |
| + const upAfter = up ? up.t - lastDown : -1; |
| + check(up && levels.indexOf(up.bAsks) === levels.indexOf(from) - 1 && upAfter >= 44 && upAfter <= 62, |
| + 'B steps A’s video up one level 45–60 s after its last step down (' + from + ' -> ' + (up && up.bAsks) + |
| + ' ' + upAfter + ' s after the step at ' + lastDown + ' s)'); |
| // The rest on a shortened wait, all the way back to normal. |
| await pageB.evaluate(() => window.meet.adaptForTest({ upAfter: 4000 })); |
| const normal = await until(async () => { |