dom/speakermanager/SpeakerManagerService.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "SpeakerManagerService.h"
michael@0 8 #include "SpeakerManagerServiceChild.h"
michael@0 9 #include "mozilla/Services.h"
michael@0 10 #include "mozilla/StaticPtr.h"
michael@0 11 #include "mozilla/unused.h"
michael@0 12 #include "mozilla/dom/ContentParent.h"
michael@0 13 #include "nsIPropertyBag2.h"
michael@0 14 #include "nsThreadUtils.h"
michael@0 15 #include "nsServiceManagerUtils.h"
michael@0 16 #include "AudioChannelService.h"
michael@0 17 #include <cutils/properties.h>
michael@0 18
michael@0 19 #define NS_AUDIOMANAGER_CONTRACTID "@mozilla.org/telephony/audiomanager;1"
michael@0 20 #include "nsIAudioManager.h"
michael@0 21
michael@0 22 using namespace mozilla;
michael@0 23 using namespace mozilla::dom;
michael@0 24
michael@0 25 StaticRefPtr<SpeakerManagerService> gSpeakerManagerService;
michael@0 26
michael@0 27 // static
michael@0 28 SpeakerManagerService*
michael@0 29 SpeakerManagerService::GetSpeakerManagerService()
michael@0 30 {
michael@0 31 MOZ_ASSERT(NS_IsMainThread());
michael@0 32
michael@0 33 if (XRE_GetProcessType() != GeckoProcessType_Default) {
michael@0 34 return SpeakerManagerServiceChild::GetSpeakerManagerService();
michael@0 35 }
michael@0 36
michael@0 37 // If we already exist, exit early
michael@0 38 if (gSpeakerManagerService) {
michael@0 39 return gSpeakerManagerService;
michael@0 40 }
michael@0 41
michael@0 42 // Create new instance, register, return
michael@0 43 nsRefPtr<SpeakerManagerService> service = new SpeakerManagerService();
michael@0 44 NS_ENSURE_TRUE(service, nullptr);
michael@0 45
michael@0 46 gSpeakerManagerService = service;
michael@0 47 return gSpeakerManagerService;
michael@0 48 }
michael@0 49
michael@0 50 void
michael@0 51 SpeakerManagerService::Shutdown()
michael@0 52 {
michael@0 53 if (XRE_GetProcessType() != GeckoProcessType_Default) {
michael@0 54 return SpeakerManagerServiceChild::Shutdown();
michael@0 55 }
michael@0 56
michael@0 57 if (gSpeakerManagerService) {
michael@0 58 gSpeakerManagerService = nullptr;
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 NS_IMPL_ISUPPORTS(SpeakerManagerService, nsIObserver)
michael@0 63
michael@0 64 void
michael@0 65 SpeakerManagerService::ForceSpeaker(bool aEnable, uint64_t aChildId)
michael@0 66 {
michael@0 67 TuruOnSpeaker(aEnable);
michael@0 68 if (aEnable) {
michael@0 69 mSpeakerStatusSet.Put(aChildId);
michael@0 70 }
michael@0 71 Notify();
michael@0 72 return;
michael@0 73 }
michael@0 74
michael@0 75 void
michael@0 76 SpeakerManagerService::ForceSpeaker(bool aEnable, bool aVisible)
michael@0 77 {
michael@0 78 // b2g main process without oop
michael@0 79 TuruOnSpeaker(aEnable && aVisible);
michael@0 80 mVisible = aVisible;
michael@0 81 mOrgSpeakerStatus = aEnable;
michael@0 82 Notify();
michael@0 83 }
michael@0 84
michael@0 85 void
michael@0 86 SpeakerManagerService::TuruOnSpeaker(bool aOn)
michael@0 87 {
michael@0 88 nsCOMPtr<nsIAudioManager> audioManager = do_GetService(NS_AUDIOMANAGER_CONTRACTID);
michael@0 89 NS_ENSURE_TRUE_VOID(audioManager);
michael@0 90 int32_t phoneState;
michael@0 91 audioManager->GetPhoneState(&phoneState);
michael@0 92 int32_t forceuse = (phoneState == nsIAudioManager::PHONE_STATE_IN_CALL ||
michael@0 93 phoneState == nsIAudioManager::PHONE_STATE_IN_COMMUNICATION)
michael@0 94 ? nsIAudioManager::USE_COMMUNICATION : nsIAudioManager::USE_MEDIA;
michael@0 95 if (aOn) {
michael@0 96 audioManager->SetForceForUse(forceuse, nsIAudioManager::FORCE_SPEAKER);
michael@0 97 } else {
michael@0 98 audioManager->SetForceForUse(forceuse, nsIAudioManager::FORCE_NONE);
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 bool
michael@0 103 SpeakerManagerService::GetSpeakerStatus()
michael@0 104 {
michael@0 105 char propQemu[PROPERTY_VALUE_MAX];
michael@0 106 property_get("ro.kernel.qemu", propQemu, "");
michael@0 107 if (!strncmp(propQemu, "1", 1)) {
michael@0 108 return mOrgSpeakerStatus;
michael@0 109 }
michael@0 110 nsCOMPtr<nsIAudioManager> audioManager = do_GetService(NS_AUDIOMANAGER_CONTRACTID);
michael@0 111 NS_ENSURE_TRUE(audioManager, false);
michael@0 112 int32_t usage;
michael@0 113 audioManager->GetForceForUse(nsIAudioManager::USE_MEDIA, &usage);
michael@0 114 return usage == nsIAudioManager::FORCE_SPEAKER;
michael@0 115 }
michael@0 116
michael@0 117 void
michael@0 118 SpeakerManagerService::Notify()
michael@0 119 {
michael@0 120 // Parent Notify to all the child processes.
michael@0 121 nsTArray<ContentParent*> children;
michael@0 122 ContentParent::GetAll(children);
michael@0 123 for (uint32_t i = 0; i < children.Length(); i++) {
michael@0 124 unused << children[i]->SendSpeakerManagerNotify();
michael@0 125 }
michael@0 126
michael@0 127 for (uint32_t i = 0; i < mRegisteredSpeakerManagers.Length(); i++) {
michael@0 128 mRegisteredSpeakerManagers[i]->
michael@0 129 DispatchSimpleEvent(NS_LITERAL_STRING("speakerforcedchange"));
michael@0 130 }
michael@0 131 }
michael@0 132
michael@0 133 void
michael@0 134 SpeakerManagerService::SetAudioChannelActive(bool aIsActive)
michael@0 135 {
michael@0 136 if (!aIsActive && !mVisible) {
michael@0 137 ForceSpeaker(!mOrgSpeakerStatus, mVisible);
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 NS_IMETHODIMP
michael@0 142 SpeakerManagerService::Observe(nsISupports* aSubject, const char*
michael@0 143 aTopic, const char16_t* aData)
michael@0 144 {
michael@0 145 if (!strcmp(aTopic, "ipc:content-shutdown")) {
michael@0 146 nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
michael@0 147 if (!props) {
michael@0 148 NS_WARNING("ipc:content-shutdown message without property bag as subject");
michael@0 149 return NS_OK;
michael@0 150 }
michael@0 151
michael@0 152 uint64_t childID = 0;
michael@0 153 nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"),
michael@0 154 &childID);
michael@0 155 if (NS_SUCCEEDED(rv)) {
michael@0 156 // If the audio has paused by audiochannel,
michael@0 157 // the enable flag should be false and don't need to handle.
michael@0 158 if (mSpeakerStatusSet.Contains(childID)) {
michael@0 159 TuruOnSpeaker(false);
michael@0 160 mSpeakerStatusSet.Remove(childID);
michael@0 161 }
michael@0 162 if (mOrgSpeakerStatus) {
michael@0 163 TuruOnSpeaker(!mOrgSpeakerStatus);
michael@0 164 mOrgSpeakerStatus = false;
michael@0 165 }
michael@0 166 } else {
michael@0 167 NS_WARNING("ipc:content-shutdown message without childID property");
michael@0 168 }
michael@0 169 }
michael@0 170 return NS_OK;
michael@0 171 }
michael@0 172
michael@0 173 SpeakerManagerService::SpeakerManagerService()
michael@0 174 : mOrgSpeakerStatus(false),
michael@0 175 mVisible(false)
michael@0 176 {
michael@0 177 MOZ_COUNT_CTOR(SpeakerManagerService);
michael@0 178 if (XRE_GetProcessType() == GeckoProcessType_Default) {
michael@0 179 nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
michael@0 180 if (obs) {
michael@0 181 obs->AddObserver(this, "ipc:content-shutdown", false);
michael@0 182 }
michael@0 183 }
michael@0 184 AudioChannelService* audioChannelService =
michael@0 185 AudioChannelService::GetAudioChannelService();
michael@0 186 if (audioChannelService) {
michael@0 187 audioChannelService->RegisterSpeakerManager(this);
michael@0 188 }
michael@0 189 }
michael@0 190
michael@0 191 SpeakerManagerService::~SpeakerManagerService()
michael@0 192 {
michael@0 193 MOZ_COUNT_DTOR(SpeakerManagerService);
michael@0 194 AudioChannelService* audioChannelService =
michael@0 195 AudioChannelService::GetAudioChannelService();
michael@0 196 if (audioChannelService)
michael@0 197 audioChannelService->UnregisterSpeakerManager(this);
michael@0 198 }

mercurial