Bug 1213517 - Consolidate camera Allocate's and Restart's constraints logic. r=padenot
authorJan-Ivar Bruaroey <jib@mozilla.com>
Sat, 18 Jun 2016 16:11:54 -0400
changeset 389182 62c5705fc282e760ae52c60671460c297c0714a6
parent 389181 2a924630566f530e4d80d9f24c714d9eecb2edfa
child 389183 bf9c34d7ecdf254f9c7730eef3d228e2968deb80
push id23312
push userdmitchell@mozilla.com
push dateMon, 18 Jul 2016 17:58:50 +0000
reviewerspadenot
bugs1213517
milestone50.0a1
Bug 1213517 - Consolidate camera Allocate's and Restart's constraints logic. r=padenot MozReview-Commit-ID: CrARq0QZzrO
dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
dom/media/webrtc/MediaEngineRemoteVideoSource.h
--- a/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
+++ b/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
@@ -109,62 +109,32 @@ 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);
-  allConstraints.AppendElement(&handle->mConstraints);
-
-  NormalizedConstraints netConstraints(allConstraints);
-  if (netConstraints.mBadConstraint) {
-    *aOutBadConstraint = netConstraints.mBadConstraint;
-    return NS_ERROR_NOT_AVAILABLE;
-  }
-
-  if (!ChooseCapability(netConstraints, aPrefs, aDeviceId)) {
-    *aOutBadConstraint = FindBadConstraint(netConstraints, *this, aDeviceId);
-    return NS_ERROR_NOT_AVAILABLE;
-  }
+  RefPtr<AllocationHandle> handle = new AllocationHandle(aConstraints, aOrigin);
 
-  if (mState == kReleased) {
-    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 {
-    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));
-      }
+  nsresult rv = UpdateNew(handle, aPrefs, aDeviceId, aOutBadConstraint);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  if (mState == kStarted &&
+      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)
@@ -328,27 +298,52 @@ MediaEngineRemoteVideoSource::UpdateExis
     *aOutBadConstraint = netConstraints.mBadConstraint;
     return NS_ERROR_FAILURE;
   }
 
   if (!ChooseCapability(netConstraints, aPrefs, aDeviceId)) {
     *aOutBadConstraint = FindBadConstraint(netConstraints, *this, aDeviceId);
     return NS_ERROR_FAILURE;
   }
-  MOZ_ASSERT(mState == kStarted || !aNewConstraints);
+
+  switch (mState) {
+    case kReleased:
+      MOZ_ASSERT(aHandle);
+      MOZ_ASSERT(!aNewConstraints);
+      MOZ_ASSERT(!mRegisteredHandles.Length());
+      if (camera::GetChildAndCall(&camera::CamerasChild::AllocateCaptureDevice,
+                                  mCapEngine, GetUUID().get(),
+                                  kMaxUniqueIdLength, mCaptureIndex,
+                                  aHandle->mOrigin)) {
+        return NS_ERROR_FAILURE;
+      }
+      mState = kAllocated;
+      mLastCapability = mCapability;
+      LOG(("Video device %d allocated for %s", mCaptureIndex,
+           aHandle->mOrigin.get()));
+      break;
 
-  if (mState == kStarted && mCapability != mLastCapability) {
-    camera::GetChildAndCall(&camera::CamerasChild::StopCapture,
-                            mCapEngine, mCaptureIndex);
-    if (camera::GetChildAndCall(&camera::CamerasChild::StartCapture,
-                                mCapEngine, mCaptureIndex, mCapability, this)) {
-      LOG(("StartCapture failed"));
-      return NS_ERROR_FAILURE;
-    }
-    mLastCapability = mCapability;
+    case kStarted:
+      if (mCapability != mLastCapability) {
+        camera::GetChildAndCall(&camera::CamerasChild::StopCapture,
+                                mCapEngine, mCaptureIndex);
+        if (camera::GetChildAndCall(&camera::CamerasChild::StartCapture,
+                                    mCapEngine, mCaptureIndex, mCapability,
+                                    this)) {
+          LOG(("StartCapture failed"));
+          return NS_ERROR_FAILURE;
+        }
+        mLastCapability = mCapability;
+      }
+      break;
+
+      default:
+        LOG(("Video device %d %s in ignored state %d", mCaptureIndex,
+             (aHandle? aHandle->mOrigin.get() : ""), mState));
+        break;
   }
   if (aHandle && aNewConstraints) {
     aHandle->mConstraints = *aNewConstraints;
   }
   return NS_OK;
 }
 
 void
--- a/dom/media/webrtc/MediaEngineRemoteVideoSource.h
+++ b/dom/media/webrtc/MediaEngineRemoteVideoSource.h
@@ -69,22 +69,24 @@ public:
   // MediaEngineCameraVideoSource
   MediaEngineRemoteVideoSource(int aIndex, mozilla::camera::CaptureEngine aCapEngine,
                                dom::MediaSourceEnum aMediaSource,
                                const char* aMonitorName = "RemoteVideo.Monitor");
 
   class AllocationHandle : public BaseAllocationHandle
   {
   public:
-    AllocationHandle(const dom::MediaTrackConstraints& aConstraints)
-      : mConstraints(aConstraints) {}
+    AllocationHandle(const dom::MediaTrackConstraints& aConstraints,
+                     const nsACString& aOrigin)
+      : mConstraints(aConstraints), mOrigin(aOrigin) {}
   private:
     ~AllocationHandle() override {}
   public:
     NormalizedConstraints mConstraints;
+    nsCString mOrigin;
   };
 
   nsresult Allocate(const dom::MediaTrackConstraints& aConstraints,
                     const MediaEnginePrefs& aPrefs,
                     const nsString& aDeviceId,
                     const nsACString& aOrigin,
                     BaseAllocationHandle** aOutHandle,
                     const char** aOutBadConstraint) override;