Windows native sharing could end when a refresh frame carried an older timestamp. Concurrent route preparation, source changes and upstream reconnection could also disturb an already committed relay path. This maintenance phase fixes those owners and improves large-frame reception, App return handling and update visibility. The existing 2.x wire and storage contracts are unchanged. The App update indicator uses the approved App's build identity and shares the launcher's release check and download component. Validation: `npm run check`, `node scripts/check-go.mjs --all`, repository hygiene, and the existing controlled three-viewer H.264 routing/recovery and App-return checks. The [cross-platform v2.5.3 candidate build](https://github.com/TNTcraftHIM/Piik/actions/runs/41716820555) passes all six App and three Server packages. Its only follow-up changes align the App card and retain the version label's accessible text in visual mode; those changes pass type checks, focused tests and desktop/mobile browser checks in both themes. Refs #519. ## 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.3 improves Windows native sharing, relay recovery and App update notices. It preserves the existing 2.x compatibility baseline. - Fix backward refresh-frame timestamps that could end Windows native sharing, and improve H.264 compatibility for small native outputs. - Preserve committed routes and relay children during upstream reconnection, overlapping route preparation and source changes. - Reduce large-frame UDP burst loss, keep quality comparisons within their route deadline, and release retired media-statistics work promptly. - Show App updates from the connected page as well as the launcher. Refresh App identity after returning to the browser and allow local-network permission prompts to finish. - Use AtomGit for the download mirror and show a clear startup error when page controls cannot load. Update the App and self-hosted Server to receive all fixes, then refresh open pages. Existing room data and browser settings are retained. ### 中文 Piik v2.5.3 改善 Windows 原生分享、转发线路恢复和 App 更新提醒,沿用现有 2.x 兼容基线。 - 修复补帧时间戳倒退导致 Windows 原生分享结束的问题,改善小尺寸原生画面的 H.264 兼容性。 - 上游重连、线路准备重叠或分享源变化时,保留已生效的线路和下游转发。 - 减少大画面帧突发传输时的 UDP 丢包,让画质比较遵守当前线路时限,并及时释放已退出的媒体统计任务。 - 在已联动网页和启动页显示 App 更新提醒;返回浏览器后刷新 App 信息,并允许本地网络权限提示完成。 - 下载镜像改用 AtomGit;页面控件加载失败时显示清晰的错误提示。 请更新 App 和自部署 Server,以获得全部修复,随后刷新已打开的页面。已有房间数据与浏览器设置保留。
302 lines
15 KiB
TypeScript
302 lines
15 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { BrowserEncodingProducer } from "../src/client/media/browser-encoding-producer";
|
|
import { BrowserEncodingPool } from "../src/client/media/browser-encoding-pool";
|
|
import type { BrowserEncodingOutput } from "../src/client/media/browser-encoding-output";
|
|
import { applyVideoCaptureProfile, configureVideoSender, QUALITY_PROFILES } from "../src/client/media/quality";
|
|
import { cloneSenderVideoTrack } from "../src/client/media/sender-video-track";
|
|
|
|
vi.mock("../src/client/media/sender-video-track", async (original) => ({
|
|
...await original<typeof import("../src/client/media/sender-video-track")>(),
|
|
cloneSenderVideoTrack: vi.fn((source: MediaStreamTrack) => source.clone()),
|
|
}));
|
|
|
|
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=extmap:3 urn:3gpp:video-orientation", "a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid",
|
|
`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);
|
|
expect(cloneSenderVideoTrack).toHaveBeenCalledWith(expect.anything(), expect.any(Function), { normalizeOrientation: true });
|
|
const [send, receive] = Connection.instances;
|
|
const answer = receive.localDescription!;
|
|
expect(answer.sdp).not.toContain("urn:3gpp:video-orientation");
|
|
expect(answer.sdp).toContain("a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid");
|
|
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!.sdp).not.toContain("x-google-start-bitrate");
|
|
expect(Connection.instances[1].localDescription!.sdp).not.toContain("urn:3gpp:video-orientation");
|
|
});
|
|
|
|
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("releases a pending stats wait when retired without failing the producer", async () => {
|
|
const { producer, source, failed } = createProducer();
|
|
await producer.start();
|
|
Connection.instances[0].getStats.mockReturnValueOnce(new Promise(() => {}));
|
|
let outcome = "pending";
|
|
void producer.report().then(() => { outcome = "resolved"; }, error => { outcome = error.name; });
|
|
producer.dispose();
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(outcome).toBe("AbortError");
|
|
expect(failed).not.toHaveBeenCalled();
|
|
expect(source.stop).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps the pool sampling after retiring a producer with pending stats", async () => {
|
|
vi.stubGlobal("RTCRtpSender", { getCapabilities: () => ({ codecs: [{ mimeType: "video/VP8", clockRate: 90_000 }] }) });
|
|
const pool = new BrowserEncodingPool();
|
|
const addMember = () => {
|
|
const source = Object.assign(new Track(), { kind: "video" });
|
|
const getStats = vi.fn(async () => new Map([
|
|
["video", { type: "outbound-rtp", kind: "video", codecId: "codec", targetBitrate: 2_000_000 }],
|
|
["codec", { type: "codec", mimeType: "video/VP8", clockRate: 90_000 }],
|
|
]) as unknown as RTCStatsReport);
|
|
const output = { snapshot: () => ({ frames: 0 }), select: vi.fn(), passthrough: vi.fn() } as unknown as BrowserEncodingOutput;
|
|
const fatal = vi.fn();
|
|
const handle = pool.create(source as unknown as MediaStreamTrack, { getStats } as unknown as RTCRtpSender,
|
|
{ connectionState: "connected", getStats } as unknown as RTCPeerConnection, QUALITY_PROFILES["1080p30"],
|
|
output, async () => true, async () => undefined, fatal)!;
|
|
return { handle, getStats, fatal };
|
|
};
|
|
try {
|
|
const retired = addMember(), healthy = addMember();
|
|
await vi.advanceTimersByTimeAsync(500);
|
|
Connection.instances[0].getStats.mockReturnValueOnce(new Promise(() => {}));
|
|
await vi.advanceTimersByTimeAsync(500);
|
|
expect(Connection.instances[0].getStats).toHaveBeenCalledOnce();
|
|
retired.handle.dispose();
|
|
await vi.advanceTimersByTimeAsync(500);
|
|
expect(healthy.getStats).toHaveBeenCalledTimes(3);
|
|
expect(healthy.fatal).not.toHaveBeenCalled();
|
|
expect(Connection.instances[0].close).toHaveBeenCalledOnce();
|
|
expect(Connection.instances[2].close).not.toHaveBeenCalled();
|
|
} finally { pool.dispose(); }
|
|
});
|
|
|
|
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();
|
|
});
|
|
});
|