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