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> |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 26 | |
| 27 | #include <audio_utils/clock.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <utils/Log.h> |
| 30 | #include <cutils/properties.h> |
| 31 | #include <binder/IPCThreadState.h> |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 32 | #include <binder/PermissionController.h> |
| 33 | #include <binder/IResultReceiver.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 34 | #include <utils/String16.h> |
| 35 | #include <utils/threads.h> |
| 36 | #include "AudioPolicyService.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <hardware_legacy/power.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 38 | #include <media/AudioEffect.h> |
Chih-Hung Hsieh | c84d9d2 | 2014-11-14 13:33:34 -0800 | [diff] [blame] | 39 | #include <media/AudioParameter.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 40 | #include <mediautils/ServiceUtilities.h> |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 41 | #include <sensorprivacy/SensorPrivacyManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 43 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 44 | #include <system/audio_policy.h> |
Mikhail Naganov | 61a4fac | 2016-10-13 14:44:18 -0700 | [diff] [blame] | 45 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | namespace android { |
| 47 | |
Glenn Kasten | 8dad0e3 | 2012-01-09 08:41:22 -0800 | [diff] [blame] | 48 | static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n"; |
| 49 | static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n"; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 51 | static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 53 | static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds |
Christer Fletcher | 5fa8c4b | 2013-01-18 15:27:03 +0100 | [diff] [blame] | 54 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 55 | static const String16 sManageAudioPolicyPermission("android.permission.MANAGE_AUDIO_POLICY"); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 56 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | AudioPolicyService::AudioPolicyService() |
Ytai Ben-Tsvi | 85093d5 | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 60 | : BnAudioPolicyService(), |
| 61 | mpAudioPolicyDev(NULL), |
| 62 | mpAudioPolicy(NULL), |
| 63 | mAudioPolicyManager(NULL), |
| 64 | mAudioPolicyClient(NULL), |
| 65 | mPhoneState(AUDIO_MODE_INVALID), |
| 66 | mCaptureStateNotifier(false) { |
Eric Laurent | f5ada6e | 2014-10-09 17:49:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void AudioPolicyService::onFirstRef() |
| 70 | { |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 71 | { |
| 72 | Mutex::Autolock _l(mLock); |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 73 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 74 | // start audio commands thread |
| 75 | mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this); |
| 76 | // start output activity command thread |
| 77 | mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 78 | |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 79 | mAudioPolicyClient = new AudioPolicyClient(this); |
| 80 | mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient); |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 81 | |
| 82 | mSupportedSystemUsages = std::vector<audio_usage_t> {}; |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 83 | } |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 84 | // load audio processing modules |
Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 85 | sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects(); |
| 86 | { |
| 87 | Mutex::Autolock _l(mLock); |
| 88 | mAudioPolicyEffects = audioPolicyEffects; |
| 89 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 90 | |
| 91 | mUidPolicy = new UidPolicy(this); |
| 92 | mUidPolicy->registerSelf(); |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 93 | |
| 94 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
| 95 | mSensorPrivacyPolicy->registerSelf(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | AudioPolicyService::~AudioPolicyService() |
| 99 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 100 | mAudioCommandThread->exit(); |
Eric Laurent | 657ff61 | 2014-05-07 11:58:24 -0700 | [diff] [blame] | 101 | mOutputCommandThread->exit(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 102 | |
Eric Laurent | f269b8e | 2014-06-09 20:01:29 -0700 | [diff] [blame] | 103 | destroyAudioPolicyManager(mAudioPolicyManager); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 104 | delete mAudioPolicyClient; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 105 | |
| 106 | mNotificationClients.clear(); |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 107 | mAudioPolicyEffects.clear(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 108 | |
| 109 | mUidPolicy->unregisterSelf(); |
| 110 | mUidPolicy.clear(); |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 111 | |
| 112 | mSensorPrivacyPolicy->unregisterSelf(); |
| 113 | mSensorPrivacyPolicy.clear(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // A notification client is always registered by AudioSystem when the client process |
| 117 | // connects to AudioPolicyService. |
| 118 | void AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client) |
| 119 | { |
Eric Laurent | 1259025 | 2015-08-21 18:40:20 -0700 | [diff] [blame] | 120 | if (client == 0) { |
| 121 | ALOGW("%s got NULL client", __FUNCTION__); |
| 122 | return; |
| 123 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 124 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 125 | |
| 126 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 127 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 128 | int64_t token = ((int64_t)uid<<32) | pid; |
| 129 | |
| 130 | if (mNotificationClients.indexOfKey(token) < 0) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 131 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 132 | client, |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 133 | uid, |
| 134 | pid); |
| 135 | ALOGV("registerClient() client %p, uid %d pid %d", client.get(), uid, pid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 136 | |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 137 | mNotificationClients.add(token, notificationClient); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 138 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 139 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 140 | binder->linkToDeath(notificationClient); |
| 141 | } |
| 142 | } |
| 143 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 144 | void AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled) |
| 145 | { |
| 146 | Mutex::Autolock _l(mNotificationClientsLock); |
| 147 | |
| 148 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 149 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 150 | int64_t token = ((int64_t)uid<<32) | pid; |
| 151 | |
| 152 | if (mNotificationClients.indexOfKey(token) < 0) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 153 | return; |
| 154 | } |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 155 | mNotificationClients.valueFor(token)->setAudioPortCallbacksEnabled(enabled); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 156 | } |
| 157 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 158 | void AudioPolicyService::setAudioVolumeGroupCallbacksEnabled(bool enabled) |
| 159 | { |
| 160 | Mutex::Autolock _l(mNotificationClientsLock); |
| 161 | |
| 162 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 163 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 164 | int64_t token = ((int64_t)uid<<32) | pid; |
| 165 | |
| 166 | if (mNotificationClients.indexOfKey(token) < 0) { |
| 167 | return; |
| 168 | } |
| 169 | mNotificationClients.valueFor(token)->setAudioVolumeGroupCallbacksEnabled(enabled); |
| 170 | } |
| 171 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 172 | // removeNotificationClient() is called when the client process dies. |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 173 | void AudioPolicyService::removeNotificationClient(uid_t uid, pid_t pid) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 174 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 175 | { |
| 176 | Mutex::Autolock _l(mNotificationClientsLock); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 177 | int64_t token = ((int64_t)uid<<32) | pid; |
| 178 | mNotificationClients.removeItem(token); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 179 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 180 | { |
| 181 | Mutex::Autolock _l(mLock); |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 182 | bool hasSameUid = false; |
| 183 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 184 | if (mNotificationClients.valueAt(i)->uid() == uid) { |
| 185 | hasSameUid = true; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | if (mAudioPolicyManager && !hasSameUid) { |
Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 190 | // called from binder death notification: no need to clear caller identity |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 191 | mAudioPolicyManager->releaseResourcesForUid(uid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 192 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 193 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void AudioPolicyService::onAudioPortListUpdate() |
| 197 | { |
| 198 | mOutputCommandThread->updateAudioPortListCommand(); |
| 199 | } |
| 200 | |
| 201 | void AudioPolicyService::doOnAudioPortListUpdate() |
| 202 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 203 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 204 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 205 | mNotificationClients.valueAt(i)->onAudioPortListUpdate(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void AudioPolicyService::onAudioPatchListUpdate() |
| 210 | { |
| 211 | mOutputCommandThread->updateAudioPatchListCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 214 | void AudioPolicyService::doOnAudioPatchListUpdate() |
| 215 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 216 | Mutex::Autolock _l(mNotificationClientsLock); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 217 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 218 | mNotificationClients.valueAt(i)->onAudioPatchListUpdate(); |
| 219 | } |
| 220 | } |
| 221 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 222 | void AudioPolicyService::onAudioVolumeGroupChanged(volume_group_t group, int flags) |
| 223 | { |
| 224 | mOutputCommandThread->changeAudioVolumeGroupCommand(group, flags); |
| 225 | } |
| 226 | |
| 227 | void AudioPolicyService::doOnAudioVolumeGroupChanged(volume_group_t group, int flags) |
| 228 | { |
| 229 | Mutex::Autolock _l(mNotificationClientsLock); |
| 230 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 231 | mNotificationClients.valueAt(i)->onAudioVolumeGroupChanged(group, flags); |
| 232 | } |
| 233 | } |
| 234 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 235 | void AudioPolicyService::onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 236 | { |
| 237 | ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)", |
| 238 | regId.string(), state); |
| 239 | mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state); |
| 240 | } |
| 241 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 242 | void AudioPolicyService::doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 243 | { |
| 244 | Mutex::Autolock _l(mNotificationClientsLock); |
| 245 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
| 246 | mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state); |
| 247 | } |
| 248 | } |
| 249 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 250 | void AudioPolicyService::onRecordingConfigurationUpdate( |
| 251 | int event, |
| 252 | const record_client_info_t *clientInfo, |
| 253 | const audio_config_base_t *clientConfig, |
| 254 | std::vector<effect_descriptor_t> clientEffects, |
| 255 | const audio_config_base_t *deviceConfig, |
| 256 | std::vector<effect_descriptor_t> effects, |
| 257 | audio_patch_handle_t patchHandle, |
| 258 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 259 | { |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 260 | mOutputCommandThread->recordingConfigurationUpdateCommand(event, clientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 261 | clientConfig, clientEffects, deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 264 | void AudioPolicyService::doOnRecordingConfigurationUpdate( |
| 265 | int event, |
| 266 | const record_client_info_t *clientInfo, |
| 267 | const audio_config_base_t *clientConfig, |
| 268 | std::vector<effect_descriptor_t> clientEffects, |
| 269 | const audio_config_base_t *deviceConfig, |
| 270 | std::vector<effect_descriptor_t> effects, |
| 271 | audio_patch_handle_t patchHandle, |
| 272 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 273 | { |
| 274 | Mutex::Autolock _l(mNotificationClientsLock); |
| 275 | for (size_t i = 0; i < mNotificationClients.size(); i++) { |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 276 | mNotificationClients.valueAt(i)->onRecordingConfigurationUpdate(event, clientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 277 | clientConfig, clientEffects, deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch, |
| 282 | audio_patch_handle_t *handle, |
| 283 | int delayMs) |
| 284 | { |
| 285 | return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs); |
| 286 | } |
| 287 | |
| 288 | status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle, |
| 289 | int delayMs) |
| 290 | { |
| 291 | return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs); |
| 292 | } |
| 293 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 294 | status_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config, |
| 295 | int delayMs) |
| 296 | { |
| 297 | return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs); |
| 298 | } |
| 299 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 300 | AudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service, |
| 301 | const sp<IAudioPolicyServiceClient>& client, |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 302 | uid_t uid, |
| 303 | pid_t pid) |
| 304 | : mService(service), mUid(uid), mPid(pid), mAudioPolicyServiceClient(client), |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 305 | mAudioPortCallbacksEnabled(false), mAudioVolumeGroupCallbacksEnabled(false) |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 306 | { |
| 307 | } |
| 308 | |
| 309 | AudioPolicyService::NotificationClient::~NotificationClient() |
| 310 | { |
| 311 | } |
| 312 | |
| 313 | void AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
| 314 | { |
| 315 | sp<NotificationClient> keep(this); |
| 316 | sp<AudioPolicyService> service = mService.promote(); |
| 317 | if (service != 0) { |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 318 | service->removeNotificationClient(mUid, mPid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | void AudioPolicyService::NotificationClient::onAudioPortListUpdate() |
| 323 | { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 324 | if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 325 | mAudioPolicyServiceClient->onAudioPortListUpdate(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void AudioPolicyService::NotificationClient::onAudioPatchListUpdate() |
| 330 | { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 331 | if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) { |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 332 | mAudioPolicyServiceClient->onAudioPatchListUpdate(); |
| 333 | } |
| 334 | } |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 335 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 336 | void AudioPolicyService::NotificationClient::onAudioVolumeGroupChanged(volume_group_t group, |
| 337 | int flags) |
| 338 | { |
| 339 | if (mAudioPolicyServiceClient != 0 && mAudioVolumeGroupCallbacksEnabled) { |
| 340 | mAudioPolicyServiceClient->onAudioVolumeGroupChanged(group, flags); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 345 | void AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 346 | const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 347 | { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 348 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 349 | mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void AudioPolicyService::NotificationClient::onRecordingConfigurationUpdate( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 354 | int event, |
| 355 | const record_client_info_t *clientInfo, |
| 356 | const audio_config_base_t *clientConfig, |
| 357 | std::vector<effect_descriptor_t> clientEffects, |
| 358 | const audio_config_base_t *deviceConfig, |
| 359 | std::vector<effect_descriptor_t> effects, |
| 360 | audio_patch_handle_t patchHandle, |
| 361 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 362 | { |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 363 | if (mAudioPolicyServiceClient != 0 && isServiceUid(mUid)) { |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 364 | mAudioPolicyServiceClient->onRecordingConfigurationUpdate(event, clientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 365 | clientConfig, clientEffects, deviceConfig, effects, patchHandle, source); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 369 | void AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled) |
| 370 | { |
| 371 | mAudioPortCallbacksEnabled = enabled; |
| 372 | } |
| 373 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 374 | void AudioPolicyService::NotificationClient::setAudioVolumeGroupCallbacksEnabled(bool enabled) |
| 375 | { |
| 376 | mAudioVolumeGroupCallbacksEnabled = enabled; |
| 377 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 378 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 379 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Glenn Kasten | 411e447 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 380 | ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(), |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 381 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 384 | static bool dumpTryLock(Mutex& mutex) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 385 | { |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 386 | status_t err = mutex.timedLock(kDumpLockTimeoutNs); |
| 387 | return err == NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | status_t AudioPolicyService::dumpInternals(int fd) |
| 391 | { |
| 392 | const size_t SIZE = 256; |
| 393 | char buffer[SIZE]; |
| 394 | String8 result; |
| 395 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 396 | snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 397 | result.append(buffer); |
| 398 | snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); |
| 399 | result.append(buffer); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 400 | |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 401 | snprintf(buffer, SIZE, "Supported System Usages:\n"); |
| 402 | result.append(buffer); |
| 403 | for (std::vector<audio_usage_t>::iterator it = mSupportedSystemUsages.begin(); |
| 404 | it != mSupportedSystemUsages.end(); ++it) { |
| 405 | snprintf(buffer, SIZE, "\t%d\n", *it); |
| 406 | result.append(buffer); |
| 407 | } |
| 408 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 409 | write(fd, result.string(), result.size()); |
| 410 | return NO_ERROR; |
| 411 | } |
| 412 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 413 | void AudioPolicyService::updateUidStates() |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 414 | { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 415 | Mutex::Autolock _l(mLock); |
| 416 | updateUidStates_l(); |
| 417 | } |
| 418 | |
| 419 | void AudioPolicyService::updateUidStates_l() |
| 420 | { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 421 | // Go over all active clients and allow capture (does not force silence) in the |
| 422 | // following cases: |
Eric Laurent | 8ed73c1 | 2019-08-30 17:25:19 -0700 | [diff] [blame] | 423 | // The client is the assistant |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 424 | // 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] | 425 | // AND the source is VOICE_RECOGNITION or HOTWORD |
| 426 | // OR uses VOICE_RECOGNITION AND is on TOP |
| 427 | // OR uses HOTWORD |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 428 | // AND there is no active privacy sensitive capture or call |
| 429 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 430 | // OR The client is an accessibility service |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 431 | // AND Is on TOP |
| 432 | // AND the source is VOICE_RECOGNITION or HOTWORD |
| 433 | // OR The assistant is not on TOP |
| 434 | // AND there is no active privacy sensitive capture or call |
| 435 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 436 | // AND is on TOP |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 437 | // AND the source is VOICE_RECOGNITION or HOTWORD |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 438 | // OR the client source is virtual (remote submix, call audio TX or RX...) |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 439 | // OR the client source is HOTWORD |
| 440 | // AND is on TOP |
| 441 | // OR all active clients are using HOTWORD source |
| 442 | // AND no call is active |
| 443 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 444 | // OR the client is the current InputMethodService |
| 445 | // AND a RTT call is active AND the source is VOICE_RECOGNITION |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 446 | // OR Any client |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 447 | // AND The assistant is not on TOP |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 448 | // AND is on TOP or latest started |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 449 | // AND there is no active privacy sensitive capture or call |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 450 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 451 | |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 452 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 453 | sp<AudioRecordClient> topActive; |
| 454 | sp<AudioRecordClient> latestActive; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 455 | sp<AudioRecordClient> topSensitiveActive; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 456 | sp<AudioRecordClient> latestSensitiveActive; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 457 | |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 458 | nsecs_t topStartNs = 0; |
| 459 | nsecs_t latestStartNs = 0; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 460 | nsecs_t topSensitiveStartNs = 0; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 461 | nsecs_t latestSensitiveStartNs = 0; |
| 462 | bool isA11yOnTop = mUidPolicy->isA11yOnTop(); |
| 463 | bool isAssistantOnTop = false; |
| 464 | bool isSensitiveActive = false; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 465 | bool isInCall = mPhoneState == AUDIO_MODE_IN_CALL; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 466 | bool isInCommunication = mPhoneState == AUDIO_MODE_IN_COMMUNICATION; |
| 467 | bool rttCallActive = (isInCall || isInCommunication) |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 468 | && mUidPolicy->isRttEnabled(); |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 469 | bool onlyHotwordActive = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 470 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 471 | // if Sensor Privacy is enabled then all recordings should be silenced. |
| 472 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 473 | silenceAllRecordings_l(); |
| 474 | return; |
| 475 | } |
| 476 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 477 | for (size_t i =0; i < mAudioRecordClients.size(); i++) { |
| 478 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 479 | if (!current->active) { |
| 480 | continue; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 481 | } |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 482 | |
| 483 | app_state_t appState = apmStatFromAmState(mUidPolicy->getUidState(current->uid)); |
| 484 | // clients which app is in IDLE state are not eligible for top active or |
| 485 | // latest active |
| 486 | if (appState == APP_STATE_IDLE) { |
| 487 | continue; |
| 488 | } |
| 489 | |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 490 | bool isAccessibility = mUidPolicy->isA11yUid(current->uid); |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 491 | // Clients capturing for Accessibility services are not considered |
| 492 | // for top or latest active to avoid masking regular clients started before |
| 493 | if (!isAccessibility) { |
| 494 | bool isAssistant = mUidPolicy->isAssistantUid(current->uid); |
| 495 | bool isPrivacySensitive = |
| 496 | (current->attributes.flags & AUDIO_FLAG_CAPTURE_PRIVATE) != 0; |
| 497 | if (appState == APP_STATE_TOP) { |
| 498 | if (isPrivacySensitive) { |
| 499 | if (current->startTimeNs > topSensitiveStartNs) { |
| 500 | topSensitiveActive = current; |
| 501 | topSensitiveStartNs = current->startTimeNs; |
| 502 | } |
| 503 | } else { |
| 504 | if (current->startTimeNs > topStartNs) { |
| 505 | topActive = current; |
| 506 | topStartNs = current->startTimeNs; |
| 507 | } |
| 508 | } |
| 509 | if (isAssistant) { |
| 510 | isAssistantOnTop = true; |
| 511 | } |
Eric Laurent | a46bedb | 2018-12-07 18:01:26 -0800 | [diff] [blame] | 512 | } |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 513 | // Clients capturing for HOTWORD are not considered |
| 514 | // for latest active to avoid masking regular clients started before |
| 515 | if (!(current->attributes.source == AUDIO_SOURCE_HOTWORD |
| 516 | || ((isA11yOnTop || rttCallActive) && isAssistant))) { |
| 517 | if (isPrivacySensitive) { |
| 518 | if (current->startTimeNs > latestSensitiveStartNs) { |
| 519 | latestSensitiveActive = current; |
| 520 | latestSensitiveStartNs = current->startTimeNs; |
| 521 | } |
| 522 | isSensitiveActive = true; |
| 523 | } else { |
| 524 | if (current->startTimeNs > latestStartNs) { |
| 525 | latestActive = current; |
| 526 | latestStartNs = current->startTimeNs; |
| 527 | } |
| 528 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 531 | if (current->attributes.source != AUDIO_SOURCE_HOTWORD) { |
| 532 | onlyHotwordActive = false; |
| 533 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 536 | // if no active client with UI on Top, consider latest active as top |
| 537 | if (topActive == nullptr) { |
| 538 | topActive = latestActive; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 539 | topStartNs = latestStartNs; |
| 540 | } |
| 541 | if (topSensitiveActive == nullptr) { |
| 542 | topSensitiveActive = latestSensitiveActive; |
| 543 | topSensitiveStartNs = latestSensitiveStartNs; |
| 544 | } |
| 545 | |
| 546 | // If both privacy sensitive and regular capture are active: |
| 547 | // if the regular capture is privileged |
| 548 | // allow concurrency |
| 549 | // else |
| 550 | // favor the privacy sensitive case |
| 551 | if (topActive != nullptr && topSensitiveActive != nullptr |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 552 | && !topActive->canCaptureOutput) { |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 553 | topActive = nullptr; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | for (size_t i =0; i < mAudioRecordClients.size(); i++) { |
| 557 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 558 | if (!current->active) { |
| 559 | continue; |
| 560 | } |
| 561 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 562 | audio_source_t source = current->attributes.source; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 563 | bool isTopOrLatestActive = topActive == nullptr ? false : current->uid == topActive->uid; |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 564 | bool isTopOrLatestSensitive = topSensitiveActive == nullptr ? |
| 565 | false : current->uid == topSensitiveActive->uid; |
| 566 | |
| 567 | auto canCaptureIfInCallOrCommunication = [&](const auto &recordClient) { |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 568 | bool canCaptureCall = recordClient->canCaptureOutput; |
| 569 | bool canCaptureCommunication = recordClient->canCaptureOutput |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 570 | || recordClient->uid == mPhoneStateOwnerUid |
| 571 | || isServiceUid(mPhoneStateOwnerUid); |
| 572 | return !(isInCall && !canCaptureCall) |
| 573 | && !(isInCommunication && !canCaptureCommunication); |
| 574 | }; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 575 | |
| 576 | // By default allow capture if: |
| 577 | // The assistant is not on TOP |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 578 | // AND is on TOP or latest started |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 579 | // AND there is no active privacy sensitive capture or call |
| 580 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
| 581 | bool allowCapture = !isAssistantOnTop |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 582 | && (isTopOrLatestActive || isTopOrLatestSensitive) |
| 583 | && !(isSensitiveActive |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 584 | && !(isTopOrLatestSensitive || current->canCaptureOutput)) |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 585 | && canCaptureIfInCallOrCommunication(current); |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 586 | |
| 587 | if (isVirtualSource(source)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 588 | // Allow capture for virtual (remote submix, call audio TX or RX...) sources |
| 589 | allowCapture = true; |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 590 | } else if (mUidPolicy->isAssistantUid(current->uid)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 591 | // For assistant allow capture if: |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 592 | // An accessibility service is on TOP or a RTT call is active |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 593 | // AND the source is VOICE_RECOGNITION or HOTWORD |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 594 | // OR is on TOP AND uses VOICE_RECOGNITION |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 595 | // OR uses HOTWORD |
| 596 | // AND there is no active privacy sensitive capture or call |
| 597 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 598 | if (isA11yOnTop || rttCallActive) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 599 | if (source == AUDIO_SOURCE_HOTWORD || source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 600 | allowCapture = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 601 | } |
| 602 | } else { |
Eric Laurent | a171e35 | 2019-05-07 13:04:45 -0700 | [diff] [blame] | 603 | if (((isAssistantOnTop && source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 604 | source == AUDIO_SOURCE_HOTWORD) |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 605 | && !(isSensitiveActive && !current->canCaptureOutput) |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 606 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 607 | allowCapture = true; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | } else if (mUidPolicy->isA11yUid(current->uid)) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 611 | // For accessibility service allow capture if: |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 612 | // The assistant is not on TOP |
| 613 | // AND there is no active privacy sensitive capture or call |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 614 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 615 | // OR |
| 616 | // Is on TOP AND the source is VOICE_RECOGNITION or HOTWORD |
| 617 | if (!isAssistantOnTop |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 618 | && !(isSensitiveActive && !current->canCaptureOutput) |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 619 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 620 | allowCapture = true; |
| 621 | } |
Eric Laurent | 589171c | 2019-07-25 18:04:29 -0700 | [diff] [blame] | 622 | if (isA11yOnTop) { |
| 623 | if (source == AUDIO_SOURCE_VOICE_RECOGNITION || source == AUDIO_SOURCE_HOTWORD) { |
| 624 | allowCapture = true; |
| 625 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 626 | } |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 627 | } else if (source == AUDIO_SOURCE_HOTWORD) { |
| 628 | // For HOTWORD source allow capture when not on TOP if: |
| 629 | // All active clients are using HOTWORD source |
| 630 | // AND no call is active |
| 631 | // OR client has CAPTURE_AUDIO_OUTPUT privileged permission |
Eric Laurent | c21d569 | 2020-02-25 10:24:36 -0800 | [diff] [blame] | 632 | if (onlyHotwordActive |
| 633 | && canCaptureIfInCallOrCommunication(current)) { |
Eric Laurent | 4e947da | 2019-10-17 15:24:06 -0700 | [diff] [blame] | 634 | allowCapture = true; |
| 635 | } |
Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 636 | } else if (mUidPolicy->isCurrentImeUid(current->uid)) { |
| 637 | // For current InputMethodService allow capture if: |
| 638 | // A RTT call is active AND the source is VOICE_RECOGNITION |
| 639 | if (rttCallActive && source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
| 640 | allowCapture = true; |
| 641 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 642 | } |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 643 | setAppState_l(current->portId, |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 644 | allowCapture ? apmStatFromAmState(mUidPolicy->getUidState(current->uid)) : |
| 645 | APP_STATE_IDLE); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 649 | void AudioPolicyService::silenceAllRecordings_l() { |
| 650 | for (size_t i = 0; i < mAudioRecordClients.size(); i++) { |
| 651 | sp<AudioRecordClient> current = mAudioRecordClients[i]; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 652 | if (!isVirtualSource(current->attributes.source)) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 653 | setAppState_l(current->portId, APP_STATE_IDLE); |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 654 | } |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 658 | /* static */ |
| 659 | app_state_t AudioPolicyService::apmStatFromAmState(int amState) { |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 660 | |
| 661 | if (amState == ActivityManager::PROCESS_STATE_UNKNOWN) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 662 | return APP_STATE_IDLE; |
Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 663 | } else if (amState <= ActivityManager::PROCESS_STATE_TOP) { |
| 664 | // include persistent services |
| 665 | return APP_STATE_TOP; |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 666 | } |
| 667 | return APP_STATE_FOREGROUND; |
| 668 | } |
| 669 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 670 | /* static */ |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 671 | bool AudioPolicyService::isVirtualSource(audio_source_t source) |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 672 | { |
| 673 | switch (source) { |
| 674 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 675 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 676 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 2dc962b | 2019-03-01 08:25:25 -0800 | [diff] [blame] | 677 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
| 678 | case AUDIO_SOURCE_FM_TUNER: |
Eric Laurent | 68eb212 | 2020-04-30 17:40:57 -0700 | [diff] [blame^] | 679 | case AUDIO_SOURCE_ECHO_REFERENCE: |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 680 | return true; |
| 681 | default: |
| 682 | break; |
| 683 | } |
| 684 | return false; |
| 685 | } |
| 686 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 687 | void AudioPolicyService::setAppState_l(audio_port_handle_t portId, app_state_t state) |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 688 | { |
| 689 | AutoCallerClear acc; |
| 690 | |
| 691 | if (mAudioPolicyManager) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 692 | mAudioPolicyManager->setAppState(portId, state); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 693 | } |
| 694 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 695 | if (af) { |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 696 | bool silenced = state == APP_STATE_IDLE; |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 697 | af->setRecordSilenced(portId, silenced); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 698 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 701 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 702 | { |
Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 703 | if (!dumpAllowed()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 704 | dumpPermissionDenial(fd); |
| 705 | } else { |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 706 | bool locked = dumpTryLock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 707 | if (!locked) { |
| 708 | String8 result(kDeadlockedString); |
| 709 | write(fd, result.string(), result.size()); |
| 710 | } |
| 711 | |
| 712 | dumpInternals(fd); |
Glenn Kasten | 9d1f02d | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 713 | if (mAudioCommandThread != 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 714 | mAudioCommandThread->dump(fd); |
| 715 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 716 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 717 | if (mAudioPolicyManager) { |
| 718 | mAudioPolicyManager->dump(fd); |
| 719 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 720 | |
Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 721 | mPackageManager.dump(fd); |
| 722 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 723 | if (locked) mLock.unlock(); |
| 724 | } |
| 725 | return NO_ERROR; |
| 726 | } |
| 727 | |
| 728 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
| 729 | { |
| 730 | const size_t SIZE = 256; |
| 731 | char buffer[SIZE]; |
| 732 | String8 result; |
| 733 | snprintf(buffer, SIZE, "Permission Denial: " |
| 734 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 735 | IPCThreadState::self()->getCallingPid(), |
| 736 | IPCThreadState::self()->getCallingUid()); |
| 737 | result.append(buffer); |
| 738 | write(fd, result.string(), result.size()); |
| 739 | return NO_ERROR; |
| 740 | } |
| 741 | |
| 742 | status_t AudioPolicyService::onTransact( |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 743 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 744 | switch (code) { |
| 745 | case SHELL_COMMAND_TRANSACTION: { |
| 746 | int in = data.readFileDescriptor(); |
| 747 | int out = data.readFileDescriptor(); |
| 748 | int err = data.readFileDescriptor(); |
| 749 | int argc = data.readInt32(); |
| 750 | Vector<String16> args; |
| 751 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 752 | args.add(data.readString16()); |
| 753 | } |
| 754 | sp<IBinder> unusedCallback; |
| 755 | sp<IResultReceiver> resultReceiver; |
| 756 | status_t status; |
| 757 | if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { |
| 758 | return status; |
| 759 | } |
| 760 | if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { |
| 761 | return status; |
| 762 | } |
| 763 | status = shellCommand(in, out, err, args); |
| 764 | if (resultReceiver != nullptr) { |
| 765 | resultReceiver->send(status); |
| 766 | } |
| 767 | return NO_ERROR; |
| 768 | } |
| 769 | } |
| 770 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 771 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 772 | } |
| 773 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 774 | // ------------------- Shell command implementation ------------------- |
| 775 | |
| 776 | // NOTE: This is a remote API - make sure all args are validated |
| 777 | status_t AudioPolicyService::shellCommand(int in, int out, int err, Vector<String16>& args) { |
| 778 | if (!checkCallingPermission(sManageAudioPolicyPermission, nullptr, nullptr)) { |
| 779 | return PERMISSION_DENIED; |
| 780 | } |
| 781 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 782 | return BAD_VALUE; |
| 783 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 784 | if (args.size() >= 3 && args[0] == String16("set-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 785 | return handleSetUidState(args, err); |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 786 | } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 787 | return handleResetUidState(args, err); |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 788 | } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 789 | return handleGetUidState(args, out, err); |
| 790 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 791 | printHelp(out); |
| 792 | return NO_ERROR; |
| 793 | } |
| 794 | printHelp(err); |
| 795 | return BAD_VALUE; |
| 796 | } |
| 797 | |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 798 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
| 799 | if (userId < 0) { |
| 800 | ALOGE("Invalid user: %d", userId); |
| 801 | dprintf(err, "Invalid user: %d\n", userId); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 802 | return BAD_VALUE; |
| 803 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 804 | |
| 805 | PermissionController pc; |
| 806 | uid = pc.getPackageUid(packageName, 0); |
| 807 | if (uid <= 0) { |
| 808 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 809 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
| 810 | return BAD_VALUE; |
| 811 | } |
| 812 | |
| 813 | uid = multiuser_get_uid(userId, uid); |
| 814 | return NO_ERROR; |
| 815 | } |
| 816 | |
| 817 | status_t AudioPolicyService::handleSetUidState(Vector<String16>& args, int err) { |
| 818 | // Valid arg.size() is 3 or 5, args.size() is 5 with --user option. |
| 819 | if (!(args.size() == 3 || args.size() == 5)) { |
| 820 | printHelp(err); |
| 821 | return BAD_VALUE; |
| 822 | } |
| 823 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 824 | bool active = false; |
| 825 | if (args[2] == String16("active")) { |
| 826 | active = true; |
| 827 | } else if ((args[2] != String16("idle"))) { |
| 828 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 829 | return BAD_VALUE; |
| 830 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 831 | |
| 832 | int userId = 0; |
| 833 | if (args.size() >= 5 && args[3] == String16("--user")) { |
| 834 | userId = atoi(String8(args[4])); |
| 835 | } |
| 836 | |
| 837 | uid_t uid; |
| 838 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 839 | return BAD_VALUE; |
| 840 | } |
| 841 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 842 | mUidPolicy->addOverrideUid(uid, active); |
| 843 | return NO_ERROR; |
| 844 | } |
| 845 | |
| 846 | status_t AudioPolicyService::handleResetUidState(Vector<String16>& args, int err) { |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 847 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 848 | if (!(args.size() == 2 || args.size() == 4)) { |
| 849 | printHelp(err); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 850 | return BAD_VALUE; |
| 851 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 852 | |
| 853 | int userId = 0; |
| 854 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 855 | userId = atoi(String8(args[3])); |
| 856 | } |
| 857 | |
| 858 | uid_t uid; |
| 859 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 860 | return BAD_VALUE; |
| 861 | } |
| 862 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 863 | mUidPolicy->removeOverrideUid(uid); |
| 864 | return NO_ERROR; |
| 865 | } |
| 866 | |
| 867 | status_t AudioPolicyService::handleGetUidState(Vector<String16>& args, int out, int err) { |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 868 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 869 | if (!(args.size() == 2 || args.size() == 4)) { |
| 870 | printHelp(err); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 871 | return BAD_VALUE; |
| 872 | } |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 873 | |
| 874 | int userId = 0; |
| 875 | if (args.size() >= 4 && args[2] == String16("--user")) { |
| 876 | userId = atoi(String8(args[3])); |
| 877 | } |
| 878 | |
| 879 | uid_t uid; |
| 880 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 881 | return BAD_VALUE; |
| 882 | } |
| 883 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 884 | if (mUidPolicy->isUidActive(uid)) { |
| 885 | return dprintf(out, "active\n"); |
| 886 | } else { |
| 887 | return dprintf(out, "idle\n"); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | status_t AudioPolicyService::printHelp(int out) { |
| 892 | return dprintf(out, "Audio policy service commands:\n" |
jovanak | be066e1 | 2019-09-02 11:54:39 -0700 | [diff] [blame] | 893 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 894 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 895 | " 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] | 896 | " help print this message\n"); |
| 897 | } |
| 898 | |
| 899 | // ----------- AudioPolicyService::UidPolicy implementation ---------- |
| 900 | |
| 901 | void AudioPolicyService::UidPolicy::registerSelf() { |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 902 | status_t res = mAm.linkToDeath(this); |
| 903 | mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 904 | | ActivityManager::UID_OBSERVER_IDLE |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 905 | | ActivityManager::UID_OBSERVER_ACTIVE |
| 906 | | ActivityManager::UID_OBSERVER_PROCSTATE, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 907 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 908 | String16("audioserver")); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 909 | if (!res) { |
| 910 | Mutex::Autolock _l(mLock); |
| 911 | mObserverRegistered = true; |
| 912 | } else { |
| 913 | ALOGE("UidPolicy::registerSelf linkToDeath failed: %d", res); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 914 | |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 915 | mAm.unregisterUidObserver(this); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 916 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void AudioPolicyService::UidPolicy::unregisterSelf() { |
Steven Moreland | 2f34814 | 2019-07-02 15:59:07 -0700 | [diff] [blame] | 920 | mAm.unlinkToDeath(this); |
| 921 | mAm.unregisterUidObserver(this); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 922 | Mutex::Autolock _l(mLock); |
| 923 | mObserverRegistered = false; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 926 | void AudioPolicyService::UidPolicy::binderDied(__unused const wp<IBinder> &who) { |
| 927 | Mutex::Autolock _l(mLock); |
| 928 | mCachedUids.clear(); |
| 929 | mObserverRegistered = false; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 932 | void AudioPolicyService::UidPolicy::checkRegistered() { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 933 | bool needToReregister = false; |
| 934 | { |
| 935 | Mutex::Autolock _l(mLock); |
| 936 | needToReregister = !mObserverRegistered; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 937 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 938 | if (needToReregister) { |
| 939 | // Looks like ActivityManager has died previously, attempt to re-register. |
| 940 | registerSelf(); |
| 941 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | bool AudioPolicyService::UidPolicy::isUidActive(uid_t uid) { |
| 945 | if (isServiceUid(uid)) return true; |
| 946 | checkRegistered(); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 947 | { |
| 948 | Mutex::Autolock _l(mLock); |
| 949 | auto overrideIter = mOverrideUids.find(uid); |
| 950 | if (overrideIter != mOverrideUids.end()) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 951 | return overrideIter->second.first; |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 952 | } |
| 953 | // In an absense of the ActivityManager, assume everything to be active. |
| 954 | if (!mObserverRegistered) return true; |
| 955 | auto cacheIter = mCachedUids.find(uid); |
Mikhail Naganov | eba668a | 2018-04-05 08:13:15 -0700 | [diff] [blame] | 956 | if (cacheIter != mCachedUids.end()) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 957 | return cacheIter->second.first; |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | ActivityManager am; |
Hui Yu | ffffffc | 2020-03-24 16:13:59 -0700 | [diff] [blame] | 961 | bool active = am.isUidActiveOrForeground(uid, String16("audioserver")); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 962 | { |
| 963 | Mutex::Autolock _l(mLock); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 964 | mCachedUids.insert(std::pair<uid_t, |
| 965 | std::pair<bool, int>>(uid, std::pair<bool, int>(active, |
| 966 | ActivityManager::PROCESS_STATE_UNKNOWN))); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 967 | } |
| 968 | return active; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 969 | } |
| 970 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 971 | int AudioPolicyService::UidPolicy::getUidState(uid_t uid) { |
| 972 | if (isServiceUid(uid)) { |
| 973 | return ActivityManager::PROCESS_STATE_TOP; |
| 974 | } |
| 975 | checkRegistered(); |
| 976 | { |
| 977 | Mutex::Autolock _l(mLock); |
| 978 | auto overrideIter = mOverrideUids.find(uid); |
| 979 | if (overrideIter != mOverrideUids.end()) { |
| 980 | if (overrideIter->second.first) { |
| 981 | if (overrideIter->second.second != ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 982 | return overrideIter->second.second; |
| 983 | } else { |
| 984 | auto cacheIter = mCachedUids.find(uid); |
| 985 | if (cacheIter != mCachedUids.end()) { |
| 986 | return cacheIter->second.second; |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | return ActivityManager::PROCESS_STATE_UNKNOWN; |
| 991 | } |
| 992 | // In an absense of the ActivityManager, assume everything to be active. |
| 993 | if (!mObserverRegistered) { |
| 994 | return ActivityManager::PROCESS_STATE_TOP; |
| 995 | } |
| 996 | auto cacheIter = mCachedUids.find(uid); |
| 997 | if (cacheIter != mCachedUids.end()) { |
| 998 | if (cacheIter->second.first) { |
| 999 | return cacheIter->second.second; |
| 1000 | } else { |
| 1001 | return ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | ActivityManager am; |
Hui Yu | ffffffc | 2020-03-24 16:13:59 -0700 | [diff] [blame] | 1006 | bool active = am.isUidActiveOrForeground(uid, String16("audioserver")); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1007 | int state = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1008 | if (active) { |
| 1009 | state = am.getUidProcessState(uid, String16("audioserver")); |
| 1010 | } |
| 1011 | { |
| 1012 | Mutex::Autolock _l(mLock); |
| 1013 | mCachedUids.insert(std::pair<uid_t, |
| 1014 | std::pair<bool, int>>(uid, std::pair<bool, int>(active, state))); |
| 1015 | } |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1016 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1017 | return state; |
| 1018 | } |
| 1019 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1020 | void AudioPolicyService::UidPolicy::onUidActive(uid_t uid) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1021 | updateUid(&mCachedUids, uid, true, ActivityManager::PROCESS_STATE_UNKNOWN, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | void AudioPolicyService::UidPolicy::onUidGone(uid_t uid, __unused bool disabled) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1025 | updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, false); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | void AudioPolicyService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1029 | updateUid(&mCachedUids, uid, false, ActivityManager::PROCESS_STATE_UNKNOWN, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1032 | void AudioPolicyService::UidPolicy::onUidStateChanged(uid_t uid, |
| 1033 | int32_t procState, |
Hui Yu | 13ad0eb | 2019-09-09 10:27:07 -0700 | [diff] [blame] | 1034 | int64_t procStateSeq __unused, |
| 1035 | int32_t capability __unused) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1036 | if (procState != ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1037 | updateUid(&mCachedUids, uid, true, procState, true); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | void AudioPolicyService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1042 | updateUid(&mOverrideUids, uid, active, ActivityManager::PROCESS_STATE_UNKNOWN, insert); |
| 1043 | } |
| 1044 | |
| 1045 | void AudioPolicyService::UidPolicy::notifyService() { |
| 1046 | sp<AudioPolicyService> service = mService.promote(); |
| 1047 | if (service != nullptr) { |
| 1048 | service->updateUidStates(); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1052 | void AudioPolicyService::UidPolicy::updateUid(std::unordered_map<uid_t, |
| 1053 | std::pair<bool, int>> *uids, |
| 1054 | uid_t uid, |
| 1055 | bool active, |
| 1056 | int state, |
| 1057 | bool insert) { |
| 1058 | if (isServiceUid(uid)) { |
| 1059 | return; |
| 1060 | } |
| 1061 | bool wasActive = isUidActive(uid); |
| 1062 | int previousState = getUidState(uid); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1063 | { |
| 1064 | Mutex::Autolock _l(mLock); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1065 | updateUidLocked(uids, uid, active, state, insert); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1066 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1067 | if (wasActive != isUidActive(uid) || state != previousState) { |
| 1068 | notifyService(); |
| 1069 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1072 | void AudioPolicyService::UidPolicy::updateUidLocked(std::unordered_map<uid_t, |
| 1073 | std::pair<bool, int>> *uids, |
| 1074 | uid_t uid, |
| 1075 | bool active, |
| 1076 | int state, |
| 1077 | bool insert) { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1078 | auto it = uids->find(uid); |
| 1079 | if (it != uids->end()) { |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1080 | if (insert) { |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1081 | if (state == ActivityManager::PROCESS_STATE_UNKNOWN) { |
| 1082 | it->second.first = active; |
| 1083 | } |
| 1084 | if (it->second.first) { |
| 1085 | it->second.second = state; |
| 1086 | } else { |
| 1087 | it->second.second = ActivityManager::PROCESS_STATE_UNKNOWN; |
| 1088 | } |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1089 | } else { |
| 1090 | uids->erase(it); |
| 1091 | } |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1092 | } else if (insert && (state == ActivityManager::PROCESS_STATE_UNKNOWN)) { |
| 1093 | uids->insert(std::pair<uid_t, std::pair<bool, int>>(uid, |
| 1094 | std::pair<bool, int>(active, state))); |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 1095 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1096 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1097 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1098 | bool AudioPolicyService::UidPolicy::isA11yOnTop() { |
| 1099 | for (const auto &uid : mCachedUids) { |
Eric Laurent | 47670c9 | 2019-08-28 16:59:05 -0700 | [diff] [blame] | 1100 | if (!isA11yUid(uid.first)) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1101 | continue; |
| 1102 | } |
Amith Yamasani | bcbb300 | 2019-01-23 13:53:33 -0800 | [diff] [blame] | 1103 | if (uid.second.second >= ActivityManager::PROCESS_STATE_TOP |
| 1104 | && uid.second.second <= ActivityManager::PROCESS_STATE_BOUND_FOREGROUND_SERVICE) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1105 | return true; |
| 1106 | } |
| 1107 | } |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1111 | bool AudioPolicyService::UidPolicy::isA11yUid(uid_t uid) |
| 1112 | { |
| 1113 | std::vector<uid_t>::iterator it = find(mA11yUids.begin(), mA11yUids.end(), uid); |
| 1114 | return it != mA11yUids.end(); |
| 1115 | } |
| 1116 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 1117 | // ----------- AudioPolicyService::SensorPrivacyService implementation ---------- |
| 1118 | void AudioPolicyService::SensorPrivacyPolicy::registerSelf() { |
| 1119 | SensorPrivacyManager spm; |
| 1120 | mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled(); |
| 1121 | spm.addSensorPrivacyListener(this); |
| 1122 | } |
| 1123 | |
| 1124 | void AudioPolicyService::SensorPrivacyPolicy::unregisterSelf() { |
| 1125 | SensorPrivacyManager spm; |
| 1126 | spm.removeSensorPrivacyListener(this); |
| 1127 | } |
| 1128 | |
| 1129 | bool AudioPolicyService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 1130 | return mSensorPrivacyEnabled; |
| 1131 | } |
| 1132 | |
| 1133 | binder::Status AudioPolicyService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) { |
| 1134 | mSensorPrivacyEnabled = enabled; |
| 1135 | sp<AudioPolicyService> service = mService.promote(); |
| 1136 | if (service != nullptr) { |
| 1137 | service->updateUidStates(); |
| 1138 | } |
| 1139 | return binder::Status::ok(); |
| 1140 | } |
| 1141 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1142 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 1143 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1144 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name, |
| 1145 | const wp<AudioPolicyService>& service) |
| 1146 | : Thread(false), mName(name), mService(service) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1147 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 1152 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1153 | if (!mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1154 | release_wake_lock(mName.string()); |
| 1155 | } |
| 1156 | mAudioCommands.clear(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 1160 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1161 | run(mName.string(), ANDROID_PRIORITY_AUDIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 1165 | { |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 1166 | nsecs_t waitTime = -1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1167 | |
| 1168 | mLock.lock(); |
| 1169 | while (!exitPending()) |
| 1170 | { |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1171 | sp<AudioPolicyService> svc; |
| 1172 | while (!mAudioCommands.isEmpty() && !exitPending()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1173 | nsecs_t curTime = systemTime(); |
| 1174 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 1175 | if (mAudioCommands[0]->mTime <= curTime) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1176 | sp<AudioCommand> command = mAudioCommands[0]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1177 | mAudioCommands.removeAt(0); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1178 | mLastCommand = command; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1179 | |
| 1180 | switch (command->mCommand) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1181 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1182 | VolumeData *data = (VolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1183 | ALOGV("AudioCommandThread() processing set volume stream %d, \ |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1184 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1185 | mLock.unlock(); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1186 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 1187 | data->mVolume, |
| 1188 | data->mIO); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1189 | mLock.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1190 | }break; |
| 1191 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1192 | ParametersData *data = (ParametersData *)command->mParam.get(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1193 | ALOGV("AudioCommandThread() processing set parameters string %s, io %d", |
| 1194 | data->mKeyValuePairs.string(), data->mIO); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1195 | mLock.unlock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1196 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1197 | mLock.lock(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1198 | }break; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1199 | case SET_VOICE_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1200 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1201 | ALOGV("AudioCommandThread() processing set voice volume volume %f", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1202 | data->mVolume); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1203 | mLock.unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1204 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1205 | mLock.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1206 | }break; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1207 | case STOP_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1208 | StopOutputData *data = (StopOutputData *)command->mParam.get(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1209 | ALOGV("AudioCommandThread() processing stop output portId %d", |
| 1210 | data->mPortId); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1211 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1212 | if (svc == 0) { |
| 1213 | break; |
| 1214 | } |
| 1215 | mLock.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1216 | svc->doStopOutput(data->mPortId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1217 | mLock.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1218 | }break; |
| 1219 | case RELEASE_OUTPUT: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1220 | ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1221 | ALOGV("AudioCommandThread() processing release output portId %d", |
| 1222 | data->mPortId); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1223 | svc = mService.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1224 | if (svc == 0) { |
| 1225 | break; |
| 1226 | } |
| 1227 | mLock.unlock(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1228 | svc->doReleaseOutput(data->mPortId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1229 | mLock.lock(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1230 | }break; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1231 | case CREATE_AUDIO_PATCH: { |
| 1232 | CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get(); |
| 1233 | ALOGV("AudioCommandThread() processing create audio patch"); |
| 1234 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1235 | if (af == 0) { |
| 1236 | command->mStatus = PERMISSION_DENIED; |
| 1237 | } else { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1238 | mLock.unlock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1239 | command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1240 | mLock.lock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } break; |
| 1243 | case RELEASE_AUDIO_PATCH: { |
| 1244 | ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get(); |
| 1245 | ALOGV("AudioCommandThread() processing release audio patch"); |
| 1246 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1247 | if (af == 0) { |
| 1248 | command->mStatus = PERMISSION_DENIED; |
| 1249 | } else { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1250 | mLock.unlock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1251 | command->mStatus = af->releaseAudioPatch(data->mHandle); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1252 | mLock.lock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } break; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1255 | case UPDATE_AUDIOPORT_LIST: { |
| 1256 | ALOGV("AudioCommandThread() processing update audio port list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1257 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1258 | if (svc == 0) { |
| 1259 | break; |
| 1260 | } |
| 1261 | mLock.unlock(); |
| 1262 | svc->doOnAudioPortListUpdate(); |
| 1263 | mLock.lock(); |
| 1264 | }break; |
| 1265 | case UPDATE_AUDIOPATCH_LIST: { |
| 1266 | ALOGV("AudioCommandThread() processing update audio patch list"); |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1267 | svc = mService.promote(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1268 | if (svc == 0) { |
| 1269 | break; |
| 1270 | } |
| 1271 | mLock.unlock(); |
| 1272 | svc->doOnAudioPatchListUpdate(); |
| 1273 | mLock.lock(); |
| 1274 | }break; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1275 | case CHANGED_AUDIOVOLUMEGROUP: { |
| 1276 | AudioVolumeGroupData *data = |
| 1277 | static_cast<AudioVolumeGroupData *>(command->mParam.get()); |
| 1278 | ALOGV("AudioCommandThread() processing update audio volume group"); |
| 1279 | svc = mService.promote(); |
| 1280 | if (svc == 0) { |
| 1281 | break; |
| 1282 | } |
| 1283 | mLock.unlock(); |
| 1284 | svc->doOnAudioVolumeGroupChanged(data->mGroup, data->mFlags); |
| 1285 | mLock.lock(); |
| 1286 | }break; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 1287 | case SET_AUDIOPORT_CONFIG: { |
| 1288 | SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get(); |
| 1289 | ALOGV("AudioCommandThread() processing set port config"); |
| 1290 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1291 | if (af == 0) { |
| 1292 | command->mStatus = PERMISSION_DENIED; |
| 1293 | } else { |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1294 | mLock.unlock(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 1295 | command->mStatus = af->setAudioPortConfig(&data->mConfig); |
Andy Hung | fe726a6 | 2018-09-27 15:17:25 -0700 | [diff] [blame] | 1296 | mLock.lock(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 1297 | } |
| 1298 | } break; |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1299 | case DYN_POLICY_MIX_STATE_UPDATE: { |
| 1300 | DynPolicyMixStateUpdateData *data = |
| 1301 | (DynPolicyMixStateUpdateData *)command->mParam.get(); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1302 | ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d", |
| 1303 | data->mRegId.string(), data->mState); |
| 1304 | svc = mService.promote(); |
| 1305 | if (svc == 0) { |
| 1306 | break; |
| 1307 | } |
| 1308 | mLock.unlock(); |
| 1309 | svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState); |
| 1310 | mLock.lock(); |
| 1311 | } break; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1312 | case RECORDING_CONFIGURATION_UPDATE: { |
| 1313 | RecordingConfigurationUpdateData *data = |
| 1314 | (RecordingConfigurationUpdateData *)command->mParam.get(); |
| 1315 | ALOGV("AudioCommandThread() processing recording configuration update"); |
| 1316 | svc = mService.promote(); |
| 1317 | if (svc == 0) { |
| 1318 | break; |
| 1319 | } |
| 1320 | mLock.unlock(); |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 1321 | svc->doOnRecordingConfigurationUpdate(data->mEvent, &data->mClientInfo, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1322 | &data->mClientConfig, data->mClientEffects, |
| 1323 | &data->mDeviceConfig, data->mEffects, |
| 1324 | data->mPatchHandle, data->mSource); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1325 | mLock.lock(); |
| 1326 | } break; |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1327 | case SET_EFFECT_SUSPENDED: { |
| 1328 | SetEffectSuspendedData *data = (SetEffectSuspendedData *)command->mParam.get(); |
| 1329 | ALOGV("AudioCommandThread() processing set effect suspended"); |
| 1330 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 1331 | if (af != 0) { |
| 1332 | mLock.unlock(); |
| 1333 | af->setEffectSuspended(data->mEffectId, data->mSessionId, data->mSuspended); |
| 1334 | mLock.lock(); |
| 1335 | } |
| 1336 | } break; |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 1337 | case AUDIO_MODULES_UPDATE: { |
| 1338 | ALOGV("AudioCommandThread() processing audio modules update"); |
| 1339 | svc = mService.promote(); |
| 1340 | if (svc == 0) { |
| 1341 | break; |
| 1342 | } |
| 1343 | mLock.unlock(); |
| 1344 | svc->doOnNewAudioModulesAvailable(); |
| 1345 | mLock.lock(); |
| 1346 | } break; |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1347 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1348 | default: |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1349 | ALOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1350 | } |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1351 | { |
| 1352 | Mutex::Autolock _l(command->mLock); |
| 1353 | if (command->mWaitStatus) { |
| 1354 | command->mWaitStatus = false; |
| 1355 | command->mCond.signal(); |
| 1356 | } |
| 1357 | } |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 1358 | waitTime = -1; |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 1359 | // release mLock before releasing strong reference on the service as |
| 1360 | // AudioPolicyService destructor calls AudioCommandThread::exit() which |
| 1361 | // acquires mLock. |
| 1362 | mLock.unlock(); |
| 1363 | svc.clear(); |
| 1364 | mLock.lock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1365 | } else { |
| 1366 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 1367 | break; |
| 1368 | } |
| 1369 | } |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 1370 | |
| 1371 | // release delayed commands wake lock if the queue is empty |
| 1372 | if (mAudioCommands.isEmpty()) { |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 1373 | release_wake_lock(mName.string()); |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | // At this stage we have either an empty command queue or the first command in the queue |
| 1377 | // has a finite delay. So unless we are exiting it is safe to wait. |
| 1378 | if (!exitPending()) { |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1379 | ALOGV("AudioCommandThread() going to sleep"); |
Eric Laurent | d7eda8d | 2016-02-02 17:18:39 -0800 | [diff] [blame] | 1380 | if (waitTime == -1) { |
| 1381 | mWaitWorkCV.wait(mLock); |
| 1382 | } else { |
| 1383 | mWaitWorkCV.waitRelative(mLock, waitTime); |
| 1384 | } |
Eric Laurent | 59a8923 | 2014-06-08 14:14:17 -0700 | [diff] [blame] | 1385 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1386 | } |
Ricardo Garcia | 05f2fdc | 2014-07-24 15:48:24 -0700 | [diff] [blame] | 1387 | // release delayed commands wake lock before quitting |
| 1388 | if (!mAudioCommands.isEmpty()) { |
| 1389 | release_wake_lock(mName.string()); |
| 1390 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1391 | mLock.unlock(); |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
| 1395 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
| 1396 | { |
| 1397 | const size_t SIZE = 256; |
| 1398 | char buffer[SIZE]; |
| 1399 | String8 result; |
| 1400 | |
| 1401 | snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); |
| 1402 | result.append(buffer); |
| 1403 | write(fd, result.string(), result.size()); |
| 1404 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 1405 | bool locked = dumpTryLock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1406 | if (!locked) { |
| 1407 | String8 result2(kCmdDeadlockedString); |
| 1408 | write(fd, result2.string(), result2.size()); |
| 1409 | } |
| 1410 | |
| 1411 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 1412 | result = String8(buffer); |
| 1413 | result.append(" Command Time Wait pParam\n"); |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1414 | for (size_t i = 0; i < mAudioCommands.size(); i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1415 | mAudioCommands[i]->dump(buffer, SIZE); |
| 1416 | result.append(buffer); |
| 1417 | } |
| 1418 | result.append(" Last Command\n"); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1419 | if (mLastCommand != 0) { |
| 1420 | mLastCommand->dump(buffer, SIZE); |
| 1421 | result.append(buffer); |
| 1422 | } else { |
| 1423 | result.append(" none\n"); |
| 1424 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1425 | |
| 1426 | write(fd, result.string(), result.size()); |
| 1427 | |
| 1428 | if (locked) mLock.unlock(); |
| 1429 | |
| 1430 | return NO_ERROR; |
| 1431 | } |
| 1432 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1433 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1434 | float volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1435 | audio_io_handle_t output, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1436 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1437 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1438 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1439 | command->mCommand = SET_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1440 | sp<VolumeData> data = new VolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1441 | data->mStream = stream; |
| 1442 | data->mVolume = volume; |
| 1443 | data->mIO = output; |
| 1444 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1445 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1446 | ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1447 | stream, volume, output); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1448 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1451 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1452 | const char *keyValuePairs, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1453 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1454 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1455 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1456 | command->mCommand = SET_PARAMETERS; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1457 | sp<ParametersData> data = new ParametersData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1458 | data->mIO = ioHandle; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1459 | data->mKeyValuePairs = String8(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1460 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1461 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1462 | ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1463 | keyValuePairs, ioHandle, delayMs); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1464 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 1468 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1469 | sp<AudioCommand> command = new AudioCommand(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1470 | command->mCommand = SET_VOICE_VOLUME; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1471 | sp<VoiceVolumeData> data = new VoiceVolumeData(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1472 | data->mVolume = volume; |
| 1473 | command->mParam = data; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1474 | command->mWaitStatus = true; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1475 | ALOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1476 | return sendCommand(command, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1479 | void AudioPolicyService::AudioCommandThread::setEffectSuspendedCommand(int effectId, |
| 1480 | audio_session_t sessionId, |
| 1481 | bool suspended) |
| 1482 | { |
| 1483 | sp<AudioCommand> command = new AudioCommand(); |
| 1484 | command->mCommand = SET_EFFECT_SUSPENDED; |
| 1485 | sp<SetEffectSuspendedData> data = new SetEffectSuspendedData(); |
| 1486 | data->mEffectId = effectId; |
| 1487 | data->mSessionId = sessionId; |
| 1488 | data->mSuspended = suspended; |
| 1489 | command->mParam = data; |
| 1490 | ALOGV("AudioCommandThread() adding set suspended effectId %d sessionId %d suspended %d", |
| 1491 | effectId, sessionId, suspended); |
| 1492 | sendCommand(command); |
| 1493 | } |
| 1494 | |
| 1495 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1496 | void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_port_handle_t portId) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1497 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1498 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1499 | command->mCommand = STOP_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1500 | sp<StopOutputData> data = new StopOutputData(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1501 | data->mPortId = portId; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 1502 | command->mParam = data; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1503 | ALOGV("AudioCommandThread() adding stop output portId %d", portId); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1504 | sendCommand(command); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1505 | } |
| 1506 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1507 | void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_port_handle_t portId) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1508 | { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1509 | sp<AudioCommand> command = new AudioCommand(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1510 | command->mCommand = RELEASE_OUTPUT; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1511 | sp<ReleaseOutputData> data = new ReleaseOutputData(); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1512 | data->mPortId = portId; |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 1513 | command->mParam = data; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1514 | ALOGV("AudioCommandThread() adding release output portId %d", portId); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1515 | sendCommand(command); |
| 1516 | } |
| 1517 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1518 | status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand( |
| 1519 | const struct audio_patch *patch, |
| 1520 | audio_patch_handle_t *handle, |
| 1521 | int delayMs) |
| 1522 | { |
| 1523 | status_t status = NO_ERROR; |
| 1524 | |
| 1525 | sp<AudioCommand> command = new AudioCommand(); |
| 1526 | command->mCommand = CREATE_AUDIO_PATCH; |
| 1527 | CreateAudioPatchData *data = new CreateAudioPatchData(); |
| 1528 | data->mPatch = *patch; |
| 1529 | data->mHandle = *handle; |
| 1530 | command->mParam = data; |
| 1531 | command->mWaitStatus = true; |
| 1532 | ALOGV("AudioCommandThread() adding create patch delay %d", delayMs); |
| 1533 | status = sendCommand(command, delayMs); |
| 1534 | if (status == NO_ERROR) { |
| 1535 | *handle = data->mHandle; |
| 1536 | } |
| 1537 | return status; |
| 1538 | } |
| 1539 | |
| 1540 | status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle, |
| 1541 | int delayMs) |
| 1542 | { |
| 1543 | sp<AudioCommand> command = new AudioCommand(); |
| 1544 | command->mCommand = RELEASE_AUDIO_PATCH; |
| 1545 | ReleaseAudioPatchData *data = new ReleaseAudioPatchData(); |
| 1546 | data->mHandle = handle; |
| 1547 | command->mParam = data; |
| 1548 | command->mWaitStatus = true; |
| 1549 | ALOGV("AudioCommandThread() adding release patch delay %d", delayMs); |
| 1550 | return sendCommand(command, delayMs); |
| 1551 | } |
| 1552 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1553 | void AudioPolicyService::AudioCommandThread::updateAudioPortListCommand() |
| 1554 | { |
| 1555 | sp<AudioCommand> command = new AudioCommand(); |
| 1556 | command->mCommand = UPDATE_AUDIOPORT_LIST; |
| 1557 | ALOGV("AudioCommandThread() adding update audio port list"); |
| 1558 | sendCommand(command); |
| 1559 | } |
| 1560 | |
| 1561 | void AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand() |
| 1562 | { |
| 1563 | sp<AudioCommand>command = new AudioCommand(); |
| 1564 | command->mCommand = UPDATE_AUDIOPATCH_LIST; |
| 1565 | ALOGV("AudioCommandThread() adding update audio patch list"); |
| 1566 | sendCommand(command); |
| 1567 | } |
| 1568 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1569 | void AudioPolicyService::AudioCommandThread::changeAudioVolumeGroupCommand(volume_group_t group, |
| 1570 | int flags) |
| 1571 | { |
| 1572 | sp<AudioCommand>command = new AudioCommand(); |
| 1573 | command->mCommand = CHANGED_AUDIOVOLUMEGROUP; |
| 1574 | AudioVolumeGroupData *data= new AudioVolumeGroupData(); |
| 1575 | data->mGroup = group; |
| 1576 | data->mFlags = flags; |
| 1577 | command->mParam = data; |
| 1578 | ALOGV("AudioCommandThread() adding audio volume group changed"); |
| 1579 | sendCommand(command); |
| 1580 | } |
| 1581 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 1582 | status_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand( |
| 1583 | const struct audio_port_config *config, int delayMs) |
| 1584 | { |
| 1585 | sp<AudioCommand> command = new AudioCommand(); |
| 1586 | command->mCommand = SET_AUDIOPORT_CONFIG; |
| 1587 | SetAudioPortConfigData *data = new SetAudioPortConfigData(); |
| 1588 | data->mConfig = *config; |
| 1589 | command->mParam = data; |
| 1590 | command->mWaitStatus = true; |
| 1591 | ALOGV("AudioCommandThread() adding set port config delay %d", delayMs); |
| 1592 | return sendCommand(command, delayMs); |
| 1593 | } |
| 1594 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1595 | void AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand( |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1596 | const String8& regId, int32_t state) |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1597 | { |
| 1598 | sp<AudioCommand> command = new AudioCommand(); |
| 1599 | command->mCommand = DYN_POLICY_MIX_STATE_UPDATE; |
| 1600 | DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData(); |
| 1601 | data->mRegId = regId; |
| 1602 | data->mState = state; |
| 1603 | command->mParam = data; |
| 1604 | ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d", |
| 1605 | regId.string(), state); |
| 1606 | sendCommand(command); |
| 1607 | } |
| 1608 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1609 | void AudioPolicyService::AudioCommandThread::recordingConfigurationUpdateCommand( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1610 | int event, |
| 1611 | const record_client_info_t *clientInfo, |
| 1612 | const audio_config_base_t *clientConfig, |
| 1613 | std::vector<effect_descriptor_t> clientEffects, |
| 1614 | const audio_config_base_t *deviceConfig, |
| 1615 | std::vector<effect_descriptor_t> effects, |
| 1616 | audio_patch_handle_t patchHandle, |
| 1617 | audio_source_t source) |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1618 | { |
| 1619 | sp<AudioCommand>command = new AudioCommand(); |
| 1620 | command->mCommand = RECORDING_CONFIGURATION_UPDATE; |
| 1621 | RecordingConfigurationUpdateData *data = new RecordingConfigurationUpdateData(); |
| 1622 | data->mEvent = event; |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 1623 | data->mClientInfo = *clientInfo; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 1624 | data->mClientConfig = *clientConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1625 | data->mClientEffects = clientEffects; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 1626 | data->mDeviceConfig = *deviceConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1627 | data->mEffects = effects; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1628 | data->mPatchHandle = patchHandle; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 1629 | data->mSource = source; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1630 | command->mParam = data; |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 1631 | ALOGV("AudioCommandThread() adding recording configuration update event %d, source %d uid %u", |
| 1632 | event, clientInfo->source, clientInfo->uid); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1633 | sendCommand(command); |
| 1634 | } |
| 1635 | |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 1636 | void AudioPolicyService::AudioCommandThread::audioModulesUpdateCommand() |
| 1637 | { |
| 1638 | sp<AudioCommand> command = new AudioCommand(); |
| 1639 | command->mCommand = AUDIO_MODULES_UPDATE; |
| 1640 | sendCommand(command); |
| 1641 | } |
| 1642 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1643 | status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs) |
| 1644 | { |
| 1645 | { |
| 1646 | Mutex::Autolock _l(mLock); |
| 1647 | insertCommand_l(command, delayMs); |
| 1648 | mWaitWorkCV.signal(); |
| 1649 | } |
| 1650 | Mutex::Autolock _l(command->mLock); |
| 1651 | while (command->mWaitStatus) { |
| 1652 | nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs); |
| 1653 | if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) { |
| 1654 | command->mStatus = TIMED_OUT; |
| 1655 | command->mWaitStatus = false; |
| 1656 | } |
| 1657 | } |
| 1658 | return command->mStatus; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1661 | // insertCommand_l() must be called with mLock held |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1662 | void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1663 | { |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1664 | 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] | 1665 | Vector < sp<AudioCommand> > removedCommands; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1666 | command->mTime = systemTime() + milliseconds(delayMs); |
| 1667 | |
| 1668 | // acquire wake lock to make sure delayed commands are processed |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1669 | if (mAudioCommands.isEmpty()) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1670 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); |
| 1671 | } |
| 1672 | |
| 1673 | // check same pending commands with later time stamps and eliminate them |
Ivan Lozano | 5ff158f | 2017-10-30 09:06:24 -0700 | [diff] [blame] | 1674 | for (i = (ssize_t)mAudioCommands.size()-1; i >= 0; i--) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1675 | sp<AudioCommand> command2 = mAudioCommands[i]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1676 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 1677 | if (command2->mTime <= command->mTime) break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1678 | |
| 1679 | // create audio patch or release audio patch commands are equivalent |
| 1680 | // with regard to filtering |
| 1681 | if ((command->mCommand == CREATE_AUDIO_PATCH) || |
| 1682 | (command->mCommand == RELEASE_AUDIO_PATCH)) { |
| 1683 | if ((command2->mCommand != CREATE_AUDIO_PATCH) && |
| 1684 | (command2->mCommand != RELEASE_AUDIO_PATCH)) { |
| 1685 | continue; |
| 1686 | } |
| 1687 | } else if (command2->mCommand != command->mCommand) continue; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1688 | |
| 1689 | switch (command->mCommand) { |
| 1690 | case SET_PARAMETERS: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1691 | ParametersData *data = (ParametersData *)command->mParam.get(); |
| 1692 | ParametersData *data2 = (ParametersData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1693 | if (data->mIO != data2->mIO) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1694 | ALOGV("Comparing parameter command %s to new command %s", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1695 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1696 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 1697 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 1698 | for (size_t j = 0; j < param.size(); j++) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1699 | String8 key; |
| 1700 | String8 value; |
| 1701 | param.getAt(j, key, value); |
| 1702 | for (size_t k = 0; k < param2.size(); k++) { |
| 1703 | String8 key2; |
| 1704 | String8 value2; |
| 1705 | param2.getAt(k, key2, value2); |
| 1706 | if (key2 == key) { |
| 1707 | param2.remove(key2); |
| 1708 | ALOGV("Filtering out parameter %s", key2.string()); |
| 1709 | break; |
| 1710 | } |
| 1711 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1712 | } |
| 1713 | // if all keys have been filtered out, remove the command. |
| 1714 | // otherwise, update the key value pairs |
| 1715 | if (param2.size() == 0) { |
| 1716 | removedCommands.add(command2); |
| 1717 | } else { |
| 1718 | data2->mKeyValuePairs = param2.toString(); |
| 1719 | } |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 1720 | command->mTime = command2->mTime; |
| 1721 | // force delayMs to non 0 so that code below does not request to wait for |
| 1722 | // command status as the command is now delayed |
| 1723 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1724 | } break; |
| 1725 | |
| 1726 | case SET_VOLUME: { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1727 | VolumeData *data = (VolumeData *)command->mParam.get(); |
| 1728 | VolumeData *data2 = (VolumeData *)command2->mParam.get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1729 | if (data->mIO != data2->mIO) break; |
| 1730 | if (data->mStream != data2->mStream) break; |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1731 | ALOGV("Filtering out volume command on output %d for stream %d", |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1732 | data->mIO, data->mStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1733 | removedCommands.add(command2); |
Eric Laurent | 21e5456 | 2013-09-23 12:08:05 -0700 | [diff] [blame] | 1734 | command->mTime = command2->mTime; |
| 1735 | // force delayMs to non 0 so that code below does not request to wait for |
| 1736 | // command status as the command is now delayed |
| 1737 | delayMs = 1; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1738 | } break; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1739 | |
Eric Laurent | baf35fe | 2016-07-27 15:36:53 -0700 | [diff] [blame] | 1740 | case SET_VOICE_VOLUME: { |
| 1741 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get(); |
| 1742 | VoiceVolumeData *data2 = (VoiceVolumeData *)command2->mParam.get(); |
| 1743 | ALOGV("Filtering out voice volume command value %f replaced by %f", |
| 1744 | data2->mVolume, data->mVolume); |
| 1745 | removedCommands.add(command2); |
| 1746 | command->mTime = command2->mTime; |
| 1747 | // force delayMs to non 0 so that code below does not request to wait for |
| 1748 | // command status as the command is now delayed |
| 1749 | delayMs = 1; |
| 1750 | } break; |
| 1751 | |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1752 | case CREATE_AUDIO_PATCH: |
| 1753 | case RELEASE_AUDIO_PATCH: { |
| 1754 | audio_patch_handle_t handle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 1755 | struct audio_patch patch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1756 | if (command->mCommand == CREATE_AUDIO_PATCH) { |
| 1757 | handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 1758 | patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1759 | } else { |
| 1760 | handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle; |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 1761 | memset(&patch, 0, sizeof(patch)); |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1762 | } |
| 1763 | audio_patch_handle_t handle2; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 1764 | struct audio_patch patch2; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1765 | if (command2->mCommand == CREATE_AUDIO_PATCH) { |
| 1766 | handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 1767 | patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch; |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1768 | } else { |
| 1769 | handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle; |
Glenn Kasten | f60b6b6 | 2015-07-06 10:53:26 -0700 | [diff] [blame] | 1770 | memset(&patch2, 0, sizeof(patch2)); |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1771 | } |
| 1772 | if (handle != handle2) break; |
Haynes Mathew George | a2d4a6d | 2014-10-13 13:05:22 -0700 | [diff] [blame] | 1773 | /* Filter CREATE_AUDIO_PATCH commands only when they are issued for |
| 1774 | same output. */ |
| 1775 | if( (command->mCommand == CREATE_AUDIO_PATCH) && |
| 1776 | (command2->mCommand == CREATE_AUDIO_PATCH) ) { |
| 1777 | bool isOutputDiff = false; |
| 1778 | if (patch.num_sources == patch2.num_sources) { |
| 1779 | for (unsigned count = 0; count < patch.num_sources; count++) { |
| 1780 | if (patch.sources[count].id != patch2.sources[count].id) { |
| 1781 | isOutputDiff = true; |
| 1782 | break; |
| 1783 | } |
| 1784 | } |
| 1785 | if (isOutputDiff) |
| 1786 | break; |
| 1787 | } |
| 1788 | } |
Eric Laurent | e45b48a | 2014-09-04 16:40:57 -0700 | [diff] [blame] | 1789 | ALOGV("Filtering out %s audio patch command for handle %d", |
| 1790 | (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle); |
| 1791 | removedCommands.add(command2); |
| 1792 | command->mTime = command2->mTime; |
| 1793 | // force delayMs to non 0 so that code below does not request to wait for |
| 1794 | // command status as the command is now delayed |
| 1795 | delayMs = 1; |
| 1796 | } break; |
| 1797 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1798 | case DYN_POLICY_MIX_STATE_UPDATE: { |
| 1799 | |
| 1800 | } break; |
| 1801 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1802 | case RECORDING_CONFIGURATION_UPDATE: { |
| 1803 | |
| 1804 | } break; |
| 1805 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1806 | default: |
| 1807 | break; |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | // remove filtered commands |
| 1812 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 1813 | // removed commands always have time stamps greater than current command |
| 1814 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1815 | if (mAudioCommands[k].get() == removedCommands[j].get()) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1816 | ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1817 | mAudioCommands.removeAt(k); |
| 1818 | break; |
| 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | removedCommands.clear(); |
| 1823 | |
Eric Laurent | aa79bef | 2015-01-15 14:33:51 -0800 | [diff] [blame] | 1824 | // Disable wait for status if delay is not 0. |
| 1825 | // Except for create audio patch command because the returned patch handle |
| 1826 | // is needed by audio policy manager |
| 1827 | if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) { |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 1828 | command->mWaitStatus = false; |
| 1829 | } |
Eric Laurent | cec4abb | 2012-07-03 12:23:02 -0700 | [diff] [blame] | 1830 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1831 | // insert command at the right place according to its time stamp |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 1832 | ALOGV("inserting command: %d at index %zd, num commands %zu", |
| 1833 | command->mCommand, i+1, mAudioCommands.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1834 | mAudioCommands.insertAt(command, i + 1); |
| 1835 | } |
| 1836 | |
| 1837 | void AudioPolicyService::AudioCommandThread::exit() |
| 1838 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1839 | ALOGV("AudioCommandThread::exit"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1840 | { |
| 1841 | AutoMutex _l(mLock); |
| 1842 | requestExit(); |
| 1843 | mWaitWorkCV.signal(); |
| 1844 | } |
Zach Jang | a754b4f | 2015-10-27 01:29:34 +0000 | [diff] [blame] | 1845 | // Note that we can call it from the thread loop if all other references have been released |
| 1846 | // but it will safely return WOULD_BLOCK in this case |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1847 | requestExitAndWait(); |
| 1848 | } |
| 1849 | |
| 1850 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 1851 | { |
| 1852 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
| 1853 | mCommand, |
| 1854 | (int)ns2s(mTime), |
| 1855 | (int)ns2ms(mTime)%1000, |
| 1856 | mWaitStatus, |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 1857 | mParam.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1860 | /******* helpers for the service_ops callbacks defined below *********/ |
| 1861 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 1862 | const char *keyValuePairs, |
| 1863 | int delayMs) |
| 1864 | { |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1865 | mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1866 | delayMs); |
| 1867 | } |
| 1868 | |
| 1869 | int AudioPolicyService::setStreamVolume(audio_stream_type_t stream, |
| 1870 | float volume, |
| 1871 | audio_io_handle_t output, |
| 1872 | int delayMs) |
| 1873 | { |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1874 | return (int)mAudioCommandThread->volumeCommand(stream, volume, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1875 | output, delayMs); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1878 | int AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 1879 | { |
| 1880 | return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 1881 | } |
| 1882 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1883 | void AudioPolicyService::setEffectSuspended(int effectId, |
| 1884 | audio_session_t sessionId, |
| 1885 | bool suspended) |
| 1886 | { |
| 1887 | mAudioCommandThread->setEffectSuspendedCommand(effectId, sessionId, suspended); |
| 1888 | } |
| 1889 | |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 1890 | void AudioPolicyService::onNewAudioModulesAvailable() |
| 1891 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1892 | mOutputCommandThread->audioModulesUpdateCommand(); |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1895 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1896 | extern "C" { |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1897 | audio_module_handle_t aps_load_hw_module(void *service __unused, |
| 1898 | const char *name); |
| 1899 | audio_io_handle_t aps_open_output(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1900 | audio_devices_t *pDevices, |
| 1901 | uint32_t *pSamplingRate, |
| 1902 | audio_format_t *pFormat, |
| 1903 | audio_channel_mask_t *pChannelMask, |
| 1904 | uint32_t *pLatencyMs, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1905 | audio_output_flags_t flags); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1906 | |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1907 | audio_io_handle_t aps_open_output_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1908 | audio_module_handle_t module, |
| 1909 | audio_devices_t *pDevices, |
| 1910 | uint32_t *pSamplingRate, |
| 1911 | audio_format_t *pFormat, |
| 1912 | audio_channel_mask_t *pChannelMask, |
| 1913 | uint32_t *pLatencyMs, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1914 | audio_output_flags_t flags, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1915 | const audio_offload_info_t *offloadInfo); |
| 1916 | audio_io_handle_t aps_open_dup_output(void *service __unused, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1917 | audio_io_handle_t output1, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1918 | audio_io_handle_t output2); |
| 1919 | int aps_close_output(void *service __unused, audio_io_handle_t output); |
| 1920 | int aps_suspend_output(void *service __unused, audio_io_handle_t output); |
| 1921 | int aps_restore_output(void *service __unused, audio_io_handle_t output); |
| 1922 | audio_io_handle_t aps_open_input(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1923 | audio_devices_t *pDevices, |
| 1924 | uint32_t *pSamplingRate, |
| 1925 | audio_format_t *pFormat, |
| 1926 | audio_channel_mask_t *pChannelMask, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1927 | audio_in_acoustics_t acoustics __unused); |
| 1928 | audio_io_handle_t aps_open_input_on_module(void *service __unused, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1929 | audio_module_handle_t module, |
| 1930 | audio_devices_t *pDevices, |
| 1931 | uint32_t *pSamplingRate, |
| 1932 | audio_format_t *pFormat, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1933 | audio_channel_mask_t *pChannelMask); |
| 1934 | int aps_close_input(void *service __unused, audio_io_handle_t input); |
| 1935 | int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1936 | int aps_move_effects(void *service __unused, audio_session_t session, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1937 | audio_io_handle_t src_output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1938 | audio_io_handle_t dst_output); |
| 1939 | char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle, |
| 1940 | const char *keys); |
| 1941 | void aps_set_parameters(void *service, audio_io_handle_t io_handle, |
| 1942 | const char *kv_pairs, int delay_ms); |
| 1943 | int aps_set_stream_volume(void *service, audio_stream_type_t stream, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1944 | float volume, audio_io_handle_t output, |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1945 | int delay_ms); |
Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1946 | int aps_set_voice_volume(void *service, float volume, int delay_ms); |
| 1947 | }; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1948 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 1949 | } // namespace android |