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-07-27 18:40:19 +00:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import { ConferenceCallManager } from "./ConferenceCallManager";
|
|
|
|
|
2021-08-05 00:40:25 +00:00
|
|
|
// https://stackoverflow.com/a/9039885
|
|
|
|
function isIOS() {
|
|
|
|
return (
|
|
|
|
[
|
|
|
|
"iPad Simulator",
|
|
|
|
"iPhone Simulator",
|
|
|
|
"iPod Simulator",
|
|
|
|
"iPad",
|
|
|
|
"iPhone",
|
|
|
|
"iPod",
|
|
|
|
].includes(navigator.platform) ||
|
|
|
|
// iPad on iOS 13 detection
|
|
|
|
(navigator.userAgent.includes("Mac") && "ontouchend" in document)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-27 18:40:19 +00:00
|
|
|
export function useConferenceCallManager(homeserverUrl) {
|
|
|
|
const [{ loading, authenticated, manager, error }, setState] = useState({
|
|
|
|
loading: true,
|
|
|
|
authenticated: false,
|
|
|
|
manager: undefined,
|
|
|
|
error: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
ConferenceCallManager.restore(homeserverUrl)
|
|
|
|
.then((manager) => {
|
|
|
|
setState({
|
|
|
|
manager,
|
|
|
|
loading: false,
|
|
|
|
authenticated: !!manager,
|
|
|
|
error: undefined,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
setState({
|
|
|
|
manager: undefined,
|
|
|
|
loading: false,
|
|
|
|
authenticated: false,
|
|
|
|
error: err,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2021-08-10 01:38:19 +00:00
|
|
|
const login = useCallback(async (username, password, cb) => {
|
2021-07-27 18:40:19 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
authenticated: false,
|
|
|
|
error: undefined,
|
|
|
|
}));
|
|
|
|
|
|
|
|
ConferenceCallManager.login(homeserverUrl, username, password)
|
|
|
|
.then((manager) => {
|
|
|
|
setState({
|
|
|
|
manager,
|
|
|
|
loading: false,
|
|
|
|
authenticated: true,
|
|
|
|
error: undefined,
|
|
|
|
});
|
2021-08-10 01:38:19 +00:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
2021-07-27 18:40:19 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
setState({
|
|
|
|
manager: undefined,
|
|
|
|
loading: false,
|
|
|
|
authenticated: false,
|
|
|
|
error: err,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2021-08-10 01:38:19 +00:00
|
|
|
const register = useCallback(async (username, password, cb) => {
|
2021-07-27 18:40:19 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
authenticated: false,
|
|
|
|
error: undefined,
|
|
|
|
}));
|
|
|
|
|
|
|
|
ConferenceCallManager.register(homeserverUrl, username, password)
|
|
|
|
.then((manager) => {
|
|
|
|
setState({
|
|
|
|
manager,
|
|
|
|
loading: false,
|
|
|
|
authenticated: true,
|
|
|
|
error: undefined,
|
|
|
|
});
|
2021-08-10 01:38:19 +00:00
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
2021-07-27 18:40:19 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
setState({
|
|
|
|
manager: undefined,
|
|
|
|
loading: false,
|
|
|
|
authenticated: false,
|
|
|
|
error: err,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2021-08-02 18:16:14 +00:00
|
|
|
useEffect(() => {
|
|
|
|
window.confManager = manager;
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.confManager = undefined;
|
|
|
|
};
|
|
|
|
}, [manager]);
|
|
|
|
|
2021-07-27 18:40:19 +00:00
|
|
|
return { loading, authenticated, manager, error, login, register };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useVideoRoom(manager, roomId, timeout = 5000) {
|
2021-08-20 21:43:16 +00:00
|
|
|
const [
|
|
|
|
{
|
|
|
|
loading,
|
|
|
|
joined,
|
|
|
|
joining,
|
|
|
|
room,
|
|
|
|
participants,
|
|
|
|
error,
|
|
|
|
videoMuted,
|
|
|
|
micMuted,
|
|
|
|
},
|
|
|
|
setState,
|
|
|
|
] = useState({
|
|
|
|
loading: true,
|
|
|
|
joining: false,
|
|
|
|
joined: false,
|
|
|
|
room: undefined,
|
|
|
|
participants: [],
|
|
|
|
error: undefined,
|
|
|
|
videoMuted: false,
|
|
|
|
micMuted: false,
|
|
|
|
});
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-07-27 22:49:00 +00:00
|
|
|
useEffect(() => {
|
2021-08-09 20:54:23 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
loading: true,
|
|
|
|
room: undefined,
|
|
|
|
error: undefined,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const onParticipantsChanged = () => {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
2021-08-17 23:46:32 +00:00
|
|
|
participants: [...manager.participants],
|
2021-08-09 20:54:23 +00:00
|
|
|
}));
|
2021-07-27 22:49:00 +00:00
|
|
|
};
|
|
|
|
|
2021-08-09 20:54:23 +00:00
|
|
|
manager.on("participants_changed", onParticipantsChanged);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
|
|
|
manager.client.joinRoom(roomId).catch((err) => {
|
|
|
|
setState((prevState) => ({ ...prevState, loading: false, error: err }));
|
|
|
|
});
|
|
|
|
|
|
|
|
let timeoutId;
|
|
|
|
|
|
|
|
function roomCallback(room) {
|
|
|
|
if (room && room.roomId === roomId) {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
manager.client.removeListener("Room", roomCallback);
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
loading: false,
|
|
|
|
room,
|
|
|
|
error: undefined,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 00:23:07 +00:00
|
|
|
let initialRoom = manager.client.getRoom(roomId);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-08-10 00:23:07 +00:00
|
|
|
if (initialRoom) {
|
2021-07-27 18:40:19 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
loading: false,
|
2021-08-10 00:23:07 +00:00
|
|
|
room: initialRoom,
|
|
|
|
error: undefined,
|
2021-07-27 18:40:19 +00:00
|
|
|
}));
|
2021-08-10 00:23:07 +00:00
|
|
|
} else {
|
|
|
|
manager.client.on("Room", roomCallback);
|
|
|
|
|
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
loading: false,
|
|
|
|
room: undefined,
|
|
|
|
error: new Error("Room could not be found."),
|
|
|
|
}));
|
|
|
|
manager.client.removeListener("Room", roomCallback);
|
|
|
|
}, timeout);
|
|
|
|
}
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-08-21 00:02:47 +00:00
|
|
|
function onLeaveCall() {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
videoMuted: manager.videoMuted,
|
|
|
|
micMuted: manager.micMuted,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.on("left", onLeaveCall);
|
|
|
|
|
2021-07-27 18:40:19 +00:00
|
|
|
return () => {
|
|
|
|
manager.client.removeListener("Room", roomCallback);
|
2021-08-09 20:54:23 +00:00
|
|
|
manager.removeListener("participants_changed", onParticipantsChanged);
|
2021-07-27 18:40:19 +00:00
|
|
|
clearTimeout(timeoutId);
|
2021-08-09 20:54:23 +00:00
|
|
|
manager.leaveCall();
|
2021-08-21 00:02:47 +00:00
|
|
|
manager.removeListener("left", onLeaveCall);
|
2021-07-27 18:40:19 +00:00
|
|
|
};
|
2021-08-09 20:54:23 +00:00
|
|
|
}, [manager, roomId]);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
|
|
|
const joinCall = useCallback(() => {
|
2021-08-09 20:54:23 +00:00
|
|
|
if (joining || joined) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-08-09 20:54:23 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
joining: true,
|
|
|
|
}));
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-08-05 17:39:49 +00:00
|
|
|
manager
|
2021-08-06 21:56:14 +00:00
|
|
|
.enter(roomId)
|
2021-08-05 17:39:49 +00:00
|
|
|
.then(() => {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
2021-08-09 20:54:23 +00:00
|
|
|
joining: false,
|
2021-08-05 17:39:49 +00:00
|
|
|
joined: true,
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
2021-08-09 20:54:23 +00:00
|
|
|
joining: false,
|
2021-08-05 17:39:49 +00:00
|
|
|
joined: false,
|
|
|
|
error,
|
|
|
|
}));
|
|
|
|
});
|
2021-08-09 20:54:23 +00:00
|
|
|
}, [manager, roomId, joining, joined]);
|
2021-07-27 18:40:19 +00:00
|
|
|
|
2021-07-27 20:26:18 +00:00
|
|
|
const leaveCall = useCallback(() => {
|
|
|
|
manager.leaveCall();
|
|
|
|
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
2021-08-17 23:46:32 +00:00
|
|
|
participants: [...manager.participants],
|
2021-07-27 20:26:18 +00:00
|
|
|
joined: false,
|
2021-08-09 20:54:23 +00:00
|
|
|
joining: false,
|
2021-07-27 20:26:18 +00:00
|
|
|
}));
|
|
|
|
}, [manager]);
|
|
|
|
|
2021-08-10 01:03:06 +00:00
|
|
|
useEffect(() => {
|
|
|
|
let pageVisibilityTimeout;
|
|
|
|
|
|
|
|
function onBeforeUnload(event) {
|
|
|
|
if (event.type === "visibilitychange") {
|
|
|
|
if (document.visibilityState === "visible") {
|
|
|
|
clearTimeout(pageVisibilityTimeout);
|
|
|
|
} else {
|
|
|
|
// Wait 5 seconds before closing the page to avoid accidentally leaving
|
|
|
|
// TODO: Make this configurable?
|
|
|
|
pageVisibilityTimeout = setTimeout(() => {
|
|
|
|
manager.leaveCall();
|
|
|
|
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
2021-08-17 23:46:32 +00:00
|
|
|
participants: [...manager.participants],
|
2021-08-10 01:03:06 +00:00
|
|
|
joined: false,
|
|
|
|
joining: false,
|
|
|
|
}));
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
manager.leaveCall();
|
|
|
|
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
2021-08-17 23:46:32 +00:00
|
|
|
participants: [...manager.participants],
|
2021-08-10 01:03:06 +00:00
|
|
|
joined: false,
|
|
|
|
joining: false,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// iOS doesn't fire beforeunload event, so leave the call when you hide the page.
|
|
|
|
if (isIOS()) {
|
|
|
|
window.addEventListener("pagehide", onBeforeUnload);
|
|
|
|
document.addEventListener("visibilitychange", onBeforeUnload);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("beforeunload", onBeforeUnload);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("pagehide", onBeforeUnload);
|
|
|
|
document.removeEventListener("visibilitychange", onBeforeUnload);
|
|
|
|
window.removeEventListener("beforeunload", onBeforeUnload);
|
|
|
|
clearTimeout(pageVisibilityTimeout);
|
|
|
|
};
|
|
|
|
}, [manager]);
|
|
|
|
|
2021-08-20 21:43:16 +00:00
|
|
|
const toggleMuteMic = useCallback(() => {
|
|
|
|
manager.setMicMuted(!manager.micMuted);
|
|
|
|
setState((prevState) => ({ ...prevState, micMuted: manager.micMuted }));
|
|
|
|
}, [manager]);
|
|
|
|
|
|
|
|
const toggleMuteVideo = useCallback(() => {
|
|
|
|
manager.setVideoMuted(!manager.videoMuted);
|
|
|
|
setState((prevState) => ({ ...prevState, videoMuted: manager.videoMuted }));
|
|
|
|
}, [manager]);
|
|
|
|
|
2021-08-09 20:54:23 +00:00
|
|
|
return {
|
|
|
|
loading,
|
|
|
|
joined,
|
|
|
|
joining,
|
|
|
|
room,
|
|
|
|
participants,
|
|
|
|
error,
|
|
|
|
joinCall,
|
|
|
|
leaveCall,
|
2021-08-20 21:43:16 +00:00
|
|
|
toggleMuteVideo,
|
|
|
|
toggleMuteMic,
|
|
|
|
videoMuted,
|
|
|
|
micMuted,
|
2021-08-09 20:54:23 +00:00
|
|
|
};
|
2021-07-27 18:40:19 +00:00
|
|
|
}
|
2021-07-27 18:55:45 +00:00
|
|
|
|
|
|
|
export function useRooms(manager) {
|
|
|
|
const [rooms, setRooms] = useState([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function updateRooms() {
|
|
|
|
setRooms(manager.client.getRooms());
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRooms();
|
|
|
|
|
|
|
|
manager.client.on("Room", updateRooms);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
manager.client.removeListener("Room", updateRooms);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return rooms;
|
|
|
|
}
|