Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test: verify single-download audio integration and document benchmarks

Richie McIlroyc476175633c5
diff --git a/apps/media-server/audio-quality-benchmarks.md b/apps/media-server/audio-quality-benchmarks.md
index 50983e17d..c8e31b93d 100644
--- a/apps/media-server/audio-quality-benchmarks.md
+++ b/apps/media-server/audio-quality-benchmarks.md
@@ -1,7 +1,7 @@
# Instant audio quality experiment
-The level-correction worker is connected to the desktop recording finalizer after
-the original recording is verified and published. The original output key and
+The level-correction worker reuses the local MP4 from the existing desktop Instant
+segment mux or Chrome/WebM conversion, after publishing the original recording. The original output key and
verification receipt remain intact. A separate, versioned MP4 becomes the playback
and download source only after local and remote-byte validation and an atomic check
that ownership, storage, source, and upload state have not changed.
@@ -10,8 +10,9 @@ The serving integration uses bounded, constant level correction. EQ and denoisin
remain experimental because consistent perceptual improvement has not been
established. No desktop capture change is required.
-Eligibility is limited to newly finalized, durably verified desktop MP4 recordings
-on S3-compatible storage, at most 15 minutes and 256 MiB. All existing audio-policy
+Eligibility is limited to newly finalized, durably verified desktop Instant segment
+recordings and completed browser conversions on S3-compatible storage, at most
+15 minutes and 256 MiB. All existing audio-policy
gates still apply. Google Drive, legacy recordings without an immutable output,
unsupported audio, and larger recordings retain their original audio. There is no
historical backfill. Only one enhancement runs per media-server replica, it requires
@@ -19,8 +20,22 @@ spare processing capacity, and the entire request has a two-minute deadline.
Capacity, timeout, transfer, or validation failures retain the original. The step
does not retry capacity failures or delay original publication.
-Rollback to the previous web deployment selects the retained original output;
-the original recording verification target is never redirected to a derivative.
+There is no second download of the source and no second video encode. Correction
+reads the local completed MP4, copies video packets, and encodes its audio to a
+separate MP4. Candidate decode validation is audio-only; video packet identity is
+checked separately. The original recording verification remains unchanged. Chrome
+conversion already encodes audio, so correction adds one audio encode and can make
+one bounded peak-correction attempt. Keeping both originals and derivatives requires
+an additional output upload and one full output read to verify stored bytes, plus
+two one-byte identity reads. Media bytes travel directly to storage; the web callback
+only handles metadata and signed URLs. Output verification has its own reserved
+transfer budget, independent of the completed original job's download budget.
+
+The original becomes playable before correction. Corrections run only with spare
+capacity, use one FFmpeg decoder/filter thread, and occupy one audio slot per replica.
+Clearing the two audio selection fields restores the retained original. Rolling back
+to the pre-activation web deployment also selects the original; the original recording
+verification target is never redirected to a derivative.
## Evidence and limits
@@ -161,10 +176,32 @@ Run the exact policy in the production Linux image, then verify actual share-pag
embed, seeking, downloads, edits, transcript alignment, and fallback behavior.
Measure worker memory, throughput, storage, and tail latency before rollout.
-The level-correction integration runs as a durable Workflow step. Its immutable
-derivative key is bound to the original key, object identity, and step identity.
-The worker hashes the source and output, verifies video packets and audio timing,
-and verifies the uploaded bytes before returning. Publication rechecks source and
-output identities and locks the video row while selecting the derivative. A
-response lost after upload can leave an unpublished derivative; it is never served
-and remains under the recording's prefix for normal recording deletion.
+The level-correction integration runs inside the existing media job, after the
+original completion callback and before local cleanup. It does not start another
+Workflow or media job. Its immutable derivative key is bound to the original key,
+object identity, and media job identity. A signed, expiring publication token binds
+ownership, storage, source, and output. The worker hashes the source and output,
+verifies video packets and audio timing, and verifies uploaded bytes. Publication
+rechecks source and output identities while holding the video row lock. Edits,
+replacement, and reprocessing clear stale audio selection; copying resolves selected
+bytes into the new recording's canonical output. A lost publication response can
+leave an unpublished derivative under the recording prefix for normal deletion.
+
+The local-file integration was exercised on 20 complete production MP4s in the Linux
+image with two CPUs and 2 GiB of memory. That set includes eight browser recordings
+and twelve desktop recordings. One quiet recording was eligible; nineteen remained
+unchanged under the existing stream, level, and timing gates. All 20 original hashes
+were preserved, and the eligible output was byte-identical to the prior worker's
+output. It improved from -33.06 to -21.75 LUFS with a -1.99 dBTP true peak. A timed
+run took 13.1 seconds versus 16.1 seconds with the separate-source-download worker;
+these offline runs use mocked storage and do not establish production tail latency.
+
+Route tests exercise actual segmented muxing and Chrome/WebM conversion, assert each
+source is downloaded once, and check that correction receives the published local
+MP4 only after original completion. A thrown correction failure preserves the
+successful recording and cleans up temporary files. Separate worker tests exercise
+real correction, corrupted output, unavailable publication, capacity rejection,
+concurrency, and independent transfer-budget accounting. Web tests cover authenticated
+callbacks, signed publication, source/storage races, output selection, copies, edits,
+and replacements. These checks cannot guarantee zero regressions on every recording;
+unsupported cases retain their original audio.
diff --git a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
index 9b7a8871f..d1e427d61 100644
--- a/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-routes-real-world.integration.test.ts
@@ -16,6 +16,7 @@ import os, { tmpdir } from "node:os";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import type appType from "../../app";
+import * as audioLevels from "../../lib/audio-levels";
import * as containerCpu from "../../lib/container-cpu";
import * as containerMemory from "../../lib/container-memory";
import type { Job, JobProgress } from "../../lib/job-manager";
@@ -216,6 +217,23 @@ beforeAll(async () => {
getJob = jobManager.getJob;
deleteJob = jobManager.deleteJob;
tempDir = mkdtempSync(join(tmpdir(), "cap-real-world-routes-"));
+ execFileSync("ffmpeg", [
+ "-v",
+ "error",
+ "-i",
+ TEST_VIDEO_WITH_AUDIO,
+ "-c:v",
+ "libvpx-vp9",
+ "-deadline",
+ "realtime",
+ "-cpu-used",
+ "8",
+ "-c:a",
+ "libopus",
+ "-threads",
+ "1",
+ join(tempDir, "chrome.webm"),
+ ]);
for (const kind of ["video", "audio"] as const) {
const path = join(tempDir, `${kind}-fragmented.mp4`);
execFileSync("ffmpeg", [
@@ -367,13 +385,17 @@ beforeAll(async () => {
? TEST_VIDEO_NO_AUDIO
: url.pathname === "/fixtures/test-with-audio.mp4"
? TEST_VIDEO_WITH_AUDIO
- : null;
+ : url.pathname === "/fixtures/chrome.webm"
+ ? join(tempDir, "chrome.webm")
+ : null;
if (fixturePath) {
if (request.method === "GET") fixtureReads.push(url.pathname);
const fixture = Bun.file(fixturePath);
const headers = {
- "Content-Type": "video/mp4",
+ "Content-Type": fixturePath.endsWith(".webm")
+ ? "video/webm"
+ : "video/mp4",
"Content-Length": String(await fixture.size),
};
return request.method === "HEAD"
@@ -661,6 +683,125 @@ describe("media routes real-world integration tests", () => {
}
}, 30_000);
+ test("runs optional audio correction on the local verified file after publication and retains success if it fails", async () => {
+ let observed: () => void = () => {};
+ const observations: Record<string, unknown>[] = [];
+ const called = new Promise<void>((resolve) => {
+ observed = resolve;
+ });
+ const audio = spyOn(
+ audioLevels,
+ "enhanceLocalRecording",
+ ).mockImplementation(async (input) => {
+ try {
+ observations.push({
+ phase: getJob(input.jobId)?.phase,
+ webhookPhase: webhookPhases.at(-1),
+ matchesPublishedBytes:
+ objectIdentity(await readFile(input.path)) === input.sourceIdentity,
+ sourceKey: input.sourceKey,
+ });
+ } finally {
+ observed();
+ }
+ throw new Error("Optional correction unavailable");
+ });
+ let jobId: string | undefined;
+ try {
+ const body = { ...fencedMuxRequest("local-audio"), audioLevels: true };
+ const response = await app.fetch(
+ mediaPostRequest("/video/mux-segments", body),
+ );
+ expect(response.status).toBe(200);
+ jobId = ((await response.json()) as { jobId: string }).jobId;
+ await withTimeout(called, 10_000);
+ await new Promise((resolve) => setTimeout(resolve, 25));
+ expect(getJob(jobId)?.phase).toBe("complete");
+ expect(getJob(jobId)?.error).toBeUndefined();
+ expect(audio).toHaveBeenCalledTimes(1);
+ expect(observations).toEqual([
+ {
+ phase: "complete",
+ webhookPhase: "complete",
+ matchesPublishedBytes: true,
+ sourceKey: "recording-generations/local-audio/result.mp4",
+ },
+ ]);
+ expect(sourceReads).toHaveLength(body.sourceObjects.length);
+ } finally {
+ audio.mockRestore();
+ if (jobId) deleteJob(jobId);
+ }
+ }, 30_000);
+
+ test("downloads Chrome input once and corrects the local MP4 only after publishing the original", async () => {
+ let observed: () => void = () => {};
+ const called = new Promise<void>((resolve) => {
+ observed = resolve;
+ });
+ const observations: Record<string, unknown>[] = [];
+ let outputPath: string | undefined;
+ const audio = spyOn(
+ audioLevels,
+ "enhanceLocalRecording",
+ ).mockImplementation(async (input) => {
+ try {
+ outputPath = input.path;
+ observations.push({
+ phase: getJob(input.jobId)?.phase,
+ webhookPhase: webhookPhases.at(-1),
+ matchesPublishedBytes:
+ objectIdentity(await readFile(input.path)) === input.sourceIdentity,
+ sourceKey: input.sourceKey,
+ });
+ } finally {
+ observed();
+ }
+ throw new Error("Optional correction unavailable");
+ });
+ let jobId: string | undefined;
+ try {
+ const response = await app.fetch(
+ mediaPostRequest("/video/process", {
+ videoId: "chrome-audio",
+ userId: "owner",
+ audioLevels: true,
+ videoUrl: fixtureUrl("chrome.webm"),
+ inputExtension: ".webm",
+ outputPresignedUrl: uploadUrl("chrome-audio.mp4"),
+ webhookUrl: `${baseUrl}/ignored-webhook`,
+ webhookSecret: MEDIA_SERVER_SECRET,
+ }),
+ );
+ expect(response.status).toBe(200);
+ jobId = ((await response.json()) as { jobId: string }).jobId;
+ await withTimeout(called, 10_000);
+ await withTimeout(
+ (async () => {
+ while (outputPath && (await Bun.file(outputPath).exists()))
+ await Bun.sleep(10);
+ })(),
+ 2_000,
+ );
+ expect(getJob(jobId)?.phase).toBe("complete");
+ expect(getJob(jobId)?.error).toBeUndefined();
+ expect(audio).toHaveBeenCalledTimes(1);
+ expect(observations).toEqual([
+ {
+ phase: "complete",
+ webhookPhase: "complete",
+ matchesPublishedBytes: true,
+ sourceKey: "owner/chrome-audio/result.mp4",
+ },
+ ]);
+ expect(fixtureReads).toEqual(["/fixtures/chrome.webm"]);
+ expect(uploadRequests).toEqual(["/uploads/chrome-audio.mp4"]);
+ } finally {
+ audio.mockRestore();
+ if (jobId) deleteJob(jobId);
+ }
+ }, 30_000);
+
test("refuses a local output changed between full decode and byte binding", async () => {
const originalHash = recordingVerification.hashRecordingFile;
const hashing = spyOn(