Bug 1213517 - Consider competing constraints in getUserMedia+applyConstraints. r=jesup
☠☠ backed out by 4e9cca9e0124 ☠ ☠
authorJan-Ivar Bruaroey <jib@mozilla.com>
Mon, 06 Jun 2016 13:15:50 -0400
changeset 330311 6f025025d2597e95ef01c04b8d97f12c047c941e
parent 330310 9701bb4384d8967b118921d90fa40eb1bb3f9c08
child 330312 c0c0d3eb6dc98dc067a2946992e17d6abdf5fca4
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 - Consider competing constraints in getUserMedia+applyConstraints. r=jesup MozReview-Commit-ID: 9jzjNrJVUMX
dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
--- a/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
+++ b/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
@@ -108,45 +108,61 @@ MediaEngineRemoteVideoSource::Allocate(
   LOG((__PRETTY_FUNCTION__));
   AssertIsOnOwningThread();
 
   if (!mInitDone) {
     LOG(("Init not done"));
     return NS_ERROR_FAILURE;
   }
 
+  AutoTArray<const NormalizedConstraints*, 10> allConstraints;
+  for (auto& registered : mRegisteredHandles) {
+    allConstraints.AppendElement(&registered->mConstraints);
+  }
   RefPtr<AllocationHandle> handle = new AllocationHandle(aConstraints);
-  mRegisteredHandles.AppendElement(handle);
+  allConstraints.AppendElement(&handle->mConstraints);
+
+  NormalizedConstraints netConstraints(allConstraints);
+  if (netConstraints.mOverconstrained) {
+    return NS_ERROR_FAILURE;
+  }
+
+  if (!ChooseCapability(netConstraints, aPrefs, aDeviceId)) {
+    return NS_ERROR_FAILURE;
+  }
 
   if (mState == kReleased) {
-    // Note: if shared, we don't allow a later opener to affect the resolution.
-    // (This may change depending on spec changes for Constraints/settings)
-
-    if (!ChooseCapability(handle->mConstraints, aPrefs, aDeviceId)) {
-      return NS_ERROR_UNEXPECTED;
-    }
-
     if (mozilla::camera::GetChildAndCall(
       &mozilla::camera::CamerasChild::AllocateCaptureDevice,
       mCapEngine, GetUUID().get(), kMaxUniqueIdLength, mCaptureIndex, aOrigin)) {
       return NS_ERROR_FAILURE;
     }
     mState = kAllocated;
     LOG(("Video device %d allocated for %s", mCaptureIndex,
          PromiseFlatCString(aOrigin).get()));
-  } else if (MOZ_LOG_TEST(GetMediaManagerLog(), mozilla::LogLevel::Debug)) {
-    MonitorAutoLock lock(mMonitor);
-    if (mSources.IsEmpty()) {
-      MOZ_ASSERT(mPrincipalHandles.IsEmpty());
-      LOG(("Video device %d reallocated", mCaptureIndex));
-    } else {
-      LOG(("Video device %d allocated shared", mCaptureIndex));
+  } else {
+    camera::GetChildAndCall(&camera::CamerasChild::StopCapture, mCapEngine,
+                            mCaptureIndex);
+    if (camera::GetChildAndCall(&camera::CamerasChild::StartCapture, mCapEngine,
+                                mCaptureIndex, mCapability, this)) {
+      LOG(("StartCapture failed"));
+      return NS_ERROR_FAILURE;
+    }
+    if (MOZ_LOG_TEST(GetMediaManagerLog(), mozilla::LogLevel::Debug)) {
+      MonitorAutoLock lock(mMonitor);
+      if (mSources.IsEmpty()) {
+        MOZ_ASSERT(mPrincipalHandles.IsEmpty());
+        LOG(("Video device %d reallocated", mCaptureIndex));
+      } else {
+        LOG(("Video device %d allocated shared", mCaptureIndex));
+      }
     }
   }
 
+  mRegisteredHandles.AppendElement(handle);
   ++mNrAllocations;
   handle.forget(aOutHandle);
   return NS_OK;
 }
 
 nsresult
 MediaEngineRemoteVideoSource::Deallocate(BaseAllocationHandle* aHandle)
 {
@@ -271,33 +287,50 @@ MediaEngineRemoteVideoSource::Restart(Ba
 {
   AssertIsOnOwningThread();
   if (!mInitDone) {
     LOG(("Init not done"));
     return NS_ERROR_FAILURE;
   }
   MOZ_ASSERT(aHandle);
   auto handle = static_cast<AllocationHandle*>(aHandle);
-  handle->mConstraints = NormalizedConstraints(aConstraints);
-  if (!ChooseCapability(handle->mConstraints, aPrefs, aDeviceId)) {
-    return NS_ERROR_NOT_AVAILABLE;
+  RefPtr<AllocationHandle> temp = new AllocationHandle(aConstraints);
+  temp->mConstraints = NormalizedConstraints(aConstraints);
+
+  AutoTArray<const NormalizedConstraints*, 10> allConstraints;
+  for (auto& registered : mRegisteredHandles) {
+    if (registered.get() == handle) {
+      continue; // Don't count old constraints
+    }
+    allConstraints.AppendElement(&registered->mConstraints);
+  }
+  allConstraints.AppendElement(&temp->mConstraints);
+
+  NormalizedConstraints netConstraints(allConstraints);
+  if (netConstraints.mOverconstrained) {
+    return NS_ERROR_FAILURE;
+  }
+
+  if (!ChooseCapability(netConstraints, aPrefs, aDeviceId)) {
+    return NS_ERROR_FAILURE;
   }
   if (mState != kStarted) {
     return NS_OK;
   }
 
   mozilla::camera::GetChildAndCall(
     &mozilla::camera::CamerasChild::StopCapture,
     mCapEngine, mCaptureIndex);
   if (mozilla::camera::GetChildAndCall(
     &mozilla::camera::CamerasChild::StartCapture,
     mCapEngine, mCaptureIndex, mCapability, this)) {
     LOG(("StartCapture failed"));
     return NS_ERROR_FAILURE;
   }
+  handle->mConstraints = temp->mConstraints;
   return NS_OK;
 }
 
 void
 MediaEngineRemoteVideoSource::NotifyPull(MediaStreamGraph* aGraph,
                                          SourceMediaStream* aSource,
                                          TrackID aID, StreamTime aDesiredTime,
                                          const PrincipalHandle& aPrincipalHandle)