Bug 1162412 - Part 2: Order devices by shortest fitness distance. r=jesup, a=lmandel
authorJan-Ivar Bruaroey <jib@mozilla.com>
Thu, 07 May 2015 13:15:49 -0400
changeset 260515 9c1d3c0257ec
parent 260514 c14434ed2197
child 260516 e3045256cb27
push id805
push userryanvm@gmail.com
push date2015-05-18 17:06 +0000
treeherdermozilla-release@4bfd19d00ed4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup, lmandel
bugs1162412
milestone38.0.5
Bug 1162412 - Part 2: Order devices by shortest fitness distance. r=jesup, a=lmandel
dom/media/MediaManager.cpp
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -56,16 +56,18 @@
 
 #if defined(XP_MACOSX)
 #include "nsCocoaFeatures.h"
 #endif
 #if defined (XP_WIN)
 #include "mozilla/WindowsVersion.h"
 #endif
 
+#include <map>
+
 // GetCurrentTime is defined in winbase.h as zero argument macro forwarding to
 // GetTickCount() and conflicts with MediaStream::GetCurrentTime.
 #ifdef GetCurrentTime
 #undef GetCurrentTime
 #endif
 
 // XXX Workaround for bug 986974 to maintain the existing broken semantics
 template<>
@@ -1019,23 +1021,33 @@ static void
   // First apply top-level constraints.
 
   // Stack constraintSets that pass, starting with the required one, because the
   // whole stack must be re-satisfied each time a capability-set is ruled out
   // (this avoids storing state or pushing algorithm into the lower-level code).
   nsTArray<const MediaTrackConstraintSet*> aggregateConstraints;
   aggregateConstraints.AppendElement(&c);
 
+  std::multimap<uint32_t, nsRefPtr<DeviceType>> ordered;
+
   for (uint32_t i = 0; i < candidateSet.Length();) {
-    if (candidateSet[i]->GetBestFitnessDistance(aggregateConstraints) == UINT32_MAX) {
+    uint32_t distance = candidateSet[i]->GetBestFitnessDistance(aggregateConstraints);
+    if (distance == UINT32_MAX) {
       candidateSet.RemoveElementAt(i);
     } else {
+      ordered.insert(std::pair<uint32_t, nsRefPtr<DeviceType>>(distance,
+                                                               candidateSet[i]));
       ++i;
     }
   }
+  // Order devices by shortest distance
+  for (auto& ordinal : ordered) {
+    candidateSet.RemoveElement(ordinal.second);
+    candidateSet.AppendElement(ordinal.second);
+  }
 
   // Then apply advanced constraints.
 
   if (c.mAdvanced.WasPassed()) {
     auto &array = c.mAdvanced.Value();
 
     for (int i = 0; i < int(array.Length()); i++) {
       aggregateConstraints.AppendElement(&array[i]);