2022-05-04 17:09:48 +01:00
|
|
|
/*
|
2023-06-12 18:06:18 -04: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-06-26 13:48:41 -04:00
|
|
|
import React, { useCallback } from "react";
|
2023-06-12 18:06:18 -04:00
|
|
|
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";
|
2023-06-02 14:49:11 +02:00
|
|
|
import { LocalParticipant, RemoteParticipant, Track } from "livekit-client";
|
2023-06-02 20:15:29 +02:00
|
|
|
import {
|
|
|
|
ConnectionQualityIndicator,
|
|
|
|
VideoTrack,
|
|
|
|
useMediaTrack,
|
|
|
|
} from "@livekit/components-react";
|
2023-06-14 17:12:03 +02:00
|
|
|
import {
|
|
|
|
RoomMember,
|
|
|
|
RoomMemberEvent,
|
|
|
|
} from "matrix-js-sdk/src/models/room-member";
|
2022-08-12 19:27:34 +02:00
|
|
|
|
2023-06-14 17:12:03 +02:00
|
|
|
import { Avatar } from "../Avatar";
|
2022-04-07 14:22:36 -07:00
|
|
|
import styles from "./VideoTile.module.css";
|
2023-05-12 14:32:16 -04:00
|
|
|
import { ReactComponent as MicIcon } from "../icons/Mic.svg";
|
2022-04-07 14:22:36 -07:00
|
|
|
import { ReactComponent as MicMutedIcon } from "../icons/MicMuted.svg";
|
2023-06-18 14:27:15 -04:00
|
|
|
import { useReactiveState } from "../useReactiveState";
|
2023-06-26 13:48:41 -04:00
|
|
|
import { FullscreenButton } from "../button/Button";
|
2022-04-07 14:22:36 -07:00
|
|
|
|
2023-06-13 12:33:46 -04:00
|
|
|
export interface ItemData {
|
2023-06-26 13:48:41 -04:00
|
|
|
id: string;
|
2023-06-14 17:12:03 +02:00
|
|
|
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;
|
2023-06-26 13:48:41 -04:00
|
|
|
maximised: boolean;
|
|
|
|
fullscreen: boolean;
|
|
|
|
onToggleFullscreen: (itemId: string) => void;
|
2023-06-14 17:12:03 +02:00
|
|
|
// TODO: Refactor these props.
|
2023-06-12 18:06:18 -04:00
|
|
|
targetWidth: number;
|
|
|
|
targetHeight: number;
|
2023-06-13 17:23:42 +02:00
|
|
|
className?: string;
|
2023-06-14 17:12:03 +02:00
|
|
|
style?: React.ComponentProps<typeof animated.div>["style"];
|
2023-06-16 10:35:29 -04:00
|
|
|
showSpeakingIndicator: boolean;
|
2023-06-16 10:59:57 -04:00
|
|
|
showConnectionStats: boolean;
|
2022-08-12 19:27:34 +02:00
|
|
|
}
|
|
|
|
|
2023-06-14 17:12:03 +02:00
|
|
|
export const VideoTile = React.forwardRef<HTMLDivElement, Props>(
|
2023-06-16 10:35:29 -04:00
|
|
|
(
|
|
|
|
{
|
|
|
|
data,
|
2023-06-26 13:48:41 -04:00
|
|
|
maximised,
|
|
|
|
fullscreen,
|
|
|
|
onToggleFullscreen,
|
2023-06-16 10:35:29 -04:00
|
|
|
className,
|
|
|
|
style,
|
|
|
|
targetWidth,
|
|
|
|
targetHeight,
|
|
|
|
showSpeakingIndicator,
|
2023-06-16 10:59:57 -04:00
|
|
|
showConnectionStats,
|
2023-06-16 10:35:29 -04:00
|
|
|
},
|
|
|
|
tileRef
|
|
|
|
) => {
|
2022-10-10 09:19:10 -04:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-06-14 17:12:03 +02:00
|
|
|
const { content, sfuParticipant, member } = data;
|
|
|
|
|
|
|
|
// Handle display name changes.
|
2023-06-18 14:27:15 -04:00
|
|
|
const [displayName, setDisplayName] = useReactiveState(
|
|
|
|
() => member?.rawDisplayName ?? "[👻]",
|
|
|
|
[member]
|
|
|
|
);
|
2023-06-14 17:12:03 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (member) {
|
|
|
|
const updateName = () => {
|
|
|
|
setDisplayName(member.rawDisplayName);
|
|
|
|
};
|
|
|
|
|
|
|
|
member!.on(RoomMemberEvent.Name, updateName);
|
|
|
|
return () => {
|
|
|
|
member!.removeListener(RoomMemberEvent.Name, updateName);
|
|
|
|
};
|
|
|
|
}
|
2023-06-20 12:13:27 -04:00
|
|
|
}, [member, setDisplayName]);
|
2023-06-13 12:33:46 -04:00
|
|
|
|
2023-06-02 14:49:11 +02:00
|
|
|
const { isMuted: microphoneMuted } = useMediaTrack(
|
2023-06-06 13:16:36 +02:00
|
|
|
content === TileContent.UserMedia
|
|
|
|
? Track.Source.Microphone
|
|
|
|
: Track.Source.ScreenShareAudio,
|
2023-06-26 13:48:41 -04:00
|
|
|
sfuParticipant
|
2023-06-02 14:49:11 +02:00
|
|
|
);
|
2022-10-21 17:24:56 +01:00
|
|
|
|
2023-06-26 13:48:41 -04:00
|
|
|
const onFullscreen = useCallback(() => {
|
|
|
|
onToggleFullscreen(data.id);
|
|
|
|
}, [data, onToggleFullscreen]);
|
|
|
|
|
|
|
|
const toolbarButtons: JSX.Element[] = [];
|
|
|
|
if (!sfuParticipant.isLocal) {
|
|
|
|
// TODO local volume option, which would also go here
|
|
|
|
|
|
|
|
if (content === TileContent.ScreenShare) {
|
|
|
|
toolbarButtons.push(
|
|
|
|
<FullscreenButton
|
|
|
|
key="fullscreen"
|
|
|
|
className={styles.button}
|
|
|
|
fullscreen={fullscreen}
|
|
|
|
onPress={onFullscreen}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 18:06:18 -04:00
|
|
|
// 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, {
|
2023-06-02 14:49:11 +02:00
|
|
|
[styles.isLocal]: sfuParticipant.isLocal,
|
2023-06-16 10:35:29 -04:00
|
|
|
[styles.speaking]:
|
|
|
|
sfuParticipant.isSpeaking &&
|
|
|
|
content === TileContent.UserMedia &&
|
|
|
|
showSpeakingIndicator,
|
2023-06-02 14:49:11 +02:00
|
|
|
[styles.muted]: microphoneMuted,
|
2023-06-13 14:39:05 +02:00
|
|
|
[styles.screenshare]: content === TileContent.ScreenShare,
|
2023-06-26 13:48:41 -04:00
|
|
|
[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
|
|
|
>
|
2023-06-26 13:48:41 -04:00
|
|
|
{toolbarButtons.length > 0 && (!maximised || fullscreen) && (
|
|
|
|
<div className={classNames(styles.toolbar)}>{toolbarButtons}</div>
|
|
|
|
)}
|
2023-06-14 17:12:03 +02:00
|
|
|
{content === TileContent.UserMedia && !sfuParticipant.isCameraEnabled && (
|
2022-05-31 10:43:05 -04:00
|
|
|
<>
|
|
|
|
<div className={styles.videoMutedOverlay} />
|
2023-06-14 17:12:03 +02:00
|
|
|
<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
|
|
|
</>
|
|
|
|
)}
|
2023-06-26 13:48:41 -04:00
|
|
|
{content === TileContent.ScreenShare ? (
|
2023-06-13 17:23:42 +02:00
|
|
|
<div className={styles.presenterLabel}>
|
2023-06-14 17:12:03 +02:00
|
|
|
<span>{t("{{displayName}} is presenting", { displayName })}</span>
|
2023-06-13 17:23:42 +02:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className={classNames(styles.infoBubble, styles.memberName)}>
|
2023-06-18 14:28:28 -04:00
|
|
|
{microphoneMuted === false ? <MicIcon /> : <MicMutedIcon />}
|
2023-06-14 17:12:03 +02:00
|
|
|
<span title={displayName}>{displayName}</span>
|
2023-06-16 10:59:57 -04:00
|
|
|
{showConnectionStats && (
|
|
|
|
<ConnectionQualityIndicator participant={sfuParticipant} />
|
|
|
|
)}
|
2023-06-13 17:23:42 +02:00
|
|
|
</div>
|
|
|
|
)}
|
2023-06-06 13:16:36 +02:00
|
|
|
<VideoTrack
|
|
|
|
participant={sfuParticipant}
|
|
|
|
source={
|
|
|
|
content === TileContent.UserMedia
|
|
|
|
? Track.Source.Camera
|
|
|
|
: Track.Source.ScreenShare
|
|
|
|
}
|
|
|
|
/>
|
2022-05-31 10:43:05 -04:00
|
|
|
</animated.div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|