Bug 1495064 - part1 : refactor the logic of requesting wakelock. r=jya

HTMLMediaElement::UpdateWakeLock() is responsible for creating and releasing audio wakelock.
HTMLVideoElement::UpdateWakeLock() is responsible for creating and releasing video wakelock.

In addition, each platform would handle system wakelock properly depending on different requests.

Differential Revision: https://phabricator.services.mozilla.com/D7214
This commit is contained in:
alwu
2018-10-02 17:56:21 +00:00
parent 488ac5fe26
commit ab38dd17ca
4 changed files with 54 additions and 29 deletions

View File

@@ -4282,14 +4282,14 @@ HTMLMediaElement::UpdateWakeLock()
Volume() > 0.0 && !mMuted && mIsAudioTrackAudible;
// WakeLock when playing audible media.
if (playing && isAudible) {
WakeLockCreate();
CreateAudioWakeLockIfNeeded();
} else {
WakeLockRelease();
ReleaseAudioWakeLockIfExists();
}
}
void
HTMLMediaElement::WakeLockCreate()
HTMLMediaElement::CreateAudioWakeLockIfNeeded()
{
if (!mWakeLock) {
RefPtr<power::PowerManagerService> pmService =
@@ -4297,13 +4297,14 @@ HTMLMediaElement::WakeLockCreate()
NS_ENSURE_TRUE_VOID(pmService);
ErrorResult rv;
mWakeLock = pmService->NewWakeLock(
NS_LITERAL_STRING("audio-playing"), OwnerDoc()->GetInnerWindow(), rv);
mWakeLock = pmService->NewWakeLock(NS_LITERAL_STRING("audio-playing"),
OwnerDoc()->GetInnerWindow(),
rv);
}
}
void
HTMLMediaElement::WakeLockRelease()
HTMLMediaElement::ReleaseAudioWakeLockIfExists()
{
if (mWakeLock) {
ErrorResult rv;
@@ -4313,6 +4314,12 @@ HTMLMediaElement::WakeLockRelease()
}
}
void
HTMLMediaElement::WakeLockRelease()
{
ReleaseAudioWakeLockIfExists();
}
HTMLMediaElement::OutputMediaStream::OutputMediaStream()
: mNextAvailableTrackID(1)
, mFinishWhenEnded(false)

View File

@@ -844,8 +844,6 @@ protected:
void SetDecoder(MediaDecoder* aDecoder);
void UpdateWakeLock();
// Holds references to the DOM wrappers for the MediaStreams that we're
// writing to.
struct OutputMediaStream {
@@ -882,11 +880,14 @@ protected:
void ChangeNetworkState(nsMediaNetworkState aState);
/**
* These two methods are called when mPaused is changed to ensure we have
* a wake lock active when we're playing audibly.
* The MediaElement will be responsible for creating and releasing the audio
* wakelock depending on the playing and audible state.
*/
virtual void WakeLockCreate();
virtual void WakeLockRelease();
virtual void UpdateWakeLock();
void CreateAudioWakeLockIfNeeded();
void ReleaseAudioWakeLockIfExists();
RefPtr<WakeLock> mWakeLock;
/**

View File

@@ -296,32 +296,29 @@ HTMLVideoElement::GetVideoPlaybackQuality()
return playbackQuality.forget();
}
void
HTMLVideoElement::WakeLockCreate()
{
HTMLMediaElement::WakeLockCreate();
UpdateScreenWakeLock();
}
void
HTMLVideoElement::WakeLockRelease()
{
UpdateScreenWakeLock();
HTMLMediaElement::WakeLockRelease();
ReleaseVideoWakeLockIfExists();
}
void
HTMLVideoElement::UpdateScreenWakeLock()
HTMLVideoElement::UpdateWakeLock()
{
if (mScreenWakeLock && mPaused) {
ErrorResult rv;
mScreenWakeLock->Unlock(rv);
rv.SuppressException();
mScreenWakeLock = nullptr;
return;
HTMLMediaElement::UpdateWakeLock();
if (!mPaused) {
CreateVideoWakeLockIfNeeded();
} else {
ReleaseVideoWakeLockIfExists();
}
}
if (!mScreenWakeLock && !mPaused && HasVideo()) {
void
HTMLVideoElement::CreateVideoWakeLockIfNeeded()
{
if (!mScreenWakeLock && HasVideo()) {
RefPtr<power::PowerManagerService> pmService =
power::PowerManagerService::GetInstance();
NS_ENSURE_TRUE_VOID(pmService);
@@ -333,6 +330,18 @@ HTMLVideoElement::UpdateScreenWakeLock()
}
}
void
HTMLVideoElement::ReleaseVideoWakeLockIfExists()
{
if (mScreenWakeLock) {
ErrorResult rv;
mScreenWakeLock->Unlock(rv);
rv.SuppressException();
mScreenWakeLock = nullptr;
return;
}
}
void
HTMLVideoElement::Init()
{

View File

@@ -155,9 +155,17 @@ protected:
virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
virtual void WakeLockCreate() override;
virtual void WakeLockRelease() override;
void UpdateScreenWakeLock();
/**
* We create video wakelock when the video is playing and release it when
* video pauses. Note, the actual platform wakelock will automatically be
* released when the page is in the background, so we don't need to check the
* video's visibility by ourselves.
*/
void WakeLockRelease() override;
void UpdateWakeLock() override;
void CreateVideoWakeLockIfNeeded();
void ReleaseVideoWakeLockIfExists();
RefPtr<WakeLock> mScreenWakeLock;