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