Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "AudioPolicyService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 20 | #include "Configuration.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 21 | #undef __STRICT_ANSI__ |
| 22 | #define __STDINT_LIMITS |
| 23 | #define __STDC_LIMIT_MACROS |
| 24 | #include <stdint.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 25 | #include <sys/time.h> |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 26 | #include <dlfcn.h> |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 27 | |
| 28 | #include <audio_utils/clock.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 29 | #include <binder/IServiceManager.h> |
| 30 | #include <utils/Log.h> |
| 31 | #include <cutils/properties.h> |
| 32 | #include <binder/IPCThreadState.h> |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 33 | #include <binder/PermissionController.h> |
| 34 | #include <binder/IResultReceiver.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | #include <utils/String16.h> |
| 36 | #include <utils/threads.h> |
| 37 | #include "AudioPolicyService.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | #include <hardware_legacy/power.h> |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 39 | #include <media/AidlConversion.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 40 | #include <media/AudioEffect.h> |
Chih-Hung Hsieh | c84d9d2 | 2014-11-14 13:33:34 -0800 | [diff] [blame] | 41 | #include <media/AudioParameter.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 42 | #include <mediautils/ServiceUtilities.h> |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 43 | #include <mediautils/TimeCheck.h> |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 44 | #include <sensorprivacy/SensorPrivacyManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 46 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 47 | #include <system/audio_policy.h> |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 48 | #include <AudioPolicyManager.h> |
Mikhail Naganov | 61a4fac | 2016-10-13 14:44:18 -0700 | [diff] [blame] | 49 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | namespace android { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 51 | using binder::Status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 53 | static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; |
| 54 | static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 55 | static const char kAudioPolicyManagerCustomPath[] = "libaudiopolicymanagercustom.so"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 57 | static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 59 | static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 60 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 61 | static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY"); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 62 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | // ---------------------------------------------------------------------------- |
| 64 | |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 65 | static AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 66 | { |
| 67 | AudioPolicyManager *apm = new AudioPolicyManager(clientInterface); |
| 68 | status_t status = apm->initialize(); |
| 69 | if (status != NO_ERROR) { |
| 70 | delete apm; |
| 71 | apm = nullptr; |
| 72 | } |
| 73 | return apm; |
| 74 | } |
| 75 | |
| 76 | static void destroyAudioPolicyManager(AudioPolicyInterface *interface) |
| 77 | { |
| 78 | delete interface; |
| 79 | } |
| 80 | // ---------------------------------------------------------------------------- |
| 81 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 82 | AudioPolicyService::AudioPolicyService() |
Ytai Ben-Tsvi | 85093d5 | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 83 | : BnAudioPolicyService(), |
Ytai Ben-Tsvi | 85093d5 | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 84 | mAudioPolicyManager(NULL), |
| 85 | mAudioPolicyClient(NULL), |
| 86 | mPhoneState(AUDIO_MODE_INVALID), |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 87 | mCaptureStateNotifier(false), |
| 88 | mCreateAudioPolicyManager(createAudioPolicyManager), |
| 89 | mDestroyAudioPolicyManager(destroyAudioPolicyManager) { |
| 90 | } |
| 91 | |
| 92 | void AudioPolicyService::loadAudioPolicyManager() |
| 93 | { |
| 94 | mLibraryHandle = dlopen(kAudioPolicyManagerCustomPath, RTLD_NOW); |
| 95 | if (mLibraryHandle != nullptr) { |
| 96 | ALOGI("%s loading %s", __func__, kAudioPolicyManagerCustomPath); |
| 97 | mCreateAudioPolicyManager = reinterpret_cast<CreateAudioPolicyManagerInstance> |
| 98 | (dlsym(mLibraryHandle, "createAudioPolicyManager")); |
| 99 | const char *lastError = dlerror(); |
| 100 | ALOGW_IF(mCreateAudioPolicyManager == nullptr, "%s createAudioPolicyManager is null %s", |
| 101 | __func__, lastError != nullptr ? lastError : "no error"); |
| 102 | |
| 103 | mDestroyAudioPolicyManager = reinterpret_cast<DestroyAudioPolicyManagerInstance>( |
| 104 | dlsym(mLibraryHandle, "destroyAudioPolicyManager")); |
| 105 | lastError = dlerror(); |
| 106 | ALOGW_IF(mDestroyAudioPolicyManager == nullptr, "%s destroyAudioPolicyManager is null %s", |
| 107 | __func__, lastError != nullptr ? lastError : "no error"); |
| 108 | if (mCreateAudioPolicyManager == nullptr || mDestroyAudioPolicyManager == nullptr){ |
| 109 | unloadAudioPolicyManager(); |
| 110 | LOG_ALWAYS_FATAL("could not find audiopolicymanager interface methods"); |
| 111 | } |
| 112 | } |
Eric Laurent | f5ada6e | 2014-10-09 17:49:00 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void AudioPolicyService::onFirstRef() |
| 116 | { |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 117 | { |
| 118 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 119 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 120 | // start audio commands thread |
| 121 | mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this); |
| 122 | // start output activity command thread |
| 123 | mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 124 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 125 | mAudioPolicyClient = new AudioPolicyClient(this); |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 126 | |
| 127 | loadAudioPolicyManager(); |
| 128 | mAudioPolicyManager = mCreateAudioPolicyManager(mAudioPolicyClient); |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 129 | } |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 130 | |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 131 | // load audio processing modules |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 132 | sp<AudioPolicyEffects> audioPolicyEffects = new AudioPolicyEffects(); |
| 133 | sp<UidPolicy> uidPolicy = new UidPolicy(this); |
| 134 | sp<SensorPrivacyPolicy> sensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 135 | { |
| 136 | Mutex::Autolock _l(mLock); |
| 137 | mAudioPolicyEffects = audioPolicyEffects; |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 138 | mUidPolicy = uidPolicy; |
| 139 | mSensorPrivacyPolicy = sensorPrivacyPolicy; |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 140 | } |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 141 | uidPolicy->registerSelf(); |
| 142 | sensorPrivacyPolicy->registerSelf(); |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 143 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 144 | // Create spatializer if supported |
Eric Laurent | 52b0bd5 | 2021-09-27 15:25:40 +0200 | [diff] [blame] | 145 | if (mAudioPolicyManager != nullptr) { |
| 146 | Mutex::Autolock _l(mLock); |
| 147 | const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 148 | AudioDeviceTypeAddrVector devices; |
| 149 | bool hasSpatializer = mAudioPolicyManager->canBeSpatialized(&attr, nullptr, devices); |
| 150 | if (hasSpatializer) { |
| 151 | mSpatializer = Spatializer::create(this); |
| 152 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 153 | } |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 154 | AudioSystem::audioPolicyReady(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 157 | void AudioPolicyService::unloadAudioPolicyManager() |
| 158 | { |
| 159 | ALOGV("%s ", __func__); |
| 160 | if (mLibraryHandle != nullptr) { |
| 161 | dlclose(mLibraryHandle); |
| 162 | } |
| 163 | mLibraryHandle = nullptr; |
| 164 | mCreateAudioPolicyManager = nullptr; |
| 165 | mDestroyAudioPolicyManager = nullptr; |
| 166 | } |
| 167 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 168 | AudioPolicyService::~AudioPolicyService() |
| 169 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 170 | mAudioCommandThread->exit(); |
Eric Laurent | 657ff61 | 2014-05-07 11:58:24 -0700 | [diff] [blame] | 171 | mOutputCommandThread->exit(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 172 | |
Jaideep Sharma | ba9053b | 2021-01-25 21:24:26 +0530 | [diff] [blame] | 173 | mDestroyAudioPolicyManager(mAudioPolicyManager); |
| 174 | unloadAudioPolicyManager(); |
| 175 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 176 | delete mAudioPolicyClient; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 177 | |
| 178 | mNotificationClients.clear(); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 179 | mAudioPolicyEffects.clear(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 180 | |
| 181 | mUidPolicy->unregisterSelf(); |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 182 | mSensorPrivacyPolicy->unregisterSelf(); |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 183 | |
| 184 | mUidPolicy.clear(); |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 185 | mSensorPrivacyPolicy.clear(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // A notification client is always registered by AudioSystem when the client process |
| 189 | // connects to AudioPolicyService. |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 190 | Status AudioPolicyService::registerClient(const sp<media::IAudioPolicyServiceClient>& client) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 191 | { |
Eric Laurent | 1259025 | 2015-08-21 18:40:20 -0700 | [diff] [blame] | 192 | if (client == 0) { |
| 193 | ALOGW("%s got NULL client", __FUNCTION__); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 194 | return Status::ok(); |
Eric Laurent | 1259025 | 2015-08-21 18:40:20 -0700 | [diff] [blame] | 195 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 196 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 197 | |
| 198 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 199 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 200 | int64_t token = ((int64_t)uid<<32) | pid; |
| 201 | |
| 202 | if (mNotificationClients.indexOfKey(token) < 0) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 203 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 204 | client, |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 205 | uid, |
| 206 | pid); |
| 207 | ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 208 | |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 209 | mNotificationClients.add(token, notificationClient); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 210 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 211 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 212 | binder->linkToDeath(notificationClient); |
| 213 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 214 | return Status::ok(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 217 | Status AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled) |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 218 | { |
| 219 | Mutex::Autolock _l(mNotificationClientsLock); |
| 220 | |
| 221 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 222 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 223 | int64_t token = ((int64_t)uid<<32) | pid; |
| 224 | |
| 225 | if (mNotificationClients.indexOfKey(token) < 0) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 226 | return Status::ok(); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 227 | } |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 228 | mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 229 | return Status::ok(); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 232 | Status AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled) |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 233 | { |
| 234 | Mutex::Autolock _l(mNotificationClientsLock); |
| 235 | |
| 236 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 237 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 238 | int64_t token = ((int64_t)uid<<32) | pid; |
| 239 | |
| 240 | if (mNotificationClients.indexOfKey(token) < 0) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 241 | return Status::ok(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 242 | } |
| 243 | mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 244 | return Status::ok(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 247 | // removeNotificationClient() is called when the client process dies. |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 248 | void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 249 | { |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 250 | bool hasSameUid = false; |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 251 | { |
| 252 | Mutex::Autolock _l(mNotificationClientsLock); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 253 | int64_t token = ((int64_t)uid<<32) | pid; |
| 254 | mNotificationClients.removeItem(token); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 255 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 256 | if (mNotificationClients.valueAt(i)->uid() == uid) { |
| 257 | hasSameUid = true; |
| 258 | break; |
| 259 | } |
| 260 | } |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 261 | } |
| 262 | { |
| 263 | Mutex::Autolock _l(mLock); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 264 | if (mAudioPolicyManager && !hasSameUid) { |
Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 265 | // called from binder death notification: no need to clear caller identity |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 266 | mAudioPolicyManager->releaseResourcesForUid(uid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 267 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 268 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void AudioPolicyService::onAudioPortListUpdate() |
| 272 | { |
| 273 | mOutputCommandThread->updateAudioPortListCommand(); |
| 274 | } |
| 275 | |
| 276 | void AudioPolicyService::doOnAudioPortListUpdate() |
| 277 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 278 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 279 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 280 | mNotificationClients.valueAt(i)->onAudioPortListUpdate(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void AudioPolicyService::onAudioPatchListUpdate() |
| 285 | { |
| 286 | mOutputCommandThread->updateAudioPatchListCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 289 | void AudioPolicyService::doOnAudioPatchListUpdate() |
| 290 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 291 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 292 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 293 | mNotificationClients.valueAt(i)->onAudioPatchListUpdate(); |
| 294 | } |
| 295 | } |
| 296 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 297 | void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags) |
| 298 | { |
| 299 | mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags); |
| 300 | } |
| 301 | |
| 302 | void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags) |
| 303 | { |
| 304 | Mutex::Autolock _l(mNotificationClientsLock); |
| 305 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 306 | mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags); |
| 307 | } |
| 308 | } |
| 309 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 310 | void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 311 | { |
| 312 | ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)", |
| 313 | regId.string(), state); |
| 314 | mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state); |
| 315 | } |
| 316 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 317 | void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 318 | { |
| 319 | Mutex::Autolock _l(mNotificationClientsLock); |
| 320 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 321 | mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state); |
| 322 | } |
| 323 | } |
| 324 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 325 | void AudioPolicyService::onRecordingConfigurationUpdate( |
| 326 | int event, |
| 327 | const record_client_info_t *clientInfo, |
| 328 | const audio_config_base_t *clientConfig, |
| 329 | std::vector<effect_descriptor_t> clientEffects, |
| 330 | const audio_config_base_t *deviceConfig, |
| 331 | std::vector<effect_descriptor_t> effects, |
| 332 | audio_patch_handle_t patchHandle, |
| 333 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 334 | { |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 335 | mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 336 | clientConfig, clientEffects, deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 339 | void AudioPolicyService::doOnRecordingConfigurationUpdate( |
| 340 | int event, |
| 341 | const record_client_info_t *clientInfo, |
| 342 | const audio_config_base_t *clientConfig, |
| 343 | std::vector<effect_descriptor_t> clientEffects, |
| 344 | const audio_config_base_t *deviceConfig, |
| 345 | std::vector<effect_descriptor_t> effects, |
| 346 | audio_patch_handle_t patchHandle, |
| 347 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 348 | { |
| 349 | Mutex::Autolock _l(mNotificationClientsLock); |
| 350 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 351 | mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 352 | clientConfig, clientEffects, deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 356 | void AudioPolicyService::onRoutingUpdated() |
| 357 | { |
| 358 | mOutputCommandThread->routingChangedCommand(); |
| 359 | } |
| 360 | |
| 361 | void AudioPolicyService::doOnRoutingUpdated() |
| 362 | { |
| 363 | Mutex::Autolock _l(mNotificationClientsLock); |
| 364 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 365 | mNotificationClients.valueAt(i)->onRoutingUpdated(); |
| 366 | } |
| 367 | } |
| 368 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 369 | void AudioPolicyService::onCheckSpatializer() |
| 370 | { |
| 371 | Mutex::Autolock _l(mLock); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 372 | onCheckSpatializer_l(); |
| 373 | } |
| 374 | |
| 375 | void AudioPolicyService::onCheckSpatializer_l() |
| 376 | { |
| 377 | if (mSpatializer != nullptr) { |
| 378 | mOutputCommandThread->checkSpatializerCommand(); |
| 379 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void AudioPolicyService::doOnCheckSpatializer() |
| 383 | { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 384 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 385 | |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 386 | if (mSpatializer != nullptr) { |
Eric Laurent | 52b0bd5 | 2021-09-27 15:25:40 +0200 | [diff] [blame] | 387 | // Note: mSpatializer != nullptr => mAudioPolicyManager != nullptr |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 388 | if (mSpatializer->getLevel() != media::SpatializationLevel::NONE) { |
| 389 | audio_io_handle_t currentOutput = mSpatializer->getOutput(); |
| 390 | audio_io_handle_t newOutput; |
| 391 | const audio_attributes_t attr = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 392 | audio_config_base_t config = mSpatializer->getAudioInConfig(); |
| 393 | status_t status = |
| 394 | mAudioPolicyManager->getSpatializerOutput(&config, &attr, &newOutput); |
| 395 | |
| 396 | if (status == NO_ERROR && currentOutput == newOutput) { |
| 397 | return; |
| 398 | } |
| 399 | mLock.unlock(); |
| 400 | // It is OK to call detachOutput() is none is already attached. |
| 401 | mSpatializer->detachOutput(); |
| 402 | if (status != NO_ERROR || newOutput == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 403 | mLock.lock(); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | status = mSpatializer->attachOutput(newOutput); |
| 407 | mLock.lock(); |
| 408 | if (status != NO_ERROR) { |
| 409 | mAudioPolicyManager->releaseSpatializerOutput(newOutput); |
| 410 | } |
| 411 | } else if (mSpatializer->getLevel() == media::SpatializationLevel::NONE |
| 412 | && mSpatializer->getOutput() != AUDIO_IO_HANDLE_NONE) { |
| 413 | mLock.unlock(); |
| 414 | audio_io_handle_t output = mSpatializer->detachOutput(); |
| 415 | mLock.lock(); |
| 416 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 417 | mAudioPolicyManager->releaseSpatializerOutput(output); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 423 | status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch, |
| 424 | audio_patch_handle_t *handle, |
| 425 | int delayMs) |
| 426 | { |
| 427 | return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs); |
| 428 | } |
| 429 | |
| 430 | status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle, |
| 431 | int delayMs) |
| 432 | { |
| 433 | return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs); |
| 434 | } |
| 435 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 436 | status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config, |
| 437 | int delayMs) |
| 438 | { |
| 439 | return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs); |
| 440 | } |
| 441 | |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 442 | AudioPolicyService::NotificationClient::NotificationClient( |
| 443 | const sp<AudioPolicyService>& service, |
| 444 | const sp<media::IAudioPolicyServiceClient>& client, |
| 445 | uid_t uid, |
| 446 | pid_t pid) |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 447 | : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client), |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 448 | mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 449 | { |
| 450 | } |
| 451 | |
| 452 | AudioPolicyService::NotificationClient::~NotificationClient() |
| 453 | { |
| 454 | } |
| 455 | |
| 456 | void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
| 457 | { |
| 458 | sp<NotificationClient> keep(this); |
| 459 | sp<AudioPolicyService> service = mService.promote(); |
| 460 | if (service != 0) { |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 461 | service->removeNotificationClient(mUid, mPid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | void AudioPolicyService::NotificationClient::onAudioPortListUpdate() |
| 466 | { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 467 | if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 468 | mAudioPolicyServiceClient->onAudioPortListUpdate(); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void AudioPolicyService::NotificationClient::onAudioPatchListUpdate() |
| 473 | { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 474 | if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 475 | mAudioPolicyServiceClient->onAudioPatchListUpdate(); |
| 476 | } |
| 477 | } |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 478 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 479 | void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group, |
| 480 | int flags) |
| 481 | { |
| 482 | if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) { |
| 483 | mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 488 | void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 489 | const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 490 | { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 491 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 492 | mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate( |
| 493 | legacy2aidl_String8_string(regId).value(), state); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 498 | int event, |
| 499 | const record_client_info_t *clientInfo, |
| 500 | const audio_config_base_t *clientConfig, |
| 501 | std::vector<effect_descriptor_t> clientEffects, |
| 502 | const audio_config_base_t *deviceConfig, |
| 503 | std::vector<effect_descriptor_t> effects, |
| 504 | audio_patch_handle_t patchHandle, |
| 505 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 506 | { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 507 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 508 | status_t status = [&]() -> status_t { |
| 509 | int32_t eventAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(event)); |
| 510 | media::RecordClientInfo clientInfoAidl = VALUE_OR_RETURN_STATUS( |
| 511 | legacy2aidl_record_client_info_t_RecordClientInfo(*clientInfo)); |
| 512 | media::AudioConfigBase clientConfigAidl = VALUE_OR_RETURN_STATUS( |
| 513 | legacy2aidl_audio_config_base_t_AudioConfigBase(*clientConfig)); |
| 514 | std::vector<media::EffectDescriptor> clientEffectsAidl = VALUE_OR_RETURN_STATUS( |
| 515 | convertContainer<std::vector<media::EffectDescriptor>>( |
| 516 | clientEffects, |
| 517 | legacy2aidl_effect_descriptor_t_EffectDescriptor)); |
| 518 | media::AudioConfigBase deviceConfigAidl = VALUE_OR_RETURN_STATUS( |
| 519 | legacy2aidl_audio_config_base_t_AudioConfigBase(*deviceConfig)); |
| 520 | std::vector<media::EffectDescriptor> effectsAidl = VALUE_OR_RETURN_STATUS( |
| 521 | convertContainer<std::vector<media::EffectDescriptor>>( |
| 522 | effects, |
| 523 | legacy2aidl_effect_descriptor_t_EffectDescriptor)); |
| 524 | int32_t patchHandleAidl = VALUE_OR_RETURN_STATUS( |
| 525 | legacy2aidl_audio_patch_handle_t_int32_t(patchHandle)); |
| 526 | media::AudioSourceType sourceAidl = VALUE_OR_RETURN_STATUS( |
| 527 | legacy2aidl_audio_source_t_AudioSourceType(source)); |
| 528 | return aidl_utils::statusTFromBinderStatus( |
| 529 | mAudioPolicyServiceClient->onRecordingConfigurationUpdate(eventAidl, |
| 530 | clientInfoAidl, |
| 531 | clientConfigAidl, |
| 532 | clientEffectsAidl, |
| 533 | deviceConfigAidl, |
| 534 | effectsAidl, |
| 535 | patchHandleAidl, |
| 536 | sourceAidl)); |
| 537 | }(); |
| 538 | ALOGW_IF(status != OK, "onRecordingConfigurationUpdate() failed: %d", status); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 542 | void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled) |
| 543 | { |
| 544 | mAudioPortCallbacksEnabled = enabled; |
| 545 | } |
| 546 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 547 | void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled) |
| 548 | { |
| 549 | mAudioVolumeGroupCallbacksEnabled = enabled; |
| 550 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 551 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 552 | void AudioPolicyService::NotificationClient::onRoutingUpdated() |
| 553 | { |
| 554 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
| 555 | mAudioPolicyServiceClient->onRoutingUpdated(); |
| 556 | } |
| 557 | } |
| 558 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 559 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 560 | ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 561 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 564 | static bool dumpTryLock(Mutex& mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 565 | { |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 566 | return mutex.timedLock(kDumpLockTimeoutNs) == NO_ERROR; |
| 567 | } |
| 568 | |
| 569 | static void dumpReleaseLock(Mutex& mutex, bool locked) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS |
| 570 | { |
| 571 | if (locked) mutex.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | status_t AudioPolicyService::dumpInternals(int fd) |
| 575 | { |
| 576 | const size_t SIZE = 256; |
| 577 | char buffer[SIZE]; |
| 578 | String8 result; |
| 579 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 580 | snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 581 | result.append(buffer); |
| 582 | snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); |
| 583 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 584 | |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 585 | snprintf(buffer, SIZE, "Supported System Usages:\n"); |
| 586 | result.append(buffer); |
| 587 | for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin(); |
| 588 | it != mSupportedSystemUsages.end(); ++it) { |
| 589 | snprintf(buffer, SIZE, "\t%d\n", *it); |
| 590 | result.append(buffer); |
| 591 | } |
| 592 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 593 | write(fd, result.string(), result.size()); |
| 594 | return NO_ERROR; |
| 595 | } |
| 596 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 597 | void AudioPolicyService::updateUidStates() |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 598 | { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 599 | Mutex::Autolock _l(mLock); |
| 600 | updateUidStates_l(); |
| 601 | } |
| 602 | |
| 603 | void AudioPolicyService::updateUidStates_l() |
| 604 | { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 605 | // Go over all active clients and allow capture (does not force silence) in the |
| 606 | // following cases: |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 607 | // The client is the assistant |
| 608 | // AND an accessibility service is on TOP or a RTT call is active |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 609 | // AND the source is VOICE_RECOGNITION or HOTWORD |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 610 | // OR uses VOICE_RECOGNITION AND is on TOP |
| 611 | // OR uses HOTWORD |
| 612 | // AND there is no active privacy sensitive capture or call |
| 613 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 614 | // OR The client is an accessibility service |
| 615 | // AND Is on TOP |
| 616 | // AND the source is VOICE_RECOGNITION or HOTWORD |
| 617 | // OR The assistant is not on TOP |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 618 | // AND there is no active privacy sensitive capture or call |
| 619 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 620 | // AND is on TOP |
| 621 | // AND the source is VOICE_RECOGNITION or HOTWORD |
| 622 | // OR the client source is virtual (remote submix, call audio TX or RX...) |
| 623 | // OR the client source is HOTWORD |
| 624 | // AND is on TOP |
| 625 | // OR all active clients are using HOTWORD source |
| 626 | // AND no call is active |
| 627 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 628 | // OR the client is the current InputMethodService |
| 629 | // AND a RTT call is active AND the source is VOICE_RECOGNITION |
| 630 | // OR Any client |
| 631 | // AND The assistant is not on TOP |
| 632 | // AND is on TOP or latest started |
| 633 | // AND there is no active privacy sensitive capture or call |
| 634 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 635 | |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 636 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 637 | sp<AudioRecordClient> topActive; |
| 638 | sp<AudioRecordClient> latestActive; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 639 | sp<AudioRecordClient> topSensitiveActive; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 640 | sp<AudioRecordClient> latestSensitiveActiveOrComm; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 641 | |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 642 | nsecs_t topStartNs = 0; |
| 643 | nsecs_t latestStartNs = 0; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 644 | nsecs_t topSensitiveStartNs = 0; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 645 | nsecs_t latestSensitiveStartNs = 0; |
| 646 | bool isA11yOnTop = mUidPolicy->isA11yOnTop(); |
| 647 | bool isAssistantOnTop = false; |
| 648 | bool isSensitiveActive = false; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 649 | bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 650 | bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION; |
| 651 | bool rttCallActive = (isInCall || isInCommunication) |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 652 | && mUidPolicy->isRttEnabled(); |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 653 | bool onlyHotwordActive = true; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 654 | bool isPhoneStateOwnerActive = false; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 655 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 656 | // if Sensor Privacy is enabled then all recordings should be silenced. |
| 657 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 658 | silenceAllRecordings_l(); |
| 659 | return; |
| 660 | } |
| 661 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 662 | for (size_t i =0; i < mAudioRecordClients.size(); i++) { |
| 663 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 664 | uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
| 665 | current->attributionSource.uid)); |
Evan Severson | 1f700cd | 2021-02-10 13:10:37 -0800 | [diff] [blame] | 666 | if (!current->active) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 667 | continue; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 668 | } |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 669 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 670 | app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(currentUid)); |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 671 | // clients which app is in IDLE state are not eligible for top active or |
| 672 | // latest active |
| 673 | if (appState == APP_STATE_IDLE) { |
| 674 | continue; |
| 675 | } |
| 676 | |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 677 | bool isAccessibility = mUidPolicy->isA11yUid(currentUid); |
Eric Laurent | 14a8863 | 2020-07-16 12:28:30 -0700 | [diff] [blame] | 678 | // Clients capturing for Accessibility services or virtual sources are not considered |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 679 | // for top or latest active to avoid masking regular clients started before |
Eric Laurent | 14a8863 | 2020-07-16 12:28:30 -0700 | [diff] [blame] | 680 | if (!isAccessibility && !isVirtualSource(current->attributes.source)) { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 681 | bool isAssistant = mUidPolicy->isAssistantUid(currentUid); |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 682 | bool isPrivacySensitive = |
| 683 | (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 684 | |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 685 | if (appState == APP_STATE_TOP) { |
| 686 | if (isPrivacySensitive) { |
| 687 | if (current->startTimeNs > topSensitiveStartNs) { |
| 688 | topSensitiveActive = current; |
| 689 | topSensitiveStartNs = current->startTimeNs; |
| 690 | } |
| 691 | } else { |
| 692 | if (current->startTimeNs > topStartNs) { |
| 693 | topActive = current; |
| 694 | topStartNs = current->startTimeNs; |
| 695 | } |
| 696 | } |
| 697 | if (isAssistant) { |
| 698 | isAssistantOnTop = true; |
| 699 | } |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 700 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 701 | // Clients capturing for HOTWORD are not considered |
| 702 | // for latest active to avoid masking regular clients started before |
| 703 | if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD |
| 704 | || ((isA11yOnTop || rttCallActive) && isAssistant))) { |
| 705 | if (isPrivacySensitive) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 706 | // if audio mode is IN_COMMUNICATION, make sure the audio mode owner |
| 707 | // is marked latest sensitive active even if another app qualifies. |
| 708 | if (current->startTimeNs > latestSensitiveStartNs |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 709 | || (isInCommunication && currentUid == mPhoneStateOwnerUid)) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 710 | if (!isInCommunication || latestSensitiveActiveOrComm == nullptr |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 711 | || VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 712 | latestSensitiveActiveOrComm->attributionSource.uid)) |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 713 | != mPhoneStateOwnerUid) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 714 | latestSensitiveActiveOrComm = current; |
| 715 | latestSensitiveStartNs = current->startTimeNs; |
| 716 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 717 | } |
| 718 | isSensitiveActive = true; |
| 719 | } else { |
| 720 | if (current->startTimeNs > latestStartNs) { |
| 721 | latestActive = current; |
| 722 | latestStartNs = current->startTimeNs; |
| 723 | } |
| 724 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 725 | } |
| 726 | } |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 727 | if (current->attributes.source != AUDIO_SOURCE_HOTWORD) { |
| 728 | onlyHotwordActive = false; |
| 729 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 730 | if (currentUid == mPhoneStateOwnerUid) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 731 | isPhoneStateOwnerActive = true; |
| 732 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 735 | // if no active client with UI on Top, consider latest active as top |
| 736 | if (topActive == nullptr) { |
| 737 | topActive = latestActive; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 738 | topStartNs = latestStartNs; |
| 739 | } |
| 740 | if (topSensitiveActive == nullptr) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 741 | topSensitiveActive = latestSensitiveActiveOrComm; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 742 | topSensitiveStartNs = latestSensitiveStartNs; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 743 | } else if (latestSensitiveActiveOrComm != nullptr) { |
| 744 | // if audio mode is IN_COMMUNICATION, favor audio mode owner over an app with |
| 745 | // foreground UI in case both are capturing with privacy sensitive flag. |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 746 | uid_t latestActiveUid = VALUE_OR_FATAL( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 747 | aidl2legacy_int32_t_uid_t(latestSensitiveActiveOrComm->attributionSource.uid)); |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 748 | if (isInCommunication && latestActiveUid == mPhoneStateOwnerUid) { |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 749 | topSensitiveActive = latestSensitiveActiveOrComm; |
| 750 | topSensitiveStartNs = latestSensitiveStartNs; |
| 751 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // If both privacy sensitive and regular capture are active: |
| 755 | // if the regular capture is privileged |
| 756 | // allow concurrency |
| 757 | // else |
| 758 | // favor the privacy sensitive case |
| 759 | if (topActive != nullptr && topSensitiveActive != nullptr |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 760 | && !topActive->canCaptureOutput) { |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 761 | topActive = nullptr; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | for (size_t i =0; i < mAudioRecordClients.size(); i++) { |
| 765 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 766 | uid_t currentUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 767 | current->attributionSource.uid)); |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 768 | if (!current->active) { |
| 769 | continue; |
| 770 | } |
| 771 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 772 | audio_source_t source = current->attributes.source; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 773 | bool isTopOrLatestActive = topActive == nullptr ? false : |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 774 | current->attributionSource.uid == topActive->attributionSource.uid; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 775 | bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? false : |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 776 | current->attributionSource.uid == topSensitiveActive->attributionSource.uid; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 777 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 778 | auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) REQUIRES(mLock) { |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 779 | uid_t recordUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t( |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 780 | recordClient->attributionSource.uid)); |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 781 | bool canCaptureCall = recordClient->canCaptureOutput; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 782 | bool canCaptureCommunication = recordClient->canCaptureOutput |
| 783 | || !isPhoneStateOwnerActive |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 784 | || recordUid == mPhoneStateOwnerUid; |
Eric Laurent | b809a75 | 2020-06-29 09:53:13 -0700 | [diff] [blame] | 785 | return !(isInCall && !canCaptureCall) |
| 786 | && !(isInCommunication && !canCaptureCommunication); |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 787 | }; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 788 | |
| 789 | // By default allow capture if: |
| 790 | // The assistant is not on TOP |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 791 | // AND is on TOP or latest started |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 792 | // AND there is no active privacy sensitive capture or call |
| 793 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 794 | bool allowCapture = !isAssistantOnTop |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 795 | && (isTopOrLatestActive || isTopOrLatestSensitive) |
| 796 | && !(isSensitiveActive |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 797 | && !(isTopOrLatestSensitive || current->canCaptureOutput)) |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 798 | && canCaptureIfInCallOrCommunication(current); |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 799 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 800 | if (!current->hasOp()) { |
| 801 | // Never allow capture if app op is denied |
| 802 | allowCapture = false; |
| 803 | } else if (isVirtualSource(source)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 804 | // Allow capture for virtual (remote submix, call audio TX or RX...) sources |
| 805 | allowCapture = true; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 806 | } else if (mUidPolicy->isAssistantUid(currentUid)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 807 | // For assistant allow capture if: |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 808 | // An accessibility service is on TOP or a RTT call is active |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 809 | // AND the source is VOICE_RECOGNITION or HOTWORD |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 810 | // OR is on TOP AND uses VOICE_RECOGNITION |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 811 | // OR uses HOTWORD |
| 812 | // AND there is no active privacy sensitive capture or call |
| 813 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 814 | if (isA11yOnTop || rttCallActive) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 815 | if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 816 | allowCapture = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 817 | } |
| 818 | } else { |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 819 | if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 820 | source == AUDIO_SOURCE_HOTWORD) |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 821 | && !(isSensitiveActive && !current->canCaptureOutput) |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 822 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 823 | allowCapture = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 824 | } |
| 825 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 826 | } else if (mUidPolicy->isA11yUid(currentUid)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 827 | // For accessibility service allow capture if: |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 828 | // The assistant is not on TOP |
| 829 | // AND there is no active privacy sensitive capture or call |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 830 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 831 | // OR |
| 832 | // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD |
| 833 | if (!isAssistantOnTop |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 834 | && !(isSensitiveActive && !current->canCaptureOutput) |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 835 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 836 | allowCapture = true; |
| 837 | } |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 838 | if (isA11yOnTop) { |
| 839 | if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) { |
| 840 | allowCapture = true; |
| 841 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 842 | } |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 843 | } else if (source == AUDIO_SOURCE_HOTWORD) { |
| 844 | // For HOTWORD source allow capture when not on TOP if: |
| 845 | // All active clients are using HOTWORD source |
| 846 | // AND no call is active |
| 847 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 848 | if (onlyHotwordActive |
| 849 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 850 | allowCapture = true; |
| 851 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 852 | } else if (mUidPolicy->isCurrentImeUid(currentUid)) { |
Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 853 | // For current InputMethodService allow capture if: |
| 854 | // A RTT call is active AND the source is VOICE_RECOGNITION |
| 855 | if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
| 856 | allowCapture = true; |
| 857 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 858 | } |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 859 | setAppState_l(current, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 860 | allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(currentUid)) : |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 861 | APP_STATE_IDLE); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 865 | void AudioPolicyService::silenceAllRecordings_l() { |
| 866 | for (size_t i = 0; i < mAudioRecordClients.size(); i++) { |
| 867 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 868 | if (!isVirtualSource(current->attributes.source)) { |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 869 | setAppState_l(current, APP_STATE_IDLE); |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 870 | } |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 874 | /* static */ |
| 875 | app_state_t AudioPolicyService::apmStatFromAmState(int amState) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 876 | |
| 877 | if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 878 | return APP_STATE_IDLE; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 879 | } else if (amState <= ActivityManager::PROCESS_STATE_TOP) { |
| 880 | // include persistent services |
| 881 | return APP_STATE_TOP; |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 882 | } |
| 883 | return APP_STATE_FOREGROUND; |
| 884 | } |
| 885 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 886 | /* static */ |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 887 | bool AudioPolicyService::isVirtualSource(audio_source_t source) |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 888 | { |
| 889 | switch (source) { |
| 890 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 891 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 892 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 893 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 894 | case AUDIO_SOURCE_FM_TUNER: |
Eric Laurent | 68eb212 | 2020-04-30 17:40:57 -0700 | [diff] [blame] | 895 | case AUDIO_SOURCE_ECHO_REFERENCE: |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 896 | return true; |
| 897 | default: |
| 898 | break; |
| 899 | } |
| 900 | return false; |
| 901 | } |
| 902 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 903 | /* static */ |
| 904 | bool AudioPolicyService::isAppOpSource(audio_source_t source) |
| 905 | { |
| 906 | switch (source) { |
| 907 | case AUDIO_SOURCE_FM_TUNER: |
| 908 | case AUDIO_SOURCE_ECHO_REFERENCE: |
| 909 | return false; |
| 910 | default: |
| 911 | break; |
| 912 | } |
| 913 | return true; |
| 914 | } |
| 915 | |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 916 | void AudioPolicyService::setAppState_l(sp<AudioRecordClient> client, app_state_t state) |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 917 | { |
| 918 | AutoCallerClear acc; |
| 919 | |
| 920 | if (mAudioPolicyManager) { |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 921 | mAudioPolicyManager->setAppState(client->portId, state); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 922 | } |
| 923 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 924 | if (af) { |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 925 | bool silenced = state == APP_STATE_IDLE; |
Eric Laurent | 8c7ef89 | 2021-06-10 13:32:16 +0200 | [diff] [blame] | 926 | if (client->silenced != silenced) { |
| 927 | if (client->active) { |
| 928 | if (silenced) { |
| 929 | finishRecording(client->attributionSource, client->attributes.source); |
| 930 | } else { |
| 931 | std::stringstream msg; |
| 932 | msg << "Audio recording un-silenced on session " << client->session; |
| 933 | if (!startRecording(client->attributionSource, String16(msg.str().c_str()), |
| 934 | client->attributes.source)) { |
| 935 | silenced = true; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | af->setRecordSilenced(client->portId, silenced); |
| 940 | client->silenced = silenced; |
| 941 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 942 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 945 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 946 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 947 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 948 | dumpPermissionDenial(fd); |
| 949 | } else { |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 950 | const bool locked = dumpTryLock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 951 | if (!locked) { |
| 952 | String8 result(kDeadlockedString); |
| 953 | write(fd, result.string(), result.size()); |
| 954 | } |
| 955 | |
| 956 | dumpInternals(fd); |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 957 | if (mAudioCommandThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 958 | mAudioCommandThread->dump(fd); |
| 959 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 960 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 961 | if (mAudioPolicyManager) { |
| 962 | mAudioPolicyManager->dump(fd); |
| 963 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 964 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 965 | mPackageManager.dump(fd); |
| 966 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 967 | dumpReleaseLock(mLock, locked); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 968 | } |
| 969 | return NO_ERROR; |
| 970 | } |
| 971 | |
| 972 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
| 973 | { |
| 974 | const size_t SIZE = 256; |
| 975 | char buffer[SIZE]; |
| 976 | String8 result; |
| 977 | snprintf(buffer, SIZE, "Permission Denial: " |
| 978 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 979 | IPCThreadState::self()->getCallingPid(), |
| 980 | IPCThreadState::self()->getCallingUid()); |
| 981 | result.append(buffer); |
| 982 | write(fd, result.string(), result.size()); |
| 983 | return NO_ERROR; |
| 984 | } |
| 985 | |
| 986 | status_t AudioPolicyService::onTransact( |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 987 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 988 | // make sure transactions reserved to AudioFlinger do not come from other processes |
| 989 | switch (code) { |
| 990 | case TRANSACTION_startOutput: |
| 991 | case TRANSACTION_stopOutput: |
| 992 | case TRANSACTION_releaseOutput: |
| 993 | case TRANSACTION_getInputForAttr: |
| 994 | case TRANSACTION_startInput: |
| 995 | case TRANSACTION_stopInput: |
| 996 | case TRANSACTION_releaseInput: |
| 997 | case TRANSACTION_getOutputForEffect: |
| 998 | case TRANSACTION_registerEffect: |
| 999 | case TRANSACTION_unregisterEffect: |
| 1000 | case TRANSACTION_setEffectEnabled: |
| 1001 | case TRANSACTION_getStrategyForStream: |
| 1002 | case TRANSACTION_getOutputForAttr: |
| 1003 | case TRANSACTION_moveEffectsToIo: |
| 1004 | ALOGW("%s: transaction %d received from PID %d", |
| 1005 | __func__, code, IPCThreadState::self()->getCallingPid()); |
| 1006 | return INVALID_OPERATION; |
| 1007 | default: |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | // make sure the following transactions come from system components |
| 1012 | switch (code) { |
| 1013 | case TRANSACTION_setDeviceConnectionState: |
| 1014 | case TRANSACTION_handleDeviceConfigChange: |
| 1015 | case TRANSACTION_setPhoneState: |
| 1016 | //FIXME: Allow setForceUse calls from system apps until a better use case routing API is available |
| 1017 | // case TRANSACTION_setForceUse: |
| 1018 | case TRANSACTION_initStreamVolume: |
| 1019 | case TRANSACTION_setStreamVolumeIndex: |
| 1020 | case TRANSACTION_setVolumeIndexForAttributes: |
| 1021 | case TRANSACTION_getStreamVolumeIndex: |
| 1022 | case TRANSACTION_getVolumeIndexForAttributes: |
| 1023 | case TRANSACTION_getMinVolumeIndexForAttributes: |
| 1024 | case TRANSACTION_getMaxVolumeIndexForAttributes: |
| 1025 | case TRANSACTION_isStreamActive: |
| 1026 | case TRANSACTION_isStreamActiveRemotely: |
| 1027 | case TRANSACTION_isSourceActive: |
| 1028 | case TRANSACTION_getDevicesForStream: |
| 1029 | case TRANSACTION_registerPolicyMixes: |
| 1030 | case TRANSACTION_setMasterMono: |
| 1031 | case TRANSACTION_getSurroundFormats: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1032 | case TRANSACTION_getReportedSurroundFormats: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1033 | case TRANSACTION_setSurroundFormatEnabled: |
| 1034 | case TRANSACTION_setAssistantUid: |
| 1035 | case TRANSACTION_setA11yServicesUids: |
| 1036 | case TRANSACTION_setUidDeviceAffinities: |
| 1037 | case TRANSACTION_removeUidDeviceAffinities: |
| 1038 | case TRANSACTION_setUserIdDeviceAffinities: |
| 1039 | case TRANSACTION_removeUserIdDeviceAffinities: |
| 1040 | case TRANSACTION_getHwOffloadEncodingFormatsSupportedForA2DP: |
| 1041 | case TRANSACTION_listAudioVolumeGroups: |
| 1042 | case TRANSACTION_getVolumeGroupFromAudioAttributes: |
| 1043 | case TRANSACTION_acquireSoundTriggerSession: |
| 1044 | case TRANSACTION_releaseSoundTriggerSession: |
| 1045 | case TRANSACTION_setRttEnabled: |
| 1046 | case TRANSACTION_isCallScreenModeSupported: |
| 1047 | case TRANSACTION_setDevicesRoleForStrategy: |
| 1048 | case TRANSACTION_setSupportedSystemUsages: |
| 1049 | case TRANSACTION_removeDevicesRoleForStrategy: |
| 1050 | case TRANSACTION_getDevicesForRoleAndStrategy: |
| 1051 | case TRANSACTION_getDevicesForAttributes: |
| 1052 | case TRANSACTION_setAllowedCapturePolicy: |
| 1053 | case TRANSACTION_onNewAudioModulesAvailable: |
| 1054 | case TRANSACTION_setCurrentImeUid: |
| 1055 | case TRANSACTION_registerSoundTriggerCaptureStateListener: |
| 1056 | case TRANSACTION_setDevicesRoleForCapturePreset: |
| 1057 | case TRANSACTION_addDevicesRoleForCapturePreset: |
| 1058 | case TRANSACTION_removeDevicesRoleForCapturePreset: |
| 1059 | case TRANSACTION_clearDevicesRoleForCapturePreset: |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1060 | case TRANSACTION_getDevicesForRoleAndCapturePreset: |
| 1061 | case TRANSACTION_getSpatializer: { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1062 | if (!isServiceUid(IPCThreadState::self()->getCallingUid())) { |
| 1063 | ALOGW("%s: transaction %d received from PID %d unauthorized UID %d", |
| 1064 | __func__, code, IPCThreadState::self()->getCallingPid(), |
| 1065 | IPCThreadState::self()->getCallingUid()); |
| 1066 | return INVALID_OPERATION; |
| 1067 | } |
| 1068 | } break; |
| 1069 | default: |
| 1070 | break; |
| 1071 | } |
| 1072 | |
| 1073 | std::string tag("IAudioPolicyService command " + std::to_string(code)); |
| 1074 | TimeCheck check(tag.c_str()); |
| 1075 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1076 | switch (code) { |
| 1077 | case SHELL_COMMAND_TRANSACTION: { |
| 1078 | int in = data.readFileDescriptor(); |
| 1079 | int out = data.readFileDescriptor(); |
| 1080 | int err = data.readFileDescriptor(); |
| 1081 | int argc = data.readInt32(); |
| 1082 | Vector<String16> args; |
| 1083 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 1084 | args.add(data.readString16()); |
| 1085 | } |
| 1086 | sp<IBinder> unusedCallback; |
| 1087 | sp<IResultReceiver> resultReceiver; |
| 1088 | status_t status; |
| 1089 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 1090 | return status; |
| 1091 | } |
| 1092 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 1093 | return status; |
| 1094 | } |
| 1095 | status = shellCommand(in, out, err, args); |
| 1096 | if (resultReceiver != nullptr) { |
| 1097 | resultReceiver->send(status); |
| 1098 | } |
| 1099 | return NO_ERROR; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1103 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 1104 | } |
| 1105 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1106 | // ------------------- Shell command implementation ------------------- |
| 1107 | |
| 1108 | // NOTE: This is a remote API - make sure all args are validated |
| 1109 | status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) { |
| 1110 | if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) { |
| 1111 | return PERMISSION_DENIED; |
| 1112 | } |
| 1113 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 1114 | return BAD_VALUE; |
| 1115 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1116 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1117 | return handleSetUidState(args, err); |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1118 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1119 | return handleResetUidState(args, err); |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1120 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1121 | return handleGetUidState(args, out, err); |
Eric Laurent | 269acb4 | 2021-04-23 16:53:22 +0200 | [diff] [blame] | 1122 | } else if (args.size() >= 1 && args[0] == String16("purge_permission-cache")) { |
| 1123 | purgePermissionCache(); |
| 1124 | return NO_ERROR; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1125 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 1126 | printHelp(out); |
| 1127 | return NO_ERROR; |
| 1128 | } |
| 1129 | printHelp(err); |
| 1130 | return BAD_VALUE; |
| 1131 | } |
| 1132 | |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1133 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 1134 | if (userId < 0) { |
| 1135 | ALOGE("Invalid user: %d", userId); |
| 1136 | dprintf(err, "Invalid user: %d\n", userId); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1137 | return BAD_VALUE; |
| 1138 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1139 | |
| 1140 | PermissionController pc; |
| 1141 | uid = pc.getPackageUid(packageName, 0); |
| 1142 | if (uid <= 0) { |
| 1143 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 1144 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 1145 | return BAD_VALUE; |
| 1146 | } |
| 1147 | |
| 1148 | uid = multiuser_get_uid(userId, uid); |
| 1149 | return NO_ERROR; |
| 1150 | } |
| 1151 | |
| 1152 | status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) { |
| 1153 | // Valid arg.size() is 3 or 5, args.size() is 5 with --user option. |
| 1154 | if (!(args.size() == 3 || args.size() == 5)) { |
| 1155 | printHelp(err); |
| 1156 | return BAD_VALUE; |
| 1157 | } |
| 1158 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1159 | bool active = false; |
| 1160 | if (args[2] == String16("active")) { |
| 1161 | active = true; |
| 1162 | } else if ((args[2] != String16("idle"))) { |
| 1163 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 1164 | return BAD_VALUE; |
| 1165 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1166 | |
| 1167 | int userId = 0; |
| 1168 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 1169 | userId = atoi(String8(args[4])); |
| 1170 | } |
| 1171 | |
| 1172 | uid_t uid; |
| 1173 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 1174 | return BAD_VALUE; |
| 1175 | } |
| 1176 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1177 | sp<UidPolicy> uidPolicy; |
| 1178 | { |
| 1179 | Mutex::Autolock _l(mLock); |
| 1180 | uidPolicy = mUidPolicy; |
| 1181 | } |
| 1182 | if (uidPolicy) { |
| 1183 | uidPolicy->addOverrideUid(uid, active); |
| 1184 | return NO_ERROR; |
| 1185 | } |
| 1186 | return NO_INIT; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) { |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1190 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 1191 | if (!(args.size() == 2 || args.size() == 4)) { |
| 1192 | printHelp(err); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1193 | return BAD_VALUE; |
| 1194 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1195 | |
| 1196 | int userId = 0; |
| 1197 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 1198 | userId = atoi(String8(args[3])); |
| 1199 | } |
| 1200 | |
| 1201 | uid_t uid; |
| 1202 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 1203 | return BAD_VALUE; |
| 1204 | } |
| 1205 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1206 | sp<UidPolicy> uidPolicy; |
| 1207 | { |
| 1208 | Mutex::Autolock _l(mLock); |
| 1209 | uidPolicy = mUidPolicy; |
| 1210 | } |
| 1211 | if (uidPolicy) { |
| 1212 | uidPolicy->removeOverrideUid(uid); |
| 1213 | return NO_ERROR; |
| 1214 | } |
| 1215 | return NO_INIT; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) { |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1219 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 1220 | if (!(args.size() == 2 || args.size() == 4)) { |
| 1221 | printHelp(err); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1222 | return BAD_VALUE; |
| 1223 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1224 | |
| 1225 | int userId = 0; |
| 1226 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 1227 | userId = atoi(String8(args[3])); |
| 1228 | } |
| 1229 | |
| 1230 | uid_t uid; |
| 1231 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 1232 | return BAD_VALUE; |
| 1233 | } |
| 1234 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1235 | sp<UidPolicy> uidPolicy; |
| 1236 | { |
| 1237 | Mutex::Autolock _l(mLock); |
| 1238 | uidPolicy = mUidPolicy; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1239 | } |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1240 | if (uidPolicy) { |
| 1241 | return dprintf(out, uidPolicy->isUidActive(uid) ? "active\n" : "idle\n"); |
| 1242 | } |
| 1243 | return NO_INIT; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | status_t AudioPolicyService::printHelp(int out) { |
| 1247 | return dprintf(out, "Audio policy service commands:\n" |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 1248 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 1249 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 1250 | " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n" |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1251 | " help print this message\n"); |
| 1252 | } |
| 1253 | |
| 1254 | // ----------- AudioPolicyService::UidPolicy implementation ---------- |
| 1255 | |
| 1256 | void AudioPolicyService::UidPolicy::registerSelf() { |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 1257 | status_t res = mAm.linkToDeath(this); |
| 1258 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1259 | | ActivityManager::UID_OBSERVER_IDLE |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1260 | | ActivityManager::UID_OBSERVER_ACTIVE |
| 1261 | | ActivityManager::UID_OBSERVER_PROCSTATE, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1262 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 1263 | String16("audioserver")); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1264 | if (!res) { |
| 1265 | Mutex::Autolock _l(mLock); |
| 1266 | mObserverRegistered = true; |
| 1267 | } else { |
| 1268 | ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1269 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 1270 | mAm.unregisterUidObserver(this); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1271 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | void AudioPolicyService::UidPolicy::unregisterSelf() { |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 1275 | mAm.unlinkToDeath(this); |
| 1276 | mAm.unregisterUidObserver(this); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1277 | Mutex::Autolock _l(mLock); |
| 1278 | mObserverRegistered = false; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1281 | void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) { |
| 1282 | Mutex::Autolock _l(mLock); |
| 1283 | mCachedUids.clear(); |
| 1284 | mObserverRegistered = false; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1287 | void AudioPolicyService::UidPolicy::checkRegistered() { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1288 | bool needToReregister = false; |
| 1289 | { |
| 1290 | Mutex::Autolock _l(mLock); |
| 1291 | needToReregister = !mObserverRegistered; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1292 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1293 | if (needToReregister) { |
| 1294 | // Looks like ActivityManager has died previously, attempt to re-register. |
| 1295 | registerSelf(); |
| 1296 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) { |
| 1300 | if (isServiceUid(uid)) return true; |
| 1301 | checkRegistered(); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1302 | { |
| 1303 | Mutex::Autolock _l(mLock); |
| 1304 | auto overrideIter = mOverrideUids.find(uid); |
| 1305 | if (overrideIter != mOverrideUids.end()) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1306 | return overrideIter->second.first; |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1307 | } |
| 1308 | // In an absense of the ActivityManager, assume everything to be active. |
| 1309 | if (!mObserverRegistered) return true; |
| 1310 | auto cacheIter = mCachedUids.find(uid); |
Mikhail Naganov | eba668a | 2018-04-05 08:13:15 -0700 | [diff] [blame] | 1311 | if (cacheIter != mCachedUids.end()) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1312 | return cacheIter->second.first; |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | ActivityManager am; |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 1316 | bool active = am.isUidActive(uid, String16("audioserver")); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1317 | { |
| 1318 | Mutex::Autolock _l(mLock); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1319 | mCachedUids.insert(std::pair<uid_t, |
| 1320 | std::pair<bool, int>>(uid, std::pair<bool, int>(active, |
| 1321 | ActivityManager::PROCESS_STATE_UNKNOWN))); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1322 | } |
| 1323 | return active; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1324 | } |
| 1325 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1326 | int AudioPolicyService::UidPolicy::getUidState(uid_t uid) { |
| 1327 | if (isServiceUid(uid)) { |
| 1328 | return ActivityManager::PROCESS_STATE_TOP; |
| 1329 | } |
| 1330 | checkRegistered(); |
| 1331 | { |
| 1332 | Mutex::Autolock _l(mLock); |
| 1333 | auto overrideIter = mOverrideUids.find(uid); |
| 1334 | if (overrideIter != mOverrideUids.end()) { |
| 1335 | if (overrideIter->second.first) { |
| 1336 | if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1337 | return overrideIter->second.second; |
| 1338 | } else { |
| 1339 | auto cacheIter = mCachedUids.find(uid); |
| 1340 | if (cacheIter != mCachedUids.end()) { |
| 1341 | return cacheIter->second.second; |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | return ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1346 | } |
| 1347 | // In an absense of the ActivityManager, assume everything to be active. |
| 1348 | if (!mObserverRegistered) { |
| 1349 | return ActivityManager::PROCESS_STATE_TOP; |
| 1350 | } |
| 1351 | auto cacheIter = mCachedUids.find(uid); |
| 1352 | if (cacheIter != mCachedUids.end()) { |
| 1353 | if (cacheIter->second.first) { |
| 1354 | return cacheIter->second.second; |
| 1355 | } else { |
| 1356 | return ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1357 | } |
| 1358 | } |
| 1359 | } |
| 1360 | ActivityManager am; |
Hui Yu | 12c7ec7 | 2020-05-04 17:40:52 +0000 | [diff] [blame] | 1361 | bool active = am.isUidActive(uid, String16("audioserver")); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1362 | int state = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1363 | if (active) { |
| 1364 | state = am.getUidProcessState(uid, String16("audioserver")); |
| 1365 | } |
| 1366 | { |
| 1367 | Mutex::Autolock _l(mLock); |
| 1368 | mCachedUids.insert(std::pair<uid_t, |
| 1369 | std::pair<bool, int>>(uid, std::pair<bool, int>(active, state))); |
| 1370 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1371 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1372 | return state; |
| 1373 | } |
| 1374 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1375 | void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1376 | updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1380 | updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1384 | updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1387 | void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid, |
| 1388 | int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 1389 | int64_t procStateSeq __unused, |
| 1390 | int32_t capability __unused) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1391 | if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1392 | updateUid(&mCachedUids, uid, true, procState, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1397 | updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert); |
| 1398 | } |
| 1399 | |
| 1400 | void AudioPolicyService::UidPolicy::notifyService() { |
| 1401 | sp<AudioPolicyService> service = mService.promote(); |
| 1402 | if (service != nullptr) { |
| 1403 | service->updateUidStates(); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1407 | void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t, |
| 1408 | std::pair<bool, int>> *uids, |
| 1409 | uid_t uid, |
| 1410 | bool active, |
| 1411 | int state, |
| 1412 | bool insert) { |
| 1413 | if (isServiceUid(uid)) { |
| 1414 | return; |
| 1415 | } |
| 1416 | bool wasActive = isUidActive(uid); |
| 1417 | int previousState = getUidState(uid); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1418 | { |
| 1419 | Mutex::Autolock _l(mLock); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1420 | updateUidLocked(uids, uid, active, state, insert); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1421 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1422 | if (wasActive != isUidActive(uid) || state != previousState) { |
| 1423 | notifyService(); |
| 1424 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1427 | void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, |
| 1428 | std::pair<bool, int>> *uids, |
| 1429 | uid_t uid, |
| 1430 | bool active, |
| 1431 | int state, |
| 1432 | bool insert) { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1433 | auto it = uids->find(uid); |
| 1434 | if (it != uids->end()) { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1435 | if (insert) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1436 | if (state == ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1437 | it->second.first = active; |
| 1438 | } |
| 1439 | if (it->second.first) { |
| 1440 | it->second.second = state; |
| 1441 | } else { |
| 1442 | it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1443 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1444 | } else { |
| 1445 | uids->erase(it); |
| 1446 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1447 | } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) { |
| 1448 | uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid, |
| 1449 | std::pair<bool, int>(active, state))); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1450 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1451 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1452 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1453 | bool AudioPolicyService::UidPolicy::isA11yOnTop() { |
| 1454 | for (const auto &uid : mCachedUids) { |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 1455 | if (!isA11yUid(uid.first)) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1456 | continue; |
| 1457 | } |
Amith Yamasani | bcbb300 | 2019-01-23 13:53:33 -0800 | [diff] [blame] | 1458 | if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP |
| 1459 | && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1460 | return true; |
| 1461 | } |
| 1462 | } |
| 1463 | return false; |
| 1464 | } |
| 1465 | |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1466 | bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid) |
| 1467 | { |
| 1468 | std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid); |
| 1469 | return it != mA11yUids.end(); |
| 1470 | } |
| 1471 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 1472 | // ----------- AudioPolicyService::SensorPrivacyService implementation ---------- |
| 1473 | void AudioPolicyService::SensorPrivacyPolicy::registerSelf() { |
| 1474 | SensorPrivacyManager spm; |
| 1475 | mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled(); |
| 1476 | spm.addSensorPrivacyListener(this); |
| 1477 | } |
| 1478 | |
Evan Severson | 241d959 | 2021-01-08 12:16:02 -0800 | [diff] [blame] | 1479 | void AudioPolicyService::SensorPrivacyPolicy::registerSelfForMicrophoneOnly(int userId) { |
| 1480 | SensorPrivacyManager spm; |
| 1481 | mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId, |
| 1482 | SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE); |
| 1483 | spm.addIndividualSensorPrivacyListener(userId, |
| 1484 | SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this); |
| 1485 | } |
| 1486 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 1487 | void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() { |
| 1488 | SensorPrivacyManager spm; |
| 1489 | spm.removeSensorPrivacyListener(this); |
| 1490 | } |
| 1491 | |
| 1492 | bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 1493 | return mSensorPrivacyEnabled; |
| 1494 | } |
| 1495 | |
| 1496 | binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) { |
| 1497 | mSensorPrivacyEnabled = enabled; |
| 1498 | sp<AudioPolicyService> service = mService.promote(); |
| 1499 | if (service != nullptr) { |
| 1500 | service->updateUidStates(); |
| 1501 | } |
| 1502 | return binder::Status::ok(); |
| 1503 | } |
| 1504 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 1505 | // ----------- AudioPolicyService::OpRecordAudioMonitor implementation ---------- |
| 1506 | |
| 1507 | // static |
| 1508 | sp<AudioPolicyService::OpRecordAudioMonitor> |
| 1509 | AudioPolicyService::OpRecordAudioMonitor::createIfNeeded( |
| 1510 | const AttributionSourceState& attributionSource, const audio_attributes_t& attr, |
| 1511 | wp<AudioCommandThread> commandThread) |
| 1512 | { |
Eric Laurent | 987ce10 | 2021-07-05 12:11:51 +0200 | [diff] [blame] | 1513 | if (isAudioServerOrRootUid(attributionSource.uid)) { |
| 1514 | ALOGV("not silencing record for audio or root source %s", |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 1515 | attributionSource.toString().c_str()); |
| 1516 | return nullptr; |
| 1517 | } |
| 1518 | |
| 1519 | if (!AudioPolicyService::isAppOpSource(attr.source)) { |
| 1520 | ALOGD("not monitoring app op for uid %d and source %d", |
| 1521 | attributionSource.uid, attr.source); |
| 1522 | return nullptr; |
| 1523 | } |
| 1524 | |
| 1525 | if (!attributionSource.packageName.has_value() |
| 1526 | || attributionSource.packageName.value().size() == 0) { |
| 1527 | return nullptr; |
| 1528 | } |
| 1529 | return new OpRecordAudioMonitor(attributionSource, getOpForSource(attr.source), commandThread); |
| 1530 | } |
| 1531 | |
| 1532 | AudioPolicyService::OpRecordAudioMonitor::OpRecordAudioMonitor( |
| 1533 | const AttributionSourceState& attributionSource, int32_t appOp, |
| 1534 | wp<AudioCommandThread> commandThread) : |
| 1535 | mHasOp(true), mAttributionSource(attributionSource), mAppOp(appOp), |
| 1536 | mCommandThread(commandThread) |
| 1537 | { |
| 1538 | } |
| 1539 | |
| 1540 | AudioPolicyService::OpRecordAudioMonitor::~OpRecordAudioMonitor() |
| 1541 | { |
| 1542 | if (mOpCallback != 0) { |
| 1543 | mAppOpsManager.stopWatchingMode(mOpCallback); |
| 1544 | } |
| 1545 | mOpCallback.clear(); |
| 1546 | } |
| 1547 | |
| 1548 | void AudioPolicyService::OpRecordAudioMonitor::onFirstRef() |
| 1549 | { |
| 1550 | checkOp(); |
| 1551 | mOpCallback = new RecordAudioOpCallback(this); |
| 1552 | ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str()); |
| 1553 | // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too |
| 1554 | // since it controls the mic permission for legacy apps. |
| 1555 | mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16( |
| 1556 | mAttributionSource.packageName.value_or(""))), |
| 1557 | mOpCallback); |
| 1558 | } |
| 1559 | |
| 1560 | bool AudioPolicyService::OpRecordAudioMonitor::hasOp() const { |
| 1561 | return mHasOp.load(); |
| 1562 | } |
| 1563 | |
| 1564 | // Called by RecordAudioOpCallback when the app op corresponding to this OpRecordAudioMonitor |
| 1565 | // is updated in AppOp callback and in onFirstRef() |
| 1566 | // Note this method is never called (and never to be) for audio server / root track |
| 1567 | // due to the UID in createIfNeeded(). As a result for those record track, it's: |
| 1568 | // - not called from constructor, |
| 1569 | // - not called from RecordAudioOpCallback because the callback is not installed in this case |
| 1570 | void AudioPolicyService::OpRecordAudioMonitor::checkOp(bool updateUidStates) |
| 1571 | { |
| 1572 | // TODO: We need to always check AppOpsManager::OP_RECORD_AUDIO too |
| 1573 | // since it controls the mic permission for legacy apps. |
| 1574 | const int32_t mode = mAppOpsManager.checkOp(mAppOp, |
| 1575 | mAttributionSource.uid, VALUE_OR_FATAL(aidl2legacy_string_view_String16( |
| 1576 | mAttributionSource.packageName.value_or("")))); |
| 1577 | const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED); |
| 1578 | // verbose logging only log when appOp changed |
| 1579 | ALOGI_IF(hasIt != mHasOp.load(), |
| 1580 | "App op %d missing, %ssilencing record %s", |
| 1581 | mAppOp, hasIt ? "un" : "", mAttributionSource.toString().c_str()); |
| 1582 | mHasOp.store(hasIt); |
| 1583 | |
| 1584 | if (updateUidStates) { |
| 1585 | sp<AudioCommandThread> commandThread = mCommandThread.promote(); |
| 1586 | if (commandThread != nullptr) { |
| 1587 | commandThread->updateUidStatesCommand(); |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback( |
| 1593 | const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor) |
| 1594 | { } |
| 1595 | |
| 1596 | void AudioPolicyService::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op, |
| 1597 | const String16& packageName __unused) { |
| 1598 | sp<OpRecordAudioMonitor> monitor = mMonitor.promote(); |
| 1599 | if (monitor != NULL) { |
| 1600 | if (op != monitor->getOp()) { |
| 1601 | return; |
| 1602 | } |
| 1603 | monitor->checkOp(true); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1608 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 1609 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1610 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name, |
| 1611 | const wp<AudioPolicyService>& service) |
| 1612 | : Thread(false), mName(name), mService(service) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1613 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | |
| 1617 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 1618 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1619 | if (!mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1620 | release_wake_lock(mName.string()); |
| 1621 | } |
| 1622 | mAudioCommands.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 1626 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1627 | run(mName.string(), ANDROID_PRIORITY_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 1631 | { |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 1632 | nsecs_t waitTime = -1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1633 | |
| 1634 | mLock.lock(); |
| 1635 | while (!exitPending()) |
| 1636 | { |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1637 | sp<AudioPolicyService> svc; |
| 1638 | while (!mAudioCommands.isEmpty() && !exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1639 | nsecs_t curTime = systemTime(); |
| 1640 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 1641 | if (mAudioCommands[0]->mTime <= curTime) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1642 | sp<AudioCommand> command = mAudioCommands[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1643 | mAudioCommands.removeAt(0); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1644 | mLastCommand = command; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1645 | |
| 1646 | switch (command->mCommand) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1647 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1648 | VolumeData *data = (VolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1649 | ALOGV("AudioCommandThread() processing set volume stream %d, \ |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1650 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1651 | mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1652 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 1653 | data->mVolume, |
| 1654 | data->mIO); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1655 | mLock.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1656 | }break; |
| 1657 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1658 | ParametersData *data = (ParametersData *)command->mParam.get(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1659 | ALOGV("AudioCommandThread() processing set parameters string %s, io %d", |
| 1660 | data->mKeyValuePairs.string(), data->mIO); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1661 | mLock.unlock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1662 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1663 | mLock.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1664 | }break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1665 | case SET_VOICE_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1666 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1667 | ALOGV("AudioCommandThread() processing set voice volume volume %f", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1668 | data->mVolume); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1669 | mLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1670 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1671 | mLock.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1672 | }break; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1673 | case STOP_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1674 | StopOutputData *data = (StopOutputData *)command->mParam.get(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1675 | ALOGV("AudioCommandThread() processing stop output portId %d", |
| 1676 | data->mPortId); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1677 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1678 | if (svc == 0) { |
| 1679 | break; |
| 1680 | } |
| 1681 | mLock.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1682 | svc->doStopOutput(data->mPortId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1683 | mLock.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1684 | }break; |
| 1685 | case RELEASE_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1686 | ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1687 | ALOGV("AudioCommandThread() processing release output portId %d", |
| 1688 | data->mPortId); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1689 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1690 | if (svc == 0) { |
| 1691 | break; |
| 1692 | } |
| 1693 | mLock.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1694 | svc->doReleaseOutput(data->mPortId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1695 | mLock.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1696 | }break; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1697 | case CREATE_AUDIO_PATCH: { |
| 1698 | CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get(); |
| 1699 | ALOGV("AudioCommandThread() processing create audio patch"); |
| 1700 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1701 | if (af == 0) { |
| 1702 | command->mStatus = PERMISSION_DENIED; |
| 1703 | } else { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1704 | mLock.unlock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1705 | command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1706 | mLock.lock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1707 | } |
| 1708 | } break; |
| 1709 | case RELEASE_AUDIO_PATCH: { |
| 1710 | ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get(); |
| 1711 | ALOGV("AudioCommandThread() processing release audio patch"); |
| 1712 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1713 | if (af == 0) { |
| 1714 | command->mStatus = PERMISSION_DENIED; |
| 1715 | } else { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1716 | mLock.unlock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1717 | command->mStatus = af->releaseAudioPatch(data->mHandle); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1718 | mLock.lock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1719 | } |
| 1720 | } break; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1721 | case UPDATE_AUDIOPORT_LIST: { |
| 1722 | ALOGV("AudioCommandThread() processing update audio port list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1723 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1724 | if (svc == 0) { |
| 1725 | break; |
| 1726 | } |
| 1727 | mLock.unlock(); |
| 1728 | svc->doOnAudioPortListUpdate(); |
| 1729 | mLock.lock(); |
| 1730 | }break; |
| 1731 | case UPDATE_AUDIOPATCH_LIST: { |
| 1732 | ALOGV("AudioCommandThread() processing update audio patch list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1733 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1734 | if (svc == 0) { |
| 1735 | break; |
| 1736 | } |
| 1737 | mLock.unlock(); |
| 1738 | svc->doOnAudioPatchListUpdate(); |
| 1739 | mLock.lock(); |
| 1740 | }break; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1741 | case CHANGED_AUDIOVOLUMEGROUP: { |
| 1742 | AudioVolumeGroupData *data = |
| 1743 | static_cast<AudioVolumeGroupData *>(command->mParam.get()); |
| 1744 | ALOGV("AudioCommandThread() processing update audio volume group"); |
| 1745 | svc = mService.promote(); |
| 1746 | if (svc == 0) { |
| 1747 | break; |
| 1748 | } |
| 1749 | mLock.unlock(); |
| 1750 | svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags); |
| 1751 | mLock.lock(); |
| 1752 | }break; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 1753 | case SET_AUDIOPORT_CONFIG: { |
| 1754 | SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get(); |
| 1755 | ALOGV("AudioCommandThread() processing set port config"); |
| 1756 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1757 | if (af == 0) { |
| 1758 | command->mStatus = PERMISSION_DENIED; |
| 1759 | } else { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1760 | mLock.unlock(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 1761 | command->mStatus = af->setAudioPortConfig(&data->mConfig); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1762 | mLock.lock(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 1763 | } |
| 1764 | } break; |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1765 | case DYN_POLICY_MIX_STATE_UPDATE: { |
| 1766 | DynPolicyMixStateUpdateData *data = |
| 1767 | (DynPolicyMixStateUpdateData *)command->mParam.get(); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1768 | ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d", |
| 1769 | data->mRegId.string(), data->mState); |
| 1770 | svc = mService.promote(); |
| 1771 | if (svc == 0) { |
| 1772 | break; |
| 1773 | } |
| 1774 | mLock.unlock(); |
| 1775 | svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState); |
| 1776 | mLock.lock(); |
| 1777 | } break; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1778 | case RECORDING_CONFIGURATION_UPDATE: { |
| 1779 | RecordingConfigurationUpdateData *data = |
| 1780 | (RecordingConfigurationUpdateData *)command->mParam.get(); |
| 1781 | ALOGV("AudioCommandThread() processing recording configuration update"); |
| 1782 | svc = mService.promote(); |
| 1783 | if (svc == 0) { |
| 1784 | break; |
| 1785 | } |
| 1786 | mLock.unlock(); |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 1787 | svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1788 | &data->mClientConfig, data->mClientEffects, |
| 1789 | &data->mDeviceConfig, data->mEffects, |
| 1790 | data->mPatchHandle, data->mSource); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1791 | mLock.lock(); |
| 1792 | } break; |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1793 | case SET_EFFECT_SUSPENDED: { |
| 1794 | SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get(); |
| 1795 | ALOGV("AudioCommandThread() processing set effect suspended"); |
| 1796 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1797 | if (af != 0) { |
| 1798 | mLock.unlock(); |
| 1799 | af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended); |
| 1800 | mLock.lock(); |
| 1801 | } |
| 1802 | } break; |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 1803 | case AUDIO_MODULES_UPDATE: { |
| 1804 | ALOGV("AudioCommandThread() processing audio modules update"); |
| 1805 | svc = mService.promote(); |
| 1806 | if (svc == 0) { |
| 1807 | break; |
| 1808 | } |
| 1809 | mLock.unlock(); |
| 1810 | svc->doOnNewAudioModulesAvailable(); |
| 1811 | mLock.lock(); |
| 1812 | } break; |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 1813 | case ROUTING_UPDATED: { |
| 1814 | ALOGV("AudioCommandThread() processing routing update"); |
| 1815 | svc = mService.promote(); |
| 1816 | if (svc == 0) { |
| 1817 | break; |
| 1818 | } |
| 1819 | mLock.unlock(); |
| 1820 | svc->doOnRoutingUpdated(); |
| 1821 | mLock.lock(); |
| 1822 | } break; |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1823 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 1824 | case UPDATE_UID_STATES: { |
| 1825 | ALOGV("AudioCommandThread() processing updateUID states"); |
| 1826 | svc = mService.promote(); |
| 1827 | if (svc == 0) { |
| 1828 | break; |
| 1829 | } |
| 1830 | mLock.unlock(); |
| 1831 | svc->updateUidStates(); |
| 1832 | mLock.lock(); |
| 1833 | } break; |
| 1834 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1835 | case CHECK_SPATIALIZER: { |
| 1836 | ALOGV("AudioCommandThread() processing updateUID states"); |
| 1837 | svc = mService.promote(); |
| 1838 | if (svc == 0) { |
| 1839 | break; |
| 1840 | } |
| 1841 | mLock.unlock(); |
| 1842 | svc->doOnCheckSpatializer(); |
| 1843 | mLock.lock(); |
| 1844 | } break; |
| 1845 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1846 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1847 | ALOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1848 | } |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1849 | { |
| 1850 | Mutex::Autolock _l(command->mLock); |
| 1851 | if (command->mWaitStatus) { |
| 1852 | command->mWaitStatus = false; |
| 1853 | command->mCond.signal(); |
| 1854 | } |
| 1855 | } |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 1856 | waitTime = -1; |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 1857 | // release mLock before releasing strong reference on the service as |
| 1858 | // AudioPolicyService destructor calls AudioCommandThread::exit() which |
| 1859 | // acquires mLock. |
| 1860 | mLock.unlock(); |
| 1861 | svc.clear(); |
| 1862 | mLock.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1863 | } else { |
| 1864 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 1865 | break; |
| 1866 | } |
| 1867 | } |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 1868 | |
| 1869 | // release delayed commands wake lock if the queue is empty |
| 1870 | if (mAudioCommands.isEmpty()) { |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 1871 | release_wake_lock(mName.string()); |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | // At this stage we have either an empty command queue or the first command in the queue |
| 1875 | // has a finite delay. So unless we are exiting it is safe to wait. |
| 1876 | if (!exitPending()) { |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1877 | ALOGV("AudioCommandThread() going to sleep"); |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 1878 | if (waitTime == -1) { |
| 1879 | mWaitWorkCV.wait(mLock); |
| 1880 | } else { |
| 1881 | mWaitWorkCV.waitRelative(mLock, waitTime); |
| 1882 | } |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1883 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1884 | } |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 1885 | // release delayed commands wake lock before quitting |
| 1886 | if (!mAudioCommands.isEmpty()) { |
| 1887 | release_wake_lock(mName.string()); |
| 1888 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1889 | mLock.unlock(); |
| 1890 | return false; |
| 1891 | } |
| 1892 | |
| 1893 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
| 1894 | { |
| 1895 | const size_t SIZE = 256; |
| 1896 | char buffer[SIZE]; |
| 1897 | String8 result; |
| 1898 | |
| 1899 | snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); |
| 1900 | result.append(buffer); |
| 1901 | write(fd, result.string(), result.size()); |
| 1902 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1903 | const bool locked = dumpTryLock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1904 | if (!locked) { |
| 1905 | String8 result2(kCmdDeadlockedString); |
| 1906 | write(fd, result2.string(), result2.size()); |
| 1907 | } |
| 1908 | |
| 1909 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 1910 | result = String8(buffer); |
| 1911 | result.append(" Command Time Wait pParam\n"); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1912 | for (size_t i = 0; i < mAudioCommands.size(); i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1913 | mAudioCommands[i]->dump(buffer, SIZE); |
| 1914 | result.append(buffer); |
| 1915 | } |
| 1916 | result.append(" Last Command\n"); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1917 | if (mLastCommand != 0) { |
| 1918 | mLastCommand->dump(buffer, SIZE); |
| 1919 | result.append(buffer); |
| 1920 | } else { |
| 1921 | result.append(" none\n"); |
| 1922 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1923 | |
| 1924 | write(fd, result.string(), result.size()); |
| 1925 | |
Mikhail Naganov | 12b716c | 2020-04-30 22:37:43 +0000 | [diff] [blame] | 1926 | dumpReleaseLock(mLock, locked); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1927 | |
| 1928 | return NO_ERROR; |
| 1929 | } |
| 1930 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1931 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1932 | float volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1933 | audio_io_handle_t output, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1934 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1935 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1936 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1937 | command->mCommand = SET_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1938 | sp<VolumeData> data = new VolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1939 | data->mStream = stream; |
| 1940 | data->mVolume = volume; |
| 1941 | data->mIO = output; |
| 1942 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1943 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1944 | ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1945 | stream, volume, output); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1946 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1949 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1950 | const char *keyValuePairs, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1951 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1952 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1953 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1954 | command->mCommand = SET_PARAMETERS; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1955 | sp<ParametersData> data = new ParametersData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1956 | data->mIO = ioHandle; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1957 | data->mKeyValuePairs = String8(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1958 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1959 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1960 | ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1961 | keyValuePairs, ioHandle, delayMs); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1962 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 1966 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1967 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1968 | command->mCommand = SET_VOICE_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1969 | sp<VoiceVolumeData> data = new VoiceVolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1970 | data->mVolume = volume; |
| 1971 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1972 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1973 | ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1974 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1977 | void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId, |
| 1978 | audio_session_t sessionId, |
| 1979 | bool suspended) |
| 1980 | { |
| 1981 | sp<AudioCommand> command = new AudioCommand(); |
| 1982 | command->mCommand = SET_EFFECT_SUSPENDED; |
| 1983 | sp<SetEffectSuspendedData> data = new SetEffectSuspendedData(); |
| 1984 | data->mEffectId = effectId; |
| 1985 | data->mSessionId = sessionId; |
| 1986 | data->mSuspended = suspended; |
| 1987 | command->mParam = data; |
| 1988 | ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d", |
| 1989 | effectId, sessionId, suspended); |
| 1990 | sendCommand(command); |
| 1991 | } |
| 1992 | |
| 1993 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1994 | void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1995 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1996 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1997 | command->mCommand = STOP_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1998 | sp<StopOutputData> data = new StopOutputData(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1999 | data->mPortId = portId; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 2000 | command->mParam = data; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2001 | ALOGV("AudioCommandThread() adding stop output portId %d", portId); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2002 | sendCommand(command); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2005 | void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2006 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2007 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2008 | command->mCommand = RELEASE_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2009 | sp<ReleaseOutputData> data = new ReleaseOutputData(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2010 | data->mPortId = portId; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 2011 | command->mParam = data; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 2012 | ALOGV("AudioCommandThread() adding release output portId %d", portId); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2013 | sendCommand(command); |
| 2014 | } |
| 2015 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2016 | status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand( |
| 2017 | const struct audio_patch *patch, |
| 2018 | audio_patch_handle_t *handle, |
| 2019 | int delayMs) |
| 2020 | { |
| 2021 | status_t status = NO_ERROR; |
| 2022 | |
| 2023 | sp<AudioCommand> command = new AudioCommand(); |
| 2024 | command->mCommand = CREATE_AUDIO_PATCH; |
| 2025 | CreateAudioPatchData *data = new CreateAudioPatchData(); |
| 2026 | data->mPatch = *patch; |
| 2027 | data->mHandle = *handle; |
| 2028 | command->mParam = data; |
| 2029 | command->mWaitStatus = true; |
| 2030 | ALOGV("AudioCommandThread() adding create patch delay %d", delayMs); |
| 2031 | status = sendCommand(command, delayMs); |
| 2032 | if (status == NO_ERROR) { |
| 2033 | *handle = data->mHandle; |
| 2034 | } |
| 2035 | return status; |
| 2036 | } |
| 2037 | |
| 2038 | status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle, |
| 2039 | int delayMs) |
| 2040 | { |
| 2041 | sp<AudioCommand> command = new AudioCommand(); |
| 2042 | command->mCommand = RELEASE_AUDIO_PATCH; |
| 2043 | ReleaseAudioPatchData *data = new ReleaseAudioPatchData(); |
| 2044 | data->mHandle = handle; |
| 2045 | command->mParam = data; |
| 2046 | command->mWaitStatus = true; |
| 2047 | ALOGV("AudioCommandThread() adding release patch delay %d", delayMs); |
| 2048 | return sendCommand(command, delayMs); |
| 2049 | } |
| 2050 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2051 | void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand() |
| 2052 | { |
| 2053 | sp<AudioCommand> command = new AudioCommand(); |
| 2054 | command->mCommand = UPDATE_AUDIOPORT_LIST; |
| 2055 | ALOGV("AudioCommandThread() adding update audio port list"); |
| 2056 | sendCommand(command); |
| 2057 | } |
| 2058 | |
Eric Laurent | ed726cc | 2021-07-01 14:26:41 +0200 | [diff] [blame] | 2059 | void AudioPolicyService::AudioCommandThread::updateUidStatesCommand() |
| 2060 | { |
| 2061 | sp<AudioCommand> command = new AudioCommand(); |
| 2062 | command->mCommand = UPDATE_UID_STATES; |
| 2063 | ALOGV("AudioCommandThread() adding update UID states"); |
| 2064 | sendCommand(command); |
| 2065 | } |
| 2066 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2067 | void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand() |
| 2068 | { |
| 2069 | sp<AudioCommand>command = new AudioCommand(); |
| 2070 | command->mCommand = UPDATE_AUDIOPATCH_LIST; |
| 2071 | ALOGV("AudioCommandThread() adding update audio patch list"); |
| 2072 | sendCommand(command); |
| 2073 | } |
| 2074 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2075 | void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group, |
| 2076 | int flags) |
| 2077 | { |
| 2078 | sp<AudioCommand>command = new AudioCommand(); |
| 2079 | command->mCommand = CHANGED_AUDIOVOLUMEGROUP; |
| 2080 | AudioVolumeGroupData *data= new AudioVolumeGroupData(); |
| 2081 | data->mGroup = group; |
| 2082 | data->mFlags = flags; |
| 2083 | command->mParam = data; |
| 2084 | ALOGV("AudioCommandThread() adding audio volume group changed"); |
| 2085 | sendCommand(command); |
| 2086 | } |
| 2087 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2088 | status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand( |
| 2089 | const struct audio_port_config *config, int delayMs) |
| 2090 | { |
| 2091 | sp<AudioCommand> command = new AudioCommand(); |
| 2092 | command->mCommand = SET_AUDIOPORT_CONFIG; |
| 2093 | SetAudioPortConfigData *data = new SetAudioPortConfigData(); |
| 2094 | data->mConfig = *config; |
| 2095 | command->mParam = data; |
| 2096 | command->mWaitStatus = true; |
| 2097 | ALOGV("AudioCommandThread() adding set port config delay %d", delayMs); |
| 2098 | return sendCommand(command, delayMs); |
| 2099 | } |
| 2100 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2101 | void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2102 | const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2103 | { |
| 2104 | sp<AudioCommand> command = new AudioCommand(); |
| 2105 | command->mCommand = DYN_POLICY_MIX_STATE_UPDATE; |
| 2106 | DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData(); |
| 2107 | data->mRegId = regId; |
| 2108 | data->mState = state; |
| 2109 | command->mParam = data; |
| 2110 | ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d", |
| 2111 | regId.string(), state); |
| 2112 | sendCommand(command); |
| 2113 | } |
| 2114 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2115 | void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2116 | int event, |
| 2117 | const record_client_info_t *clientInfo, |
| 2118 | const audio_config_base_t *clientConfig, |
| 2119 | std::vector<effect_descriptor_t> clientEffects, |
| 2120 | const audio_config_base_t *deviceConfig, |
| 2121 | std::vector<effect_descriptor_t> effects, |
| 2122 | audio_patch_handle_t patchHandle, |
| 2123 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2124 | { |
| 2125 | sp<AudioCommand>command = new AudioCommand(); |
| 2126 | command->mCommand = RECORDING_CONFIGURATION_UPDATE; |
| 2127 | RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData(); |
| 2128 | data->mEvent = event; |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 2129 | data->mClientInfo = *clientInfo; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 2130 | data->mClientConfig = *clientConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2131 | data->mClientEffects = clientEffects; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 2132 | data->mDeviceConfig = *deviceConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2133 | data->mEffects = effects; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2134 | data->mPatchHandle = patchHandle; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 2135 | data->mSource = source; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2136 | command->mParam = data; |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 2137 | ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u", |
| 2138 | event, clientInfo->source, clientInfo->uid); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2139 | sendCommand(command); |
| 2140 | } |
| 2141 | |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2142 | void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand() |
| 2143 | { |
| 2144 | sp<AudioCommand> command = new AudioCommand(); |
| 2145 | command->mCommand = AUDIO_MODULES_UPDATE; |
| 2146 | sendCommand(command); |
| 2147 | } |
| 2148 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2149 | void AudioPolicyService::AudioCommandThread::routingChangedCommand() |
| 2150 | { |
| 2151 | sp<AudioCommand>command = new AudioCommand(); |
| 2152 | command->mCommand = ROUTING_UPDATED; |
| 2153 | ALOGV("AudioCommandThread() adding routing update"); |
| 2154 | sendCommand(command); |
| 2155 | } |
| 2156 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 2157 | void AudioPolicyService::AudioCommandThread::checkSpatializerCommand() |
| 2158 | { |
| 2159 | sp<AudioCommand>command = new AudioCommand(); |
| 2160 | command->mCommand = CHECK_SPATIALIZER; |
| 2161 | ALOGV("AudioCommandThread() adding check spatializer"); |
| 2162 | sendCommand(command); |
| 2163 | } |
| 2164 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2165 | status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs) |
| 2166 | { |
| 2167 | { |
| 2168 | Mutex::Autolock _l(mLock); |
| 2169 | insertCommand_l(command, delayMs); |
| 2170 | mWaitWorkCV.signal(); |
| 2171 | } |
| 2172 | Mutex::Autolock _l(command->mLock); |
| 2173 | while (command->mWaitStatus) { |
| 2174 | nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs); |
| 2175 | if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) { |
| 2176 | command->mStatus = TIMED_OUT; |
| 2177 | command->mWaitStatus = false; |
| 2178 | } |
| 2179 | } |
| 2180 | return command->mStatus; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2183 | // insertCommand_l() must be called with mLock held |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2184 | void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2185 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2186 | ssize_t i; // not size_t because i will count down to -1 |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2187 | Vector < sp<AudioCommand> > removedCommands; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2188 | command->mTime = systemTime() + milliseconds(delayMs); |
| 2189 | |
| 2190 | // acquire wake lock to make sure delayed commands are processed |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2191 | if (mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2192 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); |
| 2193 | } |
| 2194 | |
| 2195 | // check same pending commands with later time stamps and eliminate them |
Ivan Lozano | 5ff158f | 2017-10-30 09:06:24 -0700 | [diff] [blame] | 2196 | for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2197 | sp<AudioCommand> command2 = mAudioCommands[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2198 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 2199 | if (command2->mTime <= command->mTime) break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2200 | |
| 2201 | // create audio patch or release audio patch commands are equivalent |
| 2202 | // with regard to filtering |
| 2203 | if ((command->mCommand == CREATE_AUDIO_PATCH) || |
| 2204 | (command->mCommand == RELEASE_AUDIO_PATCH)) { |
| 2205 | if ((command2->mCommand != CREATE_AUDIO_PATCH) && |
| 2206 | (command2->mCommand != RELEASE_AUDIO_PATCH)) { |
| 2207 | continue; |
| 2208 | } |
| 2209 | } else if (command2->mCommand != command->mCommand) continue; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2210 | |
| 2211 | switch (command->mCommand) { |
| 2212 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2213 | ParametersData *data = (ParametersData *)command->mParam.get(); |
| 2214 | ParametersData *data2 = (ParametersData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2215 | if (data->mIO != data2->mIO) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2216 | ALOGV("Comparing parameter command %s to new command %s", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2217 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2218 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 2219 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 2220 | for (size_t j = 0; j < param.size(); j++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 2221 | String8 key; |
| 2222 | String8 value; |
| 2223 | param.getAt(j, key, value); |
| 2224 | for (size_t k = 0; k < param2.size(); k++) { |
| 2225 | String8 key2; |
| 2226 | String8 value2; |
| 2227 | param2.getAt(k, key2, value2); |
| 2228 | if (key2 == key) { |
| 2229 | param2.remove(key2); |
| 2230 | ALOGV("Filtering out parameter %s", key2.string()); |
| 2231 | break; |
| 2232 | } |
| 2233 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2234 | } |
| 2235 | // if all keys have been filtered out, remove the command. |
| 2236 | // otherwise, update the key value pairs |
| 2237 | if (param2.size() == 0) { |
| 2238 | removedCommands.add(command2); |
| 2239 | } else { |
| 2240 | data2->mKeyValuePairs = param2.toString(); |
| 2241 | } |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 2242 | command->mTime = command2->mTime; |
| 2243 | // force delayMs to non 0 so that code below does not request to wait for |
| 2244 | // command status as the command is now delayed |
| 2245 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2246 | } break; |
| 2247 | |
| 2248 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2249 | VolumeData *data = (VolumeData *)command->mParam.get(); |
| 2250 | VolumeData *data2 = (VolumeData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2251 | if (data->mIO != data2->mIO) break; |
| 2252 | if (data->mStream != data2->mStream) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2253 | ALOGV("Filtering out volume command on output %d for stream %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2254 | data->mIO, data->mStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2255 | removedCommands.add(command2); |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 2256 | command->mTime = command2->mTime; |
| 2257 | // force delayMs to non 0 so that code below does not request to wait for |
| 2258 | // command status as the command is now delayed |
| 2259 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2260 | } break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2261 | |
Eric Laurent | baf35fe | 2016-07-27 15:36:53 -0700 | [diff] [blame] | 2262 | case SET_VOICE_VOLUME: { |
| 2263 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); |
| 2264 | VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get(); |
| 2265 | ALOGV("Filtering out voice volume command value %f replaced by %f", |
| 2266 | data2->mVolume, data->mVolume); |
| 2267 | removedCommands.add(command2); |
| 2268 | command->mTime = command2->mTime; |
| 2269 | // force delayMs to non 0 so that code below does not request to wait for |
| 2270 | // command status as the command is now delayed |
| 2271 | delayMs = 1; |
| 2272 | } break; |
| 2273 | |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2274 | case CREATE_AUDIO_PATCH: |
| 2275 | case RELEASE_AUDIO_PATCH: { |
| 2276 | audio_patch_handle_t handle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2277 | struct audio_patch patch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2278 | if (command->mCommand == CREATE_AUDIO_PATCH) { |
| 2279 | handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2280 | patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2281 | } else { |
| 2282 | handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle; |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 2283 | memset(&patch, 0, sizeof(patch)); |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2284 | } |
| 2285 | audio_patch_handle_t handle2; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2286 | struct audio_patch patch2; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2287 | if (command2->mCommand == CREATE_AUDIO_PATCH) { |
| 2288 | handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2289 | patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2290 | } else { |
| 2291 | handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle; |
Glenn Kasten | f60b6b6 | 2015-07-06 10:53:26 -0700 | [diff] [blame] | 2292 | memset(&patch2, 0, sizeof(patch2)); |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2293 | } |
| 2294 | if (handle != handle2) break; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 2295 | /* Filter CREATE_AUDIO_PATCH commands only when they are issued for |
| 2296 | same output. */ |
| 2297 | if( (command->mCommand == CREATE_AUDIO_PATCH) && |
| 2298 | (command2->mCommand == CREATE_AUDIO_PATCH) ) { |
| 2299 | bool isOutputDiff = false; |
| 2300 | if (patch.num_sources == patch2.num_sources) { |
| 2301 | for (unsigned count = 0; count < patch.num_sources; count++) { |
| 2302 | if (patch.sources[count].id != patch2.sources[count].id) { |
| 2303 | isOutputDiff = true; |
| 2304 | break; |
| 2305 | } |
| 2306 | } |
| 2307 | if (isOutputDiff) |
| 2308 | break; |
| 2309 | } |
| 2310 | } |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 2311 | ALOGV("Filtering out %s audio patch command for handle %d", |
| 2312 | (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle); |
| 2313 | removedCommands.add(command2); |
| 2314 | command->mTime = command2->mTime; |
| 2315 | // force delayMs to non 0 so that code below does not request to wait for |
| 2316 | // command status as the command is now delayed |
| 2317 | delayMs = 1; |
| 2318 | } break; |
| 2319 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2320 | case DYN_POLICY_MIX_STATE_UPDATE: { |
| 2321 | |
| 2322 | } break; |
| 2323 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2324 | case RECORDING_CONFIGURATION_UPDATE: { |
| 2325 | |
| 2326 | } break; |
| 2327 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2328 | case ROUTING_UPDATED: { |
| 2329 | |
| 2330 | } break; |
| 2331 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2332 | default: |
| 2333 | break; |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | // remove filtered commands |
| 2338 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 2339 | // removed commands always have time stamps greater than current command |
| 2340 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2341 | if (mAudioCommands[k].get() == removedCommands[j].get()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2342 | ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2343 | mAudioCommands.removeAt(k); |
| 2344 | break; |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | removedCommands.clear(); |
| 2349 | |
Eric Laurent | aa79bef | 2015-01-15 14:33:51 -0800 | [diff] [blame] | 2350 | // Disable wait for status if delay is not 0. |
| 2351 | // Except for create audio patch command because the returned patch handle |
| 2352 | // is needed by audio policy manager |
| 2353 | if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) { |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 2354 | command->mWaitStatus = false; |
| 2355 | } |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 2356 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2357 | // insert command at the right place according to its time stamp |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 2358 | ALOGV("inserting command: %d at index %zd, num commands %zu", |
| 2359 | command->mCommand, i+1, mAudioCommands.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2360 | mAudioCommands.insertAt(command, i + 1); |
| 2361 | } |
| 2362 | |
| 2363 | void AudioPolicyService::AudioCommandThread::exit() |
| 2364 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2365 | ALOGV("AudioCommandThread::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2366 | { |
| 2367 | AutoMutex _l(mLock); |
| 2368 | requestExit(); |
| 2369 | mWaitWorkCV.signal(); |
| 2370 | } |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 2371 | // Note that we can call it from the thread loop if all other references have been released |
| 2372 | // but it will safely return WOULD_BLOCK in this case |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2373 | requestExitAndWait(); |
| 2374 | } |
| 2375 | |
| 2376 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 2377 | { |
| 2378 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
| 2379 | mCommand, |
| 2380 | (int)ns2s(mTime), |
| 2381 | (int)ns2ms(mTime)%1000, |
| 2382 | mWaitStatus, |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 2383 | mParam.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2384 | } |
| 2385 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2386 | /******* helpers for the service_ops callbacks defined below *********/ |
| 2387 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 2388 | const char *keyValuePairs, |
| 2389 | int delayMs) |
| 2390 | { |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2391 | mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2392 | delayMs); |
| 2393 | } |
| 2394 | |
| 2395 | int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, |
| 2396 | float volume, |
| 2397 | audio_io_handle_t output, |
| 2398 | int delayMs) |
| 2399 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 2400 | return (int)mAudioCommandThread->volumeCommand(stream, volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2401 | output, delayMs); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2402 | } |
| 2403 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2404 | int AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 2405 | { |
| 2406 | return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 2407 | } |
| 2408 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2409 | void AudioPolicyService::setEffectSuspended(int effectId, |
| 2410 | audio_session_t sessionId, |
| 2411 | bool suspended) |
| 2412 | { |
| 2413 | mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended); |
| 2414 | } |
| 2415 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2416 | Status AudioPolicyService::onNewAudioModulesAvailable() |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2417 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 2418 | mOutputCommandThread->audioModulesUpdateCommand(); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2419 | return Status::ok(); |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 2422 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2423 | extern "C" { |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2424 | audio_module_handle_t aps_load_hw_module(void *service __unused, |
| 2425 | const char *name); |
| 2426 | audio_io_handle_t aps_open_output(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2427 | audio_devices_t *pDevices, |
| 2428 | uint32_t *pSamplingRate, |
| 2429 | audio_format_t *pFormat, |
| 2430 | audio_channel_mask_t *pChannelMask, |
| 2431 | uint32_t *pLatencyMs, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2432 | audio_output_flags_t flags); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2433 | |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2434 | audio_io_handle_t aps_open_output_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2435 | audio_module_handle_t module, |
| 2436 | audio_devices_t *pDevices, |
| 2437 | uint32_t *pSamplingRate, |
| 2438 | audio_format_t *pFormat, |
| 2439 | audio_channel_mask_t *pChannelMask, |
| 2440 | uint32_t *pLatencyMs, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 2441 | audio_output_flags_t flags, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2442 | const audio_offload_info_t *offloadInfo); |
| 2443 | audio_io_handle_t aps_open_dup_output(void *service __unused, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2444 | audio_io_handle_t output1, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2445 | audio_io_handle_t output2); |
| 2446 | int aps_close_output(void *service __unused, audio_io_handle_t output); |
| 2447 | int aps_suspend_output(void *service __unused, audio_io_handle_t output); |
| 2448 | int aps_restore_output(void *service __unused, audio_io_handle_t output); |
| 2449 | audio_io_handle_t aps_open_input(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2450 | audio_devices_t *pDevices, |
| 2451 | uint32_t *pSamplingRate, |
| 2452 | audio_format_t *pFormat, |
| 2453 | audio_channel_mask_t *pChannelMask, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2454 | audio_in_acoustics_t acoustics __unused); |
| 2455 | audio_io_handle_t aps_open_input_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2456 | audio_module_handle_t module, |
| 2457 | audio_devices_t *pDevices, |
| 2458 | uint32_t *pSamplingRate, |
| 2459 | audio_format_t *pFormat, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2460 | audio_channel_mask_t *pChannelMask); |
| 2461 | int aps_close_input(void *service __unused, audio_io_handle_t input); |
| 2462 | int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2463 | int aps_move_effects(void *service __unused, audio_session_t session, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2464 | audio_io_handle_t src_output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2465 | audio_io_handle_t dst_output); |
| 2466 | char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle, |
| 2467 | const char *keys); |
| 2468 | void aps_set_parameters(void *service, audio_io_handle_t io_handle, |
| 2469 | const char *kv_pairs, int delay_ms); |
| 2470 | int aps_set_stream_volume(void *service, audio_stream_type_t stream, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2471 | float volume, audio_io_handle_t output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2472 | int delay_ms); |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 2473 | int aps_set_voice_volume(void *service, float volume, int delay_ms); |
| 2474 | }; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 2475 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 2476 | } // namespace android |