Fix audio being muted when joining a call

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner
2023-07-10 14:26:22 +02:00
parent d367921db9
commit cc2808a1da
2 changed files with 25 additions and 30 deletions

View File

@@ -28,7 +28,7 @@ import { useModalTriggerState } from "../Modal";
import { SettingsModal } from "../settings/SettingsModal"; import { SettingsModal } from "../settings/SettingsModal";
import { useClient } from "../ClientContext"; import { useClient } from "../ClientContext";
import { useMediaDevicesSwitcher } from "../livekit/useMediaDevicesSwitcher"; import { useMediaDevicesSwitcher } from "../livekit/useMediaDevicesSwitcher";
import { DeviceChoices, UserChoices } from "../livekit/useLiveKit"; import { UserChoices } from "../livekit/useLiveKit";
import { useDefaultDevices } from "../settings/useSetting"; import { useDefaultDevices } from "../settings/useSetting";
export type MatrixInfo = { export type MatrixInfo = {
@@ -96,7 +96,7 @@ export function VideoPreview({ matrixInfo, onUserChoicesChanged }: Props) {
// Only let the MediaDeviceSwitcher request permissions if a video track is already available. // Only let the MediaDeviceSwitcher request permissions if a video track is already available.
// Otherwise we would end up asking for permissions in usePreviewTracks and in useMediaDevicesSwitcher. // Otherwise we would end up asking for permissions in usePreviewTracks and in useMediaDevicesSwitcher.
const requestPermissions = !!videoTrack; const requestPermissions = !!audioTrack && !!videoTrack;
const mediaSwitcher = useMediaDevicesSwitcher( const mediaSwitcher = useMediaDevicesSwitcher(
undefined, undefined,
{ {
@@ -110,27 +110,22 @@ export function VideoPreview({ matrixInfo, onUserChoicesChanged }: Props) {
const videoEl = React.useRef(null); const videoEl = React.useRef(null);
// pretend the video is available until the initialization is over // pretend the video is available until the initialization is over
const videoAvailableAndEndabled = const videoAvailableAndEnabled =
videoEnabled && (!!videoTrack || initializingVideo); videoEnabled && (!!videoTrack || initializingVideo);
const audioAvailableAndEndabled = const audioAvailableAndEnabled =
audioEnabled && (!!videoTrack || initializingAudio); audioEnabled && (!!videoTrack || initializingAudio);
useEffect(() => { useEffect(() => {
// Effect to update the settings // Effect to update the settings
const createChoices = (
enabled: boolean,
deviceId?: string
): DeviceChoices | undefined => {
return deviceId
? {
selectedId: deviceId,
enabled,
}
: undefined;
};
onUserChoicesChanged({ onUserChoicesChanged({
video: createChoices(videoAvailableAndEndabled, videoIn.selectedId), video: {
audio: createChoices(audioAvailableAndEndabled, audioIn.selectedId), selectedId: videoIn.selectedId,
enabled: videoAvailableAndEnabled,
},
audio: {
selectedId: audioIn.selectedId,
enabled: audioAvailableAndEnabled,
},
}); });
}, [ }, [
onUserChoicesChanged, onUserChoicesChanged,
@@ -138,8 +133,8 @@ export function VideoPreview({ matrixInfo, onUserChoicesChanged }: Props) {
videoEnabled, videoEnabled,
audioIn.selectedId, audioIn.selectedId,
audioEnabled, audioEnabled,
videoAvailableAndEndabled, videoAvailableAndEnabled,
audioAvailableAndEndabled, audioAvailableAndEnabled,
]); ]);
useEffect(() => { useEffect(() => {
@@ -149,9 +144,7 @@ export function VideoPreview({ matrixInfo, onUserChoicesChanged }: Props) {
setInitializingVideo(false); setInitializingVideo(false);
} }
videoTrack?.getDeviceId().then((videoId) => { videoTrack?.getDeviceId().then((videoId) => {
if (videoId) { videoIn.setSelected(videoId ?? "default");
videoIn.setSelected(videoId);
}
}); });
} }
if (!audioIn.selectedId || audioIn.selectedId == "") { if (!audioIn.selectedId || audioIn.selectedId == "") {
@@ -159,9 +152,7 @@ export function VideoPreview({ matrixInfo, onUserChoicesChanged }: Props) {
setInitializingAudio(false); setInitializingAudio(false);
} }
audioTrack?.getDeviceId().then((audioId) => { audioTrack?.getDeviceId().then((audioId) => {
if (audioId) { audioIn.setSelected(audioId ?? "default");
audioIn.setSelected(audioId);
}
}); });
} }
}, [videoIn, audioIn, videoTrack, audioTrack]); }, [videoIn, audioIn, videoTrack, audioTrack]);
@@ -201,13 +192,13 @@ export function VideoPreview({ matrixInfo, onUserChoicesChanged }: Props) {
)} )}
<div className={styles.previewButtons}> <div className={styles.previewButtons}>
<MicButton <MicButton
muted={!audioAvailableAndEndabled} muted={!audioAvailableAndEnabled}
onPress={() => setAudioEnabled(!audioAvailableAndEndabled)} onPress={() => setAudioEnabled(!audioAvailableAndEnabled)}
disabled={!audioTrack} disabled={!audioTrack}
/> />
<VideoButton <VideoButton
muted={!videoAvailableAndEndabled} muted={!videoAvailableAndEnabled}
onPress={() => setVideoEnabled(!videoAvailableAndEndabled)} onPress={() => setVideoEnabled(!videoAvailableAndEnabled)}
disabled={!videoTrack} disabled={!videoTrack}
/> />
<SettingsButton onPress={openSettings} /> <SettingsButton onPress={openSettings} />

View File

@@ -78,7 +78,11 @@ export const SettingsModal = (props: Props) => {
return ( return (
<SelectInput <SelectInput
label={caption} label={caption}
selectedKey={devices.selectedId} selectedKey={
devices.selectedId === "" || !devices.selectedId
? "default"
: devices.selectedId
}
onSelectionChange={(id) => devices.setSelected(id.toString())} onSelectionChange={(id) => devices.setSelected(id.toString())}
> >
{devices.available.map(({ deviceId, label }, index) => ( {devices.available.map(({ deviceId, label }, index) => (