Merge branch 'main' into dbkr/fix_lint_errors
This commit is contained in:
@@ -1,8 +1,30 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
export function usePTT(client, groupCall, userMediaFeeds) {
|
||||
const [
|
||||
{ pttButtonHeld, isAdmin, talkOverEnabled, activeSpeakerUserId },
|
||||
{
|
||||
pttButtonHeld,
|
||||
isAdmin,
|
||||
talkOverEnabled,
|
||||
activeSpeakerUserId,
|
||||
unmuteError,
|
||||
},
|
||||
setState,
|
||||
] = useState(() => {
|
||||
const roomMember = groupCall.room.getMember(client.getUserId());
|
||||
@@ -14,6 +36,7 @@ export function usePTT(client, groupCall, userMediaFeeds) {
|
||||
talkOverEnabled: false,
|
||||
pttButtonHeld: false,
|
||||
activeSpeakerUserId: activeSpeakerFeed ? activeSpeakerFeed.userId : null,
|
||||
unmuteError: null,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -47,17 +70,26 @@ export function usePTT(client, groupCall, userMediaFeeds) {
|
||||
};
|
||||
}, [userMediaFeeds]);
|
||||
|
||||
const startTalking = useCallback(() => {
|
||||
const startTalking = useCallback(async () => {
|
||||
setState((prevState) => ({
|
||||
...prevState,
|
||||
pttButtonHeld: true,
|
||||
unmuteError: null,
|
||||
}));
|
||||
if (!activeSpeakerUserId || isAdmin || talkOverEnabled) {
|
||||
if (groupCall.isMicrophoneMuted()) {
|
||||
groupCall.setMicrophoneMuted(false);
|
||||
try {
|
||||
await groupCall.setMicrophoneMuted(false);
|
||||
} catch (e) {
|
||||
setState((prevState) => ({ ...prevState, unmuteError: null }));
|
||||
}
|
||||
}
|
||||
|
||||
setState((prevState) => ({ ...prevState, pttButtonHeld: true }));
|
||||
}
|
||||
}, [groupCall, activeSpeakerUserId, isAdmin, talkOverEnabled]);
|
||||
}, [groupCall, activeSpeakerUserId, isAdmin, talkOverEnabled, setState]);
|
||||
|
||||
const stopTalking = useCallback(() => {
|
||||
setState((prevState) => ({ ...prevState, pttButtonHeld: false }));
|
||||
|
||||
if (!groupCall.isMicrophoneMuted()) {
|
||||
groupCall.setMicrophoneMuted(true);
|
||||
}
|
||||
@@ -70,6 +102,8 @@ export function usePTT(client, groupCall, userMediaFeeds) {
|
||||
if (event.code === "Space") {
|
||||
event.preventDefault();
|
||||
|
||||
if (pttButtonHeld) return;
|
||||
|
||||
startTalking();
|
||||
}
|
||||
}
|
||||
@@ -100,14 +134,7 @@ export function usePTT(client, groupCall, userMediaFeeds) {
|
||||
window.removeEventListener("keyup", onKeyUp);
|
||||
window.removeEventListener("blur", onBlur);
|
||||
};
|
||||
}, [
|
||||
groupCall,
|
||||
startTalking,
|
||||
stopTalking,
|
||||
activeSpeakerUserId,
|
||||
isAdmin,
|
||||
talkOverEnabled,
|
||||
]);
|
||||
}, [activeSpeakerUserId, isAdmin, talkOverEnabled, pttButtonHeld]);
|
||||
|
||||
const setTalkOverEnabled = useCallback((talkOverEnabled) => {
|
||||
setState((prevState) => ({
|
||||
@@ -124,5 +151,6 @@ export function usePTT(client, groupCall, userMediaFeeds) {
|
||||
activeSpeakerUserId,
|
||||
startTalking,
|
||||
stopTalking,
|
||||
unmuteError,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user