Files
piik/tests/quality.test.ts
T
AAA 1715a191a1 refactor(client): remove exports only their tests called
The orchestration that used these helpers was retired in 5d4a67cf and
5126b9a0, leaving them reachable only from their own tests. The Browser-
rejected video check and its error, the encoded-frame maximum, the quality
label and whole-settings comparison (with the audio comparison it alone
used), and the unused PeerSnapshot, ChannelRequest and NativePageMpdTrack
types go. The video-only comparison, which the encoding pool and transport
still use, stays.

currentLang had no product caller; the tests that read the language through
it now read it through say(), the accessor the product uses.
2026-11-25 18:30:26 +08:00

626 lines
24 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import {
applyCaptureProfile,
applyVideoCaptureProfile,
captureDisplay,
captureFramerateCeiling,
supportsCaptureProfile,
configureAudioSender,
configureVideoSender,
matchingQualityProfileId,
needsStartupVideoProfile,
QUALITY_PROFILES,
QUALITY_RESOLUTIONS,
videoQualitySettingsEqual,
screenAudioBitrate,
screenVideoCeiling,
audioSenderCeiling,
screenContentHint,
setMediaPaused,
} from "../src/client/media/quality.ts";
function createVideoStream() {
const videoTrack = {
contentHint: "",
enabled: true,
applyConstraints: vi.fn(async () => undefined),
getSettings: () => ({ width: 1920, height: 1080, frameRate: 60 }),
} as unknown as MediaStreamTrack;
const audioTrack = {
contentHint: "",
enabled: true,
} as unknown as MediaStreamTrack;
const stream = {
getTracks: () => [videoTrack, audioTrack],
getVideoTracks: () => [videoTrack],
getAudioTracks: () => [audioTrack],
} as unknown as MediaStream;
return { stream, videoTrack, audioTrack };
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("realtime quality controls", () => {
it("bounds custom size, orientation and frame rate together before capture", () => {
expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["2160p"])).toBe(64);
expect(captureFramerateCeiling({ width: 2160, height: 3840 })).toBe(64);
expect(captureFramerateCeiling(QUALITY_RESOLUTIONS["1440p"])).toBe(144);
expect(captureFramerateCeiling({ width: 1440, height: 2560 })).toBe(144);
expect(captureFramerateCeiling({ width: 3840, height: 2100 })).toBe(65);
expect(captureFramerateCeiling({ width: 5120, height: 1440 })).toBe(72);
expect(captureFramerateCeiling({ width: 1920, height: 1080 })).toBe(240);
for (const size of [{ width: 3840, height: 3840 }, { width: 1921, height: 1080 },
{ width: 8194, height: 100 }, { width: 6562, height: 1438 },
{ width: 4.5, height: 4 }, { width: Infinity, height: 4 }, { width: 2, height: 4 }]) {
expect(captureFramerateCeiling(size)).toBe(0);
}
});
it("rejects unsupported requests before changing capture or asking for permission", async () => {
const { stream, videoTrack } = createVideoStream();
const getDisplayMedia = vi.fn(async () => stream);
vi.stubGlobal("navigator", { mediaDevices: { getDisplayMedia } });
for (const change of [{ maxFramerate: 241 }, { maxFramerate: 30.5 }, { maxFramerate: 0 },
{ maxBitrate: 99_999 }, { maxBitrate: 240_000_001 },
{ resolution: { width: 32768, height: 32768 } }, { maxAudioBitrate: 510_000 }]) {
const requested = { ...QUALITY_PROFILES["1080p30"], ...change };
expect(supportsCaptureProfile(requested)).toBe(false);
await expect(captureDisplay(requested)).rejects.toThrow("not supported");
await expect(applyCaptureProfile(stream, requested)).rejects.toThrow("not supported");
}
expect(getDisplayMedia).not.toHaveBeenCalled();
expect(videoTrack.applyConstraints).not.toHaveBeenCalled();
});
it("admits useful low-rate and high-rate requests without changing presets", async () => {
const { stream, videoTrack } = createVideoStream();
for (const [maxFramerate, maxBitrate] of [[1, 100_000], [5, 800_000], [60, 50_000_000],
[120, 80_000_000], [144, 100_000_000], [240, 240_000_000]]) {
const profile = { ...QUALITY_PROFILES["1080p30"], maxFramerate, maxBitrate };
expect(supportsCaptureProfile(profile)).toBe(true);
await applyCaptureProfile(stream, profile);
expect(videoTrack.applyConstraints).toHaveBeenLastCalledWith(expect.objectContaining({
frameRate: { ideal: maxFramerate, max: maxFramerate },
}));
}
});
it("requests 4K60 in either orientation through the existing capture controls", async () => {
const { stream, videoTrack } = createVideoStream();
const getDisplayMedia = vi.fn(async () => stream);
vi.stubGlobal("navigator", { mediaDevices: { getDisplayMedia } });
for (const resolution of [QUALITY_RESOLUTIONS["2160p"], { width: 2160, height: 3840 }]) {
const profile = { ...QUALITY_PROFILES["1080p60"], resolution, maxBitrate: 50_000_000 };
expect(supportsCaptureProfile(profile)).toBe(true);
await captureDisplay(profile);
expect(getDisplayMedia).toHaveBeenLastCalledWith(expect.objectContaining({
video: expect.objectContaining({
width: expect.objectContaining({ max: resolution.width }),
height: expect.objectContaining({ max: resolution.height }),
frameRate: expect.objectContaining({ max: 60 }),
}),
}));
await applyCaptureProfile(stream, profile);
expect(videoTrack.applyConstraints).toHaveBeenCalled();
}
});
it("keeps exactly three recommended profiles with 1080p30 as the default", () => {
expect(Object.keys(QUALITY_PROFILES)).toEqual([
"720p30",
"1080p30",
"1080p60",
]);
expect(QUALITY_PROFILES["1080p30"]).toMatchObject({
resolution: { width: 1920, height: 1080 },
maxFramerate: 30,
maxBitrate: 5_000_000,
});
});
it("offers 854x480 only through advanced resolution settings", () => {
expect(QUALITY_RESOLUTIONS["480p"]).toEqual({
width: 854,
height: 480,
});
expect(
Object.values(QUALITY_PROFILES).map((profile) => profile.resolution),
).not.toContainEqual(QUALITY_RESOLUTIONS["480p"]);
});
it("bounds initial capture to the selected profile", async () => {
const { stream, videoTrack, audioTrack } = createVideoStream();
const getDisplayMedia = vi.fn(async () => stream);
vi.stubGlobal("navigator", { mediaDevices: { getDisplayMedia } });
await expect(captureDisplay(QUALITY_PROFILES["1080p30"])).resolves.toBe(
stream,
);
expect(getDisplayMedia).toHaveBeenCalledWith({
video: {
displaySurface: "browser",
width: { max: 1920 },
height: { max: 1080 },
frameRate: { ideal: 30, max: 30 },
},
audio: {
echoCancellation: false,
noiseSuppression: false,
autoGainControl: false,
voiceIsolation: false,
channelCount: { ideal: 2 },
restrictOwnAudio: true,
},
systemAudio: "include",
windowAudio: "window",
});
// The sender owns the video hint from the picture preference.
expect(videoTrack.contentHint).toBe("");
expect(audioTrack.contentHint).toBe("music");
});
it("changes capture ceilings without requesting a crop or locking source orientation", async () => {
const { stream, videoTrack } = createVideoStream();
videoTrack.getSettings = () => ({ width: 540, height: 960, aspectRatio: 9 / 16 });
await applyCaptureProfile(stream, {
resolution: { width: 1280, height: 720 },
maxFramerate: 45,
maxBitrate: 9_500_000,
degradationPreference: "balanced",
});
expect(videoTrack.applyConstraints).toHaveBeenCalledWith({
width: { max: 1280 },
height: { max: 720 },
frameRate: { ideal: 45, max: 45 },
});
await applyVideoCaptureProfile(videoTrack, screenVideoCeiling(QUALITY_PROFILES["720p30"]));
expect(videoTrack.applyConstraints).toHaveBeenLastCalledWith({
width: { max: 1280 }, height: { max: 720 }, frameRate: { ideal: 30, max: 30 },
});
});
it("compares deserialized size values without treating audio edits as new video", () => {
const profile = QUALITY_PROFILES["1080p30"];
const restored = JSON.parse(JSON.stringify(profile));
expect(videoQualitySettingsEqual(profile, { ...restored, maxAudioBitrate: 256_000 })).toBe(true);
expect(videoQualitySettingsEqual(profile, { ...restored, resolution: { width: 1920, height: 1200 } })).toBe(false);
});
it("leaves capture constraints with the owner of a remote track", async () => {
const { videoTrack } = createVideoStream();
videoTrack.getCapabilities = vi.fn(() => ({}));
await applyVideoCaptureProfile(videoTrack, QUALITY_PROFILES["1080p30"]);
expect(videoTrack.applyConstraints).not.toHaveBeenCalled();
});
it("defaults every recommended profile to balanced", () => {
expect(QUALITY_PROFILES["1080p30"].degradationPreference).toBe("balanced");
expect(QUALITY_PROFILES["1080p60"].degradationPreference).toBe("balanced");
expect(QUALITY_PROFILES["720p30"].degradationPreference).toBe("balanced");
});
it("holds resolution only while a scaling preference starts", () => {
expect(needsStartupVideoProfile(QUALITY_PROFILES["1080p30"])).toBe(true);
const clarity = {
...QUALITY_PROFILES["1080p30"],
degradationPreference: "maintain-resolution",
} as const;
expect(needsStartupVideoProfile(clarity)).toBe(false);
});
it("asks for screen content only when a display share keeps clarity", () => {
expect(screenContentHint("maintain-resolution", true)).toBe("detail");
expect(screenContentHint("balanced", true)).toBe("motion");
expect(screenContentHint("maintain-framerate", true)).toBe("motion");
expect(screenContentHint(undefined, true)).toBe("motion");
for (const preference of ["maintain-resolution", "balanced", "maintain-framerate", undefined] as const) {
expect(screenContentHint(preference, false)).toBe("motion");
}
});
it("sets the sender hint from the stored preference through edits, rollback and start-up", async () => {
const display = { displaySurface: "monitor", width: 1920, height: 1080 };
let applied = { encodings: [{}] } as RTCRtpSendParameters;
const track = { contentHint: "", getSettings: () => display } as unknown as MediaStreamTrack;
const sender = {
track,
getParameters: () => applied,
setParameters: async (parameters: RTCRtpSendParameters) => { applied = parameters; },
} as unknown as RTCRtpSender;
const clarity = { ...QUALITY_PROFILES["1080p30"], degradationPreference: "maintain-resolution", displaySource: true } as const;
const fluency = { ...QUALITY_PROFILES["1080p30"], degradationPreference: "maintain-framerate" } as const;
await configureVideoSender(sender, clarity);
expect([track.contentHint, applied.degradationPreference]).toEqual(["detail", "maintain-resolution"]);
await configureVideoSender(sender, fluency);
expect([track.contentHint, applied.degradationPreference]).toEqual(["motion", "maintain-framerate"]);
// A refused edit reapplies the previous profile through the same owner.
await configureVideoSender(sender, clarity);
expect([track.contentHint, applied.degradationPreference]).toEqual(["detail", "maintain-resolution"]);
// Start-up holds resolution on the sender only; the hint keeps Balanced.
await configureVideoSender(sender, QUALITY_PROFILES["1080p30"], undefined, true);
expect([track.contentHint, applied.degradationPreference]).toEqual(["motion", "maintain-resolution"]);
await configureVideoSender(sender, QUALITY_PROFILES["1080p30"]);
expect([track.contentHint, applied.degradationPreference]).toEqual(["motion", "balanced"]);
// The carrier's hint belongs to transport.
track.contentHint = "detail";
await configureVideoSender(sender, fluency, 1);
expect(track.contentHint).toBe("detail");
const camera = { contentHint: "motion", getSettings: () => ({ width: 1280, height: 720 }) } as unknown as MediaStreamTrack;
await configureVideoSender({ ...sender, track: camera } as unknown as RTCRtpSender, { ...clarity, displaySource: false });
expect(camera.contentHint).toBe("motion");
});
it("keeps audio selection orthogonal while defaulting missing settings to music", () => {
const saver = {
...QUALITY_PROFILES["1080p60"],
maxAudioBitrate: 64000,
} as const;
expect(matchingQualityProfileId(saver)).toBe("1080p60");
expect(screenAudioBitrate(undefined))
.toBe(screenAudioBitrate(QUALITY_PROFILES["1080p60"].maxAudioBitrate));
});
it("reads back every requested sender control after setParameters", async () => {
let applied = { encodings: [] } as unknown as RTCRtpSendParameters;
const setParameters = vi.fn(async (parameters: RTCRtpSendParameters) => {
applied = parameters;
});
const sender = {
track: {
getSettings: () => ({ width: 2560, height: 1440 }),
},
getParameters: () => applied,
setParameters,
} as unknown as RTCRtpSender;
const readback = await configureVideoSender(
sender,
QUALITY_PROFILES["1080p60"],
);
expect(readback).toEqual({
requested: {
maxBitrate: 8_000_000,
maxFramerate: 60,
scaleResolutionDownBy: 4 / 3,
degradationPreference: "balanced",
scalabilityMode: null,
},
applied: {
maxBitrate: 8_000_000,
maxFramerate: 60,
scaleResolutionDownBy: 4 / 3,
degradationPreference: "balanced",
scalabilityMode: null,
},
mismatches: [],
});
expect(setParameters).toHaveBeenCalledOnce();
});
it("updates both supplied simulcast ceilings without changing their scale or identity", async () => {
let applied = {
encodings: [
{
rid: "q",
maxBitrate: 1_250_000,
maxFramerate: 30,
scaleResolutionDownBy: 2,
},
{
rid: "h",
maxBitrate: 5_000_000,
maxFramerate: 30,
scaleResolutionDownBy: 1,
},
],
} as unknown as RTCRtpSendParameters;
const sender = {
track: {
getSettings: () => ({ width: 1920, height: 1080 }),
},
getParameters: () => applied,
setParameters: vi.fn(async (parameters: RTCRtpSendParameters) => {
applied = parameters;
}),
} as unknown as RTCRtpSender;
await expect(
configureVideoSender(sender, QUALITY_PROFILES["1080p60"]),
).resolves.toMatchObject({
requested: { maxBitrate: 8_000_000, maxFramerate: 60 },
applied: { maxBitrate: 8_000_000, maxFramerate: 60 },
mismatches: [],
});
expect(applied.encodings).toEqual([
{
rid: "q",
maxBitrate: 2_000_000,
maxFramerate: 60,
scaleResolutionDownBy: 2,
},
{
rid: "h",
maxBitrate: 8_000_000,
maxFramerate: 60,
scaleResolutionDownBy: 1,
},
]);
});
it("leaves picture cadence with the producer and restores ordinary sender adaptation", async () => {
let applied = { encodings: [{ maxFramerate: 30 }] } as RTCRtpSendParameters;
const sender = {
getParameters: () => applied,
setParameters: async (parameters: RTCRtpSendParameters) => { applied = parameters; },
} as unknown as RTCRtpSender;
const carrier = await configureVideoSender(sender, QUALITY_PROFILES["1080p60"], 1);
expect(applied.encodings[0]).not.toHaveProperty("maxFramerate");
expect(carrier).toMatchObject({ requested: { maxFramerate: null, maxBitrate: 8_000_000,
degradationPreference: "maintain-framerate" }, mismatches: [] });
const ceiling = screenVideoCeiling(QUALITY_PROFILES["1080p60"]);
for (const carrierScale of [1, undefined]) {
await configureVideoSender(sender, QUALITY_PROFILES["1080p60"], carrierScale, true);
const restored = await configureVideoSender(sender, ceiling);
expect(applied).not.toHaveProperty("degradationPreference");
expect(applied.encodings[0]!.maxFramerate).toBe(ceiling.maxFramerate);
expect(restored).toMatchObject({ requested: { degradationPreference: null },
applied: { degradationPreference: null }, mismatches: [] });
}
for (const id of ["720p30", "1080p60"] as const) {
const readback = await configureVideoSender(sender, QUALITY_PROFILES[id]);
expect(applied.encodings[0]!.maxFramerate).toBe(QUALITY_PROFILES[id].maxFramerate);
expect(applied.degradationPreference).toBe(QUALITY_PROFILES[id].degradationPreference);
expect(readback.mismatches).toEqual([]);
}
});
it("does not scale a remote source that already owns the room profile", async () => {
let applied = { encodings: [{}] } as unknown as RTCRtpSendParameters;
const sender = {
track: {
getCapabilities: () => ({}),
getSettings: () => ({ width: 2560, height: 1440 }),
},
getParameters: () => applied,
setParameters: vi.fn(async (parameters: RTCRtpSendParameters) => {
applied = parameters;
}),
} as unknown as RTCRtpSender;
const readback = await configureVideoSender(
sender,
QUALITY_PROFILES["720p30"],
);
expect(readback.requested.scaleResolutionDownBy).toBe(1);
expect(applied.encodings[0]?.scaleResolutionDownBy).toBe(1);
});
it.each([
[undefined, 128_000],
[64_000, 64_000],
[128_000, 128_000],
[192_000, 192_000],
] as const)("applies and reads back the %s audio ceiling", async (quality, bitrate) => {
let applied = { encodings: [] } as unknown as RTCRtpSendParameters;
const sender = {
getParameters: () => applied,
setParameters: vi.fn(async (parameters: RTCRtpSendParameters) => {
applied = parameters;
}),
} as unknown as RTCRtpSender;
await expect(configureAudioSender(sender, screenAudioBitrate(quality))).resolves.toEqual({
requestedMaxBitrate: bitrate,
appliedMaxBitrate: bitrate,
mismatch: false,
});
expect(applied.encodings).toEqual([{ maxBitrate: bitrate }]);
});
it.each([96_000, 256_000, 320_000])("applies a numeric %i audio ceiling without a preset", async (bitrate) => {
let applied = { encodings: [] } as unknown as RTCRtpSendParameters;
const sender = {
getParameters: () => applied,
setParameters: vi.fn(async (parameters: RTCRtpSendParameters) => {
applied = parameters;
}),
} as unknown as RTCRtpSender;
await expect(configureAudioSender(sender, bitrate)).resolves.toEqual({
requestedMaxBitrate: bitrate,
appliedMaxBitrate: bitrate,
mismatch: false,
});
});
it.each([192_000, 510_000])("honors the receiver's %i Opus limit on every live update", async (limit) => {
let applied = {
codecs: [{ mimeType: "audio/opus", sdpFmtpLine: `stereo=1;maxaveragebitrate=${limit}` }],
encodings: [{}],
} as RTCRtpSendParameters;
const sender = {
getParameters: () => applied,
setParameters: async (value: RTCRtpSendParameters) => { applied = value; },
} as RTCRtpSender;
for (const bitrate of [128_000, 320_000, 96_000, 510_000]) {
const ceiling = Math.min(bitrate, limit, 320_000);
await expect(configureAudioSender(sender, bitrate)).resolves.toEqual({
requestedMaxBitrate: bitrate, appliedMaxBitrate: ceiling, mismatch: ceiling !== bitrate,
});
expect(applied.encodings[0]?.maxBitrate).toBe(ceiling);
}
});
it.each([
["", 32_000],
["stereo=1", 64_000],
["stereo=1;maxaveragebitrate=invalid", 64_000],
["stereo=1;maxaveragebitrate=510001", 64_000],
["maxplaybackrate=8000", 12_000],
["stereo=1;maxplaybackrate=16000", 40_000],
["maxaveragebitrate=510000;maxaveragebitrate=96000", 96_000],
] as const)("keeps omitted/invalid Opus limits bounded: %s", (sdpFmtpLine, expected) => {
const parameters = { codecs: [{ mimeType: "audio/opus", sdpFmtpLine }] } as RTCRtpSendParameters;
expect(audioSenderCeiling(parameters, 320_000)).toBe(expected);
expect(audioSenderCeiling(parameters, 6_000)).toBe(6_000);
});
it("reports an audio ceiling the browser rewrites", async () => {
const sender = {
getParameters: vi
.fn<() => RTCRtpSendParameters>()
.mockReturnValueOnce({ encodings: [{}] } as RTCRtpSendParameters)
.mockReturnValue({
encodings: [{ maxBitrate: 96_000 }],
} as RTCRtpSendParameters),
setParameters: vi.fn(async () => undefined),
} as unknown as RTCRtpSender;
const readback = await configureAudioSender(sender, 128_000);
expect(readback).toEqual({
requestedMaxBitrate: 128_000,
appliedMaxBitrate: 96_000,
mismatch: true,
});
});
it("does not mutate prior audio parameters when setParameters fails", async () => {
const applied = {
encodings: [{ maxBitrate: 128_000 }],
} as RTCRtpSendParameters;
const sender = {
getParameters: () => applied,
setParameters: vi.fn(async () => {
throw new Error("rejected");
}),
} as unknown as RTCRtpSender;
await expect(
configureAudioSender(sender, 192_000),
).rejects.toThrow("rejected");
expect(applied.encodings[0]?.maxBitrate).toBe(128_000);
});
it("reports fields the browser does not retain", async () => {
const before = { encodings: [{}] } as RTCRtpSendParameters;
const after = {
encodings: [{ maxBitrate: 8_000_000 }],
degradationPreference: "maintain-resolution",
} as unknown as RTCRtpSendParameters;
const getParameters = vi
.fn<() => RTCRtpSendParameters>()
.mockReturnValueOnce(before)
.mockReturnValue(after);
const sender = {
track: { getSettings: () => ({ width: 1920, height: 1080 }) },
getParameters,
setParameters: vi.fn(async () => undefined),
} as unknown as RTCRtpSender;
const readback = await configureVideoSender(
sender,
QUALITY_PROFILES["1080p60"],
);
expect(readback.mismatches).toEqual([
"maxFramerate",
"scaleResolutionDownBy",
"degradationPreference",
]);
});
it("reports an applied default scalability mode without claiming a request", async () => {
const before = {
encodings: [{}],
} as unknown as RTCRtpSendParameters;
const after = {
encodings: [
{
maxBitrate: 8_000_000,
maxFramerate: 60,
scaleResolutionDownBy: 1,
scalabilityMode: "L1T2",
},
],
degradationPreference: "balanced",
} as unknown as RTCRtpSendParameters;
const sender = {
track: { getSettings: () => ({ width: 1920, height: 1080 }) },
getParameters: vi
.fn<() => RTCRtpSendParameters>()
.mockReturnValueOnce(before)
.mockReturnValue(after),
setParameters: vi.fn(async () => undefined),
} as unknown as RTCRtpSender;
const readback = await configureVideoSender(
sender,
QUALITY_PROFILES["1080p60"],
);
expect(readback.requested.scalabilityMode).toBeNull();
expect(readback.applied.scalabilityMode).toBe("L1T2");
expect(readback.mismatches).not.toContain("scalabilityMode");
});
it("keeps scalability mode unknown when sender encodings are ambiguous", async () => {
const before = { encodings: [{}] } as RTCRtpSendParameters;
const after = {
encodings: [
{
rid: "low",
maxBitrate: 8_000_000,
maxFramerate: 60,
scaleResolutionDownBy: 1,
scalabilityMode: "L1T1",
},
{ rid: "high", scalabilityMode: "L1T2" },
],
degradationPreference: "balanced",
} as unknown as RTCRtpSendParameters;
const sender = {
track: { getSettings: () => ({ width: 1920, height: 1080 }) },
getParameters: vi
.fn<() => RTCRtpSendParameters>()
.mockReturnValueOnce(before)
.mockReturnValue(after),
setParameters: vi.fn(async () => undefined),
} as unknown as RTCRtpSender;
const readback = await configureVideoSender(
sender,
QUALITY_PROFILES["1080p60"],
);
expect(readback.applied.scalabilityMode).toBeNull();
expect(readback.mismatches).not.toContain("scalabilityMode");
});
it("pauses and resumes the shared video and audio tracks together", () => {
const { stream, videoTrack, audioTrack } = createVideoStream();
expect(setMediaPaused(stream, true)).toBe(true);
expect(videoTrack.enabled).toBe(false);
expect(audioTrack.enabled).toBe(false);
expect(setMediaPaused(stream, false)).toBe(true);
expect(videoTrack.enabled).toBe(true);
expect(audioTrack.enabled).toBe(true);
});
});