feat(media): shade live record — a broadcast to files, or to a sealed film with its own link

`live record <link> [-o base] [--put --server URL]` writes what a channel
carries until the channel ends or Ctrl-C:
- text goes to base.txt;
- video and sound go to base.webm or base.mp4: the init, then every chunk in
  the order sent. A chunk that arrives late is left out and counted. A new
  init continues in base-2.…;
- pictures go to base-images/.

ffmpeg then rewrites each recording with -c copy, which adds an index for
seeking and starts the clock at 0:00. `--put` seals and uploads the recording
under the channel's name (several files become an album). LiveRecorder is
exported from @shade/media/node.

`live video` now sends a sticky meta frame like text and picture channels do.
The browser and Android players ignore it.

Verified against a real relay:
- A 30 s VP8/Opus broadcast was recorded and is 29–32 s long, starting at 0.
- It was uploaded as "Kveldssending.webm" and plays in Chromium, with a seek
  to 22 s (frame 14).
- Live playback with the meta frame still plays, with 2.4 s latency.

Tests:
- object-store's memory test now runs in its own process. RSS is process-wide,
  and other test files in the same `bun test` made it flaky (192 MiB). It now
  compares memory held after a collection at 128 MiB and 512 MiB, ~85 vs
  ~103 MiB, instead of using a magic number.
- The sweep test has its own prefix.
- tests/live-record.test.ts (4).
- media 122 green, 3 runs in a row. All 27 packages type-check.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-19 00:01:55 +02:00
parent ee93dee8db
commit 5cc9133b3c
10 changed files with 462 additions and 26 deletions

View File

@@ -111,28 +111,27 @@ describe.skipIf(URL_ === undefined)('ObjectMediaStore (S3)', () => {
}, 60_000);
test('a film streams through: memory is bounded by the parts in flight, not the film', async () => {
const partSize = 8 * 1024 * 1024;
const store = new ObjectMediaStore(s3, new MemoryMediaIndex(), { prefix, partSize });
const chunk = new Uint8Array(1024 * 1024);
const total = 512 * 1024 * 1024;
Bun.gc(true);
const before = process.memoryUsage().rss;
let peak = before;
const id = toHex(new Uint8Array(16).fill(0x5a));
const up = await store.begin(id);
for (let off = 0; off < total; off += chunk.length) {
chunk.fill((off / chunk.length) & 0xff);
await up.write(chunk);
peak = Math.max(peak, process.memoryUsage().rss);
}
await up.commit({ id, owner: 'o', size: total, readTokenHash: null, createdAt: Date.now() });
// Three parts (one filling, two uploading) and the HTTP machinery — not 512 MiB.
const grewMiB = (peak - before) / 1024 / 1024;
console.log(`object upload of 512 MiB: RSS grew ${grewMiB.toFixed(0)} MiB`);
expect(grewMiB).toBeLessThan(120);
const body = await store.body(id, 300 * 1024 * 1024 - 1, 300 * 1024 * 1024 + 1);
expect(new Uint8Array(await new Response(body).arrayBuffer())).toEqual(new Uint8Array([299 & 0xff, 300 & 0xff]));
expect(await store.delete(id)).toBe(true);
// Each size in a process of its own: RSS is process-wide, and other test
// files in this one leave garbage the collector has not returned yet.
const grew = async (mib: number) => {
const proc = Bun.spawn(['bun', `${import.meta.dir}/fixtures/object-upload-rss.ts`, URL_!, prefix, String(mib)], { stdout: 'pipe', stderr: 'inherit' });
const out = await new Response(proc.stdout).text();
expect(await proc.exited).toBe(0);
const r = JSON.parse(out) as { retainedMiB: number; peakMiB: number; readBack: boolean };
expect(r.readBack).toBe(true);
return r;
};
const small = await grew(128);
const large = await grew(512);
const f = (n: number) => n.toFixed(0);
console.log(
`object upload: RSS held ${f(small.retainedMiB)} MiB for 128 MiB, ${f(large.retainedMiB)} MiB for 512 MiB ` +
`(peaks before collection ${f(small.peakMiB)} / ${f(large.peakMiB)})`,
);
// Three parts (one filling, two uploading) and the HTTP machinery: four
// times the film is not four times the memory, nor anywhere near the film.
expect(large.retainedMiB).toBeLessThan(small.retainedMiB + 48);
expect(large.retainedMiB).toBeLessThan(128);
}, 180_000);
test('a film in pieces across two instances: each piece lands where it lands, one multipart upload, one object', async () => {
@@ -164,15 +163,18 @@ describe.skipIf(URL_ === undefined)('ObjectMediaStore (S3)', () => {
test('pieces out of order and twice at once on two instances; sweep spares an old upload still being sent', async () => {
const index = new MemoryMediaIndex();
const grace = 2000;
const a = new ObjectMediaStore(s3, index, { prefix, sweepGraceMs: grace });
const b = new ObjectMediaStore(s3, index, { prefix, sweepGraceMs: grace });
// A prefix of its own: with a grace period this short, a sweep over the
// shared one would take other tests' objects for orphans.
const own = `${prefix}sweep/`;
const a = new ObjectMediaStore(s3, index, { prefix: own, sweepGraceMs: grace });
const b = new ObjectMediaStore(s3, index, { prefix: own, sweepGraceMs: grace });
const { bytes, result } = await sealMediaBytes(pattern(DEFAULT_CHUNK_BYTES + 1024 * 1024));
const id = toHex(result.mediaId);
const terms = { id, owner: 'o', size: bytes.length, readTokenHash: null, chunk: DEFAULT_CHUNK_BYTES, createdAt: Date.now() };
await a.openUpload(terms);
// B starting the same upload finds A's, rather than a second multipart upload.
await b.openUpload(terms);
expect((await s3.listMultipart(prefix)).filter((u) => u.key.includes(id)).length).toBe(1);
expect((await s3.listMultipart(own)).filter((u) => u.key.includes(id)).length).toBe(1);
// The last piece first; then, past the grace period, the first one on both instances at once.
expect(await b.appendUpload(id, DEFAULT_CHUNK_BYTES, bytes.slice(DEFAULT_CHUNK_BYTES))).toBe(0);
@@ -187,6 +189,6 @@ describe.skipIf(URL_ === undefined)('ObjectMediaStore (S3)', () => {
await Bun.sleep(grace + 1200);
expect(await b.sweep()).toEqual({ objects: 0, uploads: 1 });
expect(await index.getPending(id)).toBeNull();
expect((await s3.listMultipart(prefix)).filter((u) => u.key.includes(id))).toEqual([]);
expect((await s3.listMultipart(own)).filter((u) => u.key.includes(id))).toEqual([]);
}, 60_000);
});