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-04 23:46:07 +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 "AudioChannelService.h"
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
|
2014-12-29 13:56:00 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-07-10 19:38:44 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-10-17 19:29:39 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "nsHashPropertyBag.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2014-03-11 14:46:55 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-09-16 21:15:16 +04:00
|
|
|
#include "mozilla/dom/SettingChangeNotificationBinding.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2012-12-06 14:11:06 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2013-07-18 09:22:36 +04:00
|
|
|
#include "nsJSUtils.h"
|
2012-12-06 14:11:06 +04:00
|
|
|
#include "nsIAudioManager.h"
|
2013-11-25 03:50:03 +04:00
|
|
|
#include "SpeakerManagerService.h"
|
2013-07-18 09:22:36 +04:00
|
|
|
#define NS_AUDIOMANAGER_CONTRACTID "@mozilla.org/telephony/audiomanager;1"
|
2012-12-06 14:11:06 +04:00
|
|
|
#endif
|
2013-07-18 09:22:36 +04:00
|
|
|
|
2014-03-20 14:45:55 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2013-02-15 00:41:29 +04:00
|
|
|
using namespace mozilla::hal;
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
namespace {
|
|
|
|
|
2015-07-10 19:38:49 +03:00
|
|
|
// If true, any new AudioChannelAgent will be muted when created.
|
|
|
|
bool sAudioChannelMutedByDefault = false;
|
|
|
|
|
2015-07-10 19:38:53 +03:00
|
|
|
class NotifyChannelActiveRunnable final : public nsRunnable
|
2015-04-08 11:24:04 +03:00
|
|
|
{
|
2015-07-10 19:38:53 +03:00
|
|
|
public:
|
|
|
|
NotifyChannelActiveRunnable(uint64_t aWindowID, AudioChannel aAudioChannel,
|
|
|
|
bool aActive)
|
|
|
|
: mWindowID(aWindowID)
|
|
|
|
, mAudioChannel(aAudioChannel)
|
|
|
|
, mActive(aActive)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
services::GetObserverService();
|
|
|
|
if (NS_WARN_IF(!observerService)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:53 +03:00
|
|
|
nsCOMPtr<nsISupportsPRUint64> wrapper =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_PRUINT64_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!wrapper)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapper->SetData(mWindowID);
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:53 +03:00
|
|
|
nsAutoString name;
|
|
|
|
AudioChannelService::GetAudioChannelString(mAudioChannel, name);
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:53 +03:00
|
|
|
nsAutoCString topic;
|
|
|
|
topic.Assign("audiochannel-activity-");
|
|
|
|
topic.Append(NS_ConvertUTF16toUTF8(name));
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:53 +03:00
|
|
|
observerService->NotifyObservers(wrapper, topic.get(),
|
|
|
|
mActive
|
|
|
|
? MOZ_UTF16("active")
|
|
|
|
: MOZ_UTF16("inactive"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const uint64_t mWindowID;
|
|
|
|
const AudioChannel mAudioChannel;
|
|
|
|
const bool mActive;
|
|
|
|
};
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:53 +03:00
|
|
|
void
|
|
|
|
NotifyChannelActive(uint64_t aWindowID, AudioChannel aAudioChannel,
|
|
|
|
bool aActive)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsRunnable> runnable =
|
|
|
|
new NotifyChannelActiveRunnable(aWindowID, aAudioChannel, aActive);
|
|
|
|
NS_DispatchToCurrentThread(runnable);
|
2015-07-10 19:38:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsPIDOMWindow>
|
|
|
|
GetTopWindow(nsIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> topWindow;
|
|
|
|
aWindow->GetScriptableTop(getter_AddRefs(topWindow));
|
|
|
|
MOZ_ASSERT(topWindow);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(topWindow);
|
|
|
|
window = window->GetOuterWindow();
|
|
|
|
|
|
|
|
return window.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
bool
|
|
|
|
IsParentProcess()
|
|
|
|
{
|
|
|
|
return XRE_GetProcessType() == GeckoProcessType_Default;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:53 +03:00
|
|
|
class MediaPlaybackRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MediaPlaybackRunnable(nsIDOMWindow* aWindow, bool aActive)
|
|
|
|
: mWindow(aWindow)
|
|
|
|
, mActive(aActive)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->NotifyObservers(
|
|
|
|
ToSupports(mWindow),
|
|
|
|
"media-playback",
|
|
|
|
mActive ? NS_LITERAL_STRING("active").get()
|
|
|
|
: NS_LITERAL_STRING("inactive").get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDOMWindow> mWindow;
|
|
|
|
bool mActive;
|
|
|
|
};
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
} // anonymous namespace
|
2015-07-10 19:38:53 +03:00
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
StaticRefPtr<AudioChannelService> gAudioChannelService;
|
|
|
|
|
2014-04-10 21:39:20 +04:00
|
|
|
// 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 },
|
|
|
|
{ nullptr }
|
|
|
|
};
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
/* static */ already_AddRefed<AudioChannelService>
|
|
|
|
AudioChannelService::GetOrCreate()
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
if (!gAudioChannelService) {
|
|
|
|
gAudioChannelService = new AudioChannelService();
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsRefPtr<AudioChannelService> service = gAudioChannelService.get();
|
|
|
|
return service.forget();
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelService::Shutdown()
|
|
|
|
{
|
2015-07-11 15:14:58 +03:00
|
|
|
if (gAudioChannelService) {
|
2015-07-10 19:38:46 +03:00
|
|
|
if (IsParentProcess()) {
|
2015-07-10 19:38:44 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(gAudioChannelService, "ipc:content-shutdown");
|
|
|
|
obs->RemoveObserver(gAudioChannelService, "xpcom-shutdown");
|
2015-07-15 13:17:52 +03:00
|
|
|
obs->RemoveObserver(gAudioChannelService, "outer-window-destroyed");
|
2015-07-10 19:38:44 +03:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// To monitor the volume settings based on audio channel.
|
|
|
|
obs->RemoveObserver(gAudioChannelService, "mozsettings-changed");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
gAudioChannelService = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(AudioChannelService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAudioChannelService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAudioChannelService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(AudioChannelService)
|
|
|
|
NS_IMPL_RELEASE(AudioChannelService)
|
2012-12-04 23:46:07 +04:00
|
|
|
|
|
|
|
AudioChannelService::AudioChannelService()
|
2015-07-15 03:16:21 +03:00
|
|
|
: mDefChannelChildID(CONTENT_PROCESS_ID_UNKNOWN)
|
2015-07-10 19:38:46 +03:00
|
|
|
, mTelephonyChannel(false)
|
|
|
|
, mContentOrNormalChannel(false)
|
|
|
|
, mAnyChannel(false)
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
2015-07-10 19:38:46 +03:00
|
|
|
if (IsParentProcess()) {
|
2012-12-28 21:57:35 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, "ipc:content-shutdown", false);
|
2013-10-08 10:30:04 +04:00
|
|
|
obs->AddObserver(this, "xpcom-shutdown", false);
|
2015-07-15 13:17:52 +03:00
|
|
|
obs->AddObserver(this, "outer-window-destroyed", false);
|
2013-07-18 09:22:36 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// To monitor the volume settings based on audio channel.
|
|
|
|
obs->AddObserver(this, "mozsettings-changed", false);
|
|
|
|
#endif
|
2012-12-28 21:57:35 +04:00
|
|
|
}
|
|
|
|
}
|
2015-07-10 19:38:49 +03:00
|
|
|
|
|
|
|
Preferences::AddBoolVarCache(&sAudioChannelMutedByDefault,
|
|
|
|
"dom.audiochannel.mutedByDefault");
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelService::~AudioChannelService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-06 19:25:18 +04:00
|
|
|
AudioChannelService::RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannel aChannel)
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
2015-07-10 19:38:44 +03:00
|
|
|
uint64_t windowID = aAgent->WindowID();
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetWindowData(windowID);
|
|
|
|
if (!winData) {
|
|
|
|
winData = new AudioChannelWindow(windowID);
|
|
|
|
mWindows.AppendElement(winData);
|
|
|
|
}
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
MOZ_ASSERT(!winData->mAgents.Contains(aAgent));
|
|
|
|
winData->mAgents.AppendElement(aAgent);
|
2015-07-10 19:38:44 +03:00
|
|
|
|
|
|
|
++winData->mChannels[(uint32_t)aChannel].mNumberOfAgents;
|
|
|
|
|
|
|
|
// The first one, we must inform the BrowserElementAudioChannel.
|
|
|
|
if (winData->mChannels[(uint32_t)aChannel].mNumberOfAgents == 1) {
|
|
|
|
NotifyChannelActive(aAgent->WindowID(), aChannel, true);
|
|
|
|
}
|
2015-07-11 03:55:59 +03:00
|
|
|
|
|
|
|
// If this is the first agent for this window, we must notify the observers.
|
2015-07-10 19:38:51 +03:00
|
|
|
if (winData->mAgents.Length() == 1) {
|
2015-07-10 19:38:53 +03:00
|
|
|
nsRefPtr<MediaPlaybackRunnable> runnable =
|
|
|
|
new MediaPlaybackRunnable(aAgent->Window(), true /* active */);
|
|
|
|
NS_DispatchToCurrentThread(runnable);
|
2015-07-10 19:38:51 +03:00
|
|
|
}
|
2015-07-10 19:38:46 +03:00
|
|
|
|
|
|
|
MaybeSendStatusUpdate();
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-11 03:55:59 +03:00
|
|
|
void
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelService::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent)
|
2015-07-11 03:55:59 +03:00
|
|
|
{
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetWindowData(aAgent->WindowID());
|
|
|
|
if (!winData) {
|
2015-07-11 15:14:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
if (winData->mAgents.Contains(aAgent)) {
|
|
|
|
int32_t channel = aAgent->AudioChannelType();
|
|
|
|
uint64_t windowID = aAgent->WindowID();
|
|
|
|
|
|
|
|
// aAgent can be freed after this call.
|
|
|
|
winData->mAgents.RemoveElement(aAgent);
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
MOZ_ASSERT(winData->mChannels[channel].mNumberOfAgents > 0);
|
|
|
|
|
|
|
|
--winData->mChannels[channel].mNumberOfAgents;
|
2015-07-10 19:38:44 +03:00
|
|
|
|
|
|
|
// The last one, we must inform the BrowserElementAudioChannel.
|
2015-07-10 19:38:51 +03:00
|
|
|
if (winData->mChannels[channel].mNumberOfAgents == 0) {
|
|
|
|
NotifyChannelActive(windowID, static_cast<AudioChannel>(channel), false);
|
2015-07-10 19:38:44 +03:00
|
|
|
}
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
bool active = AnyAudioChannelIsActive();
|
|
|
|
for (uint32_t i = 0; i < mSpeakerManager.Length(); i++) {
|
|
|
|
mSpeakerManager[i]->SetAudioChannelActive(active);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If this is the last agent for this window, we must notify the observers.
|
2015-07-10 19:38:51 +03:00
|
|
|
if (winData->mAgents.IsEmpty()) {
|
2015-07-10 19:38:53 +03:00
|
|
|
nsRefPtr<MediaPlaybackRunnable> runnable =
|
|
|
|
new MediaPlaybackRunnable(aAgent->Window(), false /* active */);
|
|
|
|
NS_DispatchToCurrentThread(runnable);
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
2015-07-10 19:38:46 +03:00
|
|
|
|
|
|
|
MaybeSendStatusUpdate();
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelService::GetState(nsPIDOMWindow* aWindow, uint32_t aAudioChannel,
|
|
|
|
float* aVolume, bool* aMuted)
|
2015-07-11 15:14:58 +03:00
|
|
|
{
|
2015-07-10 19:38:44 +03:00
|
|
|
MOZ_ASSERT(!aWindow || aWindow->IsOuterWindow());
|
|
|
|
MOZ_ASSERT(aVolume && aMuted);
|
|
|
|
MOZ_ASSERT(aAudioChannel < NUMBER_OF_AUDIO_CHANNELS);
|
2015-07-11 03:55:59 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
*aVolume = 1.0;
|
|
|
|
*aMuted = false;
|
2015-07-11 03:55:59 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
if (!aWindow || !aWindow->IsOuterWindow()) {
|
|
|
|
return;
|
2015-07-11 15:14:58 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelWindow* winData = nullptr;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = aWindow;
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
// The volume must be calculated based on the window hierarchy. Here we go up
|
|
|
|
// to the top window and we calculate the volume and the muted flag.
|
|
|
|
do {
|
2015-07-10 19:38:51 +03:00
|
|
|
winData = GetWindowData(window->WindowID());
|
|
|
|
if (winData) {
|
2015-07-10 19:38:44 +03:00
|
|
|
*aVolume *= winData->mChannels[aAudioChannel].mVolume;
|
|
|
|
*aMuted = *aMuted || winData->mChannels[aAudioChannel].mMuted;
|
2015-07-11 15:14:58 +03:00
|
|
|
}
|
2015-07-11 03:55:59 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
*aVolume *= window->GetAudioVolume();
|
|
|
|
*aMuted = *aMuted || window->GetAudioMuted();
|
2015-07-11 03:55:59 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsCOMPtr<nsIDOMWindow> win;
|
|
|
|
window->GetScriptableParent(getter_AddRefs(win));
|
|
|
|
if (window == win) {
|
2015-07-11 03:55:59 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
window = do_QueryInterface(win);
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
// If there is no parent, or we are the toplevel we don't continue.
|
|
|
|
} while (window && window != aWindow);
|
2015-07-11 15:14:58 +03:00
|
|
|
}
|
|
|
|
|
2015-07-11 03:55:59 +03:00
|
|
|
bool
|
|
|
|
AudioChannelService::TelephonyChannelIsActive()
|
|
|
|
{
|
2015-07-10 19:38:51 +03:00
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelWindow>>::ForwardIterator iter(mWindows);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
AudioChannelWindow* next = iter.GetNext();
|
|
|
|
if (next->mChannels[(uint32_t)AudioChannel::Telephony].mNumberOfAgents != 0 &&
|
|
|
|
!next->mChannels[(uint32_t)AudioChannel::Telephony].mMuted) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-10 19:38:46 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
if (IsParentProcess()) {
|
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelChildStatus>>::ForwardIterator
|
|
|
|
iter(mPlayingChildren);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
AudioChannelChildStatus* child = iter.GetNext();
|
|
|
|
if (child->mActiveTelephonyChannel) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 19:38:46 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
return false;
|
2015-07-10 19:38:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioChannelService::ContentOrNormalChannelIsActive()
|
|
|
|
{
|
|
|
|
// This method is meant to be used just by the child to send status update.
|
|
|
|
MOZ_ASSERT(!IsParentProcess());
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelWindow>>::ForwardIterator iter(mWindows);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
AudioChannelWindow* next = iter.GetNext();
|
|
|
|
if (next->mChannels[(uint32_t)AudioChannel::Content].mNumberOfAgents > 0 ||
|
|
|
|
next->mChannels[(uint32_t)AudioChannel::Normal].mNumberOfAgents > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelService::AudioChannelChildStatus*
|
|
|
|
AudioChannelService::GetChildStatus(uint64_t aChildID) const
|
2015-07-11 03:55:59 +03:00
|
|
|
{
|
2015-07-10 19:38:51 +03:00
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelChildStatus>>::ForwardIterator
|
|
|
|
iter(mPlayingChildren);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
AudioChannelChildStatus* child = iter.GetNext();
|
|
|
|
if (child->mChildID == aChildID) {
|
|
|
|
return child;
|
|
|
|
}
|
2015-07-11 15:14:58 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
return nullptr;
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
void
|
|
|
|
AudioChannelService::RemoveChildStatus(uint64_t aChildID)
|
|
|
|
{
|
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelChildStatus>>::ForwardIterator
|
|
|
|
iter(mPlayingChildren);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
nsAutoPtr<AudioChannelChildStatus>& child = iter.GetNext();
|
|
|
|
if (child->mChildID == aChildID) {
|
|
|
|
mPlayingChildren.RemoveElement(child);
|
2015-07-11 03:55:59 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 19:38:51 +03:00
|
|
|
}
|
2015-07-11 03:55:59 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
bool
|
|
|
|
AudioChannelService::ProcessContentOrNormalChannelIsActive(uint64_t aChildID)
|
|
|
|
{
|
|
|
|
AudioChannelChildStatus* child = GetChildStatus(aChildID);
|
|
|
|
if (!child) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return child->mActiveContentOrNormalChannel;
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 11:20:42 +04:00
|
|
|
bool
|
2015-07-11 03:55:59 +03:00
|
|
|
AudioChannelService::AnyAudioChannelIsActive()
|
2014-05-29 11:20:42 +04:00
|
|
|
{
|
2015-07-10 19:38:51 +03:00
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelWindow>>::ForwardIterator iter(mWindows);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
AudioChannelWindow* next = iter.GetNext();
|
|
|
|
for (uint32_t i = 0; kMozAudioChannelAttributeTable[i].tag; ++i) {
|
|
|
|
if (next->mChannels[kMozAudioChannelAttributeTable[i].value].mNumberOfAgents
|
|
|
|
!= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 19:38:46 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
if (IsParentProcess()) {
|
|
|
|
return !mPlayingChildren.IsEmpty();
|
2015-07-10 19:38:46 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
return false;
|
2014-10-17 19:29:39 +04:00
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:35 +04:00
|
|
|
NS_IMETHODIMP
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelService::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData)
|
2012-12-28 21:57:35 +04:00
|
|
|
{
|
2013-10-08 10:30:04 +04:00
|
|
|
if (!strcmp(aTopic, "xpcom-shutdown")) {
|
2015-07-10 19:38:44 +03:00
|
|
|
mWindows.Clear();
|
2015-07-15 03:16:21 +03:00
|
|
|
Shutdown();
|
2013-07-18 09:22:36 +04:00
|
|
|
}
|
2014-10-17 19:29:39 +04:00
|
|
|
|
2013-07-18 09:22:36 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// To process the volume control on each audio channel according to
|
|
|
|
// change of settings
|
|
|
|
else if (!strcmp(aTopic, "mozsettings-changed")) {
|
2014-12-29 13:56:00 +03:00
|
|
|
RootedDictionary<SettingChangeNotification> setting(nsContentUtils::RootingCxForThread());
|
|
|
|
if (!WrappedJSToDictionary(aSubject, setting)) {
|
2013-07-18 09:22:36 +04:00
|
|
|
return NS_OK;
|
2012-12-28 21:57:35 +04:00
|
|
|
}
|
2014-09-16 21:15:16 +04:00
|
|
|
if (!StringBeginsWith(setting.mKey, NS_LITERAL_STRING("audio.volume."))) {
|
2013-07-18 09:22:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-16 21:15:16 +04:00
|
|
|
if (!setting.mValue.isNumber()) {
|
2013-07-18 09:22:36 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-02-04 05:10:22 +03:00
|
|
|
|
2013-07-18 09:22:36 +04:00
|
|
|
nsCOMPtr<nsIAudioManager> audioManager = do_GetService(NS_AUDIOMANAGER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(audioManager, NS_OK);
|
|
|
|
|
2014-09-16 21:15:16 +04:00
|
|
|
int32_t index = setting.mValue.toNumber();
|
|
|
|
if (setting.mKey.EqualsLiteral("audio.volume.content")) {
|
2014-04-10 21:39:20 +04:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Content, index);
|
2014-09-16 21:15:16 +04:00
|
|
|
} else if (setting.mKey.EqualsLiteral("audio.volume.notification")) {
|
2014-04-10 21:39:20 +04:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Notification, index);
|
2014-09-16 21:15:16 +04:00
|
|
|
} else if (setting.mKey.EqualsLiteral("audio.volume.alarm")) {
|
2014-04-10 21:39:20 +04:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Alarm, index);
|
2014-09-16 21:15:16 +04:00
|
|
|
} else if (setting.mKey.EqualsLiteral("audio.volume.telephony")) {
|
2014-04-10 21:39:20 +04:00
|
|
|
audioManager->SetAudioChannelVolume((int32_t)AudioChannel::Telephony, index);
|
2014-09-16 21:15:16 +04:00
|
|
|
} else if (!setting.mKey.EqualsLiteral("audio.volume.bt_sco")) {
|
2013-10-14 14:45:35 +04:00
|
|
|
// bt_sco is not a valid audio channel so we manipulate it in
|
|
|
|
// AudioManager.cpp. And the others should not be used.
|
2014-04-20 11:36:40 +04:00
|
|
|
// We didn't use MOZ_CRASH or MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE here
|
|
|
|
// because any web content who has permission of mozSettings can set any
|
|
|
|
// names then it can be easy to crash the B2G.
|
2013-10-14 14:45:35 +04:00
|
|
|
NS_WARNING("unexpected audio channel for volume control");
|
2013-07-18 09:22:36 +04:00
|
|
|
}
|
2012-12-28 21:57:35 +04:00
|
|
|
}
|
2013-07-18 09:22:36 +04:00
|
|
|
#endif
|
2012-12-28 21:57:35 +04:00
|
|
|
|
2015-07-15 13:17:52 +03:00
|
|
|
else if (!strcmp(aTopic, "outer-window-destroyed")) {
|
2014-10-17 19:29:39 +04:00
|
|
|
nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
|
|
|
|
|
2015-07-15 13:17:52 +03:00
|
|
|
uint64_t outerID;
|
|
|
|
nsresult rv = wrapper->GetData(&outerID);
|
2014-10-17 19:29:39 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
nsAutoPtr<AudioChannelWindow> winData;
|
|
|
|
{
|
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelWindow>>::ForwardIterator
|
|
|
|
iter(mWindows);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
nsAutoPtr<AudioChannelWindow>& next = iter.GetNext();
|
2015-07-15 13:17:52 +03:00
|
|
|
if (next->mWindowID == outerID) {
|
2015-07-10 19:38:51 +03:00
|
|
|
uint32_t pos = mWindows.IndexOf(next);
|
|
|
|
winData = next.forget();
|
|
|
|
mWindows.RemoveElementAt(pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (winData) {
|
|
|
|
nsTObserverArray<AudioChannelAgent*>::ForwardIterator
|
|
|
|
iter(winData->mAgents);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
iter.GetNext()->WindowVolumeChanged();
|
|
|
|
}
|
2015-04-08 11:24:04 +03:00
|
|
|
}
|
2014-10-17 19:29:39 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
bool active = AnyAudioChannelIsActive();
|
|
|
|
for (uint32_t i = 0; i < mSpeakerManager.Length(); i++) {
|
|
|
|
mSpeakerManager[i]->SetAudioChannelActive(active);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
else if (!strcmp(aTopic, "ipc:content-shutdown")) {
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
|
|
|
|
if (!props) {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without property bag as subject");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
uint64_t childID = 0;
|
|
|
|
nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"),
|
|
|
|
&childID);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDefChannelChildID == childID) {
|
|
|
|
SetDefaultVolumeControlChannelInternal(-1, false, childID);
|
|
|
|
mDefChannelChildID = CONTENT_PROCESS_ID_UNKNOWN;
|
|
|
|
}
|
2015-07-10 19:38:46 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
RemoveChildStatus(childID);
|
2013-01-09 11:18:16 +04:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
return NS_OK;
|
2013-01-09 11:18:16 +04:00
|
|
|
}
|
2014-03-11 14:46:55 +04:00
|
|
|
|
|
|
|
struct RefreshAgentsVolumeData
|
|
|
|
{
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit RefreshAgentsVolumeData(nsPIDOMWindow* aWindow)
|
2014-03-11 14:46:55 +04:00
|
|
|
: mWindow(aWindow)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
nsTArray<nsRefPtr<AudioChannelAgent>> mAgents;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelService::RefreshAgentsVolume(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
2015-07-10 19:38:51 +03:00
|
|
|
MOZ_ASSERT(aWindow);
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(aWindow->IsOuterWindow());
|
2015-07-10 19:38:51 +03:00
|
|
|
|
|
|
|
AudioChannelWindow* winData = GetWindowData(aWindow->WindowID());
|
2015-07-10 19:38:44 +03:00
|
|
|
if (!winData) {
|
|
|
|
return;
|
2015-07-11 15:14:58 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
nsTObserverArray<AudioChannelAgent*>::ForwardIterator
|
|
|
|
iter(winData->mAgents);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
iter.GetNext()->WindowVolumeChanged();
|
|
|
|
}
|
2014-03-11 14:47:25 +04:00
|
|
|
}
|
2014-03-20 14:45:55 +04:00
|
|
|
|
2014-04-10 21:39:20 +04:00
|
|
|
/* 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;
|
|
|
|
}
|
2014-03-20 14:45:55 +04:00
|
|
|
|
|
|
|
/* static */ AudioChannel
|
|
|
|
AudioChannelService::GetDefaultAudioChannel()
|
|
|
|
{
|
2015-07-10 19:38:44 +03:00
|
|
|
nsAutoString audioChannel(Preferences::GetString("media.defaultAudioChannel"));
|
2014-03-20 14:45:55 +04:00
|
|
|
if (audioChannel.IsEmpty()) {
|
|
|
|
return AudioChannel::Normal;
|
|
|
|
}
|
|
|
|
|
2014-04-10 21:39:20 +04:00
|
|
|
for (uint32_t i = 0; kMozAudioChannelAttributeTable[i].tag; ++i) {
|
|
|
|
if (audioChannel.EqualsASCII(kMozAudioChannelAttributeTable[i].tag)) {
|
|
|
|
return static_cast<AudioChannel>(kMozAudioChannelAttributeTable[i].value);
|
2014-03-20 14:45:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AudioChannel::Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2014-04-10 21:39:20 +04:00
|
|
|
AudioChannelService::GetAudioChannelString(AudioChannel aChannel,
|
|
|
|
nsAString& aString)
|
|
|
|
{
|
|
|
|
aString.AssignASCII("normal");
|
|
|
|
|
|
|
|
for (uint32_t i = 0; kMozAudioChannelAttributeTable[i].tag; ++i) {
|
|
|
|
if (aChannel ==
|
|
|
|
static_cast<AudioChannel>(kMozAudioChannelAttributeTable[i].value)) {
|
|
|
|
aString.AssignASCII(kMozAudioChannelAttributeTable[i].tag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
AudioChannelService::GetDefaultAudioChannelString(nsAString& aString)
|
2014-03-20 14:45:55 +04:00
|
|
|
{
|
|
|
|
aString.AssignASCII("normal");
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsAutoString audioChannel(Preferences::GetString("media.defaultAudioChannel"));
|
2014-03-20 14:45:55 +04:00
|
|
|
if (!audioChannel.IsEmpty()) {
|
2014-04-10 21:39:20 +04:00
|
|
|
for (uint32_t i = 0; kMozAudioChannelAttributeTable[i].tag; ++i) {
|
|
|
|
if (audioChannel.EqualsASCII(kMozAudioChannelAttributeTable[i].tag)) {
|
2014-03-20 14:45:55 +04:00
|
|
|
aString = audioChannel;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 17:06:23 +04:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelService::AudioChannelWindow*
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelService::GetOrCreateWindowData(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aWindow->IsOuterWindow());
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetWindowData(aWindow->WindowID());
|
|
|
|
if (!winData) {
|
|
|
|
winData = new AudioChannelWindow(aWindow->WindowID());
|
|
|
|
mWindows.AppendElement(winData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return winData;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelService::AudioChannelWindow*
|
|
|
|
AudioChannelService::GetWindowData(uint64_t aWindowID) const
|
|
|
|
{
|
|
|
|
nsTObserverArray<nsAutoPtr<AudioChannelWindow>>::ForwardIterator
|
|
|
|
iter(mWindows);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
AudioChannelWindow* next = iter.GetNext();
|
|
|
|
if (next->mWindowID == aWindowID) {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2015-07-10 19:38:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
AudioChannelService::GetAudioChannelVolume(nsPIDOMWindow* aWindow,
|
|
|
|
AudioChannel aAudioChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aWindow->IsOuterWindow());
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetOrCreateWindowData(aWindow);
|
|
|
|
return winData->mChannels[(uint32_t)aAudioChannel].mVolume;
|
2015-07-10 19:38:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelService::GetAudioChannelVolume(nsIDOMWindow* aWindow,
|
|
|
|
unsigned short aAudioChannel,
|
|
|
|
float* aVolume)
|
|
|
|
{
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTopWindow(aWindow);
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(window->IsOuterWindow());
|
2015-07-10 19:38:44 +03:00
|
|
|
*aVolume = GetAudioChannelVolume(window, (AudioChannel)aAudioChannel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
void
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelService::SetAudioChannelVolume(nsPIDOMWindow* aWindow,
|
|
|
|
AudioChannel aAudioChannel,
|
|
|
|
float aVolume)
|
2015-07-10 19:38:46 +03:00
|
|
|
{
|
2015-07-10 19:38:44 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aWindow->IsOuterWindow());
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetOrCreateWindowData(aWindow);
|
|
|
|
winData->mChannels[(uint32_t)aAudioChannel].mVolume = aVolume;
|
2015-07-10 19:38:44 +03:00
|
|
|
RefreshAgentsVolume(aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelService::SetAudioChannelVolume(nsIDOMWindow* aWindow,
|
|
|
|
unsigned short aAudioChannel,
|
|
|
|
float aVolume)
|
|
|
|
{
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTopWindow(aWindow);
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(window->IsOuterWindow());
|
2015-07-10 19:38:44 +03:00
|
|
|
SetAudioChannelVolume(window, (AudioChannel)aAudioChannel, aVolume);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioChannelService::GetAudioChannelMuted(nsPIDOMWindow* aWindow,
|
|
|
|
AudioChannel aAudioChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aWindow->IsOuterWindow());
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetOrCreateWindowData(aWindow);
|
|
|
|
return winData->mChannels[(uint32_t)aAudioChannel].mMuted;
|
2015-07-10 19:38:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelService::GetAudioChannelMuted(nsIDOMWindow* aWindow,
|
|
|
|
unsigned short aAudioChannel,
|
|
|
|
bool* aMuted)
|
|
|
|
{
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTopWindow(aWindow);
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(window->IsOuterWindow());
|
2015-07-10 19:38:44 +03:00
|
|
|
*aMuted = GetAudioChannelMuted(window, (AudioChannel)aAudioChannel);
|
|
|
|
return NS_OK;
|
2015-07-10 19:38:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelService::SetAudioChannelMuted(nsPIDOMWindow* aWindow,
|
|
|
|
AudioChannel aAudioChannel,
|
|
|
|
bool aMuted)
|
2015-07-10 19:38:44 +03:00
|
|
|
{
|
2015-07-10 19:38:44 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aWindow->IsOuterWindow());
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetOrCreateWindowData(aWindow);
|
|
|
|
winData->mChannels[(uint32_t)aAudioChannel].mMuted = aMuted;
|
2015-07-10 19:38:44 +03:00
|
|
|
RefreshAgentsVolume(aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelService::SetAudioChannelMuted(nsIDOMWindow* aWindow,
|
|
|
|
unsigned short aAudioChannel,
|
|
|
|
bool aMuted)
|
|
|
|
{
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTopWindow(aWindow);
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(window->IsOuterWindow());
|
2015-07-10 19:38:44 +03:00
|
|
|
SetAudioChannelMuted(window, (AudioChannel)aAudioChannel, aMuted);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioChannelService::IsAudioChannelActive(nsPIDOMWindow* aWindow,
|
|
|
|
AudioChannel aAudioChannel)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aWindow->IsOuterWindow());
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelWindow* winData = GetOrCreateWindowData(aWindow);
|
|
|
|
return !!winData->mChannels[(uint32_t)aAudioChannel].mNumberOfAgents;
|
2015-07-10 19:38:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelService::IsAudioChannelActive(nsIDOMWindow* aWindow,
|
|
|
|
unsigned short aAudioChannel,
|
|
|
|
bool* aActive)
|
|
|
|
{
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = GetTopWindow(aWindow);
|
2015-07-14 04:23:55 +03:00
|
|
|
MOZ_ASSERT(window->IsOuterWindow());
|
2015-07-10 19:38:44 +03:00
|
|
|
*aActive = IsAudioChannelActive(window, (AudioChannel)aAudioChannel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
void
|
|
|
|
AudioChannelService::SetDefaultVolumeControlChannel(int32_t aChannel,
|
|
|
|
bool aVisible)
|
|
|
|
{
|
|
|
|
SetDefaultVolumeControlChannelInternal(aChannel, aVisible,
|
|
|
|
CONTENT_PROCESS_ID_MAIN);
|
|
|
|
}
|
2015-07-10 19:38:46 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
void
|
|
|
|
AudioChannelService::SetDefaultVolumeControlChannelInternal(int32_t aChannel,
|
|
|
|
bool aVisible,
|
|
|
|
uint64_t aChildID)
|
|
|
|
{
|
2015-07-10 19:38:46 +03:00
|
|
|
if (!IsParentProcess()) {
|
2015-07-10 19:38:44 +03:00
|
|
|
ContentChild* cc = ContentChild::GetSingleton();
|
|
|
|
if (cc) {
|
|
|
|
cc->SendAudioChannelChangeDefVolChannel(aChannel, aVisible);
|
2015-07-11 03:55:59 +03:00
|
|
|
}
|
2015-07-10 19:38:44 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this child is in the background and mDefChannelChildID is set to
|
|
|
|
// others then it means other child in the foreground already set it's
|
|
|
|
// own default channel.
|
|
|
|
if (!aVisible && mDefChannelChildID != aChildID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Workaround for the call screen app. The call screen app is running on the
|
|
|
|
// main process, that will results in wrong visible state. Because we use the
|
|
|
|
// docshell's active state as visible state, the main process is always
|
|
|
|
// active. Therefore, we will see the strange situation that the visible
|
|
|
|
// state of the call screen is always true. If the mDefChannelChildID is set
|
|
|
|
// to others then it means other child in the foreground already set it's
|
|
|
|
// own default channel already.
|
|
|
|
// Summary :
|
|
|
|
// Child process : foreground app always can set type.
|
|
|
|
// Parent process : check the mDefChannelChildID.
|
|
|
|
else if (aChildID == CONTENT_PROCESS_ID_MAIN &&
|
|
|
|
mDefChannelChildID != CONTENT_PROCESS_ID_UNKNOWN) {
|
|
|
|
return;
|
2015-07-10 19:38:51 +03:00
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
mDefChannelChildID = aVisible ? aChildID : CONTENT_PROCESS_ID_UNKNOWN;
|
|
|
|
nsAutoString channelName;
|
|
|
|
|
|
|
|
if (aChannel == -1) {
|
|
|
|
channelName.AssignASCII("unknown");
|
|
|
|
} else {
|
|
|
|
GetAudioChannelString(static_cast<AudioChannel>(aChannel), channelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->NotifyObservers(nullptr, "default-volume-channel-changed",
|
|
|
|
channelName.get());
|
|
|
|
}
|
2015-07-10 19:38:49 +03:00
|
|
|
}
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
void
|
|
|
|
AudioChannelService::MaybeSendStatusUpdate()
|
|
|
|
{
|
|
|
|
if (IsParentProcess()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool telephonyChannel = TelephonyChannelIsActive();
|
|
|
|
bool contentOrNormalChannel = ContentOrNormalChannelIsActive();
|
|
|
|
bool anyChannel = AnyAudioChannelIsActive();
|
|
|
|
|
|
|
|
if (telephonyChannel == mTelephonyChannel &&
|
|
|
|
contentOrNormalChannel == mContentOrNormalChannel &&
|
|
|
|
anyChannel == mAnyChannel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTelephonyChannel = telephonyChannel;
|
|
|
|
mContentOrNormalChannel = contentOrNormalChannel;
|
|
|
|
mAnyChannel = anyChannel;
|
|
|
|
|
|
|
|
ContentChild* cc = ContentChild::GetSingleton();
|
|
|
|
if (cc) {
|
|
|
|
cc->SendAudioChannelServiceStatus(telephonyChannel, contentOrNormalChannel,
|
|
|
|
anyChannel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelService::ChildStatusReceived(uint64_t aChildID,
|
|
|
|
bool aTelephonyChannel,
|
|
|
|
bool aContentOrNormalChannel,
|
|
|
|
bool aAnyChannel)
|
|
|
|
{
|
|
|
|
if (!aAnyChannel) {
|
2015-07-10 19:38:51 +03:00
|
|
|
RemoveChildStatus(aChildID);
|
2015-07-10 19:38:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:51 +03:00
|
|
|
AudioChannelChildStatus* data = GetChildStatus(aChildID);
|
|
|
|
if (!data) {
|
|
|
|
data = new AudioChannelChildStatus(aChildID);
|
|
|
|
mPlayingChildren.AppendElement(data);
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
data->mActiveTelephonyChannel = aTelephonyChannel;
|
|
|
|
data->mActiveContentOrNormalChannel = aContentOrNormalChannel;
|
|
|
|
}
|
|
|
|
|
2015-07-10 19:38:49 +03:00
|
|
|
/* static */ bool
|
|
|
|
AudioChannelService::IsAudioChannelMutedByDefault()
|
|
|
|
{
|
|
|
|
return sAudioChannelMutedByDefault;
|
|
|
|
}
|