import { afterEach, expect, it, vi } from "vitest"; import { normalizeVideoOfferSdp, withoutVideoOrientation } from "../src/client/webrtc/video-codec"; import { preferredVideoCodecForTrack } from "../src/client/webrtc/video-codec-preflight"; afterEach(() => { vi.unstubAllGlobals(); vi.useRealTimers(); }); it.each(["deadline", "cancel"] as const)("releases the probe clone and both PCs when setup never completes (%s)", async reason => { vi.useFakeTimers(); const connections: { close: ReturnType }[] = []; const stop = vi.fn(); vi.stubGlobal("RTCRtpSender", { getCapabilities: () => ({ codecs: [{ mimeType: "video/H264", sdpFmtpLine: "packetization-mode=1" }] }) }); vi.stubGlobal("RTCPeerConnection", class { close = vi.fn(); constructor() { connections.push(this); } addTransceiver() { return { setCodecPreferences() {}, sender: {} }; } createOffer() { return new Promise(() => {}); } }); const track = { kind: "video", readyState: "live", clone: () => ({ stop }) } as unknown as MediaStreamTrack; const abort = new AbortController(); const selecting = preferredVideoCodecForTrack(track, abort.signal); const outcome = reason === "cancel" ? expect(selecting).rejects.toMatchObject({ name: "AbortError" }) : expect(selecting).resolves.toBe("vp8"); if (reason === "cancel") abort.abort(); else await vi.advanceTimersByTimeAsync(4000); await outcome; expect(connections).toHaveLength(2); for (const pc of connections) expect(pc.close).toHaveBeenCalledOnce(); expect(stop).toHaveBeenCalledOnce(); expect(track.readyState).toBe("live"); expect(vi.getTimerCount()).toBe(0); }); it("encodes the probe clone with the share's hint and leaves the source's alone", async () => { const clone = { contentHint: "", stop: vi.fn() }; vi.stubGlobal("RTCRtpSender", { getCapabilities: () => ({ codecs: [] }) }); vi.stubGlobal("RTCPeerConnection", class { close() {} addTransceiver() { return { setCodecPreferences() {}, sender: {} }; } }); const track = { kind: "video", readyState: "live", contentHint: "", clone: () => clone } as unknown as MediaStreamTrack; await expect(preferredVideoCodecForTrack(track, new AbortController().signal, "detail")).resolves.toBe("vp8"); expect(clone.contentHint).toBe("detail"); expect(track.contentHint).toBe(""); }); it.each(["\r\n", "\n"])("removes orphan codec attributes within each offered video section (%j)", newline => { const lines = [ "v=0", "a=x-session:keep", "m=video 9 UDP/TLS/RTP/SAVPF 120 124", "a=mid:0", "a=ice-ufrag:unchanged", "a=rtpmap:120 VP8/90000", "a=rtpmap:124 rtx/90000", "a=fmtp:124 apt=120", "a=rtcp-fb:120 nack", "a=rtcp-fb:* transport-cc", "a=x-video:keep", "a=fmtp:126 profile-level-id=42e01f", "a=rtcp-fb:126 nack pli", "m=video 9 UDP/TLS/RTP/SAVPF 126", "a=mid:1", "a=rtpmap:126 H264/90000", "a=fmtp:126 profile-level-id=42e01f;packetization-mode=1", "a=rtcp-fb:126 nack", "a=rtcp-fb:120 nack pli", "m=audio 9 UDP/TLS/RTP/SAVPF 0", "a=fmtp:0 keep-audio", "m=application 9 UDP/DTLS/SCTP webrtc-datachannel", "a=sctp-port:5000", "", ]; const omitted = new Set([11, 12, 18]); const expected = lines.filter((_line, index) => !omitted.has(index)).join(newline); expect(normalizeVideoOfferSdp(lines.join(newline))).toBe(expected); expect(normalizeVideoOfferSdp(expected)).toBe(expected); }); it("preserves valid codec parameters, feedback and opaque data without reserializing SDP", () => { const sdp = "v=0\r\nm=video 9 UDP/TLS/RTP/SAVPF 96 97\r\n" + "a=rtpmap:96 H264/90000\r\na=fmtp:96 profile-level-id=42e034;packetization-mode=1\r\n" + "a=rtpmap:97 rtx/90000\r\na=fmtp:97 apt=96\r\na=rtcp-fb:96 nack\r\n" + "a=rtcp-fb:* transport-cc\r\na=unknown:exact spacing and values\r\n"; expect(normalizeVideoOfferSdp(sdp)).toBe(sdp); }); it("declines CVO while retaining the other negotiated header extensions", () => { const section = "m=video 9 UDP/TLS/RTP/SAVPF 96\r\na=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid\r\n"; const cvo = "a=extmap:13/sendrecv urn:3gpp:video-orientation\r\n"; expect(normalizeVideoOfferSdp(section + cvo)).toBe(section + cvo); expect(withoutVideoOrientation(section + cvo)).toBe(section); });