Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

fix: make H264 gate content independent

AAA68719dcdd8a2
diff --git a/src/client/webrtc/video-codec-preflight.ts b/src/client/webrtc/video-codec-preflight.ts
index 93174768..30f3597d 100644
--- a/src/client/webrtc/video-codec-preflight.ts
+++ b/src/client/webrtc/video-codec-preflight.ts
@@ -1,5 +1,6 @@
import {
configureVideoSender,
+ QUALITY_RESOLUTIONS,
startupVideoProfile,
type QualityProfile,
} from "../media/quality";
@@ -10,9 +11,19 @@ import {
const PREFLIGHT_DEADLINE_MS = 4_000;
const PREFLIGHT_POLL_MS = 100;
-const PREFLIGHT_MIN_SOURCE_FRAMES = 15;
-const PREFLIGHT_MAX_FRAME_DEFICIT = 2;
-const PREFLIGHT_FPS_WINDOW_MS = 1_000;
+const PREFLIGHT_WARMUP_MS = 500;
+const PREFLIGHT_MEASUREMENT_MS = 1_000;
+
+export interface H264ProbeTarget {
+ width: number;
+ height: number;
+ frameRate: number;
+}
+
+interface H264ProbeTrack {
+ track: MediaStreamTrack;
+ stop: () => void;
+}
export interface H264ProbeSample {
outboundId: string;
@@ -28,6 +39,104 @@ export interface H264ProbeSample {
type StatsRecord = RTCStats & Record<string, unknown>;
+function positiveNumber(value: unknown, fallback: number): number {
+ return typeof value === "number" && Number.isFinite(value) && value > 0
+ ? value
+ : fallback;
+}
+
+function evenDimension(value: number, ceiling: number): number {
+ return Math.min(ceiling, Math.max(2, Math.round(value / 2) * 2));
+}
+
+export function h264ProbeTarget(
+ settings: MediaTrackSettings,
+ profile: QualityProfile,
+): H264ProbeTarget {
+ const ceiling = QUALITY_RESOLUTIONS[profile.resolution];
+ const sourceWidth = positiveNumber(settings.width, ceiling.width);
+ const sourceHeight = positiveNumber(settings.height, ceiling.height);
+ const scale = Math.max(
+ sourceWidth / ceiling.width,
+ sourceHeight / ceiling.height,
+ 1,
+ );
+ return {
+ width: evenDimension(sourceWidth / scale, ceiling.width),
+ height: evenDimension(sourceHeight / scale, ceiling.height),
+ frameRate: profile.maxFramerate,
+ };
+}
+
+function drawProbeFrame(
+ context: CanvasRenderingContext2D,
+ target: H264ProbeTarget,
+ frame: number,
+): void {
+ const columns = 24;
+ const rows = 14;
+ const tileWidth = Math.ceil(target.width / columns);
+ const tileHeight = Math.ceil(target.height / rows);
+ for (let row = 0; row < rows; row += 1) {
+ for (let column = 0; column < columns; column += 1) {
+ const hue = (frame * 29 + row * 71 + column * 43) % 360;
+ const lightness = 35 + ((frame + row + column) % 30);
+ context.fillStyle = `hsl(${hue} 80% ${lightness}%)`;
+ context.fillRect(
+ column * tileWidth,
+ row * tileHeight,
+ tileWidth,
+ tileHeight,
+ );
+ }
+ }
+ const sweep =
+ (frame * Math.max(8, Math.floor(target.width / 120))) % target.width;
+ context.fillStyle = "#ffffff";
+ context.fillRect(
+ sweep,
+ 0,
+ Math.max(4, Math.floor(target.width / 180)),
+ target.height,
+ );
+}
+
+function createH264ProbeTrack(
+ source: MediaStreamTrack,
+ profile: QualityProfile,
+): H264ProbeTrack | null {
+ if (typeof document === "undefined") {
+ return null;
+ }
+ const target = h264ProbeTarget(source.getSettings(), profile);
+ const canvas = document.createElement("canvas");
+ canvas.width = target.width;
+ canvas.height = target.height;
+ const context = canvas.getContext("2d", { alpha: false });
+ if (!context || typeof canvas.captureStream !== "function") {
+ return null;
+ }
+ const stream = canvas.captureStream(target.frameRate);
+ const track = stream.getVideoTracks()[0];
+ if (!track) {
+ return null;
+ }
+ track.contentHint = "motion";
+ let frame = 1;
+ drawProbeFrame(context, target, frame);
+ const timer = window.setInterval(() => {
+ frame += 1;
+ drawProbeFrame(context, target, frame);
+ }, 1_000 / target.frameRate);
+ return {
+ track,
+ stop: () => {
+ window.clearInterval(timer);
+ track.stop();
+ },
+ };
+}
+
function finiteNumber(value: unknown): number | null {
return typeof value === "number" && Number.isFinite(value) ? value : null;
}
@@ -86,9 +195,17 @@ function readH264ProbeSample(report: RTCStatsReport): H264ProbeSample | null {
};
}
-export function h264ProbeSustainsSource(
+function allowedFrameLag(targetFrameRate: number): number {
+ return Math.max(
+ 1,
+ Math.ceil((targetFrameRate * PREFLIGHT_POLL_MS) / 1_000),
+ );
+}
+
+export function h264ProbeSustainsTarget(
baseline: H264ProbeSample,
current: H264ProbeSample,
+ targetFrameRate: number,
): boolean | null {
if (
baseline.outboundId !== current.outboundId ||
@@ -99,6 +216,12 @@ export function h264ProbeSustainsSource(
) {
return false;
}
+ const elapsedMs = current.timestamp - baseline.timestamp;
+ if (elapsedMs < PREFLIGHT_MEASUREMENT_MS) {
+ return null;
+ }
+ const expectedFrames = (targetFrameRate * elapsedMs) / 1_000;
+ const frameLag = allowedFrameLag(targetFrameRate);
if (
baseline.framesEncoded !== null &&
current.framesEncoded !== null &&
@@ -109,13 +232,13 @@ export function h264ProbeSustainsSource(
) {
const encoded = current.framesEncoded - baseline.framesEncoded;
const source = current.sourceFrames - baseline.sourceFrames;
- if (source < PREFLIGHT_MIN_SOURCE_FRAMES) {
- return null;
- }
- return encoded + PREFLIGHT_MAX_FRAME_DEFICIT >= source;
+ return (
+ source + frameLag >= expectedFrames &&
+ encoded + frameLag >= expectedFrames &&
+ encoded + frameLag >= source
+ );
}
if (
- current.timestamp - baseline.timestamp < PREFLIGHT_FPS_WINDOW_MS ||
current.encodedFramesPerSecond === null ||
current.sourceFramesPerSecond === null ||
current.sourceFramesPerSecond <= 0
@@ -123,8 +246,10 @@ export function h264ProbeSustainsSource(
return null;
}
return (
- current.encodedFramesPerSecond + PREFLIGHT_MAX_FRAME_DEFICIT >=
- current.sourceFramesPerSecond
+ current.sourceFramesPerSecond + frameLag >= targetFrameRate &&
+ current.encodedFramesPerSecond + frameLag >= targetFrameRate &&
+ current.encodedFramesPerSecond + frameLag >=
+ current.sourceFramesPerSecond
);
}
@@ -139,17 +264,7 @@ function hasProbeWindow(
) {
return false;
}
- if (
- baseline.sourceFrames !== null &&
- current.sourceFrames !== null &&
- current.sourceFrames >= baseline.sourceFrames
- ) {
- return (
- current.sourceFrames - baseline.sourceFrames >=
- PREFLIGHT_MIN_SOURCE_FRAMES
- );
- }
- return current.timestamp - baseline.timestamp >= PREFLIGHT_FPS_WINDOW_MS;
+ return current.timestamp - baseline.timestamp >= PREFLIGHT_WARMUP_MS;
}
function delay(milliseconds: number, signal?: AbortSignal): Promise<void> {
@@ -240,7 +355,11 @@ async function runH264Probe(
) {
measurementBaseline = sample;
} else if (measurementBaseline && sample !== measurementBaseline) {
- const result = h264ProbeSustainsSource(measurementBaseline, sample);
+ const result = h264ProbeSustainsTarget(
+ measurementBaseline,
+ sample,
+ profile.maxFramerate,
+ );
if (result !== null) {
return result;
}
@@ -264,9 +383,15 @@ export async function preferredVideoCodecForTrack(
if (track.kind !== "video" || track.readyState === "ended") {
return "vp8";
}
+ let probe: H264ProbeTrack | null = null;
try {
- return (await runH264Probe(track, profile, signal)) ? "h264" : "vp8";
+ probe = createH264ProbeTrack(track, profile);
+ return probe && (await runH264Probe(probe.track, profile, signal))
+ ? "h264"
+ : "vp8";
} catch {
return "vp8";
+ } finally {
+ probe?.stop();
}
}
diff --git a/tests/video-codec-preflight.test.ts b/tests/video-codec-preflight.test.ts
index 9c677ab9..a8467338 100644
--- a/tests/video-codec-preflight.test.ts
+++ b/tests/video-codec-preflight.test.ts
@@ -1,9 +1,11 @@
import { describe, expect, it } from "vitest";
import {
- h264ProbeSustainsSource,
+ h264ProbeTarget,
+ h264ProbeSustainsTarget,
type H264ProbeSample,
} from "../src/client/webrtc/video-codec-preflight.ts";
+import { QUALITY_PROFILES } from "../src/client/media/quality.ts";
function sample(
overrides: Partial<H264ProbeSample> = {},
@@ -23,6 +25,15 @@ function sample(
}
describe("H264 sender preflight", () => {
+ it("uses the selected target cadence instead of captured-content cadence", () => {
+ expect(
+ h264ProbeTarget(
+ { width: 2_560, height: 1_440, frameRate: 1 },
+ QUALITY_PROFILES["1080p30"],
+ ),
+ ).toEqual({ width: 1_920, height: 1_080, frameRate: 30 });
+ });
+
it("accepts bounded pipeline lag against the same source", () => {
const baseline = sample();
const current = sample({
@@ -31,7 +42,7 @@ describe("H264 sender preflight", () => {
sourceFrames: 40,
});
- expect(h264ProbeSustainsSource(baseline, current)).toBe(true);
+ expect(h264ProbeSustainsTarget(baseline, current, 30)).toBe(true);
});
it("rejects sustained encoder frame dropping", () => {
@@ -44,7 +55,7 @@ describe("H264 sender preflight", () => {
sourceFramesPerSecond: 30,
});
- expect(h264ProbeSustainsSource(baseline, current)).toBe(false);
+ expect(h264ProbeSustainsTarget(baseline, current, 30)).toBe(false);
});
it("waits for enough source progress before deciding", () => {
@@ -55,14 +66,14 @@ describe("H264 sender preflight", () => {
sourceFrames: 20,
});
- expect(h264ProbeSustainsSource(baseline, current)).toBeNull();
+ expect(h264ProbeSustainsTarget(baseline, current, 30)).toBeNull();
});
it("rejects CPU-limited or identity-mismatched samples", () => {
const baseline = sample();
expect(
- h264ProbeSustainsSource(
+ h264ProbeSustainsTarget(
baseline,
sample({
timestamp: 2_000,
@@ -70,10 +81,11 @@ describe("H264 sender preflight", () => {
sourceFrames: 40,
qualityLimitationReason: "cpu",
}),
+ 30,
),
).toBe(false);
expect(
- h264ProbeSustainsSource(
+ h264ProbeSustainsTarget(
baseline,
sample({
outboundId: "replacement",
@@ -81,6 +93,7 @@ describe("H264 sender preflight", () => {
framesEncoded: 40,
sourceFrames: 40,
}),
+ 30,
),
).toBe(false);
});
@@ -89,7 +102,7 @@ describe("H264 sender preflight", () => {
const baseline = sample({ framesEncoded: null, sourceFrames: null });
expect(
- h264ProbeSustainsSource(
+ h264ProbeSustainsTarget(
baseline,
sample({
timestamp: 2_000,
@@ -98,10 +111,11 @@ describe("H264 sender preflight", () => {
encodedFramesPerSecond: 29,
sourceFramesPerSecond: 30,
}),
+ 30,
),
).toBe(true);
expect(
- h264ProbeSustainsSource(
+ h264ProbeSustainsTarget(
baseline,
sample({
timestamp: 2_000,
@@ -110,10 +124,11 @@ describe("H264 sender preflight", () => {
encodedFramesPerSecond: 12,
sourceFramesPerSecond: 30,
}),
+ 30,
),
).toBe(false);
expect(
- h264ProbeSustainsSource(
+ h264ProbeSustainsTarget(
baseline,
sample({
timestamp: 1_500,
@@ -122,7 +137,24 @@ describe("H264 sender preflight", () => {
encodedFramesPerSecond: 30,
sourceFramesPerSecond: 30,
}),
+ 30,
),
).toBeNull();
});
+
+ it("rejects a probe source that does not reach the selected cadence", () => {
+ expect(
+ h264ProbeSustainsTarget(
+ sample(),
+ sample({
+ timestamp: 2_000,
+ framesEncoded: 30,
+ sourceFrames: 30,
+ encodedFramesPerSecond: 20,
+ sourceFramesPerSecond: 20,
+ }),
+ 30,
+ ),
+ ).toBe(false);
+ });
});