2026-11-02 20:15:49 +01:00
|
|
|
import { execSync } from "child_process";
|
2024-12-12 15:42:09 +01:00
|
|
|
import { execCommandInBackground } from "./helper";
|
|
|
|
|
|
|
|
|
|
const LIVEKIT_URL = "ws://localhost:7880";
|
|
|
|
|
const LIVEKIT_API_KEY = "devkey";
|
|
|
|
|
const LIVEKIT_API_SECRET = "secret";
|
|
|
|
|
|
|
|
|
|
const LK_CLI = "lk";
|
|
|
|
|
|
|
|
|
|
export class Livekit {
|
2026-10-24 14:39:17 +02:00
|
|
|
/** Joins a participant that publishes the demo tracks and subscribes to the others; returns the pid of the CLI. */
|
2024-12-12 15:42:09 +01:00
|
|
|
static joinParticipantToRoom(participantIdentity: string, roomName: string): number {
|
|
|
|
|
const args = [
|
|
|
|
|
"room",
|
|
|
|
|
"join",
|
|
|
|
|
"--url",
|
|
|
|
|
LIVEKIT_URL,
|
|
|
|
|
"--api-key",
|
|
|
|
|
LIVEKIT_API_KEY,
|
|
|
|
|
"--api-secret",
|
|
|
|
|
LIVEKIT_API_SECRET,
|
|
|
|
|
"--publish-demo",
|
2026-10-24 14:39:17 +02:00
|
|
|
"--auto-subscribe",
|
2024-12-12 15:42:09 +01:00
|
|
|
"--identity",
|
|
|
|
|
participantIdentity,
|
|
|
|
|
roomName
|
|
|
|
|
];
|
|
|
|
|
const pid = execCommandInBackground(LK_CLI, args);
|
|
|
|
|
if (!pid) {
|
|
|
|
|
throw new Error("Error joining participant to room");
|
|
|
|
|
}
|
|
|
|
|
return pid;
|
|
|
|
|
}
|
2026-11-02 20:15:49 +01:00
|
|
|
|
|
|
|
|
/** Whether LiveKit lists a participant with this identity in the room; false when the room does not exist. */
|
|
|
|
|
static isParticipantInRoom(participantIdentity: string, roomName: string): boolean {
|
|
|
|
|
const args = ["--url", LIVEKIT_URL, "--api-key", LIVEKIT_API_KEY, "--api-secret", LIVEKIT_API_SECRET];
|
|
|
|
|
try {
|
|
|
|
|
return execSync(`${LK_CLI} room participants list ${args.join(" ")} ${roomName}`, { stdio: "pipe" })
|
|
|
|
|
.toString()
|
|
|
|
|
.includes(participantIdentity);
|
|
|
|
|
} catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-12 15:42:09 +01:00
|
|
|
}
|