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