skip to Main Content

I want to automatically select the default camera (Builtin Camera) of the laptop if users is using any virtual camera. I want to prevent users from using virtual camera in the web application.

I can get list of cameras available in web browser. But I can’t detect which one is virtual camera and which is BuiltIn camera.

const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter((device) => device.kind === "videoinput");

In videoDevices i get all the information of all available cameras (both builtin cameras and virtual cameras).

But the problem is how to detect which is virtual camera and which is builtin Camera.

2

Answers


  1. Unfortunately, there’s no foolproof way to detect whether a camera is virtual or built-in, as it depends on the implementation of the virtual camera software.

    However, one possible approach you can take is to check the camera’s device ID or label which may include specific keywords in the device ID or label, such as "OBS", "ManyCam", or "Snap Camera".

    const virtualCameraSoftware = ['OBS', 'ManyCam', 'Snap Camera'];
    const devices = await navigator.mediaDevices.enumerateDevices();
    const videoDevices = devices.filter((device) => device.kind === 'videoinput');
    const defaultCamera = videoDevices.find(device => !virtualCameraSoftware.some(keyword => device.deviceId.includes(keyword) || device.label.includes(keyword))));
    

    second approach

    const { exec } = require('child_process');
    
    async function checkVirtualCameras() {
      return new Promise((resolve, reject) => {
        exec('reg query HKLM\SYSTEM\CurrentControlSet\Control\MediaResources\MediaClass\{65E8773D-8F56-11D0-A3B9-00A0C9223196}\Instance /s', (error, stdout, stderr) => {
          if (error) {
            reject(error);
          } else {
            const virtualCameras = stdout.match(/{[w-]+}/g); // match GUIDs of virtual cameras
            resolve(virtualCameras);
          }
        });
      });
    }
    
    const devices = await navigator.mediaDevices.enumerateDevices();
    const videoDevices = devices.filter((device) => device.kind === "videoinput");
    
    const virtualCameras = await checkVirtualCameras();
    
    const defaultCamera = videoDevices.find((device) => {
      if (!virtualCameras.includes(device.groupId)) {
        return true;
      }
    });
    

    hope this works!

    Login or Signup to reply.
  2. navigator.mediaDevices.enumerateDevices()
      .then(devices => {
        let videoInputs = devices.filter(device => device.kind === 'videoinput');
        let groups = {};
    
        videoInputs.forEach(device => {
          if (device.groupId in groups) {
            groups[device.groupId].push(device);
          } else {
            groups[device.groupId] = [device];
          }
        });
    
        Object.values(groups).forEach(group => {
          if (group.some(device => device.label.toLowerCase().includes('virtual'))) {
            console.log(group[0].label + ' is a virtual camera.');
          } else {
            console.log(group[0].label + ' is a system built-in camera.');
          }
        });
      })
      .catch(err => {
        console.error('Error getting media devices: ', err);
      });

    I hope this will works!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search