Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
www: refactor audio/video controls into "track controls"
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgang42 committed Dec 5, 2022
1 parent 33951ba commit bf59085
Showing 1 changed file with 25 additions and 54 deletions.
79 changes: 25 additions & 54 deletions www/components/room/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ function Control(props: ControlProps) {
);
}

function AudioControl() {
function TrackControl(track: "video" | "audio") {
const [local, setLocal] = useRecoilState(localState);
const peers = useRecoilValue(peersState);
const stream = mapGet(streamMap, LocalStreamKey);

assert(local.status === "connecting" || local.status === "connected");

const { audioEnabled, videoEnabled } = getVideoAudioEnabled(stream);
const enabled = videoEnabled;
const enabled = track === "audio" ? audioEnabled : videoEnabled;

const handleToggle = React.useCallback(() => {
peers.forEach((peer) => {
Expand All @@ -54,84 +54,55 @@ function AudioControl() {
sendMessage(channel, {
type: "peer-state",
name: local.name,
audioEnabled: !audioEnabled,
videoEnabled,
audioEnabled: track === "audio" ? !audioEnabled : audioEnabled,
videoEnabled: track === "video" ? !videoEnabled : videoEnabled,
});
}
});

const tracks = stream?.getAudioTracks();
const tracks =
track === "audio" ? stream?.getAudioTracks() : stream?.getVideoTracks();

if (tracks !== undefined && tracks.length > 0) {
tracks[0].enabled = !enabled;
}

setLocal(localActions.setAudioVideoEnabled(!audioEnabled, videoEnabled));
setLocal(
localActions.setAudioVideoEnabled(
track === "audio" ? !audioEnabled : audioEnabled,
track === "video" ? !videoEnabled : videoEnabled
)
);
}, [audioEnabled, local.name, peers, setLocal, stream, videoEnabled]);

const iconClassName = classNames("absolute", {
"text-slate-800": !enabled,
});

return (
<Control disabled={!enabled} text="Mic">
<Control disabled={!enabled} text={track === "audio" ? "Mic" : "Cam"}>
<Button
color={enabled ? "slate" : "red"}
icon={<MicrophoneIcon width={24} className={iconClassName} />}
icon={
track === "audio" ? (
<MicrophoneIcon width={24} className={iconClassName} />
) : (
<VideoCameraIcon width={24} className={iconClassName} />
)
}
onClick={handleToggle}
square
/>
</Control>
);
}

function VideoControl() {
const [local, setLocal] = useRecoilState(localState);
const peers = useRecoilValue(peersState);
const stream = mapGet(streamMap, LocalStreamKey);

assert(local.status === "connecting" || local.status === "connected");

const { audioEnabled, videoEnabled } = getVideoAudioEnabled(stream);
const enabled = videoEnabled;

const handleToggle = React.useCallback(() => {
peers.forEach((peer) => {
const channel = rtcDataChannelMap.get(peer.sid);

if (channel !== undefined) {
sendMessage(channel, {
type: "peer-state",
name: local.name,
audioEnabled,
videoEnabled: !videoEnabled,
});
}
});

const tracks = stream?.getVideoTracks();

if (tracks !== undefined && tracks.length > 0) {
tracks[0].enabled = !enabled;
}

setLocal(localActions.setAudioVideoEnabled(audioEnabled, !videoEnabled));
}, [audioEnabled, local.name, peers, setLocal, stream, videoEnabled]);

const iconClassName = classNames("absolute", {
"text-slate-800": !enabled,
});
function AudioControl() {
return TrackControl("audio");
}

return (
<Control disabled={!enabled} text="Cam">
<Button
color={enabled ? "slate" : "red"}
icon={<VideoCameraIcon width={24} className={iconClassName} />}
onClick={handleToggle}
square
/>
</Control>
);
function VideoControl() {
return TrackControl("video");
}

export default function Controls() {
Expand Down

0 comments on commit bf59085

Please sign in to comment.