Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

5.6 KB
Raw Blame
// The numbers below are the contract with the Rust exporter, not a snapshot of this file's
// arithmetic. `speed_segments_match_the_exporter_frame_totals` in
// `crates/compositor/src/regions.rs` asserts the SAME table against
// `speed_segments_for_window`, which is what `walk_composited_timeline` iterates. Change one
// side and the other goes red — which is the point: a silent divergence here is a progress
// bar that lies, and it has already shipped once (OpenScreen#371, the "frozen at ~80%").

import { describe, expect, it } from "vitest";
import { clipOutputFrameCount, outputFrameCount } from "./outputFrameCount";

const FPS = 30;

describe("outputFrameCount", () => {
	it("counts a clip with no speed region at its plain duration", () => {
		expect(clipOutputFrameCount({ sourceStartSec: 0, sourceEndSec: 10 }, [], FPS)).toBe(300);
	});

	it("is the whole bug: a 1.25x clip emits 80% of the frames its duration suggests", () => {
		// 10 s at 30 fps looks like 300 frames and renders 240. The old total was the former,
		// so the bar stopped at exactly 80% and the export finished there.
		const naive = Math.round(10 * FPS);
		const real = clipOutputFrameCount(
			{ sourceStartSec: 0, sourceEndSec: 10 },
			[{ startSec: 0, endSec: 10, speed: 1.25 }],
			FPS,
		);
		expect(real).toBe(240);
		expect(real / naive).toBeCloseTo(0.8, 5);
	});

	it("counts a slow-motion clip above its duration", () => {
		// The other half of the same bug: a 0.5x region pins the bar at 100% for the second
		// half of the export instead of stopping short.
		expect(
			clipOutputFrameCount(
				{ sourceStartSec: 0, sourceEndSec: 10 },
				[{ startSec: 0, endSec: 10, speed: 0.5 }],
				FPS,
			),
		).toBe(600);
	});

	it("splits a partially covered clip into 1x and sped spans", () => {
		expect(
			clipOutputFrameCount(
				{ sourceStartSec: 0, sourceEndSec: 10 },
				[{ startSec: 2, endSec: 4, speed: 2 }],
				FPS,
			),
		).toBe(60 + 30 + 180);
	});

	it("clamps a region that runs past the trimmed window", () => {
		expect(
			clipOutputFrameCount(
				{ sourceStartSec: 1, sourceEndSec: 5 },
				[{ startSec: 0, endSec: 100, speed: 2 }],
				FPS,
			),
		).toBe(60);
	});

	it("does not double-count a clip for an empty speed region at its trimmed edge (#876)", () => {
		const clips = [
			{ sourceStartSec: 0, sourceEndSec: 44.81830642526596 },
			{ sourceStartSec: 0, sourceEndSec: 47.833333 },
		];
		const regions = [
			{ startSec: 44.818, endSec: 44.818, speed: 1.5, clipIndex: 0 },
			{ startSec: 0, endSec: 1.87, speed: 1.5, clipIndex: 1 },
		];

		// The first region is empty after trim projection. Before the fix it caused the first
		// clip's normal-speed prefix to be counted twice, yielding the reported 8,213 frames.
		// 5,521.7 programme frames.
		expect(outputFrameCount(clips, regions, 60)).toBe(5_522);
	});

	it("never renders the same source time twice when two regions overlap", () => {
		// A stale payload can overlap; the first region keeps the covered portion, matching
		// `speed_segments_for_window`'s cursor.
		expect(
			clipOutputFrameCount(
				{ sourceStartSec: 0, sourceEndSec: 10 },
				[
					{ startSec: 2, endSec: 6, speed: 2 },
					{ startSec: 4, endSec: 8, speed: 4 },
				],
				FPS,
			),
		).toBe(60 + 60 + 15 + 60);
	});

	it("ignores a region belonging to another clip", () => {
		const clips = [
			{ sourceStartSec: 0, sourceEndSec: 10 },
			{ sourceStartSec: 0, sourceEndSec: 10 },
		];
		const regions = [{ startSec: 0, endSec: 10, speed: 2, clipIndex: 1 }];
		expect(outputFrameCount(clips, regions, FPS)).toBe(300 + 150);
	});

	it("applies a region with no clipIndex to every clip", () => {
		const clips = [
			{ sourceStartSec: 0, sourceEndSec: 10 },
			{ sourceStartSec: 0, sourceEndSec: 10 },
		];
		expect(outputFrameCount(clips, [{ startSec: 0, endSec: 10, speed: 2 }], FPS)).toBe(150 + 150);
	});

	it("treats a non-positive speed as 1x rather than dividing by it", () => {
		expect(
			clipOutputFrameCount(
				{ sourceStartSec: 0, sourceEndSec: 10 },
				[{ startSec: 0, endSec: 10, speed: 0 }],
				FPS,
			),
		).toBe(300);
	});

	it("lands a cut-up timeline on its programme duration (v2.1.0 QA project)", () => {
		// Two clips of one file, six trims, a 1.5x and a 4x region per clip: 2,444.86
		// programme frames. Rounding each of the 14 spans up used to make it 2,452.
		const end = 48.789333;
		const clips = [
			[0, 10.614275104787234],
			[13.105645300531915, 14.88],
			[16.03, 21.54],
			[21.85, 24.83],
			[26.53, 43.806592608510634],
			[46.34986635, end],
			[0, 10.614275104787234],
			[13.105645300531915, end],
		].map(([sourceStartSec, sourceEndSec]) => ({ sourceStartSec, sourceEndSec }));
		const regions = [
			{ startSec: 33.711, endSec: 36.202, speed: 1.5 },
			{ startSec: 36.202, endSec: 38.693, speed: 4 },
		];
		expect(outputFrameCount(clips, regions, FPS)).toBe(2_445);
	});

	it("keeps the error under a frame whatever the span count", () => {
		// 200 windows of 0.35 s, each half at 1.5x: 1,750 programme frames. Per-span
		// rounding made it 2,000.
		const clips = Array.from({ length: 200 }, (_, i) => ({
			sourceStartSec: i,
			sourceEndSec: i + 0.35,
		}));
		const regions = clips.map(({ sourceStartSec }) => ({
			startSec: sourceStartSec + 0.1,
			endSec: sourceStartSec + 0.275,
			speed: 1.5,
		}));
		expect(outputFrameCount(clips, regions, FPS)).toBe(1_750);
	});

	it("never returns a total the callers would divide by zero", () => {
		expect(outputFrameCount([], [], FPS)).toBe(1);
		expect(outputFrameCount([{ sourceStartSec: 4, sourceEndSec: 4 }], [], FPS)).toBe(1);
		expect(outputFrameCount([{ sourceStartSec: 0, sourceEndSec: 10 }], [], 0)).toBe(1);
	});
});