Bug 1213517 - Normalize all the constraints internally, not just some. r=jesup
authorJan-Ivar Bruaroey <jib@mozilla.com>
Thu, 30 Jun 2016 15:46:07 -0400
changeset 330476 cadeeb107695107af88f27298ca90f4750c1c5c4
parent 330475 87361f380664a1bfd65b88964b1026156603b97a
child 330477 84fb1be97595f6fdccbc49d8c3ec8d7b34bef37c
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs1213517
milestone50.0a1
Bug 1213517 - Normalize all the constraints internally, not just some. r=jesup MozReview-Commit-ID: BfTleKUxiVU
dom/media/webrtc/MediaTrackConstraints.cpp
dom/media/webrtc/MediaTrackConstraints.h
--- a/dom/media/webrtc/MediaTrackConstraints.cpp
+++ b/dom/media/webrtc/MediaTrackConstraints.cpp
@@ -78,16 +78,111 @@ NormalizedConstraintSet::BooleanRange::B
     }
     if (r.mExact.WasPassed()) {
       mMin = r.mExact.Value();
       mMax = r.mExact.Value();
     }
   }
 }
 
+NormalizedConstraintSet::StringRange::StringRange(
+    const dom::OwningStringOrStringSequenceOrConstrainDOMStringParameters& aOther,
+    bool advanced)
+{
+  if (aOther.IsString()) {
+    if (advanced) {
+      mExact.insert(aOther.GetAsString());
+    } else {
+      mIdeal.insert(aOther.GetAsString());
+    }
+  } else if (aOther.IsStringSequence()) {
+    if (advanced) {
+      mExact.clear();
+      for (auto& str : aOther.GetAsStringSequence()) {
+        mExact.insert(str);
+      }
+    } else {
+      mIdeal.clear();
+      for (auto& str : aOther.GetAsStringSequence()) {
+        mIdeal.insert(str);
+      }
+    }
+  } else {
+    SetFrom(aOther.GetAsConstrainDOMStringParameters());
+  }
+}
+
+void
+NormalizedConstraintSet::StringRange::SetFrom(
+    const ConstrainDOMStringParameters& aOther)
+{
+  if (aOther.mIdeal.WasPassed()) {
+    mIdeal.clear();
+    if (aOther.mIdeal.Value().IsString()) {
+      mIdeal.insert(aOther.mIdeal.Value().GetAsString());
+    } else {
+      for (auto& str : aOther.mIdeal.Value().GetAsStringSequence()) {
+        mIdeal.insert(str);
+      }
+    }
+  }
+  if (aOther.mExact.WasPassed()) {
+    mExact.clear();
+    if (aOther.mExact.Value().IsString()) {
+      mExact.insert(aOther.mExact.Value().GetAsString());
+    } else {
+      for (auto& str : aOther.mExact.Value().GetAsStringSequence()) {
+        mIdeal.insert(str);
+      }
+    }
+  }
+}
+
+auto
+NormalizedConstraintSet::StringRange::Clamp(const ValueType& n) const -> ValueType
+{
+  if (!mExact.size()) {
+    return n;
+  }
+  ValueType result;
+  for (auto& entry : n) {
+    if (mExact.find(entry) != mExact.end()) {
+      result.insert(entry);
+    }
+  }
+  return result;
+}
+
+bool
+NormalizedConstraintSet::StringRange::Intersects(const StringRange& aOther) const
+{
+  if (!mExact.size() || !aOther.mExact.size()) {
+    return true;
+  }
+  for (auto& entry : aOther.mExact) {
+    if (mExact.find(entry) != mExact.end()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+void
+NormalizedConstraintSet::StringRange::Intersect(const StringRange& aOther)
+{
+  if (!aOther.mExact.size()) {
+    return;
+  }
+  for (auto& entry : mExact) {
+    if (aOther.mExact.find(entry) == aOther.mExact.end()) {
+      mExact.erase(entry);
+    }
+  }
+}
+
 FlattenedConstraints::FlattenedConstraints(const dom::MediaTrackConstraints& aOther)
 : NormalizedConstraintSet(aOther, false)
 {
   if (aOther.mAdvanced.WasPassed()) {
     const auto& advanced = aOther.mAdvanced.Value();
     for (size_t i = 0; i < advanced.Length(); i++) {
       NormalizedConstraintSet set(advanced[i], true);
       // Must only apply compatible i.e. inherently non-overconstraining sets
--- a/dom/media/webrtc/MediaTrackConstraints.h
+++ b/dom/media/webrtc/MediaTrackConstraints.h
@@ -8,16 +8,17 @@
 #define MEDIATRACKCONSTRAINTS_H_
 
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/MediaStreamTrackBinding.h"
 #include "mozilla/dom/MediaTrackConstraintSetBinding.h"
 #include "mozilla/dom/MediaTrackSupportedConstraintsBinding.h"
 
 #include <map>
+#include <set>
 
 namespace mozilla {
 
 template<class EnumValuesStrings, class Enum>
 static const char* EnumToASCII(const EnumValuesStrings& aStrings, Enum aValue) {
   return aStrings[uint32_t(aValue)].value;
 }
 
@@ -73,27 +74,57 @@ struct NormalizedConstraintSet
   };
 
   struct BooleanRange : public Range<bool>
   {
     BooleanRange(const dom::OwningBooleanOrConstrainBooleanParameters& aOther,
                  bool advanced);
   };
 
-  // Do you need to add your constraint here? Only if your code uses flattening
+  struct StringRange
+  {
+    typedef std::set<nsString> ValueType;
+    ValueType mExact, mIdeal;
+
+    StringRange(
+        const dom::OwningStringOrStringSequenceOrConstrainDOMStringParameters& aOther,
+        bool advanced);
+
+    void SetFrom(const dom::ConstrainDOMStringParameters& aOther);
+    ValueType Clamp(const ValueType& n) const;
+    ValueType Get(const ValueType& defaultValue) const {
+      return Clamp(mIdeal.size() ? mIdeal : defaultValue);
+    }
+    bool Intersects(const StringRange& aOther) const;
+    void Intersect(const StringRange& aOther);
+  };
+
+  // All new constraints should be added here whether they use flattening or not
   LongRange mWidth, mHeight;
   DoubleRange mFrameRate;
+  StringRange mFacingMode;
+  nsString mMediaSource;
+  long long mBrowserWindow;
+  bool mScrollWithPage;
+  StringRange mDeviceId;
   LongRange mViewportOffsetX, mViewportOffsetY, mViewportWidth, mViewportHeight;
   BooleanRange mEchoCancellation, mMozNoiseSuppression, mMozAutoGainControl;
 
   NormalizedConstraintSet(const dom::MediaTrackConstraintSet& aOther,
                           bool advanced)
   : mWidth(aOther.mWidth, advanced)
   , mHeight(aOther.mHeight, advanced)
   , mFrameRate(aOther.mFrameRate, advanced)
+  , mFacingMode(aOther.mFacingMode, advanced)
+  , mMediaSource(aOther.mMediaSource)
+  , mBrowserWindow(aOther.mBrowserWindow.WasPassed() ?
+                   aOther.mBrowserWindow.Value() : 0)
+  , mScrollWithPage(aOther.mScrollWithPage.WasPassed() ?
+                    aOther.mScrollWithPage.Value() : false)
+  , mDeviceId(aOther.mDeviceId, advanced)
   , mViewportOffsetX(aOther.mViewportOffsetX, advanced)
   , mViewportOffsetY(aOther.mViewportOffsetY, advanced)
   , mViewportWidth(aOther.mViewportWidth, advanced)
   , mViewportHeight(aOther.mViewportHeight, advanced)
   , mEchoCancellation(aOther.mEchoCancellation, advanced)
   , mMozNoiseSuppression(aOther.mMozNoiseSuppression, advanced)
   , mMozAutoGainControl(aOther.mMozAutoGainControl, advanced) {}
 };