Bug 1242874 - part4 : trigger callbacks in NotifyStartedPlaying draft
authorAlastor Wu <alwu@mozilla.com>
Thu, 21 Apr 2016 17:18:02 +0800
changeset 354583 f4da87dc7e3f59ff1f4466a0d54cd90ed3a89b3a
parent 354582 9493cfcfb3fcf593bc88023e6a0bc0b9dc50d9cd
child 354584 9fe6e9ed0c46b8598ee85f50bde6d384cb01c7f4
push id16130
push useralwu@mozilla.com
push dateThu, 21 Apr 2016 10:15:27 +0000
bugs1242874
milestone48.0a1
Bug 1242874 - part4 : trigger callbacks in NotifyStartedPlaying MozReview-Commit-ID: Ha7345HO2Cg
dom/audiochannel/AudioChannelAgent.cpp
dom/audiochannel/nsIAudioChannelAgent.idl
dom/fmradio/FMRadio.cpp
dom/html/HTMLMediaElement.cpp
dom/media/webaudio/AudioDestinationNode.cpp
dom/media/webspeech/synth/nsSpeechTask.cpp
dom/plugins/base/nsNPAPIPlugin.cpp
dom/telephony/Telephony.cpp
--- a/dom/audiochannel/AudioChannelAgent.cpp
+++ b/dom/audiochannel/AudioChannelAgent.cpp
@@ -196,29 +196,18 @@ AudioChannelAgent::InitInternal(nsPIDOMW
   MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
          ("AudioChannelAgent, InitInternal, this = %p, type = %d, "
           "owner = %p, hasCallback = %d\n", this, mAudioChannelType,
           mWindow.get(), (!!mCallback || !!mWeakCallback)));
 
   return NS_OK;
 }
 
-NS_IMETHODIMP AudioChannelAgent::NotifyStartedPlaying(float *aVolume,
-                                                      bool* aMuted)
+NS_IMETHODIMP AudioChannelAgent::NotifyStartedPlaying()
 {
-  MOZ_ASSERT(aVolume);
-  MOZ_ASSERT(aMuted);
-
-  // Window-less AudioChannelAgents are muted by default.
-  if (!mWindow) {
-    *aVolume = 0;
-    *aMuted = true;
-    return NS_OK;
-  }
-
   RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
   if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
       service == nullptr || mIsRegToService) {
     return NS_ERROR_FAILURE;
   }
 
   service->RegisterAudioChannelAgent(this,
     static_cast<AudioChannel>(mAudioChannelType));
@@ -226,18 +215,21 @@ NS_IMETHODIMP AudioChannelAgent::NotifyS
   WindowMediaConfig config = service->GetMediaConfig(mWindow,
                                                      mAudioChannelType);
 
   MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
          ("AudioChannelAgent, NotifyStartedPlaying, this = %p, "
           "mute = %d, volume = %f, suspended = %d\n", this,
           config.mMuted, config.mVolume, config.mSuspended));
 
-  *aVolume = config.mVolume;
-  *aMuted = config.mMuted;
+  nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
+  if (callback) {
+    callback->WindowVolumeChanged(config.mVolume, config.mMuted);
+    callback->WindowSuspendedChanged(config.mSuspended);
+  }
 
   mIsRegToService = true;
   return NS_OK;
 }
 
 NS_IMETHODIMP AudioChannelAgent::NotifyStoppedPlaying()
 {
   if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
--- a/dom/audiochannel/nsIAudioChannelAgent.idl
+++ b/dom/audiochannel/nsIAudioChannelAgent.idl
@@ -78,17 +78,17 @@ interface nsIAudioChannelAgentCallback :
  *   3. Notifying the agent when they start/stop using this channel.
  *   4. Notifying the agent of changes to the visibility of the component using
  *      this channel.
  *
  * The agent will invoke a callback to notify Gecko components of
  *   1. Changes to the playable status of this channel.
  */
 
-[uuid(ab7e21c0-970c-11e5-a837-0800200c9a66)]
+[uuid(f494b338-7a73-4f5c-a1aa-c8dd007c9679)]
 interface nsIAudioChannelAgent : nsISupports
 {
   const long AUDIO_AGENT_CHANNEL_NORMAL             = 0;
   const long AUDIO_AGENT_CHANNEL_CONTENT            = 1;
   const long AUDIO_AGENT_CHANNEL_NOTIFICATION       = 2;
   const long AUDIO_AGENT_CHANNEL_ALARM              = 3;
   const long AUDIO_AGENT_CHANNEL_TELEPHONY          = 4;
   const long AUDIO_AGENT_CHANNEL_RINGER             = 5;
@@ -139,31 +139,18 @@ interface nsIAudioChannelAgent : nsISupp
    * In order for this to work, |callback| must implement
    * nsISupportsWeakReference.
    */
   void initWithWeakCallback(in mozIDOMWindow window, in long channelType,
                             in nsIAudioChannelAgentCallback callback);
 
   /**
    * Notify the agent that we want to start playing.
-   * Note: Gecko component SHOULD call this function first then start to
-   *          play audio stream only when return value is true.
-   *
-   * @return
-   *    normal state: the agent has registered with audio channel service and
-   *          the component should start playback.
-   *    muted state: the agent has registered with audio channel service but
-   *          the component should not start playback.
-   *    faded state: the agent has registered with audio channel service the
-   *          component should start playback as well as reducing the volume.
+   * Note: Gecko component MUST call this function before thet want to play any
+   * audible sound.
    */
-  void notifyStartedPlaying(out float volume, out bool muted);
+  void notifyStartedPlaying();
 
   /**
    * Notify the agent we no longer want to play.
-   *
-   * Note : even if notifyStartedPlaying() returned false, the agent would
-   * still be registered with the audio channel service and receive callbacks
-   * for status changes. So notifyStoppedPlaying must still eventually be
-   * called to unregister the agent with the channel service.
    */
   void notifyStoppedPlaying();
 };
--- a/dom/fmradio/FMRadio.cpp
+++ b/dom/fmradio/FMRadio.cpp
@@ -447,21 +447,17 @@ FMRadio::DisableRDS()
   return r.forget();
 }
 
 void
 FMRadio::EnableAudioChannelAgent()
 {
   NS_ENSURE_TRUE_VOID(mAudioChannelAgent);
 
-  float volume = 0.0;
-  bool muted = true;
-  mAudioChannelAgent->NotifyStartedPlaying(&volume, &muted);
-  WindowVolumeChanged(volume, muted);
-
+  mAudioChannelAgent->NotifyStartedPlaying();
   mAudioChannelAgentEnabled = true;
 }
 
 NS_IMETHODIMP
 FMRadio::WindowVolumeChanged(float aVolume, bool aMuted)
 {
   // TODO : Not support to change volume now, so we just close it.
   IFMRadioService::Singleton()->EnableAudio(!aMuted);
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -5052,20 +5052,17 @@ void
 HTMLMediaElement::NotifyAudioChannelAgent(bool aPlaying)
 {
   // This is needed to pass nsContentUtils::IsCallerChrome().
   // AudioChannel API should not called from content but it can happen that
   // this method has some content JS in its stack.
   AutoNoJSAPI nojsapi;
 
   if (aPlaying) {
-    float volume = 0.0;
-    bool muted = true;
-    mAudioChannelAgent->NotifyStartedPlaying(&volume, &muted);
-    WindowVolumeChanged(volume, muted);
+    mAudioChannelAgent->NotifyStartedPlaying();
   } else {
     mAudioChannelAgent->NotifyStoppedPlaying();
     mAudioChannelAgent = nullptr;
   }
 }
 
 NS_IMETHODIMP
 HTMLMediaElement::WindowVolumeChanged(float aVolume, bool aMuted)
--- a/dom/media/webaudio/AudioDestinationNode.cpp
+++ b/dom/media/webaudio/AudioDestinationNode.cpp
@@ -729,20 +729,16 @@ AudioDestinationNode::InputMuted(bool aM
     CreateAudioChannelAgent();
   }
 
   if (aMuted) {
     mAudioChannelAgent->NotifyStoppedPlaying();
     return;
   }
 
-  float volume = 0.0;
-  bool muted = true;
-  nsresult rv = mAudioChannelAgent->NotifyStartedPlaying(&volume, &muted);
+  nsresult rv = mAudioChannelAgent->NotifyStartedPlaying();
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return;
   }
-
-  WindowVolumeChanged(volume, muted);
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/webspeech/synth/nsSpeechTask.cpp
+++ b/dom/media/webspeech/synth/nsSpeechTask.cpp
@@ -707,19 +707,17 @@ nsSpeechTask::CreateAudioChannelAgent()
   if (mAudioChannelAgent) {
     mAudioChannelAgent->NotifyStoppedPlaying();
   }
 
   mAudioChannelAgent = new AudioChannelAgent();
   mAudioChannelAgent->InitWithWeakCallback(mUtterance->GetOwner(),
                                            static_cast<int32_t>(AudioChannelService::GetDefaultAudioChannel()),
                                            this);
-  float volume = 0.0f;
-  bool muted = true;
-  mAudioChannelAgent->NotifyStartedPlaying(&volume, &muted);
+  mAudioChannelAgent->NotifyStartedPlaying();
 }
 
 void
 nsSpeechTask::DestroyAudioChannelAgent()
 {
   if (mAudioChannelAgent) {
     mAudioChannelAgent->NotifyStoppedPlaying();
     mAudioChannelAgent = nullptr;
--- a/dom/plugins/base/nsNPAPIPlugin.cpp
+++ b/dom/plugins/base/nsNPAPIPlugin.cpp
@@ -2284,24 +2284,17 @@ NPError
       MOZ_ASSERT(agent);
 
       if (isMuted) {
         rv = agent->NotifyStoppedPlaying();
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return NPERR_NO_ERROR;
         }
       } else {
-        float volume = 0.0;
-        bool muted = true;
-        rv = agent->NotifyStartedPlaying(&volume, &muted);
-        if (NS_WARN_IF(NS_FAILED(rv))) {
-          return NPERR_NO_ERROR;
-        }
-
-        rv = inst->WindowVolumeChanged(volume, muted);
+        rv = agent->NotifyStartedPlaying();
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return NPERR_NO_ERROR;
         }
       }
 
       return NPERR_NO_ERROR;
     }
 
--- a/dom/telephony/Telephony.cpp
+++ b/dom/telephony/Telephony.cpp
@@ -556,35 +556,31 @@ Telephony::HandleAudioAgentState()
     mIsAudioStartPlaying = false;
     rv = mAudioAgent->NotifyStoppedPlaying();
     mAudioAgent = nullptr;
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   } else if (!activeCall.IsNull() && !mIsAudioStartPlaying) {
     mIsAudioStartPlaying = true;
-    float volume;
-    bool muted;
-    rv = mAudioAgent->NotifyStartedPlaying(&volume, &muted);
+    rv = mAudioAgent->NotifyStartedPlaying();
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     // In B2G, the system app manages audio playback policy. If there is a new
     // sound want to be playback, it must wait for the permission from the
     // system app. It means that the sound would be muted first, and then be
     // unmuted. For telephony, the behaviors are hold() first, then resume().
     // However, the telephony service can't handle all these requests within a
     // short period. The telephony service would reject our resume request,
     // because the modem have not changed the call state yet. It causes that
     // the telephony can't be resumed. Therefore, we don't mute the telephony
     // at the beginning.
-    volume = 1.0;
-    muted = false;
-    rv = WindowVolumeChanged(volume, muted);
+    rv = WindowVolumeChanged(1.0, false);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
   return NS_OK;
 }
 
 bool