`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>
80 lines
3.8 KiB
TypeScript
80 lines
3.8 KiB
TypeScript
import { describe, test, expect, afterAll } from 'bun:test';
|
|
import { mkdtemp, readdir, readFile, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { LiveRecorder, mediaExtension } from '../src/node.js';
|
|
import type { ReceivedFrame, LiveContent } from '../src/index.js';
|
|
|
|
const dirs: string[] = [];
|
|
afterAll(async () => {
|
|
for (const d of dirs) await rm(d, { recursive: true, force: true });
|
|
});
|
|
async function base(): Promise<string> {
|
|
const d = await mkdtemp(join(tmpdir(), 'shade-record-'));
|
|
dirs.push(d);
|
|
return join(d, 'rec');
|
|
}
|
|
const b = (...xs: number[]) => new Uint8Array(xs);
|
|
const frame = (seq: number, content: LiveContent, epoch = 0): ReceivedFrame => ({
|
|
epoch,
|
|
seq,
|
|
timestamp: seq,
|
|
sticky: content.type === 'init' || content.type === 'meta',
|
|
keyframe: false,
|
|
content,
|
|
});
|
|
|
|
describe('LiveRecorder', () => {
|
|
test('media: the init, then chunks in order; the init again is not news; a late chunk is left out', async () => {
|
|
const at = await base();
|
|
const r = new LiveRecorder(at);
|
|
const init: LiveContent = { type: 'init', mime: 'video/webm; codecs="vp9,opus"', bytes: b(1, 2, 3) };
|
|
await r.frame(frame(1, { type: 'chunk', bytes: b(9) })); // before any init: undecodable
|
|
await r.frame(frame(0, init));
|
|
await r.frame(frame(2, { type: 'chunk', bytes: b(4) }));
|
|
await r.frame(frame(4, { type: 'chunk', bytes: b(6) }));
|
|
await r.frame(frame(3, { type: 'chunk', bytes: b(5) })); // late: after 4
|
|
await r.frame(frame(0, init)); // a reconnect replays the sticky init
|
|
await r.frame(frame(5, { type: 'chunk', bytes: b(7) }));
|
|
await r.frame(frame(0, { type: 'chunk', bytes: b(8) }, 1)); // next epoch starts over at 0
|
|
const stats = await r.close();
|
|
expect(stats.late).toBe(1);
|
|
expect(stats.beforeInit).toBe(1);
|
|
expect(stats.files).toEqual([{ path: `${at}.webm`, kind: 'media', mime: 'video/webm; codecs="vp9,opus"', bytes: 7 }]);
|
|
expect(new Uint8Array(await readFile(`${at}.webm`))).toEqual(b(1, 2, 3, 4, 6, 7, 8));
|
|
});
|
|
|
|
test('a different init (the publisher started over) goes on in a second file', async () => {
|
|
const at = await base();
|
|
const r = new LiveRecorder(at);
|
|
await r.frame(frame(0, { type: 'init', mime: 'video/mp4; codecs="avc1.64001f"', bytes: b(1) }));
|
|
await r.frame(frame(1, { type: 'chunk', bytes: b(2) }));
|
|
await r.frame(frame(2, { type: 'init', mime: 'video/mp4; codecs="avc1.640028"', bytes: b(3) }));
|
|
await r.frame(frame(3, { type: 'chunk', bytes: b(4) }));
|
|
const { files } = await r.close();
|
|
expect(files.map((f) => f.path)).toEqual([`${at}.mp4`, `${at}-2.mp4`]);
|
|
expect(new Uint8Array(await readFile(`${at}-2.mp4`))).toEqual(b(3, 4));
|
|
});
|
|
|
|
test('text to .txt, pictures to a folder in order, the channel meta kept', async () => {
|
|
const at = await base();
|
|
const r = new LiveRecorder(at);
|
|
await r.frame(frame(0, { type: 'meta', meta: { name: 'Kveldssending' } }));
|
|
await r.frame(frame(1, { type: 'text', text: 'linje 1\n' }));
|
|
await r.frame(frame(2, { type: 'image', mime: 'image/jpeg', bytes: b(0xff, 0xd8) }));
|
|
await r.frame(frame(3, { type: 'text', text: 'linje 2\n' }));
|
|
await r.frame(frame(4, { type: 'image', mime: 'image/png', bytes: b(0x89) }));
|
|
const stats = await r.close();
|
|
expect(stats.meta).toEqual({ name: 'Kveldssending' });
|
|
expect(await readFile(`${at}.txt`, 'utf8')).toBe('linje 1\nlinje 2\n');
|
|
expect((await readdir(`${at}-images`)).sort()).toEqual(['00001.jpg', '00002.png']);
|
|
expect(stats.files.find((f) => f.kind === 'images')).toMatchObject({ count: 2, bytes: 3 });
|
|
});
|
|
|
|
test('extensions from the init type', () => {
|
|
expect(mediaExtension('audio/webm; codecs="opus"')).toBe('webm');
|
|
expect(mediaExtension('audio/mp4; codecs="mp4a.40.2"')).toBe('mp4');
|
|
expect(mediaExtension('application/x-weird')).toBe('bin');
|
|
});
|
|
});
|