2019-09-26 02:41:23 +03:00
|
|
|
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "MediaPlaybackDelayPolicy.h"
|
|
|
|
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2019-10-02 13:23:02 +03:00
|
|
|
#include "mozilla/StaticPrefs_media.h"
|
2019-09-26 02:41:23 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using AudibleState = AudioChannelService::AudibleState;
|
|
|
|
|
2019-09-26 02:55:42 +03:00
|
|
|
static AudibleState DetermineMediaAudibleState(const HTMLMediaElement* aElement,
|
|
|
|
bool aIsAudible) {
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
if (!aElement->HasAudio()) {
|
|
|
|
return AudibleState::eNotAudible;
|
|
|
|
}
|
|
|
|
// `eMaybeAudible` is used to distinguish if the media has audio track or not,
|
|
|
|
// because we would only show the delayed media playback icon for media with
|
|
|
|
// an audio track.
|
|
|
|
return aIsAudible ? AudibleState::eAudible : AudibleState::eMaybeAudible;
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:41:23 +03:00
|
|
|
ResumeDelayedPlaybackAgent::~ResumeDelayedPlaybackAgent() {
|
|
|
|
if (mDelegate) {
|
|
|
|
mDelegate->Clear();
|
|
|
|
mDelegate = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResumeDelayedPlaybackAgent::InitDelegate(const HTMLMediaElement* aElement,
|
2019-09-26 02:55:42 +03:00
|
|
|
bool aIsAudible) {
|
2019-09-26 02:41:23 +03:00
|
|
|
MOZ_ASSERT(!mDelegate, "Delegate has been initialized!");
|
|
|
|
mDelegate = new ResumePlayDelegate();
|
2019-09-26 02:55:42 +03:00
|
|
|
if (!mDelegate->Init(aElement, aIsAudible)) {
|
2019-09-26 02:41:23 +03:00
|
|
|
mDelegate->Clear();
|
|
|
|
mDelegate = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ResumeDelayedPlaybackAgent::ResumePromise>
|
|
|
|
ResumeDelayedPlaybackAgent::GetResumePromise() {
|
|
|
|
MOZ_ASSERT(mDelegate);
|
|
|
|
return mDelegate->GetResumePromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResumeDelayedPlaybackAgent::UpdateAudibleState(
|
2019-09-26 02:55:42 +03:00
|
|
|
const HTMLMediaElement* aElement, bool aIsAudible) {
|
|
|
|
MOZ_ASSERT(aElement);
|
2019-09-26 02:41:23 +03:00
|
|
|
MOZ_ASSERT(mDelegate);
|
2019-09-26 02:55:42 +03:00
|
|
|
mDelegate->UpdateAudibleState(aElement, aIsAudible);
|
2019-09-26 02:41:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ResumeDelayedPlaybackAgent::ResumePlayDelegate,
|
|
|
|
nsIAudioChannelAgentCallback)
|
|
|
|
|
|
|
|
ResumeDelayedPlaybackAgent::ResumePlayDelegate::~ResumePlayDelegate() {
|
|
|
|
MOZ_ASSERT(!mAudioChannelAgent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResumeDelayedPlaybackAgent::ResumePlayDelegate::Init(
|
2019-09-26 02:55:42 +03:00
|
|
|
const HTMLMediaElement* aElement, bool aIsAudible) {
|
2019-09-26 02:41:23 +03:00
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
if (!aElement->OwnerDoc()->GetInnerWindow()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mAudioChannelAgent);
|
|
|
|
mAudioChannelAgent = new AudioChannelAgent();
|
|
|
|
nsresult rv =
|
|
|
|
mAudioChannelAgent->Init(aElement->OwnerDoc()->GetInnerWindow(), this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start AudioChannelAgent in order to wait the suspended state change when we
|
|
|
|
// are able to resume delayed playback.
|
2019-09-26 02:55:42 +03:00
|
|
|
AudibleState audibleState = DetermineMediaAudibleState(aElement, aIsAudible);
|
|
|
|
rv = mAudioChannelAgent->NotifyStartedPlaying(audibleState);
|
2019-09-26 02:41:23 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResumeDelayedPlaybackAgent::ResumePlayDelegate::Clear() {
|
|
|
|
if (mAudioChannelAgent) {
|
|
|
|
mAudioChannelAgent->NotifyStoppedPlaying();
|
|
|
|
mAudioChannelAgent = nullptr;
|
|
|
|
}
|
|
|
|
mPromise.RejectIfExists(true, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ResumeDelayedPlaybackAgent::ResumePromise>
|
|
|
|
ResumeDelayedPlaybackAgent::ResumePlayDelegate::GetResumePromise() {
|
|
|
|
return mPromise.Ensure(__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResumeDelayedPlaybackAgent::ResumePlayDelegate::UpdateAudibleState(
|
2019-09-26 02:55:42 +03:00
|
|
|
const HTMLMediaElement* aElement, bool aIsAudible) {
|
|
|
|
MOZ_ASSERT(aElement);
|
2019-09-26 02:41:23 +03:00
|
|
|
// It's possible for the owner of `ResumeDelayedPlaybackAgent` to call
|
|
|
|
// `UpdateAudibleState()` after we resolve the promise and clean resource.
|
|
|
|
if (!mAudioChannelAgent) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-26 02:55:42 +03:00
|
|
|
AudibleState audibleState = DetermineMediaAudibleState(aElement, aIsAudible);
|
2019-09-26 02:41:23 +03:00
|
|
|
mAudioChannelAgent->NotifyStartedAudible(
|
2019-09-26 02:55:42 +03:00
|
|
|
audibleState,
|
2019-09-26 02:41:23 +03:00
|
|
|
AudioChannelService::AudibleChangedReasons::eDataAudibleChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ResumeDelayedPlaybackAgent::ResumePlayDelegate::WindowVolumeChanged(
|
|
|
|
float aVolume, bool aMuted) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ResumeDelayedPlaybackAgent::ResumePlayDelegate::WindowAudioCaptureChanged(
|
|
|
|
bool aCapture) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ResumeDelayedPlaybackAgent::ResumePlayDelegate::WindowSuspendChanged(
|
|
|
|
SuspendTypes aSuspend) {
|
|
|
|
if (aSuspend == nsISuspendedTypes::NONE_SUSPENDED) {
|
|
|
|
mPromise.ResolveIfExists(true, __func__);
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaPlaybackDelayPolicy::ShouldDelayPlayback(
|
|
|
|
const HTMLMediaElement* aElement) {
|
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
if (!StaticPrefs::media_block_autoplay_until_in_foreground()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Document* doc = aElement->OwnerDoc();
|
|
|
|
nsPIDOMWindowInner* inner = nsPIDOMWindowInner::From(doc->GetInnerWindow());
|
|
|
|
nsPIDOMWindowOuter* outer = nsPIDOMWindowOuter::GetFromCurrentInner(inner);
|
|
|
|
if (!outer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return outer->GetMediaSuspend() == nsISuspendedTypes::SUSPENDED_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ResumeDelayedPlaybackAgent>
|
|
|
|
MediaPlaybackDelayPolicy::CreateResumeDelayedPlaybackAgent(
|
2019-09-26 02:55:42 +03:00
|
|
|
const HTMLMediaElement* aElement, bool aIsAudible) {
|
2019-09-26 02:41:23 +03:00
|
|
|
MOZ_ASSERT(aElement);
|
|
|
|
RefPtr<ResumeDelayedPlaybackAgent> agent = new ResumeDelayedPlaybackAgent();
|
2019-09-26 02:55:42 +03:00
|
|
|
return agent->InitDelegate(aElement, aIsAudible) ? agent : nullptr;
|
2019-09-26 02:41:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|