Bug 736939 - AudioManager implementation. r=cjones, a=b2g-only
authorSteven Lee <slee@mozilla.com>
Sun, 22 Apr 2012 14:14:36 -0400
changeset 92101 ce9fff905894b2d253c4d9cb496e286f6f801d6a
parent 92100 e2af0b79da44efb6b6256109944974889e83d3f0
child 92102 03d6eed81646a0010c3ffacee20210ccdaaa7eb9
child 92106 c823e4f030174242f6f22495e5115a1dbe554a17
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerscjones, b2g-only
bugs736939
milestone14.0a1
Bug 736939 - AudioManager implementation. r=cjones, a=b2g-only
dom/system/gonk/AudioManager.cpp
dom/system/gonk/AudioManager.h
dom/system/gonk/Makefile.in
--- a/dom/system/gonk/AudioManager.cpp
+++ b/dom/system/gonk/AudioManager.cpp
@@ -30,24 +30,76 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+#include <android/log.h> 
+
+#include "mozilla/Hal.h"
 #include "AudioManager.h"
 #include "gonk/AudioSystem.h"
 
 using namespace mozilla::dom::gonk;
 using namespace android;
+using namespace mozilla::hal;
+using namespace mozilla;
+
+#define LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "AudioManager" , ## args) 
 
 NS_IMPL_ISUPPORTS1(AudioManager, nsIAudioManager)
 
+static AudioSystem::audio_devices
+GetRoutingMode(int aType) {
+  if (aType == nsIAudioManager::FORCE_SPEAKER) {
+    return AudioSystem::DEVICE_OUT_SPEAKER;
+  } else if (aType == nsIAudioManager::FORCE_HEADPHONES) {
+    return AudioSystem::DEVICE_OUT_WIRED_HEADSET;
+  } else if (aType == nsIAudioManager::FORCE_BT_SCO) {
+    return AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
+  } else if (aType == nsIAudioManager::FORCE_BT_A2DP) {
+    return AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
+  } else {
+    return AudioSystem::DEVICE_IN_DEFAULT;
+  }
+}
+
+static void
+InternalSetAudioRoutes(SwitchState aState)
+{
+  if (aState == SWITCH_STATE_ON) {
+    AudioManager::SetAudioRoute(nsIAudioManager::FORCE_HEADPHONES);
+  } else if (aState == SWITCH_STATE_OFF) {
+    AudioManager::SetAudioRoute(nsIAudioManager::FORCE_SPEAKER);
+  }
+}
+
+class HeadphoneSwitchObserver : public SwitchObserver
+{
+public:
+  void Notify(const SwitchEvent& aEvent) {
+    InternalSetAudioRoutes(aEvent.status());
+  }
+};
+
+AudioManager::AudioManager() : mPhoneState(PHONE_STATE_CURRENT),
+                 mObserver(new HeadphoneSwitchObserver())
+{
+  RegisterSwitchObserver(SWITCH_HEADPHONES, mObserver);
+  
+  InternalSetAudioRoutes(GetCurrentSwitchState(SWITCH_HEADPHONES));
+}
+
+AudioManager::~AudioManager() {
+  UnregisterSwitchObserver(SWITCH_HEADPHONES, mObserver);
+}
+
 NS_IMETHODIMP
 AudioManager::GetMicrophoneMuted(bool* aMicrophoneMuted)
 {
   if (AudioSystem::isMicrophoneMuted(aMicrophoneMuted)) {
     return NS_ERROR_FAILURE;
   }
   return NS_OK;
 }
@@ -156,8 +208,17 @@ AudioManager::GetForceForUse(PRInt32 aUs
   } else if (static_cast<
              audio_policy_forced_cfg_t (*)(audio_policy_force_use_t)
              >(AudioSystem::getForceUse)) {
     // Dynamically resolved the ICS signature.
     *aForce = AudioSystem::getForceUse((audio_policy_force_use_t)aUsage);
   }
   return NS_OK;
 }
+
+void
+AudioManager::SetAudioRoute(int aRoutes) {
+  audio_io_handle_t handle = AudioSystem::getOutput(AudioSystem::SYSTEM);
+  
+  String8 cmd;
+  cmd.appendFormat("%s=%d", AudioParameter::keyRouting, GetRoutingMode(aRoutes));
+  AudioSystem::setParameters(handle, cmd);
+}
--- a/dom/system/gonk/AudioManager.h
+++ b/dom/system/gonk/AudioManager.h
@@ -33,40 +33,49 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef mozilla_dom_system_b2g_audiomanager_h__
 #define mozilla_dom_system_b2g_audiomanager_h__
 
+#include "mozilla/Observer.h"
+#include "nsAutoPtr.h"
 #include "nsIAudioManager.h"
 
-
 // {b2b51423-502d-4d77-89b3-7786b562b084}
 #define NS_AUDIOMANAGER_CID {0x94f6fd70, 0x7615, 0x4af9, \
       {0x89, 0x10, 0xf9, 0x3c, 0x55, 0xe6, 0x62, 0xec}}
 #define NS_AUDIOMANAGER_CONTRACTID "@mozilla.org/telephony/audiomanager;1"
 
+
 namespace mozilla {
+namespace hal {
+class SwitchEvent;
+typedef Observer<SwitchEvent> SwitchObserver;
+} // namespace hal
+
 namespace dom {
 namespace gonk {
 
 class AudioManager : public nsIAudioManager
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIAUDIOMANAGER
 
-  AudioManager() : mPhoneState(PHONE_STATE_CURRENT)
-  {
-  }
+  AudioManager();
+  ~AudioManager();
 
+  static void SetAudioRoute(int aRoutes);
 protected:
   PRInt32 mPhoneState;
+
+private:
+  nsAutoPtr<mozilla::hal::SwitchObserver> mObserver;
 };
 
-
-} /* namespace telephony */
+} /* namespace gonk */
 } /* namespace dom */
 } /* namespace mozilla */
 
 #endif // mozilla_dom_system_b2g_audiomanager_h__
--- a/dom/system/gonk/Makefile.in
+++ b/dom/system/gonk/Makefile.in
@@ -62,8 +62,9 @@ EXTRA_JS_MODULES = \
   systemlibs.js \
   $(NULL)
 
 ifdef ENABLE_TESTS
 XPCSHELL_TESTS = tests
 endif
 
 include $(topsrcdir)/config/rules.mk
+include $(topsrcdir)/ipc/chromium/chromium-config.mk