Bug 441197: Maintain an audio session in-process. r=jimm
authorKyle Huey <khuey@kylehuey.com>
Fri, 24 Jun 2011 07:20:03 -0700
changeset 72270 ffc023482b247a2508bcd6471efb2647a35b95ac
parent 72269 87c28ff05bad7b392fbaa0d6404058684f3a3590
child 72271 d004850a8358c85e2fdfb0e6dea290fc1909e5ee
push id159
push usereakhgari@mozilla.com
push dateTue, 16 Aug 2011 17:53:11 +0000
treeherdermozilla-beta@8786e3e49240 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs441197
milestone7.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 441197: Maintain an audio session in-process. r=jimm
widget/src/windows/AudioSession.cpp
widget/src/windows/AudioSession.h
widget/src/windows/Makefile.in
widget/src/windows/nsAppShell.cpp
new file mode 100644
--- /dev/null
+++ b/widget/src/windows/AudioSession.cpp
@@ -0,0 +1,347 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ *   Kyle Huey <me@kylehuey.com>
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * 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 ***** */
+
+#if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
+
+#include <windows.h>
+#include <audiopolicy.h>
+#include <Mmdeviceapi.h>
+
+#include "nsIStringBundle.h"
+#include "nsIUUIDGenerator.h"
+#include "nsIXULAppInfo.h"
+
+//#include "AudioSession.h"
+#include "nsCOMPtr.h"
+#include "nsAutoPtr.h"
+#include "nsServiceManagerUtils.h"
+#include "nsString.h"
+
+#include <objbase.h>
+
+namespace mozilla {
+namespace widget {
+
+/* 
+ * To take advantage of what Vista+ have to offer with respect to audio,
+ * we need to maintain an audio session.  This class wraps IAudioSessionControl
+ * and implements IAudioSessionEvents (for callbacks from Windows)
+ */
+class AudioSession: public IAudioSessionEvents {
+private:
+  AudioSession();
+  ~AudioSession();
+public:
+  static AudioSession* GetSingleton();
+
+  // COM IUnknown
+  STDMETHODIMP_(ULONG) AddRef();
+  STDMETHODIMP QueryInterface(REFIID, void**);
+  STDMETHODIMP_(ULONG) Release();
+
+  // IAudioSessionEvents
+  STDMETHODIMP OnChannelVolumeChanged(DWORD aChannelCount,
+                                      float aChannelVolumeArray[],
+                                      DWORD aChangedChannel,
+                                      LPCGUID aContext);
+  STDMETHODIMP OnDisplayNameChanged(LPCWSTR aDisplayName, LPCGUID aContext);
+  STDMETHODIMP OnGroupingParamChanged(LPCGUID aGroupingParam, LPCGUID aContext);
+  STDMETHODIMP OnIconPathChanged(LPCWSTR aIconPath, LPCGUID aContext);
+  STDMETHODIMP OnSessionDisconnected(AudioSessionDisconnectReason aReason);
+  STDMETHODIMP OnSimpleVolumeChanged(float aVolume,
+                                     BOOL aMute,
+                                     LPCGUID aContext);
+  STDMETHODIMP OnStateChanged(AudioSessionState aState);
+
+  nsresult Start();
+  nsresult Stop();
+
+protected:
+  nsRefPtr<IAudioSessionControl> mAudioSessionControl;
+  nsString mDisplayName;
+  nsString mIconPath;
+  nsID mSessionGroupingParameter;
+
+  nsAutoRefCnt mRefCnt;
+  NS_DECL_OWNINGTHREAD
+
+  static AudioSession* sService;
+};
+
+nsresult
+StartAudioSession()
+{
+  return AudioSession::GetSingleton()->Start();
+}
+
+nsresult
+StopAudioSession()
+{
+  return AudioSession::GetSingleton()->Stop();
+}
+
+AudioSession* AudioSession::sService = NULL;
+
+AudioSession::AudioSession()
+{
+
+}
+
+AudioSession::~AudioSession()
+{
+
+}
+
+AudioSession*
+AudioSession::GetSingleton()
+{
+  if (!(AudioSession::sService)) {
+    nsRefPtr<AudioSession> service = new AudioSession();
+    service.forget(&AudioSession::sService);
+  }
+
+  // We don't refcount AudioSession on the Gecko side, we hold one single ref
+  // as long as the appshell is running.
+  return AudioSession::sService;
+}
+
+// It appears Windows will use us on a background thread ...
+NS_IMPL_THREADSAFE_ADDREF(AudioSession)
+NS_IMPL_THREADSAFE_RELEASE(AudioSession)
+
+STDMETHODIMP
+AudioSession::QueryInterface(REFIID iid, void **ppv)
+{
+  const IID IID_IAudioSessionEvents = __uuidof(IAudioSessionEvents);
+  if ((IID_IUnknown == iid) ||
+      (IID_IAudioSessionEvents == iid)) {
+    *ppv = static_cast<IAudioSessionEvents*>(this);
+    AddRef();
+    return S_OK;
+  }
+
+  return E_NOINTERFACE;
+}
+
+// Once we are started Windows will hold a reference to us through our
+// IAudioSessionEvents interface that will keep us alive until the appshell
+// calls Stop.
+nsresult
+AudioSession::Start()
+{
+  const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
+  const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
+  const IID IID_IAudioSessionManager = __uuidof(IAudioSessionManager);
+
+  HRESULT hr;
+
+  if (FAILED(::CoInitialize(NULL)))
+    return NS_ERROR_FAILURE;
+
+  nsCOMPtr<nsIStringBundleService> bundleService = 
+    do_GetService(NS_STRINGBUNDLE_CONTRACTID);
+  NS_ENSURE_TRUE(bundleService, NS_ERROR_FAILURE);
+
+  nsCOMPtr<nsIStringBundle> bundle;
+  bundleService->CreateBundle("chrome://branding/locale/brand.properties",
+                              getter_AddRefs(bundle));
+  NS_ENSURE_TRUE(bundle, NS_ERROR_FAILURE);
+
+  bundle->GetStringFromName(NS_LITERAL_STRING("brandFullName").get(),
+                            getter_Copies(mDisplayName));
+
+  PRUnichar *buffer;
+  mIconPath.GetMutableData(&buffer, MAX_PATH);
+
+  // XXXkhuey we should provide a way for a xulrunner app to specify an icon
+  // that's not in the product binary.
+  ::GetModuleFileNameW(NULL, buffer, MAX_PATH);
+
+  nsCOMPtr<nsIUUIDGenerator> uuidgen =
+    do_GetService("@mozilla.org/uuid-generator;1");
+  NS_ASSERTION(uuidgen, "No UUID-Generator?!?");
+
+  uuidgen->GenerateUUIDInPlace(&mSessionGroupingParameter);
+
+  nsRefPtr<IMMDeviceEnumerator> enumerator;
+  hr = ::CoCreateInstance(CLSID_MMDeviceEnumerator,
+                          NULL,
+                          CLSCTX_ALL,
+                          IID_IMMDeviceEnumerator,
+                          getter_AddRefs(enumerator));
+  if (FAILED(hr))
+    return NS_ERROR_NOT_AVAILABLE;
+
+  nsRefPtr<IMMDevice> device;
+  hr = enumerator->GetDefaultAudioEndpoint(EDataFlow::eRender,
+                                           ERole::eMultimedia,
+                                           getter_AddRefs(device));
+  if (FAILED(hr)) {
+    if (hr == E_NOTFOUND)
+      return NS_ERROR_NOT_AVAILABLE;
+    return NS_ERROR_FAILURE;
+  }
+
+  nsRefPtr<IAudioSessionManager> manager;
+  hr = device->Activate(IID_IAudioSessionManager,
+                        CLSCTX_ALL,
+                        NULL,
+                        getter_AddRefs(manager));
+  if (FAILED(hr))
+    return NS_ERROR_FAILURE;
+
+  hr = manager->GetAudioSessionControl(NULL,
+                                       FALSE,
+                                       getter_AddRefs(mAudioSessionControl));
+  if (FAILED(hr))
+    return NS_ERROR_FAILURE;
+
+  hr = mAudioSessionControl->SetGroupingParam((LPCGUID)&mSessionGroupingParameter,
+                                              NULL);
+  if (FAILED(hr)) {
+    StopInternal();
+    return NS_ERROR_FAILURE;
+  }
+
+  hr = mAudioSessionControl->SetDisplayName(mDisplayName.get(), NULL);
+  if (FAILED(hr)) {
+    StopInternal();
+    return NS_ERROR_FAILURE;
+  }
+
+  hr = mAudioSessionControl->SetIconPath(mIconPath.get(), NULL);
+  if (FAILED(hr)) {
+    StopInternal();
+    return NS_ERROR_FAILURE;
+  }
+
+  hr = mAudioSessionControl->RegisterAudioSessionNotification(this);
+  if (FAILED(hr)) {
+    StopInternal();
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
+void
+AudioSession::StopInternal()
+{
+  if (mAudioSessionControl) {
+    mAudioSessionControl->SetGroupingParam((LPCGUID)&blankId, NULL);
+    mAudioSessionControl->UnregisterAudioSessionNotification(this);
+    mAudioSessionControl = nsnull;
+  }
+}
+
+nsresult
+AudioSession::Stop()
+{
+  const nsID blankId = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0} };
+  nsRefPtr<AudioSession> kungFuDeathGrip;
+  kungFuDeathGrip.swap(sService);
+
+  StopInternal();
+
+  // At this point kungFuDeathGrip should be the only reference to AudioSession
+
+  ::CoUninitialize();
+
+  return NS_OK;
+}
+
+STDMETHODIMP
+AudioSession::OnChannelVolumeChanged(DWORD aChannelCount,
+                                     float aChannelVolumeArray[],
+                                     DWORD aChangedChannel,
+                                     LPCGUID aContext)
+{
+  return S_OK; // NOOP
+}
+
+STDMETHODIMP
+AudioSession::OnDisplayNameChanged(LPCWSTR aDisplayName,
+                                   LPCGUID aContext)
+{
+  return S_OK; // NOOP
+}
+
+STDMETHODIMP
+AudioSession::OnGroupingParamChanged(LPCGUID aGroupingParam,
+                                     LPCGUID aContext)
+{
+  return S_OK; // NOOP
+}
+
+STDMETHODIMP
+AudioSession::OnIconPathChanged(LPCWSTR aIconPath,
+                                LPCGUID aContext)
+{
+  return S_OK; // NOOP
+}
+
+STDMETHODIMP
+AudioSession::OnSessionDisconnected(AudioSessionDisconnectReason aReason)
+{
+  if (!mAudioSessionControl)
+    return S_OK;
+
+  mAudioSessionControl->UnregisterAudioSessionNotification(this);
+  mAudioSessionControl = nsnull;
+  Start(); // If it fails there's not much we can do
+  return S_OK;
+}
+
+STDMETHODIMP
+AudioSession::OnSimpleVolumeChanged(float aVolume,
+                                    BOOL aMute,
+                                    LPCGUID aContext)
+{
+  return S_OK; // NOOP
+}
+
+STDMETHODIMP
+AudioSession::OnStateChanged(AudioSessionState aState)
+{
+  return S_OK; // NOOP
+}
+
+} // namespace widget
+} // namespace mozilla
+
+#endif // MOZ_NTDDI_LONGHORN
new file mode 100644
--- /dev/null
+++ b/widget/src/windows/AudioSession.h
@@ -0,0 +1,50 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ *   Kyle Huey <me@kylehuey.com>
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * 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 ***** */
+
+#if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
+
+namespace mozilla {
+namespace widget {
+
+nsresult StartAudioSession();
+nsresult StopAudioSession();
+
+} // namespace widget
+} // namespace mozilla
+
+#endif // MOZ_WINSK_TARGETVER >= MOZ_NTDDI_LONGHORN
--- a/widget/src/windows/Makefile.in
+++ b/widget/src/windows/Makefile.in
@@ -68,16 +68,17 @@ CPPSRCS = \
   TaskbarPreview.cpp \
   TaskbarTabPreview.cpp \
   TaskbarWindowPreview.cpp \
   TaskbarPreviewButton.cpp \
   JumpListBuilder.cpp \
   JumpListItem.cpp \
   GfxInfo.cpp \
   WidgetTraceEvent.cpp \
+  AudioSession.cpp \
   $(NULL)
 
 ifdef MOZ_CRASHREPORTER
 CPPSRCS += LSPAnnotator.cpp
 endif
 
 ifdef NS_PRINTING
 CPPSRCS		+= \
@@ -111,16 +112,18 @@ DEFINES		+= -DMOZ_ENABLE_D3D9_LAYER
 endif
 
 ifdef MOZ_ENABLE_D3D10_LAYER
 DEFINES		+= -DMOZ_ENABLE_D3D10_LAYER
 endif
 
 
 EXPORTS		= nsdefs.h WindowHook.h
+EXPORTS_NAMESPACES = mozilla/widget
+EXPORTS_mozilla/widget = AudioSession.h
 
 LOCAL_INCLUDES	= \
 		-I. \
 		-I$(srcdir)/../xpwidgets \
 		-I$(srcdir) \
 		-I$(topsrcdir)/toolkit/xre \
 		-I$(topsrcdir)/xpcom/base \
 		$(NULL)
--- a/widget/src/windows/nsAppShell.cpp
+++ b/widget/src/windows/nsAppShell.cpp
@@ -40,16 +40,17 @@
 
 #include "mozilla/ipc/RPCChannel.h"
 #include "nsAppShell.h"
 #include "nsToolkit.h"
 #include "nsThreadUtils.h"
 #include "WinTaskbar.h"
 #include "nsString.h"
 #include "nsIMM32Handler.h"
+#include "mozilla/widget/AudioSession.h"
 
 // For skidmark code
 #include <windows.h> 
 #include <tlhelp32.h> 
 
 const PRUnichar* kAppShellEventId = L"nsAppShell:EventID";
 const PRUnichar* kTaskbarButtonEventId = L"TaskbarButtonCreated";
 
@@ -239,20 +240,30 @@ CollectNewLoadedModules()
   CloseHandle(hModuleSnap);
 }
 
 NS_IMETHODIMP
 nsAppShell::Run(void)
 {
   LoadedModuleInfo modules[NUM_LOADEDMODULEINFO];
   memset(modules, 0, sizeof(modules));
-  sLoadedModules = modules;
+  sLoadedModules = modules;	
+
+#if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
+  // Ignore failure; failing to start the application is not exactly an
+  // appropriate response to failing to start an audio session.
+  mozilla::widget::StartAudioSession();
+#endif
 
   nsresult rv = nsBaseAppShell::Run();
 
+#ifdef MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
+  mozilla::widget::StopAudioSession();
+#endif
+
   // Don't forget to null this out!
   sLoadedModules = nsnull;
 
   return rv;
 }
 
 #endif