1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/speakermanager/SpeakerManagerService.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,198 @@ 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 "SpeakerManagerService.h" 1.11 +#include "SpeakerManagerServiceChild.h" 1.12 +#include "mozilla/Services.h" 1.13 +#include "mozilla/StaticPtr.h" 1.14 +#include "mozilla/unused.h" 1.15 +#include "mozilla/dom/ContentParent.h" 1.16 +#include "nsIPropertyBag2.h" 1.17 +#include "nsThreadUtils.h" 1.18 +#include "nsServiceManagerUtils.h" 1.19 +#include "AudioChannelService.h" 1.20 +#include <cutils/properties.h> 1.21 + 1.22 +#define NS_AUDIOMANAGER_CONTRACTID "@mozilla.org/telephony/audiomanager;1" 1.23 +#include "nsIAudioManager.h" 1.24 + 1.25 +using namespace mozilla; 1.26 +using namespace mozilla::dom; 1.27 + 1.28 +StaticRefPtr<SpeakerManagerService> gSpeakerManagerService; 1.29 + 1.30 +// static 1.31 +SpeakerManagerService* 1.32 +SpeakerManagerService::GetSpeakerManagerService() 1.33 +{ 1.34 + MOZ_ASSERT(NS_IsMainThread()); 1.35 + 1.36 + if (XRE_GetProcessType() != GeckoProcessType_Default) { 1.37 + return SpeakerManagerServiceChild::GetSpeakerManagerService(); 1.38 + } 1.39 + 1.40 + // If we already exist, exit early 1.41 + if (gSpeakerManagerService) { 1.42 + return gSpeakerManagerService; 1.43 + } 1.44 + 1.45 + // Create new instance, register, return 1.46 + nsRefPtr<SpeakerManagerService> service = new SpeakerManagerService(); 1.47 + NS_ENSURE_TRUE(service, nullptr); 1.48 + 1.49 + gSpeakerManagerService = service; 1.50 + return gSpeakerManagerService; 1.51 +} 1.52 + 1.53 +void 1.54 +SpeakerManagerService::Shutdown() 1.55 +{ 1.56 + if (XRE_GetProcessType() != GeckoProcessType_Default) { 1.57 + return SpeakerManagerServiceChild::Shutdown(); 1.58 + } 1.59 + 1.60 + if (gSpeakerManagerService) { 1.61 + gSpeakerManagerService = nullptr; 1.62 + } 1.63 +} 1.64 + 1.65 +NS_IMPL_ISUPPORTS(SpeakerManagerService, nsIObserver) 1.66 + 1.67 +void 1.68 +SpeakerManagerService::ForceSpeaker(bool aEnable, uint64_t aChildId) 1.69 +{ 1.70 + TuruOnSpeaker(aEnable); 1.71 + if (aEnable) { 1.72 + mSpeakerStatusSet.Put(aChildId); 1.73 + } 1.74 + Notify(); 1.75 + return; 1.76 +} 1.77 + 1.78 +void 1.79 +SpeakerManagerService::ForceSpeaker(bool aEnable, bool aVisible) 1.80 +{ 1.81 + // b2g main process without oop 1.82 + TuruOnSpeaker(aEnable && aVisible); 1.83 + mVisible = aVisible; 1.84 + mOrgSpeakerStatus = aEnable; 1.85 + Notify(); 1.86 +} 1.87 + 1.88 +void 1.89 +SpeakerManagerService::TuruOnSpeaker(bool aOn) 1.90 +{ 1.91 + nsCOMPtr<nsIAudioManager> audioManager = do_GetService(NS_AUDIOMANAGER_CONTRACTID); 1.92 + NS_ENSURE_TRUE_VOID(audioManager); 1.93 + int32_t phoneState; 1.94 + audioManager->GetPhoneState(&phoneState); 1.95 + int32_t forceuse = (phoneState == nsIAudioManager::PHONE_STATE_IN_CALL || 1.96 + phoneState == nsIAudioManager::PHONE_STATE_IN_COMMUNICATION) 1.97 + ? nsIAudioManager::USE_COMMUNICATION : nsIAudioManager::USE_MEDIA; 1.98 + if (aOn) { 1.99 + audioManager->SetForceForUse(forceuse, nsIAudioManager::FORCE_SPEAKER); 1.100 + } else { 1.101 + audioManager->SetForceForUse(forceuse, nsIAudioManager::FORCE_NONE); 1.102 + } 1.103 +} 1.104 + 1.105 +bool 1.106 +SpeakerManagerService::GetSpeakerStatus() 1.107 +{ 1.108 + char propQemu[PROPERTY_VALUE_MAX]; 1.109 + property_get("ro.kernel.qemu", propQemu, ""); 1.110 + if (!strncmp(propQemu, "1", 1)) { 1.111 + return mOrgSpeakerStatus; 1.112 + } 1.113 + nsCOMPtr<nsIAudioManager> audioManager = do_GetService(NS_AUDIOMANAGER_CONTRACTID); 1.114 + NS_ENSURE_TRUE(audioManager, false); 1.115 + int32_t usage; 1.116 + audioManager->GetForceForUse(nsIAudioManager::USE_MEDIA, &usage); 1.117 + return usage == nsIAudioManager::FORCE_SPEAKER; 1.118 +} 1.119 + 1.120 +void 1.121 +SpeakerManagerService::Notify() 1.122 +{ 1.123 + // Parent Notify to all the child processes. 1.124 + nsTArray<ContentParent*> children; 1.125 + ContentParent::GetAll(children); 1.126 + for (uint32_t i = 0; i < children.Length(); i++) { 1.127 + unused << children[i]->SendSpeakerManagerNotify(); 1.128 + } 1.129 + 1.130 + for (uint32_t i = 0; i < mRegisteredSpeakerManagers.Length(); i++) { 1.131 + mRegisteredSpeakerManagers[i]-> 1.132 + DispatchSimpleEvent(NS_LITERAL_STRING("speakerforcedchange")); 1.133 + } 1.134 +} 1.135 + 1.136 +void 1.137 +SpeakerManagerService::SetAudioChannelActive(bool aIsActive) 1.138 +{ 1.139 + if (!aIsActive && !mVisible) { 1.140 + ForceSpeaker(!mOrgSpeakerStatus, mVisible); 1.141 + } 1.142 +} 1.143 + 1.144 +NS_IMETHODIMP 1.145 +SpeakerManagerService::Observe(nsISupports* aSubject, const char* 1.146 + aTopic, const char16_t* aData) 1.147 +{ 1.148 + if (!strcmp(aTopic, "ipc:content-shutdown")) { 1.149 + nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject); 1.150 + if (!props) { 1.151 + NS_WARNING("ipc:content-shutdown message without property bag as subject"); 1.152 + return NS_OK; 1.153 + } 1.154 + 1.155 + uint64_t childID = 0; 1.156 + nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"), 1.157 + &childID); 1.158 + if (NS_SUCCEEDED(rv)) { 1.159 + // If the audio has paused by audiochannel, 1.160 + // the enable flag should be false and don't need to handle. 1.161 + if (mSpeakerStatusSet.Contains(childID)) { 1.162 + TuruOnSpeaker(false); 1.163 + mSpeakerStatusSet.Remove(childID); 1.164 + } 1.165 + if (mOrgSpeakerStatus) { 1.166 + TuruOnSpeaker(!mOrgSpeakerStatus); 1.167 + mOrgSpeakerStatus = false; 1.168 + } 1.169 + } else { 1.170 + NS_WARNING("ipc:content-shutdown message without childID property"); 1.171 + } 1.172 + } 1.173 + return NS_OK; 1.174 +} 1.175 + 1.176 +SpeakerManagerService::SpeakerManagerService() 1.177 + : mOrgSpeakerStatus(false), 1.178 + mVisible(false) 1.179 +{ 1.180 + MOZ_COUNT_CTOR(SpeakerManagerService); 1.181 + if (XRE_GetProcessType() == GeckoProcessType_Default) { 1.182 + nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); 1.183 + if (obs) { 1.184 + obs->AddObserver(this, "ipc:content-shutdown", false); 1.185 + } 1.186 + } 1.187 + AudioChannelService* audioChannelService = 1.188 + AudioChannelService::GetAudioChannelService(); 1.189 + if (audioChannelService) { 1.190 + audioChannelService->RegisterSpeakerManager(this); 1.191 + } 1.192 +} 1.193 + 1.194 +SpeakerManagerService::~SpeakerManagerService() 1.195 +{ 1.196 + MOZ_COUNT_DTOR(SpeakerManagerService); 1.197 + AudioChannelService* audioChannelService = 1.198 + AudioChannelService::GetAudioChannelService(); 1.199 + if (audioChannelService) 1.200 + audioChannelService->UnregisterSpeakerManager(this); 1.201 +}