Bug 1433552 - Allow zeroes to CamerasParent's FeasibilityDistance functions. r=jib a=lizzard
authorAndreas Pehrson <pehrsons@mozilla.com>
Fri, 02 Feb 2018 16:57:45 +0100
changeset 452362 10d1af7903cb7f348453e5cca0a7901e9f56faa5
parent 452361 3c16503565e67e1a4046bf19016f7b4b15515532
child 452363 47d05846b2c1459afec5c222b2328f2b0c4f6a0b
push id8696
push usernerli@mozilla.com
push dateTue, 13 Feb 2018 13:50:12 +0000
treeherdermozilla-beta@30d165841f3d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib, lizzard
bugs1433552
milestone59.0
Bug 1433552 - Allow zeroes to CamerasParent's FeasibilityDistance functions. r=jib a=lizzard 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);
 }
 
 RefPtr<VideoEngine> CamerasParent::sEngines[CaptureEngine::MaxEngine];
 int32_t CamerasParent::sNumOfOpenCamerasParentEngines = 0;
 int32_t CamerasParent::sNumOfCamerasParents = 0;
 base::Thread* CamerasParent::sVideoCaptureThread = nullptr;
 Monitor* CamerasParent::sThreadMonitor = nullptr;