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