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