2015-05-03 22:32:37 +03: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 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 "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;
|
|
|
|
|
2015-07-17 14:08:00 +03: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 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)
|
2015-07-09 17:40:08 +03:00
|
|
|
, mInnerWindowID(0)
|
2012-12-06 18:29:20 +04:00
|
|
|
, mIsRegToService(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelAgent::~AudioChannelAgent()
|
2015-07-17 14:08:00 +03:00
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelAgent::Shutdown()
|
2012-12-06 18:29:20 +04:00
|
|
|
{
|
2013-01-15 18:31:19 +04:00
|
|
|
if (mIsRegToService) {
|
2015-08-05 16:06:57 +03:00
|
|
|
NotifyStoppedPlaying(nsIAudioChannelAgent::AUDIO_AGENT_NOTIFY);
|
2013-01-15 18:31:19 +04:00
|
|
|
}
|
2012-12-06 18:29:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2015-07-10 19:38:44 +03:00
|
|
|
bool aUseWeakRef)
|
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) &&
|
2015-03-16 07:01:50 +03:00
|
|
|
int(AUDIO_AGENT_CHANNEL_SYSTEM) == int(AudioChannel::System) &&
|
2014-04-26 06:34:04 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-01 19:05:59 +03:00
|
|
|
if (NS_WARN_IF(!aWindow)) {
|
2015-10-01 11:23:14 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-09-01 19:05:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> pInnerWindow = do_QueryInterface(aWindow);
|
|
|
|
MOZ_ASSERT(pInnerWindow->IsInnerWindow());
|
|
|
|
mInnerWindowID = pInnerWindow->WindowID();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> topWindow;
|
|
|
|
aWindow->GetScriptableTop(getter_AddRefs(topWindow));
|
|
|
|
mWindow = do_QueryInterface(topWindow);
|
|
|
|
if (mWindow) {
|
|
|
|
mWindow = mWindow->GetOuterWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mWindow) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2015-04-08 11:24:04 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-05 16:06:57 +03:00
|
|
|
NS_IMETHODIMP AudioChannelAgent::NotifyStartedPlaying(uint32_t aNotifyPlayback,
|
|
|
|
float *aVolume,
|
2015-07-11 10:24:26 +03:00
|
|
|
bool* aMuted)
|
2012-12-06 18:29:20 +04:00
|
|
|
{
|
2015-07-10 19:38:44 +03:00
|
|
|
MOZ_ASSERT(aVolume);
|
|
|
|
MOZ_ASSERT(aMuted);
|
|
|
|
|
2015-09-01 19:05:59 +03:00
|
|
|
// Window-less AudioChannelAgents are muted by default.
|
|
|
|
if (!mWindow) {
|
|
|
|
*aVolume = 0;
|
|
|
|
*aMuted = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsRefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-05 16:06:57 +03:00
|
|
|
service->RegisterAudioChannelAgent(this, aNotifyPlayback,
|
2015-07-10 19:38:44 +03:00
|
|
|
static_cast<AudioChannel>(mAudioChannelType));
|
|
|
|
|
|
|
|
service->GetState(mWindow, mAudioChannelType, aVolume, aMuted);
|
|
|
|
|
2012-12-06 18:29:20 +04:00
|
|
|
mIsRegToService = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-05 16:06:57 +03:00
|
|
|
NS_IMETHODIMP AudioChannelAgent::NotifyStoppedPlaying(uint32_t aNotifyPlayback)
|
2012-12-06 18:29:20 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsRefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
2015-08-05 16:06:57 +03:00
|
|
|
service->UnregisterAudioChannelAgent(this, aNotifyPlayback);
|
2012-12-06 18:29:20 +04:00
|
|
|
mIsRegToService = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
float volume = 1.0;
|
|
|
|
bool muted = false;
|
2015-07-11 03:55:59 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsRefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
|
|
|
|
service->GetState(mWindow, mAudioChannelType, &volume, &muted);
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
callback->WindowVolumeChanged(volume, muted);
|
|
|
|
}
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
uint64_t
|
|
|
|
AudioChannelAgent::WindowID() const
|
|
|
|
{
|
|
|
|
return mWindow ? mWindow->WindowID() : 0;
|
2014-03-11 14:46:55 +04:00
|
|
|
}
|
2015-07-09 17:40:08 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelAgent::WindowAudioCaptureChanged(uint64_t aInnerWindowID)
|
|
|
|
{
|
|
|
|
if (aInnerWindowID != mInnerWindowID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
|
|
|
|
if (!callback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback->WindowAudioCaptureChanged();
|
|
|
|
}
|