Bug 1388236 - Remove B2G-only AudioChannel code from dom/audiochannel. r=alwu

This commit is contained in:
Matthew Gregan 2017-08-10 13:00:09 +12:00
parent fd14b55d10
commit 526a07e856
10 changed files with 37 additions and 180 deletions

View File

@ -37,8 +37,7 @@ NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioChannelAgent)
NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioChannelAgent)
AudioChannelAgent::AudioChannelAgent()
: mAudioChannelType(AUDIO_AGENT_CHANNEL_ERROR)
, mInnerWindowID(0)
: mInnerWindowID(0)
, mIsRegToService(false)
{
// Init service in the begining, it can help us to know whether there is any
@ -59,26 +58,19 @@ AudioChannelAgent::Shutdown()
}
}
NS_IMETHODIMP AudioChannelAgent::GetAudioChannelType(int32_t *aAudioChannelType)
{
*aAudioChannelType = mAudioChannelType;
return NS_OK;
}
NS_IMETHODIMP
AudioChannelAgent::Init(mozIDOMWindow* aWindow, int32_t aChannelType,
AudioChannelAgent::Init(mozIDOMWindow* aWindow,
nsIAudioChannelAgentCallback *aCallback)
{
return InitInternal(nsPIDOMWindowInner::From(aWindow), aChannelType,
return InitInternal(nsPIDOMWindowInner::From(aWindow),
aCallback, /* useWeakRef = */ false);
}
NS_IMETHODIMP
AudioChannelAgent::InitWithWeakCallback(mozIDOMWindow* aWindow,
int32_t aChannelType,
nsIAudioChannelAgentCallback *aCallback)
{
return InitInternal(nsPIDOMWindowInner::From(aWindow), aChannelType,
return InitInternal(nsPIDOMWindowInner::From(aWindow),
aCallback, /* useWeakRef = */ true);
}
@ -143,28 +135,9 @@ AudioChannelAgent::FindCorrectWindow(nsPIDOMWindowInner* aWindow)
nsresult
AudioChannelAgent::InitInternal(nsPIDOMWindowInner* aWindow,
int32_t aChannelType,
nsIAudioChannelAgentCallback *aCallback,
bool aUseWeakRef)
{
// We syncd the enum of channel type between nsIAudioChannelAgent.idl and
// AudioChannelBinding.h the same.
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_SYSTEM) == int(AudioChannel::System) &&
int(AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION) == int(AudioChannel::Publicnotification),
"Enum of channel on nsIAudioChannelAgent.idl should be the same with AudioChannelBinding.h");
if (mAudioChannelType != AUDIO_AGENT_CHANNEL_ERROR ||
aChannelType > AUDIO_AGENT_CHANNEL_SYSTEM ||
aChannelType < AUDIO_AGENT_CHANNEL_NORMAL) {
return NS_ERROR_FAILURE;
}
if (NS_WARN_IF(!aWindow)) {
return NS_ERROR_FAILURE;
}
@ -177,8 +150,6 @@ AudioChannelAgent::InitInternal(nsPIDOMWindowInner* aWindow,
return rv;
}
mAudioChannelType = aChannelType;
if (aUseWeakRef) {
mWeakCallback = do_GetWeakReference(aCallback);
} else {
@ -186,8 +157,8 @@ AudioChannelAgent::InitInternal(nsPIDOMWindowInner* aWindow,
}
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
("AudioChannelAgent, InitInternal, this = %p, type = %d, "
"owner = %p, hasCallback = %d\n", this, mAudioChannelType,
("AudioChannelAgent, InitInternal, this = %p, "
"owner = %p, hasCallback = %d\n", this,
mWindow.get(), (!!mCallback || !!mWeakCallback)));
return NS_OK;
@ -202,8 +173,7 @@ AudioChannelAgent::NotifyStartedPlaying(AudioPlaybackConfig* aConfig,
}
RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
service == nullptr || mIsRegToService) {
if (service == nullptr || mIsRegToService) {
return NS_ERROR_FAILURE;
}
@ -213,8 +183,7 @@ AudioChannelAgent::NotifyStartedPlaying(AudioPlaybackConfig* aConfig,
service->RegisterAudioChannelAgent(this,
static_cast<AudioChannelService::AudibleState>(aAudible));
AudioPlaybackConfig config = service->GetMediaConfig(mWindow,
mAudioChannelType);
AudioPlaybackConfig config = service->GetMediaConfig(mWindow);
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
("AudioChannelAgent, NotifyStartedPlaying, this = %p, "
@ -231,8 +200,7 @@ AudioChannelAgent::NotifyStartedPlaying(AudioPlaybackConfig* aConfig,
NS_IMETHODIMP
AudioChannelAgent::NotifyStoppedPlaying()
{
if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
!mIsRegToService) {
if (!mIsRegToService) {
return NS_ERROR_FAILURE;
}
@ -321,7 +289,7 @@ AudioChannelAgent::GetMediaConfig()
RefPtr<AudioChannelService> service = AudioChannelService::GetOrCreate();
AudioPlaybackConfig config(1.0, false, nsISuspendedTypes::NONE_SUSPENDED);
if (service) {
config = service->GetMediaConfig(mWindow, mAudioChannelType);
config = service->GetMediaConfig(mWindow);
}
return config;
}

View File

@ -61,7 +61,7 @@ private:
// nsIAudioChannelAgentCallback out of mWeakCallback.
already_AddRefed<nsIAudioChannelAgentCallback> GetCallback();
nsresult InitInternal(nsPIDOMWindowInner* aWindow, int32_t aAudioAgentType,
nsresult InitInternal(nsPIDOMWindowInner* aWindow,
nsIAudioChannelAgentCallback* aCallback,
bool aUseWeakRef);
@ -74,7 +74,6 @@ private:
nsWeakPtr mWeakCallback;
int32_t mAudioChannelType;
uint64_t mInnerWindowID;
bool mIsRegToService;
};

View File

@ -217,19 +217,6 @@ AudibleChangedReasonToStr(const AudioChannelService::AudibleChangedReasons& aRea
StaticRefPtr<AudioChannelService> gAudioChannelService;
// Mappings from 'mozaudiochannel' attribute strings to an enumeration.
static const nsAttrValue::EnumTable kMozAudioChannelAttributeTable[] = {
{ "normal", (int16_t)AudioChannel::Normal },
{ "content", (int16_t)AudioChannel::Content },
{ "notification", (int16_t)AudioChannel::Notification },
{ "alarm", (int16_t)AudioChannel::Alarm },
{ "telephony", (int16_t)AudioChannel::Telephony },
{ "ringer", (int16_t)AudioChannel::Ringer },
{ "publicnotification", (int16_t)AudioChannel::Publicnotification },
{ "system", (int16_t)AudioChannel::System },
{ nullptr, 0 }
};
/* static */ void
AudioChannelService::CreateServiceIfNeeded()
{
@ -356,11 +343,9 @@ AudioChannelService::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent)
}
AudioPlaybackConfig
AudioChannelService::GetMediaConfig(nsPIDOMWindowOuter* aWindow,
uint32_t aAudioChannel) const
AudioChannelService::GetMediaConfig(nsPIDOMWindowOuter* aWindow) const
{
MOZ_ASSERT(!aWindow || aWindow->IsOuterWindow());
MOZ_ASSERT(aAudioChannel < NUMBER_OF_AUDIO_CHANNELS);
AudioPlaybackConfig config(1.0, false,
nsISuspendedTypes::NONE_SUSPENDED);
@ -379,8 +364,8 @@ AudioChannelService::GetMediaConfig(nsPIDOMWindowOuter* aWindow,
do {
winData = GetWindowData(window->WindowID());
if (winData) {
config.mVolume *= winData->mChannels[aAudioChannel].mVolume;
config.mMuted = config.mMuted || winData->mChannels[aAudioChannel].mMuted;
config.mVolume *= winData->mChannelConfig.mVolume;
config.mMuted = config.mMuted || winData->mChannelConfig.mMuted;
config.mSuspend = winData->mOwningAudioFocus ?
config.mSuspend : nsISuspendedTypes::SUSPENDED_STOP_DISPOSABLE;
}
@ -542,42 +527,6 @@ AudioChannelService::SetWindowAudioCaptured(nsPIDOMWindowOuter* aWindow,
}
}
/* static */ const nsAttrValue::EnumTable*
AudioChannelService::GetAudioChannelTable()
{
return kMozAudioChannelAttributeTable;
}
/* static */ AudioChannel
AudioChannelService::GetAudioChannel(const nsAString& aChannel)
{
for (uint32_t i = 0; kMozAudioChannelAttributeTable[i].tag; ++i) {
if (aChannel.EqualsASCII(kMozAudioChannelAttributeTable[i].tag)) {
return static_cast<AudioChannel>(kMozAudioChannelAttributeTable[i].value);
}
}
return AudioChannel::Normal;
}
/* static */ AudioChannel
AudioChannelService::GetDefaultAudioChannel()
{
nsAutoString audioChannel;
Preferences::GetString("media.defaultAudioChannel", audioChannel);
if (audioChannel.IsEmpty()) {
return AudioChannel::Normal;
}
for (uint32_t i = 0; kMozAudioChannelAttributeTable[i].tag; ++i) {
if (audioChannel.EqualsASCII(kMozAudioChannelAttributeTable[i].tag)) {
return static_cast<AudioChannel>(kMozAudioChannelAttributeTable[i].value);
}
}
return AudioChannel::Normal;
}
AudioChannelService::AudioChannelWindow*
AudioChannelService::GetOrCreateWindowData(nsPIDOMWindowOuter* aWindow)
{
@ -769,8 +718,7 @@ AudioChannelService::AudioChannelWindow::AudioFocusChanged(AudioChannelAgent* aN
continue;
}
uint32_t type = GetCompetingBehavior(agent,
aNewPlayingAgent->AudioChannelType());
uint32_t type = GetCompetingBehavior(agent);
// If window will be suspended, it needs to abandon the audio focus
// because only one window can own audio focus at a time. However, we
@ -799,27 +747,18 @@ AudioChannelService::AudioChannelWindow::IsContainingPlayingAgent(AudioChannelAg
}
uint32_t
AudioChannelService::AudioChannelWindow::GetCompetingBehavior(AudioChannelAgent* aAgent,
int32_t aIncomingChannelType) const
AudioChannelService::AudioChannelWindow::GetCompetingBehavior(AudioChannelAgent* aAgent) const
{
MOZ_ASSERT(aAgent);
MOZ_ASSERT(IsEnableAudioCompetingForAllAgents() ?
mAgents.Contains(aAgent) : mAudibleAgents.Contains(aAgent));
uint32_t competingBehavior = nsISuspendedTypes::NONE_SUSPENDED;
int32_t presentChannelType = aAgent->AudioChannelType();
// TODO : add other competing cases for MediaSession API
if (presentChannelType == int32_t(AudioChannel::Normal) &&
aIncomingChannelType == int32_t(AudioChannel::Normal)) {
competingBehavior = nsISuspendedTypes::SUSPENDED_STOP_DISPOSABLE;
}
uint32_t competingBehavior = nsISuspendedTypes::SUSPENDED_STOP_DISPOSABLE;
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,
("AudioChannelWindow, GetCompetingBehavior, this = %p, "
"present type = %d, incoming channel = %d, behavior = %s\n",
this, presentChannelType, aIncomingChannelType,
SuspendTypeToStr(competingBehavior)));
"behavior = %s\n",
this, SuspendTypeToStr(competingBehavior)));
return competingBehavior;
}
@ -900,13 +839,12 @@ AudioChannelService::AudioChannelWindow::AppendAgentAndIncreaseAgentsNum(AudioCh
MOZ_ASSERT(aAgent);
MOZ_ASSERT(!mAgents.Contains(aAgent));
int32_t channel = aAgent->AudioChannelType();
mAgents.AppendElement(aAgent);
++mChannels[channel].mNumberOfAgents;
++mChannelConfig.mNumberOfAgents;
// TODO: Make NotifyChannelActiveRunnable irrelevant to BrowserElementAudioChannel
if (mChannels[channel].mNumberOfAgents == 1) {
if (mChannelConfig.mNumberOfAgents == 1) {
NotifyChannelActive(aAgent->WindowID(), true);
}
}
@ -917,13 +855,12 @@ AudioChannelService::AudioChannelWindow::RemoveAgentAndReduceAgentsNum(AudioChan
MOZ_ASSERT(aAgent);
MOZ_ASSERT(mAgents.Contains(aAgent));
int32_t channel = aAgent->AudioChannelType();
mAgents.RemoveElement(aAgent);
MOZ_ASSERT(mChannels[channel].mNumberOfAgents > 0);
--mChannels[channel].mNumberOfAgents;
MOZ_ASSERT(mChannelConfig.mNumberOfAgents > 0);
--mChannelConfig.mNumberOfAgents;
if (mChannels[channel].mNumberOfAgents == 0) {
if (mChannelConfig.mNumberOfAgents == 0) {
NotifyChannelActive(aAgent->WindowID(), false);
}
}

View File

@ -14,7 +14,6 @@
#include "AudioChannelAgent.h"
#include "nsAttrValue.h"
#include "mozilla/dom/AudioChannelBinding.h"
#include "mozilla/Logging.h"
#include <functional>
@ -25,8 +24,6 @@ struct PRLogModuleInfo;
namespace mozilla {
namespace dom {
#define NUMBER_OF_AUDIO_CHANNELS (uint32_t)AudioChannel::EndGuard_
class AudioPlaybackConfig
{
public:
@ -116,8 +113,7 @@ public:
* Return the state to indicate this audioChannel for his window should keep
* playing/muted/suspended.
*/
AudioPlaybackConfig GetMediaConfig(nsPIDOMWindowOuter* aWindow,
uint32_t aAudioChannel) const;
AudioPlaybackConfig GetMediaConfig(nsPIDOMWindowOuter* aWindow) const;
/**
* Called this method when the audible state of the audio playback changed,
@ -142,10 +138,6 @@ public:
uint64_t aInnerWindowID,
bool aCapture);
static const nsAttrValue::EnumTable* GetAudioChannelTable();
static AudioChannel GetAudioChannel(const nsAString& aString);
static AudioChannel GetDefaultAudioChannel();
void NotifyMediaResumedFromBlock(nsPIDOMWindowOuter* aWindow);
private:
@ -197,7 +189,7 @@ private:
uint64_t mWindowID;
bool mIsAudioCaptured;
AudioChannelConfig mChannels[NUMBER_OF_AUDIO_CHANNELS];
AudioChannelConfig mChannelConfig;
// Raw pointer because the AudioChannelAgent must unregister itself.
nsTObserverArray<AudioChannelAgent*> mAgents;
@ -238,8 +230,7 @@ private:
// We need to do audio competing only when the new incoming agent started.
void NotifyAudioCompetingChanged(AudioChannelAgent* aAgent);
uint32_t GetCompetingBehavior(AudioChannelAgent* aAgent,
int32_t aIncomingChannelType) const;
uint32_t GetCompetingBehavior(AudioChannelAgent* aAgent) const;
bool IsAgentInvolvingInAudioCompeting(AudioChannelAgent* aAgent) const;
bool IsAudioCompetingInSameTab() const;
bool IsContainingPlayingAgent(AudioChannelAgent* aAgent) const;

View File

@ -87,53 +87,26 @@ interface nsIAudioChannelAgentCallback : nsISupports
/**
* This interface provides an agent for gecko components to participate
* in the audio channel service. Gecko components are responsible for
* 1. Indicating what channel type they are using (via the init() member
* function).
* 2. Notifying the agent when they start/stop using this channel.
* 3. Notifying the agent when they are audible.
* 1. Notifying the agent when they start/stop using this channel.
* 2. Notifying the agent when they are audible.
*
* The agent will invoke a callback to notify Gecko components of
* 1. Changes to the playable status of this channel.
*/
[uuid(ab7e21c0-970c-11e5-a837-0800200c9a66)]
[uuid(4d212770-5d7b-446f-9394-632e351d96ee)]
interface nsIAudioChannelAgent : nsISupports
{
const long AUDIO_AGENT_CHANNEL_NORMAL = 0;
const long AUDIO_AGENT_CHANNEL_CONTENT = 1;
const long AUDIO_AGENT_CHANNEL_NOTIFICATION = 2;
const long AUDIO_AGENT_CHANNEL_ALARM = 3;
const long AUDIO_AGENT_CHANNEL_TELEPHONY = 4;
const long AUDIO_AGENT_CHANNEL_RINGER = 5;
const long AUDIO_AGENT_CHANNEL_PUBLICNOTIFICATION = 6;
const long AUDIO_AGENT_CHANNEL_SYSTEM = 7;
const long AUDIO_AGENT_CHANNEL_ERROR = 1000;
const long AUDIO_AGENT_STATE_NORMAL = 0;
const long AUDIO_AGENT_STATE_MUTED = 1;
const long AUDIO_AGENT_STATE_FADED = 2;
/**
* Before init() is called, this returns AUDIO_AGENT_CHANNEL_ERROR.
*/
readonly attribute long audioChannelType;
%{C++
inline int32_t AudioChannelType() {
int32_t channel;
return NS_SUCCEEDED(GetAudioChannelType(&channel)) ? channel : AUDIO_AGENT_CHANNEL_ERROR;
}
%}
/**
* Initialize the agent with a channel type.
* Note: This function should only be called once.
*
* @param window
* The window
* @param channelType
* Audio Channel Type listed as above
* @param callback
* 1. Once the playable status changes, agent uses this callback function
* to notify Gecko component.
@ -142,8 +115,7 @@ interface nsIAudioChannelAgent : nsISupports
* 3. The AudioChannelAgent keeps a strong reference to the callback
* object.
*/
void init(in mozIDOMWindow window, in long channelType,
in nsIAudioChannelAgentCallback callback);
void init(in mozIDOMWindow window, in nsIAudioChannelAgentCallback callback);
/**
* This method is just like init(), except the audio channel agent keeps a
@ -152,7 +124,7 @@ interface nsIAudioChannelAgent : nsISupports
* In order for this to work, |callback| must implement
* nsISupportsWeakReference.
*/
void initWithWeakCallback(in mozIDOMWindow window, in long channelType,
void initWithWeakCallback(in mozIDOMWindow window,
in nsIAudioChannelAgentCallback callback);
/**

View File

@ -867,9 +867,7 @@ private:
}
mAudioChannelAgent = new AudioChannelAgent();
nsresult rv = mAudioChannelAgent->Init(mOwner->OwnerDoc()->GetInnerWindow(),
static_cast<int32_t>(AudioChannelService::GetDefaultAudioChannel()),
this);
nsresult rv = mAudioChannelAgent->Init(mOwner->OwnerDoc()->GetInnerWindow(), this);
if (NS_WARN_IF(NS_FAILED(rv))) {
mAudioChannelAgent = nullptr;
MOZ_LOG(AudioChannelService::GetAudioChannelLog(), LogLevel::Debug,

View File

@ -606,9 +606,7 @@ AudioDestinationNode::CreateAudioChannelAgent()
}
mAudioChannelAgent = new AudioChannelAgent();
rv = mAudioChannelAgent->InitWithWeakCallback(GetOwner(),
static_cast<int32_t>(AudioChannel::Normal),
this);
rv = mAudioChannelAgent->InitWithWeakCallback(GetOwner(), this);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

View File

@ -738,9 +738,7 @@ nsSpeechTask::CreateAudioChannelAgent()
}
mAudioChannelAgent = new AudioChannelAgent();
mAudioChannelAgent->InitWithWeakCallback(mUtterance->GetOwner(),
static_cast<int32_t>(AudioChannelService::GetDefaultAudioChannel()),
this);
mAudioChannelAgent->InitWithWeakCallback(mUtterance->GetOwner(), this);
AudioPlaybackConfig config;
nsresult rv = mAudioChannelAgent->NotifyStartedPlaying(&config,

View File

@ -665,9 +665,7 @@ nsSynthVoiceRegistry::SpeakUtterance(SpeechSynthesisUtterance& aUtterance,
if (service) {
if (nsCOMPtr<nsPIDOMWindowInner> topWindow = aUtterance.GetOwner()) {
// TODO : use audio channel agent, open new bug to fix it.
uint32_t channel = static_cast<uint32_t>(AudioChannelService::GetDefaultAudioChannel());
AudioPlaybackConfig config = service->GetMediaConfig(topWindow->GetOuterWindow(),
channel);
AudioPlaybackConfig config = service->GetMediaConfig(topWindow->GetOuterWindow());
volume = config.mMuted ? 0.0f : config.mVolume * volume;
}
}

View File

@ -1263,9 +1263,7 @@ nsNPAPIPluginInstance::CreateAudioChannelAgentIfNeeded()
return NS_ERROR_FAILURE;
}
rv = mAudioChannelAgent->Init(window->GetCurrentInnerWindow(),
(int32_t)AudioChannelService::GetDefaultAudioChannel(),
this);
rv = mAudioChannelAgent->Init(window->GetCurrentInnerWindow(), this);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}