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

163 lines
4.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.
*/
import React 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";
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 { ReactComponent as MicIcon } from "../icons/Mic.svg";
2022-04-07 14:22:36 -07:00
import { ReactComponent as MicMutedIcon } from "../icons/MicMuted.svg";
import { useReactiveState } from "../useReactiveState";
2022-04-07 14:22:36 -07:00
export interface ItemData {
member?: RoomMember;
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 {
data: ItemData;
// TODO: Refactor these props.
targetWidth: number;
targetHeight: number;
className?: string;
style?: React.ComponentProps<typeof animated.div>["style"];
showSpeakingIndicator: boolean;
showConnectionStats: boolean;
2022-08-12 19:27:34 +02:00
}
export const VideoTile = React.forwardRef<HTMLDivElement, Props>(
(
{
data,
className,
style,
targetWidth,
targetHeight,
showSpeakingIndicator,
showConnectionStats,
},
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 ?? "[👻]",
[member]
);
React.useEffect(() => {
if (member) {
const updateName = () => {
setDisplayName(member.rawDisplayName);
};
member!.on(RoomMemberEvent.Name, updateName);
return () => {
member!.removeListener(RoomMemberEvent.Name, updateName);
};
}
}, [member]);
const audioEl = React.useRef<HTMLAudioElement>(null);
const { isMuted: microphoneMuted } = useMediaTrack(
2023-06-06 13:16:36 +02:00
content === TileContent.UserMedia
? Track.Source.Microphone
: Track.Source.ScreenShareAudio,
sfuParticipant,
{
element: audioEl,
}
);
// Firefox doesn't respect the disablePictureInPicture attribute
// https://bugzilla.mozilla.org/show_bug.cgi?id=1611831
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.muted]: microphoneMuted,
[styles.screenshare]: content === TileContent.ScreenShare,
2022-05-31 10:43:05 -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
>
{content === TileContent.UserMedia && !sfuParticipant.isCameraEnabled && (
2022-05-31 10:43:05 -04:00
<>
<div className={styles.videoMutedOverlay} />
<Avatar
key={member?.userId}
size={Math.round(Math.min(targetWidth, targetHeight) / 2)}
src={member?.getMxcAvatarUrl()}
fallback={displayName.slice(0, 1).toUpperCase()}
className={styles.avatar}
/>
2022-05-31 10:43:05 -04:00
</>
)}
{content == TileContent.ScreenShare ? (
<div className={styles.presenterLabel}>
<span>{t("{{displayName}} is presenting", { displayName })}</span>
</div>
) : (
<div className={classNames(styles.infoBubble, styles.memberName)}>
{microphoneMuted === false ? <MicIcon /> : <MicMutedIcon />}
<span title={displayName}>{displayName}</span>
{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
}
/>
<audio ref={audioEl} />
2022-05-31 10:43:05 -04:00
</animated.div>
);
}
);