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