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
--- 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;