Bug 1665527 - part1 : deactivate a controller only when it doesn't have controlled media and active media session. r=chunmin

Currently, we only keep controller active when it has controlled media. That strategy works well for non-media session situation because only controlled media need to listen to media keys.

However, when having media session, thing goes slightly different. When we don't have any controlled media, active media session may still listen to media keys and do the corresponding operation. Therefore, we should keep the active media session being able to receive media keys even if the controlled media has gone and deactivate a controller when it doesn't have controlled media and no active media session.

Example, play a audible media first, then press `next track`, if media session is going to play an inaudible media (which is not a controllable media, so no controlled media is existing), we still want media session to receive and handle media keys for users.

Differential Revision: https://phabricator.services.mozilla.com/D90771
This commit is contained in:
alwu 2020-09-23 12:12:08 +00:00
Родитель b40f06a081
Коммит f03e8c1822
1 изменённых файлов: 13 добавлений и 1 удалений

Просмотреть файл

@ -352,7 +352,14 @@ bool MediaController::ShouldActivateController() const {
bool MediaController::ShouldDeactivateController() const {
MOZ_ASSERT(!mShutdown);
return !IsAnyMediaBeingControlled() && mIsActive;
// If we don't have an active media session and no controlled media exists,
// then we don't need to keep controller active, because there is nothing to
// control. However, if we still have an active media session, then we should
// keep controller active in order to receive media keys even if we don't have
// any controlled media existing, because a website might start other media
// when media session receives media keys.
return !IsAnyMediaBeingControlled() && mIsActive &&
!mActiveMediaSessionContextId;
}
void MediaController::Activate() {
@ -499,6 +506,11 @@ void MediaController::HandleMetadataChanged(
// to use `getMetadata()` to get metadata, because it would throw an error if
// we fail to allocate artwork.
DispatchAsyncEvent(u"metadatachange"_ns);
// If metadata change is because of resetting active media session, then we
// should check if controller needs to be deactivated.
if (ShouldDeactivateController()) {
Deactivate();
}
}
void MediaController::DispatchAsyncEvent(const nsAString& aName) {