Bug 1433552 - Allow zeroes to CamerasParent's FeasibilityDistance functions. r=jib
authorAndreas Pehrson <pehrsons@mozilla.com>
Fri, 02 Feb 2018 16:57:45 +0100
changeset 403167 879aa8e4a7abc87eb2053d07666e92a38afbb674
parent 403166 b4fbfcf7789106f2befc30d3c81fa136be3c7ad2
child 403168 64ec4273a0d7627e6f65c4c89ea17f8bd5fd4bf0
push id59376
push userpehrsons@gmail.com
push dateFri, 09 Feb 2018 17:08:28 +0000
treeherderautoland@879aa8e4a7ab [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1433552
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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;