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