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