Files
piik/tests/candidate-quality-probe.test.ts
T
AAA 815f1a07c4 fix(media): stabilize sharing recovery and remote input (#513)
## Problem and outcome

Connection recovery and packet repair could leave stale pictures, missing sound or unreliable remote input. This maintenance release repairs media/route ownership, H.264 negotiation, voice forwarding and Windows input recovery, and makes shared mobile controls and popovers easier to use.

## Release notes
**Piik, now in your pocket.**

Piik App supports Android screen sharing, with touch controls for a friend's computer.

**手机,也能开播。**

Piik App 支持 Android 屏幕分享,也能用手机控制朋友的电脑。

### English

Piik v2.5.1 improves sharing recovery, voice playback and remote control, with clearer mobile controls. It keeps the existing 2.x compatibility baseline.

- Improve H.264 selection and picture recovery after reconnecting, changing sources or recovering lost packets; keep a working share when a replacement source fails.
- Keep audio and room voice continuous, and prevent duplicate sound when switching playback windows.
- Improve Windows input recovery and release remote-control permissions when their connection ends.
- Keep mobile controls together and popovers usable after layout changes or in short viewports.
- Open the appropriate Android update download, including the Gitee ZIP containing the APK. Self-hosted SFU servers refresh public addresses for new connections without a restart.
- Add macOS source-thumbnail support.

Update the App and self-hosted site together to receive all fixes. Browser settings and existing room data are retained.

### 中文

Piik v2.5.1 改善分享恢复、语音播放与远程控制的稳定性,让手机操作更顺手,沿用现有 2.x 兼容基线。

- 改善 H.264 编码选择,以及重连、切换分享源和丢包后的画面恢复;新分享源切换失败时保留正常分享。
- 保持声音与房间语音连续,避免切换播放窗口时出现双重声音。
- 改善 Windows 输入恢复,并在连接结束时释放远程控制授权。
- 手机控件保持成组,浮层在布局变化或可见高度较小时仍可操作。
- Android 更新打开对应下载入口,Gitee 提供内含 APK 的 ZIP;自部署 SFU 为新连接刷新公网地址,无需重启服务。
- 补齐 macOS 选源缩略图支持。

请一起更新 App 与自部署站点,以获得全部修复。浏览器设置与已有房间数据保留。
## Validation

- Full frontend typecheck, test and production-build acceptance; current Windows App/Server packaging, extracted-file checks and exact served JavaScript verified locally.
- Existing Go, vulnerability, native media and controlled-network results retained at their tested revisions. The final production delta from the fully checked candidate is the shared short-viewport popover correction.
- The diagnostic report supplied for #502 informed the Windows input fixes. #500 sharing is confirmed working; its missing source previews are implemented. Original-device follow-up remains separate from this release's verified local fixes.

## Compatibility and scope

Preserves the published 2.x Browser/server, Native control, capture and SQLite contracts. No 3.0 or iOS implementation or external contributor patch is included. Related: #500, #502, #510, #512.
2026-10-31 16:34:12 -04:00

371 lines
12 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
P2pQualityProbe,
SfuQualityProbe,
p2pCandidateDoesNotRegress,
sfuCandidateDoesNotRegress,
} from "../src/client/media/candidate-quality-probe.ts";
import {
EMPTY_METRICS,
type ConnectionMetrics,
} from "../src/client/types.ts";
function metrics(
timestampMs: number,
overrides: Partial<ConnectionMetrics> = {},
): ConnectionMetrics {
return {
...EMPTY_METRICS,
sampleTimestampMs: timestampMs,
sampleWindowMs: 2_000,
frameWidth: 1920,
frameHeight: 1080,
framesPerSecond: 60,
bitrateKbps: 5_000,
intervalFramesDecoded: 120,
intervalFreezeCount: 0,
intervalFreezeDurationMs: 0,
intervalPauseCount: 0,
intervalPauseDurationMs: 0,
...overrides,
};
}
describe("SFU quality probe", () => {
it.each([sfuCandidateDoesNotRegress, p2pCandidateDoesNotRegress])(
"compares frame delivery over the sampled window rather than the last second",
(compare) => {
const current = metrics(3_000);
expect(compare(current, metrics(3_100, { framesPerSecond: 30 }))).toBe(true);
expect(compare(current, metrics(3_100, { framesPerSecond: null }))).toBe(true);
expect(
compare(current, metrics(3_100, { intervalFramesDecoded: 60 })),
).toBe(false);
expect(
compare(current, metrics(3_100, {
sampleWindowMs: 3_000,
intervalFramesDecoded: 180,
framesPerSecond: 30,
})),
).toBe(true);
},
);
it("accepts only complete same-Viewer non-regressing windows", () => {
const current = metrics(2_000);
expect(sfuCandidateDoesNotRegress(current, metrics(2_100))).toBe(true);
expect(
sfuCandidateDoesNotRegress(
current,
metrics(2_100, { bitrateKbps: 4_999 }),
),
).toBe(false);
expect(
sfuCandidateDoesNotRegress(
current,
metrics(2_100, { frameHeight: 720 }),
),
).toBe(false);
expect(
sfuCandidateDoesNotRegress(
current,
metrics(2_100, { intervalFreezeCount: 1 }),
),
).toBe(false);
expect(
sfuCandidateDoesNotRegress(
current,
metrics(8_000),
),
).toBeNull();
expect(
sfuCandidateDoesNotRegress(
current,
metrics(2_100, { bitrateKbps: null }),
),
).toBeNull();
});
it("accepts clean P2P receive windows that do not regress", () => {
const current = metrics(2_000, {
frameWidth: 320,
frameHeight: 180,
framesPerSecond: 10,
intervalFramesDecoded: 20,
bitrateKbps: 200,
});
expect(
p2pCandidateDoesNotRegress(
current,
metrics(2_100, {
frameWidth: 1_280,
frameHeight: 720,
framesPerSecond: 30,
intervalFramesDecoded: 60,
bitrateKbps: 150,
}),
),
).toBe(true);
expect(
p2pCandidateDoesNotRegress(
{ ...current, bitrateKbps: null },
metrics(2_100, {
frameWidth: 1_280,
frameHeight: 720,
framesPerSecond: 30,
intervalFramesDecoded: 60,
bitrateKbps: null,
}),
),
).toBe(true);
expect(p2pCandidateDoesNotRegress(current, { ...current })).toBe(true);
expect(
p2pCandidateDoesNotRegress(
current,
metrics(2_100, {
frameWidth: 1_280,
frameHeight: 720,
framesPerSecond: 5,
intervalFramesDecoded: 10,
}),
),
).toBe(false);
expect(
p2pCandidateDoesNotRegress(
current,
metrics(2_100, {
frameWidth: 160,
frameHeight: 90,
framesPerSecond: 60,
}),
),
).toBe(false);
expect(
p2pCandidateDoesNotRegress(
current,
metrics(2_100, { intervalFreezeCount: 1 }),
),
).toBe(false);
});
it("keeps nearby but non-overlapping samples pending", () => {
const probe = new P2pQualityProbe();
const current = (timestampMs: number) =>
metrics(timestampMs, {
sampleWindowMs: 1_000,
frameWidth: 320,
frameHeight: 180,
framesPerSecond: 10,
intervalFramesDecoded: 10,
});
const candidate = (timestampMs: number) =>
metrics(timestampMs, {
sampleWindowMs: 1_000,
frameWidth: 1_280,
frameHeight: 720,
framesPerSecond: 30,
intervalFramesDecoded: 30,
});
expect(
p2pCandidateDoesNotRegress(current(10_000), candidate(11_100)),
).toBeNull();
for (const timestampMs of [10_000, 13_000, 16_000]) {
expect(
probe.observe(current(timestampMs), candidate(timestampMs + 1_100)),
).toBe("pending");
}
});
it("compares overlapping samples", () => {
const current = metrics(10_000, {
sampleWindowMs: 1_000,
frameWidth: 320,
frameHeight: 180,
framesPerSecond: 10,
intervalFramesDecoded: 10,
});
const candidate = metrics(10_900, {
sampleWindowMs: 1_000,
frameWidth: 1_280,
frameHeight: 720,
framesPerSecond: 30,
intervalFramesDecoded: 30,
});
expect(p2pCandidateDoesNotRegress(current, candidate)).toBe(true);
});
it("requires three consecutive non-regressing P2P windows", () => {
const probe = new P2pQualityProbe();
expect(probe.observe(metrics(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(metrics(3_000), metrics(3_100))).toBe("pending");
expect(probe.observe(metrics(5_000), metrics(5_100))).toBe("approved");
probe.reset();
expect(probe.observe(metrics(7_000), metrics(7_100))).toBe("pending");
});
it("rejects after three comparable regressing P2P windows", () => {
const probe = new P2pQualityProbe();
const candidate = (timestampMs: number) =>
metrics(timestampMs, { framesPerSecond: 30, intervalFramesDecoded: 60 });
expect(probe.observe(metrics(1_000), candidate(1_100))).toBe("pending");
expect(probe.observe(metrics(3_000), candidate(3_100))).toBe("pending");
expect(probe.observe(metrics(5_000), candidate(5_100))).toBe("rejected");
});
it("keeps unknown evidence pending instead of counting it as rejection", () => {
const probe = new P2pQualityProbe();
expect(probe.observe(metrics(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(metrics(3_000), metrics(3_100))).toBe("pending");
expect(
probe.observe(
metrics(5_000),
metrics(5_100, { intervalFreezeCount: null }),
),
).toBe("pending");
expect(probe.observe(metrics(7_000), metrics(7_100))).toBe("pending");
});
it("requires three consecutive windows and resets after a regression", () => {
const probe = new SfuQualityProbe();
expect(probe.observe(metrics(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(metrics(3_000), metrics(3_100))).toBe("pending");
expect(
probe.observe(
metrics(5_000),
metrics(5_100, { framesPerSecond: 30, intervalFramesDecoded: 60 }),
),
).toBe("pending");
expect(probe.observe(metrics(7_000), metrics(7_100))).toBe("pending");
expect(probe.observe(metrics(9_000), metrics(9_100))).toBe("pending");
expect(probe.observe(metrics(11_000), metrics(11_100))).toBe("approved");
});
it("resets when either side lacks a new comparable window", () => {
const probe = new SfuQualityProbe();
expect(probe.observe(metrics(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(metrics(3_000), metrics(3_100))).toBe("pending");
expect(probe.observe(metrics(3_000), metrics(5_100))).toBe("pending");
expect(probe.observe(metrics(7_000), metrics(7_100))).toBe("pending");
expect(probe.observe(metrics(9_000), metrics(9_100))).toBe("pending");
expect(probe.observe(metrics(11_000), metrics(11_100))).toBe("approved");
probe.reset();
expect(probe.observe(metrics(13_000), metrics(13_100))).toBe("pending");
expect(
probe.observe(
metrics(15_000),
metrics(15_100, { intervalFreezeDurationMs: null }),
),
).toBe("pending");
expect(probe.observe(metrics(17_000), metrics(17_100))).toBe("pending");
});
});
describe("zero-frame current route", () => {
const zeroFrames = (timestampMs: number) =>
metrics(timestampMs, { intervalFramesDecoded: 0 });
const degraded = (timestampMs: number) =>
metrics(timestampMs, {
frameWidth: 320,
frameHeight: 180,
framesPerSecond: 10,
intervalFramesDecoded: 20,
});
it("approves clean delivery on both candidate route types", () => {
for (const probe of [new P2pQualityProbe(), new SfuQualityProbe()]) {
expect(probe.observe(zeroFrames(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(zeroFrames(3_000), metrics(3_100))).toBe("pending");
expect(probe.observe(zeroFrames(5_000), metrics(5_100))).toBe("approved");
}
});
it("continues a real improvement when the current route then stops decoding", () => {
const probe = new P2pQualityProbe();
expect(probe.observe(degraded(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(zeroFrames(3_000), metrics(3_100))).toBe("pending");
expect(probe.observe(zeroFrames(5_000), metrics(5_100))).toBe("approved");
});
it("never approves when both routes stop decoding", () => {
for (const probe of [new P2pQualityProbe(), new SfuQualityProbe()]) {
for (let index = 0; index < 15; index += 1) {
const timestampMs = 1_000 + index * 2_000;
expect(
probe.observe(zeroFrames(timestampMs), zeroFrames(timestampMs + 100)),
).toBe("pending");
}
}
});
it("rejects a candidate that decodes only with freezes", () => {
for (const probe of [new P2pQualityProbe(), new SfuQualityProbe()]) {
for (const timestampMs of [1_000, 3_000]) {
expect(
probe.observe(
zeroFrames(timestampMs),
metrics(timestampMs + 100, { intervalFreezeCount: 1 }),
),
).toBe("pending");
}
expect(
probe.observe(
zeroFrames(5_000),
metrics(5_100, { intervalFreezeCount: 1 }),
),
).toBe("rejected");
}
});
it("keeps incomplete and non-overlapping candidates unknown", () => {
const incomplete = new SfuQualityProbe();
for (const timestampMs of [1_000, 3_000, 5_000]) {
expect(
incomplete.observe(
zeroFrames(timestampMs),
metrics(timestampMs + 100, { bitrateKbps: null }),
),
).toBe("pending");
}
const nonOverlapping = new P2pQualityProbe();
for (const timestampMs of [1_000, 4_000, 7_000]) {
expect(
nonOverlapping.observe(
zeroFrames(timestampMs),
metrics(timestampMs + 2_100),
),
).toBe("pending");
}
});
it("clears accumulated evidence when the current window is missing", () => {
const probe = new P2pQualityProbe();
expect(probe.observe(degraded(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(degraded(3_000), metrics(3_100))).toBe("pending");
expect(probe.observe(null, metrics(5_100))).toBe("pending");
expect(probe.observe(degraded(7_000), metrics(7_100))).toBe("pending");
expect(probe.observe(degraded(9_000), metrics(9_100))).toBe("pending");
expect(probe.observe(degraded(11_000), metrics(11_100))).toBe("approved");
});
it("ignores a duplicate timestamp but clears a late candidate window", () => {
const probe = new P2pQualityProbe();
expect(probe.observe(degraded(1_000), metrics(1_100))).toBe("pending");
expect(probe.observe(degraded(3_000), metrics(3_100))).toBe("pending");
expect(probe.observe(degraded(5_000), metrics(3_100))).toBe("pending");
expect(probe.observe(degraded(7_000), metrics(7_100))).toBe("approved");
probe.reset();
expect(probe.observe(degraded(9_000), metrics(9_100))).toBe("pending");
expect(probe.observe(degraded(11_000), metrics(11_100))).toBe("pending");
expect(probe.observe(degraded(13_000), metrics(10_100))).toBe("pending");
expect(probe.observe(degraded(15_000), metrics(15_100))).toBe("pending");
expect(probe.observe(degraded(17_000), metrics(17_100))).toBe("pending");
expect(probe.observe(degraded(19_000), metrics(19_100))).toBe("approved");
});
});