LiveWatcher is the Android LiveWatchClient (OkHttp WebSocket, subscribe with the read token, reconnect with backoff, backlog deduplicated by the subscriber). ShadeLiveDataSource turns frames into one endless WebM/fMP4 stream for ExoPlayer's extractors: init, then media from the first keyframe, and after a gap nothing until the next keyframe. Late joiners start at 0:00 (fMP4: per-join TimestampAdjuster; WebM: cluster Timecode rewritten in place). A changed init fails with a ParserException (not retried). The demo app plays live video and follows log channels. LiveSubscriber.close() wipes keys, as in TS. Verified (Robolectric): late join on H.264/AAC fMP4 and VP8/Opus WebM cut by the TS splitter (fixtures from scripts/make-live-fixtures.ts) plays from the keyframe, rebased (mutation check: without the rebase the WebM test fails, 5020 vs 4250 ms); gap recovery; format change; LiveWatcher against MockWebServer with a dropped connection and backlog replay. Kotlin 81/0. Not yet run on a device. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
59 lines
2.4 KiB
TypeScript
59 lines
2.4 KiB
TypeScript
/**
|
|
* Live-stream fixtures for the Android tests (`:shade-android-media`).
|
|
*
|
|
* Encodes a short H.264/AAC fragmented MP4 and a VP8/Opus WebM with ffmpeg,
|
|
* cuts both with the TS `MediaSplitter` — the same cut `shade live video`
|
|
* sends — and writes the pieces as `[u8 kind][u32 length][bytes]…`, kind 0 =
|
|
* init, 1 = keyframe piece, 2 = other piece. The Kotlin tests seal them into
|
|
* live frames and play them through ExoPlayer.
|
|
*
|
|
* Run: bun scripts/make-live-fixtures.ts
|
|
*/
|
|
import { writeFile } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { MediaSplitter } from '../packages/shade-media/src/splitter.js';
|
|
|
|
const OUT = join(import.meta.dir, '../android/shade-android-media/src/test/resources');
|
|
const SRC = [
|
|
'-f', 'lavfi', '-i', 'testsrc2=size=160x120:rate=15,format=yuv420p',
|
|
'-f', 'lavfi', '-i', 'sine=frequency=440:sample_rate=48000', '-t', '5',
|
|
];
|
|
|
|
async function ffmpeg(args: string[]): Promise<Uint8Array> {
|
|
const p = Bun.spawn(['ffmpeg', '-hide_banner', '-loglevel', 'error', ...SRC, ...args, 'pipe:1'], { stdout: 'pipe', stderr: 'inherit' });
|
|
const out = new Uint8Array(await new Response(p.stdout).arrayBuffer());
|
|
if ((await p.exited) !== 0) throw new Error('ffmpeg failed');
|
|
return out;
|
|
}
|
|
|
|
async function write(name: string, stream: Uint8Array): Promise<void> {
|
|
const s = new MediaSplitter();
|
|
const pieces = [...s.push(stream), ...s.finish()];
|
|
const parts: Uint8Array[] = [];
|
|
for (const p of pieces) {
|
|
const head = new Uint8Array(5);
|
|
head[0] = p.type === 'init' ? 0 : p.keyframe ? 1 : 2;
|
|
new DataView(head.buffer).setUint32(1, p.bytes.length);
|
|
parts.push(head, p.bytes);
|
|
}
|
|
await writeFile(join(OUT, name), Buffer.concat(parts));
|
|
const keys = pieces.filter((p) => p.type === 'media' && p.keyframe).length;
|
|
console.log(`${name}: ${pieces[0]?.type === 'init' ? pieces[0].mime : '?'}, ${pieces.length - 1} pieces, ${keys} keyframes`);
|
|
}
|
|
|
|
await write(
|
|
'live-h264.pieces',
|
|
await ffmpeg([
|
|
'-c:v', 'libx264', '-preset', 'veryfast', '-g', '15', '-keyint_min', '15', '-sc_threshold', '0', '-crf', '35',
|
|
'-c:a', 'aac', '-b:a', '32k',
|
|
'-f', 'mp4', '-movflags', 'frag_keyframe+empty_moov+default_base_moof', '-frag_duration', '250000',
|
|
]),
|
|
);
|
|
await write(
|
|
'live-vp8.pieces',
|
|
await ffmpeg([
|
|
'-c:v', 'libvpx', '-deadline', 'realtime', '-g', '15', '-b:v', '100k', '-c:a', 'libopus', '-b:a', '32k',
|
|
'-f', 'webm', '-live', '1', '-cluster_time_limit', '250',
|
|
]),
|
|
);
|