Bug 1436074 - Reduce turn-off timer by time since we turned on. r?jib
MozReview-Commit-ID: 2AvnJxUSTmy
--- 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");