Bug 441197: Maintain an audio session in-process. r=jimm

This commit is contained in:
Kyle Huey 2011-06-24 07:20:03 -07:00
parent 577cc65830
commit 1176ef58c8
4 changed files with 412 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -73,6 +73,7 @@ CPPSRCS = \
JumpListItem.cpp \
GfxInfo.cpp \
WidgetTraceEvent.cpp \
AudioSession.cpp \
$(NULL)
ifdef MOZ_CRASHREPORTER
@ -116,6 +117,8 @@ endif
EXPORTS = nsdefs.h WindowHook.h
EXPORTS_NAMESPACES = mozilla/widget
EXPORTS_mozilla/widget = AudioSession.h
LOCAL_INCLUDES = \
-I. \

View File

@ -45,6 +45,7 @@
#include "WinTaskbar.h"
#include "nsString.h"
#include "nsIMM32Handler.h"
#include "mozilla/widget/AudioSession.h"
// For skidmark code
#include <windows.h>
@ -244,10 +245,20 @@ 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;