Files
piik/tests/browser-encoding-producer.test.ts
T
AAA 72ca6f2bc9 fix(media): improve sharing recovery and mobile picture controls
Windows capture-device errors could end an otherwise recoverable share, and reused Browser frames could arrive with the wrong orientation on receivers without the same orientation extension. Recover through the existing capture generation, start new encoded generations with an independent frame, and normalize orientation before shared encoding.

The maintenance scope also keeps compact headers aligned, cycles local rotation through four directions, accepts late coordinated UDP replies, requests Browser own-audio exclusion, and improves capture/input diagnostics. It preserves the published 2.x protocol and storage contracts.

Validation: 106 test files / 1878 tests, TypeScript, production Web/website builds, Windows Go core tests/vet and supported cross-builds passed. Candidate CI passed all 3 Server and 6 App targets, including native macOS builds, plus vulnerability and race checks. Controlled WGC/Pion recovery, retained-clock/first-frame checks, Browser orientation/SFU playback and responsive-header checks passed. The downloaded candidate set passed the existing publication dry-run: all 9 targets and 10 distribution packages share v2.5.2 and source 916baefa05, with matching checksums.

Original-device investigations remain open, including #502 and the reported low source frame rate. This PR does not claim those reports are resolved.

## 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.2 improves sharing recovery, picture orientation and mobile controls. It keeps the existing 2.x compatibility baseline.

- Improve Windows sharing recovery after graphics-device errors, and resume new encoder generations with a decodable picture.
- Preserve rotated and mirrored pictures when Browser encoding is shared across receivers, including SFU routes.
- Rotate the local picture in 90° steps; keep mobile header controls aligned and its connection indicator visible.
- Improve P2P establishment when coordinated UDP replies arrive late.
- Ask compatible browsers to exclude Piik's own playback from screen audio, and improve native capture and remote-input diagnostics.

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

### 中文

Piik v2.5.2 改善分享恢复、画面方向与手机操作,沿用现有 2.x 兼容基线。

- 改善 Windows 图形设备异常后的分享恢复,新编码器接替时从可正常解码的画面开始。
- 修正浏览器共享编码时旋转或镜像画面的方向,覆盖 SFU 等接收线路。
- 本机画面每次旋转 90°;手机顶部工具栏保持对齐,连接状态清晰可见。
- 改善协同打洞时 UDP 回复较晚到达的 P2P 建连。
- 向支持的浏览器请求排除 Piik 页面自身播放的声音,并完善原生采集与远控输入的诊断信息。

请一起更新 App 与自部署站点,以获得全部修复,随后刷新已打开的页面。浏览器设置与已有房间数据保留。
2026-11-01 12:00:26 -04:00

245 lines
11 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { BrowserEncodingProducer } from "../src/client/media/browser-encoding-producer";
import { applyVideoCaptureProfile, configureVideoSender, QUALITY_PROFILES } from "../src/client/media/quality";
vi.mock("../src/client/media/quality", async (original) => ({
...await original<typeof import("../src/client/media/quality")>(),
applyVideoCaptureProfile: vi.fn(async () => undefined),
configureVideoSender: vi.fn(async () => undefined),
}));
class Track extends EventTarget {
readonly id = "capture";
enabled = true;
readyState = "live";
contentHint = "motion";
readonly stop = vi.fn(() => { this.readyState = "ended"; });
clone() { return new Track(); }
}
class Connection {
static instances: Connection[] = [];
connectionState = "connecting";
onconnectionstatechange: (() => void) | null = null;
onicecandidate: ((event: { candidate: RTCIceCandidate }) => void) | null = null;
readonly addIceCandidate = vi.fn(async (_candidate: RTCIceCandidateInit) => undefined);
localDescription: RTCSessionDescriptionInit | null = null;
remoteDescription: RTCSessionDescriptionInit | null = null;
readonly close = vi.fn(() => { this.connectionState = "closed"; });
private frames!: ReadableStreamDefaultController<RTCEncodedVideoFrame>;
readonly getStats = vi.fn(async () => new Map<string, object>([
["video", { type: "outbound-rtp", kind: "video", mediaSourceId: "source", framesEncoded: 100 }],
["source", { type: "media-source", trackIdentifier: "capture" }],
]) as unknown as RTCStatsReport);
readonly sender = {
getParameters: () => ({ encodings: [{}] }),
setParameters: vi.fn(async () => undefined),
createEncodedStreams: () => ({
readable: new ReadableStream<RTCEncodedVideoFrame>({ start: controller => { this.frames = controller; } }),
writable: new WritableStream<RTCEncodedVideoFrame>(),
}),
};
constructor() { Connection.instances.push(this); }
addTransceiver() { return { sender: this.sender, setCodecPreferences() {} }; }
async createOffer() { return { type: "offer" as const, sdp: "offer" }; }
async createAnswer() { return { type: "answer" as const, sdp: "answer" }; }
async setLocalDescription(value: RTCSessionDescriptionInit) { this.localDescription = value; }
async setRemoteDescription(value: RTCSessionDescriptionInit) { this.remoteDescription = value; }
state(value: string) { this.connectionState = value; this.onconnectionstatechange?.(); }
async frame(bytes = 1) {
this.frames.enqueue({ data: new ArrayBuffer(bytes) } as RTCEncodedVideoFrame);
await vi.advanceTimersByTimeAsync(0);
}
}
const producers: BrowserEncodingProducer[] = [];
function createProducer(codec: RTCRtpCodec = { mimeType: "video/VP8", clockRate: 90000 }) {
const source = new Track();
const failed = vi.fn();
const producer = new BrowserEncodingProducer(source as unknown as MediaStreamTrack,
QUALITY_PROFILES["1080p30"], codec, vi.fn(), failed);
producers.push(producer);
return { producer, source, failed };
}
beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
Connection.instances = [];
vi.stubGlobal("RTCPeerConnection", Connection);
vi.stubGlobal("MediaStream", class {});
vi.stubGlobal("document", { createElement: () => ({ pause() {}, play: async () => undefined }) });
});
afterEach(async () => {
for (const producer of producers.splice(0)) producer.dispose();
await vi.runAllTimersAsync();
vi.unstubAllGlobals();
vi.restoreAllMocks();
vi.useRealTimers();
});
describe("Browser encoding producer startup", () => {
const localAnswer = (codec: string, fmtp = "") => ({ type: "answer" as const, sdp: [
"v=0", "o=- 1 1 IN IP4 127.0.0.1", "s=-", "t=0 0",
"m=video 9 UDP/TLS/RTP/SAVPF 96", "c=IN IP4 0.0.0.0", "a=recvonly",
`a=rtpmap:96 ${codec}/90000`, ...(fmtp ? [`a=fmtp:96 ${fmtp}`] : []), "",
].join("\r\n") });
it.each([
["VP8", ""],
["H264", "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f"],
])("initializes only the local %s answer from its existing budget", async (name, fmtp) => {
const codec = Object.freeze({ mimeType: `video/${name}`, clockRate: 90000, sdpFmtpLine: fmtp });
vi.spyOn(Connection.prototype, "createAnswer").mockResolvedValue(localAnswer(name, fmtp));
const { producer } = createProducer(codec);
await producer.start(3_333_999);
const [send, receive] = Connection.instances;
const answer = receive.localDescription!;
expect(answer.sdp).toContain("x-google-start-bitrate=3333");
if (fmtp) expect(answer.sdp).toContain(fmtp);
expect(send.remoteDescription).toBe(answer);
expect(send.localDescription!.sdp).not.toContain("x-google-start-bitrate");
expect(codec.sdpFmtpLine).toBe(fmtp);
await producer.update(QUALITY_PROFILES["1080p30"], 400_000);
expect(vi.mocked(configureVideoSender).mock.lastCall![1].maxBitrate).toBe(400_000);
await producer.update(QUALITY_PROFILES["1080p30"], 5_000_000);
expect(vi.mocked(configureVideoSender).mock.lastCall![1].maxBitrate).toBe(5_000_000);
expect(applyVideoCaptureProfile).toHaveBeenCalledOnce();
expect(Connection.instances).toHaveLength(2);
expect(send.close).not.toHaveBeenCalled();
expect(receive.close).not.toHaveBeenCalled();
expect(receive.localDescription).toBe(answer);
expect(Connection.prototype.createAnswer).toHaveBeenCalledOnce();
});
it.each([0, 999, Number.NaN])("does not invent a starting rate for budget %s", async (budget) => {
const answer = localAnswer("VP8");
vi.spyOn(Connection.prototype, "createAnswer").mockResolvedValue(answer);
const { producer } = createProducer();
await producer.start(budget);
expect(Connection.instances[1].localDescription).toBe(answer);
});
it("bounds the start hint by the Host ceiling without duplicating an existing hint", async () => {
vi.spyOn(Connection.prototype, "createAnswer").mockResolvedValue(localAnswer("VP8", "x-google-start-bitrate=300"));
const { producer } = createProducer();
await producer.start(7_000_000);
const sdp = Connection.instances[1].localDescription!.sdp!;
expect(sdp).toContain("a=fmtp:96 x-google-start-bitrate=5000\r\n");
expect(sdp.match(/x-google-start-bitrate=/g)).toHaveLength(1);
});
it("protects first publication instead of spending the startup frames on local warmup", async () => {
const { producer } = createProducer();
await producer.start();
for (const connection of Connection.instances) connection.state("connected");
const send = Connection.instances[0];
const preference = () => vi.mocked(configureVideoSender).mock.lastCall![1].degradationPreference;
const report = async () => { await producer.report(); await vi.advanceTimersByTimeAsync(0); };
for (let i = 0; i < 10; i++) await send.frame();
await report();
expect(preference()).toBe("maintain-resolution");
producer.beginOutput();
producer.setPaused(true);
for (let i = 0; i < 5; i++) await send.frame();
producer.setPaused(false);
await send.frame(0);
for (let i = 0; i < 4; i++) await send.frame();
await report();
expect(preference()).toBe("maintain-resolution");
// Another child's attachment must not restart this producer's protection.
producer.beginOutput();
await send.frame();
await report();
expect(preference()).toBe("balanced");
producer.beginOutput();
await report();
expect(preference()).toBe("balanced");
});
it.each(["queued", "active"])("keeps the producer after a %s candidate rejection", async (phase) => {
const { producer, failed } = createProducer();
const starting = producer.start();
const [send, receive] = Connection.instances;
const candidate = { candidate: "candidate:1 1 udp 1 127.0.0.1 9000 typ host" } as RTCIceCandidate;
receive!.addIceCandidate.mockRejectedValueOnce(new DOMException("Rejected candidate", "OperationError"));
if (phase === "queued") send!.onicecandidate?.({ candidate });
await starting;
for (const connection of Connection.instances) connection.state("connected");
if (phase === "active") send!.onicecandidate?.({ candidate });
const valid = { candidate: "candidate:2 1 udp 1 127.0.0.1 9001 typ host" } as RTCIceCandidate;
send!.onicecandidate?.({ candidate: valid });
await vi.advanceTimersByTimeAsync(0);
expect(receive!.addIceCandidate).toHaveBeenCalledWith(valid);
expect(failed).not.toHaveBeenCalled();
expect(Connection.instances.every(connection => connection.close.mock.calls.length === 0)).toBe(true);
expect(producer.track?.readyState).toBe("live");
});
it.each([false, true])("retires a stuck local transport once (one side connected: %s)", async (oneSide) => {
const { producer, source, failed } = createProducer();
await producer.start();
const input = producer.track as unknown as Track;
if (oneSide) Connection.instances[0]!.state("connected");
await vi.advanceTimersByTimeAsync(8_000);
expect(failed).toHaveBeenCalledOnce();
expect(Connection.instances.every((connection) => connection.close.mock.calls.length === 1)).toBe(true);
expect(input.stop).toHaveBeenCalledOnce();
expect(source.stop).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(30_000);
expect(failed).toHaveBeenCalledOnce();
});
it("keeps a connected producer without requiring frames from a quiet or paused source", async () => {
const { producer, failed } = createProducer();
await producer.start();
producer.setPaused(true);
for (const connection of Connection.instances) connection.state("connected");
expect(vi.getTimerCount()).toBe(0);
await vi.advanceTimersByTimeAsync(30_000);
expect(failed).not.toHaveBeenCalled();
expect(producer.track?.readyState).toBe("live");
});
it("cancels startup when retired and does not fail its replacement", async () => {
const old = createProducer();
await old.producer.start();
old.producer.dispose();
expect(vi.getTimerCount()).toBe(0);
const next = createProducer();
await next.producer.start();
for (const connection of Connection.instances.slice(2)) connection.state("connected");
await vi.advanceTimersByTimeAsync(30_000);
expect(old.failed).not.toHaveBeenCalled();
expect(next.failed).not.toHaveBeenCalled();
expect(next.producer.track?.readyState).toBe("live");
});
it("routes keyframe request failure through the producer owner, not the outgoing connection", async () => {
const { producer, source, failed } = createProducer();
await producer.start();
for (const connection of Connection.instances) connection.state("connected");
Connection.instances[0].sender.setParameters.mockRejectedValueOnce(new Error("Encoder failed"));
await expect(producer.requestKey()).resolves.toBeUndefined();
expect(failed).toHaveBeenCalledOnce();
expect(producer.track).toBeNull();
expect(source.stop).not.toHaveBeenCalled();
});
it("ignores a keyframe request during ordinary recovery after the producer has retired", async () => {
const { producer, failed } = createProducer();
await producer.start();
Connection.instances[0].state("failed");
expect(failed).toHaveBeenCalledOnce();
await expect(producer.requestKey()).resolves.toBeUndefined();
expect(failed).toHaveBeenCalledOnce();
});
});