## Summary / 概要 This maintenance phase updates the 2.x release line from v2.7.0 to v2.8.0. It brings configurable touch controls, clearer capture and audio choices, and bilingual troubleshooting into the sharing and viewing flows. App can also build invitations from an operator-configured HTTPS origin. 此维护阶段将 2.x 发布线从 v2.7.0 更新到 v2.8.0。改动覆盖可配置的触屏控制、更清楚的采集与声音选项,以及共享和观看流程内的双语故障排查入口。App 还可用部署者配置的固定 HTTPS 地址生成邀请。 ## Implementation and impact / 实现与影响 - Reuse the existing remote-input session for touch gamepad and mouse controls and interrupt held input when control stops. Confirm media source and audio availability before sharing and reuse viewer recovery when video ends. - 使用现有远控会话承载触屏手柄和鼠标控制,控制中断时释放按住的输入。分享前确认来源和声音可用性,观看端视频结束后沿用原有恢复流程。 - Preserve browser-assigned encoding identifiers and announce the layers actually sent to the embedded SFU. Retire stalled HTTP connections at shutdown, improve web startup failure handling and room-asset caching, and update ICE/DTLS dependencies. - 保留浏览器分配的编码标识,将实际发送的视频层告知内置 SFU。关闭服务时回收滞留的 HTTP 连接,完善网页启动失败反馈和房间资源缓存,并更新 ICE/DTLS 依赖。 - Keep Linux window capture available when the portal offers only windows, and hide remote control where monitor capture is unavailable. The KDE retry belongs to [#550](https://github.com/TNTcraftHIM/Piik/pull/550): if the portal rejects the persistence request, App retries with a temporary session. Update website FAQ navigation and bilingual guides. - Linux 门户只提供窗口时,仍可采集窗口。无法采集显示器时,隐藏远控入口。KDE 重试由 [#550](https://github.com/TNTcraftHIM/Piik/pull/550) 单独交付,门户拒绝持久化请求时,App 会改用临时会话重试。更新官网常见问题入口和双语指南。 ## Verification and remaining field checks / 验证与待确认场景 - Candidate `62efbcdc` passed 2,033 frontend tests across 113 files. Local handoff evidence records 12 successful page responses, zero broken links in its link audit, and SHA-256 verification for Windows x64 App, Linux x64 Server, and the Android development APK. - 候选版本 `62efbcdc` 的 113 个前端测试文件全部通过,共 2,033 项。交接证据记录了 12 个页面正常响应、链接审查未发现失效链接,以及 Windows x64 App、Linux x64 Server、Android 开发版 APK 的 SHA-256 校验。 - Issue-specific outcomes still depend on reporter environments, including Linux desktop portal availability and mobile control interruptions. The local Android artifact is a development build. Publication uses packages generated by CI from the merged revision. - Linux 桌面门户可用性和移动端远控中断等场景仍需在报告者的环境确认。本地 Android 候选包是开发版。发布包由 CI 从合并后的提交构建。 ## Merge and truth checkpoints / 合并与事实检查点 - The final candidate `cb27609994dfc5747cdce63710293c0f80b6b95f` has the same source tree as the validated `bb54da43` candidate. The integration base `4ae3619f` already contains the separately attributed #550 fix. The public release notes include that contribution once. - 最终候选提交 `cb27609994dfc5747cdce63710293c0f80b6b95f` 与已验收的 `bb54da43` 源码树完全一致。集成基点 `4ae3619f` 已包含单独署名的 #550 修复,发布说明只收录一次。 - The final frontend check passed all 2,037 tests across 113 files. Windows and Alpine Linux Go checks passed. The Linux Go race check passed. The separate Windows race invocation requires a CGO-enabled toolchain. - 最终前端检查通过全部 113 个文件的 2,037 项测试。Windows 与 Alpine Linux 的 Go 检查均通过,Linux 的 Go 竞态检查通过。Windows 的单独竞态检查需要启用 CGO 的工具链。 - The headless Linux portal tests cover window-only and monitor-capable sources, plus one temporary-session retry after a rejection of persistence. Android CI and local build instructions use Gradle 8.14.8. - Linux 无桌面门户测试覆盖仅窗口和支持显示器的采集来源,以及持久化被拒后仅重试一次临时会话。Android CI 和本地构建说明使用 Gradle 8.14.8. ## Release notes **Turn your phone into a twin-stick controller** By default, the left stick sends WASD and the right stick moves the mouse. If the host supports a virtual gamepad, you can switch to dual analog sticks. **手机也能当双摇杆手柄了** 默认左摇杆对应 WASD,右摇杆移动鼠标。主机支持虚拟手柄时,可以切换为双模拟摇杆。 ### English Piik 2.8.0 adds configurable touch joysticks for remote control, clearer sound settings when sharing, and help you can open from a failed share or playback screen. - **Control from a phone or tablet.** In keyboard and mouse mode, the left stick sends WASD and the right stick moves the mouse. When the Host supports a virtual gamepad, switch to dual analog sticks. Use the main switch to turn both sticks on or off, and expand or reposition each side independently. Piik releases held input if the control session is interrupted. - **Share the sound you want.** The source confirmation explains which sound is available and where to enable it. Browser tab audio and supported App system audio are easier to find, with guidance for keeping room voices out of the share. On Windows, capture keeps usable system audio samples even when their timestamps are uncertain. - **Recover a missing picture or sound.** Ended viewer video enters the existing recovery flow. Browser video forwarded through the Server uses the layers the browser actually sends. The searchable bilingual Help Center walks through joining, sharing, restoring playback, and collecting diagnostics. [English Help Center](https://piik.tv/docs/) - **Share windows on Linux.** If the desktop portal offers only windows, the App can still share a window and hides remote control when the portal cannot support it. - **Use a fixed HTTPS address for invitations.** App operators with an existing public HTTPS endpoint, such as one provided through frp, can configure App to generate invitations with that address. [Fixed invitations with frp](https://piik.tv/docs/frp.html)  Update the App or self-hosted Server with the matching package outside an active share. Reopen the room page after the update. ### 中文 Piik 2.8.0 为远程控制新增可调整的触屏摇杆,分享前更容易确认声音来源,共享或播放失败时也能直接打开对应的帮助。 - **用手机或平板远程控制。** 在键鼠模式下,左摇杆对应 WASD,右摇杆控制鼠标。主机支持虚拟手柄时,可切换到双模拟摇杆。总开关可开启或关闭两个摇杆,每侧可独立展开、调整位置。控制会话中断时,Piik 会释放仍按住的输入。 - **分享想让朋友听到的声音。** 确认分享来源时,可以看到有哪些声音可用、在哪里开启。浏览器标签页音频和 App 支持的系统声音更容易找到,帮助还说明了如何避免把房间语音收进共享声音。Windows 系统音频采集会保留时间戳不确定但仍可用的音频采样。 - **画面或声音消失时恢复播放。** 观看端的视频轨道结束后会进入现有恢复流程。经服务器转发的浏览器视频会使用浏览器实际发送的分层。可搜索的双语帮助中心介绍了如何加入房间、分享、恢复播放和收集诊断信息。[中文帮助中心](https://piik.tv/docs/zh/) - **在 Linux 上分享窗口。** 桌面门户只提供窗口时,App 仍可采集窗口,并隐藏门户无法支持的远程控制入口。 - **用固定 HTTPS 地址生成邀请。** 已经有公网 HTTPS 入口的 App 部署者(例如通过 frp 配置的入口)可以让 App 用该地址生成邀请链接。[使用 frp 配置固定邀请](https://piik.tv/docs/zh/frp.html)  请在共享结束后使用对应的软件包更新 App 或自托管服务器,更新完成后重新打开房间页面。
605 lines
32 KiB
TypeScript
605 lines
32 KiB
TypeScript
import { expect, it, vi } from "vitest";
|
|
import { RemoteInputSession } from "../src/client/remote-control/input-session";
|
|
import { picturePoint, rotatePicturePoint, rotatePictureDelta, type VideoRotation } from "../src/client/media/video-geometry";
|
|
import { InputRoute, type InputIdentity } from "../src/client/remote-control/input-route";
|
|
import { matchesRemoteShortcut, remoteInputStateSchema } from "../src/client/remote-control/protocol";
|
|
import { RemoteGamepad } from "../src/client/remote-control/gamepad";
|
|
|
|
it("recognizes only the selected local release chord outside IME composition", () => {
|
|
const chord = { code: "F9", ctrlKey: true, altKey: true, metaKey: false, shiftKey: false, isComposing: false };
|
|
expect(matchesRemoteShortcut(chord, 9)).toBe(true);
|
|
expect(matchesRemoteShortcut(chord, 10)).toBe(false);
|
|
for (const changed of [{ ctrlKey: false }, { altKey: false }, { shiftKey: true }, { metaKey: true }, { isComposing: true }]) {
|
|
expect(matchesRemoteShortcut({ ...chord, ...changed }, 9)).toBe(false);
|
|
}
|
|
});
|
|
|
|
class Channel extends EventTarget {
|
|
readyState = "open";
|
|
bufferedAmount = 0;
|
|
sent: Record<string, unknown>[] = [];
|
|
send(value: string) { this.sent.push(JSON.parse(value)); }
|
|
close() { this.readyState = "closed"; this.dispatchEvent(new Event("close")); }
|
|
receive(value: object) { this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify(value) })); }
|
|
}
|
|
|
|
it("opts into input feedback only when advertised and retains the same grant after a rejected input", () => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
const ready = { platform: "windows", mode: "room", state: "ready" };
|
|
channel.receive(ready);
|
|
expect(channel.sent).toEqual([{ type: "sync" }, { type: "capabilities" }]);
|
|
channel.receive({ ...ready, capabilities: ["coPlay", "text"] });
|
|
expect(channel.sent).toHaveLength(2);
|
|
channel.receive({ ...ready, capabilities: ["coPlay", "text", "inputFeedback"] });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "capabilities", inputFeedback: true });
|
|
channel.receive({ ...ready, capabilities: ["coPlay", "text", "inputFeedback"] });
|
|
expect(channel.sent).toHaveLength(3);
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent, grant = "f".repeat(64);
|
|
const active = { ...ready, state: "active", intent, grant, capabilities: ["inputFeedback"] };
|
|
channel.receive(active);
|
|
session.input({ type: "key", code: "KeyW", down: true });
|
|
const beforeFeedback = channel.sent.length;
|
|
channel.receive({ ...active, inputFailed: true });
|
|
expect(session.state).toMatchObject({ state: "active", grant, intent, inputFailed: true });
|
|
expect(channel.sent).toHaveLength(beforeFeedback);
|
|
session.input({ type: "key", code: "KeyD", down: true });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "key", code: "KeyD", down: true, grant });
|
|
channel.receive(active);
|
|
expect(session.state?.inputFailed).toBeUndefined();
|
|
expect(session.state?.grant).toBe(grant);
|
|
expect(channel.readyState).toBe("open");
|
|
session.release();
|
|
expect(channel.sent.at(-1)).toEqual({ type: "release", intent });
|
|
session.dispose();
|
|
});
|
|
|
|
it("updates text availability without replacing an active keyboard grant", () => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
channel.receive({ platform: "windows", mode: "room", state: "ready", coPlay: true, capabilities: ["coPlay"] });
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent, grant = "d".repeat(64);
|
|
const active = { platform: "windows", mode: "room", state: "active", coPlay: true, intent, grant };
|
|
for (const enabled of [true, false, true]) {
|
|
channel.receive({ ...active, capabilities: enabled ? ["coPlay", "text"] : ["coPlay"] });
|
|
const before = channel.sent.length;
|
|
session.input({ type: "text", text: "hello" });
|
|
expect(channel.sent.length - before).toBe(enabled ? 1 : 0);
|
|
expect(session.state?.grant).toBe(grant);
|
|
}
|
|
session.dispose();
|
|
});
|
|
|
|
it("keeps keyboard control when optional gamepad support fails for the grant", () => {
|
|
const channel = new Channel(), changed = vi.fn();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, changed);
|
|
channel.receive({ platform: "windows", mode: "room", state: "ready", coPlay: true, capabilities: ["coPlay", "gamepad"] });
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent, grant = "e".repeat(64);
|
|
const active = { platform: "windows", mode: "room", state: "active", coPlay: true, intent, grant };
|
|
const gamepad = { buttons: 0, leftTrigger: 0, rightTrigger: 0, leftX: 0, leftY: 0, rightX: 0, rightY: 0 };
|
|
channel.receive({ ...active, capabilities: ["coPlay", "gamepad", "text"] });
|
|
session.input({ type: "gamepad", gamepad });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad", gamepad, grant });
|
|
channel.receive({ ...active, capabilities: ["coPlay", "text"], reason: "gamepad" });
|
|
const before = channel.sent.length;
|
|
session.input({ type: "gamepad", gamepad }); session.input({ type: "gamepad-reset" }); session.input({ type: "gamepad-stop" });
|
|
expect(channel.sent).toHaveLength(before);
|
|
session.input({ type: "key", code: "KeyW", down: false });
|
|
session.input({ type: "text", text: "hello" });
|
|
session.input({ type: "reset" });
|
|
expect(channel.sent.slice(before)).toEqual([
|
|
{ type: "key", code: "KeyW", down: false, grant }, { type: "text", text: "hello", grant }, { type: "reset", grant },
|
|
]);
|
|
expect(session.state?.state).toBe("active"); expect(session.state?.grant).toBe(grant);
|
|
session.dispose();
|
|
});
|
|
|
|
it("maps standard gamepads into one negotiated grant and resets on focus loss, unplug and retirement", () => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready" });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "capabilities" });
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready", coPlay: true, capabilities: ["coPlay", "gamepad"] });
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent;
|
|
expect(channel.sent.at(-1)).toEqual({ type: "request", intent });
|
|
const grant = "d".repeat(64);
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent, grant, coPlay: true, capabilities: ["coPlay", "gamepad"] });
|
|
const beforeInput = channel.sent.length;
|
|
session.input({ type: "key", code: "KeyW", down: true });
|
|
session.input({ type: "button", button: 0, down: true });
|
|
expect(channel.sent.slice(beforeInput)).toEqual([
|
|
{ type: "key", code: "KeyW", down: true, grant }, { type: "button", button: 0, down: true, grant },
|
|
]);
|
|
|
|
const pad = { index: 1, connected: true, mapping: "standard", axes: [-1, -1, 1, 1],
|
|
buttons: Array.from({ length: 17 }, (_, index) => ({ pressed: [0, 1, 4, 8, 9, 10, 12].includes(index), value: index === 6 ? .5 : index === 7 ? 1 : 0 })),
|
|
} as unknown as Gamepad;
|
|
let pads: (Gamepad | null)[] = [{ ...pad, index: 0, mapping: "" } as Gamepad, pad];
|
|
let focused = true;
|
|
const page = Object.assign(new EventTarget(), { hidden: false, hasFocus: () => focused });
|
|
const frames = new Map<number, FrameRequestCallback>();
|
|
let sequence = 0;
|
|
const owner = Object.assign(new EventTarget(), { navigator: { getGamepads: () => pads },
|
|
requestAnimationFrame: (frame: FrameRequestCallback) => { frames.set(++sequence, frame); return sequence; },
|
|
cancelAnimationFrame: (id: number) => frames.delete(id),
|
|
});
|
|
const tick = (time: number) => {
|
|
const entry = frames.entries().next().value!;
|
|
frames.delete(entry[0]); entry[1](time);
|
|
};
|
|
const controller = new RemoteGamepad(event => session.input(event), () => {}, owner as unknown as Window, page as unknown as Document);
|
|
try {
|
|
tick(0); tick(17);
|
|
const report = { type: "gamepad", grant, gamepad: { buttons: 0x1713,
|
|
leftTrigger: 128, rightTrigger: 255, leftX: -32768, leftY: -32768, rightX: 32767, rightY: 32767 } };
|
|
expect(channel.sent.slice(-2)).toEqual([report, report]);
|
|
const beforeSuspend = channel.sent.length;
|
|
channel.receive({ platform: "windows", mode: "ask", state: "suspended", intent, grant, coPlay: true, capabilities: ["coPlay", "gamepad"] });
|
|
tick(34);
|
|
expect(channel.sent).toHaveLength(beforeSuspend);
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent, grant, coPlay: true, capabilities: ["coPlay", "gamepad"] });
|
|
tick(51);
|
|
expect(channel.sent.at(-1)).toEqual(report);
|
|
focused = false; owner.dispatchEvent(new Event("blur"));
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-reset", grant });
|
|
expect(frames.size).toBe(0);
|
|
focused = true; owner.dispatchEvent(new Event("focus")); tick(68);
|
|
expect(channel.sent.at(-1)).toEqual(report);
|
|
pads = [];
|
|
owner.dispatchEvent(Object.assign(new Event("gamepaddisconnected"), { gamepad: pad }));
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-stop", grant });
|
|
tick(85);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-stop", grant });
|
|
pads = [pad]; tick(102);
|
|
expect(channel.sent.at(-1)).toEqual(report);
|
|
// Polling also notices a device removed without a Browser event.
|
|
pads = []; tick(119);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-stop", grant });
|
|
controller.setLeftStick(0.25, -0.5); tick(136);
|
|
expect(channel.sent.at(-1)).toMatchObject({ type: "gamepad", gamepad: { leftX: 8192, leftY: -16384, buttons: 0 } });
|
|
controller.setLeftStick(0, 0);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-reset", grant });
|
|
controller.setLeftStick(-0.75, 0.5);
|
|
pads = [pad]; tick(153);
|
|
controller.setLeftStick(0, 0);
|
|
expect(channel.sent.at(-1)).toEqual(report);
|
|
pads = []; tick(170);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-stop", grant });
|
|
controller.setLeftStick(0.5, 0); tick(187);
|
|
page.hidden = true; page.dispatchEvent(new Event("visibilitychange"));
|
|
expect(frames.size).toBe(0);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-reset", grant });
|
|
channel.receive({ platform: "windows", mode: "ask", state: "suspended", intent, grant, coPlay: true, capabilities: ["coPlay", "gamepad"] });
|
|
} finally {
|
|
controller.dispose();
|
|
expect(channel.sent.at(-1)).toEqual({ type: "gamepad-stop", grant });
|
|
session.dispose();
|
|
}
|
|
expect(frames.size).toBe(0);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "release", intent });
|
|
owner.dispatchEvent(new Event("focus"));
|
|
expect(frames.size).toBe(0);
|
|
});
|
|
|
|
it.each(["windows", "darwin", "linux"] as const)("keeps the Host platform in local request state (%s)", platform => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
session.request();
|
|
expect(channel.sent).toEqual([{ type: "sync" }]);
|
|
expect(remoteInputStateSchema.safeParse({ mode: "ask", state: "ready" }).success).toBe(false);
|
|
expect(remoteInputStateSchema.safeParse({ mode: "ask", state: "ready", platform: "unknown" }).success).toBe(false);
|
|
channel.receive({ mode: "ask", state: "ready", platform });
|
|
session.request();
|
|
expect(session.state).toMatchObject({ state: "pending", platform });
|
|
session.release();
|
|
expect(session.state).toMatchObject({ state: "ready", platform });
|
|
session.dispose();
|
|
});
|
|
|
|
it("reuses the authenticated identity across media routes and syncs after delayed key installation", async () => {
|
|
const registered: InputIdentity[] = [];
|
|
const route = new InputRoute(() => {});
|
|
route.enable(identity => { registered.push(identity); return true; });
|
|
const first = new Channel();
|
|
route.setUpstream(first as unknown as RTCDataChannel);
|
|
first.receive({ ready: true });
|
|
const oldEndpoint = route.current!;
|
|
const session = new RemoteInputSession(oldEndpoint, () => {});
|
|
await vi.waitFor(() => expect(first.sent.filter(p => p.data)).toHaveLength(1));
|
|
const oldPacket = first.sent.find(p => p.data)!;
|
|
expect(oldPacket.data).not.toContain("sync");
|
|
first.receive({ id: registered[0].endpointId, ready: true });
|
|
await vi.waitFor(() => expect(first.sent.filter(p => p.data)).toHaveLength(2));
|
|
session.dispose();
|
|
const next = new Channel();
|
|
route.setUpstream(next as unknown as RTCDataChannel);
|
|
expect(oldEndpoint.readyState).toBe("closed");
|
|
next.receive({ ready: true });
|
|
const nextSession = new RemoteInputSession(route.current!, () => {});
|
|
await vi.waitFor(() => expect(next.sent.filter(p => p.data)).toHaveLength(1));
|
|
const newPacket = next.sent.find(p => p.data)!;
|
|
expect(registered).toHaveLength(1);
|
|
expect(newPacket.id).toBe(oldPacket.id);
|
|
expect(newPacket.epoch).not.toBe(oldPacket.epoch);
|
|
const sequence = (p: Record<string, unknown>) => new DataView(Uint8Array.from(atob(p.data as string), c => c.charCodeAt(0)).buffer).getBigUint64(0);
|
|
expect(sequence(newPacket)).toBeGreaterThan(sequence(oldPacket));
|
|
nextSession.dispose();
|
|
route.dispose();
|
|
});
|
|
|
|
it("opens a fresh control endpoint after local backpressure retires an intent on a healthy route", async () => {
|
|
const registered: InputIdentity[] = [];
|
|
const route = new InputRoute(() => {});
|
|
const upstream = new Channel();
|
|
route.enable(identity => { registered.push(identity); return true; });
|
|
route.setUpstream(upstream as unknown as RTCDataChannel);
|
|
upstream.receive({ ready: true });
|
|
const original = route.current!;
|
|
const interrupted = vi.fn();
|
|
const session = new RemoteInputSession(original, () => {}, interrupted);
|
|
try {
|
|
if (!(original instanceof EventTarget)) throw new Error("Expected the route's input event target");
|
|
original.dispatchEvent(new MessageEvent("message", { data: JSON.stringify({ platform: "windows", mode: "room", state: "ready" }) }));
|
|
session.request();
|
|
const intent = session.state?.intent;
|
|
expect(intent).toBeDefined();
|
|
original.dispatchEvent(new MessageEvent("message", { data: JSON.stringify({
|
|
platform: "windows", mode: "room", state: "active", intent, grant: "a".repeat(64),
|
|
}) }));
|
|
expect(session.state?.state).toBe("active");
|
|
// Reproduce a stalled endpoint queue while leaving the underlying media channel open.
|
|
const text = "x".repeat(1900);
|
|
for (let index = 0; index < 6; index++) session.input({ type: "text", text });
|
|
expect(original.readyState).toBe("closed");
|
|
expect(interrupted).toHaveBeenCalledOnce();
|
|
expect(upstream.readyState).toBe("open");
|
|
const retiredEpoch = upstream.sent.find(packet => packet.closed === true)?.epoch;
|
|
expect(retiredEpoch).toBeDefined();
|
|
expect(route.current).toBeNull();
|
|
expect(upstream.sent.at(-1)).toEqual({ ready: true });
|
|
upstream.receive({ ready: true });
|
|
expect(route.current).not.toBe(original);
|
|
expect(route.current?.readyState).toBe("open");
|
|
expect(registered).toHaveLength(1);
|
|
const restored = new RemoteInputSession(route.current!, () => {});
|
|
try {
|
|
await vi.waitFor(() => expect(upstream.sent.some(packet => packet.data && packet.epoch !== retiredEpoch)).toBe(true));
|
|
expect(restored.state).toBeNull();
|
|
expect(upstream.sent.some(packet => packet.data && packet.epoch === retiredEpoch)).toBe(false);
|
|
} finally { restored.dispose(); }
|
|
} finally { session.dispose(); route.dispose(); }
|
|
expect(interrupted).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it.each([false, true])("retires the current input epoch after late old-branch packets (closed=%s)", async closed => {
|
|
const controller = new InputRoute(() => {}), relay = new InputRoute(() => {});
|
|
controller.enable(() => true);
|
|
const oldUpstream = new Channel(), nextUpstream = new Channel(), upstream = new Channel(), sharedChild = new Channel();
|
|
const connection = Object.assign(new EventTarget(), { connectionState: "connected" });
|
|
relay.setUpstream(upstream as unknown as RTCDataChannel);
|
|
upstream.receive({ ready: true });
|
|
relay.attachChild(sharedChild as unknown as RTCDataChannel, connection as unknown as RTCPeerConnection);
|
|
try {
|
|
controller.setUpstream(oldUpstream as unknown as RTCDataChannel);
|
|
oldUpstream.receive({ ready: true });
|
|
controller.current!.send(JSON.stringify({ type: "request" }));
|
|
controller.current!.send(JSON.stringify({ type: "key", code: "KeyA", down: true }));
|
|
await vi.waitFor(() => expect(oldUpstream.sent.filter(packet => packet.data)).toHaveLength(2));
|
|
const [oldRequest, lateOldInput] = oldUpstream.sent.filter(packet => packet.data);
|
|
controller.setUpstream(nextUpstream as unknown as RTCDataChannel);
|
|
nextUpstream.receive({ ready: true });
|
|
controller.current!.send(JSON.stringify({ type: "request" }));
|
|
controller.current!.send(JSON.stringify({ type: "key", code: "KeyW", down: true }));
|
|
await vi.waitFor(() => expect(nextUpstream.sent.filter(packet => packet.data)).toHaveLength(2));
|
|
const currentPackets = nextUpstream.sent.filter(packet => packet.data);
|
|
expect(currentPackets[0].id).toBe(oldRequest.id);
|
|
expect(currentPackets[0].epoch).not.toBe(oldRequest.epoch);
|
|
|
|
sharedChild.receive(oldRequest);
|
|
currentPackets.forEach(packet => sharedChild.receive(packet));
|
|
// A longer retired route can reach this shared leg after the current route.
|
|
sharedChild.receive(lateOldInput);
|
|
if (closed) sharedChild.receive({ id: oldRequest.id, epoch: oldRequest.epoch, closed: true });
|
|
connection.connectionState = "disconnected";
|
|
connection.dispatchEvent(new Event("connectionstatechange"));
|
|
expect(upstream.sent.filter(packet => packet.closed)).toContainEqual({
|
|
id: currentPackets[0].id, epoch: currentPackets[0].epoch, closed: true,
|
|
});
|
|
} finally {
|
|
controller.dispose(); relay.dispose();
|
|
}
|
|
});
|
|
|
|
it("bounds unclosed child epochs and retires them before input-channel overflow closes", () => {
|
|
const route = new InputRoute(() => {}), upstream = new Channel(), child = new Channel();
|
|
const connection = Object.assign(new EventTarget(), { connectionState: "connected" });
|
|
const envelope = (index: number) => ({ id: (index % 20).toString(16).padStart(32, "0"),
|
|
epoch: index.toString(16).padStart(32, "0"), data: "opaque" });
|
|
route.setUpstream(upstream as unknown as RTCDataChannel);
|
|
upstream.receive({ ready: true });
|
|
route.attachChild(child as unknown as RTCDataChannel, connection as unknown as RTCPeerConnection);
|
|
// Real close is asynchronous; retirement cannot wait for its close event.
|
|
vi.spyOn(child, "close").mockImplementation(() => { child.readyState = "closing"; });
|
|
try {
|
|
for (let index = 0; index < 128; index++) child.receive(envelope(index));
|
|
child.receive(envelope(0)); // Existing tuples still forward at capacity.
|
|
child.receive({ ...envelope(0), id: "f".repeat(32) }); // Keep the existing 20-identity bound.
|
|
expect(upstream.sent.filter(packet => packet.data)).toHaveLength(129);
|
|
expect(child.close).not.toHaveBeenCalled();
|
|
child.receive(envelope(128));
|
|
expect(child.close).toHaveBeenCalledOnce();
|
|
expect(upstream.sent.filter(packet => packet.data)).toHaveLength(129);
|
|
expect(upstream.sent.filter(packet => packet.closed)).toHaveLength(128);
|
|
for (let index = 0; index < 128; index++) expect(upstream.sent).toContainEqual({
|
|
id: envelope(index).id, epoch: envelope(index).epoch, closed: true,
|
|
});
|
|
const retiredCount = upstream.sent.length;
|
|
child.receive(envelope(129));
|
|
child.dispatchEvent(new Event("close"));
|
|
expect(upstream.sent).toHaveLength(retiredCount);
|
|
expect(connection.connectionState).toBe("connected");
|
|
} finally {
|
|
route.dispose();
|
|
}
|
|
});
|
|
|
|
it("binds input to the current request and retires cancelled or replaced grants", () => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
const grant = "a".repeat(64);
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready" });
|
|
session.request();
|
|
const first = channel.sent.at(-1)?.intent;
|
|
session.release();
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent: first, grant });
|
|
expect(session.state?.state).toBe("ready");
|
|
expect(channel.sent.at(-1)).toEqual({ type: "release", intent: first });
|
|
session.request();
|
|
const second = channel.sent.at(-1)?.intent;
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready", intent: first });
|
|
expect(session.state?.state).toBe("pending");
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent: second, grant });
|
|
session.input({ type: "key", code: "KeyW", down: true });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "key", code: "KeyW", down: true, grant });
|
|
channel.receive({ platform: "windows", mode: "ask", state: "suspended", intent: second, grant });
|
|
const count = channel.sent.length;
|
|
session.input({ type: "key", code: "KeyD", down: true });
|
|
expect(channel.sent).toHaveLength(count);
|
|
session.input({ type: "reset" });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "reset", grant });
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent: second, grant });
|
|
session.dispose();
|
|
expect(channel.sent.at(-1)).toEqual({ type: "release", intent: second });
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent: second, grant });
|
|
session.input({ type: "key", code: "KeyW", down: true });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "release", intent: second });
|
|
expect(channel.readyState).toBe("open"); // Media owns the channel lifetime.
|
|
});
|
|
|
|
it("maps only visible picture pixels when playback has letterboxing", () => {
|
|
const video = { videoWidth: 1920, videoHeight: 1080,
|
|
style: { objectFit: "contain" }, dataset: {},
|
|
getBoundingClientRect: () => ({ left: 100, top: 50, width: 800, height: 600 }),
|
|
} as HTMLVideoElement;
|
|
expect(picturePoint(video, 500, 350)).toEqual({ x: 0.5, y: 0.5 });
|
|
expect(picturePoint(video, 500, 60)).toBeNull();
|
|
expect(picturePoint(video, 901, 350)).toBeNull();
|
|
video.style.objectFit = "cover";
|
|
expect(picturePoint(video, 500, 350)).toEqual({ x: 0.5, y: 0.5 });
|
|
// Filling this 4:3 player crops the 16:9 picture, it does not stretch input.
|
|
expect(picturePoint(video, 100, 50)?.x).toBeCloseTo(0.125);
|
|
expect(picturePoint(video, 900, 650)?.x).toBeCloseTo(0.875);
|
|
video.style.objectFit = "fill";
|
|
expect(picturePoint(video, 100, 50)).toEqual({ x: 0, y: 0 });
|
|
expect(picturePoint(video, 900, 650)).toEqual({ x: 1, y: 1 });
|
|
expect(picturePoint(video, 300, 500)).toEqual({ x: 0.25, y: 0.75 });
|
|
});
|
|
|
|
it("ends a pending request when its source becomes unavailable, even before the request reaches the Host", () => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready" });
|
|
session.request();
|
|
// Source pause/off can be announced before the Host has seen this intent.
|
|
// It still invalidates the whole endpoint, not just an older request.
|
|
channel.receive({ platform: "windows", mode: "ask", state: "unavailable" });
|
|
expect(session.state?.state).toBe("unavailable");
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready" });
|
|
session.request();
|
|
channel.receive({ platform: "windows", mode: "off", state: "unavailable", intent: "a".repeat(32) });
|
|
expect(session.state?.mode).toBe("off");
|
|
session.dispose();
|
|
});
|
|
|
|
it.each([
|
|
[0, { x: .25, y: .75 }], [90, { x: .25, y: .25 }],
|
|
[180, { x: .75, y: .25 }], [270, { x: .75, y: .75 }],
|
|
] as const)("maps rotated picture pixels and relative motion back to source axes (%s°)", (rotation, shown) => {
|
|
const video = { videoWidth: 1920, videoHeight: 1080,
|
|
style: { objectFit: "fill" }, dataset: { pictureRotation: String(rotation) },
|
|
getBoundingClientRect: () => ({ left: 100, top: 50, width: 800, height: 600 }),
|
|
} as unknown as HTMLVideoElement;
|
|
expect(rotatePicturePoint({ x: .25, y: .75 }, rotation)).toEqual(shown);
|
|
expect(picturePoint(video, 100 + shown.x * 800, 50 + shown.y * 600)).toEqual({ x: .25, y: .75 });
|
|
expect(picturePoint(video, 99, 50)).toBeNull();
|
|
const motion = rotatePictureDelta({ x: .1, y: -.2 }, rotation as VideoRotation);
|
|
expect(rotatePictureDelta(motion, rotation, true)).toEqual({ x: .1, y: -.2 });
|
|
video.style.objectFit = "contain";
|
|
const sideways = rotation === 90 || rotation === 270;
|
|
expect(picturePoint(video, sideways ? 101 : 500, sideways ? 350 : 51)).toBeNull();
|
|
expect(picturePoint(video, 500, 350)).toEqual({ x: .5, y: .5 });
|
|
});
|
|
|
|
it("sends a control password only in the request and follows the App policy after cancellation", () => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
channel.receive({ platform: "windows", state: "ready", mode: "password" });
|
|
session.request("temporary-secret");
|
|
const intent = channel.sent.at(-1)?.intent;
|
|
expect(channel.sent.at(-1)).toMatchObject({ type: "request", password: "temporary-secret" });
|
|
channel.receive({ platform: "windows", state: "ready", mode: "password", reason: "password", intent });
|
|
expect(session.state?.reason).toBe("password");
|
|
session.release();
|
|
expect(session.state?.reason).toBeUndefined();
|
|
expect(session.state?.mode).toBe("password");
|
|
session.request("temporary-secret");
|
|
const nextIntent = channel.sent.at(-1)?.intent;
|
|
channel.receive({ platform: "windows", state: "active", mode: "password", intent: nextIntent, grant: "a".repeat(64) });
|
|
session.release();
|
|
expect(session.state?.mode).toBe("password");
|
|
expect(channel.sent.at(-1)).toEqual({ type: "release", intent: nextIntent });
|
|
channel.receive({ platform: "windows", state: "ready", mode: "ask", intent: nextIntent });
|
|
expect(session.state?.mode).toBe("ask");
|
|
session.dispose();
|
|
});
|
|
|
|
it("preserves wheel magnitude and fractions across browser units, only within the current grant", () => {
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready" });
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent;
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent, grant: "c".repeat(64) });
|
|
session.scroll(100, 0);
|
|
session.scroll(3, 1);
|
|
session.scroll(1, 2);
|
|
expect(channel.sent.slice(-3).map(event => event.delta)).toEqual([-120, -120, -120]);
|
|
const count = channel.sent.length;
|
|
for (let i = 0; i < 4; i++) session.scroll(0.25, 0);
|
|
expect(channel.sent).toHaveLength(count + 1);
|
|
expect(channel.sent.at(-1)?.delta).toBe(-1);
|
|
session.scroll(-10_000, 0);
|
|
expect(channel.sent.at(-1)?.delta).toBe(1200);
|
|
channel.receive({ platform: "windows", mode: "ask", state: "suspended", intent, grant: "c".repeat(64) });
|
|
const suspendedCount = channel.sent.length;
|
|
session.scroll(100, 0);
|
|
expect(channel.sent).toHaveLength(suspendedCount);
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent, grant: "c".repeat(64) });
|
|
session.scroll(0.75, 0);
|
|
expect(channel.sent).toHaveLength(suspendedCount);
|
|
session.dispose();
|
|
});
|
|
|
|
it("flushes coalesced motion before buttons and discards it when control ends", () => {
|
|
vi.useFakeTimers();
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
const grant = "b".repeat(64);
|
|
try {
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready" });
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent;
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent, grant });
|
|
session.input({ type: "move", x: 0.2, y: 0.3 });
|
|
session.input({ type: "move", x: 0.7, y: 0.8 });
|
|
session.input({ type: "button", button: 0, down: false });
|
|
expect(channel.sent.slice(-2)).toEqual([
|
|
{ type: "move", x: 0.7, y: 0.8, grant },
|
|
{ type: "button", button: 0, down: false, grant },
|
|
]);
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
session.input({ type: "relative", dx: 3, dy: -2 });
|
|
session.input({ type: "relative", dx: 4, dy: 5 });
|
|
vi.advanceTimersByTime(4); // Input does not depend on a rendered frame.
|
|
expect(channel.sent.at(-1)).toEqual({ type: "relative", dx: 7, dy: 3, grant });
|
|
session.input({ type: "move", x: 0.9, y: 0.1 });
|
|
const count = channel.sent.length;
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready", intent });
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
expect(channel.sent).toHaveLength(count);
|
|
} finally {
|
|
session.dispose();
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("sends idle motion immediately and only batches samples inside the send interval", () => {
|
|
vi.useFakeTimers();
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
const grant = "b".repeat(64);
|
|
try {
|
|
channel.receive({ platform: "windows", mode: "ask", state: "ready" });
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent;
|
|
channel.receive({ platform: "windows", mode: "ask", state: "active", intent, grant });
|
|
const before = channel.sent.length;
|
|
session.input({ type: "move", x: .1, y: .2 });
|
|
expect(channel.sent.slice(before)).toEqual([{ type: "move", x: .1, y: .2, grant }]);
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
vi.advanceTimersByTime(1);
|
|
session.input({ type: "move", x: .3, y: .4 });
|
|
vi.advanceTimersByTime(1);
|
|
session.input({ type: "move", x: .5, y: .6 });
|
|
expect(channel.sent).toHaveLength(before + 1);
|
|
vi.advanceTimersByTime(2);
|
|
expect(channel.sent.slice(before)).toEqual([
|
|
{ type: "move", x: .1, y: .2, grant }, { type: "move", x: .5, y: .6, grant },
|
|
]);
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
vi.advanceTimersByTime(12);
|
|
session.input({ type: "relative", dx: 2, dy: -3 });
|
|
expect(channel.sent.at(-1)).toEqual({ type: "relative", dx: 2, dy: -3, grant });
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
} finally {
|
|
session.dispose();
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("keeps queued motion and wheel precision across updates to the same active grant", () => {
|
|
vi.useFakeTimers();
|
|
const channel = new Channel();
|
|
const session = new RemoteInputSession(channel as unknown as RTCDataChannel, () => {});
|
|
const grant = "b".repeat(64);
|
|
try {
|
|
channel.receive({ platform: "windows", mode: "room", state: "ready", coPlay: true, capabilities: ["coPlay", "text"] });
|
|
session.request();
|
|
const intent = channel.sent.at(-1)?.intent;
|
|
const active = { platform: "windows", mode: "room", state: "active", intent, grant, coPlay: true };
|
|
channel.receive({ ...active, capabilities: ["coPlay", "text"] });
|
|
session.input({ type: "move", x: .1, y: .2 });
|
|
session.input({ type: "move", x: .3, y: .4 });
|
|
session.scroll(.5, 0);
|
|
// Another controller joining disables committed text, not this player's motion.
|
|
channel.receive({ ...active, capabilities: ["coPlay"] });
|
|
vi.advanceTimersByTime(4);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "move", x: .3, y: .4, grant });
|
|
session.scroll(.5, 0);
|
|
expect(channel.sent.at(-1)).toEqual({ type: "wheel", delta: -1, grant });
|
|
// Suspension still discards motion and precision belonging to the old activity.
|
|
session.input({ type: "move", x: .9, y: .9 });
|
|
const beforeSuspend = channel.sent.length;
|
|
channel.receive({ ...active, state: "suspended", capabilities: ["coPlay"] });
|
|
vi.advanceTimersByTime(4);
|
|
expect(channel.sent).toHaveLength(beforeSuspend);
|
|
channel.receive({ ...active, capabilities: ["coPlay"] });
|
|
session.scroll(.75, 0);
|
|
expect(channel.sent).toHaveLength(beforeSuspend);
|
|
} finally {
|
|
session.dispose();
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
{ rotation: 0 as const, bounds: { left: 200, top: 150, width: 400, height: 300 },
|
|
gutter: { x: 180, y: 300 }, inside: { x: 240, y: 300 } },
|
|
{ rotation: 90 as const, bounds: { left: 200, top: 100 / 3, width: 400, height: 1600 / 3 },
|
|
gutter: { x: 400, y: 20 }, inside: { x: 400, y: 260 / 3 } },
|
|
])("rejects cover pixels clipped by a zoomed video box at $rotation degrees", ({ rotation, bounds, gutter, inside }) => {
|
|
// A 1920x1080 source covers an 800x600 cell, then local zoom shrinks it to 0.5x.
|
|
// object-fit clips the fitted source to the video box before the CSS transform.
|
|
const video = { videoWidth: 1920, videoHeight: 1080,
|
|
style: { objectFit: "cover" }, dataset: { pictureRotation: String(rotation) },
|
|
getBoundingClientRect: () => bounds,
|
|
} as unknown as HTMLVideoElement;
|
|
expect(picturePoint(video, gutter.x, gutter.y)).toBeNull();
|
|
const point = picturePoint(video, inside.x, inside.y);
|
|
expect(point?.x).toBeCloseTo(.2); expect(point?.y).toBeCloseTo(.5);
|
|
});
|