Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"types": "dist/react-webcam.d.ts",
"scripts": {
"prepublish": "npm run build:production",
"prepare": "npm run build:production",
"build": "NODE_ENV=development webpack-cli",
"build:production": "npm run build && NODE_ENV=production webpack-cli",
"lint": "eslint src/react-webcam.tsx",
Expand Down
19 changes: 16 additions & 3 deletions src/react-webcam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface WebcamProps extends React.HTMLProps<HTMLVideoElement> {
onUserMediaError: (error: string) => void;
screenshotFormat: "image/webp" | "image/png" | "image/jpeg";
screenshotQuality: number;
videoConstraints?: MediaStreamConstraints["video"];
videoConstraints?: MediaStreamConstraints["video"] | MediaStreamConstraints["video"][];
}

interface WebcamState {
Expand Down Expand Up @@ -242,7 +242,10 @@ export default class Webcam extends React.Component<WebcamProps, WebcamState> {
private requestUserMedia() {
const { props } = this;

const sourceSelected = (audioConstraints, videoConstraints) => {
const sourceSelected = (audioConstraints, videoConstraintsSet) => {
const videoConstraints = Array.isArray(videoConstraintsSet)
? videoConstraintsSet[0]
: videoConstraintsSet;
const constraints: MediaStreamConstraints = {
video: typeof videoConstraints !== "undefined" ? videoConstraints : true
};
Expand All @@ -252,6 +255,10 @@ export default class Webcam extends React.Component<WebcamProps, WebcamState> {
typeof audioConstraints !== "undefined" ? audioConstraints : true;
}

if (constraints.video) {
console.log('[webcam] trying constraints', constraints.video);
}

navigator.mediaDevices
.getUserMedia(constraints)
.then(stream => {
Expand All @@ -262,7 +269,13 @@ export default class Webcam extends React.Component<WebcamProps, WebcamState> {
}
})
.catch(e => {
this.handleUserMedia(e);
// try next constraints if available
const isRecoverableError = e.name === "AbortError" || e.name === "OverconstrainedError";
if (isRecoverableError && Array.isArray(videoConstraintsSet) && videoConstraintsSet.length > 1) {
sourceSelected(audioConstraints, videoConstraintsSet.slice(1));
} else {
this.handleUserMedia(e);
}
});
};

Expand Down