import { execFileSync } from "node:child_process"; import { mkdtempSync, rmSync } from "node:fs"; import { constants, getPriority, tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { getFfmpegBinary } from "../utils/ffmpegBinaries.js"; import { lowerPriority, spawnStreamingEncoder } from "./streamingEncoder.js"; const os = vi.hoisted(() => ({ setPriority: vi.fn() })); vi.mock("os", async (importOriginal) => { const real = await importOriginal(); os.setPriority.mockImplementation(real.setPriority); return { ...real, setPriority: os.setPriority }; }); const dirs: string[] = []; beforeEach(() => { os.setPriority.mockClear(); }); afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function jpegFrame(): Buffer { return execFileSync(getFfmpegBinary(), [ "-v", "error", "-f", "lavfi", "-i", "testsrc2=s=64x64:r=30", "-frames:v", "1", "-f", "image2pipe", "-c:v", "mjpeg", "pipe:1", ]); } async function encodeThreeFrames(belowNormalPriority?: boolean, whileRunning = () => {}) { const dir = mkdtempSync(join(tmpdir(), "hf-encoder-priority-")); dirs.push(dir); const encoder = await spawnStreamingEncoder(join(dir, "out.mp4"), { fps: { num: 30, den: 1 }, width: 64, height: 64, codec: "h264", preset: "ultrafast", quality: 23, pixelFormat: "yuv420p", imageFormat: "jpeg", belowNormalPriority, }); const frame = jpegFrame(); for (let i = 0; i < 3; i++) await encoder.writeFrame(frame); whileRunning(); return encoder.close(); } describe("streaming encoder priority", () => { it("goes one step below normal, below below-normal, and stays at the lowest", () => { const { PRIORITY_HIGH, PRIORITY_NORMAL, PRIORITY_BELOW_NORMAL, PRIORITY_LOW } = constants.priority; expect(lowerPriority(PRIORITY_HIGH)).toBe(PRIORITY_BELOW_NORMAL); expect(lowerPriority(PRIORITY_NORMAL)).toBe(PRIORITY_BELOW_NORMAL); expect(lowerPriority(5)).toBe(PRIORITY_BELOW_NORMAL); expect(lowerPriority(PRIORITY_BELOW_NORMAL)).toBe(PRIORITY_LOW); expect(lowerPriority(15)).toBe(PRIORITY_LOW); expect(lowerPriority(PRIORITY_LOW)).toBe(PRIORITY_LOW); }); // The runner's own priority is the baseline, so a runner that already runs below normal still checks the step. it.runIf(getPriority() < constants.priority.PRIORITY_LOW)( "runs its ffmpeg child below the priority it inherits", async () => { const parent = getPriority(); let child: number | undefined; let priority: number | undefined; const result = await encodeThreeFrames(true, () => { child = os.setPriority.mock.calls[0]?.[0]; if (child !== undefined) priority = getPriority(child); }); expect(result.success).toBe(true); expect(child).toBeTypeOf("number"); expect(child).not.toBe(process.pid); expect(priority).toBe(lowerPriority(parent)); expect(priority).toBeGreaterThan(parent); }, ); it("leaves ffmpeg at normal priority by default", async () => { expect((await encodeThreeFrames()).success).toBe(true); expect(os.setPriority).not.toHaveBeenCalled(); }); it("still encodes when the system refuses the lower priority", async () => { os.setPriority.mockImplementationOnce(() => { throw Object.assign(new Error("setpriority EPERM"), { code: "EPERM" }); }); expect((await encodeThreeFrames(true)).success).toBe(true); }); });