element-call/src/Room.jsx

507 lines
13 KiB
React
Raw Normal View History

2021-07-27 19:27:59 +00:00
/*
Copyright 2021 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.
*/
2021-10-12 23:52:20 +00:00
import React, { useCallback, useEffect, useMemo, useState } from "react";
2021-07-27 19:27:59 +00:00
import styles from "./Room.module.css";
2021-10-06 18:19:42 +00:00
import { useLocation, useParams } from "react-router-dom";
2021-08-19 19:11:12 +00:00
import {
HangupButton,
MicButton,
VideoButton,
2021-09-01 23:42:01 +00:00
LayoutToggleButton,
2021-10-01 18:24:49 +00:00
ScreenshareButton,
2021-10-12 23:52:20 +00:00
DropdownButton,
2021-10-15 23:41:23 +00:00
SettingsButton,
2021-08-19 19:11:12 +00:00
} from "./RoomButton";
2021-08-20 16:28:11 +00:00
import { Header, LeftNav, RightNav, CenterNav } from "./Header";
2021-10-06 21:19:34 +00:00
import { Button } from "./Input";
2021-09-29 23:23:18 +00:00
import { GroupCallState } from "matrix-js-sdk/src/webrtc/groupCall";
2021-09-29 21:34:29 +00:00
import VideoGrid, {
useVideoGridLayout,
} from "matrix-react-sdk/src/components/views/voip/GroupCallView/VideoGrid";
2021-11-08 18:53:47 +00:00
import SimpleVideoGrid from "matrix-react-sdk/src/components/views/voip/GroupCallView/SimpleVideoGrid";
2021-09-29 17:50:39 +00:00
import "matrix-react-sdk/res/css/views/voip/GroupCallView/_VideoGrid.scss";
import { useGroupCall } from "matrix-react-sdk/src/hooks/useGroupCall";
import { useCallFeed } from "matrix-react-sdk/src/hooks/useCallFeed";
import { useMediaStream } from "matrix-react-sdk/src/hooks/useMediaStream";
2021-09-29 23:23:18 +00:00
import { fetchGroupCall } from "./ConferenceCallManagerHooks";
2021-10-06 21:19:34 +00:00
import { ErrorModal } from "./ErrorModal";
2021-10-15 23:41:23 +00:00
import { GroupCallInspector } from "./GroupCallInspector";
2021-11-02 18:38:47 +00:00
import * as Sentry from "@sentry/react";
2021-07-28 23:14:38 +00:00
const canScreenshare = "getDisplayMedia" in navigator.mediaDevices;
2021-10-26 19:48:24 +00:00
// There is currently a bug in Safari our our code with cloning and sending MediaStreams
// or with getUsermedia and getDisplaymedia being used within the same session.
// For now we can disable screensharing in Safari.
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
2021-11-17 23:22:27 +00:00
function useLoadGroupCall(client, roomId, viaServers) {
2021-09-29 17:50:39 +00:00
const [state, setState] = useState({
loading: true,
error: undefined,
groupCall: undefined,
});
2021-07-27 19:27:59 +00:00
2021-09-29 17:50:39 +00:00
useEffect(() => {
setState({ loading: true });
2021-11-17 23:22:27 +00:00
fetchGroupCall(client, roomId, viaServers, 30000)
2021-09-29 17:50:39 +00:00
.then((groupCall) => setState({ loading: false, groupCall }))
.catch((error) => setState({ loading: false, error }));
}, [roomId]);
2021-07-27 19:27:59 +00:00
2021-09-29 17:50:39 +00:00
return state;
2021-09-10 19:20:17 +00:00
}
2021-09-29 17:50:39 +00:00
export function Room({ client }) {
2021-10-06 18:19:42 +00:00
const { roomId: maybeRoomId } = useParams();
2021-11-08 18:53:47 +00:00
const { hash, search } = useLocation();
2021-11-17 23:22:27 +00:00
const [simpleGrid, viaServers] = useMemo(() => {
const params = new URLSearchParams(search);
return [params.has("simple"), params.getAll("via")];
}, [search]);
2021-10-06 18:19:42 +00:00
const roomId = maybeRoomId || hash;
2021-11-17 23:22:27 +00:00
const { loading, error, groupCall } = useLoadGroupCall(
client,
roomId,
viaServers
2021-11-08 18:53:47 +00:00
);
2021-09-29 17:50:39 +00:00
2021-11-01 19:37:45 +00:00
useEffect(() => {
window.groupCall = groupCall;
}, [groupCall]);
2021-09-29 17:50:39 +00:00
if (loading) {
return (
<div className={styles.room}>
<LoadingRoomView />
</div>
);
}
2021-09-01 23:42:01 +00:00
2021-09-29 17:50:39 +00:00
if (error) {
return (
<div className={styles.room}>
2021-10-06 21:19:34 +00:00
<ErrorModal error={error} />
2021-09-29 17:50:39 +00:00
</div>
);
}
2021-09-01 23:42:01 +00:00
2021-09-29 17:50:39 +00:00
return (
<div className={styles.room}>
2021-11-08 18:53:47 +00:00
<GroupCallView
client={client}
groupCall={groupCall}
simpleGrid={simpleGrid}
/>
2021-09-29 17:50:39 +00:00
</div>
);
2021-09-10 19:20:17 +00:00
}
2021-11-08 18:53:47 +00:00
export function GroupCallView({ client, groupCall, simpleGrid }) {
2021-09-10 19:20:17 +00:00
const {
2021-09-29 17:50:39 +00:00
state,
error,
activeSpeaker,
userMediaFeeds,
2021-09-10 19:20:17 +00:00
microphoneMuted,
localVideoMuted,
2021-09-29 17:50:39 +00:00
localCallFeed,
initLocalCallFeed,
2021-09-10 19:20:17 +00:00
enter,
leave,
toggleLocalVideoMuted,
toggleMicrophoneMuted,
2021-09-30 23:11:01 +00:00
toggleScreensharing,
isScreensharing,
localScreenshareFeed,
screenshareFeeds,
2021-10-21 21:22:59 +00:00
hasLocalParticipant,
2021-09-29 17:50:39 +00:00
} = useGroupCall(groupCall);
2021-09-10 19:20:17 +00:00
2021-11-02 18:38:47 +00:00
useEffect(() => {
function onHangup(call) {
if (call.hangupReason === "ice_failed") {
Sentry.captureException(new Error("Call hangup due to ICE failure."));
}
}
2021-11-15 20:05:46 +00:00
function onError(error) {
Sentry.captureException(error);
}
2021-11-02 18:38:47 +00:00
if (groupCall) {
groupCall.on("hangup", onHangup);
2021-11-15 20:05:46 +00:00
groupCall.on("error", onError);
2021-11-02 18:38:47 +00:00
}
return () => {
if (groupCall) {
groupCall.removeListener("hangup", onHangup);
2021-11-15 20:05:46 +00:00
groupCall.removeListener("error", onError);
2021-11-02 18:38:47 +00:00
}
};
}, [groupCall]);
2021-09-29 17:50:39 +00:00
if (error) {
2021-10-18 18:48:44 +00:00
return <ErrorModal error={error} />;
2021-09-29 17:50:39 +00:00
} else if (state === GroupCallState.Entered) {
return (
<InRoomView
2021-10-15 23:41:23 +00:00
groupCall={groupCall}
2021-10-12 23:52:20 +00:00
client={client}
2021-09-29 17:50:39 +00:00
roomName={groupCall.room.name}
microphoneMuted={microphoneMuted}
localVideoMuted={localVideoMuted}
toggleLocalVideoMuted={toggleLocalVideoMuted}
toggleMicrophoneMuted={toggleMicrophoneMuted}
userMediaFeeds={userMediaFeeds}
activeSpeaker={activeSpeaker}
onLeave={leave}
2021-09-30 23:11:01 +00:00
toggleScreensharing={toggleScreensharing}
isScreensharing={isScreensharing}
localScreenshareFeed={localScreenshareFeed}
screenshareFeeds={screenshareFeeds}
2021-11-08 18:53:47 +00:00
simpleGrid={simpleGrid}
2021-09-29 17:50:39 +00:00
/>
);
} else if (state === GroupCallState.Entering) {
return <EnteringRoomView />;
} else {
return (
<RoomSetupView
2021-10-21 21:22:59 +00:00
hasLocalParticipant={hasLocalParticipant}
2021-09-29 17:50:39 +00:00
roomName={groupCall.room.name}
state={state}
onInitLocalCallFeed={initLocalCallFeed}
localCallFeed={localCallFeed}
onEnter={enter}
microphoneMuted={microphoneMuted}
localVideoMuted={localVideoMuted}
toggleLocalVideoMuted={toggleLocalVideoMuted}
toggleMicrophoneMuted={toggleMicrophoneMuted}
/>
);
}
2021-07-27 19:27:59 +00:00
}
2021-08-21 00:02:47 +00:00
2021-09-10 19:20:17 +00:00
export function LoadingRoomView() {
return (
<>
<div className={styles.centerMessage}>
<p>Loading room...</p>
</div>
</>
);
}
2021-09-03 22:45:07 +00:00
2021-09-10 19:20:17 +00:00
export function EnteringRoomView() {
return (
<>
<div className={styles.centerMessage}>
<p>Entering room...</p>
</div>
</>
2021-09-03 22:45:07 +00:00
);
2021-09-10 19:20:17 +00:00
}
2021-09-03 22:45:07 +00:00
2021-09-10 19:20:17 +00:00
function RoomSetupView({
roomName,
2021-09-29 17:50:39 +00:00
state,
onInitLocalCallFeed,
2021-09-10 19:20:17 +00:00
onEnter,
2021-09-29 17:50:39 +00:00
localCallFeed,
2021-09-10 19:20:17 +00:00
microphoneMuted,
localVideoMuted,
toggleLocalVideoMuted,
toggleMicrophoneMuted,
2021-10-21 21:22:59 +00:00
hasLocalParticipant,
2021-08-21 00:02:47 +00:00
}) {
2021-09-29 17:50:39 +00:00
const { stream } = useCallFeed(localCallFeed);
const videoRef = useMediaStream(stream, true);
2021-08-21 00:02:47 +00:00
useEffect(() => {
2021-09-29 17:50:39 +00:00
onInitLocalCallFeed();
}, [onInitLocalCallFeed]);
2021-08-21 00:02:47 +00:00
return (
2021-09-10 19:20:17 +00:00
<>
<Header>
<LeftNav />
<CenterNav>
<h3>{roomName}</h3>
</CenterNav>
</Header>
<div className={styles.joinRoom}>
2021-10-21 21:22:59 +00:00
{hasLocalParticipant && (
<p>Warning, you are signed into this call on another device.</p>
)}
2021-09-10 19:20:17 +00:00
<div className={styles.preview}>
2021-09-30 18:39:57 +00:00
{state === GroupCallState.LocalCallFeedUninitialized && (
2021-09-10 19:20:17 +00:00
<p className={styles.webcamPermissions}>
2021-10-07 20:48:45 +00:00
Webcam/microphone permissions needed to join the call.
2021-09-30 18:39:57 +00:00
</p>
)}
{state === GroupCallState.InitializingLocalCallFeed && (
<p className={styles.webcamPermissions}>
2021-10-07 20:48:45 +00:00
Accept webcam/microphone permissions to join the call.
2021-09-10 19:20:17 +00:00
</p>
)}
<video ref={videoRef} muted playsInline disablePictureInPicture />
</div>
2021-09-29 17:50:39 +00:00
{state === GroupCallState.LocalCallFeedInitialized && (
2021-09-10 19:20:17 +00:00
<div className={styles.previewButtons}>
<MicButton
muted={microphoneMuted}
onClick={toggleMicrophoneMuted}
/>
<VideoButton
enabled={localVideoMuted}
onClick={toggleLocalVideoMuted}
/>
</div>
2021-08-21 00:02:47 +00:00
)}
2021-09-10 19:20:17 +00:00
<Button
2021-09-29 17:50:39 +00:00
disabled={state !== GroupCallState.LocalCallFeedInitialized}
2021-09-10 19:20:17 +00:00
onClick={onEnter}
>
Enter Call
</Button>
2021-08-21 00:02:47 +00:00
</div>
2021-09-10 19:20:17 +00:00
</>
);
}
2021-10-12 23:52:20 +00:00
function useMediaHandler(client) {
const [{ audioInput, videoInput, audioInputs, videoInputs }, setState] =
useState({
audioInput: null,
videoInput: null,
audioInputs: [],
videoInputs: [],
});
useEffect(() => {
function updateDevices() {
navigator.mediaDevices.enumerateDevices().then((devices) => {
const audioInputs = devices.filter(
(device) => device.kind === "audioinput"
);
const videoInputs = devices.filter(
(device) => device.kind === "videoinput"
);
setState((prevState) => ({
...prevState,
audioInputs,
videoInputs,
}));
});
}
updateDevices();
navigator.mediaDevices.addEventListener("devicechange", updateDevices);
return () => {
navigator.mediaDevices.removeEventListener("devicechange", updateDevices);
};
}, []);
const setAudioInput = useCallback(
(deviceId) => {
setState((prevState) => ({ ...prevState, audioInput: deviceId }));
client.getMediaHandler().setAudioInput(deviceId);
},
[client]
);
const setVideoInput = useCallback(
(deviceId) => {
setState((prevState) => ({ ...prevState, videoInput: deviceId }));
client.getMediaHandler().setVideoInput(deviceId);
},
[client]
);
return {
audioInput,
audioInputs,
setAudioInput,
videoInput,
videoInputs,
setVideoInput,
};
}
2021-10-08 00:25:40 +00:00
2021-09-10 19:20:17 +00:00
function InRoomView({
2021-10-12 23:52:20 +00:00
client,
2021-10-15 23:41:23 +00:00
groupCall,
2021-09-10 19:20:17 +00:00
roomName,
microphoneMuted,
localVideoMuted,
toggleLocalVideoMuted,
toggleMicrophoneMuted,
2021-09-29 17:50:39 +00:00
userMediaFeeds,
activeSpeaker,
2021-09-10 19:20:17 +00:00
onLeave,
2021-09-30 23:11:01 +00:00
toggleScreensharing,
isScreensharing,
screenshareFeeds,
2021-11-08 18:53:47 +00:00
simpleGrid,
2021-09-10 19:20:17 +00:00
}) {
2021-10-15 23:41:23 +00:00
const [showInspector, setShowInspector] = useState(false);
2021-09-29 17:50:39 +00:00
const [layout, toggleLayout] = useVideoGridLayout();
2021-10-08 00:25:40 +00:00
const {
2021-10-12 23:52:20 +00:00
audioInput,
audioInputs,
setAudioInput,
videoInput,
videoInputs,
setVideoInput,
} = useMediaHandler(client);
2021-10-08 00:25:40 +00:00
2021-10-15 00:24:42 +00:00
useEffect(() => {
if (screenshareFeeds.length > 0 && layout === "gallery") {
toggleLayout();
}
}, [screenshareFeeds]);
2021-09-30 23:11:01 +00:00
const items = useMemo(() => {
const participants = [];
for (const callFeed of userMediaFeeds) {
participants.push({
id: callFeed.stream.id,
2021-11-23 20:12:11 +00:00
usermediaCallFeed: callFeed,
2021-10-15 00:24:42 +00:00
isActiveSpeaker:
screenshareFeeds.length === 0
? callFeed.userId === activeSpeaker
: false,
2021-09-30 23:11:01 +00:00
});
}
for (const callFeed of screenshareFeeds) {
2021-11-23 20:12:11 +00:00
const participant = participants.find(
(p) => p.usermediaCallFeed.userId === callFeed.userId
);
participant.screenshareCallFeed = callFeed;
2021-09-30 23:11:01 +00:00
}
return participants;
}, [userMediaFeeds, activeSpeaker, screenshareFeeds]);
2021-09-10 19:20:17 +00:00
2021-10-15 00:06:52 +00:00
const onFocusTile = useCallback(
(tiles, focusedTile) => {
if (layout === "gallery") {
return tiles.map((tile) => {
if (tile === focusedTile) {
return { ...tile, presenter: !tile.presenter };
}
return tile;
});
} else {
toggleLayout();
return tiles.map((tile) => {
if (tile === focusedTile) {
return { ...tile, presenter: true };
}
return { ...tile, presenter: false };
});
}
},
[layout, toggleLayout]
);
2021-09-10 19:20:17 +00:00
return (
<>
<Header>
<LeftNav />
<CenterNav>
<h3>{roomName}</h3>
</CenterNav>
<RightNav>
2021-10-15 23:41:23 +00:00
<SettingsButton
title={showInspector ? "Hide Inspector" : "Show Inspector"}
on={showInspector}
onClick={() => setShowInspector((prev) => !prev)}
/>
2021-09-10 19:20:17 +00:00
<LayoutToggleButton
2021-09-29 17:50:39 +00:00
title={layout === "spotlight" ? "Spotlight" : "Gallery"}
layout={layout}
onClick={toggleLayout}
2021-09-10 19:20:17 +00:00
/>
</RightNav>
</Header>
2021-09-29 17:50:39 +00:00
{items.length === 0 ? (
2021-09-10 19:20:17 +00:00
<div className={styles.centerMessage}>
<p>Waiting for other participants...</p>
2021-08-21 00:02:47 +00:00
</div>
2021-11-08 18:53:47 +00:00
) : simpleGrid ? (
<SimpleVideoGrid items={items} />
2021-09-10 19:20:17 +00:00
) : (
2021-11-15 23:23:17 +00:00
<VideoGrid
items={items}
layout={layout}
onFocusTile={onFocusTile}
disableAnimations={isSafari}
/>
2021-08-21 00:02:47 +00:00
)}
2021-09-10 19:20:17 +00:00
<div className={styles.footer}>
2021-10-12 23:52:20 +00:00
<DropdownButton
value={audioInput}
onChange={({ value }) => setAudioInput(value)}
options={audioInputs.map(({ label, deviceId }) => ({
2021-10-08 00:25:40 +00:00
label,
value: deviceId,
}))}
2021-10-12 23:52:20 +00:00
>
<MicButton muted={microphoneMuted} onClick={toggleMicrophoneMuted} />
</DropdownButton>
<DropdownButton
value={videoInput}
onChange={({ value }) => setVideoInput(value)}
options={videoInputs.map(({ label, deviceId }) => ({
2021-10-08 00:25:40 +00:00
label,
value: deviceId,
}))}
2021-10-12 23:52:20 +00:00
>
<VideoButton
enabled={localVideoMuted}
onClick={toggleLocalVideoMuted}
/>
</DropdownButton>
2021-10-26 19:48:24 +00:00
{canScreenshare && !isSafari && (
<ScreenshareButton
enabled={isScreensharing}
onClick={toggleScreensharing}
/>
)}
2021-09-10 19:20:17 +00:00
<HangupButton onClick={onLeave} />
</div>
2021-10-15 23:41:23 +00:00
<GroupCallInspector
client={client}
groupCall={groupCall}
show={showInspector}
/>
2021-09-10 19:20:17 +00:00
</>
2021-08-21 00:02:47 +00:00
);
}