Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(media-server): download recording segments from one shared, configurable pool

Richie McIlroy39bfcd2c8530
diff --git a/apps/media-server/src/__tests__/routes/segment-download-pool.test.ts b/apps/media-server/src/__tests__/routes/segment-download-pool.test.ts
new file mode 100644
index 000000000..913dd72a8
--- /dev/null
+++ b/apps/media-server/src/__tests__/routes/segment-download-pool.test.ts
@@ -0,0 +1,93 @@
+import { afterAll, beforeAll, describe, expect, test } from "bun:test";
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import {
+ downloadSegmentTracks,
+ segmentDownloadConcurrencyFromEnv,
+} from "../../routes/video";
+
+let server: ReturnType<typeof Bun.serve>;
+let directory: string;
+let inFlight = 0;
+let peak = 0;
+const startedAt = new Map<string, number>();
+const finishedAt = new Map<string, number>();
+
+beforeAll(async () => {
+ directory = await mkdtemp(join(tmpdir(), "segment-pool-"));
+ server = Bun.serve({
+ hostname: "127.0.0.1",
+ port: 0,
+ async fetch(request) {
+ const name = new URL(request.url).pathname.slice(1);
+ startedAt.set(name, performance.now());
+ inFlight++;
+ peak = Math.max(peak, inFlight);
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ inFlight--;
+ finishedAt.set(name, performance.now());
+ return new Response(name, {
+ headers: { "Content-Length": String(name.length) },
+ });
+ },
+ });
+});
+
+afterAll(async () => {
+ server.stop(true);
+ await rm(directory, { recursive: true, force: true });
+ delete process.env.MEDIA_SERVER_SEGMENT_DOWNLOAD_CONCURRENCY;
+});
+
+describe("segment download pool", () => {
+ test("reads its concurrency from the environment with a safe default and cap", () => {
+ expect(segmentDownloadConcurrencyFromEnv({})).toBe(10);
+ expect(
+ segmentDownloadConcurrencyFromEnv({
+ MEDIA_SERVER_SEGMENT_DOWNLOAD_CONCURRENCY: "48",
+ }),
+ ).toBe(48);
+ expect(
+ segmentDownloadConcurrencyFromEnv({
+ MEDIA_SERVER_SEGMENT_DOWNLOAD_CONCURRENCY: "500",
+ }),
+ ).toBe(64);
+ expect(
+ segmentDownloadConcurrencyFromEnv({
+ MEDIA_SERVER_SEGMENT_DOWNLOAD_CONCURRENCY: "nope",
+ }),
+ ).toBe(10);
+ });
+
+ test("downloads video and audio segments from one bounded pool", async () => {
+ process.env.MEDIA_SERVER_SEGMENT_DOWNLOAD_CONCURRENCY = "8";
+ const base = `http://127.0.0.1:${server.port}`;
+ const video = Array.from({ length: 30 }, (_, i) => `${base}/video-${i}`);
+ const audio = Array.from({ length: 30 }, (_, i) => `${base}/audio-${i}`);
+ const videoDir = join(directory, "video");
+ const audioDir = join(directory, "audio");
+ await Bun.write(join(videoDir, ".keep"), "");
+ await Bun.write(join(audioDir, ".keep"), "");
+ const [videoFiles, audioFiles] = await downloadSegmentTracks(
+ [
+ { urls: video, dir: videoDir },
+ { urls: audio, dir: audioDir },
+ ],
+ "segment-pool-test",
+ 5,
+ 55,
+ );
+ expect(peak).toBe(8);
+ // Audio starts while video segments are still downloading instead of after the whole video track.
+ expect(startedAt.get("audio-0")).toBeLessThan(
+ finishedAt.get("video-29") ?? 0,
+ );
+ expect(videoFiles).toHaveLength(30);
+ expect(audioFiles).toHaveLength(30);
+ expect(videoFiles[0]).toBe(join(videoDir, "segment_001.m4s"));
+ expect(audioFiles[29]).toBe(join(audioDir, "segment_030.m4s"));
+ expect(await readFile(videoFiles[12], "utf8")).toBe("video-12");
+ expect(await readFile(audioFiles[7], "utf8")).toBe("audio-7");
+ });
+});
diff --git a/apps/media-server/src/routes/video.ts b/apps/media-server/src/routes/video.ts
index 1bedd8777..9c72ac68b 100644
--- a/apps/media-server/src/routes/video.ts
+++ b/apps/media-server/src/routes/video.ts
@@ -2845,40 +2845,63 @@ async function downloadUrlToFile(
throw lastError ?? new Error("Download failed");
}
-async function downloadSegmentsBatchTracked(
- urls: string[],
- dir: string,
+export function segmentDownloadConcurrencyFromEnv(
+ env: Record<string, string | undefined> = process.env,
+): number {
+ const value = Number.parseInt(
+ env.MEDIA_SERVER_SEGMENT_DOWNLOAD_CONCURRENCY ?? "",
+ 10,
+ );
+ return Number.isFinite(value) && value > 0 ? Math.min(value, 64) : 10;
+}
+
+interface SegmentTrackDownload {
+ urls: string[];
+ dir: string;
+}
+
+// Recordings are thousands of small segments, so latency per request dominates; video and audio
+// share one pool so neither track waits for the other to finish.
+export async function downloadSegmentTracks(
+ tracks: SegmentTrackDownload[],
jobId: string,
progressBase: number,
progressRange: number,
abortSignal?: AbortSignal,
sources?: Map<string, MuxSourceObject>,
-): Promise<string[]> {
+): Promise<string[][]> {
const { join } = await import("node:path");
let completed = 0;
let fatalError: Error | undefined;
- const total = urls.length;
- const indexWidth = Math.max(3, String(total).length);
- const outputPaths = new Array<string>(total);
- const pending = [...urls.entries()];
+ const outputPaths = tracks.map(
+ (track) => new Array<string>(track.urls.length),
+ );
+ const pending = tracks.flatMap((track, trackIndex) => {
+ const indexWidth = Math.max(3, String(track.urls.length).length);
+ return track.urls.map((url, i) => ({
+ trackIndex,
+ i,
+ url,
+ outputPath: join(
+ track.dir,
+ `segment_${String(i + 1).padStart(indexWidth, "0")}.m4s`,
+ ),
+ }));
+ });
+ const total = pending.length;
let pendingIndex = 0;
const batchAbortController = new AbortController();
const batchSignal = abortSignal
? AbortSignal.any([abortSignal, batchAbortController.signal])
: batchAbortController.signal;
- const CONCURRENCY = 10;
async function worker() {
while (pendingIndex < pending.length && !fatalError) {
const entry = pending[pendingIndex++];
if (!entry) break;
- const [i, url] = entry;
+ const { trackIndex, i, url, outputPath } = entry;
try {
- const outputPath = join(
- dir,
- `segment_${String(i + 1).padStart(indexWidth, "0")}.m4s`,
- );
- outputPaths[i] = outputPath;
+ outputPaths[trackIndex][i] = outputPath;
await downloadUrlToFile(
url,
outputPath,
@@ -2891,7 +2914,7 @@ async function downloadSegmentsBatchTracked(
pendingIndex = pending.length;
batchAbortController.abort();
console.error(
- `[mux-segments] Failed to download segment ${i + 1}/${total}:`,
+ `[mux-segments] Failed to download segment ${i + 1}/${tracks[trackIndex].urls.length}:`,
err instanceof Error ? err.message : err,
);
}
@@ -2909,7 +2932,10 @@ async function downloadSegmentsBatchTracked(
}
await Promise.all(
- Array.from({ length: Math.min(CONCURRENCY, total) }, () => worker()),
+ Array.from(
+ { length: Math.min(segmentDownloadConcurrencyFromEnv(), total) },
+ () => worker(),
+ ),
);
if (fatalError) throw fatalError;
return outputPaths;
@@ -2998,49 +3024,45 @@ async function muxSegmentsAsync(
await mkdir(videoDir, { recursive: true });
await mkdir(audioDir, { recursive: true });
- await downloadUrlToFile(
- videoInitUrl,
- join(videoDir, "init.mp4"),
- abortController.signal,
- sources?.get(videoInitUrl),
- );
- updateJob(jobId, { phase: "downloading", progress: 5 });
- sendCurrentJobWebhook(jobId);
-
- const videoSegmentFiles = await downloadSegmentsBatchTracked(
- videoSegmentUrls,
- videoDir,
- jobId,
- 5,
- 45,
- abortController.signal,
- sources,
- );
-
const audioInput =
audioInitUrl !== null &&
audioSegmentUrls !== null &&
audioSegmentUrls.length > 0
? { initUrl: audioInitUrl, segmentUrls: audioSegmentUrls }
: null;
- let audioSegmentFiles: string[] = [];
- if (audioInput) {
- await downloadUrlToFile(
- audioInput.initUrl,
- join(audioDir, "init.mp4"),
+ await Promise.all([
+ downloadUrlToFile(
+ videoInitUrl,
+ join(videoDir, "init.mp4"),
abortController.signal,
- sources?.get(audioInput.initUrl),
- );
- audioSegmentFiles = await downloadSegmentsBatchTracked(
- audioInput.segmentUrls,
- audioDir,
+ sources?.get(videoInitUrl),
+ ),
+ audioInput
+ ? downloadUrlToFile(
+ audioInput.initUrl,
+ join(audioDir, "init.mp4"),
+ abortController.signal,
+ sources?.get(audioInput.initUrl),
+ )
+ : undefined,
+ ]);
+ updateJob(jobId, { phase: "downloading", progress: 5 });
+ sendCurrentJobWebhook(jobId);
+
+ const [videoSegmentFiles, audioSegmentFiles = []] =
+ await downloadSegmentTracks(
+ [
+ { urls: videoSegmentUrls, dir: videoDir },
+ ...(audioInput
+ ? [{ urls: audioInput.segmentUrls, dir: audioDir }]
+ : []),
+ ],
jobId,
- 50,
- 10,
+ 5,
+ 55,
abortController.signal,
sources,
);
- }
updateJob(jobId, {
phase: "processing",