element-call/src/Room.jsx

486 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-12-07 02:00:34 +00:00
import { useLocation, useParams, useHistory, Link } from "react-router-dom";
2021-08-19 19:11:12 +00:00
import {
2021-12-07 19:59:57 +00:00
Button,
CopyButton,
2021-08-19 19:11:12 +00:00
HangupButton,
MicButton,
VideoButton,
2021-10-01 18:24:49 +00:00
ScreenshareButton,
2021-12-07 19:59:57 +00:00
} from "./button";
2021-12-03 01:21:37 +00:00
import {
Header,
LeftNav,
RightNav,
RoomHeaderInfo,
RoomSetupHeaderInfo,
2021-12-08 01:59:55 +00:00
HeaderLogo,
2021-12-03 01:21:37 +00:00
} from "./Header";
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-12-09 20:58:30 +00:00
import { useClient, useLoadGroupCall } 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-12-04 00:42:29 +00:00
import { OverflowMenu } from "./OverflowMenu";
import { GridLayoutMenu } from "./GridLayoutMenu";
import { UserMenu } from "./UserMenu";
2021-12-10 22:44:04 +00:00
import classNames from "classnames";
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-12-09 20:58:30 +00:00
export function Room() {
const [registeringGuest, setRegisteringGuest] = useState(false);
const [registrationError, setRegistrationError] = useState();
const { loading, isAuthenticated, error, client, registerGuest } =
useClient();
2021-07-27 19:27:59 +00:00
2021-09-29 17:50:39 +00:00
useEffect(() => {
2021-12-09 20:58:30 +00:00
if (!loading && !isAuthenticated) {
setRegisteringGuest(true);
registerGuest()
.then(() => {
setRegisteringGuest(false);
})
.catch((error) => {
setRegistrationError(error);
setRegisteringGuest(false);
});
}
}, [loading, isAuthenticated]);
if (loading || registeringGuest) {
return <div>Loading...</div>;
}
2021-07-27 19:27:59 +00:00
2021-12-09 20:58:30 +00:00
if (registrationError || error) {
2021-12-11 00:22:42 +00:00
return (
<div className={styles.room}>
<ErrorModal error={registrationError || error} />
</div>
);
2021-12-09 20:58:30 +00:00
}
return <GroupCall client={client} />;
2021-09-10 19:20:17 +00:00
}
2021-12-09 20:58:30 +00:00
export function GroupCall({ 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) {
2021-12-11 00:22:42 +00:00
return <LoadingRoomView />;
2021-09-29 17:50:39 +00:00
}
2021-09-01 23:42:01 +00:00
2021-09-29 17:50:39 +00:00
if (error) {
return (
<div className={styles.room}>
2021-12-03 01:21:37 +00:00
<Header>
<LeftNav>
<HeaderLogo />
</LeftNav>
</Header>
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 (
2021-12-10 22:44:04 +00:00
<GroupCallView
client={client}
roomId={roomId}
groupCall={groupCall}
simpleGrid={simpleGrid}
/>
2021-09-29 17:50:39 +00:00
);
2021-09-10 19:20:17 +00:00
}
2021-12-10 21:44:06 +00:00
export function GroupCallView({ client, roomId, groupCall, simpleGrid }) {
2021-12-07 02:00:34 +00:00
const [showInspector, setShowInspector] = useState(false);
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-12-03 01:21:37 +00:00
return (
2021-12-10 22:44:04 +00:00
<div className={styles.room}>
2021-12-03 01:21:37 +00:00
<Header>
<LeftNav>
<HeaderLogo />
</LeftNav>
</Header>
<ErrorModal error={error} />
2021-12-10 22:44:04 +00:00
</div>
2021-12-03 01:21:37 +00:00
);
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-12-07 02:00:34 +00:00
setShowInspector={setShowInspector}
showInspector={showInspector}
2021-12-10 21:44:06 +00:00
roomId={roomId}
2021-09-29 17:50:39 +00:00
/>
);
} else if (state === GroupCallState.Entering) {
return <EnteringRoomView />;
} else {
return (
<RoomSetupView
2021-11-29 22:35:32 +00:00
client={client}
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-12-07 02:00:34 +00:00
setShowInspector={setShowInspector}
showInspector={showInspector}
2021-12-10 21:44:06 +00:00
roomId={roomId}
2021-09-29 17:50:39 +00:00
/>
);
}
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 (
2021-12-10 22:44:04 +00:00
<div className={styles.room}>
2021-09-10 19:20:17 +00:00
<div className={styles.centerMessage}>
<p>Loading room...</p>
</div>
2021-12-10 22:44:04 +00:00
</div>
2021-09-10 19:20:17 +00:00
);
}
2021-09-03 22:45:07 +00:00
2021-09-10 19:20:17 +00:00
export function EnteringRoomView() {
return (
2021-12-10 22:44:04 +00:00
<div className={styles.room}>
2021-09-10 19:20:17 +00:00
<div className={styles.centerMessage}>
<p>Entering room...</p>
</div>
2021-12-10 22:44:04 +00:00
</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({
2021-11-29 22:35:32 +00:00
client,
2021-09-10 19:20:17 +00:00
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-12-07 02:00:34 +00:00
setShowInspector,
showInspector,
2021-12-10 21:44:06 +00:00
roomId,
2021-08-21 00:02:47 +00:00
}) {
2021-12-03 01:21:37 +00:00
const history = useHistory();
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-12-10 22:44:04 +00:00
<div className={styles.room}>
2021-12-03 01:21:37 +00:00
<Header>
<LeftNav>
<RoomSetupHeaderInfo
2021-12-07 19:59:57 +00:00
onPress={() => history.goBack()}
2021-12-03 01:21:37 +00:00
roomName={roomName}
/>
</LeftNav>
<RightNav>
2021-12-09 20:58:30 +00:00
<UserMenu />
2021-12-03 01:21:37 +00:00
</RightNav>
</Header>
2021-09-10 19:20:17 +00:00
<div className={styles.joinRoom}>
2021-12-10 23:07:39 +00:00
<div className={styles.joinRoomContent}>
<h1>New Call</h1>
{hasLocalParticipant && (
<p>Warning, you are signed into this call on another device.</p>
2021-12-07 02:00:34 +00:00
)}
2021-12-10 23:07:39 +00:00
<div className={styles.preview}>
{state === GroupCallState.LocalCallFeedUninitialized && (
<p className={styles.webcamPermissions}>
Webcam/microphone permissions needed to join the call.
</p>
)}
{state === GroupCallState.InitializingLocalCallFeed && (
<p className={styles.webcamPermissions}>
Accept webcam/microphone permissions to join the call.
</p>
)}
<video ref={videoRef} muted playsInline disablePictureInPicture />
{state === GroupCallState.LocalCallFeedInitialized && (
<>
<Button
className={styles.joinCallButton}
disabled={state !== GroupCallState.LocalCallFeedInitialized}
onPress={onEnter}
>
Join call now
</Button>
<div className={styles.previewButtons}>
<MicButton
muted={microphoneMuted}
onPress={toggleMicrophoneMuted}
/>
<VideoButton
muted={localVideoMuted}
onPress={toggleLocalVideoMuted}
/>
<OverflowMenu
roomId={roomId}
setShowInspector={setShowInspector}
showInspector={showInspector}
client={client}
/>
</div>
</>
)}
</div>
<p>Or</p>
<CopyButton
value={window.location.href}
className={styles.copyButton}
>
Copy call link and join later
</CopyButton>
</div>
<div className={styles.joinRoomFooter}>
<Link className={styles.homeLink} to="/">
Take me Home
</Link>
2021-09-10 19:20:17 +00:00
</div>
2021-08-21 00:02:47 +00:00
</div>
2021-12-10 22:44:04 +00:00
</div>
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-12-07 02:00:34 +00:00
setShowInspector,
showInspector,
2021-12-10 21:44:06 +00:00
roomId,
2021-09-10 19:20:17 +00:00
}) {
2021-12-03 01:21:37 +00:00
const [layout, setLayout] = useVideoGridLayout();
2021-09-29 17:50:39 +00:00
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) => {
2021-12-03 01:21:37 +00:00
if (layout === "freedom") {
2021-10-15 00:06:52 +00:00
return tiles.map((tile) => {
if (tile === focusedTile) {
return { ...tile, presenter: !tile.presenter };
}
return tile;
});
} else {
2021-12-03 01:21:37 +00:00
setLayout("spotlight");
2021-10-15 00:06:52 +00:00
return tiles.map((tile) => {
if (tile === focusedTile) {
return { ...tile, presenter: true };
}
return { ...tile, presenter: false };
});
}
},
2021-12-03 01:21:37 +00:00
[layout, setLayout]
2021-10-15 00:06:52 +00:00
);
2021-09-10 19:20:17 +00:00
return (
2021-12-10 22:44:04 +00:00
<div className={classNames(styles.room, styles.inRoom)}>
2021-12-03 01:21:37 +00:00
<Header>
<LeftNav>
<RoomHeaderInfo roomName={roomName} />
</LeftNav>
<RightNav>
2021-12-04 00:42:29 +00:00
<GridLayoutMenu layout={layout} setLayout={setLayout} />
2021-12-09 20:58:30 +00:00
<UserMenu />
2021-12-03 01:21:37 +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-12-04 00:42:29 +00:00
<MicButton muted={microphoneMuted} onPress={toggleMicrophoneMuted} />
2021-12-06 20:19:38 +00:00
<VideoButton muted={localVideoMuted} onPress={toggleLocalVideoMuted} />
2021-10-26 19:48:24 +00:00
{canScreenshare && !isSafari && (
<ScreenshareButton
enabled={isScreensharing}
2021-12-04 00:42:29 +00:00
onPress={toggleScreensharing}
/>
)}
2021-12-07 01:34:10 +00:00
<OverflowMenu
2021-12-10 21:44:06 +00:00
roomId={roomId}
2021-12-07 01:34:10 +00:00
setShowInspector={setShowInspector}
showInspector={showInspector}
client={client}
/>
2021-12-04 00:42:29 +00:00
<HangupButton onPress={onLeave} />
2021-09-10 19:20:17 +00:00
</div>
2021-10-15 23:41:23 +00:00
<GroupCallInspector
client={client}
groupCall={groupCall}
show={showInspector}
/>
2021-12-10 22:44:04 +00:00
</div>
2021-08-21 00:02:47 +00:00
);
}