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

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

@ -58,6 +58,20 @@ AudioChannelService::GetAudioChannelService()
return AudioChannelServiceChild::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 we already exist, exit early
if (gAudioChannelService) { if (gAudioChannelService) {
return gAudioChannelService; return gAudioChannelService;
@ -65,7 +79,7 @@ AudioChannelService::GetAudioChannelService()
// Create new instance, register, return // Create new instance, register, return
nsRefPtr<AudioChannelService> service = new AudioChannelService(); nsRefPtr<AudioChannelService> service = new AudioChannelService();
NS_ENSURE_TRUE(service, nullptr); MOZ_ASSERT(service);
gAudioChannelService = service; gAudioChannelService = service;
return gAudioChannelService; return gAudioChannelService;

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

@ -36,13 +36,18 @@ public:
NS_DECL_NSITIMERCALLBACK NS_DECL_NSITIMERCALLBACK
/** /**
* Returns the AudioChannelServce singleton. Only to be called from main * Returns the AudioChannelServce singleton or null if the process havn't create it before.
* thread. * Only to be called from main thread.
*
* @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise.
*/ */
static AudioChannelService* GetAudioChannelService(); 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. * Shutdown the singleton.
*/ */

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

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

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

@ -21,13 +21,18 @@ class AudioChannelServiceChild : public AudioChannelService
public: public:
/** /**
* Returns the AudioChannelServce singleton. Only to be called from main * Returns the AudioChannelServce singleton or null if the process havn't create it before.
* thread. * Only to be called from main thread.
*
* @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise.
*/ */
static AudioChannelService* GetAudioChannelService(); 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(); static void Shutdown();
virtual void RegisterAudioChannelAgent(AudioChannelAgent* aAgent, virtual void RegisterAudioChannelAgent(AudioChannelAgent* aAgent,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -316,8 +316,8 @@ AudioManager::HandleAudioChannelProcessChanged()
return; return;
} }
AudioChannelService *service = AudioChannelService::GetAudioChannelService(); AudioChannelService *service = AudioChannelService::GetOrCreateAudioChannelService();
NS_ENSURE_TRUE_VOID(service); MOZ_ASSERT(service);
bool telephonyChannelIsActive = service->TelephonyChannelIsActive(); bool telephonyChannelIsActive = service->TelephonyChannelIsActive();
telephonyChannelIsActive ? SetPhoneState(PHONE_STATE_IN_COMMUNICATION) : 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(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) NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(DataStoreService, DataStoreService::GetOrCreate)