oficina / meet Public

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

Plain git — no account needed to clone.

Download

Download this file

1// native-motion.mjs — the app shares a 1080p page scrolling at 30 fps
2// and the browser measures what actually arrives: frames a second,
3// frame size and freezes on the received screen, beside the app's own
4// account of its sender (encoder, limitation, bitrate, path).
5//
6// Written for a report on 2026-09-24 that a screen shared from the Mac
7// app "isn't keeping up with what I'm doing on the screen". It measures
8// each "Optimise for" mode twice — on an open local link, and with the
9// app's uplink estimate capped the way a thin home connection caps it —
10// and holds motion (and the balanced default) to a frame rate a scroll
11// can be followed at.
12//
13// Run by TestNativeMacScreenMotion (make native-browser). Environment:
14// MOTION_MODES comma-separated modes, default "balanced,detail,motion"
15// MOTION_UPLINKS comma-separated caps in bit/s, 0 for none, default "0,1500000"
16// MOTION_ASSERT "0" to measure without asserting
17import { syntheticMedia } from './synthetic-media.mjs';
18import { chromium } from 'playwright';
19import { spawn } from 'node:child_process';
20import { createInterface } from 'node:readline';
21
22const [origin, room, binary] = process.argv.slice(2);
23const modes = (process.env.MOTION_MODES || 'balanced,detail,motion').split(',').filter(Boolean);
24const uplinks = (process.env.MOTION_UPLINKS || '0,1500000').split(',').map(Number);
25const asserting = process.env.MOTION_ASSERT !== '0';
26// A scroll followed at fewer frames than this reads as a slideshow.
27const MOTION_FLOOR_FPS = Number(process.env.MOTION_FLOOR_FPS || 15);
28
29const browser = await chromium.launch({ headless: true, args: [
30 '--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream',
31 '--autoplay-policy=no-user-gesture-required', '--disable-features=WebRtcHideLocalIpsWithMdns',
32 '--mute-audio',
33] });
34const context = await browser.newContext({ permissions: ['camera', 'microphone'] });
35if (process.env.CALLS_SYNTHETIC_MEDIA) await context.addInitScript(syntheticMedia);
36await context.addCookies([{ name: process.env.CALLS_COOKIE_NAME, value: process.env.CALLS_COOKIE_A, url: origin }]);
37const page = await context.newPage();
38await page.addInitScript(() => {
39 const Native = window.RTCPeerConnection;
40 window.__nativeInteropPCs = new Set();
41 window.RTCPeerConnection = class extends Native {
42 constructor(...args) { super(...args); window.__nativeInteropPCs.add(this); }
43 };
44});
45let native;
46let failed = false;
47try {
48 await page.goto(`${origin}/m/${room}`);
49 await page.waitForFunction(() => !!window.meet);
50 native = spawn(binary, [], { stdio: ['pipe', 'pipe', 'pipe'] });
51 native.stdin.write(process.env.CALLS_NATIVE_INPUT + '\n');
52 let sample = null;
53 let exited = false;
54 native.on('exit', () => { exited = true; });
55 createInterface({ input: native.stdout }).on('line', (line) => {
56 try {
57 const value = JSON.parse(line);
58 if (value.stats) sample = value;
59 } catch {}
60 });
61 native.stderr.resume();
62 const wait = async (predicate, label, seconds = 35) => {
63 const until = Date.now() + seconds * 1000;
64 while (Date.now() < until) {
65 if (exited) throw new Error('native executable exited');
66 if (await predicate()) return;
67 await new Promise((r) => setTimeout(r, 250));
68 }
69 throw new Error(label + ': ' + JSON.stringify(sample));
70 };
71 const sleep = (s) => new Promise((r) => setTimeout(r, s * 1000));
72 await wait(() => sample?.stats.framesDecoded > 15 && sample?.stats.videoBytesSent > 0, 'native and browser connect');
73
74 // The received screen's counters, found by the share tile's own track.
75 const received = () => page.evaluate(async () => {
76 const v = document.querySelector('#tiles figure.calls-tile-share video');
77 const track = v && v.srcObject ? v.srcObject.getVideoTracks()[0] : null;
78 if (!track) return null;
79 for (const pc of window.__nativeInteropPCs) {
80 if (pc.connectionState === 'closed') continue;
81 const stats = await pc.getStats();
82 for (const r of stats.values()) {
83 if (r.type === 'inbound-rtp' && r.kind === 'video' && r.trackIdentifier === track.id) {
84 return {
85 at: r.timestamp, decoded: r.framesDecoded || 0, width: r.frameWidth || 0, height: r.frameHeight || 0,
86 freezes: r.freezeCount || 0, frozen: r.totalFreezesDuration || 0,
87 jitter: r.jitterBufferDelay || 0, emitted: r.jitterBufferEmittedCount || 0,
88 codec: (stats.get(r.codecId) || {}).mimeType || '', decoder: r.decoderImplementation || '',
89 };
90 }
91 }
92 }
93 return null;
94 });
95
96 const results = [];
97 for (const mode of modes) {
98 native.stdin.write(`screen-mode ${mode}\n`);
99 native.stdin.write('screen-motion\n');
100 await wait(() => sample?.sharingScreen === true, 'native shares the moving screen');
101 await wait(async () => (await received())?.decoded > 30, 'the browser decodes the moving screen');
102 for (const uplink of uplinks) {
103 native.stdin.write(`uplink ${uplink}\n`);
104 // Long enough for the estimate to ramp or fall and the encoder to
105 // settle on it; then ten seconds measured.
106 await sleep(uplink ? 12 : 15);
107 const a = await received();
108 const s0 = sample.stats;
109 const p0 = sample.syntheticScreenFrames || 0;
110 await sleep(10);
111 const b = await received();
112 const s1 = sample.stats;
113 const p1 = sample.syntheticScreenFrames || 0;
114 const seconds = (b.at - a.at) / 1000;
115 const result = {
116 mode, uplink,
117 receivedFps: +((b.decoded - a.decoded) / seconds).toFixed(1),
118 receivedSize: `${b.width}x${b.height}`,
119 freezes: b.freezes - a.freezes,
120 frozenSeconds: +(b.frozen - a.frozen).toFixed(2),
121 jitterBufferMs: b.emitted > a.emitted ? Math.round(((b.jitter - a.jitter) / (b.emitted - a.emitted)) * 1000) : 0,
122 codec: b.codec, decoder: b.decoder,
123 sent: `${s1.screenSendWidth}x${s1.screenSendHeight}`,
124 sentFps: (s1.screenSendFpsTenths || 0) / 10,
125 producedFps: +((p1 - p0) / seconds).toFixed(1),
126 capturedFps: (s1.screenCapturedFpsTenths || 0) / 10,
127 encodedFps: +(((s1.screenFramesEncoded || 0) - (s0.screenFramesEncoded || 0)) / seconds).toFixed(1),
128 limitation: ['none', 'cpu', 'bandwidth', 'other'][s1.screenLimitation || 0],
129 sentKbps: Math.round((s1.screenSentBitrate || 0) / 1000),
130 targetKbps: Math.round((s1.screenTargetBitrate || 0) / 1000),
131 cameraKbps: Math.round(((s1.videoBytesSent || 0) - (s0.videoBytesSent || 0)) * 8 / seconds / 1000),
132 encodeMs: (s1.screenEncodeMsTenths || 0) / 10,
133 hardware: s1.screenHardwareEncoder === 1,
134 path: ['', 'host', 'srflx', 'prflx', 'relay'][s1.screenPath || 0],
135 };
136 results.push(result);
137 console.log('MEASURE ' + JSON.stringify(result));
138 }
139 native.stdin.write('uplink 0\n');
140 native.stdin.write('screen-off\n');
141 await wait(() => sample?.sharingScreen === false, 'native stops sharing');
142 await wait(async () => (await received()) === null, 'the browser drops the screen');
143 }
144
145 if (asserting) {
146 for (const r of results) {
147 if (r.mode === 'motion' || r.mode === 'balanced') {
148 if (r.receivedFps < MOTION_FLOOR_FPS) {
149 failed = true;
150 console.log(`FAIL ${r.mode} at uplink ${r.uplink || 'open'}: ${r.receivedFps} fps received, below ${MOTION_FLOOR_FPS}`);
151 } else {
152 console.log(`PASS ${r.mode} at uplink ${r.uplink || 'open'}: ${r.receivedFps} fps received at ${r.receivedSize}`);
153 }
154 }
155 if (r.mode === 'detail' && r.receivedSize !== '1920x1080') {
156 failed = true;
157 console.log(`FAIL detail at uplink ${r.uplink || 'open'} gave up resolution: ${r.receivedSize}`);
158 }
159 }
160 }
161 native.stdin.write('leave\n');
162 await sleep(1);
163} finally {
164 native?.kill('SIGTERM');
165 await browser.close();
166}
167if (failed) process.exit(1);
168