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