1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/audiochannel/AudioChannelServiceChild.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "AudioChannelServiceChild.h" 1.11 + 1.12 +#include "base/basictypes.h" 1.13 + 1.14 +#include "mozilla/Services.h" 1.15 +#include "mozilla/StaticPtr.h" 1.16 +#include "mozilla/unused.h" 1.17 +#include "mozilla/dom/ContentChild.h" 1.18 +#include "mozilla/dom/ContentParent.h" 1.19 +#include "nsIObserverService.h" 1.20 +#include "nsThreadUtils.h" 1.21 + 1.22 +#ifdef MOZ_WIDGET_GONK 1.23 +#include "SpeakerManagerService.h" 1.24 +#endif 1.25 + 1.26 +using namespace mozilla; 1.27 +using namespace mozilla::dom; 1.28 +using namespace mozilla::hal; 1.29 + 1.30 +StaticRefPtr<AudioChannelServiceChild> gAudioChannelServiceChild; 1.31 + 1.32 +// static 1.33 +AudioChannelService* 1.34 +AudioChannelServiceChild::GetAudioChannelService() 1.35 +{ 1.36 + MOZ_ASSERT(NS_IsMainThread()); 1.37 + 1.38 + // If we already exist, exit early 1.39 + if (gAudioChannelServiceChild) { 1.40 + return gAudioChannelServiceChild; 1.41 + } 1.42 + 1.43 + // Create new instance, register, return 1.44 + nsRefPtr<AudioChannelServiceChild> service = new AudioChannelServiceChild(); 1.45 + NS_ENSURE_TRUE(service, nullptr); 1.46 + 1.47 + gAudioChannelServiceChild = service; 1.48 + return gAudioChannelServiceChild; 1.49 +} 1.50 + 1.51 +void 1.52 +AudioChannelServiceChild::Shutdown() 1.53 +{ 1.54 + if (gAudioChannelServiceChild) { 1.55 + gAudioChannelServiceChild = nullptr; 1.56 + } 1.57 +} 1.58 + 1.59 +AudioChannelServiceChild::AudioChannelServiceChild() 1.60 +{ 1.61 +} 1.62 + 1.63 +AudioChannelServiceChild::~AudioChannelServiceChild() 1.64 +{ 1.65 +} 1.66 + 1.67 +AudioChannelState 1.68 +AudioChannelServiceChild::GetState(AudioChannelAgent* aAgent, bool aElementHidden) 1.69 +{ 1.70 + AudioChannelAgentData* data; 1.71 + if (!mAgents.Get(aAgent, &data)) { 1.72 + return AUDIO_CHANNEL_STATE_MUTED; 1.73 + } 1.74 + 1.75 + AudioChannelState state = AUDIO_CHANNEL_STATE_MUTED; 1.76 + bool oldElementHidden = data->mElementHidden; 1.77 + 1.78 + UpdateChannelType(data->mChannel, CONTENT_PROCESS_ID_MAIN, aElementHidden, 1.79 + oldElementHidden); 1.80 + 1.81 + // Update visibility. 1.82 + data->mElementHidden = aElementHidden; 1.83 + 1.84 + ContentChild* cc = ContentChild::GetSingleton(); 1.85 + cc->SendAudioChannelGetState(data->mChannel, aElementHidden, oldElementHidden, 1.86 + &state); 1.87 + data->mState = state; 1.88 + cc->SendAudioChannelChangedNotification(); 1.89 + 1.90 + return state; 1.91 +} 1.92 + 1.93 +void 1.94 +AudioChannelServiceChild::RegisterAudioChannelAgent(AudioChannelAgent* aAgent, 1.95 + AudioChannel aChannel, 1.96 + bool aWithVideo) 1.97 +{ 1.98 + AudioChannelService::RegisterAudioChannelAgent(aAgent, aChannel, aWithVideo); 1.99 + 1.100 + ContentChild::GetSingleton()->SendAudioChannelRegisterType(aChannel, aWithVideo); 1.101 + 1.102 + nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); 1.103 + if (obs) { 1.104 + obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr); 1.105 + } 1.106 +} 1.107 + 1.108 +void 1.109 +AudioChannelServiceChild::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent) 1.110 +{ 1.111 + AudioChannelAgentData *pData; 1.112 + if (!mAgents.Get(aAgent, &pData)) { 1.113 + return; 1.114 + } 1.115 + 1.116 + // We need to keep a copy because unregister will remove the 1.117 + // AudioChannelAgentData object from the hashtable. 1.118 + AudioChannelAgentData data(*pData); 1.119 + 1.120 + AudioChannelService::UnregisterAudioChannelAgent(aAgent); 1.121 + 1.122 + ContentChild::GetSingleton()->SendAudioChannelUnregisterType( 1.123 + data.mChannel, data.mElementHidden, data.mWithVideo); 1.124 + 1.125 + nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); 1.126 + if (obs) { 1.127 + obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr); 1.128 + } 1.129 +#ifdef MOZ_WIDGET_GONK 1.130 + bool active = AnyAudioChannelIsActive(); 1.131 + for (uint32_t i = 0; i < mSpeakerManager.Length(); i++) { 1.132 + mSpeakerManager[i]->SetAudioChannelActive(active); 1.133 + } 1.134 +#endif 1.135 +} 1.136 + 1.137 +void 1.138 +AudioChannelServiceChild::SetDefaultVolumeControlChannel(int32_t aChannel, 1.139 + bool aHidden) 1.140 +{ 1.141 + ContentChild *cc = ContentChild::GetSingleton(); 1.142 + if (cc) { 1.143 + cc->SendAudioChannelChangeDefVolChannel(aChannel, aHidden); 1.144 + } 1.145 +}