element-call/src/Room.jsx

284 lines
7.1 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-09-29 17:50:39 +00:00
import React, { useEffect, useMemo, useState } from "react";
2021-07-27 19:27:59 +00:00
import styles from "./Room.module.css";
2021-09-29 17:50:39 +00:00
import { 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-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-09-29 17:50:39 +00:00
import { Button, ErrorMessage } 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-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-07-28 23:14:38 +00:00
2021-09-29 17:50:39 +00:00
function useLoadGroupCall(client, roomId) {
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-09-29 23:23:18 +00:00
fetchGroupCall(client, roomId, 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 }) {
const { roomId } = useParams();
const { loading, error, groupCall } = useLoadGroupCall(client, roomId);
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}>
<LoadingErrorView error={error} />
</div>
);
}
2021-09-01 23:42:01 +00:00
2021-09-29 17:50:39 +00:00
return (
<div className={styles.room}>
<GroupCallView groupCall={groupCall} />
</div>
);
2021-09-10 19:20:17 +00:00
}
2021-09-29 17:50:39 +00:00
export function GroupCallView({ groupCall }) {
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-29 17:50:39 +00:00
} = useGroupCall(groupCall);
2021-09-10 19:20:17 +00:00
2021-09-29 17:50:39 +00:00
if (error) {
return <LoadingErrorView error={error} />;
} else if (state === GroupCallState.Entered) {
return (
<InRoomView
roomName={groupCall.room.name}
microphoneMuted={microphoneMuted}
localVideoMuted={localVideoMuted}
toggleLocalVideoMuted={toggleLocalVideoMuted}
toggleMicrophoneMuted={toggleMicrophoneMuted}
userMediaFeeds={userMediaFeeds}
activeSpeaker={activeSpeaker}
onLeave={leave}
/>
);
} else if (state === GroupCallState.Entering) {
return <EnteringRoomView />;
} else {
return (
<RoomSetupView
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
export function LoadingErrorView({ error }) {
2021-09-10 23:05:57 +00:00
useEffect(() => {
console.error(error);
}, [error]);
2021-09-03 22:45:07 +00:00
return (
<>
2021-09-10 19:20:17 +00:00
<div className={styles.centerMessage}>
<ErrorMessage>{error.message}</ErrorMessage>
</div>
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-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}>
<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}>
Webcam permissions needed to join the call.
2021-09-30 18:39:57 +00:00
</p>
)}
{state === GroupCallState.InitializingLocalCallFeed && (
<p className={styles.webcamPermissions}>
Accept Webcam 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
</>
);
}
function InRoomView({
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-29 17:50:39 +00:00
const [layout, toggleLayout] = useVideoGridLayout();
const items = useMemo(
() =>
userMediaFeeds.map((callFeed) => ({
id: callFeed.userId,
callFeed,
isActiveSpeaker: callFeed.userId === activeSpeaker,
})),
[userMediaFeeds, activeSpeaker]
);
2021-09-10 19:20:17 +00:00
return (
<>
<Header>
<LeftNav />
<CenterNav>
<h3>{roomName}</h3>
</CenterNav>
<RightNav>
<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-09-10 19:20:17 +00:00
) : (
2021-09-29 17:50:39 +00:00
<VideoGrid items={items} layout={layout} />
2021-08-21 00:02:47 +00:00
)}
2021-09-10 19:20:17 +00:00
<div className={styles.footer}>
<MicButton muted={microphoneMuted} onClick={toggleMicrophoneMuted} />
<VideoButton
enabled={localVideoMuted}
onClick={toggleLocalVideoMuted}
/>
<HangupButton onClick={onLeave} />
</div>
</>
2021-08-21 00:02:47 +00:00
);
}