2022-05-04 16:09:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-04-29 00:44:50 +00:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2022-05-06 10:32:09 +00:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { GroupCall } from "matrix-js-sdk/src/webrtc/groupCall";
|
2022-05-10 16:18:26 +00:00
|
|
|
import { CallFeed, CallFeedEvent } from "matrix-js-sdk/src/webrtc/callFeed";
|
2022-05-11 15:28:08 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
|
|
|
import { PlayClipFunction, PTTClipID } from "../sound/usePttSounds";
|
2022-05-06 10:32:09 +00:00
|
|
|
|
|
|
|
export interface PTTState {
|
|
|
|
pttButtonHeld: boolean;
|
|
|
|
isAdmin: boolean;
|
|
|
|
talkOverEnabled: boolean;
|
|
|
|
setTalkOverEnabled: (boolean) => void;
|
|
|
|
activeSpeakerUserId: string;
|
|
|
|
startTalking: () => void;
|
|
|
|
stopTalking: () => void;
|
2022-05-11 15:28:08 +00:00
|
|
|
transmitBlocked: boolean;
|
2022-05-06 10:32:09 +00:00
|
|
|
}
|
2022-04-29 00:44:50 +00:00
|
|
|
|
2022-05-06 10:32:09 +00:00
|
|
|
export const usePTT = (
|
|
|
|
client: MatrixClient,
|
|
|
|
groupCall: GroupCall,
|
2022-05-11 15:28:08 +00:00
|
|
|
userMediaFeeds: CallFeed[],
|
|
|
|
playClip: PlayClipFunction
|
2022-05-06 10:32:09 +00:00
|
|
|
): PTTState => {
|
2022-04-29 00:44:50 +00:00
|
|
|
const [
|
2022-05-06 20:27:07 +00:00
|
|
|
{
|
|
|
|
pttButtonHeld,
|
|
|
|
isAdmin,
|
|
|
|
talkOverEnabled,
|
|
|
|
activeSpeakerUserId,
|
2022-05-11 15:28:08 +00:00
|
|
|
transmitBlocked,
|
2022-05-06 20:27:07 +00:00
|
|
|
},
|
2022-04-29 00:44:50 +00:00
|
|
|
setState,
|
|
|
|
] = useState(() => {
|
|
|
|
const roomMember = groupCall.room.getMember(client.getUserId());
|
|
|
|
|
|
|
|
const activeSpeakerFeed = userMediaFeeds.find((f) => !f.isAudioMuted());
|
|
|
|
|
|
|
|
return {
|
|
|
|
isAdmin: roomMember.powerLevel >= 100,
|
|
|
|
talkOverEnabled: false,
|
|
|
|
pttButtonHeld: false,
|
|
|
|
activeSpeakerUserId: activeSpeakerFeed ? activeSpeakerFeed.userId : null,
|
2022-05-11 15:28:08 +00:00
|
|
|
transmitBlocked: false,
|
2022-04-29 00:44:50 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-06 10:32:09 +00:00
|
|
|
function onMuteStateChanged(...args): void {
|
2022-04-29 00:44:50 +00:00
|
|
|
const activeSpeakerFeed = userMediaFeeds.find((f) => !f.isAudioMuted());
|
|
|
|
|
2022-05-11 15:28:08 +00:00
|
|
|
if (activeSpeakerUserId === null && activeSpeakerFeed.userId !== null) {
|
|
|
|
if (activeSpeakerFeed.userId === client.getUserId()) {
|
|
|
|
playClip(PTTClipID.START_TALKING_LOCAL);
|
|
|
|
} else {
|
|
|
|
playClip(PTTClipID.START_TALKING_REMOTE);
|
|
|
|
}
|
|
|
|
} else if (
|
|
|
|
activeSpeakerFeed &&
|
|
|
|
activeSpeakerUserId === client.getUserId() &&
|
|
|
|
activeSpeakerFeed.userId !== client.getUserId()
|
|
|
|
) {
|
|
|
|
// We were talking but we've been cut off
|
|
|
|
playClip(PTTClipID.BLOCKED);
|
|
|
|
}
|
|
|
|
|
2022-04-29 00:44:50 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
activeSpeakerUserId: activeSpeakerFeed
|
|
|
|
? activeSpeakerFeed.userId
|
|
|
|
: null,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const callFeed of userMediaFeeds) {
|
2022-05-10 16:18:26 +00:00
|
|
|
callFeed.addListener(CallFeedEvent.MuteStateChanged, onMuteStateChanged);
|
2022-04-29 00:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const activeSpeakerFeed = userMediaFeeds.find((f) => !f.isAudioMuted());
|
|
|
|
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
activeSpeakerUserId: activeSpeakerFeed ? activeSpeakerFeed.userId : null,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
for (const callFeed of userMediaFeeds) {
|
2022-05-10 16:18:26 +00:00
|
|
|
callFeed.removeListener(
|
|
|
|
CallFeedEvent.MuteStateChanged,
|
|
|
|
onMuteStateChanged
|
|
|
|
);
|
2022-04-29 00:44:50 +00:00
|
|
|
}
|
|
|
|
};
|
2022-05-11 15:28:08 +00:00
|
|
|
}, [userMediaFeeds, activeSpeakerUserId, client, playClip]);
|
2022-04-29 00:44:50 +00:00
|
|
|
|
2022-05-05 11:21:46 +00:00
|
|
|
const startTalking = useCallback(async () => {
|
2022-05-11 15:28:08 +00:00
|
|
|
if (pttButtonHeld) return;
|
|
|
|
|
|
|
|
let blocked = false;
|
|
|
|
if (!activeSpeakerUserId || (isAdmin && talkOverEnabled)) {
|
2022-04-29 17:56:17 +00:00
|
|
|
if (groupCall.isMicrophoneMuted()) {
|
2022-05-05 11:21:46 +00:00
|
|
|
try {
|
|
|
|
await groupCall.setMicrophoneMuted(false);
|
|
|
|
} catch (e) {
|
2022-05-11 15:28:08 +00:00
|
|
|
logger.error("Failed to unmute microphone", e);
|
2022-05-05 11:21:46 +00:00
|
|
|
}
|
2022-04-29 17:56:17 +00:00
|
|
|
}
|
2022-05-11 15:28:08 +00:00
|
|
|
} else {
|
|
|
|
playClip(PTTClipID.BLOCKED);
|
|
|
|
blocked = true;
|
2022-04-29 17:56:17 +00:00
|
|
|
}
|
2022-05-11 15:28:08 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
pttButtonHeld: true,
|
|
|
|
transmitBlocked: blocked,
|
|
|
|
}));
|
|
|
|
}, [
|
|
|
|
pttButtonHeld,
|
|
|
|
groupCall,
|
|
|
|
activeSpeakerUserId,
|
|
|
|
isAdmin,
|
|
|
|
talkOverEnabled,
|
|
|
|
setState,
|
|
|
|
playClip,
|
|
|
|
]);
|
2022-04-29 17:56:17 +00:00
|
|
|
|
|
|
|
const stopTalking = useCallback(() => {
|
2022-05-11 15:28:08 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
pttButtonHeld: false,
|
|
|
|
unmuteError: null,
|
|
|
|
}));
|
2022-05-05 11:21:46 +00:00
|
|
|
|
2022-04-29 17:56:17 +00:00
|
|
|
if (!groupCall.isMicrophoneMuted()) {
|
|
|
|
groupCall.setMicrophoneMuted(true);
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:28:08 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
pttButtonHeld: false,
|
|
|
|
transmitBlocked: false,
|
|
|
|
}));
|
2022-05-05 12:15:07 +00:00
|
|
|
}, [groupCall]);
|
2022-04-29 17:56:17 +00:00
|
|
|
|
2022-04-29 00:44:50 +00:00
|
|
|
useEffect(() => {
|
2022-05-06 10:32:09 +00:00
|
|
|
function onKeyDown(event: KeyboardEvent): void {
|
2022-04-29 00:44:50 +00:00
|
|
|
if (event.code === "Space") {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-05-05 11:21:46 +00:00
|
|
|
if (pttButtonHeld) return;
|
|
|
|
|
2022-04-29 17:56:17 +00:00
|
|
|
startTalking();
|
2022-04-29 00:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 10:32:09 +00:00
|
|
|
function onKeyUp(event: KeyboardEvent): void {
|
2022-04-29 00:44:50 +00:00
|
|
|
if (event.code === "Space") {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-04-29 17:56:17 +00:00
|
|
|
stopTalking();
|
2022-04-29 00:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 10:32:09 +00:00
|
|
|
function onBlur(): void {
|
2022-04-29 00:44:50 +00:00
|
|
|
// TODO: We will need to disable this for a global PTT hotkey to work
|
|
|
|
if (!groupCall.isMicrophoneMuted()) {
|
|
|
|
groupCall.setMicrophoneMuted(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState((prevState) => ({ ...prevState, pttButtonHeld: false }));
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("keydown", onKeyDown);
|
2022-05-11 15:28:08 +00:00
|
|
|
//window.addEventListener("keyup", onKeyUp);
|
|
|
|
//window.addEventListener("blur", onBlur);
|
2022-04-29 00:44:50 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("keydown", onKeyDown);
|
|
|
|
window.removeEventListener("keyup", onKeyUp);
|
|
|
|
window.removeEventListener("blur", onBlur);
|
|
|
|
};
|
2022-05-06 20:34:58 +00:00
|
|
|
}, [
|
|
|
|
groupCall,
|
|
|
|
startTalking,
|
|
|
|
stopTalking,
|
|
|
|
activeSpeakerUserId,
|
|
|
|
isAdmin,
|
|
|
|
talkOverEnabled,
|
|
|
|
pttButtonHeld,
|
|
|
|
]);
|
2022-04-29 00:44:50 +00:00
|
|
|
|
|
|
|
const setTalkOverEnabled = useCallback((talkOverEnabled) => {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
talkOverEnabled,
|
|
|
|
}));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
|
|
|
pttButtonHeld,
|
|
|
|
isAdmin,
|
|
|
|
talkOverEnabled,
|
|
|
|
setTalkOverEnabled,
|
|
|
|
activeSpeakerUserId,
|
2022-04-29 17:56:17 +00:00
|
|
|
startTalking,
|
|
|
|
stopTalking,
|
2022-05-11 15:28:08 +00:00
|
|
|
transmitBlocked,
|
2022-04-29 00:44:50 +00:00
|
|
|
};
|
2022-05-06 10:32:09 +00:00
|
|
|
};
|