Skip to content

Commit 766bbed

Browse files
committed
Revert "fix: cover some device selection edge cases (#1604)"
This reverts commit a8fc0ea.
1 parent 646ae9f commit 766bbed

File tree

6 files changed

+54
-76
lines changed

6 files changed

+54
-76
lines changed

packages/client/src/devices/InputMediaDeviceManager.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ export abstract class InputMediaDeviceManager<
215215
await this.applySettingsToStream();
216216
} catch (error) {
217217
this.state.setDevice(prevDeviceId);
218-
await this.applySettingsToStream();
219218
throw error;
220219
}
221220
}

packages/client/src/devices/devices.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export const getAudioStream = async (
184184
const constraints: MediaStreamConstraints = {
185185
audio: {
186186
...audioDeviceConstraints.audio,
187-
...normalizeContraints(trackConstraints),
187+
...trackConstraints,
188188
},
189189
};
190190

@@ -195,6 +195,16 @@ export const getAudioStream = async (
195195
});
196196
return await getStream(constraints);
197197
} catch (error) {
198+
if (error instanceof OverconstrainedError && trackConstraints?.deviceId) {
199+
const { deviceId, ...relaxedContraints } = trackConstraints;
200+
getLogger(['devices'])(
201+
'warn',
202+
'Failed to get audio stream, will try again with relaxed contraints',
203+
{ error, constraints, relaxedContraints },
204+
);
205+
return getAudioStream(relaxedContraints);
206+
}
207+
198208
getLogger(['devices'])('error', 'Failed to get audio stream', {
199209
error,
200210
constraints,
@@ -217,7 +227,7 @@ export const getVideoStream = async (
217227
const constraints: MediaStreamConstraints = {
218228
video: {
219229
...videoDeviceConstraints.video,
220-
...normalizeContraints(trackConstraints),
230+
...trackConstraints,
221231
},
222232
};
223233
try {
@@ -227,6 +237,16 @@ export const getVideoStream = async (
227237
});
228238
return await getStream(constraints);
229239
} catch (error) {
240+
if (error instanceof OverconstrainedError && trackConstraints?.deviceId) {
241+
const { deviceId, ...relaxedContraints } = trackConstraints;
242+
getLogger(['devices'])(
243+
'warn',
244+
'Failed to get video stream, will try again with relaxed contraints',
245+
{ error, constraints, relaxedContraints },
246+
);
247+
return getVideoStream(relaxedContraints);
248+
}
249+
230250
getLogger(['devices'])('error', 'Failed to get video stream', {
231251
error,
232252
constraints,
@@ -235,20 +255,6 @@ export const getVideoStream = async (
235255
}
236256
};
237257

238-
function normalizeContraints(constraints: MediaTrackConstraints | undefined) {
239-
if (
240-
constraints?.deviceId === 'default' ||
241-
(typeof constraints?.deviceId === 'object' &&
242-
'exact' in constraints.deviceId &&
243-
constraints.deviceId.exact === 'default')
244-
) {
245-
const { deviceId, ...contraintsWithoutDeviceId } = constraints;
246-
return contraintsWithoutDeviceId;
247-
}
248-
249-
return constraints;
250-
}
251-
252258
/**
253259
* Prompts the user for a permission to share a screen.
254260
* If the user grants the permission, a screen sharing stream is returned. Throws otherwise.

packages/react-sdk/src/components/DeviceSettings/DeviceSelectorAudio.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
import { useCallStateHooks } from '@stream-io/video-react-bindings';
22
import { DeviceSelector } from './DeviceSelector';
3-
import {
4-
createCallControlHandler,
5-
PropsWithErrorHandler,
6-
} from '../../utilities/callControlHandler';
73

8-
export type DeviceSelectorAudioInputProps = PropsWithErrorHandler<{
4+
export type DeviceSelectorAudioInputProps = {
95
title?: string;
106
visualType?: 'list' | 'dropdown';
11-
}>;
7+
};
128

13-
export const DeviceSelectorAudioInput = (
14-
props: DeviceSelectorAudioInputProps,
15-
) => {
9+
export const DeviceSelectorAudioInput = ({
10+
title,
11+
visualType,
12+
}: DeviceSelectorAudioInputProps) => {
1613
const { useMicrophoneState } = useCallStateHooks();
1714
const { microphone, selectedDevice, devices } = useMicrophoneState();
18-
const handleChange = createCallControlHandler(
19-
props,
20-
async (deviceId: string) => {
21-
await microphone.select(deviceId);
22-
},
23-
);
2415

2516
return (
2617
<DeviceSelector
2718
devices={devices || []}
2819
selectedDeviceId={selectedDevice}
2920
type="audioinput"
30-
onChange={handleChange}
31-
title={props.title}
32-
visualType={props.visualType}
21+
onChange={async (deviceId) => {
22+
await microphone.select(deviceId);
23+
}}
24+
title={title}
25+
visualType={visualType}
3326
icon="mic"
3427
/>
3528
);

packages/react-sdk/src/components/DeviceSettings/DeviceSelectorVideo.tsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
import {
2-
createCallControlHandler,
3-
PropsWithErrorHandler,
4-
} from '../../utilities/callControlHandler';
51
import { DeviceSelector } from './DeviceSelector';
62
import { useCallStateHooks } from '@stream-io/video-react-bindings';
73

8-
export type DeviceSelectorVideoProps = PropsWithErrorHandler<{
4+
export type DeviceSelectorVideoProps = {
95
title?: string;
106
visualType?: 'list' | 'dropdown';
11-
}>;
7+
};
128

13-
export const DeviceSelectorVideo = (props: DeviceSelectorVideoProps) => {
9+
export const DeviceSelectorVideo = ({
10+
title,
11+
visualType,
12+
}: DeviceSelectorVideoProps) => {
1413
const { useCameraState } = useCallStateHooks();
1514
const { camera, devices, selectedDevice } = useCameraState();
16-
const handleChange = createCallControlHandler(
17-
props,
18-
async (deviceId: string) => {
19-
await camera.select(deviceId);
20-
},
21-
);
2215

2316
return (
2417
<DeviceSelector
2518
devices={devices || []}
2619
type="videoinput"
2720
selectedDeviceId={selectedDevice}
28-
onChange={handleChange}
29-
title={props.title}
30-
visualType={props.visualType}
21+
onChange={async (deviceId) => {
22+
await camera.select(deviceId);
23+
}}
24+
title={title}
25+
visualType={visualType}
3126
icon="camera"
3227
/>
3328
);

packages/react-sdk/src/hooks/usePersistedDevicePreferences.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,8 @@ const usePersistDevicePreferences = (
6868
*
6969
* @param key the key to use for local storage.
7070
*/
71-
const useApplyDevicePreferences = (
72-
key: string,
73-
onWillApply: () => void,
74-
onApplied: () => void,
75-
) => {
71+
const useApplyDevicePreferences = (key: string, onApplied: () => void) => {
7672
const call = useCall();
77-
const onWillApplyRef = useRef(onWillApply);
78-
onWillApplyRef.current = onWillApply;
7973
const onAppliedRef = useRef(onApplied);
8074
onAppliedRef.current = onApplied;
8175
useEffect(() => {
@@ -121,22 +115,17 @@ const useApplyDevicePreferences = (
121115
console.warn('Failed to load device preferences', err);
122116
}
123117
if (preferences) {
124-
await initMic(preferences.mic).catch((err) => {
125-
console.warn('Failed to apply microphone preferences', err);
126-
});
127-
await initCamera(preferences.camera).catch((err) => {
128-
console.warn('Failed to apply camera preferences', err);
129-
});
118+
await initMic(preferences.mic);
119+
await initCamera(preferences.camera);
130120
initSpeaker(preferences.speaker);
131121
}
132122
};
133123

134-
onWillApplyRef.current();
135124
apply()
125+
.then(() => onAppliedRef.current())
136126
.catch((err) => {
137127
console.warn('Failed to apply device preferences', err);
138-
})
139-
.then(() => onAppliedRef.current());
128+
});
140129

141130
return () => {
142131
cancel = true;
@@ -153,11 +142,7 @@ export const usePersistedDevicePreferences = (
153142
key: string = '@stream-io/device-preferences',
154143
) => {
155144
const shouldPersistRef = useRef(false);
156-
useApplyDevicePreferences(
157-
key,
158-
() => (shouldPersistRef.current = false),
159-
() => (shouldPersistRef.current = true),
160-
);
145+
useApplyDevicePreferences(key, () => (shouldPersistRef.current = true));
161146
usePersistDevicePreferences(key, shouldPersistRef);
162147
};
163148

packages/react-sdk/src/utilities/callControlHandler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export type PropsWithErrorHandler<T = unknown> = T & {
1616
* @param props component props, including the onError callback
1717
* @param handler event handler to wrap
1818
*/
19-
export const createCallControlHandler = <P extends unknown[]>(
19+
export const createCallControlHandler = (
2020
props: PropsWithErrorHandler,
21-
handler: (...args: P) => Promise<void>,
21+
handler: () => Promise<void>,
2222
): (() => Promise<void>) => {
2323
const logger = getLogger(['react-sdk']);
2424

25-
return async (...args: P) => {
25+
return async () => {
2626
try {
27-
await handler(...args);
27+
await handler();
2828
} catch (error) {
2929
if (props.onError) {
3030
props.onError(error);

0 commit comments

Comments
 (0)