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