Skip to content

Commit

Permalink
add new initial cam/mic state "idle"
Browse files Browse the repository at this point in the history
this also changes checks for awaiting cam/mic access and adds checks for when devices are off initially
  • Loading branch information
Regaddi committed Jan 6, 2023
1 parent 2c9f839 commit 671300e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
32 changes: 20 additions & 12 deletions src/DailyDevices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useDailyEvent } from './hooks/useDailyEvent';
import { RECOIL_PREFIX } from './lib/constants';

type GeneralState =
| 'idle'
| 'pending'
| 'not-supported'
| 'granted'
Expand All @@ -32,11 +33,11 @@ export interface StatefulDevice {

export const generalCameraState = atom<GeneralState>({
key: RECOIL_PREFIX + 'general-camera-state',
default: 'pending',
default: 'idle',
});
export const generalMicrophoneState = atom<GeneralState>({
key: RECOIL_PREFIX + 'general-microphone-state',
default: 'pending',
default: 'idle',
});
export const cameraDevicesState = atom<StatefulDevice[]>({
key: RECOIL_PREFIX + 'camera-devices',
Expand Down Expand Up @@ -152,25 +153,24 @@ export const DailyDevices: React.FC<React.PropsWithChildren<unknown>> = ({
* Updates general and specific device states, based on blocked status.
*/
const updateDeviceStates = useRecoilCallback(
({ set, snapshot, transact_UNSTABLE }) =>
({ set, transact_UNSTABLE }) =>
async () => {
if (!daily) return;

const currentCamState = await snapshot.getPromise(generalCameraState);
const currentMicState = await snapshot.getPromise(
generalMicrophoneState
);

const participants = daily.participants();
// Guard against potentially uninitialized local participant
if (!participants.local) return;

const { tracks } = participants.local;

const awaitingCamAccess =
currentCamState === 'pending' && tracks.video.state === 'interrupted';
tracks.video.state === 'interrupted' && !tracks.video.persistentTrack;
const initialCamOff =
!tracks.video.persistentTrack && Boolean(tracks.video.off?.byUser);
const awaitingMicAccess =
currentMicState === 'pending' && tracks.audio.state === 'interrupted';
tracks.audio.state === 'interrupted' && !tracks.audio.persistentTrack;
const initialMicOff =
!tracks.audio.persistentTrack && Boolean(tracks.audio.off?.byUser);

if (tracks.audio?.blocked?.byDeviceInUse) {
transact_UNSTABLE(({ set }) => {
Expand All @@ -185,7 +185,11 @@ export const DailyDevices: React.FC<React.PropsWithChildren<unknown>> = ({
set(generalMicrophoneState, 'not-found');
} else if (tracks.audio?.blocked?.byPermissions) {
set(generalMicrophoneState, 'blocked');
} else if (!awaitingMicAccess) {
} else if (awaitingMicAccess) {
set(generalMicrophoneState, 'pending');
} else if (initialMicOff) {
set(generalMicrophoneState, 'idle');
} else {
transact_UNSTABLE(({ set }) => {
set(generalMicrophoneState, 'granted');
set(microphoneDevicesState, (mics) =>
Expand All @@ -209,7 +213,11 @@ export const DailyDevices: React.FC<React.PropsWithChildren<unknown>> = ({
set(generalCameraState, 'not-found');
} else if (tracks.video?.blocked?.byPermissions) {
set(generalCameraState, 'blocked');
} else if (!awaitingCamAccess) {
} else if (awaitingCamAccess) {
set(generalCameraState, 'pending');
} else if (initialCamOff) {
set(generalCameraState, 'idle');
} else {
transact_UNSTABLE(({ set }) => {
set(generalCameraState, 'granted');
set(cameraDevicesState, (cams) =>
Expand Down
8 changes: 4 additions & 4 deletions test/hooks/useDevices.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ describe('useDevices', () => {
wrapper: createWrapper(daily),
});
await waitFor(() => {
expect(result.current.camState).toBe('pending');
expect(result.current.camState).toBe('idle');
expect(result.current.cameras).toEqual([]);
expect(result.current.hasCamError).toBe(false);
expect(result.current.hasMicError).toBe(false);
expect(result.current.micState).toBe('pending');
expect(result.current.micState).toBe('idle');
expect(result.current.microphones).toEqual([]);
expect(typeof result.current.refreshDevices).toBe('function');
expect(typeof result.current.setCamera).toBe('function');
Expand Down Expand Up @@ -492,8 +492,8 @@ describe('useDevices', () => {
daily.emit('participant-updated', payload);
});
await waitFor(() => {
expect(result.current.camState).toBe('pending');
expect(result.current.micState).toBe('pending');
expect(result.current.camState).toBe('idle');
expect(result.current.micState).toBe('idle');
});
});
it('local updates state (granted)', async () => {
Expand Down

0 comments on commit 671300e

Please sign in to comment.