Files
piik/scripts/client-native-sfu-page.ts
T
AAA 5781d6bbfa feat(client): complete native source and release boundary
Complete the Native Client source/audio boundary, strict v5 loopback contract, Browser media lifecycle handling, and non-blocking release checks. Keep unverified platform/physical gates explicit; do not rewrite the Go Server.
2026-09-04 03:42:54 +08:00

141 lines
4.6 KiB
TypeScript

import { DEFAULT_QUALITY_SETTINGS } from "../src/shared/protocol";
import { NativeClient } from "../src/client/native/client";
import { NativeMediaBridge } from "../src/client/native/media-bridge";
import { SfuPublisher } from "../src/client/sfu/publisher";
let hostResources: {
client: NativeClient;
bridge: NativeMediaBridge;
publisher: SfuPublisher;
shareId: string;
} | null = null;
let viewerRoom: import("livekit-client").Room | null = null;
export async function startNativeSfuHost(input: {
url: string;
token: string;
sourceTitle: string;
}): Promise<{ audio: boolean }> {
if (hostResources) throw new Error("native SFU Host is already active");
const client = await NativeClient.connect();
if (!client) throw new Error("native Client is unavailable");
const shareId = "native_sfu_share_123456";
let bridge: NativeMediaBridge | null = null;
let publisher: SfuPublisher | null = null;
try {
const adapters = await client.captureOptions();
const adapter = adapters.find((candidate) => candidate.hardwareH264.length > 0);
const encoder = adapter?.hardwareH264[0];
const sources = await client.sources();
const target = sources.find((candidate) =>
candidate.kind === "window" &&
candidate.title.includes(input.sourceTitle)
);
if (!adapter || !encoder || !target) {
throw new Error("native capture path is unavailable");
}
const started = await client.startShare({
shareId,
source: target,
audio: true,
adapterIndex: adapter.index,
encoderIndex: encoder.index,
edgeCapacity: 3,
});
bridge = new NativeMediaBridge(
shareId,
client,
() => undefined,
started.audio,
);
const stream = await bridge.start();
publisher = new SfuPublisher();
if (!(await publisher.connect({ url: input.url, token: input.token }))) {
throw new Error("native SFU publisher did not connect");
}
if (!(await publisher.activate(stream, DEFAULT_QUALITY_SETTINGS, "h264"))) {
throw new Error("native SFU publisher did not activate");
}
hostResources = { client, bridge, publisher, shareId };
return { audio: stream.getAudioTracks().length > 0 };
} catch (error) {
await publisher?.disconnect().catch(() => undefined);
bridge?.dispose();
await client.stopShare(shareId).catch(() => undefined);
client.close();
throw error;
}
}
export async function startNativeSfuViewer(input: {
url: string;
token: string;
}): Promise<{ frames: number; width: number; height: number }> {
if (viewerRoom) throw new Error("native SFU Viewer is already active");
const sdk = await import("livekit-client");
const room = new sdk.Room({
adaptiveStream: false,
disconnectOnPageLeave: false,
});
viewerRoom = room;
const result = new Promise<{ frames: number; width: number; height: number }>(
(resolve, reject) => {
const timer = window.setTimeout(
() => reject(new Error("native SFU Viewer timed out")),
20_000,
);
room.on(sdk.RoomEvent.TrackSubscribed, (track, publication) => {
if (track.kind !== sdk.Track.Kind.Video) return;
publication.setVideoQuality(sdk.VideoQuality.HIGH);
const video = track.attach() as HTMLVideoElement;
video.muted = true;
video.autoplay = true;
document.body.append(video);
void video.play().catch(() => undefined);
let frames = 0;
const next = () => {
video.requestVideoFrameCallback(() => {
if (video.videoWidth === 1280 && video.videoHeight === 720) {
frames += 1;
} else {
frames = 0;
}
if (frames < 30) {
next();
return;
}
window.clearTimeout(timer);
resolve({
frames,
width: video.videoWidth,
height: video.videoHeight,
});
});
};
next();
});
},
);
try {
await room.connect(input.url, input.token);
return await result;
} catch (error) {
await room.disconnect(false).catch(() => undefined);
viewerRoom = null;
throw error;
}
}
export async function stopNativeSfuGate(): Promise<void> {
const viewer = viewerRoom;
viewerRoom = null;
if (viewer) await viewer.disconnect(false).catch(() => undefined);
const host = hostResources;
hostResources = null;
if (!host) return;
await host.publisher.disconnect().catch(() => undefined);
host.bridge.dispose();
await host.client.stopShare(host.shareId).catch(() => undefined);
host.client.close();
}