2022-05-04 17:09:48 +01:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2022-05-04 17:09:48 +01:00
|
|
|
|
2024-09-06 10:22:13 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2022-05-04 17:09:48 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-10-28 17:29:26 -04:00
|
|
|
import { useEffect, useRef, FC, ReactNode } from "react";
|
2022-08-02 00:46:16 +02:00
|
|
|
import useMeasure from "react-use-measure";
|
2024-11-01 17:26:14 -04:00
|
|
|
import { facingModeFromLocalTrack, LocalVideoTrack } from "livekit-client";
|
2023-09-18 15:45:48 -04:00
|
|
|
import classNames from "classnames";
|
2022-08-02 00:46:16 +02:00
|
|
|
|
2022-04-27 15:18:55 -07:00
|
|
|
import { Avatar } from "../Avatar";
|
|
|
|
|
import styles from "./VideoPreview.module.css";
|
2023-08-02 15:29:37 -04:00
|
|
|
import { MuteStates } from "./MuteStates";
|
2024-04-23 15:15:13 +02:00
|
|
|
import { 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;
|
2023-07-24 17:06:09 -04:00
|
|
|
roomAlias: string | null;
|
2023-09-08 15:39:10 -04:00
|
|
|
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;
|
2023-08-02 15:29:37 -04:00
|
|
|
muteStates: MuteStates;
|
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,
|
|
|
|
|
muteStates,
|
2024-10-28 17:29:26 -04:00
|
|
|
videoTrack,
|
2023-09-18 15:45:48 -04:00
|
|
|
children,
|
|
|
|
|
}) => {
|
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
|
|
|
|
2023-06-30 18:21:18 -04:00
|
|
|
useEffect(() => {
|
2023-07-07 14:41:29 +02:00
|
|
|
// 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 => {
|
2023-07-07 14:41:29 +02:00
|
|
|
videoTrack?.detach();
|
2023-05-26 20:41:32 +02:00
|
|
|
};
|
2023-07-07 14:41:29 +02:00
|
|
|
}, [videoTrack]);
|
2023-05-26 20:41:32 +02:00
|
|
|
|
2024-07-03 15:08:30 -04:00
|
|
|
return (
|
|
|
|
|
<div className={classNames(styles.preview)} ref={previewRef}>
|
2023-11-20 20:42:27 -05:00
|
|
|
<video
|
2024-11-01 17:26:14 -04:00
|
|
|
className={
|
|
|
|
|
videoTrack &&
|
|
|
|
|
facingModeFromLocalTrack(videoTrack).facingMode === "user"
|
|
|
|
|
? styles.mirror
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
2023-11-20 20:42:27 -05:00
|
|
|
ref={videoEl}
|
|
|
|
|
muted
|
|
|
|
|
playsInline
|
|
|
|
|
// There's no reason for this to be focusable
|
|
|
|
|
tabIndex={-1}
|
2024-02-01 14:06:53 -05:00
|
|
|
disablePictureInPicture
|
2023-11-20 20:42:27 -05:00
|
|
|
/>
|
2023-09-18 15:45:48 -04:00
|
|
|
{!muteStates.video.enabled && (
|
|
|
|
|
<div className={styles.avatarContainer}>
|
|
|
|
|
<Avatar
|
|
|
|
|
id={matrixInfo.userId}
|
|
|
|
|
name={matrixInfo.displayName}
|
|
|
|
|
size={Math.min(previewBounds.width, previewBounds.height) / 2}
|
|
|
|
|
src={matrixInfo.avatarUrl}
|
2023-08-30 21:58:29 -04:00
|
|
|
/>
|
2023-05-26 20:41:32 +02:00
|
|
|
</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
|
|
|
};
|