| index 82a46ac..1c58cbe 100644 |
| --- a/internal/calls/static/room.js |
| +++ b/internal/calls/static/room.js |
| @@ -111,6 +111,9 @@ |
| const rawInput = rawRow ? rawRow.querySelector('input[type="checkbox"]') : null; |
| const shareModeRow = $('share-mode-row'); |
| const pickShareMode = $('pick-share-mode'); |
| + // §15.30's own choice: Auto, Low bandwidth, Audio only. |
| + const videoQualityRow = $('video-quality-row'); |
| + const pickVideoQuality = $('pick-video-quality'); |
| |
| const DEVICE_KEY = 'meet.devices'; |
| const BACKOFF_MIN = 500; |
| @@ -395,6 +398,50 @@ |
| // counted as time a notice was showing: a suspended tab's timers stop, |
| // and the gap when they start again is not a notice someone saw. |
| const QUALITY_LOOK_MAX = 1000; |
| + // §15.30 (2026-10-28): adaptive video. Every receiver watches what |
| + // arrives from each sender and asks that sender, through its own media |
| + // report, for one of four levels; the sender applies it to that one |
| + // connection's sender parameters. Down quickly, up slowly, and a probe up |
| + // that fails doubles the wait before the next. The apps' VideoAdapt has |
| + // the same numbers; test/browser/room-adapt.test.mjs and |
| + // VideoAdaptTests hold both. |
| + const ADAPT_LEVELS = ['normal', 'reduced', 'minimal', 'audio']; |
| + const ADAPT_WINDOW = 10000; // the trouble window, and the time at a level before a step down |
| + const ADAPT_SEVERE_AFTER = 5000; // severe trouble steps two levels after this long |
| + const ADAPT_DOWN_FREEZES = 2; // camera freezes in the window |
| + const ADAPT_DOWN_LOSS = 0.05; // packets lost in the window, of all inbound |
| + const ADAPT_DOWN_RTT = 600; // ms, counted with at least one freeze |
| + const ADAPT_DOWN_JITTER_BUFFER = 500; // ms, the audio jitter buffer's mean delay |
| + const ADAPT_DOWN_DROPPED = 0.25; // frames dropped, of decoded and dropped, at least ADAPT_DOWN_DROPPED_MIN |
| + const ADAPT_DOWN_DROPPED_MIN = 10; |
| + const ADAPT_SEVERE_FREEZES = 5; |
| + const ADAPT_SEVERE_FREEZE_SECONDS = 4; // seconds frozen in the window |
| + const ADAPT_SEVERE_LOSS = 0.15; |
| + const ADAPT_CLEAN_LOSS = 0.02; // a clean second: no freeze, loss at most this, |
| + const ADAPT_CLEAN_RTT = 400; // a round trip under this (ms), |
| + const ADAPT_CLEAN_JITTER_BUFFER = 300; // and the jitter buffer under this (ms) |
| + const ADAPT_UP_AFTER = 45000; // clean this long at a level, then probe one up |
| + const ADAPT_PROBE_WATCH = 20000; // trouble this soon after a probe steps back |
| + const ADAPT_PROBE_JUDGE = 5000; // and is judged on at least this much of it |
| + const ADAPT_UP_MAX = 300000; // the probe's wait doubles to at most this |
| + // What each level sends (the sender's half). The camera: its bitrate, |
| + // frame rate, and the box its picture is fitted into (long side, short |
| + // side). The screen keeps its size and gives up frame rate first, and |
| + // only at minimal is it made smaller; it is never paused. |
| + const ADAPT_CAMERA = [null, |
| + { maxBitrate: 350000, maxFramerate: 15, long: 640, short: 360 }, |
| + { maxBitrate: 120000, maxFramerate: 8, long: 320, short: 180 }]; |
| + const ADAPT_SCREEN = [null, |
| + { maxBitrate: 1200000, maxFramerate: 10 }, |
| + { maxBitrate: 500000, maxFramerate: 5, long: 1280 }]; |
| + // An app older than this ignores a request; its video can only be hidden |
| + // here, which saves this device's work and not the connection. |
| + const ADAPT_MIN_NATIVE_VERSION = '0.4.14'; |
| + // How long the sender's "Sending reduced video to …" stays up. |
| + const ADAPT_NOTICE_FOR = 8000; |
| + // The person's own choice, in the Devices panel: Auto, Low bandwidth |
| + // (minimal from everyone), Audio only (no video either way). |
| + const VIDEO_SETTINGS = [['auto', 'Auto'], ['low', 'Low bandwidth'], ['audio', 'Audio only']]; |
| // §5.4, sending: "Optimise shared screen for". What a screen gives up |
| // first when the uplink or the encoder cannot carry all of it. The |
| // first is the default; sharePlan says what each one means. |
| @@ -468,6 +515,18 @@ |
| const screenSeen = new Map(); |
| let screenStalled = []; |
| const screenAsks = new Map(); |
| + // §15.30: peer id -> the adaptive watch's record of what arrives from |
| + // them; the asks this tab last put in its report (peer id -> level); |
| + // what each connection is sending at, as asked (on the entry); the |
| + // sender's notice and its timer; seconds at each level for the report; |
| + // and, for the gate, the watch's up wait. |
| + const adapt = new Map(); |
| + let videoAsks = {}; |
| + let adaptNoticeTimer = 0; |
| + let adaptNoticeShown = ''; |
| + const videoTime = new Map(); |
| + let adaptUpAfter = ADAPT_UP_AFTER; |
| + let adaptPolledAt = 0; |
| let shareRecovery = ''; |
| let screenStallTestUntil = 0; |
| // §15.22: each arriving screen's statistics at the last poll and when |
| @@ -821,19 +880,28 @@ |
| // tuneShare puts one connection's screen and camera where the mode |
| // wants them: the screen at its plan, the camera capped beside it — |
| // or, with nothing shared, the camera back at its defaults. |
| + // |
| + // §15.30: at the level this connection's peer asks of us |
| + // (entry.sentLevel), and with no camera at all to anyone while the |
| + // person's own Video quality is Audio only. |
| function tuneShare(entry) { |
| if (!entry) return; |
| const sharing = !!(share && entry.shareTx); |
| + const level = entry.sentLevel || 0; |
| if (sharing) { |
| const s = share.track.getSettings ? share.track.getSettings() : {}; |
| - const plan = sharePlan(shareMode(), s.width, s.height); |
| + const plan = screenAtLevel(sharePlan(shareMode(), s.width, s.height), level, s.width, s.height); |
| tuneSender(entry.shareTx.sender, plan.encoding, plan.degradationPreference); |
| } |
| const camera = senderFor(entry, 'video'); |
| const track = camera && camera.sender && camera.sender.track; |
| if (!track) return; |
| const settings = track.getSettings ? track.getSettings() : {}; |
| - tuneSender(camera.sender, cameraCap(sharing, settings.height || 0)); |
| + const encoding = cameraAtLevel(videoSetting() === 'audio' ? 3 : level, sharing, settings.width || 0, settings.height || 0); |
| + // The camera watch's to know: a camera paused on purpose encodes |
| + // nothing, and that is not a stalled encoder. |
| + entry.cameraPaused = encoding.active === false; |
| + tuneSender(camera.sender, encoding); |
| } |
| |
| // startLocal walks down a ladder: the remembered devices, then the |
| @@ -2044,6 +2112,10 @@ |
| addShareTo(entry); |
| upgradeWhenQuiet(entry); |
| adoptWhenQuiet(id, entry); |
| + // §15.30: a connection that settles is given what its peer asks of |
| + // us, and the person's own Audio only — a new connection's senders |
| + // have no parameters to set until it has. |
| + if (pc.signalingState === 'stable' && (entry.sentLevel || videoSetting() === 'audio')) tuneShare(entry); |
| // And it is when an ask the negotiation rounds ran out on gets |
| // its turn (Codex S4-8): a connection that was mid-negotiation is |
| // the usual reason they ran out, and this is it settling. |
| @@ -4389,6 +4461,19 @@ |
| } |
| out.hearing = ids; |
| } |
| + // §15.30: what they ask each sender for, the wire's levels only. |
| + if (value.video && typeof value.video === 'object' && !Array.isArray(value.video)) { |
| + const asks = {}; |
| + let n = 0; |
| + for (const [id, level] of Object.entries(value.video)) { |
| + if (n >= 15) break; |
| + if (/^[0-9a-f]{32}$/.test(id) && ['reduced', 'minimal', 'audio'].includes(level)) { |
| + asks[id] = level; |
| + n += 1; |
| + } |
| + } |
| + if (n) out.video = asks; |
| + } |
| return out; |
| } |
| |
| @@ -4406,6 +4491,9 @@ |
| if (m.noSoundFrom && m.noSoundFrom.length) key += ':' + m.noSoundFrom.slice().sort().join(','); |
| if (m.screenStalled && m.screenStalled.length) key += '/' + m.screenStalled.slice().sort().join(','); |
| if (Array.isArray(m.hearing)) key += '|' + m.hearing.slice().sort().join(','); |
| + if (m.video && Object.keys(m.video).length) { |
| + key += '>' + Object.keys(m.video).sort().map((id) => id + '=' + m.video[id]).join(','); |
| + } |
| return key; |
| } |
| |
| @@ -4415,7 +4503,9 @@ |
| function localMedia() { |
| const hasAudio = !!(local && local.getAudioTracks().length); |
| const hasVideo = !!(local && local.getVideoTracks().length); |
| - return { muted: !!prefs.muted || !hasAudio, cameraOff: !!prefs.cameraOff || !hasVideo }; |
| + // §15.30: Audio only stops the camera going to anyone, so it is off as |
| + // far as anyone watching can tell. |
| + return { muted: !!prefs.muted || !hasAudio, cameraOff: !!prefs.cameraOff || !hasVideo || videoSetting() === 'audio' }; |
| } |
| |
| // mediaPayload is the report itself: §15.19's two booleans, and |
| @@ -4424,7 +4514,7 @@ |
| // is good news, and its presence tells the others this tab says who it |
| // hears. `plain` is a server that has refused the extras once. Pure, |
| // and lifted by name in the node test. |
| - function mediaPayload(mine, blocked, silentFrom, plain, stalled, heard) { |
| + function mediaPayload(mine, blocked, silentFrom, plain, stalled, heard, asks) { |
| const out = { muted: !!mine.muted, cameraOff: !!mine.cameraOff }; |
| if (plain) return out; |
| if (blocked) out.audioBlocked = true; |
| @@ -4432,6 +4522,8 @@ |
| // §15.21: whose screens have stalled here, bad news only, like the rest. |
| if (stalled && stalled.length) out.screenStalled = stalled.slice(0, 15); |
| if (Array.isArray(heard)) out.hearing = heard.slice(0, 15); |
| + // §15.30: what this tab asks each sender for, only those below normal. |
| + if (asks && Object.keys(asks).length) out.video = Object.assign({}, asks); |
| return out; |
| } |
| |
| @@ -4447,7 +4539,7 @@ |
| // as muted or not matters more than being heard as unable to hear. |
| function reportMedia() { |
| if (!state.admitted || state.stopped || !state.peer) return; |
| - const now = mediaPayload(localMedia(), audioBlocked, noSoundList(), !mediaExtras, screenStalled, hearingList()); |
| + const now = mediaPayload(localMedia(), audioBlocked, noSoundList(), !mediaExtras, screenStalled, hearingList(), videoAsks); |
| const key = mediaKey(now); |
| if (key === mediaSent) return; |
| mediaSent = key; |
| @@ -4477,10 +4569,13 @@ |
| learnMedia(rp.peer, rp.media); |
| continue; |
| } |
| - const now = mediaPayload(localMedia(), audioBlocked, noSoundList(), !mediaExtras, screenStalled, hearingList()); |
| + const now = mediaPayload(localMedia(), audioBlocked, noSoundList(), !mediaExtras, screenStalled, hearingList(), videoAsks); |
| mediaSent = sameMedia(mediaFrom(rp.media), now) ? mediaKey(now) : ''; |
| } |
| reportMedia(); |
| + // §15.30: and what each of them asks of us now. |
| + answerVideoAsks(); |
| + for (const id of pcs.keys()) markAdapt(id); |
| } |
| |
| // learnMedia records a roster's or a peer event's word on one peer. |
| @@ -4503,6 +4598,398 @@ |
| announceMedia(mediaChange(msg.name || names.get(id) || 'Someone', before, next)); |
| markTile(id, next); |
| answerScreenStalls(); |
| + answerVideoAsks(); |
| + markAdapt(id); |
| + } |
| + |
| + // --------------------------------------------------------------- |
| + // Adaptive video (§15.30, 2026-10-28) |
| + // --------------------------------------------------------------- |
| + // |
| + // Paul, after a Dublin–London call whose guest was on café Wi-Fi in |
| + // Safari, relayed, at 840–1300 ms round trips, seeing up to seven |
| + // freezes every thirty seconds in his video: "Low-bandwidth with |
| + // auto-switching up and down." Audio mattered most. |
| + // |
| + // It is a mesh, so every pair has its own sender, and the one who can |
| + // see the trouble is the receiver. So each receiver watches what arrives |
| + // from each sender — camera freezes, packet loss, the jitter buffer, the |
| + // round trip, frames dropped — and asks that sender for a level in its |
| + // own media report (`video`, peer id -> level, only the ones below |
| + // normal). The sender reads its own id in each report and sets that one |
| + // connection's sender parameters: no renegotiation, and audio is never |
| + // touched. A shared screen keeps its size and gives up frame rate first, |
| + // and is never paused; while one is going, the camera gives way first. |
| + |
| + // adaptRecord is one sender's watch, on the receiving side. `upAfter` is |
| + // the clean time before the first probe up (ADAPT_UP_AFTER, shortened |
| + // only by the gate's hook). Pure, as is everything down to adaptAsks; |
| + // test/browser/room-adapt.test.mjs lifts them by name. |
| + function adaptRecord(upAfter) { |
| + const base = upAfter || ADAPT_UP_AFTER; |
| + return { level: 0, since: null, samples: [], cleanSince: null, upBase: base, upWait: base, |
| + probeAt: null, probeFrom: 0, downs: 0, ups: 0 }; |
| + } |
| + |
| + // adaptSample is one second of what arrived from a sender, from |
| + // qualityPeer's deltas: camera freezes (only while the camera is to be |
| + // watched — on, and the tab visible — because a still screen's gaps and |
| + // a hidden tab's unpainted frames are not freezes), packets and loss |
| + // across audio, camera and screen, the round trip, the audio jitter |
| + // buffer, and frames decoded and dropped. Null when nothing at all |
| + // arrived and nothing was measured: a connection that is down is |
| + // §15.28's to mend, and says nothing about the link's width. |
| + function adaptSample(q, cameraWatched) { |
| + const inb = (q && q.in) || {}; |
| + const a = inb.audio || {}; |
| + const v = inb.video || {}; |
| + const s = inb.screen || {}; |
| + const packets = (a.packets || 0) + (v.packets || 0) + (s.packets || 0); |
| + const lost = (a.lost || 0) + (v.lost || 0) + (s.lost || 0); |
| + const rtt = q && typeof q.rtt === 'number' ? q.rtt : null; |
| + if (packets + lost === 0 && rtt === null) return null; |
| + return { |
| + freezes: cameraWatched ? v.freezes || 0 : 0, |
| + freezeSeconds: cameraWatched ? v.freezeSeconds || 0 : 0, |
| + packets, |
| + lost, |
| + rtt, |
| + jitterBufferMs: typeof a.jitterBufferMs === 'number' ? a.jitterBufferMs : null, |
| + decoded: (v.decoded || 0) + (s.decoded || 0), |
| + dropped: (v.dropped || 0) + (s.dropped || 0), |
| + }; |
| + } |
| + |
| + // adaptWindow sums the samples in the window: counts added up, the |
| + // round trip as their median (one spike is not a slow link), the jitter |
| + // buffer as their mean. |
| + function adaptWindow(samples) { |
| + const w = { freezes: 0, freezeSeconds: 0, packets: 0, lost: 0, decoded: 0, dropped: 0, rtt: null, jitterBuffer: null }; |
| + const rtts = []; |
| + const jbs = []; |
| + for (const x of samples) { |
| + w.freezes += x.freezes; |
| + w.freezeSeconds += x.freezeSeconds; |
| + w.packets += x.packets; |
| + w.lost += x.lost; |
| + w.decoded += x.decoded; |
| + w.dropped += x.dropped; |
| + if (x.rtt !== null) rtts.push(x.rtt); |
| + if (x.jitterBufferMs !== null) jbs.push(x.jitterBufferMs); |
| + } |
| + if (rtts.length) { |
| + rtts.sort((p, q) => p - q); |
| + w.rtt = rtts[Math.floor(rtts.length / 2)]; |
| + } |
| + if (jbs.length) w.jitterBuffer = jbs.reduce((p, q) => p + q, 0) / jbs.length; |
| + w.loss = w.packets + w.lost > 0 ? w.lost / (w.packets + w.lost) : 0; |
| + return w; |
| + } |
| + |
| + // adaptTrouble is a window's verdict: 'severe', 'trouble' or ''. |
| + function adaptTrouble(w) { |
| + if (w.freezes >= ADAPT_SEVERE_FREEZES || w.freezeSeconds >= ADAPT_SEVERE_FREEZE_SECONDS || |
| + w.loss > ADAPT_SEVERE_LOSS) { |
| + return 'severe'; |
| + } |
| + if (w.freezes >= ADAPT_DOWN_FREEZES || w.loss > ADAPT_DOWN_LOSS || |
| + (w.rtt !== null && w.rtt >= ADAPT_DOWN_RTT && w.freezes >= 1) || |
| + (w.jitterBuffer !== null && w.jitterBuffer >= ADAPT_DOWN_JITTER_BUFFER) || |
| + (w.dropped >= ADAPT_DOWN_DROPPED_MIN && w.dropped >= ADAPT_DOWN_DROPPED * (w.decoded + w.dropped))) { |
| + return 'trouble'; |
| + } |
| + return ''; |
| + } |
| + |
| + // adaptClean is whether one second was clean, for the slow way up. |
| + function adaptClean(x) { |
| + const n = x.packets + x.lost; |
| + return x.freezes === 0 && x.freezeSeconds === 0 && (n === 0 || x.lost / n <= ADAPT_CLEAN_LOSS) && |
| + (x.rtt === null || x.rtt < ADAPT_CLEAN_RTT) && |
| + (x.jitterBufferMs === null || x.jitterBufferMs < ADAPT_CLEAN_JITTER_BUFFER); |
| + } |
| + |
| + // adaptMove changes the level, and starts the watch again from nothing: |
| + // the new level is judged on what it does, not on what the old one did. |
| + function adaptMove(rec, now, level, up) { |
| + if (up) rec.ups += 1; |
| + else rec.downs += 1; |
| + rec.level = level; |
| + rec.since = now; |
| + rec.samples = []; |
| + rec.cleanSince = null; |
| + if (!up) rec.probeAt = null; |
| + return level; |
| + } |
| + |
| + // adaptStep is one look, about once a second, with that second's sample |
| + // (or null for none). Returns the level, 0 normal to 3 audio only. |
| + // |
| + // down one level once the window at this level has been trouble for |
| + // ADAPT_WINDOW; two at once for severe trouble, after |
| + // ADAPT_SEVERE_AFTER |
| + // up one level after `upWait` of clean seconds in a row, as a |
| + // probe: trouble within ADAPT_PROBE_WATCH of it (judged on at |
| + // least ADAPT_PROBE_JUDGE) steps straight back and doubles the |
| + // wait, to at most ADAPT_UP_MAX; a probe that holds puts the |
| + // wait back to its base |
| + function adaptStep(rec, now, sample) { |
| + if (rec.since === null) rec.since = now; |
| + if (!sample) return rec.level; |
| + rec.samples.push(Object.assign({ at: now }, sample)); |
| + while (rec.samples.length && rec.samples[0].at <= now - ADAPT_WINDOW) rec.samples.shift(); |
| + const observed = now - rec.since; |
| + const verdict = adaptTrouble(adaptWindow(rec.samples)); |
| + if (adaptClean(sample)) { |
| + if (rec.cleanSince === null) rec.cleanSince = now; |
| + } else { |
| + rec.cleanSince = null; |
| + } |
| + const top = ADAPT_LEVELS.length - 1; |
| + if (rec.probeAt !== null) { |
| + if (now - rec.probeAt < ADAPT_PROBE_WATCH) { |
| + if (verdict && observed >= ADAPT_PROBE_JUDGE) { |
| + rec.upWait = Math.min(rec.upWait * 2, ADAPT_UP_MAX); |
| + return adaptMove(rec, now, rec.probeFrom, false); |
| + } |
| + } else { |
| + rec.probeAt = null; |
| + rec.upWait = rec.upBase; |
| + } |
| + } |
| + if (verdict === 'severe' && observed >= ADAPT_SEVERE_AFTER && rec.level < top) { |
| + return adaptMove(rec, now, Math.min(top, rec.level + 2), false); |
| + } |
| + if (verdict && observed >= ADAPT_WINDOW && rec.level < top) { |
| + return adaptMove(rec, now, rec.level + 1, false); |
| + } |
| + if (rec.level > 0 && rec.cleanSince !== null && now - rec.cleanSince >= rec.upWait) { |
| + const from = rec.level; |
| + adaptMove(rec, now, from - 1, true); |
| + rec.probeAt = now; |
| + rec.probeFrom = from; |
| + } |
| + return rec.level; |
| + } |
| + |
| + // videoFloor is the person's own setting as a level: Low bandwidth asks |
| + // everyone for minimal, Audio only for no video; Auto leaves it to the |
| + // watch. |
| + function videoFloor(setting) { |
| + if (setting === 'audio') return 3; |
| + if (setting === 'low') return 2; |
| + return 0; |
| + } |
| + |
| + // adaptAsks is the `video` object for the report: every peer whose level |
| + // — the watch's or the setting's, whichever is lower — is below normal. |
| + // `levels` is peer id -> the watch's level. |
| + function adaptAsks(levels, setting) { |
| + const out = {}; |
| + const floor = videoFloor(setting); |
| + for (const [id, level] of levels) { |
| + const at = Math.max(level || 0, floor); |
| + if (at > 0 && Object.keys(out).length < 15) out[id] = ADAPT_LEVELS[at]; |
| + } |
| + return out; |
| + } |
| + |
| + // askedOf is what one peer's report asks of us, as a level index. |
| + function askedOf(media, me) { |
| + const at = media && media.video && me ? ADAPT_LEVELS.indexOf(media.video[me]) : -1; |
| + return at > 0 ? at : 0; |
| + } |
| + |
| + // cameraAtLevel is the camera's encoding for one receiver at a level. |
| + // While a screen is going out the camera gives way first: normal is |
| + // §5.4's cap beside the screen, and every level below is one lower for |
| + // the camera, so a receiver at minimal gets the screen and no camera. |
| + // `width` and `height` are the camera's own, fitted by their long and |
| + // short sides so a portrait camera is boxed the right way round. |
| + function cameraAtLevel(level, sharing, width, height) { |
| + const at = sharing && level > 0 ? Math.min(3, level + 1) : level; |
| + if (at <= 0) return Object.assign({ maxFramerate: undefined, active: true }, cameraCap(sharing, height || 0)); |
| + const c = ADAPT_CAMERA[Math.min(at, 2)]; |
| + const long = Math.max(width || 0, height || 0); |
| + const short = Math.min(width || 0, height || 0); |
| + return { |
| + maxBitrate: c.maxBitrate, |
| + maxFramerate: c.maxFramerate, |
| + scaleResolutionDownBy: Math.max(1, long / c.long, short / c.short), |
| + active: at < 3, |
| + }; |
| + } |
| + |
| + // screenAtLevel is sharePlan's answer for one receiver at a level: the |
| + // frame rate and bitrate lowered first, the size kept, and only at |
| + // minimal and below the long side brought to ADAPT_SCREEN's. Resolution |
| + // is held under pressure from then on (maintain-resolution), because a |
| + // screen is for reading. Never paused. |
| + function screenAtLevel(plan, level, width, height) { |
| + if (level <= 0) return plan; |
| + const c = ADAPT_SCREEN[Math.min(level, 2)]; |
| + const encoding = Object.assign({}, plan.encoding, { |
| + maxFramerate: c.maxFramerate, |
| + maxBitrate: Math.min(plan.encoding.maxBitrate || c.maxBitrate, c.maxBitrate), |
| + }); |
| + if (c.long) { |
| + const longest = Math.max(width || 0, height || 0); |
| + encoding.scaleResolutionDownBy = Math.max(plan.encoding.scaleResolutionDownBy || 1, longest > c.long ? longest / c.long : 1); |
| + } |
| + return { contentHint: plan.contentHint, degradationPreference: 'maintain-resolution', encoding }; |
| + } |
| + |
| + // honoursAsks is whether a peer's client acts on a request: every |
| + // browser (its page is this server's), and an app from |
| + // ADAPT_MIN_NATIVE_VERSION on. |
| + function honoursAsks(client) { |
| + const c = parseClient(client || ''); |
| + if (!c) return !client; |
| + return compareVersions(c.version, ADAPT_MIN_NATIVE_VERSION) >= 0; |
| + } |
| + |
| + // adaptNoticeWords is the sender's quiet notice for the people it is |
| + // sending less to, by name: '' for nobody. |
| + function adaptNoticeWords(people) { |
| + if (!people.length) return ''; |
| + const names = people.map((p) => p.name); |
| + const list = names.length === 1 ? names[0] |
| + : names.slice(0, -1).join(', ') + ' and ' + names[names.length - 1]; |
| + const weak = names.length === 1 ? '(their connection is weak)' : '(their connections are weak)'; |
| + if (people.every((p) => p.level >= 3)) return 'Sending only audio to ' + list + ' ' + weak; |
| + return 'Sending reduced video to ' + list + ' ' + weak; |
| + } |
| + |
| + // videoSetting is the person's choice, Auto unless they made one. |
| + function videoSetting() { |
| + return VIDEO_SETTINGS.some(([v]) => v === prefs.videoQuality) ? prefs.videoQuality : 'auto'; |
| + } |
| + |
| + // adaptLook is one connection's second, from the stats walk pollFrames |
| + // already makes: qualityReading's parse of it, the delta from the last |
| + // one, and a step of that sender's watch. |
| + function adaptLook(id, entry, stats, now) { |
| + const reading = qualityReading(stats, (r) => qualityKind(id, entry, r)); |
| + const prev = entry.adaptReading; |
| + entry.adaptReading = { pc: entry.pc, reading, at: now }; |
| + if (!prev || prev.pc !== entry.pc || now <= prev.at) return; |
| + const q = qualityPeer(prev.reading, reading, (now - prev.at) / 1000); |
| + const media = remoteMedia.get(id) || null; |
| + const watched = !(media && media.cameraOff) && !(typeof document !== 'undefined' && document.hidden); |
| + let rec = adapt.get(id); |
| + if (!rec) { |
| + rec = adaptRecord(adaptUpAfter); |
| + adapt.set(id, rec); |
| + } |
| + const before = rec.level; |
| + adaptStep(rec, now, adaptSample(q, watched)); |
| + if (rec.level !== before) { |
| + console.info('calls: video from ' + id.slice(0, 6) + ' ' + ADAPT_LEVELS[before] + ' -> ' + |
| + ADAPT_LEVELS[rec.level] + (rec.level < before ? ' (probe)' : '')); |
| + } |
| + } |
| + |
| + // answerAdapt runs after each stats walk: the asks this tab makes, sent |
| + // when they change; the tiles' badges; and the seconds at each level, |
| + // asked and sent, for the next quality report. |
| + function answerAdapt(now, dt) { |
| + for (const id of Array.from(adapt.keys())) if (!pcs.has(id)) adapt.delete(id); |
| + const levels = new Map(); |
| + for (const id of pcs.keys()) levels.set(id, adapt.has(id) ? adapt.get(id).level : 0); |
| + const asks = adaptAsks(levels, videoSetting()); |
| + const changed = JSON.stringify(Object.entries(asks).sort()) !== JSON.stringify(Object.entries(videoAsks).sort()); |
| + videoAsks = asks; |
| + if (changed) { |
| + reportMedia(); |
| + for (const id of pcs.keys()) markAdapt(id); |
| + } |
| + const step = Math.min(Math.max(0, dt || 0), 2000) / 1000; |
| + for (const [id, entry] of pcs) { |
| + const asked = ADAPT_LEVELS.indexOf(asks[id] || 'normal'); |
| + const sent = entry.sentLevel || 0; |
| + if (!step || (asked <= 0 && sent <= 0)) continue; |
| + let t = videoTime.get(id); |
| + if (!t) { |
| + t = { asked: [0, 0, 0], sent: [0, 0, 0] }; |
| + videoTime.set(id, t); |
| + } |
| + if (asked > 0) t.asked[asked - 1] += step; |
| + if (sent > 0) t.sent[sent - 1] += step; |
| + } |
| + } |
| + |
| + // answerVideoAsks is the sender's half: each connection's senders set |
| + // to what that peer last asked of us, and the notice when it changes. |
| + function answerVideoAsks() { |
| + let moved = false; |
| + for (const [id, entry] of pcs) { |
| + const level = askedOf(remoteMedia.get(id), state.peer); |
| + if ((entry.sentLevel || 0) === level) continue; |
| + console.info('calls: ' + id.slice(0, 6) + ' asks for ' + ADAPT_LEVELS[level] + ' video'); |
| + if (level > (entry.sentLevel || 0)) moved = true; |
| + entry.sentLevel = level; |
| + tuneShare(entry); |
| + } |
| + const people = []; |
| + for (const [id, entry] of pcs) { |
| + if (entry.sentLevel > 0) people.push({ name: names.get(id) || 'Someone', level: entry.sentLevel }); |
| + } |
| + const words = adaptNoticeWords(people); |
| + if (!words) { |
| + showAdaptNotice(''); |
| + } else if (moved && words !== adaptNoticeShown) { |
| + showAdaptNotice(words); |
| + } |
| + } |
| + |
| + // applyVideoSetting is the person's choice taking effect: what this tab |
| + // asks of everyone, its own camera to everyone, and its report. |
| + function applyVideoSetting() { |
| + console.info('calls: video quality ' + videoSetting()); |
| + answerAdapt(Date.now(), 0); |
| + for (const entry of pcs.values()) tuneShare(entry); |
| + applyToggles(); |
| + for (const id of pcs.keys()) markAdapt(id); |
| + } |
| + |
| + // showAdaptNotice puts the sender's notice up for ADAPT_NOTICE_FOR, once |
| + // per change, and says it to a screen reader politely. |
| + function showAdaptNotice(words) { |
| + adaptNoticeShown = words; |
| + const el = $('video-notice'); |
| + if (adaptNoticeTimer) clearTimeout(adaptNoticeTimer); |
| + adaptNoticeTimer = 0; |
| + if (!el) return; |
| + el.textContent = words; |
| + el.hidden = !words; |
| + if (!words) return; |
| + announceMedia(words); |
| + adaptNoticeTimer = setTimeout(() => { |
| + el.hidden = true; |
| + el.textContent = ''; |
| + }, ADAPT_NOTICE_FOR); |
| + } |
| + |
| + // markAdapt draws one tile's level: the badge in its caption while this |
| + // tab asks that person for less than normal, and, at audio only, the |
| + // cover over their picture — which also hides it here for an app too |
| + // old to send less. |
| + function markAdapt(id) { |
| + const t = tiles.get(id); |
| + if (!t || !t.lowBadge) return; |
| + const level = ADAPT_LEVELS.indexOf(videoAsks[id] || 'normal'); |
| + const who = names.get(id) || 'them'; |
| + const own = videoSetting() !== 'auto'; |
| + t.lowBadge.hidden = level <= 0; |
| + t.lowBadge.title = level <= 0 ? '' : (own |
| + ? 'Asking ' + who + ' for less video, as your Video quality setting says' |
| + : 'Asking ' + who + ' for less video while your connection is weak'); |
| + t.lowBadge.setAttribute('aria-label', level <= 0 ? '' : 'Low bandwidth: ' + t.lowBadge.title); |
| + const media = remoteMedia.get(id) || null; |
| + const paused = level >= 3 && !(media && media.cameraOff); |
| + t.videoPaused.hidden = !paused; |
| + if (t.videoPausedOld) t.videoPausedOld.hidden = !paused || honoursAsks(clients.get(id) || ''); |
| + t.figure.classList.toggle('calls-tile-video-paused', paused); |
| } |
| |
| // --------------------------------------------------------------- |
| @@ -5175,6 +5662,19 @@ |
| return { call, peers }; |
| } |
| |
| + // withVideoTime adds §15.30's seconds at each level, asked and sent, to |
| + // one peer's events, only where there were any. Pure, and lifted by name |
| + // in the node test. |
| + function withVideoTime(events, t) { |
| + if (!t) return events; |
| + const out = Object.assign({}, events || {}); |
| + for (const k of ['asked', 'sent']) { |
| + if (!t[k].some((x) => x > 0)) continue; |
| + out[k + 'Seconds'] = t[k].map((x) => Math.round(Math.min(x, QUALITY_MAX_SECONDS) * 10) / 10); |
| + } |
| + return Object.keys(out).length ? out : events; |
| + } |
| + |
| // qualityReport is the body POSTed to /m/{sqid}/quality |
| // (protocol.QualityReport). `o.peers` is [{id, prev, now, ladder, |
| // reattaches, events}]: the two readings, the §15.22 ladder's steps |
| @@ -5319,9 +5819,11 @@ |
| ladder: shareCountsBy.get(id) || null, |
| reattaches: reattachesBy.get(id) || 0, |
| stalls: stallsBy.get(id) || null, |
| - events: events.peers.get(id) || null, |
| + events: withVideoTime(events.peers.get(id) || null, videoTime.get(id)), |
| }); |
| } |
| + videoTime.clear(); |
| + if (videoSetting() !== 'auto') events.call.videoSetting = videoSetting(); |
| for (const id of Array.from(qualityPrev.keys())) if (!pcs.has(id)) qualityPrev.delete(id); |
| shareCountsBy.clear(); |
| reattachesBy.clear(); |
| @@ -6338,6 +6840,31 @@ |
| return { muted, off }; |
| } |
| |
| + // adaptMarks are §15.30's: the "Low bandwidth" badge, the vendored |
| + // badge from the page's template so the state is a word and not a |
| + // colour, and the cover for a person whose video is paused to keep their |
| + // audio clear, with the line for an app too old to send less. |
| + function adaptMarks() { |
| + const badge = document.createElement('span'); |
| + badge.className = 'calls-tile-low'; |
| + badge.hidden = true; |
| + const badgeMark = $('tile-low-bandwidth'); |
| + if (badgeMark) badge.append(badgeMark.content.cloneNode(true)); |
| + else badge.textContent = 'Low bandwidth'; |
| + const cover = document.createElement('div'); |
| + cover.className = 'calls-tile-off calls-tile-video-paused-cover'; |
| + cover.hidden = true; |
| + const coverMark = $('tile-video-paused'); |
| + if (coverMark) cover.append(coverMark.content.cloneNode(true)); |
| + else cover.textContent = 'Video paused to keep audio clear'; |
| + const old = document.createElement('span'); |
| + old.className = 'calls-tile-paused-old'; |
| + old.textContent = 'Their app can’t send less video, so it’s only hidden here'; |
| + old.hidden = true; |
| + cover.append(old); |
| + return { badge, cover, old }; |
| + } |
| + |
| // markTile shows or hides one tile's marks. `media` null is unknown. |
| function markTile(key, media) { |
| const t = tiles.get(key); |
| @@ -6345,6 +6872,7 @@ |
| t.muted.hidden = !(media && media.muted); |
| t.off.hidden = !(media && media.cameraOff); |
| t.figure.classList.toggle('calls-tile-camera-off', !!(media && media.cameraOff)); |
| + if (t.lowBadge) markAdapt(key); |
| drawHearing(key); |
| } |
| |
| @@ -7130,7 +7658,7 @@ |
| // live-looking select that silently does nothing is the worse half |
| // of that, so they are disabled and say so. |
| if (devicesToggle) devicesToggle.disabled = !on; |
| - for (const el of [pickCamera, pickMic, pickSpeaker, rawInput, pickShareMode]) { |
| + for (const el of [pickCamera, pickMic, pickSpeaker, rawInput, pickShareMode, pickVideoQuality]) { |
| if (el) el.disabled = !on; |
| } |
| } |
| @@ -7332,9 +7860,16 @@ |
| paused.append(said, show); |
| paused.addEventListener('click', () => playSelf(t)); |
| } |
| + // §15.30's two, on another person's tile: the badge while this tab |
| + // asks them for less video, and the cover at audio only. |
| + const low = marks && !isSelf ? adaptMarks() : null; |
| if (marks) { |
| nameEl.after(marks.muted); |
| figure.append(marks.off); |
| + if (low) { |
| + marks.muted.after(low.badge); |
| + figure.append(low.cover); |
| + } |
| if (paused) figure.append(paused); |
| // After Play, so the caption's buttons keep their order. |
| label.append(hear.box, hear.notice); |
| @@ -7346,6 +7881,7 @@ |
| t = { |
| figure, video, label, nameEl, note, retry, play, |
| muted: marks && marks.muted, off: marks && marks.off, |
| + lowBadge: low && low.badge, videoPaused: low && low.cover, videoPausedOld: low && low.old, |
| hear, hearShown: '', |
| remove: null, stream: null, track: null, self: isSelf, |
| // What judgeRender remembers between polls: the element's own |
| @@ -7774,6 +8310,25 @@ |
| }); |
| } |
| |
| + // §15.30's "Video quality". Applied at once: the asks this tab makes of |
| + // everyone, and — for Audio only — no camera to anyone, reported as off. |
| + // Remembered with the devices, so the next call starts the same way. |
| + if (pickVideoQuality && videoQualityRow) { |
| + for (const [value, label] of VIDEO_SETTINGS) { |
| + const option = document.createElement('option'); |
| + option.value = value; |
| + option.textContent = label; |
| + pickVideoQuality.append(option); |
| + } |
| + pickVideoQuality.value = videoSetting(); |
| + videoQualityRow.hidden = false; |
| + pickVideoQuality.addEventListener('change', () => { |
| + prefs.videoQuality = pickVideoQuality.value; |
| + savePrefs(); |
| + applyVideoSetting(); |
| + }); |
| + } |
| + |
| // §5.4's gesture. The handler is not async: a rejection escaping one |
| // goes where neither a person nor a gate could see it, so the work is |
| // a named function with a catch of its own — the same shape as the |
| @@ -8165,7 +8720,7 @@ |
| // an iPhone's camera paused. Disabled (camera off) is the person's |
| // own doing, and no more a stall. |
| if (!tx || !sends || !track || track.readyState === 'ended' || track.muted || |
| - track.enabled === false || |
| + track.enabled === false || entry.cameraPaused || |
| !report || typeof report.framesEncoded !== 'number') { |
| entry.cameraFrames = -1; |
| entry.cameraStalls = 0; |
| @@ -8430,6 +8985,8 @@ |
| // §15.26: the quality report reads this walk rather than making |
| // one of its own. |
| entry.statsReport = stats; |
| + // §15.30: and so does the adaptive watch. |
| + adaptLook(id, entry, stats, Date.now()); |
| const outbound = []; |
| // What the decoder is delivering, per tile: the fallback frame |
| // count where there is no requestVideoFrameCallback, and — in |
| @@ -8481,6 +9038,10 @@ |
| } |
| watchScreens(screens, screenReports); |
| answerScreenStalls(); |
| + const at = Date.now(); |
| + answerAdapt(at, adaptPolledAt ? at - adaptPolledAt : 0); |
| + adaptPolledAt = at; |
| + answerVideoAsks(); |
| qualityTick(); |
| } |
| |
| @@ -8638,6 +9199,17 @@ |
| recoveryForTest(on) { |
| recoveryOn = !!on; |
| }, |
| + // §15.30, for the gate: the clean time before the watch's first probe |
| + // up, shortened so a scenario can see a whole climb back; at least a |
| + // second, at most the real one, and only for watches made after it. |
| + adaptForTest(o) { |
| + const up = Number(o && o.upAfter) || ADAPT_UP_AFTER; |
| + adaptUpAfter = Math.max(1000, Math.min(up, ADAPT_UP_AFTER)); |
| + for (const r of adapt.values()) { |
| + r.upBase = adaptUpAfter; |
| + r.upWait = Math.min(r.upWait, adaptUpAfter); |
| + } |
| + }, |
| stallScreenForTest(ms) { |
| screenStallTestUntil = Date.now() + Math.max(0, Math.min(Number(ms) || 0, 60000)); |
| }, |
| @@ -8742,6 +9314,19 @@ |
| shareMode: shareMode(), |
| shareSend: Object.fromEntries( |
| Array.from(pcs, ([id, entry]) => [id, entry.shareSend || null])), |
| + // §15.30: per peer, the watch's level and what this tab asks of |
| + // them; what each asks of us and what we send them at; the |
| + // person's setting; and the sender's notice as it stands. |
| + adapt: Object.fromEntries(Array.from(pcs.keys(), (id) => { |
| + const r = adapt.get(id); |
| + const e = pcs.get(id); |
| + return [id, { level: r ? ADAPT_LEVELS[r.level] : 'normal', asked: videoAsks[id] || 'normal', |
| + sent: ADAPT_LEVELS[(e && e.sentLevel) || 0], downs: r ? r.downs : 0, ups: r ? r.ups : 0, |
| + upWait: r ? r.upWait : adaptUpAfter, cameraPaused: !!(e && e.cameraPaused) }]; |
| + })), |
| + videoAsks: Object.assign({}, videoAsks), |
| + videoSetting: videoSetting(), |
| + videoNotice: adaptNoticeShown, |
| // §15.19: what this tab reports, and what it has been told, by |
| // peer id. Peers that never reported are absent. |
| media: localMedia(), |