Files
BetterDesk/web-nodejs/lib/remoteViewerPrefs.js
UNITRONIX a8f32a845f feat: enhance multi-session viewer functionality
- Added session management features to synchronize media capture and input handling across active tabs.
- Updated audio handling to respect session activity and mute states.
- Improved clipboard functionality to only allow copying to local clipboard from the active session.
- Refactored input capture logic to ensure it only activates for the active viewer tab.
- Introduced a new `syncSessionMediaCapture` function to manage media input across sessions.
2026-06-14 10:09:59 +02:00

99 lines
2.4 KiB
JavaScript

'use strict';
/** FPS sent to the peer for inactive viewer tabs (RustDesk customFps / CDAP quality_set). */
const BACKGROUND_FPS = 1;
const QUALITY_TO_PRESET = {
Best: 'best',
Balanced: 'balanced',
Low: 'speed',
};
const PRESET_FPS = {
best: 60,
balanced: 30,
quality: 30,
speed: 60,
};
const DEFAULTS = {
quality: 'Best',
scale: 'fit',
codec: 'Auto',
adaptiveQuality: true,
backgroundFps: BACKGROUND_FPS,
};
function sanitizePrefs(raw) {
const source = raw && typeof raw === 'object' ? raw : {};
const clean = { ...DEFAULTS };
if (['Best', 'Balanced', 'Low'].includes(source.quality)) {
clean.quality = source.quality;
}
if (['fit', 'fill', '1:1', 'stretch'].includes(source.scale)) {
clean.scale = source.scale;
}
if (typeof source.codec === 'string' && source.codec.length <= 16) {
clean.codec = source.codec;
}
if (typeof source.adaptiveQuality === 'boolean') {
clean.adaptiveQuality = source.adaptiveQuality;
}
const bg = Number(source.backgroundFps);
if (Number.isFinite(bg) && bg >= 1 && bg <= 5) {
clean.backgroundFps = Math.round(bg);
}
return clean;
}
function getPresetForQuality(quality) {
return QUALITY_TO_PRESET[quality] || 'balanced';
}
function getActiveFpsForQuality(quality) {
return PRESET_FPS[getPresetForQuality(quality)] || 30;
}
function storageKey(userId) {
const id = userId != null ? String(userId) : 'anonymous';
return 'betterdesk_remote_prefs_' + id;
}
function loadRemoteViewerPrefs(userId, storage) {
if (!storage || typeof storage.getItem !== 'function') {
return { ...DEFAULTS };
}
try {
const raw = storage.getItem(storageKey(userId));
if (!raw) return { ...DEFAULTS };
return sanitizePrefs(JSON.parse(raw));
} catch (_) {
return { ...DEFAULTS };
}
}
function saveRemoteViewerPrefs(userId, prefs, storage) {
const clean = sanitizePrefs(prefs);
if (storage && typeof storage.setItem === 'function') {
try {
storage.setItem(storageKey(userId), JSON.stringify(clean));
} catch (_) { /* ignore */ }
}
return clean;
}
module.exports = {
BACKGROUND_FPS,
DEFAULTS,
QUALITY_TO_PRESET,
PRESET_FPS,
sanitizePrefs,
getPresetForQuality,
getActiveFpsForQuality,
storageKey,
loadRemoteViewerPrefs,
saveRemoteViewerPrefs,
};