Files
element-call/src/video-grid/VideoTile.tsx
T

274 lines
8.7 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
Copyright 2022-2023 New Vector Ltd
2022-05-04 17:09:48 +01:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2023-09-17 14:35:35 -04:00
import {
ComponentProps,
forwardRef,
useCallback,
useEffect,
useState,
} from "react";
import { animated } from "@react-spring/web";
2022-04-07 14:22:36 -07:00
import classNames from "classnames";
2022-10-10 09:19:10 -04:00
import { useTranslation } from "react-i18next";
import { LocalParticipant, RemoteParticipant, Track } from "livekit-client";
import {
ConnectionQualityIndicator,
VideoTrack,
useMediaTrack,
} from "@livekit/components-react";
import {
RoomMember,
RoomMemberEvent,
} from "matrix-js-sdk/src/models/room-member";
2023-09-27 19:06:10 -04:00
import MicOnSolidIcon from "@vector-im/compound-design-tokens/icons/mic-on-solid.svg?react";
import MicOffSolidIcon from "@vector-im/compound-design-tokens/icons/mic-off-solid.svg?react";
2023-11-20 19:04:53 -05:00
import ErrorIcon from "@vector-im/compound-design-tokens/icons/error.svg?react";
import { Text, Tooltip } from "@vector-im/compound-web";
2022-08-12 19:27:34 +02:00
import { Avatar } from "../Avatar";
2022-04-07 14:22:36 -07:00
import styles from "./VideoTile.module.css";
import { useReactiveState } from "../useReactiveState";
2023-07-11 12:04:01 +02:00
import { AudioButton, FullscreenButton } from "../button/Button";
import { VideoTileSettingsModal } from "./VideoTileSettingsModal";
2023-11-20 19:04:53 -05:00
import { MatrixInfo } from "../room/VideoPreview";
2022-04-07 14:22:36 -07:00
2023-06-13 12:33:46 -04:00
export interface ItemData {
id: string;
member?: RoomMember;
2023-06-13 12:33:46 -04:00
sfuParticipant: LocalParticipant | RemoteParticipant;
content: TileContent;
}
2022-04-07 14:22:36 -07:00
2023-06-06 13:16:36 +02:00
export enum TileContent {
UserMedia = "user-media",
ScreenShare = "screen-share",
}
2022-08-12 19:27:34 +02:00
interface Props {
2023-06-13 12:33:46 -04:00
data: ItemData;
maximised: boolean;
fullscreen: boolean;
onToggleFullscreen: (itemId: string) => void;
// TODO: Refactor these props.
targetWidth: number;
targetHeight: number;
className?: string;
style?: ComponentProps<typeof animated.div>["style"];
showSpeakingIndicator: boolean;
showConnectionStats: boolean;
2023-11-20 19:04:53 -05:00
// TODO: This dependency in particular should probably not be here. We can fix
// this with a view model.
matrixInfo: MatrixInfo;
2022-08-12 19:27:34 +02:00
}
export const VideoTile = forwardRef<HTMLDivElement, Props>(
(
{
data,
maximised,
fullscreen,
onToggleFullscreen,
className,
style,
targetWidth,
targetHeight,
showSpeakingIndicator,
showConnectionStats,
2023-11-20 19:04:53 -05:00
matrixInfo,
},
2023-10-11 10:42:04 -04:00
tileRef,
) => {
2022-10-10 09:19:10 -04:00
const { t } = useTranslation();
const { content, sfuParticipant, member } = data;
// Handle display name changes.
const [displayName, setDisplayName] = useReactiveState(
() => member?.rawDisplayName ?? "[👻]",
2023-10-11 10:42:04 -04:00
[member],
);
useEffect(() => {
if (member) {
2023-09-22 18:05:13 -04:00
const updateName = (): void => {
setDisplayName(member.rawDisplayName);
};
member!.on(RoomMemberEvent.Name, updateName);
2023-09-22 18:05:13 -04:00
return (): void => {
member!.removeListener(RoomMemberEvent.Name, updateName);
};
}
2023-06-20 12:13:27 -04:00
}, [member, setDisplayName]);
2023-06-13 12:33:46 -04:00
2023-11-20 19:04:53 -05:00
const audioInfo = useMediaTrack(
content === TileContent.UserMedia
? Track.Source.Microphone
: Track.Source.ScreenShareAudio,
sfuParticipant,
);
const videoInfo = useMediaTrack(
content === TileContent.UserMedia
? Track.Source.Camera
: Track.Source.ScreenShare,
sfuParticipant,
);
const muted = audioInfo.isMuted !== false;
const encrypted =
audioInfo.publication?.isEncrypted !== false &&
videoInfo.publication?.isEncrypted !== false;
2023-09-13 16:19:29 -04:00
const MicIcon = muted ? MicOffSolidIcon : MicOnSolidIcon;
const onFullscreen = useCallback(() => {
onToggleFullscreen(data.id);
}, [data, onToggleFullscreen]);
2023-09-17 14:35:35 -04:00
const [videoTileSettingsModalOpen, setVideoTileSettingsModalOpen] =
useState(false);
const openVideoTileSettingsModal = useCallback(
() => setVideoTileSettingsModalOpen(true),
2023-10-11 10:42:04 -04:00
[setVideoTileSettingsModalOpen],
2023-09-17 14:35:35 -04:00
);
const closeVideoTileSettingsModal = useCallback(
() => setVideoTileSettingsModalOpen(false),
2023-10-11 10:42:04 -04:00
[setVideoTileSettingsModalOpen],
2023-09-17 14:35:35 -04:00
);
2023-07-11 12:04:01 +02:00
const toolbarButtons: JSX.Element[] = [];
if (!sfuParticipant.isLocal) {
2023-07-25 17:17:49 +02:00
toolbarButtons.push(
<AudioButton
key="localVolume"
className={styles.button}
volume={(sfuParticipant as RemoteParticipant).getVolume() ?? 0}
2023-09-17 14:35:35 -04:00
onPress={openVideoTileSettingsModal}
2023-10-11 10:42:04 -04:00
/>,
2023-07-25 17:17:49 +02:00
);
if (content === TileContent.ScreenShare) {
toolbarButtons.push(
<FullscreenButton
key="fullscreen"
className={styles.button}
fullscreen={fullscreen}
onPress={onFullscreen}
2023-10-11 10:42:04 -04:00
/>,
);
}
}
2022-05-31 10:43:05 -04:00
return (
<animated.div
className={classNames(styles.videoTile, className, {
[styles.isLocal]: sfuParticipant.isLocal,
[styles.speaking]:
sfuParticipant.isSpeaking &&
content === TileContent.UserMedia &&
showSpeakingIndicator,
[styles.screenshare]: content === TileContent.ScreenShare,
[styles.maximised]: maximised,
2022-05-31 10:43:05 -04:00
})}
2023-06-13 12:33:46 -04:00
style={style}
ref={tileRef}
2023-05-02 17:33:56 +01:00
data-testid="videoTile"
2022-05-31 10:43:05 -04:00
>
{toolbarButtons.length > 0 && (!maximised || fullscreen) && (
<div className={classNames(styles.toolbar)}>{toolbarButtons}</div>
)}
2023-09-18 21:05:01 -04:00
{content === TileContent.UserMedia &&
!sfuParticipant.isCameraEnabled && (
<>
<div className={styles.videoMutedOverlay} />
<Avatar
key={member?.userId}
id={member?.userId ?? displayName}
name={displayName}
size={Math.round(Math.min(targetWidth, targetHeight) / 2)}
src={member?.getMxcAvatarUrl()}
className={styles.avatar}
/>
</>
)}
{content === TileContent.ScreenShare ? (
<div className={styles.presenterLabel}>
2023-11-20 13:00:43 +00:00
<span>{t("video_tile.presenter_label", { displayName })}</span>
</div>
) : (
2023-09-13 16:19:29 -04:00
<div className={styles.nameTag}>
<MicIcon
width={20}
height={20}
2023-11-20 13:00:43 +00:00
aria-label={muted ? t("microphone_off") : t("microphone_on")}
2023-09-13 16:19:29 -04:00
data-muted={muted}
2023-11-20 19:04:53 -05:00
className={styles.muteIcon}
2023-09-13 16:19:29 -04:00
/>
<Text as="span" size="sm" weight="medium">
2023-11-20 13:38:01 +00:00
{sfuParticipant.isLocal
? t("video_tile.sfu_participant_local")
: displayName}
2023-09-13 16:19:29 -04:00
</Text>
2023-11-20 19:04:53 -05:00
{matrixInfo.roomEncrypted && !encrypted && (
<Tooltip label={t("common.unencrypted")} side="bottom">
<ErrorIcon
width={20}
height={20}
aria-label={t("common.unencrypted")}
className={styles.errorIcon}
// Make the icon focusable so that the tooltip can be opened
// with keyboard navigation
// TODO: Replace this with the solution from
// https://github.com/vector-im/compound-web/pull/130 once it
// lands
tabIndex={0}
/>
</Tooltip>
)}
{showConnectionStats && (
<ConnectionQualityIndicator participant={sfuParticipant} />
)}
</div>
)}
2023-06-06 13:16:36 +02:00
<VideoTrack
participant={sfuParticipant}
source={
content === TileContent.UserMedia
? Track.Source.Camera
: Track.Source.ScreenShare
}
// There's no reason for this to be focusable
tabIndex={-1}
// React supports the disablePictureInPicture attribute, but Firefox
// only recognizes a value of "true", whereas React sets it to the empty
// string. So we need to bypass React and set it specifically to "true".
// https://bugzilla.mozilla.org/show_bug.cgi?id=1865748
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line react/no-unknown-property
disablepictureinpicture="true"
2023-06-06 13:16:36 +02:00
/>
2023-09-17 14:35:35 -04:00
{!maximised && (
2023-07-11 12:04:01 +02:00
<VideoTileSettingsModal
data={data}
2023-09-17 14:35:35 -04:00
open={videoTileSettingsModalOpen}
onDismiss={closeVideoTileSettingsModal}
2023-07-11 12:04:01 +02:00
/>
)}
2022-05-31 10:43:05 -04:00
</animated.div>
);
2023-10-11 10:42:04 -04:00
},
2022-05-31 10:43:05 -04:00
);