2011-06-24 07:20:03 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2011-06-24 07:20:03 -07:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <audiopolicy.h>
|
2011-07-13 08:58:33 -07:00
|
|
|
#include <mmdeviceapi.h>
|
2011-06-24 07:20:03 -07:00
|
|
|
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsIXULAppInfo.h"
|
|
|
|
|
|
|
|
//#include "AudioSession.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsString.h"
|
2011-12-29 08:59:29 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2011-07-13 08:58:33 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-06-14 11:28:06 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-06-24 07:20:03 -07:00
|
|
|
|
|
|
|
#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)
|
|
|
|
*/
|
2012-06-14 11:28:06 -07:00
|
|
|
class AudioSession MOZ_FINAL : public IAudioSessionEvents {
|
2011-06-24 07:20:03 -07:00
|
|
|
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);
|
2011-12-29 08:59:29 -08:00
|
|
|
private:
|
|
|
|
nsresult OnSessionDisconnectedInternal();
|
|
|
|
public:
|
2011-06-24 07:20:03 -07:00
|
|
|
STDMETHODIMP OnSimpleVolumeChanged(float aVolume,
|
|
|
|
BOOL aMute,
|
|
|
|
LPCGUID aContext);
|
|
|
|
STDMETHODIMP OnStateChanged(AudioSessionState aState);
|
|
|
|
|
|
|
|
nsresult Start();
|
|
|
|
nsresult Stop();
|
2011-06-24 07:20:03 -07:00
|
|
|
void StopInternal();
|
|
|
|
|
|
|
|
nsresult GetSessionData(nsID& aID,
|
|
|
|
nsString& aSessionName,
|
|
|
|
nsString& aIconPath);
|
|
|
|
|
|
|
|
nsresult SetSessionData(const nsID& aID,
|
|
|
|
const nsString& aSessionName,
|
|
|
|
const nsString& aIconPath);
|
|
|
|
|
|
|
|
enum SessionState {
|
|
|
|
UNINITIALIZED, // Has not been initialized yet
|
|
|
|
STARTED, // Started
|
|
|
|
CLONED, // SetSessionInfoCalled, Start not called
|
|
|
|
FAILED, // The autdio session failed to start
|
2012-02-17 06:17:27 -08:00
|
|
|
STOPPED, // Stop called
|
|
|
|
AUDIO_SESSION_DISCONNECTED // Audio session disconnected
|
2011-06-24 07:20:03 -07:00
|
|
|
};
|
2011-06-24 07:20:03 -07:00
|
|
|
protected:
|
|
|
|
nsRefPtr<IAudioSessionControl> mAudioSessionControl;
|
|
|
|
nsString mDisplayName;
|
|
|
|
nsString mIconPath;
|
|
|
|
nsID mSessionGroupingParameter;
|
2011-06-24 07:20:03 -07:00
|
|
|
SessionState mState;
|
2011-06-24 07:20:03 -07:00
|
|
|
|
2013-07-18 19:24:15 -07:00
|
|
|
ThreadSafeAutoRefCnt mRefCnt;
|
2011-06-24 07:20:03 -07:00
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
|
|
|
|
static AudioSession* sService;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
StartAudioSession()
|
|
|
|
{
|
|
|
|
return AudioSession::GetSingleton()->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
StopAudioSession()
|
|
|
|
{
|
|
|
|
return AudioSession::GetSingleton()->Stop();
|
|
|
|
}
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
nsresult
|
|
|
|
GetAudioSessionData(nsID& aID,
|
|
|
|
nsString& aSessionName,
|
|
|
|
nsString& aIconPath)
|
|
|
|
{
|
|
|
|
return AudioSession::GetSingleton()->GetSessionData(aID,
|
|
|
|
aSessionName,
|
|
|
|
aIconPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RecvAudioSessionData(const nsID& aID,
|
|
|
|
const nsString& aSessionName,
|
|
|
|
const nsString& aIconPath)
|
|
|
|
{
|
|
|
|
return AudioSession::GetSingleton()->SetSessionData(aID,
|
|
|
|
aSessionName,
|
|
|
|
aIconPath);
|
|
|
|
}
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
AudioSession* AudioSession::sService = NULL;
|
|
|
|
|
|
|
|
AudioSession::AudioSession()
|
|
|
|
{
|
2011-06-24 07:20:03 -07:00
|
|
|
mState = UNINITIALIZED;
|
2011-06-24 07:20:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ...
|
2013-07-18 19:24:15 -07:00
|
|
|
NS_IMPL_ADDREF(AudioSession)
|
|
|
|
NS_IMPL_RELEASE(AudioSession)
|
2011-06-24 07:20:03 -07:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2012-02-17 06:17:27 -08:00
|
|
|
NS_ABORT_IF_FALSE(mState == UNINITIALIZED ||
|
|
|
|
mState == CLONED ||
|
|
|
|
mState == AUDIO_SESSION_DISCONNECTED,
|
2011-06-24 07:20:03 -07:00
|
|
|
"State invariants violated");
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
|
|
|
|
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
|
|
|
|
const IID IID_IAudioSessionManager = __uuidof(IAudioSessionManager);
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
2012-08-09 17:54:35 -07:00
|
|
|
// Don't check for errors in case something already initialized COM
|
|
|
|
// on this thread.
|
|
|
|
CoInitialize(NULL);
|
2011-06-24 07:20:03 -07:00
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
if (mState == UNINITIALIZED) {
|
|
|
|
mState = FAILED;
|
|
|
|
|
|
|
|
// XXXkhuey implement this for content processes
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ABORT_IF_FALSE(XRE_GetProcessType() == GeckoProcessType_Default,
|
|
|
|
"Should only get here in a chrome process!");
|
|
|
|
|
|
|
|
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);
|
2011-06-24 07:20:03 -07:00
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
bundle->GetStringFromName(NS_LITERAL_STRING("brandFullName").get(),
|
|
|
|
getter_Copies(mDisplayName));
|
2011-06-24 07:20:03 -07:00
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
PRUnichar *buffer;
|
|
|
|
mIconPath.GetMutableData(&buffer, MAX_PATH);
|
2011-06-24 07:20:03 -07:00
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
// 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);
|
2011-06-24 07:20:03 -07:00
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1");
|
2012-08-09 17:54:35 -07:00
|
|
|
NS_ENSURE_TRUE(uuidgen, NS_ERROR_FAILURE);
|
2011-06-24 07:20:03 -07:00
|
|
|
uuidgen->GenerateUUIDInPlace(&mSessionGroupingParameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = FAILED;
|
2011-06-24 07:20:03 -07:00
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
NS_ABORT_IF_FALSE(!mDisplayName.IsEmpty() || !mIconPath.IsEmpty(),
|
|
|
|
"Should never happen ...");
|
2011-06-24 07:20:03 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
mState = STARTED;
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioSession::StopInternal()
|
|
|
|
{
|
2011-06-24 07:20:03 -07:00
|
|
|
static const nsID blankId = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0} };
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
if (mAudioSessionControl) {
|
|
|
|
mAudioSessionControl->SetGroupingParam((LPCGUID)&blankId, NULL);
|
|
|
|
mAudioSessionControl->UnregisterAudioSessionNotification(this);
|
2012-07-30 07:20:58 -07:00
|
|
|
mAudioSessionControl = nullptr;
|
2011-06-24 07:20:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AudioSession::Stop()
|
|
|
|
{
|
2011-06-24 07:20:03 -07:00
|
|
|
NS_ABORT_IF_FALSE(mState == STARTED ||
|
|
|
|
mState == UNINITIALIZED || // XXXremove this
|
|
|
|
mState == FAILED,
|
|
|
|
"State invariants violated");
|
|
|
|
mState = STOPPED;
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
nsRefPtr<AudioSession> kungFuDeathGrip;
|
|
|
|
kungFuDeathGrip.swap(sService);
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Content)
|
|
|
|
StopInternal();
|
2011-06-24 07:20:03 -07:00
|
|
|
|
|
|
|
// At this point kungFuDeathGrip should be the only reference to AudioSession
|
|
|
|
|
|
|
|
::CoUninitialize();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
void CopynsID(nsID& lhs, const nsID& rhs)
|
|
|
|
{
|
|
|
|
lhs.m0 = rhs.m0;
|
|
|
|
lhs.m1 = rhs.m1;
|
|
|
|
lhs.m2 = rhs.m2;
|
|
|
|
for (int i = 0; i < 8; i++ ) {
|
|
|
|
lhs.m3[i] = rhs.m3[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AudioSession::GetSessionData(nsID& aID,
|
|
|
|
nsString& aSessionName,
|
|
|
|
nsString& aIconPath)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mState == FAILED ||
|
|
|
|
mState == STARTED ||
|
|
|
|
mState == CLONED,
|
|
|
|
"State invariants violated");
|
|
|
|
|
|
|
|
CopynsID(aID, mSessionGroupingParameter);
|
|
|
|
aSessionName = mDisplayName;
|
|
|
|
aIconPath = mIconPath;
|
|
|
|
|
|
|
|
if (mState == FAILED)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AudioSession::SetSessionData(const nsID& aID,
|
|
|
|
const nsString& aSessionName,
|
|
|
|
const nsString& aIconPath)
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mState == UNINITIALIZED,
|
|
|
|
"State invariants violated");
|
|
|
|
NS_ABORT_IF_FALSE(XRE_GetProcessType() != GeckoProcessType_Default,
|
|
|
|
"Should never get here in a chrome process!");
|
|
|
|
mState = CLONED;
|
|
|
|
|
|
|
|
CopynsID(mSessionGroupingParameter, aID);
|
|
|
|
mDisplayName = aSessionName;
|
|
|
|
mIconPath = aIconPath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
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)
|
2011-12-29 08:59:29 -08:00
|
|
|
{
|
|
|
|
// Run our code asynchronously. Per MSDN we can't do anything interesting
|
|
|
|
// in this callback.
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethod(this, &AudioSession::OnSessionDisconnectedInternal);
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AudioSession::OnSessionDisconnectedInternal()
|
2011-06-24 07:20:03 -07:00
|
|
|
{
|
|
|
|
if (!mAudioSessionControl)
|
2011-12-29 08:59:29 -08:00
|
|
|
return NS_OK;
|
2011-06-24 07:20:03 -07:00
|
|
|
|
|
|
|
mAudioSessionControl->UnregisterAudioSessionNotification(this);
|
2012-07-30 07:20:58 -07:00
|
|
|
mAudioSessionControl = nullptr;
|
2011-12-29 08:59:29 -08:00
|
|
|
|
2012-02-17 06:17:27 -08:00
|
|
|
mState = AUDIO_SESSION_DISCONNECTED;
|
|
|
|
CoUninitialize();
|
2011-12-29 08:59:29 -08:00
|
|
|
Start(); // If it fails there's not much we can do.
|
|
|
|
return NS_OK;
|
2011-06-24 07:20:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|