2023-08-02 15:29:37 -04:00
|
|
|
/*
|
|
|
|
|
Copyright 2023 New Vector Ltd
|
|
|
|
|
|
|
|
|
|
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 { Dispatch, SetStateAction, useMemo } from "react";
|
|
|
|
|
|
|
|
|
|
import { MediaDevice, useMediaDevices } from "../livekit/MediaDevicesContext";
|
|
|
|
|
import { useReactiveState } from "../useReactiveState";
|
|
|
|
|
|
|
|
|
|
/**
|
2024-04-23 15:15:13 +02:00
|
|
|
* If there already are this many participants in the call, we automatically mute
|
|
|
|
|
* the user.
|
2023-08-02 15:29:37 -04:00
|
|
|
*/
|
2024-04-23 15:15:13 +02:00
|
|
|
export const MUTE_PARTICIPANT_COUNT = 8;
|
2023-08-02 15:29:37 -04:00
|
|
|
|
|
|
|
|
interface DeviceAvailable {
|
|
|
|
|
enabled: boolean;
|
|
|
|
|
setEnabled: Dispatch<SetStateAction<boolean>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface DeviceUnavailable {
|
|
|
|
|
enabled: false;
|
|
|
|
|
setEnabled: null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deviceUnavailable: DeviceUnavailable = {
|
|
|
|
|
enabled: false,
|
|
|
|
|
setEnabled: null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type MuteState = DeviceAvailable | DeviceUnavailable;
|
|
|
|
|
|
|
|
|
|
export interface MuteStates {
|
|
|
|
|
audio: MuteState;
|
|
|
|
|
video: MuteState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function useMuteState(
|
|
|
|
|
device: MediaDevice,
|
2023-10-11 10:42:04 -04:00
|
|
|
enabledByDefault: () => boolean,
|
2023-08-02 15:29:37 -04:00
|
|
|
): MuteState {
|
2024-04-23 15:15:13 +02:00
|
|
|
const [enabled, setEnabled] = useReactiveState<boolean | undefined>(
|
|
|
|
|
(prev) =>
|
2024-07-17 10:07:26 +02:00
|
|
|
device.available.length > 0 ? (prev ?? enabledByDefault()) : undefined,
|
2023-10-11 10:42:04 -04:00
|
|
|
[device],
|
2023-08-02 15:29:37 -04:00
|
|
|
);
|
|
|
|
|
return useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
device.available.length === 0
|
|
|
|
|
? deviceUnavailable
|
2024-04-23 15:15:13 +02:00
|
|
|
: {
|
|
|
|
|
enabled: enabled ?? false,
|
|
|
|
|
setEnabled: setEnabled as Dispatch<SetStateAction<boolean>>,
|
|
|
|
|
},
|
2023-10-11 10:42:04 -04:00
|
|
|
[device, enabled, setEnabled],
|
2023-08-02 15:29:37 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 15:15:13 +02:00
|
|
|
export function useMuteStates(): MuteStates {
|
2023-08-02 15:29:37 -04:00
|
|
|
const devices = useMediaDevices();
|
|
|
|
|
|
2024-04-23 15:15:13 +02:00
|
|
|
const audio = useMuteState(devices.audioInput, () => true);
|
2023-08-02 15:29:37 -04:00
|
|
|
const video = useMuteState(devices.videoInput, () => true);
|
|
|
|
|
|
|
|
|
|
return useMemo(() => ({ audio, video }), [audio, video]);
|
|
|
|
|
}
|