Skip to content

Commit 193dc3f

Browse files
committed
Make camera unavailable if using earpice mode
1 parent 3ffb118 commit 193dc3f

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/MediaDevicesContext.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ export function useMediaDevices(): MediaDevices {
2323
return mediaDevices;
2424
}
2525

26+
export const useIsEarpiece = (): boolean => {
27+
const devices = useMediaDevices();
28+
const audioOutput = useObservableEagerState(devices.audioOutput.selected$);
29+
const available = useObservableEagerState(devices.audioOutput.available$);
30+
if (!audioOutput?.id) return false;
31+
return available.get(audioOutput.id)?.type === "earpiece";
32+
};
33+
2634
/**
2735
* A convenience hook to get the audio node configuration for the earpiece.
2836
* It will check the `useAsEarpiece` of the `audioOutput` device and return
@@ -36,17 +44,13 @@ export const useEarpieceAudioConfig = (): {
3644
} => {
3745
const devices = useMediaDevices();
3846
const audioOutput = useObservableEagerState(devices.audioOutput.selected$);
39-
// We use only the right speaker (pan = 1) for the earpiece.
40-
// This mimics the behavior of the native earpiece speaker (only the top speaker on an iPhone)
41-
const pan = useMemo(
42-
() => (audioOutput?.virtualEarpiece ? 1 : 0),
43-
[audioOutput?.virtualEarpiece],
44-
);
45-
// We also do lower the volume by a factor of 10 to optimize for the usecase where
46-
// a user is holding the phone to their ear.
47-
const volume = useMemo(
48-
() => (audioOutput?.virtualEarpiece ? 0.1 : 1),
49-
[audioOutput?.virtualEarpiece],
50-
);
51-
return { pan, volume };
47+
const isVirtualEarpiece = audioOutput?.virtualEarpiece ?? false;
48+
return {
49+
// We use only the right speaker (pan = 1) for the earpiece.
50+
// This mimics the behavior of the native earpiece speaker (only the top speaker on an iPhone)
51+
pan: useMemo(() => (isVirtualEarpiece ? 1 : 0), [isVirtualEarpiece]),
52+
// We also do lower the volume by a factor of 10 to optimize for the usecase where
53+
// a user is holding the phone to their ear.
54+
volume: useMemo(() => (isVirtualEarpiece ? 0.1 : 1), [isVirtualEarpiece]),
55+
};
5256
};

src/room/MuteStates.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
type SelectedDevice,
2222
type MediaDevice,
2323
} from "../state/MediaDevices";
24-
import { useMediaDevices } from "../MediaDevicesContext";
24+
import { useIsEarpiece, useMediaDevices } from "../MediaDevicesContext";
2525
import { useReactiveState } from "../useReactiveState";
2626
import { ElementWidgetActions, widget } from "../widget";
2727
import { Config } from "../config/Config";
@@ -58,6 +58,7 @@ export interface MuteStates {
5858
function useMuteState(
5959
device: MediaDevice<DeviceLabel, SelectedDevice>,
6060
enabledByDefault: () => boolean,
61+
forceUnavailable: boolean = false,
6162
): MuteState {
6263
const available = useObservableEagerState(device.available$);
6364
const [enabled, setEnabled] = useReactiveState<boolean | undefined>(
@@ -67,13 +68,13 @@ function useMuteState(
6768
);
6869
return useMemo(
6970
() =>
70-
available.size === 0
71+
available.size === 0 || forceUnavailable
7172
? deviceUnavailable
7273
: {
7374
enabled: enabled ?? false,
7475
setEnabled: setEnabled as Dispatch<SetStateAction<boolean>>,
7576
},
76-
[available.size, enabled, setEnabled],
77+
[available.size, enabled, forceUnavailable, setEnabled],
7778
);
7879
}
7980

@@ -85,9 +86,11 @@ export function useMuteStates(isJoined: boolean): MuteStates {
8586
const audio = useMuteState(devices.audioInput, () => {
8687
return Config.get().media_devices.enable_audio && !skipLobby && !isJoined;
8788
});
89+
const isEarpiece = useIsEarpiece();
8890
const video = useMuteState(
8991
devices.videoInput,
9092
() => Config.get().media_devices.enable_video && !skipLobby && !isJoined,
93+
isEarpiece, // Force video to be unavailable if using earpiece
9194
);
9295

9396
useEffect(() => {

0 commit comments

Comments
 (0)