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