2012-12-04 23:46:07 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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"
|
2012-12-06 07:01:58 +04:00
|
|
|
#include "AudioChannelServiceChild.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
|
|
|
|
#include "nsThreadUtils.h"
|
2013-04-26 04:53:26 +04:00
|
|
|
#include "nsHashPropertyBag.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2012-12-06 14:11:06 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
#include "nsIAudioManager.h"
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
StaticRefPtr<AudioChannelService> gAudioChannelService;
|
|
|
|
|
|
|
|
// static
|
|
|
|
AudioChannelService*
|
|
|
|
AudioChannelService::GetAudioChannelService()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-06 07:01:58 +04:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
return AudioChannelServiceChild::GetAudioChannelService();
|
|
|
|
}
|
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
// If we already exist, exit early
|
|
|
|
if (gAudioChannelService) {
|
|
|
|
return gAudioChannelService;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new instance, register, return
|
|
|
|
nsRefPtr<AudioChannelService> service = new AudioChannelService();
|
|
|
|
NS_ENSURE_TRUE(service, nullptr);
|
|
|
|
|
|
|
|
gAudioChannelService = service;
|
|
|
|
return gAudioChannelService;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelService::Shutdown()
|
|
|
|
{
|
2012-12-06 07:01:58 +04:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
return AudioChannelServiceChild::Shutdown();
|
|
|
|
}
|
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
if (gAudioChannelService) {
|
|
|
|
gAudioChannelService = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-29 06:47:15 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(AudioChannelService, nsIObserver, nsITimerCallback)
|
2012-12-04 23:46:07 +04:00
|
|
|
|
|
|
|
AudioChannelService::AudioChannelService()
|
2013-01-09 11:18:16 +04:00
|
|
|
: mCurrentHigherChannel(AUDIO_CHANNEL_LAST)
|
2013-01-28 15:47:18 +04:00
|
|
|
, mCurrentVisibleHigherChannel(AUDIO_CHANNEL_LAST)
|
2013-01-25 19:12:17 +04:00
|
|
|
, mActiveContentChildIDsFrozen(false)
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
|
|
|
// Creation of the hash table.
|
2012-12-06 19:25:18 +04:00
|
|
|
mAgents.Init();
|
2012-12-28 21:57:35 +04:00
|
|
|
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, "ipc:content-shutdown", false);
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioChannelService::~AudioChannelService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-06 19:25:18 +04:00
|
|
|
AudioChannelService::RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
|
2012-12-28 21:57:35 +04:00
|
|
|
AudioChannelType aType)
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
2013-01-11 02:56:20 +04:00
|
|
|
AudioChannelAgentData* data = new AudioChannelAgentData(aType,
|
|
|
|
true /* mElementHidden */,
|
|
|
|
true /* mMuted */);
|
2013-01-09 11:18:16 +04:00
|
|
|
mAgents.Put(aAgent, data);
|
2013-02-15 00:41:29 +04:00
|
|
|
RegisterType(aType, CONTENT_PROCESS_ID_MAIN);
|
2012-12-06 07:01:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-28 21:57:35 +04:00
|
|
|
AudioChannelService::RegisterType(AudioChannelType aType, uint64_t aChildID)
|
2012-12-06 07:01:58 +04:00
|
|
|
{
|
2013-01-09 11:18:16 +04:00
|
|
|
AudioChannelInternalType type = GetInternalType(aType, true);
|
|
|
|
mChannelCounters[type].AppendElement(aChildID);
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
2013-04-29 06:47:15 +04:00
|
|
|
// Since there is another telephony registered, we can unregister old one
|
|
|
|
// immediately.
|
|
|
|
if (mDeferTelChannelTimer && aType == AUDIO_CHANNEL_TELEPHONY) {
|
|
|
|
mDeferTelChannelTimer->Cancel();
|
|
|
|
mDeferTelChannelTimer = nullptr;
|
|
|
|
UnregisterTypeInternal(aType, mTimerElementHidden, mTimerChildID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to avoid race conditions, it's safer to notify any existing
|
|
|
|
// agent any time a new one is registered.
|
2013-04-26 04:53:26 +04:00
|
|
|
SendAudioChannelChangedNotification(aChildID);
|
2013-01-09 11:18:16 +04:00
|
|
|
Notify();
|
|
|
|
}
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-06 19:25:18 +04:00
|
|
|
AudioChannelService::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent)
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
2013-01-11 02:56:20 +04:00
|
|
|
nsAutoPtr<AudioChannelAgentData> data;
|
|
|
|
mAgents.RemoveAndForget(aAgent, data);
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2013-01-11 02:56:20 +04:00
|
|
|
if (data) {
|
2013-02-15 00:41:29 +04:00
|
|
|
UnregisterType(data->mType, data->mElementHidden, CONTENT_PROCESS_ID_MAIN);
|
2013-01-11 02:56:20 +04:00
|
|
|
}
|
2012-12-06 07:01:58 +04:00
|
|
|
}
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2012-12-06 07:01:58 +04:00
|
|
|
void
|
2013-01-09 11:18:16 +04:00
|
|
|
AudioChannelService::UnregisterType(AudioChannelType aType,
|
|
|
|
bool aElementHidden,
|
|
|
|
uint64_t aChildID)
|
2013-04-29 06:47:15 +04:00
|
|
|
{
|
|
|
|
// There are two reasons to defer the decrease of telephony channel.
|
|
|
|
// 1. User can have time to remove device from his ear before music resuming.
|
|
|
|
// 2. Give BT SCO to be disconnected before starting to connect A2DP.
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default &&
|
|
|
|
aType == AUDIO_CHANNEL_TELEPHONY &&
|
|
|
|
(mChannelCounters[AUDIO_CHANNEL_INT_TELEPHONY_HIDDEN].Length() +
|
|
|
|
mChannelCounters[AUDIO_CHANNEL_INT_TELEPHONY].Length()) == 1) {
|
|
|
|
mTimerElementHidden = aElementHidden;
|
|
|
|
mTimerChildID = aChildID;
|
|
|
|
mDeferTelChannelTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
mDeferTelChannelTimer->InitWithCallback(this, 1500, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnregisterTypeInternal(aType, aElementHidden, aChildID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelService::UnregisterTypeInternal(AudioChannelType aType,
|
|
|
|
bool aElementHidden,
|
|
|
|
uint64_t aChildID)
|
2012-12-06 07:01:58 +04:00
|
|
|
{
|
2012-12-28 21:57:35 +04:00
|
|
|
// The array may contain multiple occurrence of this appId but
|
|
|
|
// this should remove only the first one.
|
2013-01-09 11:18:16 +04:00
|
|
|
AudioChannelInternalType type = GetInternalType(aType, aElementHidden);
|
2013-02-28 23:22:59 +04:00
|
|
|
MOZ_ASSERT(mChannelCounters[type].Contains(aChildID));
|
2013-01-09 11:18:16 +04:00
|
|
|
mChannelCounters[type].RemoveElement(aChildID);
|
2012-12-07 15:46:18 +04:00
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
// In order to avoid race conditions, it's safer to notify any existing
|
2012-12-06 19:25:18 +04:00
|
|
|
// agent any time a new one is registered.
|
2013-01-09 11:18:16 +04:00
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
2013-02-25 12:54:07 +04:00
|
|
|
// We only remove ChildID when it is in the foreground.
|
|
|
|
// If in the background, we kept ChildID for allowing it to play next song.
|
|
|
|
if (aType == AUDIO_CHANNEL_CONTENT &&
|
|
|
|
mActiveContentChildIDs.Contains(aChildID) &&
|
2013-02-28 23:22:59 +04:00
|
|
|
!aElementHidden &&
|
|
|
|
!mChannelCounters[AUDIO_CHANNEL_INT_CONTENT].Contains(aChildID)) {
|
2013-02-25 12:54:07 +04:00
|
|
|
mActiveContentChildIDs.RemoveElement(aChildID);
|
|
|
|
}
|
2013-04-26 04:53:26 +04:00
|
|
|
SendAudioChannelChangedNotification(aChildID);
|
2013-01-09 11:18:16 +04:00
|
|
|
Notify();
|
|
|
|
}
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:02:32 +04:00
|
|
|
void
|
|
|
|
AudioChannelService::UpdateChannelType(AudioChannelType aType,
|
|
|
|
uint64_t aChildID,
|
|
|
|
bool aElementHidden,
|
|
|
|
bool aElementWasHidden)
|
|
|
|
{
|
|
|
|
// Calculate the new and old internal type and update the hashtable if needed.
|
|
|
|
AudioChannelInternalType newType = GetInternalType(aType, aElementHidden);
|
|
|
|
AudioChannelInternalType oldType = GetInternalType(aType, aElementWasHidden);
|
|
|
|
|
|
|
|
if (newType != oldType) {
|
|
|
|
mChannelCounters[newType].AppendElement(aChildID);
|
2013-02-28 23:22:59 +04:00
|
|
|
MOZ_ASSERT(mChannelCounters[oldType].Contains(aChildID));
|
2013-02-26 21:02:32 +04:00
|
|
|
mChannelCounters[oldType].RemoveElement(aChildID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
bool
|
2013-01-09 11:18:16 +04:00
|
|
|
AudioChannelService::GetMuted(AudioChannelAgent* aAgent, bool aElementHidden)
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
2013-01-11 02:56:20 +04:00
|
|
|
AudioChannelAgentData* data;
|
2013-01-09 11:18:16 +04:00
|
|
|
if (!mAgents.Get(aAgent, &data)) {
|
|
|
|
return true;
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
2013-01-30 06:54:07 +04:00
|
|
|
bool oldElementHidden = data->mElementHidden;
|
2013-01-09 11:18:16 +04:00
|
|
|
// Update visibility.
|
2013-01-11 02:56:20 +04:00
|
|
|
data->mElementHidden = aElementHidden;
|
2013-01-30 06:54:07 +04:00
|
|
|
|
2013-02-15 00:41:29 +04:00
|
|
|
bool muted = GetMutedInternal(data->mType, CONTENT_PROCESS_ID_MAIN,
|
2013-01-30 06:54:07 +04:00
|
|
|
aElementHidden, oldElementHidden);
|
2013-01-11 02:56:20 +04:00
|
|
|
data->mMuted = muted;
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
return muted;
|
|
|
|
}
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
bool
|
|
|
|
AudioChannelService::GetMutedInternal(AudioChannelType aType, uint64_t aChildID,
|
|
|
|
bool aElementHidden, bool aElementWasHidden)
|
|
|
|
{
|
2013-02-26 21:02:32 +04:00
|
|
|
UpdateChannelType(aType, aChildID, aElementHidden, aElementWasHidden);
|
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
// Calculating the new and old type and update the hashtable if needed.
|
|
|
|
AudioChannelInternalType newType = GetInternalType(aType, aElementHidden);
|
|
|
|
AudioChannelInternalType oldType = GetInternalType(aType, aElementWasHidden);
|
2012-12-06 07:34:17 +04:00
|
|
|
|
2013-01-25 19:12:17 +04:00
|
|
|
// If the audio content channel is visible, let's remember this ChildID.
|
|
|
|
if (newType == AUDIO_CHANNEL_INT_CONTENT &&
|
2013-02-25 12:54:07 +04:00
|
|
|
oldType == AUDIO_CHANNEL_INT_CONTENT_HIDDEN) {
|
2013-01-25 19:12:17 +04:00
|
|
|
|
|
|
|
if (mActiveContentChildIDsFrozen) {
|
|
|
|
mActiveContentChildIDsFrozen = false;
|
|
|
|
mActiveContentChildIDs.Clear();
|
|
|
|
}
|
|
|
|
|
2013-02-25 12:54:07 +04:00
|
|
|
if (!mActiveContentChildIDs.Contains(aChildID)) {
|
|
|
|
mActiveContentChildIDs.AppendElement(aChildID);
|
|
|
|
}
|
2013-01-25 19:12:17 +04:00
|
|
|
}
|
|
|
|
else if (newType == AUDIO_CHANNEL_INT_CONTENT_HIDDEN &&
|
|
|
|
oldType == AUDIO_CHANNEL_INT_CONTENT &&
|
2013-02-25 12:54:07 +04:00
|
|
|
!mActiveContentChildIDsFrozen) {
|
|
|
|
// If nothing is visible, the list has to been frozen.
|
|
|
|
// Or if there is still any one with other ChildID in foreground then
|
|
|
|
// it should be removed from list and left other ChildIDs in the foreground
|
|
|
|
// to keep playing. Finally only last one childID which go to background
|
|
|
|
// will be in list.
|
|
|
|
if (mChannelCounters[AUDIO_CHANNEL_INT_CONTENT].IsEmpty()) {
|
|
|
|
mActiveContentChildIDsFrozen = true;
|
|
|
|
} else if (!mChannelCounters[AUDIO_CHANNEL_INT_CONTENT].Contains(aChildID)) {
|
2013-02-28 23:22:59 +04:00
|
|
|
MOZ_ASSERT(mActiveContentChildIDs.Contains(aChildID));
|
2013-02-25 12:54:07 +04:00
|
|
|
mActiveContentChildIDs.RemoveElement(aChildID);
|
|
|
|
}
|
2013-01-25 19:12:17 +04:00
|
|
|
}
|
|
|
|
|
2013-01-30 06:54:07 +04:00
|
|
|
if (newType != oldType && aType == AUDIO_CHANNEL_CONTENT) {
|
|
|
|
Notify();
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
SendAudioChannelChangedNotification(aChildID);
|
|
|
|
|
2013-01-16 17:29:57 +04:00
|
|
|
// Let play any visible audio channel.
|
|
|
|
if (!aElementHidden) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
bool muted = false;
|
2012-12-06 05:20:59 +04:00
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
// We are not visible, maybe we have to mute.
|
|
|
|
if (newType == AUDIO_CHANNEL_INT_NORMAL_HIDDEN ||
|
|
|
|
(newType == AUDIO_CHANNEL_INT_CONTENT_HIDDEN &&
|
2013-01-25 19:12:17 +04:00
|
|
|
!mActiveContentChildIDs.Contains(aChildID))) {
|
2013-01-09 11:18:16 +04:00
|
|
|
muted = true;
|
|
|
|
}
|
2012-12-06 05:20:59 +04:00
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
if (!muted) {
|
|
|
|
MOZ_ASSERT(newType != AUDIO_CHANNEL_INT_NORMAL_HIDDEN);
|
|
|
|
muted = ChannelsActiveWithHigherPriorityThan(newType);
|
2012-12-06 05:20:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return muted;
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
2013-01-05 09:03:51 +04:00
|
|
|
bool
|
2013-01-28 15:47:18 +04:00
|
|
|
AudioChannelService::ContentOrNormalChannelIsActive()
|
2013-01-05 09:03:51 +04:00
|
|
|
{
|
2013-01-09 11:18:16 +04:00
|
|
|
return !mChannelCounters[AUDIO_CHANNEL_INT_CONTENT].IsEmpty() ||
|
2013-01-28 15:47:18 +04:00
|
|
|
!mChannelCounters[AUDIO_CHANNEL_INT_CONTENT_HIDDEN].IsEmpty() ||
|
|
|
|
!mChannelCounters[AUDIO_CHANNEL_INT_NORMAL].IsEmpty();
|
2013-01-05 09:03:51 +04:00
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
bool
|
|
|
|
AudioChannelService::ProcessContentOrNormalChannelIsActive(uint64_t aChildID)
|
|
|
|
{
|
|
|
|
return mChannelCounters[AUDIO_CHANNEL_INT_CONTENT].Contains(aChildID) ||
|
|
|
|
mChannelCounters[AUDIO_CHANNEL_INT_CONTENT_HIDDEN].Contains(aChildID) ||
|
|
|
|
mChannelCounters[AUDIO_CHANNEL_INT_NORMAL].Contains(aChildID);
|
|
|
|
}
|
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
void
|
2013-04-26 04:53:26 +04:00
|
|
|
AudioChannelService::SendAudioChannelChangedNotification(uint64_t aChildID)
|
2013-01-09 11:18:16 +04:00
|
|
|
{
|
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
nsRefPtr<nsHashPropertyBag> props = new nsHashPropertyBag();
|
|
|
|
props->Init();
|
|
|
|
props->SetPropertyAsUint64(NS_LITERAL_STRING("childID"), aChildID);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
obs->NotifyObservers(static_cast<nsIWritablePropertyBag*>(props),
|
|
|
|
"audio-channel-process-changed", nullptr);
|
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
// Calculating the most important active channel.
|
|
|
|
AudioChannelType higher = AUDIO_CHANNEL_LAST;
|
|
|
|
|
2013-01-16 17:38:51 +04:00
|
|
|
// Top-Down in the hierarchy for visible elements
|
2013-01-09 11:18:16 +04:00
|
|
|
if (!mChannelCounters[AUDIO_CHANNEL_INT_PUBLICNOTIFICATION].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_PUBLICNOTIFICATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_RINGER].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_RINGER;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_TELEPHONY].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_TELEPHONY;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_ALARM].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_ALARM;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_NOTIFICATION].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_NOTIFICATION;
|
|
|
|
}
|
|
|
|
|
2013-01-16 17:38:51 +04:00
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_CONTENT].IsEmpty()) {
|
2013-01-09 11:18:16 +04:00
|
|
|
higher = AUDIO_CHANNEL_CONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_NORMAL].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_NORMAL;
|
|
|
|
}
|
|
|
|
|
2013-01-28 15:47:18 +04:00
|
|
|
AudioChannelType visibleHigher = higher;
|
|
|
|
|
2013-01-16 17:38:51 +04:00
|
|
|
// Top-Down in the hierarchy for non-visible elements
|
2013-01-28 15:47:18 +04:00
|
|
|
if (higher == AUDIO_CHANNEL_LAST) {
|
|
|
|
if (!mChannelCounters[AUDIO_CHANNEL_INT_PUBLICNOTIFICATION_HIDDEN].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_PUBLICNOTIFICATION;
|
|
|
|
}
|
2013-01-16 17:38:51 +04:00
|
|
|
|
2013-01-28 15:47:18 +04:00
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_RINGER_HIDDEN].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_RINGER;
|
|
|
|
}
|
2013-01-16 17:38:51 +04:00
|
|
|
|
2013-01-28 15:47:18 +04:00
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_TELEPHONY_HIDDEN].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_TELEPHONY;
|
|
|
|
}
|
2013-01-16 17:38:51 +04:00
|
|
|
|
2013-01-28 15:47:18 +04:00
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_ALARM_HIDDEN].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_ALARM;
|
|
|
|
}
|
2013-01-16 17:38:51 +04:00
|
|
|
|
2013-01-28 15:47:18 +04:00
|
|
|
else if (!mChannelCounters[AUDIO_CHANNEL_INT_NOTIFICATION_HIDDEN].IsEmpty()) {
|
|
|
|
higher = AUDIO_CHANNEL_NOTIFICATION;
|
|
|
|
}
|
2013-01-16 17:38:51 +04:00
|
|
|
|
2013-02-25 12:54:07 +04:00
|
|
|
// There is only one Child can play content channel in the background.
|
|
|
|
// And need to check whether there is any content channels under playing
|
|
|
|
// now.
|
|
|
|
else if (!mActiveContentChildIDs.IsEmpty() &&
|
|
|
|
mChannelCounters[AUDIO_CHANNEL_INT_CONTENT_HIDDEN].Contains(
|
|
|
|
mActiveContentChildIDs[0])) {
|
2013-01-28 15:47:18 +04:00
|
|
|
higher = AUDIO_CHANNEL_CONTENT;
|
|
|
|
}
|
2013-01-16 17:38:51 +04:00
|
|
|
}
|
|
|
|
|
2013-01-09 11:18:16 +04:00
|
|
|
if (higher != mCurrentHigherChannel) {
|
|
|
|
mCurrentHigherChannel = higher;
|
|
|
|
|
|
|
|
nsString channelName;
|
|
|
|
if (mCurrentHigherChannel != AUDIO_CHANNEL_LAST) {
|
|
|
|
channelName.AssignASCII(ChannelName(mCurrentHigherChannel));
|
|
|
|
} else {
|
2013-01-09 11:20:34 +04:00
|
|
|
channelName.AssignLiteral("none");
|
2013-01-09 11:18:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
obs->NotifyObservers(nullptr, "audio-channel-changed", channelName.get());
|
|
|
|
}
|
2013-01-28 15:47:18 +04:00
|
|
|
|
|
|
|
if (visibleHigher != mCurrentVisibleHigherChannel) {
|
|
|
|
mCurrentVisibleHigherChannel = visibleHigher;
|
|
|
|
|
|
|
|
nsString channelName;
|
|
|
|
if (mCurrentVisibleHigherChannel != AUDIO_CHANNEL_LAST) {
|
|
|
|
channelName.AssignASCII(ChannelName(mCurrentVisibleHigherChannel));
|
|
|
|
} else {
|
|
|
|
channelName.AssignLiteral("none");
|
|
|
|
}
|
|
|
|
|
|
|
|
obs->NotifyObservers(nullptr, "visible-audio-channel-changed", channelName.get());
|
|
|
|
}
|
2013-01-09 11:18:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashOperator
|
|
|
|
AudioChannelService::NotifyEnumerator(AudioChannelAgent* aAgent,
|
2013-01-11 02:56:20 +04:00
|
|
|
AudioChannelAgentData* aData, void* aUnused)
|
2013-01-09 11:18:16 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aAgent);
|
|
|
|
aAgent->NotifyAudioChannelStateChanged();
|
2012-12-04 23:46:07 +04:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioChannelService::Notify()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-06 19:25:18 +04:00
|
|
|
// Notify any agent for the main process.
|
|
|
|
mAgents.EnumerateRead(NotifyEnumerator, nullptr);
|
2012-12-06 07:01:58 +04:00
|
|
|
|
|
|
|
// Notify for the child processes.
|
|
|
|
nsTArray<ContentParent*> children;
|
|
|
|
ContentParent::GetAll(children);
|
|
|
|
for (uint32_t i = 0; i < children.Length(); i++) {
|
|
|
|
unused << children[i]->SendAudioChannelNotify();
|
|
|
|
}
|
2012-12-04 23:46:07 +04:00
|
|
|
}
|
|
|
|
|
2013-04-29 06:47:15 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelService::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
UnregisterTypeInternal(AUDIO_CHANNEL_TELEPHONY, mTimerElementHidden, mTimerChildID);
|
|
|
|
mDeferTelChannelTimer = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
bool
|
2013-01-09 11:18:16 +04:00
|
|
|
AudioChannelService::ChannelsActiveWithHigherPriorityThan(AudioChannelInternalType aType)
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
2013-01-16 17:38:51 +04:00
|
|
|
for (int i = AUDIO_CHANNEL_INT_LAST - 1;
|
2013-01-09 11:18:16 +04:00
|
|
|
i != AUDIO_CHANNEL_INT_CONTENT_HIDDEN; --i) {
|
2012-12-04 23:46:07 +04:00
|
|
|
if (i == aType) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-28 21:57:35 +04:00
|
|
|
if (!mChannelCounters[i].IsEmpty()) {
|
2012-12-04 23:46:07 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-06 05:20:59 +04:00
|
|
|
|
|
|
|
const char*
|
|
|
|
AudioChannelService::ChannelName(AudioChannelType aType)
|
|
|
|
{
|
|
|
|
static struct {
|
|
|
|
int32_t type;
|
|
|
|
const char* value;
|
|
|
|
} ChannelNameTable[] = {
|
|
|
|
{ AUDIO_CHANNEL_NORMAL, "normal" },
|
2013-03-27 08:04:32 +04:00
|
|
|
{ AUDIO_CHANNEL_CONTENT, "content" },
|
2012-12-06 05:20:59 +04:00
|
|
|
{ AUDIO_CHANNEL_NOTIFICATION, "notification" },
|
|
|
|
{ AUDIO_CHANNEL_ALARM, "alarm" },
|
|
|
|
{ AUDIO_CHANNEL_TELEPHONY, "telephony" },
|
2012-12-06 13:11:19 +04:00
|
|
|
{ AUDIO_CHANNEL_RINGER, "ringer" },
|
2012-12-06 05:20:59 +04:00
|
|
|
{ AUDIO_CHANNEL_PUBLICNOTIFICATION, "publicnotification" },
|
|
|
|
{ -1, "unknown" }
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = AUDIO_CHANNEL_NORMAL; ; ++i) {
|
|
|
|
if (ChannelNameTable[i].type == aType ||
|
|
|
|
ChannelNameTable[i].type == -1) {
|
|
|
|
return ChannelNameTable[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Execution should not reach here!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2012-12-06 14:11:06 +04:00
|
|
|
|
2012-12-28 21:57:35 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioChannelService::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* data)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!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;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t childID = 0;
|
|
|
|
nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"),
|
|
|
|
&childID);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2013-01-09 11:18:16 +04:00
|
|
|
for (int32_t type = AUDIO_CHANNEL_INT_NORMAL;
|
|
|
|
type < AUDIO_CHANNEL_INT_LAST;
|
2012-12-28 21:57:35 +04:00
|
|
|
++type) {
|
|
|
|
int32_t index;
|
|
|
|
while ((index = mChannelCounters[type].IndexOf(childID)) != -1) {
|
|
|
|
mChannelCounters[type].RemoveElementAt(index);
|
|
|
|
}
|
2013-01-25 19:12:17 +04:00
|
|
|
|
|
|
|
if ((index = mActiveContentChildIDs.IndexOf(childID)) != -1) {
|
|
|
|
mActiveContentChildIDs.RemoveElementAt(index);
|
|
|
|
}
|
2012-12-28 21:57:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have to remove the agents from the mAgents hashtable because if
|
|
|
|
// that table contains only agents running on the same process.
|
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
SendAudioChannelChangedNotification(childID);
|
2012-12-28 21:57:35 +04:00
|
|
|
Notify();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without childID property");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
AudioChannelService::AudioChannelInternalType
|
|
|
|
AudioChannelService::GetInternalType(AudioChannelType aType,
|
|
|
|
bool aElementHidden)
|
|
|
|
{
|
|
|
|
switch (aType) {
|
|
|
|
case AUDIO_CHANNEL_NORMAL:
|
|
|
|
return aElementHidden
|
2013-01-16 17:38:51 +04:00
|
|
|
? AUDIO_CHANNEL_INT_NORMAL_HIDDEN
|
|
|
|
: AUDIO_CHANNEL_INT_NORMAL;
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
case AUDIO_CHANNEL_CONTENT:
|
|
|
|
return aElementHidden
|
2013-01-16 17:38:51 +04:00
|
|
|
? AUDIO_CHANNEL_INT_CONTENT_HIDDEN
|
|
|
|
: AUDIO_CHANNEL_INT_CONTENT;
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
case AUDIO_CHANNEL_NOTIFICATION:
|
2013-01-16 17:38:51 +04:00
|
|
|
return aElementHidden
|
|
|
|
? AUDIO_CHANNEL_INT_NOTIFICATION_HIDDEN
|
|
|
|
: AUDIO_CHANNEL_INT_NOTIFICATION;
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
case AUDIO_CHANNEL_ALARM:
|
2013-01-16 17:38:51 +04:00
|
|
|
return aElementHidden
|
|
|
|
? AUDIO_CHANNEL_INT_ALARM_HIDDEN
|
|
|
|
: AUDIO_CHANNEL_INT_ALARM;
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
case AUDIO_CHANNEL_TELEPHONY:
|
2013-01-16 17:38:51 +04:00
|
|
|
return aElementHidden
|
|
|
|
? AUDIO_CHANNEL_INT_TELEPHONY_HIDDEN
|
|
|
|
: AUDIO_CHANNEL_INT_TELEPHONY;
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
case AUDIO_CHANNEL_RINGER:
|
2013-01-16 17:38:51 +04:00
|
|
|
return aElementHidden
|
|
|
|
? AUDIO_CHANNEL_INT_RINGER_HIDDEN
|
|
|
|
: AUDIO_CHANNEL_INT_RINGER;
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
case AUDIO_CHANNEL_PUBLICNOTIFICATION:
|
2013-01-16 17:38:51 +04:00
|
|
|
return aElementHidden
|
|
|
|
? AUDIO_CHANNEL_INT_PUBLICNOTIFICATION_HIDDEN
|
|
|
|
: AUDIO_CHANNEL_INT_PUBLICNOTIFICATION;
|
2013-01-09 11:18:16 +04:00
|
|
|
|
|
|
|
case AUDIO_CHANNEL_LAST:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-04-21 00:36:43 +04:00
|
|
|
MOZ_NOT_REACHED("unexpected audio channel type");
|
2013-01-09 11:18:16 +04:00
|
|
|
return AUDIO_CHANNEL_INT_LAST;
|
|
|
|
}
|