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-30 15:28:16 +00:00
|
|
|
import { MatrixClient, ClientEvent } from "matrix-js-sdk/src/client";
|
2022-05-06 10:32:09 +00:00
|
|
|
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";
|
2022-05-30 15:28:16 +00:00
|
|
|
import { SyncState } from "matrix-js-sdk/src/sync";
|
2022-05-11 15:28:08 +00:00
|
|
|
|
|
|
|
import { PlayClipFunction, PTTClipID } from "../sound/usePttSounds";
|
2022-05-06 10:32:09 +00:00
|
|
|
|
2022-05-13 16:58:59 +00:00
|
|
|
// Works out who the active speaker should be given what feeds are active and
|
|
|
|
// the power level of each user.
|
|
|
|
function getActiveSpeakerFeed(
|
|
|
|
feeds: CallFeed[],
|
|
|
|
groupCall: GroupCall
|
2022-05-13 20:00:14 +00:00
|
|
|
): CallFeed | null {
|
2022-05-13 16:58:59 +00:00
|
|
|
const activeSpeakerFeeds = feeds.filter((f) => !f.isAudioMuted());
|
|
|
|
|
2022-05-30 11:14:25 +00:00
|
|
|
// make sure the feeds are in a deterministic order so every client picks
|
2022-06-01 09:11:02 +00:00
|
|
|
// the same one as the active speaker. The custom sort function sorts
|
|
|
|
// by user ID, so needs a collator of some kind to compare. We make a
|
|
|
|
// specific one to help ensure every client sorts the same way
|
|
|
|
// although of course user IDs shouldn't contain accented characters etc.
|
|
|
|
// anyway).
|
2022-05-30 11:14:25 +00:00
|
|
|
const collator = new Intl.Collator("en", {
|
|
|
|
sensitivity: "variant",
|
|
|
|
usage: "sort",
|
|
|
|
ignorePunctuation: false,
|
|
|
|
});
|
|
|
|
activeSpeakerFeeds.sort((a: CallFeed, b: CallFeed): number =>
|
|
|
|
collator.compare(a.userId, b.userId)
|
|
|
|
);
|
|
|
|
|
2022-05-13 20:00:14 +00:00
|
|
|
let activeSpeakerFeed = null;
|
|
|
|
let highestPowerLevel = null;
|
2022-05-13 16:58:59 +00:00
|
|
|
for (const feed of activeSpeakerFeeds) {
|
|
|
|
const member = groupCall.room.getMember(feed.userId);
|
2022-05-13 20:00:14 +00:00
|
|
|
if (highestPowerLevel === null || member.powerLevel > highestPowerLevel) {
|
2022-05-13 16:58:59 +00:00
|
|
|
highestPowerLevel = member.powerLevel;
|
|
|
|
activeSpeakerFeed = feed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return activeSpeakerFeed;
|
|
|
|
}
|
|
|
|
|
2022-05-06 10:32:09 +00:00
|
|
|
export interface PTTState {
|
|
|
|
pttButtonHeld: boolean;
|
|
|
|
isAdmin: boolean;
|
|
|
|
talkOverEnabled: boolean;
|
|
|
|
setTalkOverEnabled: (boolean) => void;
|
|
|
|
activeSpeakerUserId: string;
|
2022-05-31 22:01:34 +00:00
|
|
|
activeSpeakerVolume: number;
|
2022-05-06 10:32:09 +00:00
|
|
|
startTalking: () => void;
|
|
|
|
stopTalking: () => void;
|
2022-05-11 15:28:08 +00:00
|
|
|
transmitBlocked: boolean;
|
2022-05-30 15:28:16 +00:00
|
|
|
// connected is actually an indication of whether we're connected to the HS
|
|
|
|
// (ie. the client's syncing state) rather than media connection, since
|
2022-06-01 09:13:20 +00:00
|
|
|
// it's peer to peer so we can't really say which peer is 'disconnected' if
|
2022-05-30 15:28:16 +00:00
|
|
|
// there's only one other person in the call and they've lost Internet.
|
|
|
|
connected: 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[],
|
2022-06-14 20:53:56 +00:00
|
|
|
playClip: PlayClipFunction
|
2022-05-06 10:32:09 +00:00
|
|
|
): PTTState => {
|
2022-05-13 16:58:59 +00:00
|
|
|
// Used to serialise all the mute calls so they don't race. It has
|
|
|
|
// its own state as its always set separately from anything else.
|
2022-05-13 19:39:21 +00:00
|
|
|
const [mutePromise, setMutePromise] = useState(
|
|
|
|
Promise.resolve<boolean | void>(false)
|
|
|
|
);
|
2022-05-13 16:58:59 +00:00
|
|
|
|
|
|
|
// Wrapper to serialise all the mute operations on the promise
|
|
|
|
const setMicMuteWrapper = useCallback(
|
2022-05-13 19:39:21 +00:00
|
|
|
(muted: boolean) => {
|
2022-05-13 16:58:59 +00:00
|
|
|
setMutePromise(
|
|
|
|
mutePromise.then(() => {
|
2022-05-13 19:39:21 +00:00
|
|
|
return groupCall.setMicrophoneMuted(muted).catch((e) => {
|
2022-05-13 16:58:59 +00:00
|
|
|
logger.error("Failed to unmute microphone", e);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[groupCall, mutePromise]
|
|
|
|
);
|
|
|
|
|
2022-04-29 00:44:50 +00:00
|
|
|
const [
|
2022-05-06 20:27:07 +00:00
|
|
|
{
|
|
|
|
pttButtonHeld,
|
|
|
|
isAdmin,
|
|
|
|
talkOverEnabled,
|
|
|
|
activeSpeakerUserId,
|
2022-05-31 22:01:34 +00:00
|
|
|
activeSpeakerVolume,
|
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());
|
|
|
|
|
2022-05-13 16:58:59 +00:00
|
|
|
const activeSpeakerFeed = getActiveSpeakerFeed(userMediaFeeds, groupCall);
|
2022-04-29 00:44:50 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isAdmin: roomMember.powerLevel >= 100,
|
|
|
|
talkOverEnabled: false,
|
|
|
|
pttButtonHeld: false,
|
|
|
|
activeSpeakerUserId: activeSpeakerFeed ? activeSpeakerFeed.userId : null,
|
2022-05-31 22:01:34 +00:00
|
|
|
activeSpeakerVolume: -Infinity,
|
2022-05-11 15:28:08 +00:00
|
|
|
transmitBlocked: false,
|
2022-04-29 00:44:50 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-05-13 16:58:59 +00:00
|
|
|
const onMuteStateChanged = useCallback(() => {
|
|
|
|
const activeSpeakerFeed = getActiveSpeakerFeed(userMediaFeeds, groupCall);
|
|
|
|
|
2022-07-07 18:42:15 +00:00
|
|
|
let blocked = transmitBlocked;
|
2022-05-13 20:00:14 +00:00
|
|
|
if (activeSpeakerUserId === null && activeSpeakerFeed !== null) {
|
2022-05-13 16:58:59 +00:00
|
|
|
if (activeSpeakerFeed.userId === client.getUserId()) {
|
|
|
|
playClip(PTTClipID.START_TALKING_LOCAL);
|
|
|
|
} else {
|
|
|
|
playClip(PTTClipID.START_TALKING_REMOTE);
|
2022-05-11 15:28:08 +00:00
|
|
|
}
|
2022-05-13 20:00:14 +00:00
|
|
|
} else if (activeSpeakerUserId !== null && activeSpeakerFeed === null) {
|
|
|
|
playClip(PTTClipID.END_TALKING);
|
2022-05-13 16:58:59 +00:00
|
|
|
} else if (
|
|
|
|
pttButtonHeld &&
|
2022-07-07 18:42:15 +00:00
|
|
|
activeSpeakerFeed?.userId !== client.getUserId() &&
|
|
|
|
!transmitBlocked
|
2022-05-13 16:58:59 +00:00
|
|
|
) {
|
2022-05-13 17:09:45 +00:00
|
|
|
// We were talking but we've been cut off: mute our own mic
|
|
|
|
// (this is the easier way of cutting other speakers off if an
|
|
|
|
// admin barges in: we could also mute the non-admin speaker
|
|
|
|
// on all receivers, but we'd have to make sure we unmuted them
|
|
|
|
// correctly.)
|
2022-05-13 16:58:59 +00:00
|
|
|
setMicMuteWrapper(true);
|
|
|
|
blocked = true;
|
|
|
|
playClip(PTTClipID.BLOCKED);
|
|
|
|
}
|
2022-05-11 15:28:08 +00:00
|
|
|
|
2022-05-31 22:01:34 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
activeSpeakerUserId: activeSpeakerFeed ? activeSpeakerFeed.userId : null,
|
|
|
|
transmitBlocked: blocked,
|
|
|
|
}));
|
2022-05-13 16:58:59 +00:00
|
|
|
}, [
|
|
|
|
playClip,
|
|
|
|
groupCall,
|
|
|
|
pttButtonHeld,
|
|
|
|
activeSpeakerUserId,
|
|
|
|
client,
|
|
|
|
userMediaFeeds,
|
|
|
|
setMicMuteWrapper,
|
2022-07-07 18:42:15 +00:00
|
|
|
transmitBlocked,
|
2022-05-13 16:58:59 +00:00
|
|
|
]);
|
2022-04-29 00:44:50 +00:00
|
|
|
|
2022-05-13 16:58:59 +00:00
|
|
|
useEffect(() => {
|
2022-04-29 00:44:50 +00:00
|
|
|
for (const callFeed of userMediaFeeds) {
|
2022-05-31 22:01:34 +00:00
|
|
|
callFeed.on(CallFeedEvent.MuteStateChanged, onMuteStateChanged);
|
2022-04-29 00:44:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 16:58:59 +00:00
|
|
|
const activeSpeakerFeed = getActiveSpeakerFeed(userMediaFeeds, groupCall);
|
2022-04-29 00:44:50 +00:00
|
|
|
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
activeSpeakerUserId: activeSpeakerFeed ? activeSpeakerFeed.userId : null,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
for (const callFeed of userMediaFeeds) {
|
2022-05-31 22:01:34 +00:00
|
|
|
callFeed.off(CallFeedEvent.MuteStateChanged, onMuteStateChanged);
|
2022-04-29 00:44:50 +00:00
|
|
|
}
|
|
|
|
};
|
2022-05-13 16:58:59 +00:00
|
|
|
}, [userMediaFeeds, onMuteStateChanged, groupCall]);
|
2022-04-29 00:44:50 +00:00
|
|
|
|
2022-05-31 22:01:34 +00:00
|
|
|
const onVolumeChanged = useCallback((volume: number) => {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
activeSpeakerVolume: volume,
|
|
|
|
}));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const activeSpeakerFeed = getActiveSpeakerFeed(userMediaFeeds, groupCall);
|
|
|
|
activeSpeakerFeed?.on(CallFeedEvent.VolumeChanged, onVolumeChanged);
|
|
|
|
return () => {
|
|
|
|
activeSpeakerFeed?.off(CallFeedEvent.VolumeChanged, onVolumeChanged);
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
activeSpeakerVolume: -Infinity,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}, [activeSpeakerUserId, onVolumeChanged, userMediaFeeds, groupCall]);
|
|
|
|
|
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;
|
2022-05-13 16:58:59 +00:00
|
|
|
if (activeSpeakerUserId && !(isAdmin && talkOverEnabled)) {
|
2022-05-11 15:28:08 +00:00
|
|
|
playClip(PTTClipID.BLOCKED);
|
|
|
|
blocked = true;
|
2022-04-29 17:56:17 +00:00
|
|
|
}
|
2022-05-13 16:58:59 +00:00
|
|
|
// setstate before doing the async call to mute / unmute the mic
|
2022-05-11 15:28:08 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
pttButtonHeld: true,
|
|
|
|
transmitBlocked: blocked,
|
|
|
|
}));
|
2022-05-13 16:58:59 +00:00
|
|
|
|
|
|
|
if (!blocked && groupCall.isMicrophoneMuted()) {
|
|
|
|
setMicMuteWrapper(false);
|
|
|
|
}
|
2022-05-11 15:28:08 +00:00
|
|
|
}, [
|
|
|
|
pttButtonHeld,
|
|
|
|
groupCall,
|
|
|
|
activeSpeakerUserId,
|
|
|
|
isAdmin,
|
|
|
|
talkOverEnabled,
|
|
|
|
setState,
|
|
|
|
playClip,
|
2022-05-13 16:58:59 +00:00
|
|
|
setMicMuteWrapper,
|
2022-05-11 15:28:08 +00:00
|
|
|
]);
|
2022-04-29 17:56:17 +00:00
|
|
|
|
2022-05-13 16:58:59 +00:00
|
|
|
const stopTalking = useCallback(async () => {
|
2022-05-11 15:28:08 +00:00
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
pttButtonHeld: false,
|
|
|
|
transmitBlocked: false,
|
|
|
|
}));
|
2022-05-13 16:58:59 +00:00
|
|
|
|
|
|
|
setMicMuteWrapper(true);
|
|
|
|
}, [setMicMuteWrapper]);
|
2022-04-29 17:56:17 +00:00
|
|
|
|
2022-05-30 15:28:16 +00:00
|
|
|
// separate state for connected: we set it separately from other things
|
|
|
|
// in the client sync callback
|
|
|
|
const [connected, setConnected] = useState(true);
|
|
|
|
|
|
|
|
const onClientSync = useCallback(
|
|
|
|
(syncState: SyncState) => {
|
|
|
|
setConnected(syncState !== SyncState.Error);
|
|
|
|
},
|
|
|
|
[setConnected]
|
|
|
|
);
|
|
|
|
|
2022-06-01 09:21:44 +00:00
|
|
|
useEffect(() => {
|
|
|
|
client.on(ClientEvent.Sync, onClientSync);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
client.removeListener(ClientEvent.Sync, onClientSync);
|
|
|
|
};
|
|
|
|
}, [client, onClientSync]);
|
|
|
|
|
2022-04-29 00:44:50 +00:00
|
|
|
const setTalkOverEnabled = useCallback((talkOverEnabled) => {
|
|
|
|
setState((prevState) => ({
|
|
|
|
...prevState,
|
|
|
|
talkOverEnabled,
|
|
|
|
}));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
|
|
|
pttButtonHeld,
|
|
|
|
isAdmin,
|
|
|
|
talkOverEnabled,
|
|
|
|
setTalkOverEnabled,
|
|
|
|
activeSpeakerUserId,
|
2022-05-31 22:01:34 +00:00
|
|
|
activeSpeakerVolume,
|
2022-04-29 17:56:17 +00:00
|
|
|
startTalking,
|
|
|
|
stopTalking,
|
2022-05-11 15:28:08 +00:00
|
|
|
transmitBlocked,
|
2022-05-30 15:28:16 +00:00
|
|
|
connected,
|
2022-04-29 00:44:50 +00:00
|
|
|
};
|
2022-05-06 10:32:09 +00:00
|
|
|
};
|