Bug 1257738 - part2 : modify logic of requesting audio focus. draft
authorAlastor Wu <alwu@mozilla.com>
Tue, 03 May 2016 18:33:00 +0800
changeset 362849 da721d9bb4d73f27cb8770ebf3accf70c8a8f160
parent 362848 619c286e5b2f4569ae3d02887afcc930d6c5ae50
child 362850 374417bd7948c65ac51e4a62515ce0384ee4baaa
push id17046
push useralwu@mozilla.com
push dateTue, 03 May 2016 10:33:36 +0000
bugs1257738
milestone49.0a1
Bug 1257738 - part2 : modify logic of requesting audio focus. MozReview-Commit-ID: 9qTPxZgIikO
mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
widget/android/AndroidBridge.cpp
widget/android/AndroidBridge.h
--- a/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
@@ -11,17 +11,18 @@ import android.media.AudioManager.OnAudi
 import android.util.Log;
 
 public class AudioFocusAgent {
     private static final String LOGTAG = "AudioFocusAgent";
 
     private static Context mContext;
     private AudioManager mAudioManager;
     private OnAudioFocusChangeListener mAfChangeListener;
-    private int mAudibleElementCounts;
+
+    private boolean mIsOwningAudioFocus = false;
 
     @WrapForJNI
     public static void notifyStartedPlaying() {
         if (!isAttachedToContext()) {
             return;
         }
         Log.d(LOGTAG, "NotifyStartedPlaying");
         AudioFocusAgent.getInstance().requestAudioFocusIfNeeded();
@@ -78,44 +79,38 @@ public class AudioFocusAgent {
     private static boolean isAttachedToContext() {
         return (mContext != null);
     }
 
     private void notifyObservers(String topic, String data) {
         GeckoAppShell.notifyObservers(topic, data);
     }
 
-    private AudioFocusAgent() {
-        mAudibleElementCounts = 0;
-    }
+    private AudioFocusAgent() {}
 
     private void requestAudioFocusIfNeeded() {
-        if (!isFirstAudibleElement()) {
+        if (mIsOwningAudioFocus) {
             return;
         }
 
         int result = mAudioManager.requestAudioFocus(mAfChangeListener,
                                                      AudioManager.STREAM_MUSIC,
                                                      AudioManager.AUDIOFOCUS_GAIN);
 
         String focusMsg = (result == AudioManager.AUDIOFOCUS_GAIN) ?
             "AudioFocus request granted" : "AudioFoucs request failed";
         Log.d(LOGTAG, focusMsg);
         // TODO : Enable media control when get the AudioFocus, see bug1240423.
+        if (result == AudioManager.AUDIOFOCUS_GAIN) {
+            mIsOwningAudioFocus = true;
+        }
     }
 
     private void abandonAudioFocusIfNeeded() {
-        if (!isLastAudibleElement()) {
+        if (!mIsOwningAudioFocus) {
             return;
         }
 
         Log.d(LOGTAG, "Abandon AudioFocus");
         mAudioManager.abandonAudioFocus(mAfChangeListener);
-    }
-
-    private boolean isFirstAudibleElement() {
-        return (++mAudibleElementCounts == 1);
-    }
-
-    private boolean isLastAudibleElement() {
-        return (--mAudibleElementCounts == 0);
+        mIsOwningAudioFocus = false;
     }
 }
\ No newline at end of file
--- a/widget/android/AndroidBridge.cpp
+++ b/widget/android/AndroidBridge.cpp
@@ -1641,25 +1641,47 @@ 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.");
+
+    nsCOMPtr<nsPIDOMWindowOuter> window = do_QueryInterface(aSubject);
+    MOZ_ASSERT(window);
+
     nsAutoString activeStr(aData);
-    if (activeStr.EqualsLiteral("active")) {
+    bool isPlaying = activeStr.EqualsLiteral("active");
+
+    UpdateAudioPlayingWindows(window, isPlaying);
+  }
+  return NS_OK;
+}
+
+void
+nsAndroidBridge::UpdateAudioPlayingWindows(nsPIDOMWindowOuter* aWindow,
+                                           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 (mAudioPlayingWindows.Length() == 1) {
+      ALOG_BRIDGE("nsAndroidBridge, request audio focus.");
       AudioFocusAgent::NotifyStartedPlaying();
-    } else {
+    }
+  } else if (!aPlaying && mAudioPlayingWindows.Contains(aWindow)) {
+    mAudioPlayingWindows.RemoveElement(aWindow);
+    if (mAudioPlayingWindows.Length() == 0) {
+      ALOG_BRIDGE("nsAndroidBridge, abandon audio focus.");
       AudioFocusAgent::NotifyStoppedPlaying();
     }
   }
-
-  return NS_OK;
 }
 
 void
 nsAndroidBridge::AddObservers()
 {
   nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
   if (obs) {
     obs->AddObserver(this, "xpcom-shutdown", false);
--- a/widget/android/AndroidBridge.h
+++ b/widget/android/AndroidBridge.h
@@ -595,12 +595,16 @@ public:
   nsAndroidBridge();
 
 private:
   ~nsAndroidBridge();
 
   void AddObservers();
   void RemoveObservers();
 
+  void UpdateAudioPlayingWindows(nsPIDOMWindowOuter* aWindow, bool aPlaying);
+
+  nsTArray<nsPIDOMWindowOuter*> mAudioPlayingWindows;
+
 protected:
 };
 
 #endif /* AndroidBridge_h__ */