Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return to normal state when time limit reached #313

Merged
merged 7 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/room/PTTButton.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useCallback, useEffect, useState } from "react";
import classNames from "classnames";
import styles from "./PTTButton.module.css";
import { ReactComponent as MicIcon } from "../icons/Mic.svg";
Expand All @@ -14,14 +14,32 @@ export function PTTButton({
startTalking,
stopTalking,
}) {
const [isHeld, setHeld] = useState(false);
const onDocumentMouseUp = useCallback(() => {
if (isHeld) stopTalking();
setHeld(false);
}, [isHeld, setHeld]);

const onButtonMouseDown = useCallback(() => {
setHeld(true);
startTalking();
}, [setHeld]);

useEffect(() => {
window.addEventListener("mouseup", onDocumentMouseUp);

return () => {
window.removeEventListener("mouseup", onDocumentMouseUp);
};
}, [onDocumentMouseUp]);

return (
<button
className={classNames(styles.pttButton, {
[styles.talking]: activeSpeakerUserId,
[styles.error]: showTalkOverError,
})}
onMouseDown={startTalking}
onMouseUp={stopTalking}
onMouseDown={onButtonMouseDown}
>
{activeSpeakerIsLocalUser || !activeSpeakerUserId ? (
<MicIcon
Expand Down
9 changes: 6 additions & 3 deletions src/room/PTTCallView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Timer } from "./Timer";
import { Toggle } from "../input/Toggle";
import { getAvatarUrl } from "../matrix-utils";
import { ReactComponent as AudioIcon } from "../icons/Audio.svg";
import { OtherUserSpeakingError } from "matrix-js-sdk/src/webrtc/groupCall";

export function PTTCallView({
client,
Expand Down Expand Up @@ -47,12 +48,14 @@ export function PTTCallView({
activeSpeakerUserId,
startTalking,
stopTalking,
unmuteError,
} = usePTT(client, groupCall, userMediaFeeds);

const showTalkOverError =
pttButtonHeld && unmuteError instanceof OtherUserSpeakingError;

const activeSpeakerIsLocalUser =
activeSpeakerUserId && client.getUserId() === activeSpeakerUserId;
const showTalkOverError =
pttButtonHeld && !activeSpeakerIsLocalUser && !talkOverEnabled;
const activeSpeakerUser = activeSpeakerUserId
? client.getUser(activeSpeakerUserId)
: null;
Expand Down Expand Up @@ -123,7 +126,7 @@ export function PTTCallView({
<p className={styles.actionTip}>
{showTalkOverError
? "You can't talk at the same time"
: pttButtonHeld
: pttButtonHeld && activeSpeakerIsLocalUser
? "Release spacebar key to stop"
: talkOverEnabled &&
activeSpeakerUserId &&
Expand Down
35 changes: 26 additions & 9 deletions src/room/usePTT.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ 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());
Expand All @@ -14,6 +20,7 @@ export function usePTT(client, groupCall, userMediaFeeds) {
talkOverEnabled: false,
pttButtonHeld: false,
activeSpeakerUserId: activeSpeakerFeed ? activeSpeakerFeed.userId : null,
unmuteError: null,
};
});

Expand Down Expand Up @@ -47,29 +54,38 @@ 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 }));
}
}, []);
}, [setState]);

const stopTalking = useCallback(() => {
setState((prevState) => ({ ...prevState, pttButtonHeld: false }));

if (!groupCall.isMicrophoneMuted()) {
groupCall.setMicrophoneMuted(true);
}

setState((prevState) => ({ ...prevState, pttButtonHeld: false }));
}, []);

useEffect(() => {
function onKeyDown(event) {
if (event.code === "Space") {
event.preventDefault();

if (pttButtonHeld) return;

startTalking();
}
}
Expand Down Expand Up @@ -100,7 +116,7 @@ export function usePTT(client, groupCall, userMediaFeeds) {
window.removeEventListener("keyup", onKeyUp);
window.removeEventListener("blur", onBlur);
};
}, [activeSpeakerUserId, isAdmin, talkOverEnabled]);
}, [activeSpeakerUserId, isAdmin, talkOverEnabled, pttButtonHeld]);

const setTalkOverEnabled = useCallback((talkOverEnabled) => {
setState((prevState) => ({
Expand All @@ -117,5 +133,6 @@ export function usePTT(client, groupCall, userMediaFeeds) {
activeSpeakerUserId,
startTalking,
stopTalking,
unmuteError,
};
}