Bug 1392930 - part 1: Replace AudioStream by DeviceChangeListener in AudioNotificationReceiver; r=jwwang draft
authorChun-Min Chang <chun.m.chang@gmail.com>
Tue, 29 Aug 2017 17:40:06 +0800
changeset 654857 e0c1a56ff5318ef8e581576bc80443001828569b
parent 654592 1b4c59eef820b46eb0037aca68f83a15088db45f
child 654858 7bc746f4ec3b374d6598280e223649b070d93eeb
push id76710
push userbmo:cchang@mozilla.com
push dateTue, 29 Aug 2017 10:34:23 +0000
reviewersjwwang
bugs1392930
milestone57.0a1
Bug 1392930 - part 1: Replace AudioStream by DeviceChangeListener in AudioNotificationReceiver; r=jwwang
dom/media/AudioNotificationReceiver.cpp
dom/media/AudioNotificationReceiver.h
--- a/dom/media/AudioNotificationReceiver.cpp
+++ b/dom/media/AudioNotificationReceiver.cpp
@@ -1,16 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* 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/. */
 
 #include "AudioNotificationReceiver.h"
-#include "AudioStream.h"          // for AudioStream
 #include "mozilla/Logging.h"      // for LazyLogModule
 #include "mozilla/StaticMutex.h"  // for StaticMutex
 #include "mozilla/StaticPtr.h"    // for StaticAutoPtr
 #include "nsAppRunner.h"          // for XRE_IsContentProcess
 #include "nsTArray.h"             // for nsTArray
 
 static mozilla::LazyLogModule sLogger("AudioNotificationReceiver");
 
@@ -20,65 +19,68 @@ static mozilla::LazyLogModule sLogger("A
 #define ANR_LOGW(...) MOZ_LOG(sLogger, mozilla::LogLevel::Warning, (__VA_ARGS__))
 
 namespace mozilla {
 namespace audio {
 
 /*
  * A list containing all clients subscribering the device-changed notifications.
  */
-static StaticAutoPtr<nsTArray<AudioStream*>> sSubscribers;
+static StaticAutoPtr<nsTArray<DeviceChangeListener*>> sSubscribers;
 static StaticMutex sMutex;
 
 /*
  * AudioNotificationReceiver Implementation
  */
 /* static */ void
-AudioNotificationReceiver::Register(AudioStream* aAudioStream)
+AudioNotificationReceiver::Register(DeviceChangeListener* aDeviceChangeListener)
 {
   MOZ_ASSERT(XRE_IsContentProcess());
 
   StaticMutexAutoLock lock(sMutex);
   if (!sSubscribers) {
-    sSubscribers = new nsTArray<AudioStream*>();
+    sSubscribers = new nsTArray<DeviceChangeListener*>();
   }
-  sSubscribers->AppendElement(aAudioStream);
+  sSubscribers->AppendElement(aDeviceChangeListener);
 
-  ANR_LOG("The AudioStream: %p is registered successfully.", aAudioStream);
+  ANR_LOG("The DeviceChangeListener: %p is registered successfully.",
+          aDeviceChangeListener);
 }
 
 /* static */ void
-AudioNotificationReceiver::Unregister(AudioStream* aAudioStream)
+AudioNotificationReceiver::Unregister(DeviceChangeListener* aDeviceChangeListener)
 {
   MOZ_ASSERT(XRE_IsContentProcess());
 
   StaticMutexAutoLock lock(sMutex);
   MOZ_ASSERT(!sSubscribers->IsEmpty(), "No subscriber.");
 
-  sSubscribers->RemoveElement(aAudioStream);
+  sSubscribers->RemoveElement(aDeviceChangeListener);
   if (sSubscribers->IsEmpty()) {
     // Clear the static pointer here to prevent memory leak.
     sSubscribers = nullptr;
   }
 
-  ANR_LOG("The AudioStream: %p is unregistered successfully.", aAudioStream);
+  ANR_LOG("The DeviceChangeListener: %p is unregistered successfully.",
+          aDeviceChangeListener);
 }
 
 /* static */ void
 AudioNotificationReceiver::NotifyDefaultDeviceChanged()
 {
   MOZ_ASSERT(XRE_IsContentProcess());
 
   StaticMutexAutoLock lock(sMutex);
 
-  // Do nothing when there is no AudioStream.
+  // Do nothing when there is no DeviceChangeListener.
   if (!sSubscribers) {
     return;
   }
 
-  for (AudioStream* stream : *sSubscribers) {
-    ANR_LOG("Notify the AudioStream: %p that the default device has been changed.", stream);
+  for (DeviceChangeListener* stream : *sSubscribers) {
+    ANR_LOG("Notify the DeviceChangeListener: %p "
+            "that the default device has been changed.", stream);
     stream->ResetDefaultDevice();
   }
 }
 
 } // namespace audio
 } // namespace mozilla
--- a/dom/media/AudioNotificationReceiver.h
+++ b/dom/media/AudioNotificationReceiver.h
@@ -8,78 +8,88 @@
 #define MOZILLA_AUDIONOTIFICATIONRECEIVER_H_
 
 /*
  * Architecture to send/receive default device-changed notification:
  *
  *  Chrome Process                       ContentProcess 1
  *  ------------------                   ------------------
  *
- *       AudioNotification                       AudioStream 1    AudioStream N
+ *       AudioNotification               DeviceChangeListener 1   DeviceChangeListener N
  *             |      ^                             |      ^         ^
  *          (4)|      |(2)                          |(3)   |(8)      .
  *             v      |                             v      |         v
  *   AudioNotificationSender                  AudioNotificationReceiver
  *     ^       |      ^                                ^
  *     .    (5)|      |(1)                             |(7)
  *     .       v      |                                |
  *     .  (P)ContentParent 1                   (P)ContentChild 1
  *     .          |                                    ^
  *     .       (6)|                                    |
  *     .          |                                    |
  *     .          |                                    |
  *     .          +------------------------------------+
- *     .
+ *     .                      PContent IPC
  *     .
  *     .                                 Content Process M
  *     .                                 ------------------
  *     .                                          .
  *     v                                          .
  *   (P)ContentParent M  < . . . . . . . . .  > (P)ContentChild M
  *                            PContent IPC
  *
  * Steps
  * --------
  *  1) Initailize the AudioNotificationSender when ContentParent is created.
  *  2) Create an AudioNotification to get the device-changed signal
  *     from the system.
- *  3) Register the AudioStream to AudioNotificationReceiver when it's created.
+ *  3) Register the DeviceChangeListener to AudioNotificationReceiver when it's created.
  *  4) When the default device is changed, AudioNotification get the signal and
  *  5) Pass this message by AudioNotificationSender.
  *  6) The AudioNotificationSender sends the device-changed notification via
  *     the PContent.
  *  7) The ContentChild will call AudioNotificationReceiver to
  *  8) Notify all the registered audio streams to reconfigure the output devices.
  *
  * Notes
  * --------
  * a) There is only one AudioNotificationSender and AudioNotification
  *    in a chrome process.
  * b) There is only one AudioNotificationReceiver and might be many
- *    AudioStreams in a content process.
+ *    DeviceChangeListeners in a content process.
  * c) There might be many ContentParent in a chrome process.
  * d) There is only one ContentChild in a content process.
- * e) All the Audiostreams are registered in the AudioNotificationReceiver.
+ * e) All the DeviceChangeListeners are registered in the AudioNotificationReceiver.
  * f) All the ContentParents are registered in the AudioNotificationSender.
  */
 
 namespace mozilla {
+namespace audio {
 
-class AudioStream;
-
-namespace audio {
+// The base class that provides a ResetDefaultDevice interface that
+// will be called in AudioNotificationReceiver::NotifyDefaultDeviceChanged
+// when it receives device-changed notification from the chrome process.
+class DeviceChangeListener
+{
+protected:
+  virtual ~DeviceChangeListener() {};
+public:
+  // The subclass shoule provide its own implementation switching the
+  // audio stream to the new default output device.
+  virtual void ResetDefaultDevice() = 0;
+};
 
 class AudioNotificationReceiver final
 {
 public:
-  // Add the AudioStream into the subscribers list.
-  static void Register(AudioStream* aAudioStream);
+  // Add the DeviceChangeListener into the subscribers list.
+  static void Register(DeviceChangeListener* aDeviceChangeListener);
 
-  // Remove the AudioStream from the subscribers list.
-  static void Unregister(AudioStream* aAudioStream);
+  // Remove the DeviceChangeListener from the subscribers list.
+  static void Unregister(DeviceChangeListener* aDeviceChangeListener);
 
   // Notify all the streams that the default device has been changed.
   static void NotifyDefaultDeviceChanged();
 }; // AudioNotificationReceiver
 
 } // namespace audio
 } // namespace mozilla