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 与自部署站点,以获得全部修复,随后刷新已打开的页面。浏览器设置与已有房间数据保留。
249 lines
11 KiB
TypeScript
249 lines
11 KiB
TypeScript
import { debugError, debugEvent } from "../lib/debug";
|
|
import { supportsFrameOrientationNormalization } from "./sender-video-track";
|
|
|
|
// One codec-sized macroblock; transport timing is not a quality layer.
|
|
export const BROWSER_CARRIER_SIZE = 16;
|
|
|
|
export function supportsBrowserEncoding(): boolean {
|
|
return supportsFrameOrientationNormalization() && typeof RTCRtpSender !== "undefined" && typeof RTCEncodedVideoFrame === "function" &&
|
|
typeof HTMLCanvasElement !== "undefined" && typeof HTMLCanvasElement.prototype.captureStream === "function" &&
|
|
typeof (RTCRtpSender.prototype as RTCRtpSender & { createEncodedStreams?: unknown }).createEncodedStreams === "function";
|
|
}
|
|
|
|
export function encodedStreams<Frame extends RTCEncodedVideoFrame | RTCEncodedAudioFrame = RTCEncodedVideoFrame>(sender: RTCRtpSender): {
|
|
readable: ReadableStream<Frame>; writable: WritableStream<Frame>;
|
|
} {
|
|
return (sender as RTCRtpSender & { createEncodedStreams(): {
|
|
readable: ReadableStream<Frame>; writable: WritableStream<Frame>;
|
|
} }).createEncodedStreams();
|
|
}
|
|
|
|
export interface BrowserEncodingOutputSnapshot {
|
|
frames: number;
|
|
width: number | null;
|
|
height: number | null;
|
|
lastProducerId: string | null;
|
|
timestamp: number;
|
|
}
|
|
|
|
type Queue = { producerId: string; frames: RTCEncodedVideoFrame[]; needKey: boolean; requestKey: () => Promise<void> };
|
|
type Selection = { queue: Queue; onSelected: () => void; accept: (metadata: RTCEncodedVideoFrameMetadata) => boolean };
|
|
// Bound pending encoded data; a lost dependency chain resumes on a fresh key.
|
|
const MAX_QUEUED_FRAMES = 4;
|
|
|
|
function copyFrame(frame: RTCEncodedVideoFrame, rtpTimestamp?: number): RTCEncodedVideoFrame {
|
|
const Frame = RTCEncodedVideoFrame as unknown as { new(frame: RTCEncodedVideoFrame,
|
|
options?: { metadata: RTCEncodedVideoFrameMetadata & { rtpTimestamp: number } }): RTCEncodedVideoFrame };
|
|
return new Frame(frame, rtpTimestamp === undefined ? undefined : { metadata: { ...frame.getMetadata(), rtpTimestamp } });
|
|
}
|
|
|
|
/** One connection-lifetime encoded stream, including ordinary fallback. */
|
|
export class BrowserEncodingOutput {
|
|
private readonly clock = document.createElement("canvas");
|
|
private readonly clockContext: CanvasRenderingContext2D;
|
|
readonly track: CanvasCaptureMediaStreamTrack;
|
|
private clockFrame = 0;
|
|
private readonly abort = new AbortController();
|
|
private readonly writer: WritableStreamDefaultWriter<RTCEncodedVideoFrame>;
|
|
private current: Queue | undefined;
|
|
private pending: Selection | undefined;
|
|
private carrier: { frame: RTCEncodedVideoFrame; recovery: boolean } | undefined;
|
|
private writing: Promise<void> | undefined;
|
|
private ownKey: (() => Promise<void>) | undefined;
|
|
private raw: false | "key" | true = false;
|
|
private paused = false;
|
|
private epoch = 0;
|
|
private readonly sample = { frames: 0, width: null as number | null, height: null as number | null,
|
|
lastProducerId: null as string | null };
|
|
|
|
constructor(sender: RTCRtpSender, private readonly onFailure: () => void, private readonly identity: object) {
|
|
// A CPU-resident macroblock avoids repeatedly mapping/scaling a full GPU
|
|
// decoder surface merely to provide outgoing RTP timing.
|
|
this.clock.width = this.clock.height = BROWSER_CARRIER_SIZE;
|
|
const context = this.clock.getContext("2d");
|
|
if (!context) throw new Error("Browser carrier canvas unavailable");
|
|
this.clockContext = context;
|
|
this.track = this.clock.captureStream(0).getVideoTracks()[0] as CanvasCaptureMediaStreamTrack;
|
|
// This synthetic clock needs WebRTC's screen-content ALR probing when the
|
|
// real producer sends little data. The producer owns picture adaptation.
|
|
this.track.contentHint = "detail";
|
|
let streams: ReturnType<typeof encodedStreams<RTCEncodedVideoFrame>>;
|
|
try {
|
|
streams = encodedStreams(sender);
|
|
this.writer = streams.writable.getWriter();
|
|
} catch (error) {
|
|
this.track.stop();
|
|
throw error;
|
|
}
|
|
void streams.readable.pipeTo(new WritableStream({ write: (frame) => this.receive(frame) }),
|
|
{ signal: this.abort.signal }).then(() => this.fail(), (error) => this.fail(error)).finally(() => {
|
|
try { this.writer.releaseLock(); } catch { /* Owner abort retired the writer. */ }
|
|
});
|
|
}
|
|
|
|
select(producerId: string, requestKey: () => Promise<void>, onSelected: () => void,
|
|
accept: Selection["accept"]): void {
|
|
if (this.abort.signal.aborted) return;
|
|
this.epoch++;
|
|
this.carrier = undefined;
|
|
this.pending = { queue: { producerId, frames: [], needKey: true, requestKey }, onSelected, accept };
|
|
debugEvent("encoding-pool", "selection-requested", { ...this.identity, producerId });
|
|
if (!this.paused) this.request(this.pending.queue);
|
|
}
|
|
|
|
cancelSelection(producerId: string): boolean {
|
|
if (this.pending?.queue.producerId !== producerId) return true;
|
|
// Once an accepted key starts writing, that queue owns the wire. Keep its
|
|
// pool reference until completion, including across pause/resume.
|
|
if (this.current === this.pending.queue) return false;
|
|
this.pending = undefined;
|
|
return true;
|
|
}
|
|
|
|
push(producerId: string, frame: RTCEncodedVideoFrame): void {
|
|
if (this.abort.signal.aborted || this.paused || frame.data.byteLength === 0) return;
|
|
try {
|
|
let accepted = false;
|
|
for (const queue of new Set([this.current, this.pending?.queue])) {
|
|
if (queue?.producerId !== producerId) continue;
|
|
if (frame.type === "key") { queue.frames.length = 0; queue.needKey = false; }
|
|
else if (queue.frames.length >= MAX_QUEUED_FRAMES) this.recover(queue);
|
|
if (!queue.needKey) { queue.frames.push(copyFrame(frame)); accepted = true; }
|
|
}
|
|
if (accepted) {
|
|
this.clockContext.fillStyle = "#080808";
|
|
this.clockContext.fillRect(0, 0, this.clock.width, this.clock.height);
|
|
this.clockContext.fillStyle = "#181818";
|
|
this.clockContext.fillRect(this.clockFrame++ % this.clock.width, 0, 1, 1);
|
|
this.track.requestFrame();
|
|
}
|
|
void this.drain();
|
|
} catch (error) { this.fail(error); }
|
|
}
|
|
|
|
passthrough(requestKey: () => Promise<void>): void {
|
|
if (this.abort.signal.aborted) return;
|
|
this.epoch++;
|
|
this.current = undefined; this.pending = undefined; this.carrier = undefined;
|
|
this.raw = "key";
|
|
debugEvent("encoding-pool", "ordinary-requested", this.identity);
|
|
this.ownKey = requestKey;
|
|
if (!this.paused) this.requestOwnKey();
|
|
}
|
|
|
|
setPaused(paused: boolean): void {
|
|
if (this.abort.signal.aborted || this.paused === paused) return;
|
|
this.paused = paused;
|
|
this.track.enabled = !paused;
|
|
debugEvent("encoding-pool", "paused", { ...this.identity, paused });
|
|
this.epoch++;
|
|
this.carrier = undefined;
|
|
if (this.raw) this.raw = "key";
|
|
for (const queue of new Set([this.current, this.pending?.queue])) if (queue) {
|
|
queue.frames.length = 0; queue.needKey = true;
|
|
if (!paused) this.request(queue);
|
|
}
|
|
if (!paused && this.raw) this.requestOwnKey();
|
|
}
|
|
|
|
snapshot(): BrowserEncodingOutputSnapshot { return { ...this.sample, timestamp: performance.now() }; }
|
|
|
|
dispose(): void {
|
|
if (this.abort.signal.aborted) return;
|
|
this.epoch++;
|
|
this.current = undefined; this.pending = undefined; this.carrier = undefined; this.ownKey = undefined;
|
|
this.abort.abort();
|
|
this.track.stop();
|
|
void this.writer.abort().catch(() => undefined);
|
|
}
|
|
|
|
private async receive(frame: RTCEncodedVideoFrame): Promise<void> {
|
|
if (this.abort.signal.aborted || this.paused || frame.data.byteLength === 0) return;
|
|
if (this.raw) {
|
|
const epoch = this.epoch;
|
|
await this.writing;
|
|
if (this.abort.signal.aborted || this.paused || this.epoch !== epoch) return;
|
|
}
|
|
// Coalescing keeps the newest clock, but must not overwrite a recovery
|
|
// request received while the previous output write is still pending.
|
|
this.carrier = { frame, recovery: frame.type === "key" || this.carrier?.recovery === true };
|
|
const writing = this.drain();
|
|
if (this.raw) await writing;
|
|
}
|
|
|
|
private drain(): Promise<void> | undefined {
|
|
if (this.abort.signal.aborted || this.paused || this.writing || !this.carrier) return;
|
|
try {
|
|
let selection = this.pending?.queue.frames[0]?.type === "key" ? this.pending : undefined;
|
|
// Preparation is not commitment: validate the actual key while the old
|
|
// stream is still intact, before consuming either queue or writing bytes.
|
|
if (selection && selection.queue !== this.current && !selection.accept(selection.queue.frames[0]!.getMetadata())) {
|
|
debugEvent("encoding-pool", "selection-rejected", { ...this.identity, producerId: selection.queue.producerId });
|
|
this.pending = undefined;
|
|
selection = undefined;
|
|
}
|
|
const { frame: own, recovery } = this.carrier;
|
|
// A carrier key can be a remote PLI. Only an accepted candidate key can
|
|
// replace the current stream's recovery; a rejected one proves nothing.
|
|
if (recovery && !selection && this.current && this.current.frames[0]?.type !== "key") this.recover(this.current);
|
|
if (this.raw && !selection) {
|
|
this.carrier = undefined;
|
|
if (this.raw === "key" && own.type !== "key") return;
|
|
return this.write(own, null);
|
|
}
|
|
const queue = selection?.queue ?? this.current, frame = queue?.frames.shift();
|
|
if (!queue || !frame) return;
|
|
this.carrier = undefined;
|
|
const copy = copyFrame(frame, own.timestamp);
|
|
this.current = queue;
|
|
this.raw = false;
|
|
return this.write(copy, queue.producerId, selection);
|
|
} catch (error) { this.fail(error); }
|
|
}
|
|
|
|
private write(frame: RTCEncodedVideoFrame, producerId: string | null, selection?: Selection): Promise<void> {
|
|
const epoch = this.epoch, metadata = frame.getMetadata(), key = frame.type === "key";
|
|
const writing = this.writer.write(frame).then(() => {
|
|
if (this.abort.signal.aborted) return;
|
|
this.sample.frames++;
|
|
this.sample.width = metadata.width ?? null; this.sample.height = metadata.height ?? null;
|
|
this.sample.lastProducerId = producerId;
|
|
if (epoch !== this.epoch || this.paused) return;
|
|
if (key) this.raw = producerId === null;
|
|
if (selection && this.pending === selection) {
|
|
this.pending = undefined;
|
|
debugEvent("encoding-pool", "selected", { ...this.identity, producerId, width: metadata.width, height: metadata.height });
|
|
selection.onSelected();
|
|
}
|
|
}).catch((error) => this.fail(error)).finally(() => { this.writing = undefined; void this.drain(); });
|
|
this.writing = writing;
|
|
return writing;
|
|
}
|
|
|
|
private recover(queue: Queue): void {
|
|
if (queue.needKey) return;
|
|
debugEvent("encoding-pool", "recovery-requested", { ...this.identity, producerId: queue.producerId, queued: queue.frames.length });
|
|
queue.frames.length = 0; queue.needKey = true;
|
|
this.request(queue);
|
|
}
|
|
|
|
private request(queue: Queue): void {
|
|
const epoch = this.epoch;
|
|
void queue.requestKey().catch((error) => {
|
|
if (epoch === this.epoch && (this.current === queue || this.pending?.queue === queue)) this.fail(error);
|
|
});
|
|
}
|
|
|
|
private requestOwnKey(): void {
|
|
const epoch = this.epoch;
|
|
void this.ownKey?.().catch((error) => { if (epoch === this.epoch) this.fail(error); });
|
|
}
|
|
|
|
private fail(error?: unknown): void {
|
|
if (this.abort.signal.aborted) return;
|
|
debugError("encoding-pool", "output-failed", error, { ...this.identity, producerId: this.current?.producerId });
|
|
this.dispose();
|
|
this.onFailure();
|
|
}
|
|
}
|