2026-01-09 12:00:45 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2026 Element Creations Ltd.
|
|
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-01-09 13:12:19 +01:00
|
|
|
import { logger } from "matrix-js-sdk/lib/logger";
|
|
|
|
|
|
2026-01-09 12:00:45 +01:00
|
|
|
import { type UrlParams } from "../UrlParams.ts";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculates the initial mute state for media devices based on configuration.
|
|
|
|
|
*
|
|
|
|
|
* It is not always possible to start the widget with audio/video unmuted due to privacy concerns.
|
|
|
|
|
* This function encapsulates the logic to determine the appropriate initial state.
|
|
|
|
|
*/
|
|
|
|
|
export function calculateInitialMuteState(
|
|
|
|
|
urlParams: Pick<UrlParams, "skipLobby" | "callIntent">,
|
|
|
|
|
packageType: "full" | "embedded",
|
|
|
|
|
hostname: string | undefined = undefined,
|
2026-01-09 14:18:14 +01:00
|
|
|
trustLocalhost: boolean = import.meta.env.DEV || !!process.env.CI,
|
2026-01-09 12:00:45 +01:00
|
|
|
): { audioEnabled: boolean; videoEnabled: boolean } {
|
|
|
|
|
const { skipLobby, callIntent } = urlParams;
|
|
|
|
|
|
2026-01-09 13:12:19 +01:00
|
|
|
logger.debug(
|
2026-01-09 14:18:14 +01:00
|
|
|
`calculateInitialMuteState: skipLobby=${skipLobby}, callIntent=${callIntent}, hostname=${hostname}, isDevBuild=${trustLocalhost}`,
|
2026-01-09 13:12:19 +01:00
|
|
|
);
|
|
|
|
|
|
2026-01-09 12:00:45 +01:00
|
|
|
const isTrustedHost =
|
|
|
|
|
packageType == "embedded" ||
|
|
|
|
|
// Trust local hosts in dev mode to make local testing easier
|
2026-01-09 14:18:14 +01:00
|
|
|
(hostname == "localhost" && trustLocalhost);
|
2026-01-09 12:00:45 +01:00
|
|
|
|
|
|
|
|
if (skipLobby && !isTrustedHost) {
|
|
|
|
|
// If host not trusted and lobby skipped, default to muted to protect user privacy.
|
|
|
|
|
// This prevents users from inadvertently joining with active audio/video
|
|
|
|
|
// when browser permissions were previously granted in a different context.
|
|
|
|
|
return {
|
|
|
|
|
audioEnabled: false,
|
|
|
|
|
videoEnabled: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Embedded contexts are trusted environments, so they allow unmuted by default.
|
|
|
|
|
// Same for when showing a lobby, as users can adjust their settings there.
|
|
|
|
|
// Additionally, if the call intent is "audio", we disable video by default.
|
|
|
|
|
return {
|
|
|
|
|
audioEnabled: true,
|
|
|
|
|
videoEnabled: callIntent != "audio",
|
|
|
|
|
};
|
|
|
|
|
}
|