Bug 1055920 - [B2G][AudioChannel] Avoid to create unnecessary AudioChannel instance on child process. r=baku

This commit is contained in:
Randy Lin 2014-08-29 14:10:16 +08:00
Родитель bcbd8dd34b
Коммит 4b227d8d5a
14 изменённых файлов: 81 добавлений и 50 удалений

Просмотреть файл

@ -114,7 +114,7 @@ AudioChannelAgent::InitInternal(nsIDOMWindow* aWindow, int32_t aChannelType,
/* boolean startPlaying (); */
NS_IMETHODIMP AudioChannelAgent::StartPlaying(int32_t *_retval)
{
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService();
if (mAudioChannelType == AUDIO_AGENT_CHANNEL_ERROR ||
service == nullptr || mIsRegToService) {
return NS_ERROR_FAILURE;
@ -135,7 +135,7 @@ NS_IMETHODIMP AudioChannelAgent::StopPlaying(void)
return NS_ERROR_FAILURE;
}
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService();
service->UnregisterAudioChannelAgent(this);
mIsRegToService = false;
return NS_OK;
@ -150,7 +150,7 @@ NS_IMETHODIMP AudioChannelAgent::SetVisibilityState(bool visible)
mVisible = visible;
if (mIsRegToService && oldVisibility != mVisible && callback) {
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService();
callback->CanPlayChanged(service->GetState(this, !mVisible));
}
return NS_OK;
@ -160,7 +160,7 @@ void AudioChannelAgent::NotifyAudioChannelStateChanged()
{
nsCOMPtr<nsIAudioChannelAgentCallback> callback = GetCallback();
if (callback) {
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService();
callback->CanPlayChanged(service->GetState(this, !mVisible));
}
}

Просмотреть файл

@ -58,6 +58,20 @@ AudioChannelService::GetAudioChannelService()
return AudioChannelServiceChild::GetAudioChannelService();
}
return gAudioChannelService;
}
// static
AudioChannelService*
AudioChannelService::GetOrCreateAudioChannelService()
{
MOZ_ASSERT(NS_IsMainThread());
if (XRE_GetProcessType() != GeckoProcessType_Default) {
return AudioChannelServiceChild::GetOrCreateAudioChannelService();
}
// If we already exist, exit early
if (gAudioChannelService) {
return gAudioChannelService;
@ -65,7 +79,7 @@ AudioChannelService::GetAudioChannelService()
// Create new instance, register, return
nsRefPtr<AudioChannelService> service = new AudioChannelService();
NS_ENSURE_TRUE(service, nullptr);
MOZ_ASSERT(service);
gAudioChannelService = service;
return gAudioChannelService;

Просмотреть файл

@ -36,13 +36,18 @@ public:
NS_DECL_NSITIMERCALLBACK
/**
* Returns the AudioChannelServce singleton. Only to be called from main
* thread.
*
* @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise.
* Returns the AudioChannelServce singleton or null if the process havn't create it before.
* Only to be called from main thread.
*/
static AudioChannelService* GetAudioChannelService();
/**
* Returns the AudioChannelServce singleton.
* If AudioChannelServce is not exist, create and return new one.
* Only to be called from main thread.
*/
static AudioChannelService* GetOrCreateAudioChannelService();
/**
* Shutdown the singleton.
*/

Просмотреть файл

@ -32,6 +32,16 @@ AudioChannelServiceChild::GetAudioChannelService()
{
MOZ_ASSERT(NS_IsMainThread());
return gAudioChannelServiceChild;
}
// static
AudioChannelService*
AudioChannelServiceChild::GetOrCreateAudioChannelService()
{
MOZ_ASSERT(NS_IsMainThread());
// If we already exist, exit early
if (gAudioChannelServiceChild) {
return gAudioChannelServiceChild;
@ -39,7 +49,7 @@ AudioChannelServiceChild::GetAudioChannelService()
// Create new instance, register, return
nsRefPtr<AudioChannelServiceChild> service = new AudioChannelServiceChild();
NS_ENSURE_TRUE(service, nullptr);
MOZ_ASSERT(service);
gAudioChannelServiceChild = service;
return gAudioChannelServiceChild;

Просмотреть файл

@ -21,13 +21,18 @@ class AudioChannelServiceChild : public AudioChannelService
public:
/**
* Returns the AudioChannelServce singleton. Only to be called from main
* thread.
*
* @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise.
* Returns the AudioChannelServce singleton or null if the process havn't create it before.
* Only to be called from main thread.
*/
static AudioChannelService* GetAudioChannelService();
/**
* Returns the AudioChannelServce singleton.
* If AudioChannelServce is not exist, create and return new one.
* Only to be called from main thread.
*/
static AudioChannelService* GetOrCreateAudioChannelService();
static void Shutdown();
virtual void RegisterAudioChannelAgent(AudioChannelAgent* aAgent,

Просмотреть файл

@ -3797,11 +3797,9 @@ void
nsPIDOMWindow::RefreshMediaElements()
{
nsRefPtr<AudioChannelService> service =
AudioChannelService::GetAudioChannelService();
if (service) {
AudioChannelService::GetOrCreateAudioChannelService();
service->RefreshAgentsVolume(this);
}
}
void
nsPIDOMWindow::SendAfterRemotePaintIfRequested()

Просмотреть файл

@ -2352,12 +2352,12 @@ ContentParent::RecvAudioChannelGetState(const AudioChannel& aChannel,
AudioChannelState* aState)
{
nsRefPtr<AudioChannelService> service =
AudioChannelService::GetAudioChannelService();
AudioChannelService::GetOrCreateAudioChannelService();
*aState = AUDIO_CHANNEL_STATE_NORMAL;
if (service) {
MOZ_ASSERT(service);
*aState = service->GetStateInternal(aChannel, mChildID,
aElementHidden, aElementWasHidden);
}
return true;
}
@ -2366,10 +2366,10 @@ ContentParent::RecvAudioChannelRegisterType(const AudioChannel& aChannel,
const bool& aWithVideo)
{
nsRefPtr<AudioChannelService> service =
AudioChannelService::GetAudioChannelService();
if (service) {
AudioChannelService::GetOrCreateAudioChannelService();
MOZ_ASSERT(service);
service->RegisterType(aChannel, mChildID, aWithVideo);
}
return true;
}
@ -2379,10 +2379,10 @@ ContentParent::RecvAudioChannelUnregisterType(const AudioChannel& aChannel,
const bool& aWithVideo)
{
nsRefPtr<AudioChannelService> service =
AudioChannelService::GetAudioChannelService();
if (service) {
AudioChannelService::GetOrCreateAudioChannelService();
MOZ_ASSERT(service);
service->UnregisterType(aChannel, aElementHidden, mChildID, aWithVideo);
}
return true;
}
@ -2390,10 +2390,10 @@ bool
ContentParent::RecvAudioChannelChangedNotification()
{
nsRefPtr<AudioChannelService> service =
AudioChannelService::GetAudioChannelService();
if (service) {
AudioChannelService::GetOrCreateAudioChannelService();
MOZ_ASSERT(service);
service->SendAudioChannelChangedNotification(ChildID());
}
return true;
}
@ -2402,11 +2402,10 @@ ContentParent::RecvAudioChannelChangeDefVolChannel(const int32_t& aChannel,
const bool& aHidden)
{
nsRefPtr<AudioChannelService> service =
AudioChannelService::GetAudioChannelService();
if (service) {
AudioChannelService::GetOrCreateAudioChannelService();
MOZ_ASSERT(service);
service->SetDefaultVolumeControlChannelInternal(aChannel,
aHidden, mChildID);
}
return true;
}

Просмотреть файл

@ -995,7 +995,7 @@ ParticularProcessPriorityManager::ComputePriority()
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
}
AudioChannelService* service = AudioChannelService::GetAudioChannelService();
AudioChannelService* service = AudioChannelService::GetOrCreateAudioChannelService();
if (service->ProcessContentOrNormalChannelIsActive(ChildID())) {
return PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE;
}

Просмотреть файл

@ -201,7 +201,7 @@ SpeakerManager::HandleEvent(nsIDOMEvent* aEvent)
// the background, we switch 'speakerforced' to false.
if (!mVisible && mForcespeaker) {
AudioChannelService* audioChannelService =
AudioChannelService::GetAudioChannelService();
AudioChannelService::GetOrCreateAudioChannelService();
if (audioChannelService && !audioChannelService->AnyAudioChannelIsActive()) {
service->ForceSpeaker(false, mVisible);
}

Просмотреть файл

@ -194,7 +194,7 @@ SpeakerManagerService::SpeakerManagerService()
}
}
AudioChannelService* audioChannelService =
AudioChannelService::GetAudioChannelService();
AudioChannelService::GetOrCreateAudioChannelService();
if (audioChannelService) {
audioChannelService->RegisterSpeakerManager(this);
}
@ -204,7 +204,7 @@ SpeakerManagerService::~SpeakerManagerService()
{
MOZ_COUNT_DTOR(SpeakerManagerService);
AudioChannelService* audioChannelService =
AudioChannelService::GetAudioChannelService();
AudioChannelService::GetOrCreateAudioChannelService();
if (audioChannelService)
audioChannelService->UnregisterSpeakerManager(this);
}

Просмотреть файл

@ -96,7 +96,7 @@ SpeakerManagerServiceChild::SetAudioChannelActive(bool aIsActive)
SpeakerManagerServiceChild::SpeakerManagerServiceChild()
{
MOZ_ASSERT(NS_IsMainThread());
AudioChannelService* audioChannelService = AudioChannelService::GetAudioChannelService();
AudioChannelService* audioChannelService = AudioChannelService::GetOrCreateAudioChannelService();
if (audioChannelService) {
audioChannelService->RegisterSpeakerManager(this);
}
@ -105,7 +105,7 @@ SpeakerManagerServiceChild::SpeakerManagerServiceChild()
SpeakerManagerServiceChild::~SpeakerManagerServiceChild()
{
AudioChannelService* audioChannelService = AudioChannelService::GetAudioChannelService();
AudioChannelService* audioChannelService = AudioChannelService::GetOrCreateAudioChannelService();
if (audioChannelService) {
audioChannelService->UnregisterSpeakerManager(this);
}

Просмотреть файл

@ -132,7 +132,7 @@ AudioChannelManager::NotifyVolumeControlChannelChanged()
bool isActive = false;
docshell->GetIsActive(&isActive);
AudioChannelService* service = AudioChannelService::GetAudioChannelService();
AudioChannelService* service = AudioChannelService::GetOrCreateAudioChannelService();
if (isActive) {
service->SetDefaultVolumeControlChannel(mVolumeChannel, isActive);
} else {

Просмотреть файл

@ -316,8 +316,8 @@ AudioManager::HandleAudioChannelProcessChanged()
return;
}
AudioChannelService *service = AudioChannelService::GetAudioChannelService();
NS_ENSURE_TRUE_VOID(service);
AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService();
MOZ_ASSERT(service);
bool telephonyChannelIsActive = service->TelephonyChannelIsActive();
telephonyChannelIsActive ? SetPhoneState(PHONE_STATE_IN_COMMUNICATION) :

Просмотреть файл

@ -590,7 +590,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(Geolocation, Init)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsGeolocationService, nsGeolocationService::GetGeolocationService)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(AudioChannelService, AudioChannelService::GetAudioChannelService)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(AudioChannelService, AudioChannelService::GetOrCreateAudioChannelService)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(DataStoreService, DataStoreService::GetOrCreate)