import { Emitter } from "./emitter.js";
export function wallMicros() {
return Math.round((performance.timeOrigin + performance.now()) * 1000);
}
export function slrwRecord(kind, ptsUs, payload, prefix) {
const extra = prefix === undefined ? 0 : 1;
const out = new Uint8Array(13 + extra + payload.length);
const dv = new DataView(out.buffer);
dv.setUint8(0, kind);
dv.setBigUint64(1, BigInt(Math.max(0, Math.round(ptsUs))), true);
dv.setUint32(9, payload.length + extra, true);
if (prefix !== undefined)
out[13] = prefix;
out.set(payload, 13 + extra);
return out;
}
export function codecs(width, height, fps) {
const mbs = Math.ceil(width / 16) * Math.ceil(height / 16);
const rate = mbs * fps;
const levels = [
["1F", 3600, 108000],
["28", 8192, 245760],
["2A", 8704, 522240],
["33", 36864, 983040],
["34", 36864, 2073600],
];
const level = (levels.find(([, fs, mbps]) => mbs <= fs && rate <= mbps) ?? levels[4])[0];
return [`avc1.42E0${level}`, `avc1.4D40${level}`, `avc1.6400${level}`];
}
export class SurfaceEncoder extends Emitter {
info;
canvas;
#ws;
#encoder;
#keyInterval;
#lastKey = -Infinity;
#wantKey = true;
#config;
#timer;
#lastFrame = -Infinity;
#closed = false;
#skipped = 0;
#encodeMs = 0;
#encoded = 0;
#pending = new Map();
#warm = false;
#warmUntil = 0;
#ready;
codec = "";
constructor(info, canvas, opts = {}) {
super();
this.info = info;
this.canvas = canvas;
this.#keyInterval = (opts.keyInterval ?? 2) * 1e6;
const url = opts.url ??
`${globalThis.location.protocol === "https:" ? "wss:" : "ws:"}//${globalThis.location.host}${info.path}`;
this.#ws = new WebSocket(url);
this.#ws.binaryType = "arraybuffer";
this.#ws.onmessage = (ev) => {
if (typeof ev.data !== "string")
return;
let m;
try {
m = JSON.parse(ev.data);
}
catch {
return;
}
if (m["t"] === "keyframe")
this.#wantKey = true;
else if (m["t"] === "status")
this.emit("status", m);
else if (m["t"] === "stats") {
const n = Math.max(1, this.#encoded);
this.emit("stats", { ...m, encodeMs: Math.round((this.#encodeMs / n) * 10) / 10, skipped: this.#skipped });
this.#encodeMs = 0;
this.#encoded = 0;
this.#skipped = 0;
}
else if (m["t"] === "end")
this.close();
};
this.#ws.onclose = () => {
if (!this.#closed)
this.close();
};
const bitrate = opts.bitrate ?? Math.round(info.width * info.height * info.fps * 0.1);
this.#ready = this.#configure(bitrate).then(() => new Promise((ok, fail) => {
if (this.#ws.readyState === 1)
return ok();
this.#ws.onopen = () => ok();
this.#ws.onerror = () => fail(new Error("cannot reach tvpipe's surface socket"));
}));
this.#ready.then(() => {
this.#ws.send(JSON.stringify({ t: "log", codec: this.codec, agent: globalThis.navigator?.userAgent ?? "" }));
if (opts.clock === "timer")
this.#tick(performance.now());
}, (e) => this.emit("error", e));
}
get ready() {
return this.#ready;
}
async #configure(bitrate) {
const { width, height, fps } = this.info;
for (const accel of ["prefer-hardware", "no-preference"]) {
for (const codec of codecs(width, height, fps)) {
const config = {
codec,
width,
height,
bitrate,
framerate: fps,
latencyMode: "realtime",
hardwareAcceleration: accel,
avc: { format: "avc" },
};
let ok = false;
try {
ok = (await VideoEncoder.isConfigSupported(config)).supported === true;
}
catch { }
if (!ok)
continue;
this.#encoder = new VideoEncoder({
output: (chunk, meta) => this.#output(chunk, meta),
error: (e) => this.emit("error", e instanceof Error ? e : new Error(String(e))),
});
this.#encoder.configure(config);
this.codec = `${codec}${accel === "prefer-hardware" ? " (hw)" : ""}`;
return;
}
}
throw new Error("this browser cannot encode H.264 (VideoEncoder)");
}
#send(data) {
if (this.#ws.readyState !== 1)
return;
this.#ws.send(data);
}
#output(chunk, meta) {
const started = this.#pending.get(chunk.timestamp);
this.#pending.delete(chunk.timestamp);
if (started !== undefined) {
this.#encodeMs += performance.now() - started;
this.#encoded++;
}
const desc = meta?.decoderConfig?.description;
if (desc) {
const bytes = ArrayBuffer.isView(desc)
? new Uint8Array(desc.buffer, desc.byteOffset, desc.byteLength)
: new Uint8Array(desc);
const key = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
if (key !== this.#config) {
this.#config = key;
this.#send(slrwRecord(4, chunk.timestamp, bytes, 0));
}
}
if (!this.#warm) {
const now = wallMicros();
if (this.#warmUntil === 0)
this.#warmUntil = now + 3e6;
const fresh = now - chunk.timestamp <= 150_000 || now > this.#warmUntil;
if (chunk.type !== "key" || !fresh) {
if (chunk.type === "key")
this.#wantKey = true;
return;
}
this.#warm = true;
}
const data = new Uint8Array(chunk.byteLength);
chunk.copyTo(data);
const flags = (chunk.type === "key" ? 1 : 0) | (this.#config === undefined ? 2 : 0);
this.#send(slrwRecord(3, chunk.timestamp, data, flags));
}
#tick(due) {
if (this.#closed)
return;
this.frame(true);
const step = 1000 / this.info.fps;
let next = due + step;
const now = performance.now();
if (next < now - step)
next = now;
this.#timer = setTimeout(() => this.#tick(next), Math.max(0, next - now));
}
frame(force = false) {
const enc = this.#encoder;
if (this.#closed || !enc || enc.state !== "configured" || this.#ws.readyState !== 1)
return;
const now = performance.now();
if (!force && now - this.#lastFrame < 850 / this.info.fps)
return;
this.#lastFrame = now;
if (enc.encodeQueueSize > 2 || this.#ws.bufferedAmount > 4 << 20) {
this.#skipped++;
return;
}
const ts = wallMicros();
const key = this.#wantKey || ts - this.#lastKey >= this.#keyInterval;
let frame;
try {
frame = new VideoFrame(this.canvas, { timestamp: ts });
}
catch (e) {
this.#skipped++;
return;
}
this.#pending.set(ts, performance.now());
if (this.#pending.size > 64)
this.#pending.clear();
enc.encode(frame, { keyFrame: key });
frame.close();
if (key) {
this.#wantKey = false;
this.#lastKey = ts;
}
}
pushAudio(pcm, ptsUs = wallMicros()) {
if (this.#closed || !this.info.audio)
return;
this.#send(slrwRecord(2, ptsUs, new Uint8Array(pcm.buffer, pcm.byteOffset, pcm.byteLength)));
}
close() {
if (this.#closed)
return;
this.#closed = true;
if (this.#timer)
clearTimeout(this.#timer);
try {
if (this.#encoder?.state === "configured")
this.#encoder.close();
}
catch { }
try {
this.#ws.close();
}
catch { }
this.emit("end");
}
}
const WORKLET = `class TvpipePcm extends AudioWorkletProcessor {
process(inputs) {
const ch = inputs[0];
if (ch && ch.length) this.port.postMessage(ch.map((c) => c.slice(0)));
return true;
}
}
registerProcessor("tvpipe-pcm", TvpipePcm);`;
export async function captureAudio(ctx, node, channels, onPcm) {
const url = URL.createObjectURL(new Blob([WORKLET], { type: "text/javascript" }));
await ctx.audioWorklet.addModule(url);
URL.revokeObjectURL(url);
const tap = new AudioWorkletNode(ctx, "tvpipe-pcm", { numberOfInputs: 1, numberOfOutputs: 0, channelCount: channels, channelCountMode: "explicit" });
node.connect(tap);
const per = Math.round(ctx.sampleRate / 100);
let buf = new Int16Array(per * channels);
let fill = 0;
tap.port.onmessage = (ev) => {
const blocks = ev.data;
const n = blocks[0]?.length ?? 0;
for (let i = 0; i < n; i++) {
for (let c = 0; c < channels; c++) {
const v = (blocks[c] ?? blocks[0])[i];
buf[fill * channels + c] = Math.max(-32768, Math.min(32767, Math.round(v * 32767)));
}
if (++fill === per) {
onPcm(buf, wallMicros() - 10_000);
buf = new Int16Array(per * channels);
fill = 0;
}
}
};
return () => {
node.disconnect(tap);
tap.port.onmessage = null;
};
}