Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does mediaDevices.enumerateDevices list some devices twice? What is default?

I write this code in JavaScript in Chrome:

navigator.mediaDevices.enumerateDevices()
  .then((list) => {
    console.log(list);
  });

This outputs a list of media devices like microphones and cameras. Each device is represented as an object with its own deviceId. But some devices, like one of the microphones in particular, are listed twice. And one time their respective deviceId is set to 'default'. What is so special about a one default microphone? And how do I tell which actual deviceId it has?

like image 885
Gherman Avatar asked Nov 14 '18 16:11

Gherman


People also ask

What is enumerateDevices?

enumerateDevices() The MediaDevices method enumerateDevices() requests a list of the available media input and output devices, such as microphones, cameras, headsets, and so forth. The returned Promise is resolved with a MediaDeviceInfo array describing the devices.

What is navigator MediaDevices?

The Navigator. mediaDevices read-only property returns a MediaDevices object, which provides access to connected media input devices like cameras and microphones, as well as screen sharing.


1 Answers

A deviceId lets web sites manage which device their user is using. E.g. store it in a cookie to remember the user's preference from last time.

Some browsers (Chrome, Opera) list the same microphone twice, with different ids. In my case:

  1. Default - Internal Microphone (Built-in)
  2. Internal Microphone (Built-in)

The former is "the OS default", whatever the end-user has configured in System Preferences/Sound (OSX) or Control Panel/Sound (Windows). The idea is: when recalled from a cookie and used, its id gets you whatever is configured in the OS at the time of use, which may differ from last time.

The latter is always the specific mic. Their groupIds match; they're the same physical device atm.

The deviceId = "default" is an oddity of Chrome (and Opera). AFAICT it's a valid id like any other. If you want the other one, compare their groupIds to find it.

Safari does not do this. Firefox recently stopped doing it (as of version 63).

Cameras are never duplicated in this way.

like image 102
jib Avatar answered Nov 16 '22 03:11

jib