OSX camera constraint enumerator

test

by jib1

HTML

<video id="v" height="30" autoplay></video><br>
<button onclick="scan()">Scan!</button>
<button onclick="manual()">Manual test</button>
<div id="div"><br></div>

JavaScript

var log = msg => div.innerHTML = div.innerHTML + msg + "<br>";
var failed = e => log(e.name + ": " + e.message + " line " + e.lineNumber);

var wait = ms => new Promise(r => setTimeout(r, ms));
var timeout = (p, ms) => Promise.race([p, wait(ms).then(() => {
  throw new Error("timeout");
})]);

function testgum(constraints) {
  var msg = JSON.stringify(constraints).replace(/"/g,'');
  return navigator.mediaDevices.getUserMedia(constraints)
  .then(stream => {
    v.mozSrcObject = stream;
    return timeout(new Promise(r => v.onloadedmetadata = r), 4000);
  })
  .then(() => log(msg + " => " + v.videoWidth + "x" + v.videoHeight))
//  .then(() => wait(1000))
  .then(() => {
    v.mozSrcObject.stop();
    v.mozSrcObject = null;
  })
  .catch(e => log(msg + " => " + e.name))
  .then(() => wait(200));
}

function scan() {
  Promise.resolve()
  .then(() => new Array(9).fill(0).reduce((a, b, i) => a.then(() =>
      testgum({ video: { width: { exact: 1920 - 128*i },
                         height: { exact: 1080 - 72*i } } })),
      Promise.resolve()))
  .then(() => new Array(16).fill(0).reduce((a, b, i) => a.then(() =>
      testgum({ video: { width: { exact: 640 - 40*i },
                         height: { exact: 480 - 30*i } } })),
      Promise.resolve()))
  .catch(failed);
}

function manual() {
  [
    [160, 120],
    [320, 180],
    [320, 240],
    [640, 360],
    [640, 480],
  ].reduce((a, b) => a.then(() =>
    testgum({ video: { width: { exact: b[0] },
                      height: { exact: b[1] } } })),
            Promise.resolve())
  .then(() => testgum({ video: { width: 1280, height: 1024 } }))
  .then(() => testgum({ video: { width: { ideal: 1280 },
                                height: { ideal: 1024 } } }))
  .then(() => testgum({ video: { width: { ideal: 1280 },
                                height: { min: 721, ideal: 1024 } } }))
  .then(() => testgum({ video: { width: { min: 320, max: 1280, ideal: 1 },
                                height: { min:...