Bug 1625615 - part2 : suspend or resume media element according to docShell's SuspendMediaWhenInactive r=bryce

If docShell's `SuspendMediaWhenInactive` is true, then we should suspend or resume the media element according to the docshell active state when the docshell changes it active state.

Differential Revision: https://phabricator.services.mozilla.com/D69671
This commit is contained in:
alwu
2020-04-06 22:30:10 +00:00
parent 7433ac1da4
commit 2d3e07021e
2 changed files with 32 additions and 13 deletions

View File

@@ -4308,7 +4308,7 @@ void HTMLMediaElement::PlayInternal(bool aHandlingUserInput) {
if (mDecoder->IsEnded()) {
SetCurrentTime(0);
}
if (!mSuspendedForInactiveDocument) {
if (!mSuspendedByInactiveDocOrDocshell) {
mDecoder->Play();
}
}
@@ -5052,13 +5052,13 @@ nsresult HTMLMediaElement::FinishDecoderSetup(MediaDecoder* aDecoder) {
return NS_ERROR_FAILURE;
}
if (mSuspendedForInactiveDocument) {
if (mSuspendedByInactiveDocOrDocshell) {
mDecoder->Suspend();
}
if (!mPaused) {
SetPlayedOrSeeked(true);
if (!mSuspendedForInactiveDocument) {
if (!mSuspendedByInactiveDocOrDocshell) {
mDecoder->Play();
}
}
@@ -5074,7 +5074,7 @@ void HTMLMediaElement::UpdateSrcMediaStreamPlaying(uint32_t aFlags) {
}
bool shouldPlay = !(aFlags & REMOVING_SRC_STREAM) && !mPaused &&
!mSuspendedForInactiveDocument;
!mSuspendedByInactiveDocOrDocshell;
if (shouldPlay == mSrcStreamIsPlaying) {
return;
}
@@ -5948,7 +5948,7 @@ void HTMLMediaElement::ChangeReadyState(nsMediaReadyState aState) {
if (oldState < HAVE_FUTURE_DATA && mReadyState >= HAVE_FUTURE_DATA) {
DispatchAsyncEvent(NS_LITERAL_STRING("canplay"));
if (!mPaused) {
if (mDecoder && !mSuspendedForInactiveDocument) {
if (mDecoder && !mSuspendedByInactiveDocOrDocshell) {
MOZ_ASSERT(AutoplayPolicy::IsAllowedToPlay(*this));
mDecoder->Play();
}
@@ -6023,7 +6023,7 @@ bool HTMLMediaElement::CanActivateAutoplay() {
return false;
}
if (mSuspendedForInactiveDocument) {
if (mSuspendedByInactiveDocOrDocshell) {
return false;
}
@@ -6067,7 +6067,7 @@ void HTMLMediaElement::CheckAutoplayDataReady() {
if (mCurrentPlayRangeStart == -1.0) {
mCurrentPlayRangeStart = CurrentTime();
}
MOZ_ASSERT(!mSuspendedForInactiveDocument);
MOZ_ASSERT(!mSuspendedByInactiveDocOrDocshell);
mDecoder->Play();
} else if (mSrcStream) {
SetPlayedOrSeeked(true);
@@ -6366,11 +6366,11 @@ void HTMLMediaElement::UpdateMediaSize(const nsIntSize& aSize) {
void HTMLMediaElement::SuspendOrResumeElement(bool aSuspendElement) {
LOG(LogLevel::Debug, ("%p SuspendOrResumeElement(suspend=%d) hidden=%d", this,
aSuspendElement, OwnerDoc()->Hidden()));
if (aSuspendElement == mSuspendedForInactiveDocument) {
if (aSuspendElement == mSuspendedByInactiveDocOrDocshell) {
return;
}
mSuspendedForInactiveDocument = aSuspendElement;
mSuspendedByInactiveDocOrDocshell = aSuspendElement;
UpdateSrcMediaStreamPlaying();
UpdateAudioChannelPlayingState();
@@ -6436,6 +6436,16 @@ bool HTMLMediaElement::IsBeingDestroyed() {
return isBeingDestroyed;
}
bool HTMLMediaElement::ShouldBeSuspendedByInactiveDocShell() const {
nsIDocShell* docShell = OwnerDoc()->GetDocShell();
if (!docShell) {
return false;
}
bool isDocShellActive = false;
docShell->GetIsActive(&isDocShellActive);
return !isDocShellActive && docShell->GetSuspendMediaWhenInactive();
}
void HTMLMediaElement::NotifyOwnerDocumentActivityChanged() {
bool visible = !IsHidden();
if (visible) {
@@ -6450,7 +6460,11 @@ void HTMLMediaElement::NotifyOwnerDocumentActivityChanged() {
NotifyDecoderActivityChanges();
}
SuspendOrResumeElement(!IsActive());
// We would suspend media when the document is inactive, or its docshell has
// been set to hidden and explicitly wants to suspend media. In those cases,
// the media would be not visible and we don't want them to continue playing.
bool shouldSuspend = !IsActive() || ShouldBeSuspendedByInactiveDocShell();
SuspendOrResumeElement(shouldSuspend);
// If the owning document has become inactive we should shutdown the CDM.
if (!OwnerDoc()->IsCurrentActiveDocument() && mMediaKeys) {