You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
(function() {
|
|
|
|
var promisifiedOldGUM = function(constraints, successCallback, errorCallback) {
|
|
|
|
// First get ahold of getUserMedia, if present
|
|
var getUserMedia = (navigator.getUserMedia ||
|
|
navigator.webkitGetUserMedia ||
|
|
navigator.mozGetUserMedia ||
|
|
navigator.msGetUserMedia);
|
|
|
|
// Some browsers just don't implement it - return a rejected promise with an error
|
|
// to keep a consistent interface
|
|
if(!getUserMedia) {
|
|
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
|
|
}
|
|
|
|
// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
|
|
return new Promise(function(successCallback, errorCallback) {
|
|
getUserMedia.call(navigator, constraints, successCallback, errorCallback);
|
|
});
|
|
|
|
}
|
|
|
|
// Older browsers might not implement mediaDevices at all, so we set an empty object first
|
|
if(navigator.mediaDevices === undefined) {
|
|
navigator.mediaDevices = {};
|
|
}
|
|
|
|
// Some browsers partially implement mediaDevices. We can't just assign an object
|
|
// with getUserMedia as it would overwrite existing properties.
|
|
// Here, we will just add the getUserMedia property if it's missing.
|
|
if(navigator.mediaDevices.getUserMedia === undefined) {
|
|
navigator.mediaDevices.getUserMedia = promisifiedOldGUM;
|
|
}
|
|
|
|
})();
|