src/browser/src/createQRScannerInternal.js
function getCameraSpecsById(deviceId){

    // return a getUserMedia Constraints
    function getConstraintObj(deviceId, facingMode, width, height){
      var obj = { audio: false, video: {} };
      obj.video.deviceId = {exact: deviceId};
      if(facingMode) {
        obj.video.facingMode = {exact: facingMode};
      }
      if(width) {
        obj.video.width = {exact: width};
      }
      if(height) {
        obj.video.height = {exact: height};
      }
      return obj;
    }

    var facingModeConstraints = facingModes.map(function(mode){
    	return getConstraintObj(deviceId, mode);
    });
    var widthConstraints = standardWidthsAndHeights.map(function(width){
    	return getConstraintObj(deviceId, null, width);
    });
    var heightConstraints = standardWidthsAndHeights.map(function(height){
    	return getConstraintObj(deviceId, null, null, height);
    });

    // create a promise which tries to resolve the best constraints for this deviceId
    // rather than reject, failures return a value of `null`
    function getFirstResolvingConstraint(constraintsBestToWorst){
      return new Promise(function(resolveBestConstraints){
        // build a chain of promises which either resolves or continues searching
        return constraintsBestToWorst.reduce(function(chain, next){
          return chain.then(function(searchState){
            if(searchState.found){
              // The best working constraint was found. Skip further tests.
              return searchState;
            } else {
              searchState.nextConstraint = next;
              return window.navigator.mediaDevices.getUserMedia(searchState.nextConstraint).then(function(mediaStream){
                // We found the first working constraint object, now we can stop
                // the stream and short-circuit the search.
                killStream(mediaStream);
                searchState.found = true;
                return searchState;
              }, function(){
                // didn't get a media stream. The search continues:
                return searchState;
              });
            }
          });
        }, Promise.resolve({
          // kick off the search:
          found: false,
          nextConstraint: {}
        })).then(function(searchState){
          if(searchState.found){
            resolveBestConstraints(searchState.nextConstraint);
          } else {
            resolveBestConstraints(null);
          }
        });
      });
    }

    return getFirstResolvingConstraint(facingModeConstraints).then(function(facingModeSpecs){
      return getFirstResolvingConstraint(widthConstraints).then(function(widthSpecs){
        return getFirstResolvingConstraint(heightConstraints).then(function(heightSpecs){
          return {
            deviceId: deviceId,
            facingMode: facingModeSpecs === null ? null : facingModeSpecs.video.facingMode.exact,
            width: widthSpecs === null ? null : widthSpecs.video.width.exact,
            height: heightSpecs === null ? null : heightSpecs.video.height.exact
          };
        });
      });
    });
  }
function getCameraSpecsById(deviceId){

    // return a getUserMedia Constraints
    function getConstraintObj(deviceId, facingMode, width, height){
      var obj = { audio: false, video: {} };
      obj.video.deviceId = {exact: deviceId};
      if(facingMode) {
        obj.video.facingMode = {exact: facingMode};
      }
      if(width) {
        obj.video.width = {exact: width};
      }
      if(height) {
        obj.video.height = {exact: height};
      }
      return obj;
    }

    var facingModeConstraints = facingModes.map(function(mode){
    	return getConstraintObj(deviceId, mode);
    });
    var widthConstraints = standardWidthsAndHeights.map(function(width){
    	return getConstraintObj(deviceId, null, width);
    });
    var heightConstraints = standardWidthsAndHeights.map(function(height){
    	return getConstraintObj(deviceId, null, null, height);
    });

    // create a promise which tries to resolve the best constraints for this deviceId
    // rather than reject, failures return a value of `null`
    function getFirstResolvingConstraint(constraintsBestToWorst){
      return new Promise(function(resolveBestConstraints){
        // build a chain of promises which either resolves or continues searching
        return constraintsBestToWorst.reduce(function(chain, next){
          return chain.then(function(searchState){
            if(searchState.found){
              // The best working constraint was found. Skip further tests.
              return searchState;
            } else {
              searchState.nextConstraint = next;
              return window.navigator.mediaDevices.getUserMedia(searchState.nextConstraint).then(function(mediaStream){
                // We found the first working constraint object, now we can stop
                // the stream and short-circuit the search.
                killStream(mediaStream);
                searchState.found = true;
                return searchState;
              }, function(){
                // didn't get a media stream. The search continues:
                return searchState;
              });
            }
          });
        }, Promise.resolve({
          // kick off the search:
          found: false,
          nextConstraint: {}
        })).then(function(searchState){
          if(searchState.found){
            resolveBestConstraints(searchState.nextConstraint);
          } else {
            resolveBestConstraints(null);
          }
        });
      });
    }

    return getFirstResolvingConstraint(facingModeConstraints).then(function(facingModeSpecs){
      return getFirstResolvingConstraint(widthConstraints).then(function(widthSpecs){
        return getFirstResolvingConstraint(heightConstraints).then(function(heightSpecs){
          return {
            deviceId: deviceId,
            facingMode: facingModeSpecs === null ? null : facingModeSpecs.video.facingMode.exact,
            width: widthSpecs === null ? null : widthSpecs.video.width.exact,
            height: heightSpecs === null ? null : heightSpecs.video.height.exact
          };
        });
      });
    });
  }
function getCameraSpecsById ( deviceId ) { // return a getUserMedia Constraints function getConstraintObj ( deviceId , facingMode , width , height ) { var obj = { audio : false , video : { } } ; obj . video . deviceId = { exact : deviceId } ; if ( facingMode ) { obj . video . facingMode = { exact : facingMode } ; } if ( width ) { obj . video . width = { exact : width } ; } if ( height ) { obj . video . height = { exact : height } ; } return obj ; } var facingModeConstraints = facingModes . map ( function ( mode ) { return getConstraintObj ( deviceId , mode ) ; } ) ; var widthConstraints = standardWidthsAndHeights . map ( function ( width ) { return getConstraintObj ( deviceId , null , width ) ; } ) ; var heightConstraints = standardWidthsAndHeights . map ( function ( height ) { return getConstraintObj ( deviceId , null , null , height ) ; } ) ; // create a promise which tries to resolve the best constraints for this deviceId // rather than reject, failures return a value of `null` function getFirstResolvingConstraint ( constraintsBestToWorst ) { return new Promise ( function ( resolveBestConstraints ) { // build a chain of promises which either resolves or continues searching return constraintsBestToWorst . reduce ( function ( chain , next ) { return chain . then ( function ( searchState ) { if ( searchState . found ) { // The best working constraint was found. Skip further tests. return searchState ; } else { searchState . nextConstraint = next ; return window . navigator . mediaDevices . getUserMedia ( searchState . nextConstraint ) . then ( function ( mediaStream ) { // We found the first working constraint object, now we can stop // the stream and short-circuit the search. killStream ( mediaStream ) ; searchState . found = true ; return searchState ; } , function ( ) { // didn't get a media stream. The search continues: return searchState ; } ) ; } } ) ; } , Promise . resolve ( { // kick off the search: found : false , nextConstraint : { } } ) ) . then ( function ( searchState ) { if ( searchState . found ) { resolveBestConstraints ( searchState . nextConstraint ) ; } else { resolveBestConstraints ( null ) ; } } ) ; } ) ; } return getFirstResolvingConstraint ( facingModeConstraints ) . then ( function ( facingModeSpecs ) { return getFirstResolvingConstraint ( widthConstraints ) . then ( function ( widthSpecs ) { return getFirstResolvingConstraint ( heightConstraints ) . then ( function ( heightSpecs ) { return { deviceId : deviceId , facingMode : facingModeSpecs === null ? null : facingModeSpecs . video . facingMode . exact , width : widthSpecs === null ? null : widthSpecs . video . width . exact , height : heightSpecs === null ? null : heightSpecs . video . height . exact } ; } ) ; } ) ; } ) ; }
For performance, we test best-to-worst constraints. Once we find a match, we move to the next test. Since `ConstraintNotSatisfiedError`s are thrown much faster than streams can be started and stopped, the scan is much faster, even though it may iterate through more constraint objects.
For performance we test best - to - worst constraints . Once we find a match we move to the next test . Since ConstraintNotSatisfiedError s are thrown much faster than streams can be started and stopped the scan is much faster even though it may iterate through more constraint objects .