Bug 1290467 - part2 : request audio focus for any media instead of audible one. r=snorp
authorAlastor Wu <alwu@mozilla.com>
Fri, 09 Sep 2016 09:49:25 +0800
changeset 313283 ef1a345f206baf53604b4fff52e46e552623a3bd
parent 313282 c3210fd31140cd5bbe2e35c3439ba2141b8f8a6d
child 313284 fdc63760ddc8fca6b9e986b86ae4ac4e1da0b9a7
push idunknown
push userunknown
push dateunknown
reviewerssnorp
bugs1290467
milestone51.0a1
Bug 1290467 - part2 : request audio focus for any media instead of audible one. r=snorp Previous design is only to request audio focus for audible media, but now we also request focus for non-audible media. It's simple that the app should own the focus when users start watching media. MozReview-Commit-ID: 3eJP26h4kh7
widget/android/AndroidBridge.cpp
widget/android/AndroidBridge.h
--- a/widget/android/AndroidBridge.cpp
+++ b/widget/android/AndroidBridge.cpp
@@ -46,16 +46,17 @@
 #include "MediaCodec.h"
 #include "SurfaceTexture.h"
 #include "GLContextProvider.h"
 
 #include "mozilla/TimeStamp.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/dom/ContentChild.h"
 #include "nsIObserverService.h"
+#include "nsISupportsPrimitives.h"
 #include "MediaPrefs.h"
 
 using namespace mozilla;
 using namespace mozilla::gfx;
 using namespace mozilla::jni;
 using namespace mozilla::java;
 
 AndroidBridge* AndroidBridge::sBridge = nullptr;
@@ -1182,74 +1183,75 @@ NS_IMETHODIMP nsAndroidBridge::IsContent
 }
 
 NS_IMETHODIMP
 nsAndroidBridge::Observe(nsISupports* aSubject, const char* aTopic,
                          const char16_t* aData)
 {
   if (!strcmp(aTopic, "xpcom-shutdown")) {
     RemoveObservers();
-  } else if (!strcmp(aTopic, "audio-playback")) {
-    ALOG_BRIDGE("nsAndroidBridge::Observe, get audio-playback event.");
+  } else if (!strcmp(aTopic, "media-playback")) {
+    ALOG_BRIDGE("nsAndroidBridge::Observe, get media-playback event.");
 
-    nsCOMPtr<nsPIDOMWindowOuter> window = do_QueryInterface(aSubject);
-    MOZ_ASSERT(window);
-
-    nsAutoString activeStr(aData);
-    if (activeStr.EqualsLiteral("inactive-nonaudible")) {
-      // This state means the audio becomes silent, but it's still playing, so
-      // we don't need to notify the AudioFocusAgent.
+    nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
+    if (!wrapper) {
       return NS_OK;
     }
 
-    bool isPlaying = activeStr.EqualsLiteral("active");
+    uint64_t windowId = 0;
+    nsresult rv = wrapper->GetData(&windowId);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
 
-    UpdateAudioPlayingWindows(window, isPlaying);
+    nsAutoString activeStr(aData);
+    bool isPlaying = activeStr.EqualsLiteral("active");
+    UpdateAudioPlayingWindows(windowId, isPlaying);
   }
   return NS_OK;
 }
 
 void
-nsAndroidBridge::UpdateAudioPlayingWindows(nsPIDOMWindowOuter* aWindow,
+nsAndroidBridge::UpdateAudioPlayingWindows(uint64_t aWindowId,
                                            bool aPlaying)
 {
   // Request audio focus for the first audio playing window and abandon focus
   // for the last audio playing window.
-  if (aPlaying && !mAudioPlayingWindows.Contains(aWindow)) {
-    mAudioPlayingWindows.AppendElement(aWindow);
+  if (aPlaying && !mAudioPlayingWindows.Contains(aWindowId)) {
+    mAudioPlayingWindows.AppendElement(aWindowId);
     if (mAudioPlayingWindows.Length() == 1) {
       ALOG_BRIDGE("nsAndroidBridge, request audio focus.");
       AudioFocusAgent::NotifyStartedPlaying();
     }
-  } else if (!aPlaying && mAudioPlayingWindows.Contains(aWindow)) {
-    mAudioPlayingWindows.RemoveElement(aWindow);
+  } else if (!aPlaying && mAudioPlayingWindows.Contains(aWindowId)) {
+    mAudioPlayingWindows.RemoveElement(aWindowId);
     if (mAudioPlayingWindows.Length() == 0) {
       ALOG_BRIDGE("nsAndroidBridge, abandon audio focus.");
       AudioFocusAgent::NotifyStoppedPlaying();
     }
   }
 }
 
 void
 nsAndroidBridge::AddObservers()
 {
   nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
   if (obs) {
     obs->AddObserver(this, "xpcom-shutdown", false);
-    obs->AddObserver(this, "audio-playback", false);
+    obs->AddObserver(this, "media-playback", false);
   }
 }
 
 void
 nsAndroidBridge::RemoveObservers()
 {
   nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
   if (obs) {
     obs->RemoveObserver(this, "xpcom-shutdown");
-    obs->RemoveObserver(this, "audio-playback");
+    obs->RemoveObserver(this, "media-playback");
   }
 }
 
 uint32_t
 AndroidBridge::GetScreenOrientation()
 {
     ALOG_BRIDGE("AndroidBridge::GetScreenOrientation");
 
--- a/widget/android/AndroidBridge.h
+++ b/widget/android/AndroidBridge.h
@@ -496,16 +496,16 @@ public:
   nsAndroidBridge();
 
 private:
   ~nsAndroidBridge();
 
   void AddObservers();
   void RemoveObservers();
 
-  void UpdateAudioPlayingWindows(nsPIDOMWindowOuter* aWindow, bool aPlaying);
+  void UpdateAudioPlayingWindows(uint64_t aWindowId, bool aPlaying);
 
-  nsTArray<nsPIDOMWindowOuter*> mAudioPlayingWindows;
+  nsTArray<uint64_t> mAudioPlayingWindows;
 
 protected:
 };
 
 #endif /* AndroidBridge_h__ */