Skip to content

Commit 44e666f

Browse files
committed
Handle gUM failures on FF when no audio or video device is available.
Since FF 49, the call to gUM only succeeds if both audio and video constraints can be satisfied, i.e. without a webcam the request fails. This is now handled gracefully, the available devices enumerated and the gUM request is retried with updated constraints (fixes #394).
1 parent d32faba commit 44e666f

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

static/js/mediastream/usermedia.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,52 @@ define(['jquery', 'underscore', 'audiocontext', 'mediastream/dummystream', 'webr
9898
var c = {audio: convertConstraints(constraints.audio), video: convertConstraints(constraints.video)};
9999
// mediaDevices API returns a promise.
100100
console.log("Constraints for mediaDevices", c);
101-
window.navigator.mediaDevices.getUserMedia(c).then(success).catch(error);
101+
window.navigator.mediaDevices.getUserMedia(c).then(success).catch(function(err) {
102+
if (!navigator.mediaDevices.enumerateDevices) {
103+
// Don't know how to check for available devices.
104+
error(err);
105+
return;
106+
}
107+
108+
// gUM fails if one of audio/video is not available, check which devices are
109+
// available and retry with updated constraints.
110+
console.log("getUserMedia with audio/video contraints failed", err);
111+
navigator.mediaDevices.enumerateDevices().then(function(devices) {
112+
var has_audio = false;
113+
var has_video = false;
114+
console.log("Available devices", devices);
115+
_.each(devices, function(device) {
116+
switch (device.kind) {
117+
case "audioinput":
118+
has_audio = true;
119+
break;
120+
case "videoinput":
121+
has_video = true;
122+
break;
123+
default:
124+
break;
125+
}
126+
});
127+
if (!has_audio && !has_video) {
128+
// No audio or video device found, no need to retry gUM.
129+
error(err);
130+
return;
131+
}
132+
133+
if (!has_audio) {
134+
delete c.audio;
135+
}
136+
if (!has_video) {
137+
delete c.video;
138+
}
139+
console.log("Retry getUserMedia with updated constraints", c);
140+
window.navigator.mediaDevices.getUserMedia(c).then(success).catch(error);
141+
}).catch(function(devicesError) {
142+
console.log("Could not enumerate devices", devicesError);
143+
// Fail initial gUM
144+
error(err);
145+
});
146+
});
102147
}
103148
} else {
104149
// Use existing adapter.

0 commit comments

Comments
 (0)