2

Camera and microphone are manually blocked in Chrome (as shown in a picture below): enter image description here

Local media tracks are created using twilio-video library methods createLocalAudioTrack and createLocalVideoTrack which throws the next exception:

DOMException: Permission denied
code: 0
message: "Permission denied"
name: "NotAllowedError"

When trying to use browser native getUserMedia as follows:

navigator.mediaDevices.getUserMedia({ audio: true, video: true })
  .catch(error => {
    console.log(error)
  })

it console logs the same exception in the catch block:

DOMException: Permission denied

The question: is it possible to know in advance, before creating local media tracks, that there are no permissions to do it (camera and microphone are manually blocked in a browser) and programmatically request such permissions (show popup requesting the permissions) to create the tracks?

olegzhermal
  • 799
  • 10
  • 26

1 Answers1

1

Note that, at the time I'm writing, this is still experimental territory

You can query permissions using Permissions.query():

const permissionDescriptors = [
  {name: 'camera'},
  {name: 'microphone'},
];

const permissions = await Promise.all(permissionDescriptors.map(async descriptor => ({
  descriptor,
  status: await navigator.permissions.query(descriptor),
})));

for (const {descriptor, status} of permissions) {
  console.log(
    descriptor.name, // 'camera' | 'microphone'
    status.state, // 'granted' | 'denied' | 'prompt'
  );
}

You will be able to request permissions using Permissions.request(). However, this is not currently supported in any browser.

Today, it's better to just try to access the MediaStream, and catch and handle a potential exception:

let mediaStream;
try {
  const constraints = { audio: true, video: true };
  mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
}
catch (ex) {
  if (ex instanceof DOMException) {
    if (ex.name === 'NotAllowedError') {
      // handle permission denied
    }
    else if (ex.name === 'NotFoundError') {
      // handle media not found
    }
    else {
      // handle unexpected DOMException
    }
  }
  else {
    // handle unexpected error
  }
}

if (!mediaStream) {
  // handle no stream
}
else {
  // do something with stream
}
jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • Exactly. Javascript can't determine gUM permissions without actually trying to use it, because otherwise it would be a covert way to fingerprint browsers ... a leakage of user preferences that should be kept private. – O. Jones Dec 23 '21 at 13:26
  • @O.Jones Permissions are origin-specific, and querying is restricted to same-origin, so that's not a security/privacy problem. – jsejcksn Dec 23 '21 at 20:05