element-call/src/Room.jsx

180 lines
4.9 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-08-21 00:02:47 +00:00
import React, { useEffect, useMemo, useState, useRef } from "react";
2021-07-27 19:27:59 +00:00
import styles from "./Room.module.css";
2021-08-20 23:23:12 +00:00
import { useParams, useLocation } from "react-router-dom";
2021-07-27 19:27:59 +00:00
import { useVideoRoom } from "./ConferenceCallManagerHooks";
2021-07-28 23:14:38 +00:00
import { DevTools } from "./DevTools";
2021-08-17 23:57:25 +00:00
import { VideoGrid } from "./VideoGrid";
2021-08-19 19:11:12 +00:00
import {
HangupButton,
SettingsButton,
MicButton,
VideoButton,
} from "./RoomButton";
2021-08-20 16:28:11 +00:00
import { Header, LeftNav, RightNav, CenterNav } from "./Header";
import { Button } from "./Input";
2021-07-28 23:14:38 +00:00
function useQuery() {
const location = useLocation();
return useMemo(() => new URLSearchParams(location.search), [location.search]);
}
2021-07-27 19:27:59 +00:00
export function Room({ manager }) {
const { roomId } = useParams();
2021-07-28 23:14:38 +00:00
const query = useQuery();
const {
loading,
joined,
joining,
room,
participants,
error,
joinCall,
leaveCall,
2021-08-20 21:43:16 +00:00
toggleMuteVideo,
2021-08-23 22:44:08 +00:00
toggleMuteAudio,
2021-08-20 21:43:16 +00:00
videoMuted,
2021-08-23 22:44:08 +00:00
audioMuted,
} = useVideoRoom(manager, roomId);
2021-07-29 18:24:34 +00:00
const debugStr = query.get("debug");
const [debug, setDebug] = useState(debugStr === "" || debugStr === "true");
2021-07-28 23:14:38 +00:00
useEffect(() => {
function onKeyDown(event) {
if (
document.activeElement.tagName !== "input" &&
event.code === "Backquote"
) {
setDebug((prevDebug) => !prevDebug);
}
}
window.addEventListener("keydown", onKeyDown);
return () => {
window.removeEventListener("keydown", onKeyDown);
};
}, []);
2021-07-27 19:27:59 +00:00
return (
<div className={styles.room}>
{!loading && room && (
2021-08-20 00:49:45 +00:00
<Header>
<LeftNav />
<CenterNav>
<h3>{room.name}</h3>
</CenterNav>
<RightNav>
2021-08-19 19:11:12 +00:00
<SettingsButton
title={debug ? "Disable DevTools" : "Enable DevTools"}
on={debug}
onClick={() => setDebug((debug) => !debug)}
/>
2021-08-20 00:49:45 +00:00
</RightNav>
</Header>
2021-07-27 19:27:59 +00:00
)}
{loading && (
<div className={styles.centerMessage}>
<p>Loading room...</p>
</div>
)}
{error && <div className={styles.centerMessage}>{error.message}</div>}
{!loading && room && !joined && (
2021-08-21 00:02:47 +00:00
<JoinRoom
manager={manager}
joining={joining}
joinCall={joinCall}
toggleMuteVideo={toggleMuteVideo}
2021-08-23 22:44:08 +00:00
toggleMuteAudio={toggleMuteAudio}
2021-08-21 00:02:47 +00:00
videoMuted={videoMuted}
2021-08-23 22:44:08 +00:00
audioMuted={audioMuted}
2021-08-21 00:02:47 +00:00
/>
2021-07-27 19:27:59 +00:00
)}
{!loading && room && joined && participants.length === 0 && (
<div className={styles.centerMessage}>
<p>Waiting for other participants...</p>
</div>
)}
{!loading && room && joined && participants.length > 0 && (
<VideoGrid participants={participants} />
2021-07-27 19:27:59 +00:00
)}
2021-07-27 20:26:18 +00:00
{!loading && room && joined && (
<div className={styles.footer}>
2021-08-23 22:44:08 +00:00
<MicButton muted={audioMuted} onClick={toggleMuteAudio} />
2021-08-20 21:43:16 +00:00
<VideoButton enabled={videoMuted} onClick={toggleMuteVideo} />
2021-08-19 19:11:12 +00:00
<HangupButton onClick={leaveCall} />
2021-07-27 20:26:18 +00:00
</div>
)}
2021-07-28 23:14:38 +00:00
{debug && <DevTools manager={manager} />}
2021-07-27 19:27:59 +00:00
</div>
);
}
2021-08-21 00:02:47 +00:00
function JoinRoom({
joining,
joinCall,
manager,
toggleMuteVideo,
2021-08-23 22:44:08 +00:00
toggleMuteAudio,
2021-08-21 00:02:47 +00:00
videoMuted,
2021-08-23 22:44:08 +00:00
audioMuted,
2021-08-21 00:02:47 +00:00
}) {
const videoRef = useRef();
const [hasPermissions, setHasPermissions] = useState(false);
const [needsPermissions, setNeedsPermissions] = useState(false);
useEffect(() => {
manager
2021-08-21 00:02:47 +00:00
.getLocalVideoStream()
.then((stream) => {
if (videoRef.current) {
videoRef.current.srcObject = stream;
videoRef.current.play();
setHasPermissions(true);
}
})
.catch(() => {
if (videoRef.current) {
setNeedsPermissions(true);
}
});
}, [manager]);
return (
<div className={styles.joinRoom}>
<div className={styles.preview}>
{needsPermissions && (
<p className={styles.webcamPermissions}>
Webcam permissions needed to join the call.
</p>
)}
<video ref={videoRef} muted playsInline disablePictureInPicture />
</div>
{hasPermissions && (
<div className={styles.previewButtons}>
2021-08-23 22:44:08 +00:00
<MicButton muted={audioMuted} onClick={toggleMuteAudio} />
2021-08-21 00:02:47 +00:00
<VideoButton enabled={videoMuted} onClick={toggleMuteVideo} />
</div>
)}
<Button disabled={!hasPermissions || joining} onClick={joinCall}>
Join Call
</Button>
</div>
);
}