Bug 1433552 - Allow zeroes to CamerasParent's FeasibilityDistance functions. r?jib draft
authorAndreas Pehrson <pehrsons@mozilla.com>
Fri, 02 Feb 2018 16:57:45 +0100
changeset 752657 12c7aa1b25e1bd4558006e47590e454d47cd28f6
parent 750475 841512e696b91825d24c6dd1a18d277c5f7d2be4
push id98334
push userbmo:apehrson@mozilla.com
push dateThu, 08 Feb 2018 18:52:28 +0000
reviewersjib
bugs1433552
milestone60.0a1
Bug 1433552 - Allow zeroes to CamerasParent's FeasibilityDistance functions. r?jib Getting zeroes here is rare, but the numbers come from a platform API so no guarantees are given for them. This patch makes it as permissive as possible. MozReview-Commit-ID: 2bjPRzhk1L7
dom/media/systemservices/CamerasParent.cpp
--- a/dom/media/systemservices/CamerasParent.cpp
+++ b/dom/media/systemservices/CamerasParent.cpp
@@ -43,29 +43,51 @@ std::map<uint32_t, const char *> sDevice
 std::map<uint32_t, webrtc::VideoCaptureCapability> sAllRequestedCapabilities;
 
 uint32_t
 ResolutionFeasibilityDistance(int32_t candidate, int32_t requested)
 {
   // The purpose of this function is to find a smallest resolution
   // which is larger than all requested capabilities.
   // Then we can use down-scaling to fulfill each request.
-  uint32_t distance;
-  if (candidate >= requested) {
-    distance = (candidate - requested) * 1000 / std::max(candidate, requested);
-  } else {
-    distance = 10000 + (requested - candidate) *
-      1000 / std::max(candidate, requested);
+
+  MOZ_DIAGNOSTIC_ASSERT(candidate >= 0, "Candidate unexpectedly negative");
+  MOZ_DIAGNOSTIC_ASSERT(requested >= 0, "Requested unexpectedly negative");
+
+  if (candidate == 0) {
+    // Treat width|height capability of 0 as "can do any".
+    // This allows for orthogonal capabilities that are not in discrete steps.
+    return 0;
   }
-  return distance;
+
+  uint32_t distance =
+    std::abs(candidate - requested) * 1000 / std::max(candidate, requested);
+  if (candidate > requested) {
+    // This is a good case, the candidate is higher than the requested
+    // resolution which makes it feasible.
+    return distance;
+  }
+
+  // This is a bad case, the candidate is lower than the requested resolution.
+  // This is penalized with an added weight of 10000.
+  return 10000 + distance;
 }
 
 uint32_t
 FeasibilityDistance(int32_t candidate, int32_t requested)
 {
+  MOZ_DIAGNOSTIC_ASSERT(candidate >= 0, "Candidate unexpectedly negative");
+  MOZ_DIAGNOSTIC_ASSERT(requested >= 0, "Requested unexpectedly negative");
+
+  if (candidate == 0) {
+    // Treat maxFPS capability of 0 as "can do any".
+    // This allows for orthogonal capabilities that are not in discrete steps.
+    return 0;
+  }
+
   return std::abs(candidate - requested) * 1000 / std::max(candidate, requested);
 }
 
 StaticRefPtr<VideoEngine> CamerasParent::sEngines[CaptureEngine::MaxEngine];
 int32_t CamerasParent::sNumOfOpenCamerasParentEngines = 0;
 int32_t CamerasParent::sNumOfCamerasParents = 0;
 base::Thread* CamerasParent::sVideoCaptureThread = nullptr;
 Monitor* CamerasParent::sThreadMonitor = nullptr;