2012-12-06 18:29:20 +04: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"
|
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(AudioChannelAgent, nsIAudioChannelAgent)
|
|
|
|
|
|
|
|
AudioChannelAgent::AudioChannelAgent()
|
2013-04-04 00:35:05 +04:00
|
|
|
: mAudioChannelType(AUDIO_AGENT_CHANNEL_ERROR)
|
2012-12-06 18:29:20 +04:00
|
|
|
, mIsRegToService(false)
|
|
|
|
, mVisible(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelAgent::~AudioChannelAgent()
|
|
|
|
{
|
2013-01-15 18:31:19 +04:00
|
|
|
if (mIsRegToService) {
|
|
|
|
StopPlaying();
|
|
|
|
}
|
2012-12-06 18:29:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute long audioChannelType; */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::GetAudioChannelType(int32_t *aAudioChannelType)
|
|
|
|
{
|
|
|
|
*aAudioChannelType = mAudioChannelType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:35:05 +04:00
|
|
|
/* boolean init (in long channelType, in nsIAudioChannelAgentCallback callback); */
|
2012-12-06 18:29:20 +04:00
|
|
|
NS_IMETHODIMP AudioChannelAgent::Init(int32_t channelType, nsIAudioChannelAgentCallback *callback)
|
2013-04-04 00:35:05 +04:00
|
|
|
{
|
|
|
|
return InitInternal(channelType, callback, /* useWeakRef = */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean initWithWeakCallback (in long channelType,
|
|
|
|
* in nsIAudioChannelAgentCallback callback); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelAgent::InitWithWeakCallback(int32_t channelType,
|
|
|
|
nsIAudioChannelAgentCallback *callback)
|
|
|
|
{
|
|
|
|
return InitInternal(channelType, callback, /* useWeakRef = */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
AudioChannelAgent::InitInternal(int32_t aChannelType,
|
|
|
|
nsIAudioChannelAgentCallback *aCallback,
|
|
|
|
bool aUseWeakRef)
|
2012-12-06 18:29:20 +04:00
|
|
|
{
|
|
|
|
// We syncd the enum of channel type between nsIAudioChannelAgent.idl and
|
|
|
|
// AudioChannelCommon.h the same.
|
|
|
|
MOZ_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");
|
|
|
|
|
|
|
|
if (mAudioChannelType != AUDIO_AGENT_CHANNEL_ERROR ||
|
2013-04-04 00:35:05 +04:00
|
|
|
aChannelType > AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION ||
|
|
|
|
aChannelType < AUDIO_AGENT_CHANNEL_NORMAL) {
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:35:05 +04:00
|
|
|
mAudioChannelType = aChannelType;
|
|
|
|
|
|
|
|
if (aUseWeakRef) {
|
|
|
|
mWeakCallback = do_GetWeakReference(aCallback);
|
|
|
|
} else {
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean startPlaying (); */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::StartPlaying(bool *_retval)
|
|
|
|
{
|
2012-12-07 15:31:57 +04:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
2012-12-06 18:29:20 +04:00
|
|
|
if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
|
2013-01-15 18:31:19 +04:00
|
|
|
service == nullptr || mIsRegToService) {
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:31:57 +04:00
|
|
|
service->RegisterAudioChannelAgent(this,
|
2012-12-06 18:29:20 +04:00
|
|
|
static_cast<AudioChannelType>(mAudioChannelType));
|
2013-01-09 11:18:16 +04:00
|
|
|
*_retval = !service->GetMuted(this, !mVisible);
|
2012-12-06 18:29:20 +04:00
|
|
|
mIsRegToService = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void stopPlaying (); */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::StopPlaying(void)
|
|
|
|
{
|
|
|
|
if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
|
2013-01-15 18:31:19 +04:00
|
|
|
!mIsRegToService) {
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-12-07 15:31:57 +04:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
|
|
|
service->UnregisterAudioChannelAgent(this);
|
2012-12-06 18:29:20 +04:00
|
|
|
mIsRegToService = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void setVisibilityState (in boolean visible); */
|
|
|
|
NS_IMETHODIMP AudioChannelAgent::SetVisibilityState(bool visible)
|
|
|
|
{
|
|
|
|
bool oldVisibility = mVisible;
|
|
|
|
|
2013-04-04 00:35:05 +04:00
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
mVisible = visible;
|
2013-04-04 00:35:05 +04:00
|
|
|
if (mIsRegToService && oldVisibility != mVisible && callback) {
|
2012-12-07 15:31:57 +04:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
2013-04-04 00:35:05 +04:00
|
|
|
callback->CanPlayChanged(!service->GetMuted(this, !mVisible));
|
2012-12-06 18:29:20 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioChannelAgent::NotifyAudioChannelStateChanged()
|
|
|
|
{
|
2013-04-04 00:35:05 +04:00
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
if (callback) {
|
2012-12-07 15:31:57 +04:00
|
|
|
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
|
2013-04-04 00:35:05 +04:00
|
|
|
callback->CanPlayChanged(!service->GetMuted(this, !mVisible));
|
2012-12-06 18:29:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:35:05 +04:00
|
|
|
already_AddRefed<nsIAudioChannelAgentCallback>
|
|
|
|
AudioChannelAgent::GetCallback()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = mCallback;
|
|
|
|
if (!callback) {
|
|
|
|
callback = do_QueryReferent(mWeakCallback);
|
|
|
|
}
|
|
|
|
return callback.forget();
|
|
|
|
}
|