getUserMedia withTypeError illegal invocation on start video()


#1

// https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
var promisifiedOldGUM = function(constraints) {

  // First get a hold of getUserMedia, if present.
  navigator.getMedia = (navigator.getUserMedia ||
  navigator.webkitGetUserMedia ||
  navigator.mozGetUserMedia);

  // Some browsers just don't implement it.
  // Abort in order to test Cordova plugin next.
  if(!getUserMedia) {
    //return false;
    return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  }

  // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
  return new Promise(function(resolve, reject) {
    getUserMedia.call(navigator, constraints, resolve, reject);
  });

};

// Older browsers might not implement mediaDevices at all, so we set an empty object first
if(navigator.mediaDevices === undefined) {
  navigator.mediaDevices = {};
}

// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
var getUserMedia = navigator.mediaDevices.getUserMedia ||
  promisifiedOldGUM /*||
     (Meteor.isCordova && cordova.plugins.iosrtc.getUserMedia)*/; // Could not make working getUserMedia in Android (cordova-plugin-iosrtc for iOS) for now. Switching to cordova-plugin-media-capture for Android and iOS.

if (!getUserMedia) {
  throw new Meteor.Error("no-camera-API", "Votre navigateur ou téléphone n'est pas compatible avec cette application.");
}

var front = false;
document.getElementById('flip-button').onclick = function() { front = !front; };
// Flip not implemented for now.

var constraints = {
  audio: false,
  video: /*{
   facingMode: (front ? "user" : "environment")
   }*/true
};

var localMediaStream;

function startVideo() {
  getUserMedia(constraints)
    .then(function(stream) {
      localMediaStream = stream; // Keep a reference in order to be able to stop it.

      var video = document.querySelector('#video');

      if (navigator.mozGetUserMedia) {
        video.mozSrcObject = stream;
      } else {
        var vendorURL = window.URL || window.webkitURL;

        video.src = vendorURL.createObjectURL(stream);
      }
      video.onloadedmetadata = function(e) {
        video.play();
      };
    })
    .catch(function(err) {
      console.log(err.name + ": " + err.message);
    });
}