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