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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_audiochannelservice_h__
|
|
|
|
#define mozilla_dom_audiochannelservice_h__
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
#include "nsIAudioChannelService.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2012-12-28 21:57:35 +04:00
|
|
|
#include "nsIObserver.h"
|
2013-03-17 11:55:16 +04:00
|
|
|
#include "nsTArray.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2012-12-06 19:25:18 +04:00
|
|
|
#include "AudioChannelAgent.h"
|
2014-04-10 21:39:20 +04:00
|
|
|
#include "nsAttrValue.h"
|
2015-07-11 15:13:50 +03:00
|
|
|
#include "nsClassHashtable.h"
|
2014-03-20 14:45:55 +04:00
|
|
|
#include "mozilla/dom/AudioChannelBinding.h"
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2014-06-25 09:15:12 +04:00
|
|
|
class nsIRunnable;
|
2014-03-11 14:46:55 +04:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-11-25 03:50:03 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
class SpeakerManagerService;
|
|
|
|
#endif
|
2015-07-10 19:38:44 +03:00
|
|
|
|
|
|
|
#define NUMBER_OF_AUDIO_CHANNELS (uint32_t)AudioChannel::Publicnotification + 1
|
|
|
|
|
|
|
|
class AudioChannelService final : public nsIAudioChannelService
|
|
|
|
, public nsIObserver
|
2012-12-04 23:46:07 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2012-12-28 21:57:35 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
2015-07-10 19:38:44 +03:00
|
|
|
NS_DECL_NSIAUDIOCHANNELSERVICE
|
2012-12-04 23:46:07 +04:00
|
|
|
|
2014-08-29 10:10:16 +04:00
|
|
|
/**
|
|
|
|
* Returns the AudioChannelServce singleton.
|
|
|
|
* If AudioChannelServce is not exist, create and return new one.
|
|
|
|
* Only to be called from main thread.
|
|
|
|
*/
|
2015-07-10 19:38:44 +03:00
|
|
|
static already_AddRefed<AudioChannelService> GetOrCreate();
|
2014-08-29 10:10:16 +04:00
|
|
|
|
2012-12-04 23:46:07 +04:00
|
|
|
/**
|
|
|
|
* Shutdown the singleton.
|
|
|
|
*/
|
|
|
|
static void Shutdown();
|
|
|
|
|
|
|
|
/**
|
2012-12-06 19:25:18 +04:00
|
|
|
* Any audio channel agent that starts playing should register itself to
|
2014-04-10 21:39:20 +04:00
|
|
|
* this service, sharing the AudioChannel.
|
2012-12-04 23:46:07 +04:00
|
|
|
*/
|
2015-07-10 19:38:44 +03:00
|
|
|
void RegisterAudioChannelAgent(AudioChannelAgent* aAgent, AudioChannel aChannel);
|
2012-12-04 23:46:07 +04:00
|
|
|
|
|
|
|
/**
|
2013-09-12 16:26:03 +04:00
|
|
|
* Any audio channel agent that stops playing should unregister itself to
|
2012-12-04 23:46:07 +04:00
|
|
|
* this service.
|
|
|
|
*/
|
2015-07-10 19:38:44 +03:00
|
|
|
void UnregisterAudioChannelAgent(AudioChannelAgent* aAgent);
|
2012-12-04 23:46:07 +04:00
|
|
|
|
|
|
|
/**
|
2015-07-10 19:38:44 +03:00
|
|
|
* Return the state to indicate this audioChannel for his window should keep
|
|
|
|
* playing/muted.
|
2012-12-04 23:46:07 +04:00
|
|
|
*/
|
2015-07-10 19:38:44 +03:00
|
|
|
void GetState(nsPIDOMWindow* aWindow, uint32_t aChannel,
|
|
|
|
float* aVolume, bool* aMuted);
|
2015-07-10 19:38:44 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
/* Methods for the BrowserElementAudioChannel */
|
|
|
|
float GetAudioChannelVolume(nsPIDOMWindow* aWindow, AudioChannel aChannel);
|
|
|
|
|
|
|
|
void SetAudioChannelVolume(nsPIDOMWindow* aWindow, AudioChannel aChannel,
|
|
|
|
float aVolume);
|
|
|
|
|
|
|
|
bool GetAudioChannelMuted(nsPIDOMWindow* aWindow, AudioChannel aChannel);
|
|
|
|
|
|
|
|
void SetAudioChannelMuted(nsPIDOMWindow* aWindow, AudioChannel aChannel,
|
|
|
|
bool aMuted);
|
|
|
|
|
|
|
|
bool IsAudioChannelActive(nsPIDOMWindow* aWindow, AudioChannel aChannel);
|
2013-01-05 09:03:51 +04:00
|
|
|
|
2014-05-29 11:20:42 +04:00
|
|
|
/**
|
|
|
|
* Return true if there is a telephony channel active in this process
|
|
|
|
* or one of its subprocesses.
|
|
|
|
*/
|
2015-07-10 19:38:44 +03:00
|
|
|
bool TelephonyChannelIsActive();
|
2014-05-29 11:20:42 +04:00
|
|
|
|
2013-04-26 04:53:26 +04:00
|
|
|
/**
|
2013-09-02 13:45:44 +04:00
|
|
|
* Return true if a normal or content channel is active for the given
|
|
|
|
* process ID.
|
2013-04-26 04:53:26 +04:00
|
|
|
*/
|
2015-07-10 19:38:44 +03:00
|
|
|
bool ProcessContentOrNormalChannelIsActive(uint64_t aChildID);
|
2013-04-26 04:53:26 +04:00
|
|
|
|
2013-09-12 16:26:03 +04:00
|
|
|
/***
|
|
|
|
* AudioChannelManager calls this function to notify the default channel used
|
2014-04-10 21:39:20 +04: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 16:26:03 +04:00
|
|
|
*/
|
2014-04-10 21:39:20 +04:00
|
|
|
virtual void SetDefaultVolumeControlChannel(int32_t aChannel,
|
2015-02-25 13:46:11 +03:00
|
|
|
bool aVisible);
|
2013-09-12 16:26:03 +04:00
|
|
|
|
2013-11-25 03:50:03 +04:00
|
|
|
bool AnyAudioChannelIsActive();
|
|
|
|
|
2014-03-11 14:46:55 +04:00
|
|
|
void RefreshAgentsVolume(nsPIDOMWindow* aWindow);
|
|
|
|
|
2013-11-25 03:50:03 +04: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 14:45:55 +04:00
|
|
|
|
2014-04-10 21:39:20 +04:00
|
|
|
static const nsAttrValue::EnumTable* GetAudioChannelTable();
|
|
|
|
static AudioChannel GetAudioChannel(const nsAString& aString);
|
2014-03-20 14:45:55 +04:00
|
|
|
static AudioChannel GetDefaultAudioChannel();
|
2014-04-10 21:39:20 +04:00
|
|
|
static void GetAudioChannelString(AudioChannel aChannel, nsAString& aString);
|
|
|
|
static void GetDefaultAudioChannelString(nsAString& aString);
|
2014-03-20 14:45:55 +04:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
void Notify(uint64_t aWindowID);
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
void ChildStatusReceived(uint64_t aChildID, bool aTelephonyChannel,
|
|
|
|
bool aContentOrNormalChannel, bool aAnyChannel);
|
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
private:
|
|
|
|
AudioChannelService();
|
|
|
|
~AudioChannelService();
|
2015-07-10 19:38:46 +03:00
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
void MaybeSendStatusUpdate();
|
|
|
|
|
|
|
|
bool ContentOrNormalChannelIsActive();
|
|
|
|
|
2013-09-12 16:26:03 +04:00
|
|
|
/* Send the default-volume-channel-changed notification */
|
2014-04-10 21:39:20 +04:00
|
|
|
void SetDefaultVolumeControlChannelInternal(int32_t aChannel,
|
2015-02-25 13:46:11 +03:00
|
|
|
bool aVisible, uint64_t aChildID);
|
2013-09-12 16:26:03 +04:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
struct AudioChannelConfig final
|
|
|
|
{
|
|
|
|
AudioChannelConfig()
|
|
|
|
: mVolume(1.0)
|
|
|
|
, mMuted(false)
|
|
|
|
, mNumberOfAgents(0)
|
|
|
|
{}
|
2013-01-11 02:56:20 +04:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
float mVolume;
|
|
|
|
bool mMuted;
|
2015-07-10 19:38:51 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
uint32_t mNumberOfAgents;
|
|
|
|
};
|
2015-07-10 19:38:46 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
struct AudioChannelWindow final
|
|
|
|
{
|
|
|
|
AudioChannelConfig mChannels[NUMBER_OF_AUDIO_CHANNELS];
|
|
|
|
nsClassHashtable<nsPtrHashKey<AudioChannelAgent>, AudioChannel> mAgents;
|
|
|
|
};
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannelWindow&
|
|
|
|
GetOrCreateWindowData(nsPIDOMWindow* aWindow);
|
2015-07-11 15:14:58 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
static PLDHashOperator
|
|
|
|
TelephonyChannelIsActiveEnumerator(const uint64_t& aWindowID,
|
|
|
|
nsAutoPtr<AudioChannelWindow>& aWinData,
|
|
|
|
void *aPtr);
|
2015-07-11 15:13:50 +03:00
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
static PLDHashOperator
|
|
|
|
ContentOrNormalChannelIsActiveEnumerator(
|
|
|
|
const uint64_t& aWindowID,
|
|
|
|
nsAutoPtr<AudioChannelWindow>& aWinData,
|
|
|
|
void *aPtr);
|
|
|
|
|
2015-07-11 15:13:50 +03:00
|
|
|
static PLDHashOperator
|
2015-07-10 19:38:44 +03:00
|
|
|
AnyAudioChannelIsActiveEnumerator(const uint64_t& aWindowID,
|
|
|
|
nsAutoPtr<AudioChannelWindow>& aWinData,
|
|
|
|
void *aPtr);
|
2015-07-11 15:13:50 +03:00
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
RefreshAgentsVolumeEnumerator(AudioChannelAgent* aAgent,
|
2015-07-10 19:38:44 +03:00
|
|
|
AudioChannel* aUnused,
|
2015-07-11 15:13:50 +03:00
|
|
|
void *aPtr);
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2015-07-10 19:38:44 +03:00
|
|
|
NotifyEnumerator(AudioChannelAgent* aAgent,
|
|
|
|
AudioChannel* aAudioChannel,
|
|
|
|
void* aUnused);
|
2015-07-11 15:13:50 +03:00
|
|
|
|
2015-07-10 19:38:44 +03:00
|
|
|
nsClassHashtable<nsUint64HashKey, AudioChannelWindow> mWindows;
|
2015-07-11 15:13:50 +03:00
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
struct AudioChannelChildStatus final {
|
|
|
|
AudioChannelChildStatus()
|
|
|
|
: mActiveTelephonyChannel(false)
|
|
|
|
, mActiveContentOrNormalChannel(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool mActiveTelephonyChannel;
|
|
|
|
bool mActiveContentOrNormalChannel;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
TelephonyChannelIsActiveInChildrenEnumerator(
|
|
|
|
const uint64_t& aChildID,
|
|
|
|
nsAutoPtr<AudioChannelChildStatus>& aData,
|
|
|
|
void *aPtr);
|
|
|
|
|
|
|
|
nsClassHashtable<nsUint64HashKey, AudioChannelChildStatus> mPlayingChildren;
|
|
|
|
|
2013-11-25 03:50:03 +04:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
nsTArray<SpeakerManagerService*> mSpeakerManager;
|
|
|
|
#endif
|
2013-01-25 19:12:17 +04:00
|
|
|
|
2013-10-08 10:30:04 +04:00
|
|
|
bool mDisabled;
|
|
|
|
|
2014-06-25 09:15:12 +04:00
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
|
2015-07-11 03:55:59 +03:00
|
|
|
uint64_t mDefChannelChildID;
|
2015-07-10 19:38:46 +03:00
|
|
|
|
2015-07-10 19:38:46 +03:00
|
|
|
// These boolean are used to know if we have to send an status update to the
|
|
|
|
// service running in the main process.
|
|
|
|
bool mTelephonyChannel;
|
|
|
|
bool mContentOrNormalChannel;
|
|
|
|
bool mAnyChannel;
|
|
|
|
|
2012-12-06 07:01:58 +04:00
|
|
|
// This is needed for IPC comunication between
|
|
|
|
// AudioChannelServiceChild and this class.
|
|
|
|
friend class ContentParent;
|
|
|
|
friend class ContentChild;
|
2012-12-04 23:46:07 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|