Search code examples
javascripthtmlwebrtc

navigator.mediaDevices.getUserMedia Video quality and facing mode constraints


I'm not able to specify video quality and camera facing constraints together. Is there a way to combine the below constraints?

{
  audio: true,
  video: { width: 1280, height: 720 }
}

{ audio: true, video: { facingMode: "user" } }

Solution

  • navigator.mediaDevices.getUserMedia({ audio: true, video: getConstraints(1280, 720) });
    
    function getConstraints(videowidth, videoheight) {
        constraints = {
            facingMode: { exact: "environment" },
            width: { min: videowidth, ideal: videowidth, max: videowidth },
            height: { min: videoheight, ideal: videoheight, max: videoheight },
            frameRate: { min: 5, max: 8 }
        };
        return constraints;
    }