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