Bug 1434946 - Set framerate in settings when capabilities are updated. r?jib draft
authorAndreas Pehrson <pehrsons@mozilla.com>
Thu, 01 Feb 2018 17:00:59 +0100
changeset 750464 b69aacbaeb5cb23a6cf97cc44097b34f9726266b
parent 750463 0fbfe6dcbced7dafcdc089d076763faad50973d7
child 750465 aea6cacd7d95f4c907aa1180c93be05af25b9fd3
push id97667
push userbmo:apehrson@mozilla.com
push dateFri, 02 Feb 2018 10:01:06 +0000
reviewersjib
bugs1434946, 1299515
milestone60.0a1
Bug 1434946 - Set framerate in settings when capabilities are updated. r?jib This adds back the `framerate` update that was removed in bug 1299515. It also fixes a threading issue (not really an issue, but it broke the documented policy) where Start() wrote to mCapability without holding mMutex. MozReview-Commit-ID: Jda5moNhlkM
dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
--- a/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
+++ b/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
@@ -41,16 +41,17 @@ MediaEngineRemoteVideoSource::MediaEngin
   , mMutex("MediaEngineRemoteVideoSource::mMutex")
   , mRescalingBufferPool(/* zero_initialize */ false,
                          /* max_number_of_buffers */ 1)
   , mSettings(MakeAndAddRef<media::Refcountable<MediaTrackSettings>>())
 {
   MOZ_ASSERT(aMediaSource != MediaSourceEnum::Other);
   mSettings->mWidth.Construct(0);
   mSettings->mHeight.Construct(0);
+  mSettings->mFrameRate.Construct(0);
   Init();
 }
 
 void
 MediaEngineRemoteVideoSource::Init()
 {
   LOG((__PRETTY_FUNCTION__));
   AssertIsOnOwningThread();
@@ -182,36 +183,38 @@ MediaEngineRemoteVideoSource::Allocate(
 {
   LOG((__PRETTY_FUNCTION__));
   AssertIsOnOwningThread();
 
   MOZ_ASSERT(mInitDone);
   MOZ_ASSERT(mState == kReleased);
 
   NormalizedConstraints constraints(aConstraints);
-  LOG(("ChooseCapability(kFitness) for mTargetCapability and mCapability (Allocate) ++"));
-  if (!ChooseCapability(constraints, aPrefs, aDeviceId, mCapability, kFitness)) {
+  webrtc::CaptureCapability newCapability;
+  LOG(("ChooseCapability(kFitness) for mCapability (Allocate) ++"));
+  if (!ChooseCapability(constraints, aPrefs, aDeviceId, newCapability, kFitness)) {
     *aOutBadConstraint =
       MediaConstraintsHelper::FindBadConstraint(constraints, this, aDeviceId);
     return NS_ERROR_FAILURE;
   }
-  LOG(("ChooseCapability(kFitness) for mTargetCapability and mCapability (Allocate) --"));
+  LOG(("ChooseCapability(kFitness) for mCapability (Allocate) --"));
 
   if (camera::GetChildAndCall(&camera::CamerasChild::AllocateCaptureDevice,
                               mCapEngine, mUniqueId.get(),
                               kMaxUniqueIdLength, mCaptureIndex,
                               aPrincipalInfo)) {
     return NS_ERROR_FAILURE;
   }
 
   *aOutHandle = nullptr;
 
   {
     MutexAutoLock lock(mMutex);
     mState = kAllocated;
+    mCapability = newCapability;
   }
 
   LOG(("Video device %d allocated", mCaptureIndex));
   return NS_OK;
 }
 
 nsresult
 MediaEngineRemoteVideoSource::Deallocate(const RefPtr<const AllocationHandle>& aHandle)
@@ -299,16 +302,23 @@ MediaEngineRemoteVideoSource::Start(cons
   if (camera::GetChildAndCall(&camera::CamerasChild::StartCapture,
                               mCapEngine, mCaptureIndex, mCapability, this)) {
     LOG(("StartCapture failed"));
     MutexAutoLock lock(mMutex);
     mState = kStopped;
     return NS_ERROR_FAILURE;
   }
 
+  NS_DispatchToMainThread(NS_NewRunnableFunction(
+      "MediaEngineRemoteVideoSource::SetLastCapability",
+      [settings = mSettings, cap = mCapability]() mutable {
+    settings->mWidth.Value() = cap.width;
+    settings->mHeight.Value() = cap.height;
+    settings->mFrameRate.Value() = cap.maxFPS;
+  }));
 
   return NS_OK;
 }
 
 nsresult
 MediaEngineRemoteVideoSource::Stop(const RefPtr<const AllocationHandle>& aHandle)
 {
   LOG((__PRETTY_FUNCTION__));
@@ -355,19 +365,21 @@ MediaEngineRemoteVideoSource::Reconfigur
     return NS_ERROR_FAILURE;
   }
   LOG(("ChooseCapability(kFitness) for mTargetCapability (Reconfigure) --"));
 
   if (mCapability == newCapability) {
     return NS_OK;
   }
 
-  // Start() applies mCapability on the device.
-  mCapability = newCapability;
-
+  {
+    MutexAutoLock lock(mMutex);
+    // Start() applies mCapability on the device.
+    mCapability = newCapability;
+  }
 
   if (mState == kStarted) {
     // Allocate always returns a null AllocationHandle.
     // We can safely pass nullptr below.
     nsresult rv = Stop(nullptr);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }