Files
piik/tests/sender-video-track.test.ts
T
AAA ade65b94d8 fix(media): map the picture preference to a screen share's content hint
Every Browser screen share was hinted "motion", which turns off Chromium's
screen-content encoding, so text could be downscaled to keep motion even
when the person chose Clarity. The stored picture preference already
states that intent, so one pure function, screenContentHint, now maps it:
Clarity gives "detail" on a display surface; Balanced, Fluency, cameras
and received tracks keep "motion". Balanced stays "motion" because
Chromium turns an explicit "balanced" into maintain-resolution under
"detail", which would collapse three choices into two.

The function is the single owner of the hint. Display capture no longer
sets one. cloneSenderVideoTrack takes the hint instead of falling back to
"motion" and still copies it to the frame-forwarding track, so the first
configure need not rebuild the codec. configureVideoSender sets the hint
from the profile whenever no carrier is active, so a live edit and its
rollback restore it; the pool carrier keeps its own transport hint. The
pool producer clones with the mapped hint. Start-up protection becomes a
configureVideoSender flag that overrides only the sender's degradation
preference, replacing startupVideoProfile, so it cannot reach the hint.
No setting, default or per-choice degradation preference changes. The
media-quality standard now states the mapping.

Tests whose expectation changed: quality "bounds initial capture to the
selected profile" (capture leaves the video hint empty), "holds resolution
only while a scaling preference starts" and "leaves picture cadence with
the producer..." (startupVideoProfile removed; the latter uses the flag).
The sender-video-track clone helper passes a hint for the new signature.
New tests cover the mapping, a clone, a live edit, a rollback, start-up
and a camera at the quality, clone, producer and transport boundaries.

Verified: npx tsc -b --pretty false; npx vitest run (159 files, 2355
tests). Encoder behaviour under each hint still needs a lab measurement.
2026-11-25 13:38:47 +08:00

165 lines
8.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { cloneSenderVideoTrack, senderCaptureTrack, stopSenderVideoTrack } from "../src/client/media/sender-video-track";
import { applyVideoCaptureProfile, configureVideoSender, QUALITY_PROFILES } from "../src/client/media/quality";
import { captureMetrics } from "../src/client/webrtc/stats";
class Track extends EventTarget {
readonly kind = "video";
enabled = true;
contentHint = "motion";
readyState = "live";
settings: MediaTrackSettings = { displaySurface: "browser", width: 1920, height: 1080 };
readonly stop = vi.fn(() => { this.readyState = "ended"; });
readonly applyConstraints = vi.fn(async (_constraints: MediaTrackConstraints) => undefined);
getSettings() { return this.settings; }
getConstraints() { return { width: { max: 1920 }, height: { max: 1080 }, frameRate: { ideal: 30, max: 30 } }; }
getCapabilities() { return { width: { min: 1, max: 1920 } }; }
clone = vi.fn(() => {
const clone = new Track();
clone.settings = { ...this.settings };
clone.enabled = this.enabled;
return clone;
});
}
const asTrack = (track: Track) => track as unknown as MediaStreamTrack;
const flush = async () => { for (let i = 0; i < 20; i++) await Promise.resolve(); };
const owned: MediaStreamTrack[] = [];
afterEach(async () => {
owned.splice(0).forEach(stopSenderVideoTrack);
await flush();
vi.unstubAllGlobals();
});
function platform() {
const cancel = vi.fn(), write = vi.fn();
let frames!: ReadableStreamDefaultController<VideoFrame>;
const processor = vi.fn(function () {
return { readable: new ReadableStream<VideoFrame>({ start(c) { frames = c; }, cancel }) };
});
class Generator extends Track {
static instances: Generator[] = [];
override settings = {};
writable = new WritableStream<VideoFrame>({ write(frame) { write(frame); frame.close(); } });
constructor() { super(); Generator.instances.push(this); }
}
vi.stubGlobal("MediaStreamTrackProcessor", processor);
vi.stubGlobal("MediaStreamTrackGenerator", Generator);
return { cancel, write, processor, frames: () => frames, Generator };
}
function clone(source: Track, onEnded = vi.fn(), hint = "motion") {
const track = cloneSenderVideoTrack(asTrack(source), hint, onEnded);owned.push(track);return track;
}
describe("sender display-frame ownership", () => {
it("cancels the processor when a sender retires during initial capture constraints", async () => {
const p = platform(), source = new Track(), input = new Track(), ended = vi.fn();
let finish!: () => void;
input.applyConstraints.mockImplementationOnce(() => new Promise<undefined>(resolve => { finish = () => resolve(undefined); }));
source.clone.mockReturnValue(input);
const track = clone(source, ended);
try {
stopSenderVideoTrack(track);
await flush();
expect(p.cancel).toHaveBeenCalledOnce();
expect(source.readyState).toBe("live");
} finally { finish(); await flush(); }
expect(p.write).not.toHaveBeenCalled();
expect(ended).not.toHaveBeenCalled();
});
it("cancels a quiet input on retirement without stopping the shared source", async () => {
const p = platform(), source = new Track();
source.enabled = false;
const ended = vi.fn(), track = clone(source, ended), input = source.clone.mock.results[0]!.value;
expect(track.enabled).toBe(false);
expect(input.enabled).toBe(true);
expect(senderCaptureTrack(track)).toBe(input);
expect(p.processor).toHaveBeenCalledWith({ track: input, maxBufferSize: 1 });
expect(input.applyConstraints).toHaveBeenCalledWith(expect.objectContaining({ frameRate: { min: 1, ideal: 30, max: 30 } }));
stopSenderVideoTrack(track);
expect(input.readyState).toBe("ended");
expect(track.readyState).toBe("ended");
await flush();
expect(p.cancel).toHaveBeenCalledOnce();
expect(source.readyState).toBe("live");
expect(source.stop).not.toHaveBeenCalled();
expect(ended).not.toHaveBeenCalled();
});
it("forwards frames unchanged and leaves profile constraints with the capture input", async () => {
const p = platform(), source = new Track(), track = clone(source);
const input = source.clone.mock.results[0]!.value;
const frame = { close: vi.fn(), timestamp: 123, displayWidth: 1920, displayHeight: 1080 } as unknown as VideoFrame;
p.frames().enqueue(frame);
await flush();
expect(p.write).toHaveBeenCalledWith(frame);
expect(frame.close).toHaveBeenCalledOnce();
await applyVideoCaptureProfile(track, QUALITY_PROFILES["720p30"]);
expect(input.applyConstraints).toHaveBeenCalledWith(expect.objectContaining({ width: { max: 1280 } }));
expect(input.applyConstraints).toHaveBeenLastCalledWith(expect.objectContaining({ frameRate: { min: 1, ideal: 30, max: 30 } }));
expect((track as unknown as Track).applyConstraints).not.toHaveBeenCalled();
// Generated-track settings can lag by a frame. Do not apply the profile's
// scale twice after the input accepted a smaller capture size.
input.settings = { displaySurface: "browser", width: 1280, height: 720 };
(track as unknown as Track).settings = { width: 1920, height: 1080 };
expect(captureMetrics(track)).toEqual({ captureWidth: 1280, captureHeight: 720, captureFramesPerSecond: null });
let parameters = { encodings: [{}] } as RTCRtpSendParameters;
const sender = { track, getParameters: () => parameters, setParameters: async (next: RTCRtpSendParameters) => { parameters = next; } };
await configureVideoSender(sender as unknown as RTCRtpSender, QUALITY_PROFILES["720p30"]);
expect(parameters.encodings[0]!.scaleResolutionDownBy).toBe(1);
});
it.each(["ended", "failed"])("retires the generated output when its input %s", async (end) => {
const p = platform(), source = new Track(), ended = vi.fn(), track = clone(source, ended);
if (end === "ended") p.frames().close();
else p.frames().error(new Error("capture ended unexpectedly"));
await flush();
expect(track.readyState).toBe("ended");
expect(source.clone.mock.results[0]!.value.readyState).toBe("ended");
expect(source.readyState).toBe("live");
expect(ended).toHaveBeenCalledExactlyOnceWith(track);
});
it("gives the supplied hint to the clone and its forwarded frames", () => {
platform();
const source = new Track(), track = clone(source, vi.fn(), "detail");
expect(source.clone.mock.results[0]!.value.contentHint).toBe("detail");
expect(track.contentHint).toBe("detail");
expect(source.contentHint).toBe("motion");
vi.stubGlobal("MediaStreamTrackProcessor", undefined);
const empty = new Track();
empty.contentHint = "";
expect(clone(empty, vi.fn(), "detail").contentHint).toBe("detail");
});
it("keeps camera and received tracks on the ordinary clone path", () => {
const p = platform(), source = new Track();source.settings = { width: 640, height: 480 };
expect(clone(source)).toBe(source.clone.mock.results[0]!.value);
expect(p.processor).not.toHaveBeenCalled();
});
it("retires both ends if idle-refresh configuration fails before piping", async () => {
const p = platform(), source = new Track(), input = new Track(), ended = vi.fn();
source.clone.mockReturnValue(input);
input.applyConstraints.mockRejectedValueOnce(new Error("constraints rejected"));
const track = clone(source, ended);
await flush();
expect(p.cancel).toHaveBeenCalledOnce();
expect(ended).toHaveBeenCalledExactlyOnceWith(track);
expect(input.readyState).toBe("ended");
expect(track.readyState).toBe("ended");
expect(source.readyState).toBe("live");
});
it("retains the ordinary clone if frame APIs are unavailable or construction fails", () => {
vi.stubGlobal("MediaStreamTrackProcessor", undefined);
const source = new Track();expect(clone(source)).toBe(source.clone.mock.results[0]!.value);
const p = platform();
vi.stubGlobal("MediaStreamTrackProcessor", class { constructor() { throw new Error("unavailable"); } });
const other = new Track();const track = clone(other);
expect(track).toBe(other.clone.mock.results[0]!.value);
expect(track.readyState).toBe("live");
expect(p.Generator.instances[0]!.readyState).toBe("ended");
});
});