Bug 1436074 - Reduce turn-off timer by time since we turned on. r?jib draft
authorAndreas Pehrson <pehrsons@mozilla.com>
Wed, 14 Feb 2018 16:35:45 +0100
changeset 758408 dbc5f40c95d43ac8452ed16bb36d5fbff4ce0512
parent 758407 30a5743f1657d7b8bc6730f2640bce50848011c6
child 758887 473713f6c61a46d48745defb4de2c127ba375182
push id100042
push userbmo:apehrson@mozilla.com
push dateThu, 22 Feb 2018 11:33:46 +0000
reviewersjib
bugs1436074
milestone60.0a1
Bug 1436074 - Reduce turn-off timer by time since we turned on. r?jib MozReview-Commit-ID: 2AvnJxUSTmy
dom/media/MediaManager.cpp
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -169,16 +169,20 @@ struct DeviceState {
   // true if mDevice is currently enabled, i.e., turned on and capturing.
   // MainThread only.
   bool mDeviceEnabled = false;
 
   // true if the application has currently enabled mDevice.
   // MainThread only.
   bool mTrackEnabled = false;
 
+  // Time when the application last enabled mDevice.
+  // MainThread only.
+  TimeStamp mTrackEnabledTime;
+
   // true if an operation to Start() or Stop() mDevice has been dispatched to
   // the media thread and is not finished yet.
   // MainThread only.
   bool mOperationInProgress = false;
 
   // true if we are allowed to turn off the underlying source while all tracks
   // are disabled.
   // MainThread only.
@@ -3899,16 +3903,17 @@ SourceListener::InitializeAsync()
           continue;
         }
         MOZ_DIAGNOSTIC_ASSERT(!state->mTrackEnabled);
         MOZ_DIAGNOSTIC_ASSERT(!state->mDeviceEnabled);
         MOZ_DIAGNOSTIC_ASSERT(!state->mStopped);
 
         state->mDeviceEnabled = true;
         state->mTrackEnabled = true;
+        state->mTrackEnabledTime = TimeStamp::Now();
       }
       return InitPromise::CreateAndResolve(true, __func__);
     }, [self = RefPtr<SourceListener>(this), this](RefPtr<MediaMgrError>&& aResult)
     {
       if (mStopped) {
         return InitPromise::CreateAndReject(Move(aResult), __func__);
       }
 
@@ -4074,24 +4079,30 @@ SourceListener::SetEnabledFor(TrackID aT
 
   // All paths from here on must end in setting `state.mOperationInProgress`
   // to false.
   state.mOperationInProgress = true;
 
   RefPtr<MediaTimerPromise> timerPromise;
   if (aEnable) {
     timerPromise = MediaTimerPromise::CreateAndResolve(true, __func__);
+    state.mTrackEnabledTime = TimeStamp::Now();
   } else {
-    const TimeDuration offDelay = TimeDuration::FromMilliseconds(
+    const TimeDuration maxDelay = TimeDuration::FromMilliseconds(
       Preferences::GetUint(
         aTrackID == kAudioTrack
           ? "media.getusermedia.microphone.off_while_disabled.delay_ms"
           : "media.getusermedia.camera.off_while_disabled.delay_ms",
         3000));
-    timerPromise = state.mDisableTimer->WaitFor(offDelay, __func__);
+    const TimeDuration durationEnabled =
+      TimeStamp::Now() - state.mTrackEnabledTime;
+    const TimeDuration delay =
+      TimeDuration::Max(TimeDuration::FromMilliseconds(0),
+                        maxDelay - durationEnabled);
+    timerPromise = state.mDisableTimer->WaitFor(delay, __func__);
   }
 
   typedef MozPromise<nsresult, bool, /* IsExclusive = */ true> DeviceOperationPromise;
   RefPtr<SourceListener> self = this;
   timerPromise->Then(GetMainThreadSerialEventTarget(), __func__,
     [self, this, &state, aTrackID, aEnable]() mutable {
       MOZ_ASSERT(state.mDeviceEnabled != aEnable,
                  "Device operation hasn't started");