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