Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(media-server): preserve audio output and report stream failures

Aditya Garudc144cad68887
diff --git a/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts b/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
index 826d49234..66d5d71ed 100644
--- a/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, test } from "bun:test";
+import { describe, expect, spyOn, test } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -8,6 +8,7 @@ import {
extractAudioStream,
getActiveProcessCount,
} from "../../lib/media-audio";
+import * as subprocess from "../../lib/subprocess";
const FIXTURES_DIR = join(import.meta.dir, "..", "fixtures");
const TEST_VIDEO_WITH_AUDIO = `file://${join(FIXTURES_DIR, "test-with-audio.mp4")}`;
@@ -223,6 +224,50 @@ describe("mediaAudio integration tests", () => {
});
describe("extractAudioStream", () => {
+ test("drains all encoded bytes when the consumer waits for FFmpeg to exit", async () => {
+ const expected = await extractAudio(TEST_VIDEO_WITH_AUDIO);
+ const registration = spyOn(subprocess, "registerSubprocess");
+ const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
+ try {
+ const proc = registration.mock.calls[0][0];
+ expect(await proc.exited).toBe(0);
+ const actual = await readStream(stream);
+ expect(actual.byteLength).toBe(expected.byteLength);
+ expect(Buffer.from(actual).equals(expected)).toBe(true);
+ } finally {
+ cleanup();
+ registration.mockRestore();
+ }
+ });
+
+ test("errors the stream when FFmpeg cannot extract audio", async () => {
+ const beforeCount = getActiveProcessCount();
+ const { stream, cleanup } = extractAudioStream(TEST_VIDEO_NO_AUDIO);
+ try {
+ await expect(readStream(stream)).rejects.toThrow("FFmpeg exited");
+ await waitForAudioOperations(beforeCount);
+ } finally {
+ cleanup();
+ }
+ });
+
+ test("errors the stream and terminates FFmpeg at the deadline", async () => {
+ const beforeCount = getActiveProcessCount();
+ const registration = spyOn(subprocess, "registerSubprocess");
+ const { stream, cleanup } = extractAudioStream(TEST_VIDEO_WITH_AUDIO, {
+ timeoutMs: 1,
+ });
+ try {
+ await expect(readStream(stream)).rejects.toThrow("timed out");
+ const proc = registration.mock.calls[0][0];
+ expect(await proc.exited).not.toBe(0);
+ await waitForAudioOperations(beforeCount);
+ } finally {
+ cleanup();
+ registration.mockRestore();
+ }
+ });
+
test("streams real mp3 data and cleans up its operation", async () => {
const beforeCount = getActiveProcessCount();
const { stream } = extractAudioStream(TEST_VIDEO_WITH_AUDIO);
diff --git a/apps/media-server/src/lib/media-audio.ts b/apps/media-server/src/lib/media-audio.ts
index 5e438acbd..040ece6da 100644
--- a/apps/media-server/src/lib/media-audio.ts
+++ b/apps/media-server/src/lib/media-audio.ts
@@ -399,10 +399,12 @@ export function extractAudioStream(
let timeoutId: ReturnType<typeof setTimeout> | undefined;
let cleaned = false;
let reader: ReadableStreamDefaultReader<Uint8Array> | null = null;
+ let streamController: ReadableStreamDefaultController<Uint8Array> | undefined;
- const cleanup = () => {
+ const cleanup = (error = new Error("Audio extraction cancelled")) => {
if (cleaned) return;
cleaned = true;
+ streamController?.error(error);
if (timeoutId) clearTimeout(timeoutId);
if (reader) {
try {
@@ -416,20 +418,28 @@ export function extractAudioStream(
};
timeoutId = setTimeout(() => {
- cleanup();
+ cleanup(new Error(`Operation timed out after ${opts.timeoutMs}ms`));
}, opts.timeoutMs);
void drainStream(proc.stderr as ReadableStream<Uint8Array>);
- proc.exited.then(() => {
- cleanup();
+ proc.exited.then((exitCode) => {
+ if (cleaned) return;
+ if (exitCode !== 0) {
+ cleanup(new Error(`FFmpeg exited with code ${exitCode}`));
+ return;
+ }
+ if (timeoutId) clearTimeout(timeoutId);
+ unregisterMediaOperation(operation);
+ void terminateProcess(proc);
});
const originalStream = proc.stdout as ReadableStream<Uint8Array>;
const stream = new ReadableStream<Uint8Array>(
{
- start() {
+ start(controller) {
+ streamController = controller;
reader = originalStream.getReader();
},
async pull(controller) {
@@ -440,7 +450,10 @@ export function extractAudioStream(
try {
const { done, value } = await reader.read();
+ if (cleaned) return;
if (done) {
+ await proc.exited;
+ if (cleaned) return;
controller.close();
cleanup();
} else {