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