Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(media-server): decode with every core under nvenc and skip unused decoded-content hashes in remote verification

Richie McIlroyaa7f80c8cd16
diff --git a/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts b/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts
index 0d11a2fb7..129c807bd 100644
--- a/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/recording-verification.integration.test.ts
@@ -960,6 +960,26 @@ describe("complete recording decode", () => {
);
});
+ test("skips decoded content hashes only when nothing compares them", async () => {
+ const options = { expectedDuration: 5, requireAudio: true };
+ const hashed = await verifyRecording(silent, options);
+ const unhashed = await verifyRecording(silent, {
+ ...options,
+ hashDecodedContent: false,
+ });
+ expect(unhashed.integrity).toBeUndefined();
+ expect(unhashed.video).toEqual(hashed.video);
+ expect(unhashed.audio).toEqual(hashed.audio);
+ const evidence = await inspectRecordingSources(silent, silent);
+ const preserved = await verifyRecording(silent, {
+ requireAudio: false,
+ hashDecodedContent: false,
+ sourceEvidence: evidence,
+ });
+ expect(preserved.sourcePreserved).toBe(true);
+ expect(preserved.integrity).toEqual(evidence.integrity);
+ });
+
test("accepts legacy video without audio only when audio was not requested", async () => {
const input = join(FIXTURES, "test-no-audio.mp4");
const evidence = await verifyRecording(input, {
diff --git a/apps/media-server/src/lib/media-video.ts b/apps/media-server/src/lib/media-video.ts
index 591996736..a58d944d9 100644
--- a/apps/media-server/src/lib/media-video.ts
+++ b/apps/media-server/src/lib/media-video.ts
@@ -33,6 +33,7 @@ import {
nvencFallbackReason,
nvencOptionsFromEnv,
recordNvencAttempt,
+ selectedVideoEncoder,
} from "./nvenc-encoder";
import {
downloadUrlToPathParallel,
@@ -1625,9 +1626,10 @@ export async function processVideo(
extraOutputArgs,
sourceWidth: metadata.width,
sourceHeight: metadata.height,
+ // NVENC leaves the CPU free, so decode (and scale) can use every core.
decodeThreads: decodeThreadsFromEnv(
"MEDIA_SERVER_PROCESS_DECODE_THREADS",
- 2,
+ selectedVideoEncoder() === "nvenc" ? 0 : 2,
),
};
const totalDurationUs = metadata.duration * 1_000_000;
diff --git a/apps/media-server/src/lib/recording-verification.ts b/apps/media-server/src/lib/recording-verification.ts
index 948115302..59b6b8547 100644
--- a/apps/media-server/src/lib/recording-verification.ts
+++ b/apps/media-server/src/lib/recording-verification.ts
@@ -53,6 +53,9 @@ export interface RecordingVerificationOptions {
hasAudio?: boolean;
sourceEvidence?: RecordingSourceEvidence;
allowObservedDuration?: boolean;
+ // Hashing every decoded frame costs as much as the decode; skip it when the
+ // caller has no source evidence to compare against.
+ hashDecodedContent?: boolean;
abortSignal?: AbortSignal;
timeoutMs?: number;
onProgress?: (progress: { frames: number; totalFrames?: number }) => void;
@@ -846,8 +849,11 @@ async function decodeRecording(
? Boolean(options.sourceEvidence.audio)
: options.hasAudio
: sourceAudioInput !== null;
+ const hashContent =
+ !packetProof &&
+ (options.hashDecodedContent !== false || Boolean(options.sourceEvidence));
const streamDecodedContent =
- !packetProof && process.platform !== "win32" && knownAudio !== undefined;
+ hashContent && process.platform !== "win32" && knownAudio !== undefined;
if (streamDecodedContent && !Bun.semver.satisfies(Bun.version, ">=1.4.0")) {
throw new Error(
"Recording content verification requires Bun 1.4.0 or newer; upgrade the media-server runtime to avoid double-closing decoded-content pipes",
@@ -959,7 +965,7 @@ async function decodeRecording(
]
: []),
]
- : packetProof
+ : !hashContent
? []
: [
...outputOptions,
@@ -1090,7 +1096,7 @@ async function decodeRecording(
}
stream.contentSha256 = digest.sha256;
}
- } else if (!packetProof) {
+ } else if (hashContent) {
for (const digest of diagnostics.digests) decodeLine(digest, streams);
}
result = validateEvidence(
@@ -1098,7 +1104,7 @@ async function decodeRecording(
options,
sourceAudioInput !== undefined || packetProof !== undefined,
videoTiming,
- !packetProof,
+ hashContent,
);
} catch (error) {
throw failure ?? error;
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index b194bc594..7bcec5eb9 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -2079,6 +2079,7 @@ async function verifyUploadedRecordingAsync(
body.sourceObjectIdentity ?? body.objectIdentity,
expectedFileSize: body.fileSize,
hashContent: Boolean(body.generation),
+ hashDecodedContent: false,
abortSignal: abortController.signal,
}),
),