Skip to content

Commit c682908

Browse files
authored
fix: relax device constraints on NotFoundError DOMException (#1680)
Browsers react differently to `getUserMedia` requests with constraints that cannot be satisfied. In some cases, Firefox throws a DOMException called `NotFoundError`. We should handle it in the same way we handle over-constrained requests: fallback to default device.
1 parent 460778c commit c682908

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

packages/client/src/devices/devices.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,26 @@ const getStream = async (constraints: MediaStreamConstraints) => {
169169
return stream;
170170
};
171171

172-
function isOverconstrainedError(error: unknown) {
173-
return (
174-
error &&
175-
typeof error === 'object' &&
176-
(('name' in error && error.name === 'OverconstrainedError') ||
177-
('message' in error &&
178-
typeof error.message === 'string' &&
179-
error.message.startsWith('OverconstrainedError')))
180-
);
172+
function isNotFoundOrOverconstrainedError(error: unknown) {
173+
if (!error || typeof error !== 'object') {
174+
return false;
175+
}
176+
177+
if ('name' in error && typeof error.name === 'string') {
178+
const name = error.name;
179+
if (['OverconstrainedError', 'NotFoundError'].includes(name)) {
180+
return true;
181+
}
182+
}
183+
184+
if ('message' in error && typeof error.message === 'string') {
185+
const message = error.message;
186+
if (message.startsWith('OverconstrainedError')) {
187+
return true;
188+
}
189+
}
190+
191+
return false;
181192
}
182193

183194
/**
@@ -205,7 +216,7 @@ export const getAudioStream = async (
205216
});
206217
return await getStream(constraints);
207218
} catch (error) {
208-
if (isOverconstrainedError(error) && trackConstraints?.deviceId) {
219+
if (isNotFoundOrOverconstrainedError(error) && trackConstraints?.deviceId) {
209220
const { deviceId, ...relaxedConstraints } = trackConstraints;
210221
getLogger(['devices'])(
211222
'warn',
@@ -247,7 +258,7 @@ export const getVideoStream = async (
247258
});
248259
return await getStream(constraints);
249260
} catch (error) {
250-
if (isOverconstrainedError(error) && trackConstraints?.deviceId) {
261+
if (isNotFoundOrOverconstrainedError(error) && trackConstraints?.deviceId) {
251262
const { deviceId, ...relaxedConstraints } = trackConstraints;
252263
getLogger(['devices'])(
253264
'warn',

0 commit comments

Comments
 (0)