Bug 1319771 - only active media component after creating them. draft
authorAlastor Wu <alwu@mozilla.com>
Tue, 17 Jan 2017 16:44:38 +0800
changeset 462377 3b32f8303e1b3b79719b76858c1c08f5e94c8ef6
parent 457446 d192a99be4b436f2dc839435319f7630d5d8f4b0
child 542367 5ec26ed881278b6981e5725e39e66787e10862e7
push id41725
push useralwu@mozilla.com
push dateTue, 17 Jan 2017 08:50:23 +0000
bugs1319771
milestone53.0a1
Bug 1319771 - only active media component after creating them. For the first pinned tab, it would be set to visible first and then set to invisible if there exists other tabs after restarting the whole browser. If the tab is set to visible, we would activate the media component (set the |mMediaSuspended| in outer window to none-suspend). In this case, the first pinned tab would be set to visible briefly, but it doesn't mean the tab is in the foreground, it's just how DOM manage the tab's visibility. In that moment, none of the media component has been created yet. Therefore, we would only activate the media component after the audio channel service exists. MozReview-Commit-ID: FM5Kk6gaUY2
dom/audiochannel/AudioChannelAgent.cpp
dom/audiochannel/AudioChannelService.cpp
dom/audiochannel/AudioChannelService.h
dom/base/nsDocument.cpp
--- a/dom/audiochannel/AudioChannelAgent.cpp
+++ b/dom/audiochannel/AudioChannelAgent.cpp
@@ -36,16 +36,19 @@ NS_INTERFACE_MAP_END
 NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioChannelAgent)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioChannelAgent)
 
 AudioChannelAgent::AudioChannelAgent()
   : mAudioChannelType(AUDIO_AGENT_CHANNEL_ERROR)
   , mInnerWindowID(0)
   , mIsRegToService(false)
 {
+  // Init service in the begining, it can help us to know whether there is any
+  // created media component via AudioChannelService::IsServiceStarted().
+  RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
 }
 
 AudioChannelAgent::~AudioChannelAgent()
 {
   Shutdown();
 }
 
 void
--- a/dom/audiochannel/AudioChannelService.cpp
+++ b/dom/audiochannel/AudioChannelService.cpp
@@ -197,16 +197,22 @@ AudioChannelService::CreateServiceIfNeed
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (!gAudioChannelService) {
     gAudioChannelService = new AudioChannelService();
   }
 }
 
+/* static */ bool
+AudioChannelService::IsServiceStarted()
+{
+  return !!gAudioChannelService;
+}
+
 /* static */ already_AddRefed<AudioChannelService>
 AudioChannelService::GetOrCreate()
 {
   if (sXPCOMShuttingDown) {
     return nullptr;
   }
 
   CreateServiceIfNeeded();
--- a/dom/audiochannel/AudioChannelService.h
+++ b/dom/audiochannel/AudioChannelService.h
@@ -95,16 +95,18 @@ public:
   static already_AddRefed<AudioChannelService> GetOrCreate();
 
   static bool IsAudioChannelMutedByDefault();
 
   static PRLogModuleInfo* GetAudioChannelLog();
 
   static bool IsEnableAudioCompeting();
 
+  static bool IsServiceStarted();
+
   /**
    * Any audio channel agent that starts playing should register itself to
    * this service, sharing the AudioChannel.
    */
   void RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
                                  AudibleState aAudible);
 
   /**
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -3,16 +3,17 @@
 /* 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/. */
 
 /*
  * Base class for all our document implementations.
  */
 
+#include "AudioChannelService.h"
 #include "nsDocument.h"
 #include "nsIDocumentInlines.h"
 #include "mozilla/AnimationComparator.h"
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/AutoRestore.h"
 #include "mozilla/BinarySearch.h"
 #include "mozilla/DebugOnly.h"
 #include "mozilla/EffectSet.h"
@@ -11918,17 +11919,18 @@ nsDocument::MaybeActiveMediaComponents()
     return;
   }
 
   if (!mWindow) {
     return;
   }
 
   mEverInForeground = true;
-  if (GetWindow()->GetMediaSuspend() == nsISuspendedTypes::SUSPENDED_BLOCK) {
+  if (GetWindow()->GetMediaSuspend() == nsISuspendedTypes::SUSPENDED_BLOCK &&
+      AudioChannelService::IsServiceStarted()) {
     GetWindow()->SetMediaSuspend(nsISuspendedTypes::NONE_SUSPENDED);
   }
 }
 
 NS_IMETHODIMP
 nsDocument::GetHidden(bool* aHidden)
 {
   *aHidden = Hidden();