michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "AudioChannelServiceChild.h" michael@0: michael@0: #include "base/basictypes.h" michael@0: michael@0: #include "mozilla/Services.h" michael@0: #include "mozilla/StaticPtr.h" michael@0: #include "mozilla/unused.h" michael@0: #include "mozilla/dom/ContentChild.h" michael@0: #include "mozilla/dom/ContentParent.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: #ifdef MOZ_WIDGET_GONK michael@0: #include "SpeakerManagerService.h" michael@0: #endif michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: using namespace mozilla::hal; michael@0: michael@0: StaticRefPtr gAudioChannelServiceChild; michael@0: michael@0: // static michael@0: AudioChannelService* michael@0: AudioChannelServiceChild::GetAudioChannelService() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: // If we already exist, exit early michael@0: if (gAudioChannelServiceChild) { michael@0: return gAudioChannelServiceChild; michael@0: } michael@0: michael@0: // Create new instance, register, return michael@0: nsRefPtr service = new AudioChannelServiceChild(); michael@0: NS_ENSURE_TRUE(service, nullptr); michael@0: michael@0: gAudioChannelServiceChild = service; michael@0: return gAudioChannelServiceChild; michael@0: } michael@0: michael@0: void michael@0: AudioChannelServiceChild::Shutdown() michael@0: { michael@0: if (gAudioChannelServiceChild) { michael@0: gAudioChannelServiceChild = nullptr; michael@0: } michael@0: } michael@0: michael@0: AudioChannelServiceChild::AudioChannelServiceChild() michael@0: { michael@0: } michael@0: michael@0: AudioChannelServiceChild::~AudioChannelServiceChild() michael@0: { michael@0: } michael@0: michael@0: AudioChannelState michael@0: AudioChannelServiceChild::GetState(AudioChannelAgent* aAgent, bool aElementHidden) michael@0: { michael@0: AudioChannelAgentData* data; michael@0: if (!mAgents.Get(aAgent, &data)) { michael@0: return AUDIO_CHANNEL_STATE_MUTED; michael@0: } michael@0: michael@0: AudioChannelState state = AUDIO_CHANNEL_STATE_MUTED; michael@0: bool oldElementHidden = data->mElementHidden; michael@0: michael@0: UpdateChannelType(data->mChannel, CONTENT_PROCESS_ID_MAIN, aElementHidden, michael@0: oldElementHidden); michael@0: michael@0: // Update visibility. michael@0: data->mElementHidden = aElementHidden; michael@0: michael@0: ContentChild* cc = ContentChild::GetSingleton(); michael@0: cc->SendAudioChannelGetState(data->mChannel, aElementHidden, oldElementHidden, michael@0: &state); michael@0: data->mState = state; michael@0: cc->SendAudioChannelChangedNotification(); michael@0: michael@0: return state; michael@0: } michael@0: michael@0: void michael@0: AudioChannelServiceChild::RegisterAudioChannelAgent(AudioChannelAgent* aAgent, michael@0: AudioChannel aChannel, michael@0: bool aWithVideo) michael@0: { michael@0: AudioChannelService::RegisterAudioChannelAgent(aAgent, aChannel, aWithVideo); michael@0: michael@0: ContentChild::GetSingleton()->SendAudioChannelRegisterType(aChannel, aWithVideo); michael@0: michael@0: nsCOMPtr obs = mozilla::services::GetObserverService(); michael@0: if (obs) { michael@0: obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr); michael@0: } michael@0: } michael@0: michael@0: void michael@0: AudioChannelServiceChild::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent) michael@0: { michael@0: AudioChannelAgentData *pData; michael@0: if (!mAgents.Get(aAgent, &pData)) { michael@0: return; michael@0: } michael@0: michael@0: // We need to keep a copy because unregister will remove the michael@0: // AudioChannelAgentData object from the hashtable. michael@0: AudioChannelAgentData data(*pData); michael@0: michael@0: AudioChannelService::UnregisterAudioChannelAgent(aAgent); michael@0: michael@0: ContentChild::GetSingleton()->SendAudioChannelUnregisterType( michael@0: data.mChannel, data.mElementHidden, data.mWithVideo); michael@0: michael@0: nsCOMPtr obs = mozilla::services::GetObserverService(); michael@0: if (obs) { michael@0: obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr); michael@0: } michael@0: #ifdef MOZ_WIDGET_GONK michael@0: bool active = AnyAudioChannelIsActive(); michael@0: for (uint32_t i = 0; i < mSpeakerManager.Length(); i++) { michael@0: mSpeakerManager[i]->SetAudioChannelActive(active); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: AudioChannelServiceChild::SetDefaultVolumeControlChannel(int32_t aChannel, michael@0: bool aHidden) michael@0: { michael@0: ContentChild *cc = ContentChild::GetSingleton(); michael@0: if (cc) { michael@0: cc->SendAudioChannelChangeDefVolChannel(aChannel, aHidden); michael@0: } michael@0: }