2022-05-04 17:09:48 +01:00
|
|
|
/*
|
2023-05-05 11:44:35 +02: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-18 15:45:48 -04:00
|
|
|
import { useEffect, useMemo, useRef, FC, ReactNode } from "react";
|
2022-08-02 00:46:16 +02:00
|
|
|
import useMeasure from "react-use-measure";
|
|
|
|
|
import { ResizeObserver } from "@juggle/resize-observer";
|
2023-07-07 14:41:29 +02:00
|
|
|
import { usePreviewTracks } from "@livekit/components-react";
|
2023-08-02 15:29:37 -04:00
|
|
|
import {
|
|
|
|
|
CreateLocalTracksOptions,
|
|
|
|
|
LocalVideoTrack,
|
|
|
|
|
Track,
|
|
|
|
|
} from "livekit-client";
|
2023-09-18 15:45:48 -04:00
|
|
|
import classNames from "classnames";
|
2023-09-25 18:04:34 +01:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-10-13 14:40:24 -04:00
|
|
|
import { Glass } from "@vector-im/compound-web";
|
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 { useMediaDevices } from "../livekit/MediaDevicesContext";
|
|
|
|
|
import { MuteStates } from "./MuteStates";
|
2023-09-18 15:45:48 -04:00
|
|
|
import { useMediaQuery } from "../useMediaQuery";
|
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;
|
|
|
|
|
roomEncrypted: boolean;
|
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;
|
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,
|
|
|
|
|
children,
|
|
|
|
|
}) => {
|
2022-04-27 15:18:55 -07:00
|
|
|
const [previewRef, previewBounds] = useMeasure({ polyfill: ResizeObserver });
|
|
|
|
|
|
2023-08-02 15:29:37 -04:00
|
|
|
const devices = useMediaDevices();
|
|
|
|
|
|
2023-09-05 17:34:55 +01:00
|
|
|
// Capture the audio options as they were when we first mounted, because
|
|
|
|
|
// we're not doing anything with the audio anyway so we don't need to
|
|
|
|
|
// re-open the devices when they change (see below).
|
2023-08-02 15:29:37 -04:00
|
|
|
const initialAudioOptions = useRef<CreateLocalTracksOptions["audio"]>();
|
|
|
|
|
initialAudioOptions.current ??= muteStates.audio.enabled && {
|
|
|
|
|
deviceId: devices.audioInput.selectedId,
|
|
|
|
|
};
|
2023-07-07 14:41:29 +02:00
|
|
|
|
|
|
|
|
const tracks = usePreviewTracks(
|
|
|
|
|
{
|
2023-08-02 15:29:37 -04:00
|
|
|
// The only reason we request audio here is to get the audio permission
|
|
|
|
|
// request over with at the same time. But changing the audio settings
|
|
|
|
|
// shouldn't cause this hook to recreate the track, which is why we
|
|
|
|
|
// reference the initial values here.
|
2023-09-05 17:34:55 +01:00
|
|
|
// We also pass in a clone because livekit mutates the object passed in,
|
|
|
|
|
// which would cause the devices to be re-opened on the next render.
|
|
|
|
|
audio: Object.assign({}, initialAudioOptions.current),
|
2023-08-02 15:29:37 -04:00
|
|
|
video: muteStates.video.enabled && {
|
|
|
|
|
deviceId: devices.videoInput.selectedId,
|
|
|
|
|
},
|
2023-07-07 14:41:29 +02:00
|
|
|
},
|
|
|
|
|
(error) => {
|
2023-09-25 18:04:34 +01:00
|
|
|
logger.error("Error while creating preview Tracks:", error);
|
2023-10-11 10:42:04 -04:00
|
|
|
},
|
2023-06-16 18:07:13 +02:00
|
|
|
);
|
2023-08-02 15:29:37 -04:00
|
|
|
const videoTrack = useMemo(
|
2023-07-07 14:41:29 +02:00
|
|
|
() =>
|
2023-08-02 15:29:37 -04:00
|
|
|
tracks?.find((t) => t.kind === Track.Kind.Video) as
|
|
|
|
|
| LocalVideoTrack
|
|
|
|
|
| undefined,
|
2023-10-11 10:42:04 -04:00
|
|
|
[tracks],
|
2023-06-16 18:07:13 +02:00
|
|
|
);
|
2023-07-10 12:26:47 +02: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
|
|
|
}
|
|
|
|
|
return () => {
|
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
|
|
|
|
2023-09-18 15:45:48 -04:00
|
|
|
const content = (
|
|
|
|
|
<>
|
2023-07-07 14:41:29 +02:00
|
|
|
<video ref={videoEl} muted playsInline disablePictureInPicture />
|
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>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return useMediaQuery("(max-width: 550px)") ? (
|
|
|
|
|
<div
|
|
|
|
|
className={classNames(styles.preview, styles.content)}
|
|
|
|
|
ref={previewRef}
|
|
|
|
|
>
|
|
|
|
|
{content}
|
2022-04-27 15:18:55 -07:00
|
|
|
</div>
|
2023-09-18 15:45:48 -04:00
|
|
|
) : (
|
|
|
|
|
<Glass className={styles.preview}>
|
|
|
|
|
<div className={styles.content} ref={previewRef}>
|
|
|
|
|
{content}
|
|
|
|
|
</div>
|
|
|
|
|
</Glass>
|
2022-04-27 15:18:55 -07:00
|
|
|
);
|
2023-08-02 15:29:37 -04:00
|
|
|
};
|