2012-12-04 11:46:07 -08: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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_audiochannelservice_h__
|
|
|
|
#define mozilla_dom_audiochannelservice_h__
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
2012-12-28 09:57:35 -08:00
|
|
|
#include "nsIObserver.h"
|
2013-03-17 00:55:16 -07:00
|
|
|
#include "nsTArray.h"
|
2013-04-28 19:47:15 -07:00
|
|
|
#include "nsITimer.h"
|
2012-12-04 11:46:07 -08:00
|
|
|
|
|
|
|
#include "AudioChannelCommon.h"
|
2012-12-06 07:25:18 -08:00
|
|
|
#include "AudioChannelAgent.h"
|
2014-04-10 10:39:20 -07:00
|
|
|
#include "nsAttrValue.h"
|
2013-01-10 14:56:20 -08:00
|
|
|
#include "nsClassHashtable.h"
|
2014-03-20 03:45:55 -07:00
|
|
|
#include "mozilla/dom/AudioChannelBinding.h"
|
2012-12-04 11:46:07 -08:00
|
|
|
|
2014-06-24 22:15:12 -07:00
|
|
|
class nsIRunnable;
|
2014-03-11 03:46:55 -07:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
2012-12-04 11:46:07 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-11-24 15:50:03 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
class SpeakerManagerService;
|
|
|
|
#endif
|
2013-04-28 19:47:15 -07:00
|
|
|
class AudioChannelService
|
|
|
|
: public nsIObserver
|
|
|
|
, public nsITimerCallback
|
2012-12-04 11:46:07 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2012-12-28 09:57:35 -08:00
|
|
|
NS_DECL_NSIOBSERVER
|
2013-04-28 19:47:15 -07:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2012-12-04 11:46:07 -08:00
|
|
|
|
|
|
|
/**
|
2014-08-28 23:10:16 -07:00
|
|
|
* Returns the AudioChannelServce singleton or null if the process havn't create it before.
|
|
|
|
* Only to be called from main thread.
|
2012-12-04 11:46:07 -08:00
|
|
|
*/
|
2013-09-02 02:45:44 -07:00
|
|
|
static AudioChannelService* GetAudioChannelService();
|
2012-12-04 11:46:07 -08:00
|
|
|
|
2014-08-28 23:10:16 -07:00
|
|
|
/**
|
|
|
|
* Returns the AudioChannelServce singleton.
|
|
|
|
* If AudioChannelServce is not exist, create and return new one.
|
|
|
|
* Only to be called from main thread.
|
|
|
|
*/
|
|
|
|
static AudioChannelService* GetOrCreateAudioChannelService();
|
|
|
|
|
2012-12-04 11:46:07 -08:00
|
|
|
/**
|
|
|
|
* Shutdown the singleton.
|
|
|
|
*/
|
|
|
|
static void Shutdown();
|
|
|
|
|
|
|
|
/**
|
2012-12-06 07:25:18 -08:00
|
|
|
* Any audio channel agent that starts playing should register itself to
|
2014-04-10 10:39:20 -07:00
|
|
|
* this service, sharing the AudioChannel.
|
2012-12-04 11:46:07 -08:00
|
|
|
*/
|
2012-12-06 07:25:18 -08:00
|
|
|
virtual void RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
|
2014-04-10 10:39:20 -07:00
|
|
|
AudioChannel aChannel,
|
2013-09-17 20:46:22 -07:00
|
|
|
bool aWithVideo);
|
2012-12-04 11:46:07 -08:00
|
|
|
|
|
|
|
/**
|
2013-09-12 05:26:03 -07:00
|
|
|
* Any audio channel agent that stops playing should unregister itself to
|
2012-12-04 11:46:07 -08:00
|
|
|
* this service.
|
|
|
|
*/
|
2012-12-06 07:25:18 -08:00
|
|
|
virtual void UnregisterAudioChannelAgent(AudioChannelAgent* aAgent);
|
2012-12-04 11:46:07 -08:00
|
|
|
|
|
|
|
/**
|
2013-09-02 02:45:44 -07:00
|
|
|
* Return the state to indicate this agent should keep playing/
|
|
|
|
* fading volume/muted.
|
2012-12-04 11:46:07 -08:00
|
|
|
*/
|
2013-09-02 02:45:44 -07:00
|
|
|
virtual AudioChannelState GetState(AudioChannelAgent* aAgent,
|
|
|
|
bool aElementHidden);
|
2012-12-04 11:46:07 -08:00
|
|
|
|
2013-01-04 21:03:51 -08:00
|
|
|
/**
|
|
|
|
* Return true if there is a content channel active in this process
|
|
|
|
* or one of its subprocesses.
|
|
|
|
*/
|
2013-01-28 03:47:18 -08:00
|
|
|
virtual bool ContentOrNormalChannelIsActive();
|
2013-01-04 21:03:51 -08:00
|
|
|
|
2014-05-29 00:20:42 -07:00
|
|
|
/**
|
|
|
|
* Return true if there is a telephony channel active in this process
|
|
|
|
* or one of its subprocesses.
|
|
|
|
*/
|
|
|
|
virtual bool TelephonyChannelIsActive();
|
|
|
|
|
2013-04-25 17:53:26 -07:00
|
|
|
/**
|
2013-09-02 02:45:44 -07:00
|
|
|
* Return true if a normal or content channel is active for the given
|
|
|
|
* process ID.
|
2013-04-25 17:53:26 -07:00
|
|
|
*/
|
|
|
|
virtual bool ProcessContentOrNormalChannelIsActive(uint64_t aChildID);
|
|
|
|
|
2013-09-12 05:26:03 -07:00
|
|
|
/***
|
|
|
|
* AudioChannelManager calls this function to notify the default channel used
|
2014-04-10 10:39:20 -07:00
|
|
|
* to adjust volume when there is no any active channel. if aChannel is -1,
|
|
|
|
* the default audio channel will be used. Otherwise aChannel is casted to
|
|
|
|
* AudioChannel enum.
|
2013-09-12 05:26:03 -07:00
|
|
|
*/
|
2014-04-10 10:39:20 -07:00
|
|
|
virtual void SetDefaultVolumeControlChannel(int32_t aChannel,
|
2013-09-12 05:26:03 -07:00
|
|
|
bool aHidden);
|
|
|
|
|
2013-11-24 15:50:03 -08:00
|
|
|
bool AnyAudioChannelIsActive();
|
|
|
|
|
2014-03-11 03:46:55 -07:00
|
|
|
void RefreshAgentsVolume(nsPIDOMWindow* aWindow);
|
|
|
|
|
2013-11-24 15:50:03 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
void RegisterSpeakerManager(SpeakerManagerService* aSpeakerManager)
|
|
|
|
{
|
|
|
|
if (!mSpeakerManager.Contains(aSpeakerManager)) {
|
|
|
|
mSpeakerManager.AppendElement(aSpeakerManager);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnregisterSpeakerManager(SpeakerManagerService* aSpeakerManager)
|
|
|
|
{
|
|
|
|
mSpeakerManager.RemoveElement(aSpeakerManager);
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-20 03:45:55 -07:00
|
|
|
|
2014-04-10 10:39:20 -07:00
|
|
|
static const nsAttrValue::EnumTable* GetAudioChannelTable();
|
|
|
|
static AudioChannel GetAudioChannel(const nsAString& aString);
|
2014-03-20 03:45:55 -07:00
|
|
|
static AudioChannel GetDefaultAudioChannel();
|
2014-04-10 10:39:20 -07:00
|
|
|
static void GetAudioChannelString(AudioChannel aChannel, nsAString& aString);
|
|
|
|
static void GetDefaultAudioChannelString(nsAString& aString);
|
2014-03-20 03:45:55 -07:00
|
|
|
|
2012-12-04 11:46:07 -08:00
|
|
|
void Notify();
|
|
|
|
|
2014-06-24 22:15:12 -07:00
|
|
|
protected:
|
|
|
|
void SendNotification();
|
|
|
|
|
2013-01-08 23:18:16 -08:00
|
|
|
/**
|
2013-04-25 17:53:26 -07:00
|
|
|
* Send the audio-channel-changed notification for the given process ID if
|
|
|
|
* needed.
|
2013-01-08 23:18:16 -08:00
|
|
|
*/
|
2013-04-25 17:53:26 -07:00
|
|
|
void SendAudioChannelChangedNotification(uint64_t aChildID);
|
2013-01-08 23:18:16 -08:00
|
|
|
|
2012-12-05 19:01:58 -08:00
|
|
|
/* Register/Unregister IPC types: */
|
2014-04-10 10:39:20 -07:00
|
|
|
void RegisterType(AudioChannel aChannel, uint64_t aChildID, bool aWithVideo);
|
|
|
|
void UnregisterType(AudioChannel aChannel, bool aElementHidden,
|
2013-09-17 20:46:22 -07:00
|
|
|
uint64_t aChildID, bool aWithVideo);
|
2014-04-10 10:39:20 -07:00
|
|
|
void UnregisterTypeInternal(AudioChannel aChannel, bool aElementHidden,
|
2013-09-17 20:46:22 -07:00
|
|
|
uint64_t aChildID, bool aWithVideo);
|
2013-01-08 23:18:16 -08:00
|
|
|
|
2014-04-10 10:39:20 -07:00
|
|
|
AudioChannelState GetStateInternal(AudioChannel aChannel, uint64_t aChildID,
|
2013-09-02 02:45:44 -07:00
|
|
|
bool aElementHidden,
|
|
|
|
bool aElementWasHidden);
|
2012-12-05 19:01:58 -08:00
|
|
|
|
2013-02-26 09:02:32 -08:00
|
|
|
/* Update the internal type value following the visibility changes */
|
2014-04-10 10:39:20 -07:00
|
|
|
void UpdateChannelType(AudioChannel aChannel, uint64_t aChildID,
|
2013-02-26 09:02:32 -08:00
|
|
|
bool aElementHidden, bool aElementWasHidden);
|
|
|
|
|
2013-09-12 05:26:03 -07:00
|
|
|
/* Send the default-volume-channel-changed notification */
|
2014-04-10 10:39:20 -07:00
|
|
|
void SetDefaultVolumeControlChannelInternal(int32_t aChannel,
|
2013-09-12 05:26:03 -07:00
|
|
|
bool aHidden, uint64_t aChildID);
|
|
|
|
|
2014-07-10 06:06:23 -07:00
|
|
|
AudioChannelState CheckTelephonyPolicy(AudioChannel aChannel,
|
|
|
|
uint64_t aChildID);
|
|
|
|
void RegisterTelephonyChild(uint64_t aChildID);
|
|
|
|
void UnregisterTelephonyChild(uint64_t aChildID);
|
|
|
|
|
2012-12-04 11:46:07 -08:00
|
|
|
AudioChannelService();
|
|
|
|
virtual ~AudioChannelService();
|
|
|
|
|
2013-01-08 23:18:16 -08:00
|
|
|
enum AudioChannelInternalType {
|
|
|
|
AUDIO_CHANNEL_INT_NORMAL = 0,
|
|
|
|
AUDIO_CHANNEL_INT_NORMAL_HIDDEN,
|
|
|
|
AUDIO_CHANNEL_INT_CONTENT,
|
|
|
|
AUDIO_CHANNEL_INT_CONTENT_HIDDEN,
|
|
|
|
AUDIO_CHANNEL_INT_NOTIFICATION,
|
2013-01-16 05:38:51 -08:00
|
|
|
AUDIO_CHANNEL_INT_NOTIFICATION_HIDDEN,
|
2013-01-08 23:18:16 -08:00
|
|
|
AUDIO_CHANNEL_INT_ALARM,
|
2013-01-16 05:38:51 -08:00
|
|
|
AUDIO_CHANNEL_INT_ALARM_HIDDEN,
|
2013-01-08 23:18:16 -08:00
|
|
|
AUDIO_CHANNEL_INT_TELEPHONY,
|
2013-01-16 05:38:51 -08:00
|
|
|
AUDIO_CHANNEL_INT_TELEPHONY_HIDDEN,
|
2013-01-08 23:18:16 -08:00
|
|
|
AUDIO_CHANNEL_INT_RINGER,
|
2013-01-16 05:38:51 -08:00
|
|
|
AUDIO_CHANNEL_INT_RINGER_HIDDEN,
|
2013-01-08 23:18:16 -08:00
|
|
|
AUDIO_CHANNEL_INT_PUBLICNOTIFICATION,
|
2013-01-16 05:38:51 -08:00
|
|
|
AUDIO_CHANNEL_INT_PUBLICNOTIFICATION_HIDDEN,
|
2013-01-08 23:18:16 -08:00
|
|
|
AUDIO_CHANNEL_INT_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ChannelsActiveWithHigherPriorityThan(AudioChannelInternalType aType);
|
|
|
|
|
2013-09-02 02:45:44 -07:00
|
|
|
bool CheckVolumeFadedCondition(AudioChannelInternalType aType,
|
|
|
|
bool aElementHidden);
|
|
|
|
|
2014-04-10 10:39:20 -07:00
|
|
|
AudioChannelInternalType GetInternalType(AudioChannel aChannel,
|
2013-01-08 23:18:16 -08:00
|
|
|
bool aElementHidden);
|
|
|
|
|
2013-01-10 14:56:20 -08:00
|
|
|
class AudioChannelAgentData {
|
|
|
|
public:
|
2014-04-10 10:39:20 -07:00
|
|
|
AudioChannelAgentData(AudioChannel aChannel,
|
2013-01-10 14:56:20 -08:00
|
|
|
bool aElementHidden,
|
2013-09-17 20:46:22 -07:00
|
|
|
AudioChannelState aState,
|
|
|
|
bool aWithVideo)
|
2014-04-10 10:39:20 -07:00
|
|
|
: mChannel(aChannel)
|
2013-01-10 14:56:20 -08:00
|
|
|
, mElementHidden(aElementHidden)
|
2013-09-02 02:45:44 -07:00
|
|
|
, mState(aState)
|
2013-09-17 20:46:22 -07:00
|
|
|
, mWithVideo(aWithVideo)
|
2013-01-10 14:56:20 -08:00
|
|
|
{}
|
|
|
|
|
2014-04-10 10:39:20 -07:00
|
|
|
AudioChannel mChannel;
|
2013-01-08 23:18:16 -08:00
|
|
|
bool mElementHidden;
|
2013-09-02 02:45:44 -07:00
|
|
|
AudioChannelState mState;
|
2013-09-17 20:46:22 -07:00
|
|
|
const bool mWithVideo;
|
2013-01-08 23:18:16 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
NotifyEnumerator(AudioChannelAgent* aAgent,
|
2013-01-10 14:56:20 -08:00
|
|
|
AudioChannelAgentData* aData, void *aUnused);
|
2013-01-08 23:18:16 -08:00
|
|
|
|
2014-03-11 03:46:55 -07:00
|
|
|
static PLDHashOperator
|
|
|
|
RefreshAgentsVolumeEnumerator(AudioChannelAgent* aAgent,
|
|
|
|
AudioChannelAgentData* aUnused,
|
|
|
|
void *aPtr);
|
|
|
|
|
2014-03-11 03:47:25 -07:00
|
|
|
static PLDHashOperator
|
|
|
|
CountWindowEnumerator(AudioChannelAgent* aAgent,
|
|
|
|
AudioChannelAgentData* aUnused,
|
|
|
|
void *aPtr);
|
|
|
|
|
2014-10-17 08:29:39 -07:00
|
|
|
static PLDHashOperator
|
|
|
|
WindowDestroyedEnumerator(AudioChannelAgent* aAgent,
|
|
|
|
nsAutoPtr<AudioChannelAgentData>& aData,
|
|
|
|
void *aPtr);
|
|
|
|
|
2014-03-11 03:47:25 -07:00
|
|
|
// This returns the number of agents from this aWindow.
|
|
|
|
uint32_t CountWindow(nsIDOMWindow* aWindow);
|
|
|
|
|
2013-01-10 14:56:20 -08:00
|
|
|
nsClassHashtable< nsPtrHashKey<AudioChannelAgent>, AudioChannelAgentData > mAgents;
|
2013-11-24 15:50:03 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
nsTArray<SpeakerManagerService*> mSpeakerManager;
|
|
|
|
#endif
|
2013-01-08 23:18:16 -08:00
|
|
|
nsTArray<uint64_t> mChannelCounters[AUDIO_CHANNEL_INT_LAST];
|
2012-12-05 17:20:59 -08:00
|
|
|
|
2014-04-10 10:39:20 -07:00
|
|
|
int32_t mCurrentHigherChannel;
|
|
|
|
int32_t mCurrentVisibleHigherChannel;
|
2012-12-05 19:01:58 -08:00
|
|
|
|
2013-09-17 20:46:22 -07:00
|
|
|
nsTArray<uint64_t> mWithVideoChildIDs;
|
2013-09-17 00:46:06 -07:00
|
|
|
|
2014-07-10 06:06:23 -07:00
|
|
|
// Telephony Channel policy is "LIFO", the last app to require the resource is
|
|
|
|
// allowed to play. The others are muted.
|
|
|
|
struct TelephonyChild {
|
|
|
|
uint64_t mChildID;
|
|
|
|
uint32_t mInstances;
|
|
|
|
|
|
|
|
explicit TelephonyChild(uint64_t aChildID)
|
|
|
|
: mChildID(aChildID)
|
|
|
|
, mInstances(1)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
nsTArray<TelephonyChild> mTelephonyChildren;
|
|
|
|
|
2013-09-17 00:46:06 -07:00
|
|
|
// mPlayableHiddenContentChildID stores the ChildID of the process which can
|
|
|
|
// play content channel(s) in the background.
|
|
|
|
// A background process contained content channel(s) will become playable:
|
|
|
|
// 1. When this background process registers its content channel(s) in
|
|
|
|
// AudioChannelService and there is no foreground process with registered
|
|
|
|
// content channel(s).
|
|
|
|
// 2. When this process goes from foreground into background and there is
|
|
|
|
// no foreground process with registered content channel(s).
|
|
|
|
// A background process contained content channel(s) will become non-playable:
|
|
|
|
// 1. When there is a foreground process registering its content channel(s)
|
|
|
|
// in AudioChannelService.
|
|
|
|
// ps. Currently this condition is never satisfied because the default value
|
|
|
|
// of visibility status of each channel during registering is hidden = true.
|
|
|
|
// 2. When there is a process with registered content channel(s) goes from
|
|
|
|
// background into foreground.
|
|
|
|
// 3. When this process unregisters all hidden content channels.
|
|
|
|
// 4. When this process shuts down.
|
|
|
|
uint64_t mPlayableHiddenContentChildID;
|
2013-01-25 07:12:17 -08:00
|
|
|
|
2013-10-07 23:30:04 -07:00
|
|
|
bool mDisabled;
|
|
|
|
|
2014-06-24 22:15:12 -07:00
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
|
2013-04-28 19:47:15 -07:00
|
|
|
nsCOMPtr<nsITimer> mDeferTelChannelTimer;
|
|
|
|
bool mTimerElementHidden;
|
|
|
|
uint64_t mTimerChildID;
|
|
|
|
|
2013-09-12 05:26:03 -07:00
|
|
|
uint64_t mDefChannelChildID;
|
|
|
|
|
2012-12-05 19:01:58 -08:00
|
|
|
// This is needed for IPC comunication between
|
|
|
|
// AudioChannelServiceChild and this class.
|
|
|
|
friend class ContentParent;
|
|
|
|
friend class ContentChild;
|
2012-12-04 11:46:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|