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

View File

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

View File

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

View File

@@ -155,9 +155,17 @@ protected:
virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
virtual void WakeLockCreate() override; /**
virtual void WakeLockRelease() override; * We create video wakelock when the video is playing and release it when
void UpdateScreenWakeLock(); * 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; RefPtr<WakeLock> mScreenWakeLock;