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