Skip to content

Instantly share code, notes, and snippets.

@bradley
Created November 16, 2018 20:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradley/211a51bbb9c385729958d1fdd1687f10 to your computer and use it in GitHub Desktop.
Save bradley/211a51bbb9c385729958d1fdd1687f10 to your computer and use it in GitHub Desktop.
Detect User Media Devices and Permission (Async/Await)
class UserMediaDetector {
// Returns info on all user media devices.
async devices() {
if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
throw(Error("UserMediaDetector getDevices failed: enumerateDevices is not supported"));
}
const mediaDevices = await navigator.mediaDevices.enumerateDevices();
return mediaDevices.map(
({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label })
);
}
// Returns permitted status of given user media.
async permitted(kind) {
if (!kind || !Object.values(UserMediaDetector.Kinds).includes(kind)) {
throw(Error(`UserMediaDetector permitted failed: kind ${kind} is not supported`));
}
const devices = await this.devices();
// Note: The presence of a `label` on a device indicates that it
// the device is active or persistent permissions are granted.
const permitted = !!devices.find(
device => device.kind === kind && !!device.label
);
return permitted;
}
// Returns boolean value designating if all given media kinds are permitted.
async permittedAll(kinds = Object.values(UserMediaDetector.Kinds)) {
const kindsArray = Array.isArray(kinds) ? kinds : Array.of(kinds);
const permissionStates = await Promise.all(kindsArray.map(kind => this.permitted(kind)));
return permissionStates.every(isPermitted => isPermitted);
}
}
UserMediaDetector.Kinds = {
VideoInput: "videoinput",
AudioInput: "audioinput",
AudioOutput: "audioinput"
};
export default UserMediaDetector;
@bradley
Copy link
Author

bradley commented Nov 16, 2018

Example usage:

const detector = new UserMediaDetector();
const weHaveCameraPermission = await detector.permitted(UserMediaDetector.Kinds.VideoInput);
console.log(weHaveCameraPermission);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment