2015-05-03 12:32:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-12-06 06:29:20 -08: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/. */
|
|
|
|
|
|
|
|
#include "AudioChannelAgent.h"
|
|
|
|
#include "AudioChannelService.h"
|
2014-03-11 03:46:04 -07:00
|
|
|
#include "nsIDOMWindow.h"
|
2014-03-11 03:46:55 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2014-03-11 03:46:04 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-12-06 06:29:20 -08:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-07-17 04:08:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AudioChannelAgent)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioChannelAgent)
|
|
|
|
tmp->Shutdown();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioChannelAgent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-04-23 08:28:00 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioChannelAgent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAudioChannelAgent)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioChannelAgent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioChannelAgent)
|
2012-12-06 06:29:20 -08:00
|
|
|
|
|
|
|
AudioChannelAgent::AudioChannelAgent()
|
2013-04-03 13:35:05 -07:00
|
|
|
: mAudioChannelType(AUDIO_AGENT_CHANNEL_ERROR)
|
2012-12-06 06:29:20 -08:00
|
|
|
, mIsRegToService(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelAgent::~AudioChannelAgent()
|
2015-07-17 04:08:00 -07:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelAgent::Shutdown()
|
2012-12-06 06:29:20 -08:00
|
|
|
{
|
2013-01-15 06:31:19 -08:00
|
|
|
if (mIsRegToService) {
|
2015-07-11 00:24:26 -07:00
|
|
|
NotifyStoppedPlaying();
|
2013-01-15 06:31:19 -08:00
|
|
|
}
|
2012-12-06 06:29:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute long audioChannelType; */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::GetAudioChannelType(int32_t *aAudioChannelType)
|
|
|
|
{
|
|
|
|
*aAudioChannelType = mAudioChannelType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:46:04 -07:00
|
|
|
/* boolean init (in nsIDOMWindow window, in long channelType,
|
|
|
|
* in nsIAudioChannelAgentCallback callback); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelAgent::Init(nsIDOMWindow* aWindow, int32_t aChannelType,
|
|
|
|
nsIAudioChannelAgentCallback *aCallback)
|
2013-04-03 13:35:05 -07:00
|
|
|
{
|
2014-03-11 03:46:04 -07:00
|
|
|
return InitInternal(aWindow, aChannelType, aCallback,
|
|
|
|
/* useWeakRef = */ false);
|
2013-04-03 13:35:05 -07:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:46:04 -07:00
|
|
|
/* boolean initWithWeakCallback (in nsIDOMWindow window, in long channelType,
|
2013-04-03 13:35:05 -07:00
|
|
|
* in nsIAudioChannelAgentCallback callback); */
|
|
|
|
NS_IMETHODIMP
|
2014-03-11 03:46:04 -07:00
|
|
|
AudioChannelAgent::InitWithWeakCallback(nsIDOMWindow* aWindow,
|
|
|
|
int32_t aChannelType,
|
|
|
|
nsIAudioChannelAgentCallback *aCallback)
|
2013-04-03 13:35:05 -07:00
|
|
|
{
|
2014-03-11 03:46:04 -07:00
|
|
|
return InitInternal(aWindow, aChannelType, aCallback,
|
|
|
|
/* useWeakRef = */ true);
|
2013-04-03 13:35:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-11 03:46:04 -07:00
|
|
|
AudioChannelAgent::InitInternal(nsIDOMWindow* aWindow, int32_t aChannelType,
|
2013-04-03 13:35:05 -07:00
|
|
|
nsIAudioChannelAgentCallback *aCallback,
|
2015-07-10 09:38:44 -07:00
|
|
|
bool aUseWeakRef)
|
2012-12-06 06:29:20 -08:00
|
|
|
{
|
|
|
|
// We syncd the enum of channel type between nsIAudioChannelAgent.idl and
|
2014-04-10 10:39:20 -07:00
|
|
|
// AudioChannelBinding.h the same.
|
2014-04-25 19:34:04 -07:00
|
|
|
MOZ_ASSERT(int(AUDIO_AGENT_CHANNEL_NORMAL) == int(AudioChannel::Normal) &&
|
|
|
|
int(AUDIO_AGENT_CHANNEL_CONTENT) == int(AudioChannel::Content) &&
|
|
|
|
int(AUDIO_AGENT_CHANNEL_NOTIFICATION) == int(AudioChannel::Notification) &&
|
|
|
|
int(AUDIO_AGENT_CHANNEL_ALARM) == int(AudioChannel::Alarm) &&
|
|
|
|
int(AUDIO_AGENT_CHANNEL_TELEPHONY) == int(AudioChannel::Telephony) &&
|
|
|
|
int(AUDIO_AGENT_CHANNEL_RINGER) == int(AudioChannel::Ringer) &&
|
|
|
|
int(AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION) == int(AudioChannel::Publicnotification),
|
2014-04-10 10:39:20 -07:00
|
|
|
"Enum of channel on nsIAudioChannelAgent.idl should be the same with AudioChannelBinding.h");
|
2012-12-06 06:29:20 -08:00
|
|
|
|
|
|
|
if (mAudioChannelType != AUDIO_AGENT_CHANNEL_ERROR ||
|
2013-04-03 13:35:05 -07:00
|
|
|
aChannelType > AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION ||
|
|
|
|
aChannelType < AUDIO_AGENT_CHANNEL_NORMAL) {
|
2012-12-06 06:29:20 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-04-08 01:24:04 -07:00
|
|
|
if (aWindow) {
|
2015-07-10 09:38:44 -07:00
|
|
|
nsCOMPtr<nsIDOMWindow> topWindow;
|
|
|
|
aWindow->GetScriptableTop(getter_AddRefs(topWindow));
|
|
|
|
MOZ_ASSERT(topWindow);
|
2015-07-10 09:38:51 -07:00
|
|
|
|
2015-07-10 09:38:44 -07:00
|
|
|
mWindow = do_QueryInterface(topWindow);
|
2015-07-10 09:38:51 -07:00
|
|
|
if (!mWindow) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-07-10 09:38:44 -07:00
|
|
|
mWindow = mWindow->GetOuterWindow();
|
2015-04-08 01:24:04 -07:00
|
|
|
}
|
|
|
|
|
2013-04-03 13:35:05 -07:00
|
|
|
mAudioChannelType = aChannelType;
|
|
|
|
|
|
|
|
if (aUseWeakRef) {
|
|
|
|
mWeakCallback = do_GetWeakReference(aCallback);
|
|
|
|
} else {
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2012-12-06 06:29:20 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-11 00:24:26 -07:00
|
|
|
/* boolean notifyStartedPlaying (); */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::NotifyStartedPlaying(float *aVolume,
|
|
|
|
bool* aMuted)
|
2012-12-06 06:29:20 -08:00
|
|
|
{
|
2015-07-10 09:38:44 -07:00
|
|
|
MOZ_ASSERT(aVolume);
|
|
|
|
MOZ_ASSERT(aMuted);
|
|
|
|
|
|
|
|
nsRefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
2012-12-06 06:29:20 -08:00
|
|
|
if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
|
2013-01-15 06:31:19 -08:00
|
|
|
service == nullptr || mIsRegToService) {
|
2012-12-06 06:29:20 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:31:57 -08:00
|
|
|
service->RegisterAudioChannelAgent(this,
|
2015-07-10 09:38:44 -07:00
|
|
|
static_cast<AudioChannel>(mAudioChannelType));
|
|
|
|
|
|
|
|
service->GetState(mWindow, mAudioChannelType, aVolume, aMuted);
|
|
|
|
|
2012-12-06 06:29:20 -08:00
|
|
|
mIsRegToService = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-11 00:24:26 -07:00
|
|
|
/* void notifyStoppedPlaying (); */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::NotifyStoppedPlaying(void)
|
2012-12-06 06:29:20 -08:00
|
|
|
{
|
|
|
|
if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
|
2013-01-15 06:31:19 -08:00
|
|
|
!mIsRegToService) {
|
2012-12-06 06:29:20 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-07-10 09:38:44 -07:00
|
|
|
nsRefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
2012-12-07 03:31:57 -08:00
|
|
|
service->UnregisterAudioChannelAgent(this);
|
2012-12-06 06:29:20 -08:00
|
|
|
mIsRegToService = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-03 13:35:05 -07:00
|
|
|
already_AddRefed<nsIAudioChannelAgentCallback>
|
|
|
|
AudioChannelAgent::GetCallback()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = mCallback;
|
|
|
|
if (!callback) {
|
|
|
|
callback = do_QueryReferent(mWeakCallback);
|
|
|
|
}
|
|
|
|
return callback.forget();
|
|
|
|
}
|
2014-03-11 03:46:55 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelAgent::WindowVolumeChanged()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
if (!callback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-10 09:38:44 -07:00
|
|
|
float volume = 1.0;
|
|
|
|
bool muted = false;
|
2015-07-10 17:55:59 -07:00
|
|
|
|
2015-07-10 09:38:44 -07:00
|
|
|
nsRefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
|
|
|
service->GetState(mWindow, mAudioChannelType, &volume, &muted);
|
2015-07-11 05:14:58 -07:00
|
|
|
|
2015-07-10 09:38:44 -07:00
|
|
|
callback->WindowVolumeChanged(volume, muted);
|
|
|
|
}
|
2015-07-11 05:14:58 -07:00
|
|
|
|
2015-07-10 09:38:44 -07:00
|
|
|
uint64_t
|
|
|
|
AudioChannelAgent::WindowID() const
|
|
|
|
{
|
|
|
|
return mWindow ? mWindow->WindowID() : 0;
|
2014-03-11 03:46:55 -07:00
|
|
|
}
|