Proposal: use plain old Java to open devices (Was: Possible Constraint Syntax Compromise)

I feel like I'm beating a dead horse here, but since I've never gotten a 
formal reply to this proposal, here goes...

Instead inventing meta-languages for specifying device Constraints, I'd 
like to propose we use plain old Javascript to look up a device's 
capabilities, and open the device. No funky syntax needed.

---------------------------------------
Sample code:

/**
  * The application's main entry point.
  */
function main() {
   var devices = Devices.list();
   var bestDevice;
   forEach(function(device) {
     var aspectRatio = device.getMaxWidth() * 1.0 / device.getMaxHeight();
     if (!isAcceptable(device))
       continue;
   });

   if (!bestDevice)
     Window.alert("We couldn't find an acceptable camera. Are you sure 
your device is plugged in?");
   var openedDevice = getUserMedia(bestDevice);
   // Hurray!
}

/**
  * @return true if the device is acceptable for the application
  */
function isAcceptable(device) {
   if (aspectRatio === 4/3)
     return device.getMinWidth() >= 800 && device.getMaxWidth() >= 600;
   // Assume 16:9
   return device.getMinWidth() >= 1366 && device.getMaxWidth() >= 768;
}

/**
  * @return true if the second device is better than the first
  */
function isBetter(first, second) {
   if (!first)
     return true;
   return second.getMaxWidth() > first.getMaxWidth() && 
second.getMaxHeight() > first.getMaxHeight();
}
---------------------------------------

Yes, it's more lines of code than the current syntax but:

 1. It's more flexible and deterministic for application developers.
 2. It's easier for browsers to implement.
 3. It's easier to everyone to debug.
 4. And I'd argue, it's actually easier to read. There is no magic at
    play and there is no room for interpretation.

Gili

Received on Tuesday, 20 May 2014 22:51:32 UTC