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"
|
2014-03-11 14:46:04 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
2014-03-11 14:46:55 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2014-03-11 14:46:04 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-12-06 18:29:20 +04:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(AudioChannelAgent, mWindow, mCallback)
|
2013-04-23 19:28:00 +04: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 18:29:20 +04:00
|
|
|
|
|
|
|
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)
|
2013-09-18 07:46:22 +04:00
|
|
|
, mWithVideo(false)
|
2012-12-06 18:29:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-11 14:46:04 +04: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-04 00:35:05 +04:00
|
|
|
{
|
2014-03-11 14:46:04 +04:00
|
|
|
return InitInternal(aWindow, aChannelType, aCallback,
|
|
|
|
/* useWeakRef = */ false);
|
2013-04-04 00:35:05 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 14:46:04 +04:00
|
|
|
/* boolean initWithWeakCallback (in nsIDOMWindow window, in long channelType,
|
2013-04-04 00:35:05 +04:00
|
|
|
* in nsIAudioChannelAgentCallback callback); */
|
|
|
|
NS_IMETHODIMP
|
2014-03-11 14:46:04 +04:00
|
|
|
AudioChannelAgent::InitWithWeakCallback(nsIDOMWindow* aWindow,
|
|
|
|
int32_t aChannelType,
|
|
|
|
nsIAudioChannelAgentCallback *aCallback)
|
2013-04-04 00:35:05 +04:00
|
|
|
{
|
2014-03-11 14:46:04 +04:00
|
|
|
return InitInternal(aWindow, aChannelType, aCallback,
|
|
|
|
/* useWeakRef = */ true);
|
2013-04-04 00:35:05 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 14:46:04 +04:00
|
|
|
/* void initWithVideo(in nsIDOMWindow window, in long channelType,
|
|
|
|
* in nsIAudioChannelAgentCallback callback, in boolean weak); */
|
2013-09-18 07:46:22 +04:00
|
|
|
NS_IMETHODIMP
|
2014-03-11 14:46:04 +04:00
|
|
|
AudioChannelAgent::InitWithVideo(nsIDOMWindow* aWindow, int32_t aChannelType,
|
|
|
|
nsIAudioChannelAgentCallback *aCallback,
|
2013-09-18 07:46:22 +04:00
|
|
|
bool aUseWeakRef)
|
|
|
|
{
|
2014-03-11 14:46:04 +04:00
|
|
|
return InitInternal(aWindow, aChannelType, aCallback, aUseWeakRef,
|
|
|
|
/* withVideo = */ true);
|
2013-09-18 07:46:22 +04:00
|
|
|
}
|
|
|
|
|
2013-04-04 00:35:05 +04:00
|
|
|
nsresult
|
2014-03-11 14:46:04 +04:00
|
|
|
AudioChannelAgent::InitInternal(nsIDOMWindow* aWindow, int32_t aChannelType,
|
2013-04-04 00:35:05 +04:00
|
|
|
nsIAudioChannelAgentCallback *aCallback,
|
2013-09-18 07:46:22 +04:00
|
|
|
bool aUseWeakRef, bool aWithVideo)
|
2012-12-06 18:29:20 +04:00
|
|
|
{
|
2014-03-11 14:46:04 +04:00
|
|
|
// We need the window only for IPC.
|
|
|
|
MOZ_ASSERT(aWindow || XRE_GetProcessType() == GeckoProcessType_Default);
|
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
// We syncd the enum of channel type between nsIAudioChannelAgent.idl and
|
2014-04-10 21:39:20 +04:00
|
|
|
// AudioChannelBinding.h the same.
|
2014-04-26 06:34:04 +04: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 21:39:20 +04:00
|
|
|
"Enum of channel on nsIAudioChannelAgent.idl should be the same with AudioChannelBinding.h");
|
2012-12-06 18:29:20 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-11 14:46:04 +04:00
|
|
|
mWindow = aWindow;
|
2013-04-04 00:35:05 +04:00
|
|
|
mAudioChannelType = aChannelType;
|
|
|
|
|
|
|
|
if (aUseWeakRef) {
|
|
|
|
mWeakCallback = do_GetWeakReference(aCallback);
|
|
|
|
} else {
|
|
|
|
mCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2013-09-18 07:46:22 +04:00
|
|
|
mWithVideo = aWithVideo;
|
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean startPlaying (); */
|
2013-09-02 13:45:44 +04:00
|
|
|
NS_IMETHODIMP AudioChannelAgent::StartPlaying(int32_t *_retval)
|
2012-12-06 18:29:20 +04:00
|
|
|
{
|
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,
|
2014-04-10 21:39:20 +04:00
|
|
|
static_cast<AudioChannel>(mAudioChannelType), mWithVideo);
|
2013-09-02 13:45:44 +04:00
|
|
|
*_retval = service->GetState(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-09-02 13:45:44 +04:00
|
|
|
callback->CanPlayChanged(service->GetState(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-09-02 13:45:44 +04:00
|
|
|
callback->CanPlayChanged(service->GetState(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();
|
|
|
|
}
|
2014-03-11 14:46:55 +04: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;
|
|
|
|
}
|