Files
element-call/src/room/VideoPreview.tsx
T

100 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
Copyright 2022-2024 New Vector Ltd.
2022-05-04 17:09:48 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
2022-05-04 17:09:48 +01:00
*/
import { useEffect, useMemo, useRef, type FC, type ReactNode } from "react";
2022-08-02 00:46:16 +02:00
import useMeasure from "react-use-measure";
import { facingModeFromLocalTrack, type LocalVideoTrack } from "livekit-client";
2023-09-18 15:45:48 -04:00
import classNames from "classnames";
import { useTranslation } from "react-i18next";
2022-08-02 00:46:16 +02:00
import { TileAvatar } from "../tile/TileAvatar";
2022-04-27 15:18:55 -07:00
import styles from "./VideoPreview.module.css";
import { type EncryptionSystem } from "../e2ee/sharedKeyManagement";
2023-05-26 20:41:32 +02:00
export type MatrixInfo = {
2023-08-31 15:46:09 +02:00
userId: string;
2023-06-27 12:23:19 +01:00
displayName: string;
2023-05-26 20:41:32 +02:00
avatarUrl: string;
2023-07-04 18:46:27 +01:00
roomId: string;
2023-05-26 20:41:32 +02:00
roomName: string;
roomAlias: string | null;
roomAvatar: string | null;
2024-04-23 15:15:13 +02:00
e2eeSystem: EncryptionSystem;
2023-05-26 20:41:32 +02:00
};
2022-08-02 00:46:16 +02:00
interface Props {
2023-05-26 20:41:32 +02:00
matrixInfo: MatrixInfo;
2025-08-29 18:46:24 +02:00
videoEnabled: boolean;
2024-10-28 17:29:26 -04:00
videoTrack: LocalVideoTrack | null;
2023-09-18 15:45:48 -04:00
children: ReactNode;
2022-08-02 00:46:16 +02:00
}
2022-10-10 09:19:10 -04:00
2023-09-18 15:45:48 -04:00
export const VideoPreview: FC<Props> = ({
matrixInfo,
2025-08-29 18:46:24 +02:00
videoEnabled,
2024-10-28 17:29:26 -04:00
videoTrack,
2023-09-18 15:45:48 -04:00
children,
}) => {
const { t } = useTranslation();
2024-07-09 11:54:13 -04:00
const [previewRef, previewBounds] = useMeasure();
2022-04-27 15:18:55 -07:00
2023-08-02 15:29:37 -04:00
const videoEl = useRef<HTMLVideoElement | null>(null);
2023-06-16 18:07:13 +02:00
useEffect(() => {
// Effect to connect the videoTrack with the video element.
if (videoEl.current) {
videoTrack?.attach(videoEl.current);
2023-05-26 20:41:32 +02:00
}
2024-06-04 11:20:25 -04:00
return (): void => {
videoTrack?.detach();
2023-05-26 20:41:32 +02:00
};
}, [videoTrack]);
2023-05-26 20:41:32 +02:00
const cameraIsStarting = useMemo(
2025-08-29 18:46:24 +02:00
() => videoEnabled && !videoTrack,
[videoEnabled, videoTrack],
);
return (
<div className={classNames(styles.preview)} ref={previewRef}>
<video
className={
videoTrack &&
facingModeFromLocalTrack(videoTrack).facingMode === "user"
? styles.mirror
: undefined
}
ref={videoEl}
muted
playsInline
// There's no reason for this to be focusable
tabIndex={-1}
disablePictureInPicture
/>
2025-08-29 18:46:24 +02:00
{(!videoEnabled || cameraIsStarting) && (
<>
<div className={styles.avatarContainer}>
{cameraIsStarting && (
<div className={styles.cameraStarting} role="status">
{t("video_tile.camera_starting")}
</div>
)}
<TileAvatar
id={matrixInfo.userId}
name={matrixInfo.displayName}
size={Math.min(previewBounds.width, previewBounds.height) / 2}
src={matrixInfo.avatarUrl}
loading={cameraIsStarting}
/>
</div>
</>
2023-05-05 11:44:35 +02:00
)}
2023-09-18 15:45:48 -04:00
<div className={styles.buttonBar}>{children}</div>
2022-04-27 15:18:55 -07:00
</div>
);
2023-08-02 15:29:37 -04:00
};