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 "AudioChannelCommon.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;
|
|
|
|
|
2014-03-11 03:46:04 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_2(AudioChannelAgent, mWindow, mCallback)
|
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)
|
|
|
|
, mVisible(true)
|
2013-09-17 20:46:22 -07:00
|
|
|
, mWithVideo(false)
|
2012-12-06 06:29:20 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelAgent::~AudioChannelAgent()
|
|
|
|
{
|
2013-01-15 06:31:19 -08:00
|
|
|
if (mIsRegToService) {
|
|
|
|
StopPlaying();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-03-11 03:46:04 -07:00
|
|
|
/* void initWithVideo(in nsIDOMWindow window, in long channelType,
|
|
|
|
* in nsIAudioChannelAgentCallback callback, in boolean weak); */
|
2013-09-17 20:46:22 -07:00
|
|
|
NS_IMETHODIMP
|
2014-03-11 03:46:04 -07:00
|
|
|
AudioChannelAgent::InitWithVideo(nsIDOMWindow* aWindow, int32_t aChannelType,
|
|
|
|
nsIAudioChannelAgentCallback *aCallback,
|
2013-09-17 20:46:22 -07:00
|
|
|
bool aUseWeakRef)
|
|
|
|
{
|
2014-03-11 03:46:04 -07:00
|
|
|
return InitInternal(aWindow, aChannelType, aCallback, aUseWeakRef,
|
|
|
|
/* withVideo = */ true);
|
2013-09-17 20:46:22 -07:00
|
|
|
}
|
|
|
|
|
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,
|
2013-09-17 20:46:22 -07:00
|
|
|
bool aUseWeakRef, bool aWithVideo)
|
2012-12-06 06:29:20 -08:00
|
|
|
{
|
2014-03-11 03:46:04 -07:00
|
|
|
// We need the window only for IPC.
|
|
|
|
MOZ_ASSERT(aWindow || XRE_GetProcessType() == GeckoProcessType_Default);
|
|
|
|
|
2012-12-06 06:29:20 -08:00
|
|
|
// We syncd the enum of channel type between nsIAudioChannelAgent.idl and
|
|
|
|
// AudioChannelCommon.h the same.
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_NORMAL) ==
|
|
|
|
AUDIO_CHANNEL_NORMAL &&
|
|
|
|
static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_CONTENT) ==
|
|
|
|
AUDIO_CHANNEL_CONTENT &&
|
|
|
|
static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_NOTIFICATION) ==
|
|
|
|
AUDIO_CHANNEL_NOTIFICATION &&
|
|
|
|
static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_ALARM) ==
|
|
|
|
AUDIO_CHANNEL_ALARM &&
|
|
|
|
static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_TELEPHONY) ==
|
|
|
|
AUDIO_CHANNEL_TELEPHONY &&
|
|
|
|
static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_RINGER) ==
|
|
|
|
AUDIO_CHANNEL_RINGER &&
|
|
|
|
static_cast<AudioChannelType>(AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION) ==
|
|
|
|
AUDIO_CHANNEL_PUBLICNOTIFICATION,
|
|
|
|
"Enum of channel on nsIAudioChannelAgent.idl should be the same with AudioChannelCommon.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;
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:46:04 -07:00
|
|
|
mWindow = aWindow;
|
2013-04-03 13:35:05 -07:00
|
|
|
mAudioChannelType = aChannelType;
|
|
|
|
|
|
|
|
if (aUseWeakRef) {
|
|
|
|
mWeakCallback = do_GetWeakReference(aCallback);
|
|
|
|
} else {
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2013-09-17 20:46:22 -07:00
|
|
|
mWithVideo = aWithVideo;
|
|
|
|
|
2012-12-06 06:29:20 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean startPlaying (); */
|
2013-09-02 02:45:44 -07:00
|
|
|
NS_IMETHODIMP AudioChannelAgent::StartPlaying(int32_t *_retval)
|
2012-12-06 06:29:20 -08:00
|
|
|
{
|
2012-12-07 03:31:57 -08:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
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,
|
2013-09-17 20:46:22 -07:00
|
|
|
static_cast<AudioChannelType>(mAudioChannelType), mWithVideo);
|
2013-09-02 02:45:44 -07:00
|
|
|
*_retval = service->GetState(this, !mVisible);
|
2012-12-06 06:29:20 -08:00
|
|
|
mIsRegToService = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void stopPlaying (); */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::StopPlaying(void)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:31:57 -08:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
|
|
|
service->UnregisterAudioChannelAgent(this);
|
2012-12-06 06:29:20 -08:00
|
|
|
mIsRegToService = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void setVisibilityState (in boolean visible); */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::SetVisibilityState(bool visible)
|
|
|
|
{
|
|
|
|
bool oldVisibility = mVisible;
|
|
|
|
|
2013-04-03 13:35:05 -07:00
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
|
2012-12-06 06:29:20 -08:00
|
|
|
mVisible = visible;
|
2013-04-03 13:35:05 -07:00
|
|
|
if (mIsRegToService && oldVisibility != mVisible && callback) {
|
2012-12-07 03:31:57 -08:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
2013-09-02 02:45:44 -07:00
|
|
|
callback->CanPlayChanged(service->GetState(this, !mVisible));
|
2012-12-06 06:29:20 -08:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioChannelAgent::NotifyAudioChannelStateChanged()
|
|
|
|
{
|
2013-04-03 13:35:05 -07:00
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
if (callback) {
|
2012-12-07 03:31:57 -08:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
2013-09-02 02:45:44 -07:00
|
|
|
callback->CanPlayChanged(service->GetState(this, !mVisible));
|
2012-12-06 06:29:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback->WindowVolumeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelAgent::GetWindowVolume(float* aVolume)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aVolume);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mWindow);
|
|
|
|
if (!win) {
|
|
|
|
*aVolume = 1.0f;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVolume = win->GetAudioGlobalVolume();
|
|
|
|
return NS_OK;
|
|
|
|
}
|