Camera resolution finder

Tests basic camera resolutions using getUserMedia()

by thehunmonkgroup

HTML

<div id="div">
</div>

JavaScript

let nlog = 0;
const log = (msg) => {
  div.innerHTML += ++nlog + ". " + msg + "<br>";
  console.log(msg);
}
const resList = [
  [160, 120],
  [320, 180],
  [320, 240],
  [640, 360],
  [640, 480],
  [1280, 720],
  [1920, 1080],
];
let resI = 0;

function handleError(error) {
  log('device enumeration error: ', error);
}

function testResolutions (cam) {

  if (resI >= resList.length) {
    resI = 0;
    return;
  }

  const w = resList[resI][0];
  const h = resList[resI][1];
  resI++;

  const video = {
    width: {
      exact: w
    },
    height: {
      exact: h
    },
    deviceId: {
      exact: cam
    },
  };

  navigator.mediaDevices.getUserMedia({
    audio: false,
    video: video
  }).then(function(e) {
    e.getTracks().forEach(function(track) {
      track.stop();
    });
    log(w + "x" + h + " supported.");
    testResolutions(cam);
  }).catch(function(e) {
    log(w + "x" + h + " NOT supported.");
    testResolutions(cam);
  });
}
var setupVideo = function(devices) {
  for (var i = 0; i !== devices.length; ++i) {
    var device = devices[i];
    var wait = 1000 * i;
    if (device.kind === 'videoinput') {
      log("Using video device: " + device.label + "(" + device.deviceId + ")");
      testResolutions(device.deviceId);
      break;
    }
  }
}
navigator.mediaDevices.enumerateDevices().then(setupVideo).catch(handleError);