/* Copyright 2022-2023 New Vector Ltd 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 { ComponentProps, ForwardedRef, ReactNode, forwardRef, useCallback, useEffect, useState, } from "react"; import { animated } from "@react-spring/web"; import classNames from "classnames"; import { useTranslation } from "react-i18next"; import { TrackReferenceOrPlaceholder, VideoTrack, } from "@livekit/components-react"; import { RoomMember, RoomMemberEvent, } from "matrix-js-sdk/src/models/room-member"; 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"; import ErrorIcon from "@vector-im/compound-design-tokens/icons/error.svg?react"; import MicOffIcon from "@vector-im/compound-design-tokens/icons/mic-off.svg?react"; import OverflowHorizontalIcon from "@vector-im/compound-design-tokens/icons/overflow-horizontal.svg?react"; import VolumeOnIcon from "@vector-im/compound-design-tokens/icons/volume-on.svg?react"; import VolumeOffIcon from "@vector-im/compound-design-tokens/icons/volume-off.svg?react"; import UserProfileIcon from "@vector-im/compound-design-tokens/icons/user-profile.svg?react"; import ExpandIcon from "@vector-im/compound-design-tokens/icons/expand.svg?react"; import CollapseIcon from "@vector-im/compound-design-tokens/icons/collapse.svg?react"; import { Text, Tooltip, ContextMenu, MenuItem, ToggleMenuItem, Menu, } from "@vector-im/compound-web"; import { useStateObservable } from "@react-rxjs/core"; import { Avatar } from "../Avatar"; import styles from "./VideoTile.module.css"; import { useReactiveState } from "../useReactiveState"; import { ScreenShareViewModel, MediaViewModel, UserMediaViewModel, } from "../state/MediaViewModel"; import { subscribe } from "../state/subscribe"; import { useMergedRefs } from "../useMergedRefs"; import { Slider } from "../Slider"; interface TileProps { tileRef?: ForwardedRef; className?: string; style?: ComponentProps["style"]; targetWidth: number; targetHeight: number; video: TrackReferenceOrPlaceholder; member: RoomMember | undefined; videoEnabled: boolean; maximised: boolean; unencryptedWarning: boolean; nameTagLeadingIcon?: ReactNode; nameTag: string; displayName: string; primaryButton: ReactNode; secondaryButton?: ReactNode; [k: string]: unknown; } const Tile = forwardRef( ( { tileRef = null, className, style, targetWidth, targetHeight, video, member, videoEnabled, maximised, unencryptedWarning, nameTagLeadingIcon, nameTag, displayName, primaryButton, secondaryButton, ...props }, ref, ) => { const { t } = useTranslation(); const mergedRef = useMergedRefs(tileRef, ref); return (
{video.publication !== undefined && ( )}
{nameTagLeadingIcon} {nameTag} {unencryptedWarning && ( )}
{primaryButton} {secondaryButton}
); }, ); Tile.displayName = "Tile"; interface UserMediaTileProps { vm: UserMediaViewModel; className?: string; style?: ComponentProps["style"]; targetWidth: number; targetHeight: number; nameTag: string; displayName: string; maximised: boolean; onOpenProfile: () => void; showSpeakingIndicator: boolean; } const UserMediaTile = subscribe( ( { vm, className, style, targetWidth, targetHeight, nameTag, displayName, maximised, onOpenProfile, showSpeakingIndicator, }, ref, ) => { const { t } = useTranslation(); const video = useStateObservable(vm.video); const audioEnabled = useStateObservable(vm.audioEnabled); const videoEnabled = useStateObservable(vm.videoEnabled); const unencryptedWarning = useStateObservable(vm.unencryptedWarning); const mirror = useStateObservable(vm.mirror); const speaking = useStateObservable(vm.speaking); const locallyMuted = useStateObservable(vm.locallyMuted); const cropVideo = useStateObservable(vm.cropVideo); const localVolume = useStateObservable(vm.localVolume); const onChangeMute = useCallback(() => vm.toggleLocallyMuted(), [vm]); const onChangeFitContain = useCallback(() => vm.toggleFitContain(), [vm]); const onSelectMute = useCallback((e: Event) => e.preventDefault(), []); const onSelectFitContain = useCallback( (e: Event) => e.preventDefault(), [], ); const onChangeLocalVolume = useCallback( (v: number) => vm.setLocalVolume(v), [vm], ); const MicIcon = audioEnabled ? MicOnSolidIcon : MicOffSolidIcon; const VolumeIcon = locallyMuted ? VolumeOffIcon : VolumeOnIcon; const [menuOpen, setMenuOpen] = useState(false); const menu = vm.local ? ( <> ) : ( <> {/* TODO: Figure out how to make this slider keyboard accessible */} ); const tile = ( } nameTag={nameTag} displayName={displayName} primaryButton={ } side="left" align="start" > {menu} } /> ); return ( {menu} ); }, ); UserMediaTile.displayName = "UserMediaTile"; interface ScreenShareTileProps { vm: ScreenShareViewModel; className?: string; style?: ComponentProps["style"]; targetWidth: number; targetHeight: number; nameTag: string; displayName: string; maximised: boolean; fullscreen: boolean; onToggleFullscreen: (itemId: string) => void; } const ScreenShareTile = subscribe( ( { vm, className, style, targetWidth, targetHeight, nameTag, displayName, maximised, fullscreen, onToggleFullscreen, }, ref, ) => { const { t } = useTranslation(); const video = useStateObservable(vm.video); const unencryptedWarning = useStateObservable(vm.unencryptedWarning); const onClickFullScreen = useCallback( () => onToggleFullscreen(vm.id), [onToggleFullscreen, vm], ); const FullScreenIcon = fullscreen ? CollapseIcon : ExpandIcon; return ( ) } /> ); }, ); ScreenShareTile.displayName = "ScreenShareTile"; interface Props { vm: MediaViewModel; maximised: boolean; fullscreen: boolean; onToggleFullscreen: (itemId: string) => void; onOpenProfile: () => void; targetWidth: number; targetHeight: number; className?: string; style?: ComponentProps["style"]; showSpeakingIndicator: boolean; } export const VideoTile = forwardRef( ( { vm, maximised, fullscreen, onToggleFullscreen, onOpenProfile, className, style, targetWidth, targetHeight, showSpeakingIndicator, }, ref, ) => { const { t } = useTranslation(); // Handle display name changes. // TODO: Move this into the view model const [displayName, setDisplayName] = useReactiveState( () => vm.member?.rawDisplayName ?? "[👻]", [vm.member], ); useEffect(() => { if (vm.member) { const updateName = (): void => { setDisplayName(vm.member!.rawDisplayName); }; vm.member!.on(RoomMemberEvent.Name, updateName); return (): void => { vm.member!.removeListener(RoomMemberEvent.Name, updateName); }; } }, [vm.member, setDisplayName]); const nameTag = vm.local ? t("video_tile.sfu_participant_local") : displayName; if (vm instanceof UserMediaViewModel) { return ( ); } else { return ( ); } }, ); VideoTile.displayName = "VideoTile";