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');
|
||
|
|
});
|
||
|
|
});
|