Bug 1578615 - part11 : remove usages of 'AudibleState::eMaybeAudible' in media element. r=chunmin
authoralwu <alwu@mozilla.com>
Wed, 25 Sep 2019 23:55:42 +0000
changeset 495053 aeec93cc3005e59241a919f1813fee9012d66889
parent 495052 a8deaa2603d3f63a0bce7daebcd7fd2f842763ba
child 495054 1dc1a755079a15e35cb234db511e52ab463b2f42
child 495055 6d401ddd9ce003efb2cff8c4d930b9c7ba7a3e59
push id36621
push userdluca@mozilla.com
push dateThu, 26 Sep 2019 09:42:00 +0000
treeherdermozilla-central@1dc1a755079a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschunmin
bugs1578615, 1583978
milestone71.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1578615 - part11 : remove usages of 'AudibleState::eMaybeAudible' in media element. r=chunmin In bug1583978, we are going to remove `AudibleState::eMaybeAudible`. But before that, as `AudibleState::eMaybeAudible` is only used for delaying media playback, so we can remove its usage from media element and move it to the dedicated class to reduce confusion about how and when we should use `AudibleState::eMaybeAudible` or `AudibleState::eNotAudible`. Differential Revision: https://phabricator.services.mozilla.com/D47153
dom/html/HTMLMediaElement.cpp
dom/html/HTMLMediaElement.h
dom/media/MediaPlaybackDelayPolicy.cpp
dom/media/MediaPlaybackDelayPolicy.h
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -1288,17 +1288,18 @@ class HTMLMediaElement::AudioChannelAgen
             mSuspended == nsISuspendedTypes::SUSPENDED_PAUSE_DISPOSABLE);
   }
 
   AudibleState IsOwnerAudible() const {
     // Suspended or paused media doesn't produce any sound.
     if (mSuspended != nsISuspendedTypes::NONE_SUSPENDED || mOwner->mPaused) {
       return AudibleState::eNotAudible;
     }
-    return mOwner->GetAudibleState();
+    return mOwner->IsAudible() ? AudibleState::eAudible
+                               : AudibleState::eNotAudible;
   }
 
   bool IsPlayingThroughTheAudioChannel() const {
     // If we have an error, we are not playing.
     if (mOwner->GetError()) {
       return false;
     }
 
@@ -7017,17 +7018,17 @@ bool HTMLMediaElement::ShouldElementBePa
 
   return false;
 }
 
 void HTMLMediaElement::SetMediaInfo(const MediaInfo& aInfo) {
   const bool oldHasAudio = mMediaInfo.HasAudio();
   mMediaInfo = aInfo;
   if ((aInfo.HasAudio() != oldHasAudio) && mResumeDelayedPlaybackAgent) {
-    mResumeDelayedPlaybackAgent->UpdateAudibleState(GetAudibleState());
+    mResumeDelayedPlaybackAgent->UpdateAudibleState(this, IsAudible());
   }
   if (mAudioChannelWrapper) {
     mAudioChannelWrapper->AudioCaptureStreamChangeIfNeeded();
   }
   UpdateWakeLock();
 }
 
 void HTMLMediaElement::AudioCaptureStreamChange(bool aCapture) {
@@ -7179,29 +7180,28 @@ void HTMLMediaElement::NotifyDecoderActi
   if (mDecoder) {
     mDecoder->NotifyOwnerActivityChanged(!IsHidden(), mVisibilityState,
                                          IsInComposedDoc());
   }
 }
 
 Document* HTMLMediaElement::GetDocument() const { return OwnerDoc(); }
 
-AudibleState HTMLMediaElement::GetAudibleState() const {
+bool HTMLMediaElement::IsAudible() const {
   // No audio track.
   if (!HasAudio()) {
-    return AudibleState::eNotAudible;
+    return false;
   }
 
   // Muted or the volume should not be ~0
   if (mMuted || (std::fabs(Volume()) <= 1e-7)) {
-    return AudibleState::eMaybeAudible;
-  }
-
-  return mIsAudioTrackAudible ? AudibleState::eAudible
-                              : AudibleState::eMaybeAudible;
+    return false;
+  }
+
+  return mIsAudioTrackAudible;
 }
 
 void HTMLMediaElement::ConstructMediaTracks(const MediaInfo* aInfo) {
   if (mMediaTracksConstructed || !aInfo) {
     return;
   }
 
   mMediaTracksConstructed = true;
@@ -7414,18 +7414,18 @@ void HTMLMediaElement::NotifyTextTrackMo
                              }));
 }
 
 void HTMLMediaElement::CreateResumeDelayedMediaPlaybackAgentIfNeeded() {
   if (mResumeDelayedPlaybackAgent) {
     return;
   }
   mResumeDelayedPlaybackAgent =
-      MediaPlaybackDelayPolicy::CreateResumeDelayedPlaybackAgent(
-          this, GetAudibleState());
+      MediaPlaybackDelayPolicy::CreateResumeDelayedPlaybackAgent(this,
+                                                                 IsAudible());
   if (!mResumeDelayedPlaybackAgent) {
     LOG(LogLevel::Debug,
         ("%p Failed to create a delayed playback agant", this));
     return;
   }
   mResumeDelayedPlaybackAgent->GetResumePromise()
       ->Then(
           mAbstractMainThread, __func__,
--- a/dom/html/HTMLMediaElement.h
+++ b/dom/html/HTMLMediaElement.h
@@ -721,17 +721,17 @@ class HTMLMediaElement : public nsGeneri
 
   // This is used to notify MediaElementAudioSourceNode that media element is
   // allowed to play when media element is used as a source for web audio, so
   // that we can start AudioContext if it was not allowed to start.
   RefPtr<GenericNonExclusivePromise> GetAllowedToPlayPromise();
 
   bool GetShowPosterFlag() const { return mShowPoster; }
 
-  AudioChannelService::AudibleState GetAudibleState() const;
+  bool IsAudible() const;
 
  protected:
   virtual ~HTMLMediaElement();
 
   class AudioChannelAgentCallback;
   class ChannelLoader;
   class ErrorSink;
   class MediaLoadListener;
--- a/dom/media/MediaPlaybackDelayPolicy.cpp
+++ b/dom/media/MediaPlaybackDelayPolicy.cpp
@@ -8,56 +8,69 @@
 #include "nsPIDOMWindow.h"
 #include "mozilla/dom/HTMLMediaElement.h"
 
 namespace mozilla {
 namespace dom {
 
 using AudibleState = AudioChannelService::AudibleState;
 
+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;
+}
+
 ResumeDelayedPlaybackAgent::~ResumeDelayedPlaybackAgent() {
   if (mDelegate) {
     mDelegate->Clear();
     mDelegate = nullptr;
   }
 }
 
 bool ResumeDelayedPlaybackAgent::InitDelegate(const HTMLMediaElement* aElement,
-                                              AudibleState aAudibleState) {
+                                              bool aIsAudible) {
   MOZ_ASSERT(!mDelegate, "Delegate has been initialized!");
   mDelegate = new ResumePlayDelegate();
-  if (!mDelegate->Init(aElement, aAudibleState)) {
+  if (!mDelegate->Init(aElement, aIsAudible)) {
     mDelegate->Clear();
     mDelegate = nullptr;
     return false;
   }
   return true;
 }
 
 RefPtr<ResumeDelayedPlaybackAgent::ResumePromise>
 ResumeDelayedPlaybackAgent::GetResumePromise() {
   MOZ_ASSERT(mDelegate);
   return mDelegate->GetResumePromise();
 }
 
 void ResumeDelayedPlaybackAgent::UpdateAudibleState(
-    AudibleState aAudibleState) {
+    const HTMLMediaElement* aElement, bool aIsAudible) {
+  MOZ_ASSERT(aElement);
   MOZ_ASSERT(mDelegate);
-  mDelegate->UpdateAudibleState(aAudibleState);
+  mDelegate->UpdateAudibleState(aElement, aIsAudible);
 }
 
 NS_IMPL_ISUPPORTS(ResumeDelayedPlaybackAgent::ResumePlayDelegate,
                   nsIAudioChannelAgentCallback)
 
 ResumeDelayedPlaybackAgent::ResumePlayDelegate::~ResumePlayDelegate() {
   MOZ_ASSERT(!mAudioChannelAgent);
 }
 
 bool ResumeDelayedPlaybackAgent::ResumePlayDelegate::Init(
-    const HTMLMediaElement* aElement, AudibleState aAudibleState) {
+    const HTMLMediaElement* aElement, bool aIsAudible) {
   MOZ_ASSERT(aElement);
   MOZ_ASSERT(aElement->OwnerDoc());
   if (!aElement->OwnerDoc()->GetInnerWindow()) {
     return false;
   }
 
   MOZ_ASSERT(!mAudioChannelAgent);
   mAudioChannelAgent = new AudioChannelAgent();
@@ -65,17 +78,18 @@ bool ResumeDelayedPlaybackAgent::ResumeP
       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.
-  rv = mAudioChannelAgent->NotifyStartedPlaying(aAudibleState);
+  AudibleState audibleState = DetermineMediaAudibleState(aElement, aIsAudible);
+  rv = mAudioChannelAgent->NotifyStartedPlaying(audibleState);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     Clear();
     return false;
   }
 
   return true;
 }
 
@@ -88,24 +102,26 @@ void ResumeDelayedPlaybackAgent::ResumeP
 }
 
 RefPtr<ResumeDelayedPlaybackAgent::ResumePromise>
 ResumeDelayedPlaybackAgent::ResumePlayDelegate::GetResumePromise() {
   return mPromise.Ensure(__func__);
 }
 
 void ResumeDelayedPlaybackAgent::ResumePlayDelegate::UpdateAudibleState(
-    AudibleState aAudibleState) {
+    const HTMLMediaElement* aElement, bool aIsAudible) {
+  MOZ_ASSERT(aElement);
   // It's possible for the owner of `ResumeDelayedPlaybackAgent` to call
   // `UpdateAudibleState()` after we resolve the promise and clean resource.
   if (!mAudioChannelAgent) {
     return;
   }
+  AudibleState audibleState = DetermineMediaAudibleState(aElement, aIsAudible);
   mAudioChannelAgent->NotifyStartedAudible(
-      aAudibleState,
+      audibleState,
       AudioChannelService::AudibleChangedReasons::eDataAudibleChanged);
 }
 
 NS_IMETHODIMP
 ResumeDelayedPlaybackAgent::ResumePlayDelegate::WindowVolumeChanged(
     float aVolume, bool aMuted) {
   return NS_OK;
 }
@@ -143,16 +159,16 @@ bool MediaPlaybackDelayPolicy::ShouldDel
   if (!outer) {
     return false;
   }
   return outer->GetMediaSuspend() == nsISuspendedTypes::SUSPENDED_BLOCK;
 }
 
 RefPtr<ResumeDelayedPlaybackAgent>
 MediaPlaybackDelayPolicy::CreateResumeDelayedPlaybackAgent(
-    const HTMLMediaElement* aElement, AudibleState aAudibleState) {
+    const HTMLMediaElement* aElement, bool aIsAudible) {
   MOZ_ASSERT(aElement);
   RefPtr<ResumeDelayedPlaybackAgent> agent = new ResumeDelayedPlaybackAgent();
-  return agent->InitDelegate(aElement, aAudibleState) ? agent : nullptr;
+  return agent->InitDelegate(aElement, aIsAudible) ? agent : nullptr;
 }
 
 }  // namespace dom
 }  // namespace mozilla
--- a/dom/media/MediaPlaybackDelayPolicy.h
+++ b/dom/media/MediaPlaybackDelayPolicy.h
@@ -35,34 +35,32 @@ class HTMLMediaElement;
  */
 class ResumeDelayedPlaybackAgent {
  public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ResumeDelayedPlaybackAgent);
   ResumeDelayedPlaybackAgent() = default;
 
   using ResumePromise = MozPromise<bool, bool, true /* IsExclusive */>;
   RefPtr<ResumePromise> GetResumePromise();
-  void UpdateAudibleState(AudioChannelService::AudibleState aAudibleState);
+  void UpdateAudibleState(const HTMLMediaElement* aElement, bool aIsAudible);
 
  private:
   friend class MediaPlaybackDelayPolicy;
 
   ~ResumeDelayedPlaybackAgent();
-  bool InitDelegate(const HTMLMediaElement* aElement,
-                    AudioChannelService::AudibleState aAudibleState);
+  bool InitDelegate(const HTMLMediaElement* aElement, bool aIsAudible);
 
   class ResumePlayDelegate final : public nsIAudioChannelAgentCallback {
    public:
     NS_DECL_ISUPPORTS
 
     ResumePlayDelegate() = default;
 
-    bool Init(const HTMLMediaElement* aElement,
-              AudioChannelService::AudibleState aAudibleState);
-    void UpdateAudibleState(AudioChannelService::AudibleState aAudibleState);
+    bool Init(const HTMLMediaElement* aElement, bool aIsAudible);
+    void UpdateAudibleState(const HTMLMediaElement* aElement, bool aIsAudible);
     RefPtr<ResumePromise> GetResumePromise();
     void Clear();
 
     NS_IMETHODIMP WindowVolumeChanged(float aVolume, bool aMuted) override;
     NS_IMETHODIMP WindowAudioCaptureChanged(bool aCapture) override;
     NS_IMETHODIMP WindowSuspendChanged(SuspendTypes aSuspend) override;
 
    private:
@@ -74,16 +72,15 @@ class ResumeDelayedPlaybackAgent {
 
   RefPtr<ResumePlayDelegate> mDelegate;
 };
 
 class MediaPlaybackDelayPolicy {
  public:
   static bool ShouldDelayPlayback(const HTMLMediaElement* aElement);
   static RefPtr<ResumeDelayedPlaybackAgent> CreateResumeDelayedPlaybackAgent(
-      const HTMLMediaElement* aElement,
-      AudioChannelService::AudibleState aAudibleState);
+      const HTMLMediaElement* aElement, bool aIsAudible);
 };
 
 }  // namespace dom
 }  // namespace mozilla
 
 #endif